Handle mangling of anonymous types, which only have a typedef name.
[cparser] / mangle.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include <libfirm/firm.h>
23 #include <string.h>
24
25 #include "entity_t.h"
26 #include "type_t.h"
27 #include "symbol_t.h"
28 #include "mangle.h"
29 #include "diagnostic.h"
30 #include "ast2firm.h"
31 #include "lang_features.h"
32 #include "adt/error.h"
33
34 static struct obstack obst;
35
36 static void mangle_type(type_t *type);
37
38 static char get_atomic_type_mangle(atomic_type_kind_t kind)
39 {
40         switch (kind) {
41         case ATOMIC_TYPE_INVALID: break;
42         case ATOMIC_TYPE_VOID:        return 'v';
43         case ATOMIC_TYPE_BOOL:        return 'b';
44         case ATOMIC_TYPE_CHAR:        return 'c';
45         case ATOMIC_TYPE_SCHAR:       return 'a';
46         case ATOMIC_TYPE_UCHAR:       return 'h';
47         case ATOMIC_TYPE_INT:         return 'i';
48         case ATOMIC_TYPE_UINT:        return 'j';
49         case ATOMIC_TYPE_SHORT:       return 's';
50         case ATOMIC_TYPE_USHORT:      return 't';
51         case ATOMIC_TYPE_LONG:        return 'l';
52         case ATOMIC_TYPE_ULONG:       return 'm';
53         case ATOMIC_TYPE_LONGLONG:    return 'x';
54         case ATOMIC_TYPE_ULONGLONG:   return 'y';
55         case ATOMIC_TYPE_LONG_DOUBLE: return 'e';
56         case ATOMIC_TYPE_FLOAT:       return 'f';
57         case ATOMIC_TYPE_DOUBLE:      return 'd';
58         }
59         panic("invalid atomic type in mangler");
60 }
61
62 static void mangle_atomic_type(const atomic_type_t *type)
63 {
64         obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
65 }
66
67 static void mangle_pointer_type(const pointer_type_t *type)
68 {
69         obstack_1grow(&obst, 'P');
70         mangle_type(type->points_to);
71 }
72
73 static void mangle_parameters(const function_type_t *type)
74 {
75         if (type->unspecified_parameters)
76                 panic("can't mangle unspecified parameter types");
77         if (type->kr_style_parameters)
78                 panic("can't mangle kr_style_parameters type");
79
80         const function_parameter_t *parameter = type->parameters;
81         if (parameter != NULL) {
82                 for ( ; parameter != NULL; parameter = parameter->next) {
83                         mangle_type(parameter->type);
84                 }
85                 if (type->variadic) {
86                         obstack_1grow(&obst, 'z');
87                 }
88         } else {
89                 obstack_1grow(&obst, 'v');
90         }
91 }
92
93 static void mangle_function_type(const function_type_t *type)
94 {
95         obstack_1grow(&obst, 'F');
96         if (type->linkage == LINKAGE_C) {
97                 obstack_1grow(&obst, 'Y');
98         }
99
100         mangle_type(type->return_type);
101         mangle_parameters(type);
102
103         obstack_1grow(&obst, 'E');
104 }
105
106 static void print_name(const char* name)
107 {
108         obstack_printf(&obst, "%u%s", (unsigned)strlen(name), name);
109 }
110
111 static void mangle_class_type(const compound_type_t *type)
112 {
113         const symbol_t *sym = type->compound->base.symbol;
114         if (sym == NULL) {
115                 if (type->compound->alias == NULL)
116                         panic("mangling anonymous type");
117                 sym = type->compound->alias->base.symbol;
118         }
119         print_name(sym->string);
120 }
121
122 static void mangle_enum_type(const enum_type_t *type)
123 {
124         const symbol_t *sym = type->enume->base.symbol;
125         if (sym == NULL) {
126                 if (type->enume->alias == NULL)
127                         panic("mangling anonymous type");
128                 sym = type->enume->alias->base.symbol;
129         }
130         print_name(sym->string);
131 }
132
133 static void mangle_array_type(const array_type_t *type)
134 {
135         if (type->is_vla) {
136                 obstack_1grow(&obst, 'A');
137                 obstack_1grow(&obst, '_');
138         } else if (type->size_constant) {
139                 obstack_printf(&obst, "A%u_", (unsigned) type->size);
140         } else {
141                 panic("mangling of non-constant sized array types not implemented yet");
142         }
143         mangle_type(type->element_type);
144 }
145
146 static void mangle_complex_type(const complex_type_t *type)
147 {
148         obstack_1grow(&obst, 'C');
149         obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
150 }
151
152 static void mangle_imaginary_type(const imaginary_type_t *type)
153 {
154         obstack_1grow(&obst, 'G');
155         obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
156 }
157
158 static void mangle_qualifiers(type_qualifiers_t qualifiers)
159 {
160         if (qualifiers & TYPE_QUALIFIER_RESTRICT)
161                 obstack_1grow(&obst, 'r');
162         if (qualifiers & TYPE_QUALIFIER_VOLATILE)
163                 obstack_1grow(&obst, 'V');
164         if (qualifiers & TYPE_QUALIFIER_CONST)
165                 obstack_1grow(&obst, 'K');
166
167         /* handle MS extended qualifiers? */
168 }
169
170 static void mangle_type(type_t *orig_type)
171 {
172         type_t *type = skip_typeref(orig_type);
173
174         mangle_qualifiers(type->base.qualifiers);
175
176         switch (type->kind) {
177         case TYPE_ATOMIC:
178                 mangle_atomic_type(&type->atomic);
179                 return;
180         case TYPE_POINTER:
181                 mangle_pointer_type(&type->pointer);
182                 return;
183         case TYPE_FUNCTION:
184                 mangle_function_type(&type->function);
185                 return;
186         case TYPE_COMPOUND_STRUCT:
187         case TYPE_COMPOUND_UNION:
188                 mangle_class_type(&type->compound);
189                 return;
190         case TYPE_ENUM:
191                 mangle_enum_type(&type->enumt);
192                 return;
193         case TYPE_ARRAY:
194                 mangle_array_type(&type->array);
195                 return;
196         case TYPE_COMPLEX:
197                 mangle_complex_type(&type->complex);
198                 return;
199         case TYPE_IMAGINARY:
200                 mangle_imaginary_type(&type->imaginary);
201                 return;
202         case TYPE_INVALID:
203                 panic("invalid type encountered while mangling");
204         case TYPE_ERROR:
205                 panic("error type encountered while mangling");
206         case TYPE_BUILTIN:
207         case TYPE_TYPEDEF:
208         case TYPE_TYPEOF:
209                 panic("typeref not resolved while manging?!?");
210
211         case TYPE_BITFIELD:
212                 panic("no mangling for this type implemented yet");
213                 break;
214         }
215         panic("invalid type encountered while mangling");
216 }
217
218 static void mangle_entity(entity_t *entity)
219 {
220         obstack_1grow(&obst, '_');
221         obstack_1grow(&obst, 'Z');
222
223         /* TODO: mangle scope */
224
225         print_name(entity->base.symbol->string);
226
227         if (entity->kind == ENTITY_FUNCTION) {
228                 mangle_parameters(&entity->declaration.type->function);
229         }
230 }
231
232 static ident *make_id_from_obst(void)
233 {
234         size_t  size = obstack_object_size(&obst);
235         char   *str  = obstack_finish(&obst);
236         ident  *id   = new_id_from_chars(str, size);
237         obstack_free(&obst, str);
238         return id;
239 }
240
241 /**
242  * Mangles an entity linker (ld) name for win32 usage.
243  *
244  * @param ent          the entity to be mangled
245  * @param declaration  the declaration
246  */
247 ident *create_name_win32(entity_t *entity)
248 {
249         struct obstack *o = &obst;
250
251         assert(is_declaration(entity));
252
253         if (entity->declaration.modifiers & DM_DLLIMPORT) {
254                 /* add prefix for imported symbols */
255                 obstack_printf(o, "__imp_");
256         }
257
258         if (entity->kind == ENTITY_FUNCTION) {
259                 cc_kind_t cc = entity->declaration.type->function.calling_convention;
260
261                 /* calling convention prefix */
262                 switch (cc) {
263                         case CC_DEFAULT:
264                         case CC_CDECL:
265                         case CC_STDCALL:  obstack_1grow(o, '_'); break;
266                         case CC_FASTCALL: obstack_1grow(o, '@'); break;
267                         default:          panic("unhandled calling convention");
268                 }
269
270                 switch (entity->declaration.type->function.linkage) {
271                         case LINKAGE_INVALID:
272                                 break;
273
274                         case LINKAGE_C:
275                                 obstack_printf(o, "%s", entity->base.symbol->string);
276                                 break;
277
278                         case LINKAGE_CXX:
279                                 mangle_entity(entity);
280                                 break;
281                 }
282
283                 /* calling convention suffix */
284                 switch (cc) {
285                         case CC_DEFAULT:
286                         case CC_CDECL:
287                                 break;
288
289                         case CC_STDCALL:
290                         case CC_FASTCALL: {
291                                 ir_type  *irtype = get_ir_type(entity->declaration.type);
292                                 unsigned size    = 0;
293                                 for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
294                                         size += get_type_size_bytes(get_method_param_type(irtype, i));
295                                 }
296                                 obstack_printf(o, "@%u", size);
297                                 break;
298                         }
299
300                         default:
301                                 panic("unhandled calling convention");
302                 }
303         } else {
304                 obstack_printf(o, "_%s", entity->base.symbol->string);
305         }
306
307         return make_id_from_obst();
308 }
309
310 /**
311  * Mangles an entity linker (ld) name for Linux ELF usage.
312  *
313  * @param ent          the entity to be mangled
314  * @param declaration  the declaration
315  */
316 ident *create_name_linux_elf(entity_t *entity)
317 {
318         bool needs_mangling = false;
319
320         if (entity->kind == ENTITY_FUNCTION) {
321                 switch (entity->declaration.type->function.linkage) {
322                         case LINKAGE_INVALID: break;
323                         case LINKAGE_C:       break;
324                         case LINKAGE_CXX:     needs_mangling = true; break;
325                 }
326         }
327
328         if (needs_mangling) {
329                 mangle_entity(entity);
330                 return make_id_from_obst();
331         }
332
333         return new_id_from_str(entity->base.symbol->string);
334 }
335
336 /**
337  * Mangles an entity linker (ld) name for Mach-O usage.
338  *
339  * @param ent          the entity to be mangled
340  * @param declaration  the declaration
341  */
342 ident *create_name_macho(entity_t *entity)
343 {
344         obstack_printf(&obst, "_%s", entity->base.symbol->string);
345         return make_id_from_obst();
346 }
347
348 void init_mangle(void)
349 {
350         obstack_init(&obst);
351 }
352
353 void exit_mangle(void)
354 {
355         obstack_free(&obst, NULL);
356 }