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