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