-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmangle.c
233 lines (207 loc) · 4.84 KB
/
mangle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <config.h>
#include <stdbool.h>
#include "mangle.h"
#include "ast_t.h"
#include "type_t.h"
#include "adt/error.h"
#include <libfirm/firm.h>
static struct obstack obst;
static bool add_underscore_prefix;
static void mangle_string(const char *str)
{
size_t len = strlen(str);
obstack_grow(&obst, str, len);
}
static void mangle_len_string(const char *string)
{
size_t len = strlen(string);
obstack_printf(&obst, "%zu%s", len, string);
}
static void mangle_atomic_type(const atomic_type_t *type)
{
char c;
switch (type->akind) {
case ATOMIC_TYPE_INVALID:
abort();
break;
case ATOMIC_TYPE_BOOL:
c = 'b';
break;
case ATOMIC_TYPE_BYTE:
c = 'c';
break;
case ATOMIC_TYPE_UBYTE:
c = 'h';
break;
case ATOMIC_TYPE_INT:
c = 'i';
break;
case ATOMIC_TYPE_UINT:
c = 'j';
break;
case ATOMIC_TYPE_SHORT:
c = 's';
break;
case ATOMIC_TYPE_USHORT:
c = 't';
break;
case ATOMIC_TYPE_LONG:
c = 'l';
break;
case ATOMIC_TYPE_ULONG:
c = 'm';
break;
case ATOMIC_TYPE_LONGLONG:
c = 'n';
break;
case ATOMIC_TYPE_ULONGLONG:
c = 'o';
break;
case ATOMIC_TYPE_FLOAT:
c = 'f';
break;
case ATOMIC_TYPE_DOUBLE:
c = 'd';
break;
default:
abort();
break;
}
obstack_1grow(&obst, c);
}
void set_add_underscore_prefix(bool new_add_underscore_prefix)
{
add_underscore_prefix = new_add_underscore_prefix;
}
static void mangle_type_variables(type_variable_t *type_variables)
{
type_variable_t *type_variable = type_variables;
for ( ; type_variable != NULL; type_variable = type_variable->next) {
/* is this a good char? */
obstack_1grow(&obst, 'T');
mangle_type(type_variable->current_type);
}
}
static void mangle_compound_type(const compound_type_t *type)
{
mangle_len_string(type->symbol->string);
mangle_type_variables(type->type_parameters);
}
static void mangle_pointer_type(const pointer_type_t *type)
{
obstack_1grow(&obst, 'P');
mangle_type(type->points_to);
}
static void mangle_array_type(const array_type_t *type)
{
obstack_1grow(&obst, 'A');
mangle_type(type->element_type);
int size = fold_constant_to_int(type->size_expression);
obstack_printf(&obst, "%lu", size);
}
static void mangle_function_type(const function_type_t *type)
{
obstack_1grow(&obst, 'F');
mangle_type(type->result_type);
function_parameter_type_t *parameter_type = type->parameter_types;
while (parameter_type != NULL) {
mangle_type(parameter_type->type);
}
obstack_1grow(&obst, 'E');
}
static void mangle_reference_type_variable(const type_reference_t* ref)
{
type_variable_t *type_var = ref->type_variable;
type_t *current_type = type_var->current_type;
if (current_type == NULL) {
panic("can't mangle unbound type variable");
}
mangle_type(current_type);
}
static void mangle_bind_typevariables(const bind_typevariables_type_t *type)
{
compound_type_t *polymorphic_type = type->polymorphic_type;
int old_top = typevar_binding_stack_top();
push_type_variable_bindings(polymorphic_type->type_parameters,
type->type_arguments);
mangle_type((type_t*) polymorphic_type);
pop_type_variable_bindings(old_top);
}
void mangle_type(const type_t *type)
{
switch (type->kind) {
case TYPE_INVALID:
break;
case TYPE_VOID:
obstack_1grow(&obst, 'v');
return;
case TYPE_ATOMIC:
mangle_atomic_type((const atomic_type_t*) type);
return;
case TYPE_TYPEOF: {
const typeof_type_t *typeof_type = (const typeof_type_t*) type;
mangle_type(typeof_type->expression->base.type);
return;
}
case TYPE_COMPOUND_UNION:
case TYPE_COMPOUND_STRUCT:
mangle_compound_type((const compound_type_t*) type);
return;
case TYPE_FUNCTION:
mangle_function_type((const function_type_t*) type);
return;
case TYPE_POINTER:
mangle_pointer_type((const pointer_type_t*) type);
return;
case TYPE_ARRAY:
mangle_array_type((const array_type_t*) type);
return;
case TYPE_REFERENCE:
panic("can't mangle unresolved type reference");
return;
case TYPE_BIND_TYPEVARIABLES:
mangle_bind_typevariables((const bind_typevariables_type_t*) type);
return;
case TYPE_REFERENCE_TYPE_VARIABLE:
mangle_reference_type_variable((const type_reference_t*) type);
return;
case TYPE_ERROR:
panic("trying to mangle error type");
}
panic("Unknown type mangled");
}
void mangle_symbol_simple(symbol_t *symbol)
{
mangle_string(symbol->string);
}
void mangle_symbol(symbol_t *symbol)
{
mangle_len_string(symbol->string);
}
void mangle_concept_name(symbol_t *symbol)
{
obstack_grow(&obst, "tcv", 3);
mangle_len_string(symbol->string);
}
void start_mangle(void)
{
if (add_underscore_prefix) {
obstack_1grow(&obst, '_');
}
}
ident *finish_mangle(void)
{
size_t size = obstack_object_size(&obst);
char *str = obstack_finish(&obst);
ident *id = new_id_from_chars(str, size);
obstack_free(&obst, str);
return id;
}
void init_mangle(void)
{
obstack_init(&obst);
}
void exit_mangle(void)
{
obstack_free(&obst, NULL);
}