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