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