- implement -mrtd
[cparser] / type.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 <stdio.h>
23 #include <assert.h>
24
25 #include "type_t.h"
26 #include "types.h"
27 #include "entity_t.h"
28 #include "symbol_t.h"
29 #include "type_hash.h"
30 #include "adt/error.h"
31 #include "adt/util.h"
32 #include "lang_features.h"
33 #include "warning.h"
34 #include "diagnostic.h"
35 #include "driver/firm_cmdline.h"
36
37 /** The default calling convention. */
38 cc_kind_t default_calling_convention = CC_CDECL;
39
40 static struct obstack   _type_obst;
41 static FILE            *out;
42 struct obstack         *type_obst                 = &_type_obst;
43 static bool             print_implicit_array_size = false;
44
45 static void intern_print_type_pre(const type_t *type);
46 static void intern_print_type_post(const type_t *type);
47
48 typedef struct atomic_type_properties_t atomic_type_properties_t;
49 struct atomic_type_properties_t {
50         unsigned   size;              /**< type size in bytes */
51         unsigned   alignment;         /**< type alignment in bytes */
52         unsigned   flags;             /**< type flags from atomic_type_flag_t */
53 };
54
55 /**
56  * Properties of atomic types.
57  */
58 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
59         //ATOMIC_TYPE_INVALID = 0,
60         [ATOMIC_TYPE_VOID] = {
61                 .size       = 0,
62                 .alignment  = 0,
63                 .flags      = ATOMIC_TYPE_FLAG_NONE
64         },
65         [ATOMIC_TYPE_WCHAR_T] = {
66                 .size       = (unsigned)-1,
67                 .alignment  = (unsigned)-1,
68                 /* signed flag will be set when known */
69                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
70         },
71         [ATOMIC_TYPE_CHAR] = {
72                 .size       = 1,
73                 .alignment  = 1,
74                 /* signed flag will be set when known */
75                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
76         },
77         [ATOMIC_TYPE_SCHAR] = {
78                 .size       = 1,
79                 .alignment  = 1,
80                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
81                               | ATOMIC_TYPE_FLAG_SIGNED,
82         },
83         [ATOMIC_TYPE_UCHAR] = {
84                 .size       = 1,
85                 .alignment  = 1,
86                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
87         },
88         [ATOMIC_TYPE_SHORT] = {
89                 .size       = 2,
90                 .alignment  = 2,
91                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
92                               | ATOMIC_TYPE_FLAG_SIGNED
93         },
94         [ATOMIC_TYPE_USHORT] = {
95                 .size       = 2,
96                 .alignment  = 2,
97                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
98         },
99         [ATOMIC_TYPE_INT] = {
100                 .size       = (unsigned) -1,
101                 .alignment  = (unsigned) -1,
102                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
103                               | ATOMIC_TYPE_FLAG_SIGNED,
104         },
105         [ATOMIC_TYPE_UINT] = {
106                 .size       = (unsigned) -1,
107                 .alignment  = (unsigned) -1,
108                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
109         },
110         [ATOMIC_TYPE_LONG] = {
111                 .size       = (unsigned) -1,
112                 .alignment  = (unsigned) -1,
113                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
114                               | ATOMIC_TYPE_FLAG_SIGNED,
115         },
116         [ATOMIC_TYPE_ULONG] = {
117                 .size       = (unsigned) -1,
118                 .alignment  = (unsigned) -1,
119                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
120         },
121         [ATOMIC_TYPE_LONGLONG] = {
122                 .size       = (unsigned) -1,
123                 .alignment  = (unsigned) -1,
124                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
125                               | ATOMIC_TYPE_FLAG_SIGNED,
126         },
127         [ATOMIC_TYPE_ULONGLONG] = {
128                 .size       = (unsigned) -1,
129                 .alignment  = (unsigned) -1,
130                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
131         },
132         [ATOMIC_TYPE_BOOL] = {
133                 .size       = (unsigned) -1,
134                 .alignment  = (unsigned) -1,
135                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
136         },
137         [ATOMIC_TYPE_FLOAT] = {
138                 .size       = 4,
139                 .alignment  = (unsigned) -1,
140                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
141                               | ATOMIC_TYPE_FLAG_SIGNED,
142         },
143         [ATOMIC_TYPE_DOUBLE] = {
144                 .size       = 8,
145                 .alignment  = (unsigned) -1,
146                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
147                               | ATOMIC_TYPE_FLAG_SIGNED,
148         },
149         [ATOMIC_TYPE_LONG_DOUBLE] = {
150                 .size       = 12,
151                 .alignment  = (unsigned) -1,
152                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
153                               | ATOMIC_TYPE_FLAG_SIGNED,
154         },
155         /* complex and imaginary types are set in init_types */
156 };
157
158 void init_types(void)
159 {
160         obstack_init(type_obst);
161
162         atomic_type_properties_t *props = atomic_type_properties;
163
164         if (char_is_signed) {
165                 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
166         }
167
168         unsigned int_size   = machine_size < 32 ? 2 : 4;
169         unsigned long_size  = machine_size < 64 ? 4 : 8;
170         unsigned llong_size = machine_size < 32 ? 4 : 8;
171
172         props[ATOMIC_TYPE_INT].size            = int_size;
173         props[ATOMIC_TYPE_INT].alignment       = int_size;
174         props[ATOMIC_TYPE_UINT].size           = int_size;
175         props[ATOMIC_TYPE_UINT].alignment      = int_size;
176         props[ATOMIC_TYPE_LONG].size           = long_size;
177         props[ATOMIC_TYPE_LONG].alignment      = long_size;
178         props[ATOMIC_TYPE_ULONG].size          = long_size;
179         props[ATOMIC_TYPE_ULONG].alignment     = long_size;
180         props[ATOMIC_TYPE_LONGLONG].size       = llong_size;
181         props[ATOMIC_TYPE_LONGLONG].alignment  = llong_size;
182         props[ATOMIC_TYPE_ULONGLONG].size      = llong_size;
183         props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
184
185         /* TODO: backend specific, need a way to query the backend for this.
186          * The following are good settings for x86 */
187         props[ATOMIC_TYPE_FLOAT].alignment       = 4;
188         props[ATOMIC_TYPE_DOUBLE].alignment      = 4;
189         props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
190         props[ATOMIC_TYPE_LONGLONG].alignment    = 4;
191         props[ATOMIC_TYPE_ULONGLONG].alignment   = 4;
192         if (firm_opt.os_support == OS_SUPPORT_MACHO) {
193                 props[ATOMIC_TYPE_LONG_DOUBLE].size      = 16;
194                 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
195         }
196
197         /* TODO: make this configurable for platforms which do not use byte sized
198          * bools. */
199         props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
200
201         props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
202 }
203
204 void exit_types(void)
205 {
206         obstack_free(type_obst, NULL);
207 }
208
209 void type_set_output(FILE *stream)
210 {
211         out = stream;
212 }
213
214 void print_type_qualifiers(type_qualifiers_t qualifiers)
215 {
216         int first = 1;
217         if (qualifiers & TYPE_QUALIFIER_CONST) {
218                 fputs(" const" + first,    out);
219                 first = 0;
220         }
221         if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
222                 fputs(" volatile" + first, out);
223                 first = 0;
224         }
225         if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
226                 fputs(" restrict" + first, out);
227                 first = 0;
228         }
229 }
230
231 const char *get_atomic_kind_name(atomic_type_kind_t kind)
232 {
233         switch(kind) {
234         case ATOMIC_TYPE_INVALID: break;
235         case ATOMIC_TYPE_VOID:        return "void";
236         case ATOMIC_TYPE_WCHAR_T:     return "wchar_t";
237         case ATOMIC_TYPE_BOOL:        return c_mode & _CXX ? "bool" : "_Bool";
238         case ATOMIC_TYPE_CHAR:        return "char";
239         case ATOMIC_TYPE_SCHAR:       return "signed char";
240         case ATOMIC_TYPE_UCHAR:       return "unsigned char";
241         case ATOMIC_TYPE_INT:         return "int";
242         case ATOMIC_TYPE_UINT:        return "unsigned int";
243         case ATOMIC_TYPE_SHORT:       return "short";
244         case ATOMIC_TYPE_USHORT:      return "unsigned short";
245         case ATOMIC_TYPE_LONG:        return "long";
246         case ATOMIC_TYPE_ULONG:       return "unsigned long";
247         case ATOMIC_TYPE_LONGLONG:    return "long long";
248         case ATOMIC_TYPE_ULONGLONG:   return "unsigned long long";
249         case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
250         case ATOMIC_TYPE_FLOAT:       return "float";
251         case ATOMIC_TYPE_DOUBLE:      return "double";
252         }
253         return "INVALIDATOMIC";
254 }
255
256 /**
257  * Prints the name of an atomic type kinds.
258  *
259  * @param kind  The type kind.
260  */
261 static void print_atomic_kinds(atomic_type_kind_t kind)
262 {
263         const char *s = get_atomic_kind_name(kind);
264         fputs(s, out);
265 }
266
267 /**
268  * Prints the name of an atomic type.
269  *
270  * @param type  The type.
271  */
272 static void print_atomic_type(const atomic_type_t *type)
273 {
274         print_type_qualifiers(type->base.qualifiers);
275         if (type->base.qualifiers != 0)
276                 fputc(' ', out);
277         print_atomic_kinds(type->akind);
278 }
279
280 /**
281  * Prints the name of a complex type.
282  *
283  * @param type  The type.
284  */
285 static
286 void print_complex_type(const complex_type_t *type)
287 {
288         int empty = type->base.qualifiers == 0;
289         print_type_qualifiers(type->base.qualifiers);
290         fputs(" _Complex " + empty, out);
291         print_atomic_kinds(type->akind);
292 }
293
294 /**
295  * Prints the name of an imaginary type.
296  *
297  * @param type  The type.
298  */
299 static
300 void print_imaginary_type(const imaginary_type_t *type)
301 {
302         int empty = type->base.qualifiers == 0;
303         print_type_qualifiers(type->base.qualifiers);
304         fputs(" _Imaginary " + empty, out);
305         print_atomic_kinds(type->akind);
306 }
307
308 /**
309  * Print the first part (the prefix) of a type.
310  *
311  * @param type   The type to print.
312  */
313 static void print_function_type_pre(const function_type_t *type)
314 {
315         switch (type->linkage) {
316                 case LINKAGE_INVALID:
317                         break;
318
319                 case LINKAGE_C:
320                         if (c_mode & _CXX)
321                                 fputs("extern \"C\" ",   out);
322                         break;
323
324                 case LINKAGE_CXX:
325                         if (!(c_mode & _CXX))
326                                 fputs("extern \"C++\" ", out);
327                         break;
328         }
329
330         print_type_qualifiers(type->base.qualifiers);
331         if (type->base.qualifiers != 0)
332                 fputc(' ', out);
333
334         intern_print_type_pre(type->return_type);
335
336         cc_kind_t cc = type->calling_convention;
337 restart:
338         switch (cc) {
339         case CC_CDECL:    fputs(" __cdecl",    out); break;
340         case CC_STDCALL:  fputs(" __stdcall",  out); break;
341         case CC_FASTCALL: fputs(" __fastcall", out); break;
342         case CC_THISCALL: fputs(" __thiscall", out); break;
343         case CC_DEFAULT:
344                 if (default_calling_convention != CC_CDECL) {
345                         /* show the default calling convention if its not cdecl */
346                         cc = default_calling_convention;
347                         goto restart;
348                 }
349                 break;
350         }
351 }
352
353 /**
354  * Print the second part (the postfix) of a type.
355  *
356  * @param type   The type to print.
357  */
358 static void print_function_type_post(const function_type_t *type,
359                                      const scope_t *parameters)
360 {
361         fputc('(', out);
362         bool first = true;
363         if (parameters == NULL) {
364                 function_parameter_t *parameter = type->parameters;
365                 for( ; parameter != NULL; parameter = parameter->next) {
366                         if (first) {
367                                 first = false;
368                         } else {
369                                 fputs(", ", out);
370                         }
371                         print_type(parameter->type);
372                 }
373         } else {
374                 entity_t *parameter = parameters->entities;
375                 for (; parameter != NULL; parameter = parameter->base.next) {
376                         if (parameter->kind != ENTITY_PARAMETER)
377                                 continue;
378
379                         if (first) {
380                                 first = false;
381                         } else {
382                                 fputs(", ", out);
383                         }
384                         const type_t *const type = parameter->declaration.type;
385                         if (type == NULL) {
386                                 fputs(parameter->base.symbol->string, out);
387                         } else {
388                                 print_type_ext(type, parameter->base.symbol, NULL);
389                         }
390                 }
391         }
392         if (type->variadic) {
393                 if (first) {
394                         first = false;
395                 } else {
396                         fputs(", ", out);
397                 }
398                 fputs("...", out);
399         }
400         if (first && !type->unspecified_parameters) {
401                 fputs("void", out);
402         }
403         fputc(')', out);
404
405         intern_print_type_post(type->return_type);
406 }
407
408 /**
409  * Prints the prefix part of a pointer type.
410  *
411  * @param type   The pointer type.
412  */
413 static void print_pointer_type_pre(const pointer_type_t *type)
414 {
415         type_t const *const points_to = type->points_to;
416         intern_print_type_pre(points_to);
417         if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
418                 fputs(" (", out);
419         variable_t *const variable = type->base_variable;
420         if (variable != NULL) {
421                 fputs(" __based(", out);
422                 fputs(variable->base.base.symbol->string, out);
423                 fputs(") ", out);
424         }
425         fputc('*', out);
426         type_qualifiers_t const qual = type->base.qualifiers;
427         if (qual != 0)
428                 fputc(' ', out);
429         print_type_qualifiers(qual);
430 }
431
432 /**
433  * Prints the postfix part of a pointer type.
434  *
435  * @param type   The pointer type.
436  */
437 static void print_pointer_type_post(const pointer_type_t *type)
438 {
439         type_t const *const points_to = type->points_to;
440         if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
441                 fputc(')', out);
442         intern_print_type_post(points_to);
443 }
444
445 /**
446  * Prints the prefix part of a reference type.
447  *
448  * @param type   The reference type.
449  */
450 static void print_reference_type_pre(const reference_type_t *type)
451 {
452         type_t const *const refers_to = type->refers_to;
453         intern_print_type_pre(refers_to);
454         if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
455                 fputs(" (", out);
456         fputc('&', out);
457 }
458
459 /**
460  * Prints the postfix part of a reference type.
461  *
462  * @param type   The reference type.
463  */
464 static void print_reference_type_post(const reference_type_t *type)
465 {
466         type_t const *const refers_to = type->refers_to;
467         if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
468                 fputc(')', out);
469         intern_print_type_post(refers_to);
470 }
471
472 /**
473  * Prints the prefix part of an array type.
474  *
475  * @param type   The array type.
476  */
477 static void print_array_type_pre(const array_type_t *type)
478 {
479         intern_print_type_pre(type->element_type);
480 }
481
482 /**
483  * Prints the postfix part of an array type.
484  *
485  * @param type   The array type.
486  */
487 static void print_array_type_post(const array_type_t *type)
488 {
489         fputc('[', out);
490         if (type->is_static) {
491                 fputs("static ", out);
492         }
493         print_type_qualifiers(type->base.qualifiers);
494         if (type->base.qualifiers != 0)
495                 fputc(' ', out);
496         if (type->size_expression != NULL
497                         && (print_implicit_array_size || !type->has_implicit_size)) {
498                 print_expression(type->size_expression);
499         }
500         fputc(']', out);
501         intern_print_type_post(type->element_type);
502 }
503
504 /**
505  * Prints the postfix part of a bitfield type.
506  *
507  * @param type   The array type.
508  */
509 static void print_bitfield_type_post(const bitfield_type_t *type)
510 {
511         fputs(" : ", out);
512         print_expression(type->size_expression);
513         intern_print_type_post(type->base_type);
514 }
515
516 /**
517  * Prints an enum definition.
518  *
519  * @param declaration  The enum's type declaration.
520  */
521 void print_enum_definition(const enum_t *enume)
522 {
523         fputs("{\n", out);
524
525         change_indent(1);
526
527         entity_t *entry = enume->base.next;
528         for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
529                entry = entry->base.next) {
530
531                 print_indent();
532                 fputs(entry->base.symbol->string, out);
533                 if (entry->enum_value.value != NULL) {
534                         fputs(" = ", out);
535
536                         /* skip the implicit cast */
537                         expression_t *expression = entry->enum_value.value;
538                         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
539                                 expression = expression->unary.value;
540                         }
541                         print_expression(expression);
542                 }
543                 fputs(",\n", out);
544         }
545
546         change_indent(-1);
547         print_indent();
548         fputc('}', out);
549 }
550
551 /**
552  * Prints an enum type.
553  *
554  * @param type  The enum type.
555  */
556 static void print_type_enum(const enum_type_t *type)
557 {
558         int empty = type->base.qualifiers == 0;
559         print_type_qualifiers(type->base.qualifiers);
560         fputs(" enum " + empty, out);
561
562         enum_t   *enume  = type->enume;
563         symbol_t *symbol = enume->base.symbol;
564         if (symbol != NULL) {
565                 fputs(symbol->string, out);
566         } else {
567                 print_enum_definition(enume);
568         }
569 }
570
571 /**
572  * Print the compound part of a compound type.
573  */
574 void print_compound_definition(const compound_t *compound)
575 {
576         fputs("{\n", out);
577         change_indent(1);
578
579         entity_t *entity = compound->members.entities;
580         for( ; entity != NULL; entity = entity->base.next) {
581                 if (entity->kind != ENTITY_COMPOUND_MEMBER)
582                         continue;
583
584                 print_indent();
585                 print_entity(entity);
586                 fputc('\n', out);
587         }
588
589         change_indent(-1);
590         print_indent();
591         fputc('}', out);
592         if (compound->modifiers & DM_TRANSPARENT_UNION) {
593                 fputs("__attribute__((__transparent_union__))", out);
594         }
595 }
596
597 /**
598  * Prints a compound type.
599  *
600  * @param type  The compound type.
601  */
602 static void print_compound_type(const compound_type_t *type)
603 {
604         int empty = type->base.qualifiers == 0;
605         print_type_qualifiers(type->base.qualifiers);
606
607         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
608                 fputs(" struct " + empty, out);
609         } else {
610                 assert(type->base.kind == TYPE_COMPOUND_UNION);
611                 fputs(" union " + empty, out);
612         }
613
614         compound_t *compound = type->compound;
615         symbol_t   *symbol   = compound->base.symbol;
616         if (symbol != NULL) {
617                 fputs(symbol->string, out);
618         } else {
619                 print_compound_definition(compound);
620         }
621 }
622
623 /**
624  * Prints the prefix part of a typedef type.
625  *
626  * @param type   The typedef type.
627  */
628 static void print_typedef_type_pre(const typedef_type_t *const type)
629 {
630         print_type_qualifiers(type->base.qualifiers);
631         if (type->base.qualifiers != 0)
632                 fputc(' ', out);
633         fputs(type->typedefe->base.symbol->string, out);
634 }
635
636 /**
637  * Prints the prefix part of a typeof type.
638  *
639  * @param type   The typeof type.
640  */
641 static void print_typeof_type_pre(const typeof_type_t *const type)
642 {
643         fputs("typeof(", out);
644         if (type->expression != NULL) {
645                 print_expression(type->expression);
646         } else {
647                 print_type(type->typeof_type);
648         }
649         fputc(')', out);
650 }
651
652 /**
653  * Prints the prefix part of a type.
654  *
655  * @param type   The type.
656  */
657 static void intern_print_type_pre(const type_t *const type)
658 {
659         switch(type->kind) {
660         case TYPE_ERROR:
661                 fputs("<error>", out);
662                 return;
663         case TYPE_INVALID:
664                 fputs("<invalid>", out);
665                 return;
666         case TYPE_ENUM:
667                 print_type_enum(&type->enumt);
668                 return;
669         case TYPE_ATOMIC:
670                 print_atomic_type(&type->atomic);
671                 return;
672         case TYPE_COMPLEX:
673                 print_complex_type(&type->complex);
674                 return;
675         case TYPE_IMAGINARY:
676                 print_imaginary_type(&type->imaginary);
677                 return;
678         case TYPE_COMPOUND_STRUCT:
679         case TYPE_COMPOUND_UNION:
680                 print_compound_type(&type->compound);
681                 return;
682         case TYPE_BUILTIN:
683                 fputs(type->builtin.symbol->string, out);
684                 return;
685         case TYPE_FUNCTION:
686                 print_function_type_pre(&type->function);
687                 return;
688         case TYPE_POINTER:
689                 print_pointer_type_pre(&type->pointer);
690                 return;
691         case TYPE_REFERENCE:
692                 print_reference_type_pre(&type->reference);
693                 return;
694         case TYPE_BITFIELD:
695                 intern_print_type_pre(type->bitfield.base_type);
696                 return;
697         case TYPE_ARRAY:
698                 print_array_type_pre(&type->array);
699                 return;
700         case TYPE_TYPEDEF:
701                 print_typedef_type_pre(&type->typedeft);
702                 return;
703         case TYPE_TYPEOF:
704                 print_typeof_type_pre(&type->typeoft);
705                 return;
706         }
707         fputs("unknown", out);
708 }
709
710 /**
711  * Prints the postfix part of a type.
712  *
713  * @param type   The type.
714  */
715 static void intern_print_type_post(const type_t *const type)
716 {
717         switch(type->kind) {
718         case TYPE_FUNCTION:
719                 print_function_type_post(&type->function, NULL);
720                 return;
721         case TYPE_POINTER:
722                 print_pointer_type_post(&type->pointer);
723                 return;
724         case TYPE_REFERENCE:
725                 print_reference_type_post(&type->reference);
726                 return;
727         case TYPE_ARRAY:
728                 print_array_type_post(&type->array);
729                 return;
730         case TYPE_BITFIELD:
731                 print_bitfield_type_post(&type->bitfield);
732                 return;
733         case TYPE_ERROR:
734         case TYPE_INVALID:
735         case TYPE_ATOMIC:
736         case TYPE_COMPLEX:
737         case TYPE_IMAGINARY:
738         case TYPE_ENUM:
739         case TYPE_COMPOUND_STRUCT:
740         case TYPE_COMPOUND_UNION:
741         case TYPE_BUILTIN:
742         case TYPE_TYPEOF:
743         case TYPE_TYPEDEF:
744                 break;
745         }
746 }
747
748 /**
749  * Prints a type.
750  *
751  * @param type   The type.
752  */
753 void print_type(const type_t *const type)
754 {
755         print_type_ext(type, NULL, NULL);
756 }
757
758 void print_type_ext(const type_t *const type, const symbol_t *symbol,
759                     const scope_t *parameters)
760 {
761         if (type == NULL) {
762                 fputs("nil type", out);
763                 return;
764         }
765
766         intern_print_type_pre(type);
767         if (symbol != NULL) {
768                 fputc(' ', out);
769                 fputs(symbol->string, out);
770         }
771         if (type->kind == TYPE_FUNCTION) {
772                 print_function_type_post(&type->function, parameters);
773         } else {
774                 intern_print_type_post(type);
775         }
776 }
777
778 /**
779  * Return the size of a type AST node.
780  *
781  * @param type  The type.
782  */
783 static size_t get_type_struct_size(const type_t *type)
784 {
785         switch(type->kind) {
786         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
787         case TYPE_COMPLEX:         return sizeof(complex_type_t);
788         case TYPE_IMAGINARY:       return sizeof(imaginary_type_t);
789         case TYPE_COMPOUND_STRUCT:
790         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
791         case TYPE_ENUM:            return sizeof(enum_type_t);
792         case TYPE_FUNCTION:        return sizeof(function_type_t);
793         case TYPE_POINTER:         return sizeof(pointer_type_t);
794         case TYPE_REFERENCE:       return sizeof(reference_type_t);
795         case TYPE_ARRAY:           return sizeof(array_type_t);
796         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
797         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
798         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
799         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
800         case TYPE_ERROR:           panic("error type found");
801         case TYPE_INVALID:         panic("invalid type found");
802         }
803         panic("unknown type found");
804 }
805
806 /**
807  * Duplicates a type.
808  *
809  * @param type  The type to copy.
810  * @return A copy of the type.
811  *
812  * @note This does not produce a deep copy!
813  */
814 type_t *duplicate_type(const type_t *type)
815 {
816         size_t size = get_type_struct_size(type);
817
818         type_t *copy = obstack_alloc(type_obst, size);
819         memcpy(copy, type, size);
820         copy->base.firm_type = NULL;
821
822         return copy;
823 }
824
825 /**
826  * Returns the unqualified type of a given type.
827  *
828  * @param type  The type.
829  * @returns The unqualified type.
830  */
831 type_t *get_unqualified_type(type_t *type)
832 {
833         assert(!is_typeref(type));
834
835         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
836                 return type;
837
838         type_t *unqualified_type          = duplicate_type(type);
839         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
840
841         return identify_new_type(unqualified_type);
842 }
843
844 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
845 {
846         type_t *type = skip_typeref(orig_type);
847
848         type_t *copy;
849         if (is_type_array(type)) {
850                 /* For array types the element type has to be adjusted */
851                 type_t *element_type      = type->array.element_type;
852                 type_t *qual_element_type = get_qualified_type(element_type, qual);
853
854                 if (qual_element_type == element_type)
855                         return orig_type;
856
857                 copy                     = duplicate_type(type);
858                 copy->array.element_type = qual_element_type;
859         } else if (is_type_valid(type)) {
860                 if ((type->base.qualifiers & qual) == qual)
861                         return orig_type;
862
863                 copy                   = duplicate_type(type);
864                 copy->base.qualifiers |= qual;
865         } else {
866                 return type;
867         }
868
869         return identify_new_type(copy);
870 }
871
872 /**
873  * Check if a type is valid.
874  *
875  * @param type  The type to check.
876  * @return true if type represents a valid type.
877  */
878 bool type_valid(const type_t *type)
879 {
880         return type->kind != TYPE_INVALID;
881 }
882
883 static bool test_atomic_type_flag(atomic_type_kind_t kind,
884                                   atomic_type_flag_t flag)
885 {
886         assert(kind <= ATOMIC_TYPE_LAST);
887         return (atomic_type_properties[kind].flags & flag) != 0;
888 }
889
890 /**
891  * Returns true if the given type is an integer type.
892  *
893  * @param type  The type to check.
894  * @return True if type is an integer type.
895  */
896 bool is_type_integer(const type_t *type)
897 {
898         assert(!is_typeref(type));
899
900         if (type->kind == TYPE_ENUM)
901                 return true;
902         if (type->kind == TYPE_BITFIELD)
903                 return true;
904
905         if (type->kind != TYPE_ATOMIC)
906                 return false;
907
908         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
909 }
910
911 /**
912  * Returns true if the given type is an enum type.
913  *
914  * @param type  The type to check.
915  * @return True if type is an enum type.
916  */
917 bool is_type_enum(const type_t *type)
918 {
919         assert(!is_typeref(type));
920         return type->kind == TYPE_ENUM;
921 }
922
923 /**
924  * Returns true if the given type is an floating point type.
925  *
926  * @param type  The type to check.
927  * @return True if type is a floating point type.
928  */
929 bool is_type_float(const type_t *type)
930 {
931         assert(!is_typeref(type));
932
933         if (type->kind != TYPE_ATOMIC)
934                 return false;
935
936         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
937 }
938
939 /**
940  * Returns true if the given type is an complex type.
941  *
942  * @param type  The type to check.
943  * @return True if type is a complex type.
944  */
945 bool is_type_complex(const type_t *type)
946 {
947         assert(!is_typeref(type));
948
949         if (type->kind != TYPE_ATOMIC)
950                 return false;
951
952         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
953 }
954
955 /**
956  * Returns true if the given type is a signed type.
957  *
958  * @param type  The type to check.
959  * @return True if type is a signed type.
960  */
961 bool is_type_signed(const type_t *type)
962 {
963         assert(!is_typeref(type));
964
965         /* enum types are int for now */
966         if (type->kind == TYPE_ENUM)
967                 return true;
968         if (type->kind == TYPE_BITFIELD)
969                 return is_type_signed(type->bitfield.base_type);
970
971         if (type->kind != TYPE_ATOMIC)
972                 return false;
973
974         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
975 }
976
977 /**
978  * Returns true if the given type represents an arithmetic type.
979  *
980  * @param type  The type to check.
981  * @return True if type represents an arithmetic type.
982  */
983 bool is_type_arithmetic(const type_t *type)
984 {
985         assert(!is_typeref(type));
986
987         switch(type->kind) {
988         case TYPE_BITFIELD:
989         case TYPE_ENUM:
990                 return true;
991         case TYPE_ATOMIC:
992                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
993         case TYPE_COMPLEX:
994                 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
995         case TYPE_IMAGINARY:
996                 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
997         default:
998                 return false;
999         }
1000 }
1001
1002 /**
1003  * Returns true if the given type is an integer or float type.
1004  *
1005  * @param type  The type to check.
1006  * @return True if type is an integer or float type.
1007  */
1008 bool is_type_real(const type_t *type)
1009 {
1010         /* 6.2.5 (17) */
1011         return is_type_integer(type) || is_type_float(type);
1012 }
1013
1014 /**
1015  * Returns true if the given type represents a scalar type.
1016  *
1017  * @param type  The type to check.
1018  * @return True if type represents a scalar type.
1019  */
1020 bool is_type_scalar(const type_t *type)
1021 {
1022         assert(!is_typeref(type));
1023
1024         switch (type->kind) {
1025                 case TYPE_POINTER: return true;
1026                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1027                 default:           break;
1028         }
1029
1030         return is_type_arithmetic(type);
1031 }
1032
1033 /**
1034  * Check if a given type is incomplete.
1035  *
1036  * @param type  The type to check.
1037  * @return True if the given type is incomplete (ie. just forward).
1038  */
1039 bool is_type_incomplete(const type_t *type)
1040 {
1041         assert(!is_typeref(type));
1042
1043         switch(type->kind) {
1044         case TYPE_COMPOUND_STRUCT:
1045         case TYPE_COMPOUND_UNION: {
1046                 const compound_type_t *compound_type = &type->compound;
1047                 return !compound_type->compound->complete;
1048         }
1049         case TYPE_ENUM:
1050                 return false;
1051
1052         case TYPE_ARRAY:
1053                 return type->array.size_expression == NULL
1054                         && !type->array.size_constant;
1055
1056         case TYPE_ATOMIC:
1057                 return type->atomic.akind == ATOMIC_TYPE_VOID;
1058
1059         case TYPE_COMPLEX:
1060                 return type->complex.akind == ATOMIC_TYPE_VOID;
1061
1062         case TYPE_IMAGINARY:
1063                 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1064
1065         case TYPE_BITFIELD:
1066         case TYPE_FUNCTION:
1067         case TYPE_POINTER:
1068         case TYPE_REFERENCE:
1069         case TYPE_BUILTIN:
1070         case TYPE_ERROR:
1071                 return false;
1072
1073         case TYPE_TYPEDEF:
1074         case TYPE_TYPEOF:
1075                 panic("is_type_incomplete called without typerefs skipped");
1076         case TYPE_INVALID:
1077                 break;
1078         }
1079
1080         panic("invalid type found");
1081 }
1082
1083 bool is_type_object(const type_t *type)
1084 {
1085         return !is_type_function(type) && !is_type_incomplete(type);
1086 }
1087
1088 bool is_builtin_va_list(type_t *type)
1089 {
1090         type_t *tp = skip_typeref(type);
1091
1092         return tp->kind == type_valist->kind &&
1093                tp->builtin.symbol == type_valist->builtin.symbol;
1094 }
1095
1096 /**
1097  * Check if two function types are compatible.
1098  */
1099 static bool function_types_compatible(const function_type_t *func1,
1100                                       const function_type_t *func2)
1101 {
1102         const type_t* const ret1 = skip_typeref(func1->return_type);
1103         const type_t* const ret2 = skip_typeref(func2->return_type);
1104         if (!types_compatible(ret1, ret2))
1105                 return false;
1106
1107         if (func1->linkage != func2->linkage)
1108                 return false;
1109
1110         cc_kind_t cc1 = func1->calling_convention;
1111         if (cc1 == CC_DEFAULT)
1112                 cc1 = default_calling_convention;
1113         cc_kind_t cc2 = func2->calling_convention;
1114         if (cc2 == CC_DEFAULT)
1115                 cc2 = default_calling_convention;
1116
1117         if (cc1 != cc2)
1118                 return false;
1119
1120         /* can parameters be compared? */
1121         if (func1->unspecified_parameters || func2->unspecified_parameters)
1122                 return true;
1123
1124         if (func1->variadic != func2->variadic)
1125                 return false;
1126
1127         /* TODO: handling of unspecified parameters not correct yet */
1128
1129         /* all argument types must be compatible */
1130         function_parameter_t *parameter1 = func1->parameters;
1131         function_parameter_t *parameter2 = func2->parameters;
1132         for ( ; parameter1 != NULL && parameter2 != NULL;
1133                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
1134                 type_t *parameter1_type = skip_typeref(parameter1->type);
1135                 type_t *parameter2_type = skip_typeref(parameter2->type);
1136
1137                 parameter1_type = get_unqualified_type(parameter1_type);
1138                 parameter2_type = get_unqualified_type(parameter2_type);
1139
1140                 if (!types_compatible(parameter1_type, parameter2_type))
1141                         return false;
1142         }
1143         /* same number of arguments? */
1144         if (parameter1 != NULL || parameter2 != NULL)
1145                 return false;
1146
1147         return true;
1148 }
1149
1150 /**
1151  * Check if two array types are compatible.
1152  */
1153 static bool array_types_compatible(const array_type_t *array1,
1154                                    const array_type_t *array2)
1155 {
1156         type_t *element_type1 = skip_typeref(array1->element_type);
1157         type_t *element_type2 = skip_typeref(array2->element_type);
1158         if (!types_compatible(element_type1, element_type2))
1159                 return false;
1160
1161         if (!array1->size_constant || !array2->size_constant)
1162                 return true;
1163
1164         return array1->size == array2->size;
1165 }
1166
1167 /**
1168  * Check if two types are compatible.
1169  */
1170 bool types_compatible(const type_t *type1, const type_t *type2)
1171 {
1172         assert(!is_typeref(type1));
1173         assert(!is_typeref(type2));
1174
1175         /* shortcut: the same type is always compatible */
1176         if (type1 == type2)
1177                 return true;
1178
1179         if (!is_type_valid(type1) || !is_type_valid(type2))
1180                 return true;
1181
1182         if (type1->base.qualifiers != type2->base.qualifiers)
1183                 return false;
1184         if (type1->kind != type2->kind)
1185                 return false;
1186
1187         switch (type1->kind) {
1188         case TYPE_FUNCTION:
1189                 return function_types_compatible(&type1->function, &type2->function);
1190         case TYPE_ATOMIC:
1191                 return type1->atomic.akind == type2->atomic.akind;
1192         case TYPE_COMPLEX:
1193                 return type1->complex.akind == type2->complex.akind;
1194         case TYPE_IMAGINARY:
1195                 return type1->imaginary.akind == type2->imaginary.akind;
1196         case TYPE_ARRAY:
1197                 return array_types_compatible(&type1->array, &type2->array);
1198
1199         case TYPE_POINTER: {
1200                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1201                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1202                 return types_compatible(to1, to2);
1203         }
1204
1205         case TYPE_REFERENCE: {
1206                 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1207                 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1208                 return types_compatible(to1, to2);
1209         }
1210
1211         case TYPE_COMPOUND_STRUCT:
1212         case TYPE_COMPOUND_UNION: {
1213
1214
1215                 break;
1216         }
1217         case TYPE_ENUM:
1218         case TYPE_BUILTIN:
1219                 /* TODO: not implemented */
1220                 break;
1221
1222         case TYPE_BITFIELD:
1223                 /* not sure if this makes sense or is even needed, implement it if you
1224                  * really need it! */
1225                 panic("type compatibility check for bitfield type");
1226
1227         case TYPE_ERROR:
1228                 /* Hmm, the error type should be compatible to all other types */
1229                 return true;
1230         case TYPE_INVALID:
1231                 panic("invalid type found in compatible types");
1232         case TYPE_TYPEDEF:
1233         case TYPE_TYPEOF:
1234                 panic("typerefs not skipped in compatible types?!?");
1235         }
1236
1237         /* TODO: incomplete */
1238         return false;
1239 }
1240
1241 /**
1242  * Skip all typerefs and return the underlying type.
1243  */
1244 type_t *skip_typeref(type_t *type)
1245 {
1246         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1247
1248         while (true) {
1249                 switch (type->kind) {
1250                 case TYPE_ERROR:
1251                         return type;
1252                 case TYPE_TYPEDEF: {
1253                         qualifiers |= type->base.qualifiers;
1254
1255                         const typedef_type_t *typedef_type = &type->typedeft;
1256                         if (typedef_type->resolved_type != NULL) {
1257                                 type = typedef_type->resolved_type;
1258                                 break;
1259                         }
1260                         type = typedef_type->typedefe->type;
1261                         continue;
1262                 }
1263                 case TYPE_TYPEOF:
1264                         qualifiers |= type->base.qualifiers;
1265                         type        = type->typeoft.typeof_type;
1266                         continue;
1267                 default:
1268                         break;
1269                 }
1270                 break;
1271         }
1272
1273         if (qualifiers != TYPE_QUALIFIER_NONE) {
1274                 type_t *const copy = duplicate_type(type);
1275
1276                 /* for const with typedefed array type the element type has to be
1277                  * adjusted */
1278                 if (is_type_array(copy)) {
1279                         type_t *element_type           = copy->array.element_type;
1280                         element_type                   = duplicate_type(element_type);
1281                         element_type->base.qualifiers |= qualifiers;
1282                         copy->array.element_type       = element_type;
1283                 } else {
1284                         copy->base.qualifiers |= qualifiers;
1285                 }
1286
1287                 type = identify_new_type(copy);
1288         }
1289
1290         return type;
1291 }
1292
1293 unsigned get_type_size(type_t *type)
1294 {
1295         switch (type->kind) {
1296         case TYPE_INVALID:
1297                 break;
1298         case TYPE_ERROR:
1299                 return 0;
1300         case TYPE_ATOMIC:
1301                 return get_atomic_type_size(type->atomic.akind);
1302         case TYPE_COMPLEX:
1303                 return get_atomic_type_size(type->complex.akind) * 2;
1304         case TYPE_IMAGINARY:
1305                 return get_atomic_type_size(type->imaginary.akind);
1306         case TYPE_COMPOUND_UNION:
1307                 layout_union_type(&type->compound);
1308                 return type->compound.compound->size;
1309         case TYPE_COMPOUND_STRUCT:
1310                 layout_struct_type(&type->compound);
1311                 return type->compound.compound->size;
1312         case TYPE_ENUM:
1313                 return get_atomic_type_size(type->enumt.akind);
1314         case TYPE_FUNCTION:
1315                 return 0; /* non-const (but "address-const") */
1316         case TYPE_REFERENCE:
1317         case TYPE_POINTER:
1318                 /* TODO: make configurable by backend */
1319                 return 4;
1320         case TYPE_ARRAY: {
1321                 /* TODO: correct if element_type is aligned? */
1322                 il_size_t element_size = get_type_size(type->array.element_type);
1323                 return type->array.size * element_size;
1324         }
1325         case TYPE_BITFIELD:
1326                 return 0;
1327         case TYPE_BUILTIN:
1328                 return get_type_size(type->builtin.real_type);
1329         case TYPE_TYPEDEF:
1330                 return get_type_size(type->typedeft.typedefe->type);
1331         case TYPE_TYPEOF:
1332                 if (type->typeoft.typeof_type) {
1333                         return get_type_size(type->typeoft.typeof_type);
1334                 } else {
1335                         return get_type_size(type->typeoft.expression->base.type);
1336                 }
1337         }
1338         panic("invalid type in get_type_size");
1339 }
1340
1341 unsigned get_type_alignment(type_t *type)
1342 {
1343         switch (type->kind) {
1344         case TYPE_INVALID:
1345                 break;
1346         case TYPE_ERROR:
1347                 return 0;
1348         case TYPE_ATOMIC:
1349                 return get_atomic_type_alignment(type->atomic.akind);
1350         case TYPE_COMPLEX:
1351                 return get_atomic_type_alignment(type->complex.akind);
1352         case TYPE_IMAGINARY:
1353                 return get_atomic_type_alignment(type->imaginary.akind);
1354         case TYPE_COMPOUND_UNION:
1355                 layout_union_type(&type->compound);
1356                 return type->compound.compound->alignment;
1357         case TYPE_COMPOUND_STRUCT:
1358                 layout_struct_type(&type->compound);
1359                 return type->compound.compound->alignment;
1360         case TYPE_ENUM:
1361                 return get_atomic_type_alignment(type->enumt.akind);
1362         case TYPE_FUNCTION:
1363                 /* what is correct here? */
1364                 return 4;
1365         case TYPE_REFERENCE:
1366         case TYPE_POINTER:
1367                 /* TODO: make configurable by backend */
1368                 return 4;
1369         case TYPE_ARRAY:
1370                 return get_type_alignment(type->array.element_type);
1371         case TYPE_BITFIELD:
1372                 return 0;
1373         case TYPE_BUILTIN:
1374                 return get_type_alignment(type->builtin.real_type);
1375         case TYPE_TYPEDEF: {
1376                 il_alignment_t alignment
1377                         = get_type_alignment(type->typedeft.typedefe->type);
1378                 if (type->typedeft.typedefe->alignment > alignment)
1379                         alignment = type->typedeft.typedefe->alignment;
1380
1381                 return alignment;
1382         }
1383         case TYPE_TYPEOF:
1384                 if (type->typeoft.typeof_type) {
1385                         return get_type_alignment(type->typeoft.typeof_type);
1386                 } else {
1387                         return get_type_alignment(type->typeoft.expression->base.type);
1388                 }
1389         }
1390         panic("invalid type in get_type_alignment");
1391 }
1392
1393 decl_modifiers_t get_type_modifiers(const type_t *type)
1394 {
1395         switch(type->kind) {
1396         case TYPE_INVALID:
1397         case TYPE_ERROR:
1398                 break;
1399         case TYPE_COMPOUND_STRUCT:
1400         case TYPE_COMPOUND_UNION:
1401                 return type->compound.compound->modifiers;
1402         case TYPE_FUNCTION:
1403                 return type->function.modifiers;
1404         case TYPE_ENUM:
1405         case TYPE_ATOMIC:
1406         case TYPE_COMPLEX:
1407         case TYPE_IMAGINARY:
1408         case TYPE_REFERENCE:
1409         case TYPE_POINTER:
1410         case TYPE_BITFIELD:
1411         case TYPE_ARRAY:
1412                 return 0;
1413         case TYPE_BUILTIN:
1414                 return get_type_modifiers(type->builtin.real_type);
1415         case TYPE_TYPEDEF: {
1416                 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1417                 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1418                 return modifiers;
1419         }
1420         case TYPE_TYPEOF:
1421                 if (type->typeoft.typeof_type) {
1422                         return get_type_modifiers(type->typeoft.typeof_type);
1423                 } else {
1424                         return get_type_modifiers(type->typeoft.expression->base.type);
1425                 }
1426         }
1427         panic("invalid type found in get_type_modifiers");
1428 }
1429
1430 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1431 {
1432         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1433
1434         while (true) {
1435                 switch (type->base.kind) {
1436                 case TYPE_ERROR:
1437                         return TYPE_QUALIFIER_NONE;
1438                 case TYPE_TYPEDEF:
1439                         qualifiers |= type->base.qualifiers;
1440                         const typedef_type_t *typedef_type = &type->typedeft;
1441                         if (typedef_type->resolved_type != NULL)
1442                                 type = typedef_type->resolved_type;
1443                         else
1444                                 type = typedef_type->typedefe->type;
1445                         continue;
1446                 case TYPE_TYPEOF:
1447                         type = type->typeoft.typeof_type;
1448                         continue;
1449                 case TYPE_ARRAY:
1450                         if (skip_array_type) {
1451                                 type = type->array.element_type;
1452                                 continue;
1453                         }
1454                         break;
1455                 default:
1456                         break;
1457                 }
1458                 break;
1459         }
1460         return type->base.qualifiers | qualifiers;
1461 }
1462
1463 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1464 {
1465         assert(kind <= ATOMIC_TYPE_LAST);
1466         return atomic_type_properties[kind].size;
1467 }
1468
1469 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1470 {
1471         assert(kind <= ATOMIC_TYPE_LAST);
1472         return atomic_type_properties[kind].alignment;
1473 }
1474
1475 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1476 {
1477         assert(kind <= ATOMIC_TYPE_LAST);
1478         return atomic_type_properties[kind].flags;
1479 }
1480
1481 atomic_type_kind_t get_intptr_kind(void)
1482 {
1483         if (machine_size <= 32)
1484                 return ATOMIC_TYPE_INT;
1485         else if (machine_size <= 64)
1486                 return ATOMIC_TYPE_LONG;
1487         else
1488                 return ATOMIC_TYPE_LONGLONG;
1489 }
1490
1491 atomic_type_kind_t get_uintptr_kind(void)
1492 {
1493         if (machine_size <= 32)
1494                 return ATOMIC_TYPE_UINT;
1495         else if (machine_size <= 64)
1496                 return ATOMIC_TYPE_ULONG;
1497         else
1498                 return ATOMIC_TYPE_ULONGLONG;
1499 }
1500
1501 /**
1502  * Find the atomic type kind representing a given size (signed).
1503  */
1504 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1505 {
1506         static atomic_type_kind_t kinds[32];
1507
1508         assert(size < 32);
1509         atomic_type_kind_t kind = kinds[size];
1510         if (kind == ATOMIC_TYPE_INVALID) {
1511                 static const atomic_type_kind_t possible_kinds[] = {
1512                         ATOMIC_TYPE_SCHAR,
1513                         ATOMIC_TYPE_SHORT,
1514                         ATOMIC_TYPE_INT,
1515                         ATOMIC_TYPE_LONG,
1516                         ATOMIC_TYPE_LONGLONG
1517                 };
1518                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1519                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1520                                 kind = possible_kinds[i];
1521                                 break;
1522                         }
1523                 }
1524                 kinds[size] = kind;
1525         }
1526         return kind;
1527 }
1528
1529 /**
1530  * Find the atomic type kind representing a given size (signed).
1531  */
1532 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1533 {
1534         static atomic_type_kind_t kinds[32];
1535
1536         assert(size < 32);
1537         atomic_type_kind_t kind = kinds[size];
1538         if (kind == ATOMIC_TYPE_INVALID) {
1539                 static const atomic_type_kind_t possible_kinds[] = {
1540                         ATOMIC_TYPE_UCHAR,
1541                         ATOMIC_TYPE_USHORT,
1542                         ATOMIC_TYPE_UINT,
1543                         ATOMIC_TYPE_ULONG,
1544                         ATOMIC_TYPE_ULONGLONG
1545                 };
1546                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1547                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1548                                 kind = possible_kinds[i];
1549                                 break;
1550                         }
1551                 }
1552                 kinds[size] = kind;
1553         }
1554         return kind;
1555 }
1556
1557 /**
1558  * Hash the given type and return the "singleton" version
1559  * of it.
1560  */
1561 type_t *identify_new_type(type_t *type)
1562 {
1563         type_t *result = typehash_insert(type);
1564         if (result != type) {
1565                 obstack_free(type_obst, type);
1566         }
1567         return result;
1568 }
1569
1570 /**
1571  * Creates a new atomic type.
1572  *
1573  * @param akind       The kind of the atomic type.
1574  * @param qualifiers  Type qualifiers for the new type.
1575  */
1576 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1577 {
1578         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1579         memset(type, 0, sizeof(atomic_type_t));
1580
1581         type->kind            = TYPE_ATOMIC;
1582         type->base.qualifiers = qualifiers;
1583         type->atomic.akind    = akind;
1584
1585         return identify_new_type(type);
1586 }
1587
1588 /**
1589  * Creates a new complex type.
1590  *
1591  * @param akind       The kind of the atomic type.
1592  * @param qualifiers  Type qualifiers for the new type.
1593  */
1594 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1595 {
1596         type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1597         memset(type, 0, sizeof(complex_type_t));
1598
1599         type->kind            = TYPE_COMPLEX;
1600         type->base.qualifiers = qualifiers;
1601         type->complex.akind   = akind;
1602
1603         return identify_new_type(type);
1604 }
1605
1606 /**
1607  * Creates a new imaginary type.
1608  *
1609  * @param akind       The kind of the atomic type.
1610  * @param qualifiers  Type qualifiers for the new type.
1611  */
1612 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1613 {
1614         type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1615         memset(type, 0, sizeof(imaginary_type_t));
1616
1617         type->kind            = TYPE_IMAGINARY;
1618         type->base.qualifiers = qualifiers;
1619         type->imaginary.akind = akind;
1620
1621         return identify_new_type(type);
1622 }
1623
1624 /**
1625  * Creates a new pointer type.
1626  *
1627  * @param points_to   The points-to type for the new type.
1628  * @param qualifiers  Type qualifiers for the new type.
1629  */
1630 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1631 {
1632         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1633         memset(type, 0, sizeof(pointer_type_t));
1634
1635         type->kind                  = TYPE_POINTER;
1636         type->base.qualifiers       = qualifiers;
1637         type->pointer.points_to     = points_to;
1638         type->pointer.base_variable = NULL;
1639
1640         return identify_new_type(type);
1641 }
1642
1643 /**
1644  * Creates a new reference type.
1645  *
1646  * @param refers_to   The referred-to type for the new type.
1647  */
1648 type_t *make_reference_type(type_t *refers_to)
1649 {
1650         type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1651         memset(type, 0, sizeof(reference_type_t));
1652
1653         type->kind                = TYPE_REFERENCE;
1654         type->base.qualifiers     = 0;
1655         type->reference.refers_to = refers_to;
1656
1657         return identify_new_type(type);
1658 }
1659
1660 /**
1661  * Creates a new based pointer type.
1662  *
1663  * @param points_to   The points-to type for the new type.
1664  * @param qualifiers  Type qualifiers for the new type.
1665  * @param variable    The based variable
1666  */
1667 type_t *make_based_pointer_type(type_t *points_to,
1668                                                                 type_qualifiers_t qualifiers, variable_t *variable)
1669 {
1670         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1671         memset(type, 0, sizeof(pointer_type_t));
1672
1673         type->kind                  = TYPE_POINTER;
1674         type->base.qualifiers       = qualifiers;
1675         type->pointer.points_to     = points_to;
1676         type->pointer.base_variable = variable;
1677
1678         return identify_new_type(type);
1679 }
1680
1681
1682 type_t *make_array_type(type_t *element_type, size_t size,
1683                         type_qualifiers_t qualifiers)
1684 {
1685         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1686         memset(type, 0, sizeof(array_type_t));
1687
1688         type->kind                = TYPE_ARRAY;
1689         type->base.qualifiers     = qualifiers;
1690         type->array.element_type  = element_type;
1691         type->array.size          = size;
1692         type->array.size_constant = true;
1693
1694         return identify_new_type(type);
1695 }
1696
1697 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1698                                        il_alignment_t *struct_alignment,
1699                                                                            bool packed, entity_t *first)
1700 {
1701         il_size_t      offset     = *struct_offset;
1702         il_alignment_t alignment  = *struct_alignment;
1703         size_t         bit_offset = 0;
1704
1705         entity_t *member;
1706         for (member = first; member != NULL; member = member->base.next) {
1707                 if (member->kind != ENTITY_COMPOUND_MEMBER)
1708                         break;
1709
1710                 type_t *type = member->declaration.type;
1711                 if (type->kind != TYPE_BITFIELD)
1712                         break;
1713
1714                 type_t *base_type = skip_typeref(type->bitfield.base_type);
1715                 il_alignment_t base_alignment = get_type_alignment(base_type);
1716                 il_alignment_t alignment_mask = base_alignment-1;
1717                 if (base_alignment > alignment)
1718                         alignment = base_alignment;
1719
1720                 size_t bit_size = type->bitfield.bit_size;
1721                 if (!packed) {
1722                         bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1723                         offset     &= ~alignment_mask;
1724                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1725
1726                         if (bit_offset + bit_size > base_size || bit_size == 0) {
1727                                 offset    += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1728                                 offset     = (offset + base_alignment-1) & ~alignment_mask;
1729                                 bit_offset = 0;
1730                         }
1731                 }
1732
1733                 member->compound_member.offset     = offset;
1734                 member->compound_member.bit_offset = bit_offset;
1735
1736                 bit_offset += bit_size;
1737                 offset     += bit_offset / BITS_PER_BYTE;
1738                 bit_offset %= BITS_PER_BYTE;
1739         }
1740
1741         if (bit_offset > 0)
1742                 offset += 1;
1743
1744         *struct_offset    = offset;
1745         *struct_alignment = alignment;
1746
1747         return member;
1748 }
1749
1750 /**
1751  * Finish the construction of a struct type by calculating its size, offsets,
1752  * alignment.
1753  */
1754 void layout_struct_type(compound_type_t *type)
1755 {
1756         assert(type->compound != NULL);
1757
1758         compound_t *compound = type->compound;
1759         if (!compound->complete)
1760                 return;
1761         if (type->compound->layouted)
1762                 return;
1763
1764         il_size_t      offset    = 0;
1765         il_alignment_t alignment = compound->alignment;
1766         bool           need_pad  = false;
1767
1768         entity_t *entry = compound->members.entities;
1769         while (entry != NULL) {
1770                 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1771                         entry = entry->base.next;
1772                         continue;
1773                 }
1774
1775                 type_t *m_type  = entry->declaration.type;
1776                 type_t *skipped = skip_typeref(m_type);
1777                 if (! is_type_valid(skipped)) {
1778                         entry = entry->base.next;
1779                         continue;
1780                 }
1781
1782                 if (skipped->kind == TYPE_BITFIELD) {
1783                         entry = pack_bitfield_members(&offset, &alignment,
1784                                                       compound->packed, entry);
1785                         continue;
1786                 }
1787
1788                 il_alignment_t m_alignment = get_type_alignment(m_type);
1789                 if (m_alignment > alignment)
1790                         alignment = m_alignment;
1791
1792                 if (!compound->packed) {
1793                         il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1794
1795                         if (new_offset > offset) {
1796                                 need_pad = true;
1797                                 offset   = new_offset;
1798                         }
1799                 }
1800
1801                 entry->compound_member.offset = offset;
1802                 offset += get_type_size(m_type);
1803
1804                 entry = entry->base.next;
1805         }
1806
1807         if (!compound->packed) {
1808                 il_size_t new_offset = (offset + alignment-1) & -alignment;
1809                 if (new_offset > offset) {
1810                         need_pad = true;
1811                         offset   = new_offset;
1812                 }
1813         }
1814
1815         if (need_pad) {
1816                 if (warning.padded) {
1817                         warningf(&compound->base.source_position, "'%T' needs padding",
1818                                  type);
1819                 }
1820         } else if (compound->packed && warning.packed) {
1821                 warningf(&compound->base.source_position,
1822                          "superfluous packed attribute on '%T'", type);
1823         }
1824
1825         compound->size      = offset;
1826         compound->alignment = alignment;
1827         compound->layouted  = true;
1828 }
1829
1830 /**
1831  * Finish the construction of an union type by calculating
1832  * its size and alignment.
1833  */
1834 void layout_union_type(compound_type_t *type)
1835 {
1836         assert(type->compound != NULL);
1837
1838         compound_t *compound = type->compound;
1839         if (! compound->complete)
1840                 return;
1841
1842         il_size_t      size      = 0;
1843         il_alignment_t alignment = compound->alignment;
1844
1845         entity_t *entry = compound->members.entities;
1846         for (; entry != NULL; entry = entry->base.next) {
1847                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1848                         continue;
1849
1850                 type_t *m_type = entry->declaration.type;
1851                 if (! is_type_valid(skip_typeref(m_type)))
1852                         continue;
1853
1854                 entry->compound_member.offset = 0;
1855                 il_size_t m_size = get_type_size(m_type);
1856                 if (m_size > size)
1857                         size = m_size;
1858                 il_alignment_t m_alignment = get_type_alignment(m_type);
1859                 if (m_alignment > alignment)
1860                         alignment = m_alignment;
1861         }
1862         size = (size + alignment - 1) & -alignment;
1863
1864         compound->size      = size;
1865         compound->alignment = alignment;
1866 }
1867
1868 /**
1869  * Debug helper. Prints the given type to stdout.
1870  */
1871 static __attribute__((unused))
1872 void dbg_type(const type_t *type)
1873 {
1874         FILE *old_out = out;
1875         out = stderr;
1876         print_type(type);
1877         puts("\n");
1878         fflush(stderr);
1879         out = old_out;
1880 }