ast2firm: Implement casting from complex to real types.
[cparser] / attribute.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #include <config.h>
6
7 #include <assert.h>
8 #include "adt/strutil.h"
9 #include "diagnostic.h"
10 #include "warning.h"
11 #include "attribute_t.h"
12 #include "symbol_t.h"
13 #include "adt/error.h"
14 #include "type_t.h"
15
16 static const char *const attribute_names[ATTRIBUTE_LAST+1] = {
17         [ATTRIBUTE_GNU_ALIAS]                  = "alias",
18         [ATTRIBUTE_GNU_ALIGNED]                = "aligned",
19         [ATTRIBUTE_GNU_ALWAYS_INLINE]          = "always_inline",
20         [ATTRIBUTE_GNU_CDECL]                  = "cdecl",
21         [ATTRIBUTE_GNU_COMMON]                 = "common",
22         [ATTRIBUTE_GNU_CONST]                  = "const",
23         [ATTRIBUTE_GNU_CONSTRUCTOR]            = "constructor",
24         [ATTRIBUTE_GNU_DEPRECATED]             = "deprecated",
25         [ATTRIBUTE_GNU_DESTRUCTOR]             = "destructor",
26         [ATTRIBUTE_GNU_DLLEXPORT]              = "dllexport",
27         [ATTRIBUTE_GNU_DLLIMPORT]              = "dllimport",
28         [ATTRIBUTE_GNU_EIGTHBIT_DATA]          = "eightbit_data",
29         [ATTRIBUTE_GNU_EXTERNALLY_VISIBLE]     = "externally_visible",
30         [ATTRIBUTE_GNU_FAR]                    = "far",
31         [ATTRIBUTE_GNU_FASTCALL]               = "fastcall",
32         [ATTRIBUTE_GNU_FLATTEN]                = "flatten",
33         [ATTRIBUTE_GNU_FORMAT_ARG]             = "format_arg",
34         [ATTRIBUTE_GNU_FORMAT]                 = "format",
35         [ATTRIBUTE_GNU_FUNCTION_VECTOR]        = "function_vector",
36         [ATTRIBUTE_GNU_GCC_STRUCT]             = "gcc_struct",
37         [ATTRIBUTE_GNU_INTERRUPT_HANDLER]      = "interrupt_handler",
38         [ATTRIBUTE_GNU_INTERRUPT]              = "interrupt",
39         [ATTRIBUTE_GNU_LEAF]                   = "leaf",
40         [ATTRIBUTE_GNU_LONGCALL]               = "longcall",
41         [ATTRIBUTE_GNU_LONG_CALL]              = "long_call",
42         [ATTRIBUTE_GNU_MALLOC]                 = "malloc",
43         [ATTRIBUTE_GNU_MAY_ALIAS]              = "may_alias",
44         [ATTRIBUTE_GNU_MODEL]                  = "model",
45         [ATTRIBUTE_GNU_MODE]                   = "mode",
46         [ATTRIBUTE_GNU_MS_STRUCT]              = "ms_struct",
47         [ATTRIBUTE_GNU_NAKED]                  = "naked",
48         [ATTRIBUTE_GNU_NEAR]                   = "near",
49         [ATTRIBUTE_GNU_NESTING]                = "nesting",
50         [ATTRIBUTE_GNU_NMI_HANDLER]            = "nmi_handler",
51         [ATTRIBUTE_GNU_NOCOMMON]               = "nocommon",
52         [ATTRIBUTE_GNU_NOINLINE]               = "noinline",
53         [ATTRIBUTE_GNU_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
54         [ATTRIBUTE_GNU_NONNULL]                = "nonnull",
55         [ATTRIBUTE_GNU_NORETURN]               = "noreturn",
56         [ATTRIBUTE_GNU_NOTHROW]                = "nothrow",
57         [ATTRIBUTE_GNU_NOTSHARED]              = "notshared",
58         [ATTRIBUTE_GNU_PACKED]                 = "packed",
59         [ATTRIBUTE_GNU_PURE]                   = "pure",
60         [ATTRIBUTE_GNU_REGPARM]                = "regparm",
61         [ATTRIBUTE_GNU_RETURNS_TWICE]          = "returns_twice",
62         [ATTRIBUTE_GNU_SAVEALL]                = "saveall",
63         [ATTRIBUTE_GNU_SECTION]                = "section",
64         [ATTRIBUTE_GNU_SENTINEL]               = "sentinel",
65         [ATTRIBUTE_GNU_SHARED]                 = "shared",
66         [ATTRIBUTE_GNU_SHORTCALL]              = "shortcall",
67         [ATTRIBUTE_GNU_SHORT_CALL]             = "short_call",
68         [ATTRIBUTE_GNU_SIGNAL]                 = "signal",
69         [ATTRIBUTE_GNU_SP_SWITCH]              = "sp_switch",
70         [ATTRIBUTE_GNU_SSEREGPARM]             = "sseregparm",
71         [ATTRIBUTE_GNU_STDCALL]                = "stdcall",
72         [ATTRIBUTE_GNU_TINY_DATA]              = "tiny_data",
73         [ATTRIBUTE_GNU_TLS_MODEL]              = "tls_model",
74         [ATTRIBUTE_GNU_TRANSPARENT_UNION]      = "transparent_union",
75         [ATTRIBUTE_GNU_TRAP_EXIT]              = "trap_exit",
76         [ATTRIBUTE_GNU_UNUSED]                 = "unused",
77         [ATTRIBUTE_GNU_USED]                   = "used",
78         [ATTRIBUTE_GNU_VISIBILITY]             = "visibility",
79         [ATTRIBUTE_GNU_VOLATILE]               = "volatile",
80         [ATTRIBUTE_GNU_WARN_UNUSED_RESULT]     = "warn_unused_result",
81         [ATTRIBUTE_GNU_WEAKREF]                = "weakref",
82         [ATTRIBUTE_GNU_WEAK]                   = "weak",
83
84         [ATTRIBUTE_MS_ALIGN]                   = "align",
85         [ATTRIBUTE_MS_ALLOCATE]                = "allocate",
86         [ATTRIBUTE_MS_DEPRECATED]              = "deprecated",
87         [ATTRIBUTE_MS_DLLEXPORT]               = "dllexport",
88         [ATTRIBUTE_MS_DLLIMPORT]               = "dllimport",
89         [ATTRIBUTE_MS_NAKED]                   = "naked",
90         [ATTRIBUTE_MS_NOALIAS]                 = "noalias",
91         [ATTRIBUTE_MS_NOINLINE]                = "noinline",
92         [ATTRIBUTE_MS_NORETURN]                = "noreturn",
93         [ATTRIBUTE_MS_NOTHROW]                 = "nothrow",
94         [ATTRIBUTE_MS_NOVTABLE]                = "novtable",
95         [ATTRIBUTE_MS_PROPERTY]                = "property",
96         [ATTRIBUTE_MS_RESTRICT]                = "restrict",
97         [ATTRIBUTE_MS_RETURNS_TWICE]           = "returns_twice",
98         [ATTRIBUTE_MS_SELECTANY]               = "selectany",
99         [ATTRIBUTE_MS_THREAD]                  = "thread",
100         [ATTRIBUTE_MS_UUID]                    = "uuid",
101 };
102
103 const char *get_attribute_name(attribute_kind_t kind)
104 {
105         assert(kind <= ATTRIBUTE_LAST);
106         return attribute_names[kind];
107 }
108
109 type_t *handle_attribute_mode(const attribute_t *attribute, type_t *orig_type)
110 {
111         type_t *type = skip_typeref(orig_type);
112
113         /* at least: byte, word, pointer, list of machine modes
114          * __XXX___ is interpreted as XXX */
115
116         /* This isn't really correct, the backend should provide a list of machine
117          * specific modes (according to gcc philosophy that is...) */
118         attribute_argument_t *arg = attribute->a.arguments;
119         if (arg == NULL) {
120                 errorf(&attribute->pos, "__attribute__((mode(X))) misses argument");
121                 return orig_type;
122         }
123
124         const char         *symbol_str = arg->v.symbol->string;
125         bool                sign       = is_type_signed(type);
126         atomic_type_kind_t  akind;
127         if (streq_underscore("QI",   symbol_str) ||
128             streq_underscore("byte", symbol_str)) {
129                 akind = sign ? ATOMIC_TYPE_CHAR : ATOMIC_TYPE_UCHAR;
130         } else if (streq_underscore("HI", symbol_str)) {
131                 akind = sign ? ATOMIC_TYPE_SHORT : ATOMIC_TYPE_USHORT;
132         } else if (streq_underscore("SI",      symbol_str)
133                 || streq_underscore("word",    symbol_str)
134                 || streq_underscore("pointer", symbol_str)) {
135                 akind = sign ? ATOMIC_TYPE_INT : ATOMIC_TYPE_UINT;
136         } else if (streq_underscore("DI", symbol_str)) {
137                 akind = sign ? ATOMIC_TYPE_LONGLONG : ATOMIC_TYPE_ULONGLONG;
138         } else {
139                 warningf(WARN_OTHER, &attribute->pos, "ignoring unknown mode '%s'",
140                          symbol_str);
141                 return orig_type;
142         }
143
144         if (type->kind == TYPE_ATOMIC || type->kind == TYPE_ENUM) {
145                 type_t *copy       = duplicate_type(type);
146                 copy->atomic.akind = akind;
147                 return identify_new_type(copy);
148         } else if (is_type_pointer(type)) {
149                 warningf(WARN_OTHER, &attribute->pos, "__attribute__((mode)) on pointers not implemented yet (ignored)");
150                 return type;
151         }
152
153         errorf(&attribute->pos,
154                "__attribute__((mode)) only allowed on integer, enum or pointer type");
155         return orig_type;
156 }
157
158 static inline bool is_po2(unsigned x)
159 {
160         return (x & (x-1)) == 0;
161 }
162
163 static void handle_attribute_aligned(const attribute_t *attribute,
164                                      entity_t *entity)
165 {
166         int alignment = 32; /* TODO: fill in maximum useful alignment for
167                                                    target machine */
168         if (attribute->a.arguments) {
169                 attribute_argument_t *argument = attribute->a.arguments;
170                 alignment = fold_constant_to_int(argument->v.expression);
171         }
172
173         if (!is_po2(alignment)) {
174                 errorf(&attribute->pos, "alignment must be a power of 2 but is %d", alignment);
175                 return;
176         }
177         if (alignment <= 0) {
178                 errorf(&attribute->pos, "alignment must be bigger than 0 but is %d", alignment);
179                 return;
180         }
181
182         switch (entity->kind) {
183         case DECLARATION_KIND_CASES:
184                 entity->declaration.alignment = alignment;
185         case ENTITY_TYPEDEF:
186                 entity->typedefe.alignment = alignment;
187                 break;
188         case ENTITY_STRUCT:
189         case ENTITY_UNION:
190                 if (alignment > (int)entity->compound.alignment) {
191                         entity->compound.alignment = alignment;
192                 }
193                 break;
194
195         default:
196                 warningf(WARN_OTHER, &attribute->pos, "alignment attribute specification on '%N' ignored", entity);
197                 break;
198         }
199 }
200
201 static const char *get_argument_string(const attribute_argument_t *argument)
202 {
203         if (argument == NULL)
204                 return NULL;
205         if (argument->kind != ATTRIBUTE_ARGUMENT_EXPRESSION)
206                 return NULL;
207         expression_t *expression = argument->v.expression;
208         if (expression->kind != EXPR_STRING_LITERAL)
209                 return NULL;
210         return expression->string_literal.value.begin;
211 }
212
213 static void handle_attribute_visibility(const attribute_t *attribute,
214                                         entity_t *entity)
215 {
216         /* This isn't really correct, the backend should provide a list of machine
217          * specific modes (according to gcc philosophy that is...) */
218         attribute_argument_t *arg = attribute->a.arguments;
219         if (arg == NULL) {
220                 errorf(&attribute->pos,
221                        "__attribute__((visibility(X))) misses argument");
222                 return;
223         }
224         const char *string = get_argument_string(arg);
225         if (string == NULL) {
226                 errorf(&attribute->pos,
227                        "__attribute__((visibility(X))) argument is not a string");
228                 return;
229         }
230         elf_visibility_tag_t visibility = get_elf_visibility_from_string(string);
231         if (visibility == ELF_VISIBILITY_ERROR) {
232                 errorf(&attribute->pos,
233                        "unknown visibility type '%s'", string);
234                 return;
235         }
236
237         switch (entity->kind) {
238         case ENTITY_VARIABLE:
239                 entity->variable.elf_visibility = visibility;
240                 break;
241         case ENTITY_FUNCTION:
242                 entity->function.elf_visibility = visibility;
243                 break;
244
245         default:
246                 warningf(WARN_OTHER, &attribute->pos, "visibility attribute specification on '%N' ignored", entity);
247                 break;
248         }
249 }
250
251 static void warn_arguments(const attribute_t *attribute)
252 {
253         if (attribute->a.arguments == NULL)
254                 return;
255
256         position_t const *const pos  = &attribute->pos;
257         char       const *const what = get_attribute_name(attribute->kind);
258         warningf(WARN_OTHER, pos, "attribute '%s' needs no arguments", what);
259 }
260
261 static void handle_attribute_packed_e(const attribute_t *attribute,
262                                       entity_t *entity)
263 {
264 #if 0
265         if (entity->kind != ENTITY_STRUCT) {
266                 warningf(WARN_OTHER, &attribute->pos, "packed attribute on '%N' ignored", entity);
267                 return;
268         }
269 #endif
270
271         warn_arguments(attribute);
272         entity->compound.packed = true;
273 }
274
275 static void handle_attribute_packed(const attribute_t *attribute, type_t *type)
276 {
277         if (type->kind != TYPE_COMPOUND_STRUCT) {
278                 position_t const *const pos  = &attribute->pos;
279                 warningf(WARN_OTHER, pos, "packed attribute on type '%T' ignored", type);
280                 return;
281         }
282
283         handle_attribute_packed_e(attribute, (entity_t*) type->compound.compound);
284 }
285
286 static void handle_attribute_asm(const attribute_t *attribute,
287                                       entity_t *entity)
288 {
289         attribute_argument_t *argument = attribute->a.arguments;
290         assert (argument->kind == ATTRIBUTE_ARGUMENT_EXPRESSION);
291         expression_t *expression = argument->v.expression;
292         if (expression->kind != EXPR_STRING_LITERAL)
293                 errorf(&attribute->pos, "Invalid asm attribute expression");
294         symbol_t *sym = symbol_table_insert(expression->string_literal.value.begin);
295         entity->function.actual_name = sym;
296         assert(argument->next == NULL);
297         return;
298 }
299
300 void handle_entity_attributes(const attribute_t *attributes, entity_t *entity)
301 {
302         if (entity->kind == ENTITY_TYPEDEF) {
303                 type_t *type = entity->typedefe.type;
304                 type = handle_type_attributes(attributes, type);
305                 entity->typedefe.type = type;
306         } else if (is_declaration(entity)) {
307                 type_t *type = entity->declaration.type;
308                 type = handle_type_attributes(attributes, type);
309                 entity->declaration.type = type;
310         }
311
312         decl_modifiers_t modifiers = 0;
313         const attribute_t *attribute = attributes;
314         for ( ; attribute != NULL; attribute = attribute->next) {
315                 switch (attribute->kind) {
316                 case ATTRIBUTE_GNU_CONST:         modifiers |= DM_CONST; break;
317                 case ATTRIBUTE_GNU_DEPRECATED:    modifiers |= DM_DEPRECATED; break;
318                 case ATTRIBUTE_GNU_NOINLINE:      modifiers |= DM_NOINLINE; break;
319                 case ATTRIBUTE_GNU_NAKED:         modifiers |= DM_NAKED; break;
320                 case ATTRIBUTE_GNU_PURE:          modifiers |= DM_PURE; break;
321                 case ATTRIBUTE_GNU_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; break;
322                 case ATTRIBUTE_GNU_CONSTRUCTOR:   modifiers |= DM_CONSTRUCTOR; break;
323                 case ATTRIBUTE_GNU_DESTRUCTOR:    modifiers |= DM_DESTRUCTOR; break;
324                 case ATTRIBUTE_GNU_TRANSPARENT_UNION:
325                                                                                   modifiers |= DM_TRANSPARENT_UNION;
326                                                                                   break;
327                 case ATTRIBUTE_GNU_USED:          modifiers |= DM_USED; break;
328                 case ATTRIBUTE_GNU_UNUSED:        modifiers |= DM_UNUSED; break;
329                 case ATTRIBUTE_GNU_DLLIMPORT:     modifiers |= DM_DLLIMPORT; break;
330                 case ATTRIBUTE_GNU_DLLEXPORT:     modifiers |= DM_DLLEXPORT; break;
331                 case ATTRIBUTE_GNU_WEAK:          modifiers |= DM_WEAK; break;
332                 case ATTRIBUTE_GNU_LEAF:          modifiers |= DM_LEAF; break;
333
334                 case ATTRIBUTE_MS_DLLIMPORT:     modifiers |= DM_DLLIMPORT; break;
335                 case ATTRIBUTE_MS_DLLEXPORT:     modifiers |= DM_DLLEXPORT; break;
336                 case ATTRIBUTE_MS_NAKED:         modifiers |= DM_NAKED; break;
337                 case ATTRIBUTE_MS_NOINLINE:      modifiers |= DM_NOINLINE; break;
338                 case ATTRIBUTE_MS_THREAD:        modifiers |= DM_THREAD; break;
339                 case ATTRIBUTE_MS_DEPRECATED:    modifiers |= DM_DEPRECATED; break;
340                 case ATTRIBUTE_MS_RESTRICT:      modifiers |= DM_RESTRICT; break;
341                 case ATTRIBUTE_MS_NOALIAS:       modifiers |= DM_NOALIAS; break;
342
343                 case ATTRIBUTE_GNU_PACKED:
344                         handle_attribute_packed_e(attribute, entity);
345                         break;
346
347                 case ATTRIBUTE_GNU_ASM:
348                         handle_attribute_asm(attribute, entity);
349                         break;
350
351                 case ATTRIBUTE_GNU_VISIBILITY:
352                         handle_attribute_visibility(attribute, entity);
353                         break;
354
355                 case ATTRIBUTE_MS_ALIGN:
356                 case ATTRIBUTE_GNU_ALIGNED:
357                         handle_attribute_aligned(attribute, entity);
358                         break;
359                 default: break;
360                 }
361         }
362
363         if (modifiers != 0) {
364                 switch (entity->kind) {
365                 case ENTITY_TYPEDEF:
366                         entity->typedefe.modifiers |= modifiers;
367                         break;
368                 case ENTITY_UNION:
369                 case ENTITY_STRUCT:
370                         entity->compound.modifiers |= modifiers;
371                         break;
372                 case ENTITY_COMPOUND_MEMBER:
373                 case ENTITY_VARIABLE:
374                 case ENTITY_FUNCTION:
375                         entity->declaration.modifiers |= modifiers;
376                         break;
377                 default:
378                         /* TODO: warning */
379                         break;
380                 }
381         }
382 }
383
384 static type_t *change_calling_convention(type_t *type, cc_kind_t cconv)
385 {
386         if (is_typeref(type) || !is_type_function(type)) {
387                 return type;
388         }
389
390         if (type->function.calling_convention == cconv)
391                 return type;
392
393         type_t* new_type = duplicate_type(type);
394         new_type->function.calling_convention = cconv;
395         return identify_new_type(new_type);
396 }
397
398 static type_t *add_modifiers(type_t *type, decl_modifiers_t modifiers)
399 {
400         if (is_typeref(type) || !is_type_function(type)) {
401                 return type;
402         }
403
404         if ((type->function.modifiers & modifiers) == modifiers)
405                 return type;
406
407         type_t* new_type = duplicate_type(type);
408         new_type->function.modifiers |= modifiers;
409         return identify_new_type(new_type);
410 }
411
412 type_t *handle_type_attributes(const attribute_t *attributes, type_t *type)
413 {
414         const attribute_t *attribute = attributes;
415         for ( ; attribute != NULL; attribute = attribute->next) {
416                 switch (attribute->kind) {
417                 case ATTRIBUTE_GNU_PACKED:
418                         handle_attribute_packed(attribute, type);
419                         break;
420                 case ATTRIBUTE_GNU_CDECL:
421                 case ATTRIBUTE_MS_CDECL:
422                         type = change_calling_convention(type, CC_CDECL);
423                         break;
424                 case ATTRIBUTE_MS_STDCALL:
425                 case ATTRIBUTE_GNU_STDCALL:
426                         type = change_calling_convention(type, CC_STDCALL);
427                         break;
428                 case ATTRIBUTE_MS_FASTCALL:
429                 case ATTRIBUTE_GNU_FASTCALL:
430                         type = change_calling_convention(type, CC_FASTCALL);
431                         break;
432                 case ATTRIBUTE_MS_THISCALL:
433                         type = change_calling_convention(type, CC_THISCALL);
434                         break;
435                 case ATTRIBUTE_GNU_RETURNS_TWICE:
436                 case ATTRIBUTE_MS_RETURNS_TWICE:
437                         type = add_modifiers(type, DM_RETURNS_TWICE);
438                         break;
439                 case ATTRIBUTE_GNU_NORETURN:
440                 case ATTRIBUTE_MS_NORETURN:
441                         type = add_modifiers(type, DM_NORETURN);
442                         break;
443                 case ATTRIBUTE_GNU_MALLOC:
444                 case ATTRIBUTE_MS_ALLOCATE:
445                         type = add_modifiers(type, DM_MALLOC);
446                         break;
447                 case ATTRIBUTE_GNU_NOTHROW:
448                 case ATTRIBUTE_MS_NOTHROW:
449                         type = add_modifiers(type, DM_NOTHROW);
450                         break;
451                 case ATTRIBUTE_GNU_MODE:
452                         type = handle_attribute_mode(attribute, type);
453                         break;
454                 default:
455                         break;
456                 }
457         }
458
459         return type;
460 }
461
462 const char *get_deprecated_string(const attribute_t *attribute)
463 {
464         for ( ; attribute != NULL; attribute = attribute->next) {
465                 if (attribute->kind != ATTRIBUTE_MS_DEPRECATED)
466                         continue;
467
468                 attribute_argument_t *argument = attribute->a.arguments;
469                 return get_argument_string(argument);
470         }
471         return NULL;
472 }
473
474 static bool property_attribute_equal(const attribute_property_argument_t *prop1,
475                                      const attribute_property_argument_t *prop2)
476 {
477         return prop1->put_symbol == prop2->put_symbol
478                 && prop1->get_symbol == prop2->get_symbol;
479 }
480
481 static bool attribute_argument_equal(const attribute_argument_t *arg1,
482                                      const attribute_argument_t *arg2)
483 {
484         if (arg1->kind != arg2->kind)
485                 return false;
486
487         switch (arg1->kind) {
488         case ATTRIBUTE_ARGUMENT_SYMBOL:
489                 return arg1->v.symbol == arg2->v.symbol;
490         case ATTRIBUTE_ARGUMENT_EXPRESSION:
491                 /* TODO */
492                 return false;
493         }
494         panic("unknown argument type");
495 }
496
497 static bool attribute_arguments_equal(const attribute_argument_t *args1,
498                                       const attribute_argument_t *args2)
499 {
500         for ( ; args1 != NULL && args2 != NULL;
501              args1 = args1->next, args2 = args2->next) {
502                 if (!attribute_argument_equal(args1, args2))
503                         return false;
504         }
505         /* both should be NULL now */
506         return args1 == args2;
507 }
508
509 bool attributes_equal(const attribute_t *attr1, const attribute_t *attr2)
510 {
511         if (attr1->kind != attr2->kind)
512                 return false;
513
514         switch (attr1->kind) {
515         case ATTRIBUTE_MS_PROPERTY:
516                 return property_attribute_equal(attr1->a.property, attr2->a.property);
517         default:
518                 return attribute_arguments_equal(attr1->a.arguments,
519                                                  attr2->a.arguments);
520         }
521 }