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