libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
mangle.c
Go to the documentation of this file.
1// Copyright (C) 2023 Ethan Uppal. All rights reserved.
2
3#include <string.h> // strlen
4#include "mangle.h"
5#include "util/itoa.h" // ps_ztoa
6#include "enum/make.h"
7
8static STR ps_types_mangle[] = {
9#define ENUM TO_STRING2
11#undef ENUM
12};
13
14void ps_mangle_id(struct ps_dynstr** dstr, STR str) {
15 ps_dynstr_append(dstr, ps_ztoa(strlen(str)));
16 ps_dynstr_append(dstr, str);
17}
18
19void ps_mangle_type(struct ps_dynstr** dstr, struct ps_type* type) {
20 if (ps_type_is_prim(type->type)) {
21 ps_dynstr_append(dstr, ps_types_mangle[type->type]);
22 } else {
23 switch (type->type) {
24 case PS_TYPE_TUPLE: {
25 auto_t tuple = type->value.tuple;
26 ps_dynstr_append(dstr, ps_types_mangle[PS_TYPE_TUPLE]);
27 ps_dynstr_append(dstr, ps_ztoa(tuple->length));
28 ps_dynstr_append(dstr, "_");
29 for (usize i = 0; i < tuple->length; i++) {
30 ps_mangle_type(dstr, $arr(tuple)[i]);
31 }
32 break;
33 }
34 case PS_TYPE_STRUCT: {
35 auto_t struct_ = &type->value.struct_;
36 ps_dynstr_append(dstr, ps_ztoa(struct_->name->length));
37 ps_dynstr_append(dstr, struct_->name->start);
38 break;
39 }
40 case PS_TYPE_ENUM: {
41 auto_t enum_ = &type->value.enum_;
42 ps_dynstr_append(dstr, ps_ztoa(enum_->name->length));
43 ps_dynstr_append(dstr, enum_->name->start);
44 break;
45 }
46 case PS_TYPE_TRAIT: {
47 PS_NO_IMPL();
48 }
49 case PS_TYPE_UNRESOLVED: {
50 auto_t unresolved = type->value.unresolved;
51 ps_dynstr_append(dstr, ps_ztoa(unresolved->length));
52 ps_dynstr_append(dstr, unresolved->start);
53 break;
54 }
55 default: {
56 PS_NO_IMPL();
57 }
58 }
59 }
60}
#define PS_NO_IMPL()
Aborts the program due to a function taking a path not yet implemented.
Definition abort.h:70
#define STR
Definition def.h:40
#define auto_t
Definition def.h:53
#define usize
Definition def.h:50
#define ps_dynstr_append(dstrptr, str)
Appends a string str to the struct ps_dynstr string pointed to by dstrptr.
Definition dynstr.h:26
STR ps_ztoa(usize v)
Returns a read-only string of the digits of v.
Definition itoa.c:40
Conversion from integers to strings.
Retrieval of associated data from custom enum (see enum.h).
void ps_mangle_id(struct ps_dynstr **dstr, STR str)
Appends the mangled version of the given string str to dstr.
Definition mangle.c:14
void ps_mangle_type(struct ps_dynstr **dstr, struct ps_type *type)
Appends the mangled version of the given type type to dstr.
Definition mangle.c:19
Name mangling and demangling.
Definition type.h:66
struct ps_type_enum enum_
Definition type.h:71
struct ps_type_struct struct_
Definition type.h:70
struct ps_type_tuple * tuple
Definition type.h:69
union ps_type::@0 value
struct ps_token * unresolved
Definition type.h:73
enum ps_type_type type
Definition type.h:67
bool ps_type_is_prim(enum ps_type_type type)
Returns whether type represents a primitive type.
Definition type.c:19