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