Put { of function on separate line.
[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 "entity_t.h"
27 #include "symbol_t.h"
28 #include "type_hash.h"
29 #include "adt/error.h"
30 #include "adt/util.h"
31 #include "lang_features.h"
32
33 static struct obstack   _type_obst;
34 static FILE            *out;
35 struct obstack         *type_obst                 = &_type_obst;
36 static int              type_visited              = 0;
37 static bool             print_implicit_array_size = false;
38
39 static void intern_print_type_pre(const type_t *type, bool top);
40 static void intern_print_type_post(const type_t *type, bool top);
41
42 typedef struct atomic_type_properties_t atomic_type_properties_t;
43 struct atomic_type_properties_t {
44         unsigned   size;              /**< type size in bytes */
45         unsigned   alignment;         /**< type alignment in bytes */
46         unsigned   flags;             /**< type flags from atomic_type_flag_t */
47 };
48
49 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
50         //ATOMIC_TYPE_INVALID = 0,
51         [ATOMIC_TYPE_VOID] = {
52                 .size       = 0,
53                 .alignment  = 0,
54                 .flags      = ATOMIC_TYPE_FLAG_NONE
55         },
56         [ATOMIC_TYPE_WCHAR_T] = {
57                 .size       = (unsigned)-1,
58                 .alignment  = (unsigned)-1,
59                 /* signed flag will be set when known */
60                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
61         },
62         [ATOMIC_TYPE_CHAR] = {
63                 .size       = 1,
64                 .alignment  = 1,
65                 /* signed flag will be set when known */
66                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
67         },
68         [ATOMIC_TYPE_SCHAR] = {
69                 .size       = 1,
70                 .alignment  = 1,
71                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
72                               | ATOMIC_TYPE_FLAG_SIGNED,
73         },
74         [ATOMIC_TYPE_UCHAR] = {
75                 .size       = 1,
76                 .alignment  = 1,
77                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
78         },
79         [ATOMIC_TYPE_SHORT] = {
80                 .size       = 2,
81                 .alignment  = 2,
82                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
83                               | ATOMIC_TYPE_FLAG_SIGNED
84         },
85         [ATOMIC_TYPE_USHORT] = {
86                 .size       = 2,
87                 .alignment  = 2,
88                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
89         },
90         [ATOMIC_TYPE_INT] = {
91                 .size       = (unsigned) -1,
92                 .alignment  = (unsigned) -1,
93                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
94                               | ATOMIC_TYPE_FLAG_SIGNED,
95         },
96         [ATOMIC_TYPE_UINT] = {
97                 .size       = (unsigned) -1,
98                 .alignment  = (unsigned) -1,
99                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
100         },
101         [ATOMIC_TYPE_LONG] = {
102                 .size       = (unsigned) -1,
103                 .alignment  = (unsigned) -1,
104                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
105                               | ATOMIC_TYPE_FLAG_SIGNED,
106         },
107         [ATOMIC_TYPE_ULONG] = {
108                 .size       = (unsigned) -1,
109                 .alignment  = (unsigned) -1,
110                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
111         },
112         [ATOMIC_TYPE_LONGLONG] = {
113                 .size       = (unsigned) -1,
114                 .alignment  = (unsigned) -1,
115                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
116                               | ATOMIC_TYPE_FLAG_SIGNED,
117         },
118         [ATOMIC_TYPE_ULONGLONG] = {
119                 .size       = (unsigned) -1,
120                 .alignment  = (unsigned) -1,
121                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
122         },
123         [ATOMIC_TYPE_BOOL] = {
124                 .size       = (unsigned) -1,
125                 .alignment  = (unsigned) -1,
126                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
127         },
128         [ATOMIC_TYPE_FLOAT] = {
129                 .size       = 4,
130                 .alignment  = (unsigned) -1,
131                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
132                               | ATOMIC_TYPE_FLAG_SIGNED,
133         },
134         [ATOMIC_TYPE_DOUBLE] = {
135                 .size       = 8,
136                 .alignment  = (unsigned) -1,
137                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
138                               | ATOMIC_TYPE_FLAG_SIGNED,
139         },
140         [ATOMIC_TYPE_LONG_DOUBLE] = {
141                 .size       = 12,
142                 .alignment  = (unsigned) -1,
143                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
144                               | ATOMIC_TYPE_FLAG_SIGNED,
145         },
146         /* complex and imaginary types are set in init_types */
147 };
148
149 void init_types(void)
150 {
151         obstack_init(type_obst);
152
153         atomic_type_properties_t *props = atomic_type_properties;
154
155         if (char_is_signed) {
156                 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
157         }
158
159         unsigned int_size   = machine_size < 32 ? 2 : 4;
160         unsigned long_size  = machine_size < 64 ? 4 : 8;
161         unsigned llong_size = machine_size < 32 ? 4 : 8;
162
163         props[ATOMIC_TYPE_INT].size            = int_size;
164         props[ATOMIC_TYPE_INT].alignment       = int_size;
165         props[ATOMIC_TYPE_UINT].size           = int_size;
166         props[ATOMIC_TYPE_UINT].alignment      = int_size;
167         props[ATOMIC_TYPE_LONG].size           = long_size;
168         props[ATOMIC_TYPE_LONG].alignment      = long_size;
169         props[ATOMIC_TYPE_ULONG].size          = long_size;
170         props[ATOMIC_TYPE_ULONG].alignment     = long_size;
171         props[ATOMIC_TYPE_LONGLONG].size       = llong_size;
172         props[ATOMIC_TYPE_LONGLONG].alignment  = llong_size;
173         props[ATOMIC_TYPE_ULONGLONG].size      = llong_size;
174         props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
175
176         /* TODO: backend specific, need a way to query the backend for this.
177          * The following are good settings for x86 */
178         props[ATOMIC_TYPE_FLOAT].alignment       = 4;
179         props[ATOMIC_TYPE_DOUBLE].alignment      = 4;
180         props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
181         props[ATOMIC_TYPE_LONGLONG].alignment    = 4;
182         props[ATOMIC_TYPE_ULONGLONG].alignment   = 4;
183
184         /* TODO: make this configurable for platforms which do not use byte sized
185          * bools. */
186         props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
187
188         props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
189 }
190
191 void exit_types(void)
192 {
193         obstack_free(type_obst, NULL);
194 }
195
196 void type_set_output(FILE *stream)
197 {
198         out = stream;
199 }
200
201 void inc_type_visited(void)
202 {
203         type_visited++;
204 }
205
206 void print_type_qualifiers(type_qualifiers_t qualifiers)
207 {
208         int first = 1;
209         if (qualifiers & TYPE_QUALIFIER_CONST) {
210                 fputs(" const" + first,    out);
211                 first = 0;
212         }
213         if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
214                 fputs(" volatile" + first, out);
215                 first = 0;
216         }
217         if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
218                 fputs(" restrict" + first, out);
219                 first = 0;
220         }
221 }
222
223 const char *get_atomic_kind_name(atomic_type_kind_t kind)
224 {
225         switch(kind) {
226         case ATOMIC_TYPE_INVALID: break;
227         case ATOMIC_TYPE_VOID:        return "void";
228         case ATOMIC_TYPE_WCHAR_T:     return "wchar_t";
229         case ATOMIC_TYPE_BOOL:        return c_mode & _CXX ? "bool" : "_Bool";
230         case ATOMIC_TYPE_CHAR:        return "char";
231         case ATOMIC_TYPE_SCHAR:       return "signed char";
232         case ATOMIC_TYPE_UCHAR:       return "unsigned char";
233         case ATOMIC_TYPE_INT:         return "int";
234         case ATOMIC_TYPE_UINT:        return "unsigned int";
235         case ATOMIC_TYPE_SHORT:       return "short";
236         case ATOMIC_TYPE_USHORT:      return "unsigned short";
237         case ATOMIC_TYPE_LONG:        return "long";
238         case ATOMIC_TYPE_ULONG:       return "unsigned long";
239         case ATOMIC_TYPE_LONGLONG:    return "long long";
240         case ATOMIC_TYPE_ULONGLONG:   return "unsigned long long";
241         case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
242         case ATOMIC_TYPE_FLOAT:       return "float";
243         case ATOMIC_TYPE_DOUBLE:      return "double";
244         }
245         return "INVALIDATOMIC";
246 }
247
248 /**
249  * Prints the name of an atomic type kinds.
250  *
251  * @param kind  The type kind.
252  */
253 static void print_atomic_kinds(atomic_type_kind_t kind)
254 {
255         const char *s = get_atomic_kind_name(kind);
256         fputs(s, out);
257 }
258
259 /**
260  * Prints the name of an atomic type.
261  *
262  * @param type  The type.
263  */
264 static void print_atomic_type(const atomic_type_t *type)
265 {
266         print_type_qualifiers(type->base.qualifiers);
267         if (type->base.qualifiers != 0)
268                 fputc(' ', out);
269         print_atomic_kinds(type->akind);
270 }
271
272 /**
273  * Prints the name of a complex type.
274  *
275  * @param type  The type.
276  */
277 static
278 void print_complex_type(const complex_type_t *type)
279 {
280         int empty = type->base.qualifiers == 0;
281         print_type_qualifiers(type->base.qualifiers);
282         fputs(" _Complex " + empty, out);
283         print_atomic_kinds(type->akind);
284 }
285
286 /**
287  * Prints the name of an imaginary type.
288  *
289  * @param type  The type.
290  */
291 static
292 void print_imaginary_type(const imaginary_type_t *type)
293 {
294         int empty = type->base.qualifiers == 0;
295         print_type_qualifiers(type->base.qualifiers);
296         fputs(" _Imaginary " + empty, out);
297         print_atomic_kinds(type->akind);
298 }
299
300 /**
301  * Print the first part (the prefix) of a type.
302  *
303  * @param type   The type to print.
304  * @param top    true, if this is the top type, false if it's an embedded type.
305  */
306 static void print_function_type_pre(const function_type_t *type, bool top)
307 {
308         switch (type->linkage) {
309                 case LINKAGE_INVALID:
310                         break;
311
312                 case LINKAGE_C:
313                         if (c_mode & _CXX)
314                                 fputs("extern \"C\" ",   out);
315                         break;
316
317                 case LINKAGE_CXX:
318                         if (!(c_mode & _CXX))
319                                 fputs("extern \"C++\" ", out);
320                         break;
321         }
322
323         print_type_qualifiers(type->base.qualifiers);
324         if (type->base.qualifiers != 0)
325                 fputc(' ', out);
326
327         intern_print_type_pre(type->return_type, false);
328
329         switch (type->calling_convention) {
330         case CC_CDECL:    fputs("__cdecl ",    out); break;
331         case CC_STDCALL:  fputs("__stdcall ",  out); break;
332         case CC_FASTCALL: fputs("__fastcall ", out); break;
333         case CC_THISCALL: fputs("__thiscall ", out); break;
334         case CC_DEFAULT:  break;
335         }
336
337         /* don't emit parenthesis if we're the toplevel type... */
338         if (!top)
339                 fputc('(', out);
340 }
341
342 /**
343  * Print the second part (the postfix) of a type.
344  *
345  * @param type   The type to print.
346  * @param top    true, if this is the top type, false if it's an embedded type.
347  */
348 static void print_function_type_post(const function_type_t *type,
349                                      const scope_t *parameters, bool top)
350 {
351         /* don't emit parenthesis if we're the toplevel type... */
352         if (!top)
353                 fputc(')', out);
354
355         fputc('(', out);
356         bool first = true;
357         if (parameters == NULL) {
358                 function_parameter_t *parameter = type->parameters;
359                 for( ; parameter != NULL; parameter = parameter->next) {
360                         if (first) {
361                                 first = false;
362                         } else {
363                                 fputs(", ", out);
364                         }
365                         print_type(parameter->type);
366                 }
367         } else {
368                 entity_t *parameter = parameters->entities;
369                 for (; parameter != NULL; parameter = parameter->base.next) {
370                         if (first) {
371                                 first = false;
372                         } else {
373                                 fputs(", ", out);
374                         }
375                         assert(is_declaration(parameter));
376                         const type_t *const type = parameter->declaration.type;
377                         if (type == NULL) {
378                                 fputs(parameter->base.symbol->string, out);
379                         } else {
380                                 print_type_ext(type, parameter->base.symbol, NULL);
381                         }
382                 }
383         }
384         if (type->variadic) {
385                 if (first) {
386                         first = false;
387                 } else {
388                         fputs(", ", out);
389                 }
390                 fputs("...", out);
391         }
392         if (first && !type->unspecified_parameters) {
393                 fputs("void", out);
394         }
395         fputc(')', out);
396
397         intern_print_type_post(type->return_type, false);
398 }
399
400 /**
401  * Prints the prefix part of a pointer type.
402  *
403  * @param type   The pointer type.
404  */
405 static void print_pointer_type_pre(const pointer_type_t *type)
406 {
407         intern_print_type_pre(type->points_to, false);
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 prefix part of a reference type.
423  *
424  * @param type   The reference type.
425  */
426 static void print_reference_type_pre(const reference_type_t *type)
427 {
428         intern_print_type_pre(type->refers_to, false);
429         fputc('&', out);
430 }
431
432 /**
433  * Prints the postfix part of a pointer type.
434  *
435  * @param type   The pointer type.
436  */
437 static void print_pointer_type_post(const pointer_type_t *type)
438 {
439         intern_print_type_post(type->points_to, false);
440 }
441
442 /**
443  * Prints the postfix part of a reference type.
444  *
445  * @param type   The reference type.
446  */
447 static void print_reference_type_post(const reference_type_t *type)
448 {
449         intern_print_type_post(type->refers_to, false);
450 }
451
452 /**
453  * Prints the prefix part of an array type.
454  *
455  * @param type   The array type.
456  */
457 static void print_array_type_pre(const array_type_t *type)
458 {
459         intern_print_type_pre(type->element_type, false);
460 }
461
462 /**
463  * Prints the postfix part of an array type.
464  *
465  * @param type   The array type.
466  */
467 static void print_array_type_post(const array_type_t *type)
468 {
469         fputc('[', out);
470         if (type->is_static) {
471                 fputs("static ", out);
472         }
473         print_type_qualifiers(type->base.qualifiers);
474         if (type->base.qualifiers != 0)
475                 fputc(' ', out);
476         if (type->size_expression != NULL
477                         && (print_implicit_array_size || !type->has_implicit_size)) {
478                 print_expression(type->size_expression);
479         }
480         fputc(']', out);
481         intern_print_type_post(type->element_type, false);
482 }
483
484 /**
485  * Prints the postfix part of a bitfield type.
486  *
487  * @param type   The array type.
488  */
489 static void print_bitfield_type_post(const bitfield_type_t *type)
490 {
491         fputs(" : ", out);
492         print_expression(type->size_expression);
493         intern_print_type_post(type->base_type, false);
494 }
495
496 /**
497  * Prints an enum definition.
498  *
499  * @param declaration  The enum's type declaration.
500  */
501 void print_enum_definition(const enum_t *enume)
502 {
503         fputs("{\n", out);
504
505         change_indent(1);
506
507         entity_t *entry = enume->base.next;
508         for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
509                entry = entry->base.next) {
510
511                 print_indent();
512                 fputs(entry->base.symbol->string, out);
513                 if (entry->enum_value.value != NULL) {
514                         fputs(" = ", out);
515
516                         /* skip the implicit cast */
517                         expression_t *expression = entry->enum_value.value;
518                         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
519                                 expression = expression->unary.value;
520                         }
521                         print_expression(expression);
522                 }
523                 fputs(",\n", out);
524         }
525
526         change_indent(-1);
527         print_indent();
528         fputc('}', out);
529 }
530
531 /**
532  * Prints an enum type.
533  *
534  * @param type  The enum type.
535  */
536 static void print_type_enum(const enum_type_t *type)
537 {
538         int empty = type->base.qualifiers == 0;
539         print_type_qualifiers(type->base.qualifiers);
540         fputs(" enum " + empty, out);
541
542         enum_t   *enume  = type->enume;
543         symbol_t *symbol = enume->base.symbol;
544         if (symbol != NULL) {
545                 fputs(symbol->string, out);
546         } else {
547                 print_enum_definition(enume);
548         }
549 }
550
551 /**
552  * Print the compound part of a compound type.
553  */
554 void print_compound_definition(const compound_t *compound)
555 {
556         fputs("{\n", out);
557         change_indent(1);
558
559         entity_t *entity = compound->members.entities;
560         for( ; entity != NULL; entity = entity->base.next) {
561                 if (entity->kind != ENTITY_COMPOUND_MEMBER)
562                         continue;
563
564                 print_indent();
565                 print_entity(entity);
566                 fputc('\n', out);
567         }
568
569         change_indent(-1);
570         print_indent();
571         fputc('}', out);
572         if (compound->modifiers & DM_TRANSPARENT_UNION) {
573                 fputs("__attribute__((__transparent_union__))", out);
574         }
575 }
576
577 /**
578  * Prints a compound type.
579  *
580  * @param type  The compound type.
581  */
582 static void print_compound_type(const compound_type_t *type)
583 {
584         int empty = type->base.qualifiers == 0;
585         print_type_qualifiers(type->base.qualifiers);
586
587         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
588                 fputs(" struct " + empty, out);
589         } else {
590                 assert(type->base.kind == TYPE_COMPOUND_UNION);
591                 fputs(" union " + empty, out);
592         }
593
594         compound_t *compound = type->compound;
595         symbol_t   *symbol   = compound->base.symbol;
596         if (symbol != NULL) {
597                 fputs(symbol->string, out);
598         } else {
599                 print_compound_definition(compound);
600         }
601 }
602
603 /**
604  * Prints the prefix part of a typedef type.
605  *
606  * @param type   The typedef type.
607  */
608 static void print_typedef_type_pre(const typedef_type_t *const type)
609 {
610         print_type_qualifiers(type->base.qualifiers);
611         if (type->base.qualifiers != 0)
612                 fputc(' ', out);
613         fputs(type->typedefe->base.symbol->string, out);
614 }
615
616 /**
617  * Prints the prefix part of a typeof type.
618  *
619  * @param type   The typeof type.
620  */
621 static void print_typeof_type_pre(const typeof_type_t *const type)
622 {
623         fputs("typeof(", out);
624         if (type->expression != NULL) {
625                 print_expression(type->expression);
626         } else {
627                 print_type(type->typeof_type);
628         }
629         fputc(')', out);
630 }
631
632 /**
633  * Prints the prefix part of a type.
634  *
635  * @param type   The type.
636  * @param top    true if we print the toplevel type, false else.
637  */
638 static void intern_print_type_pre(const type_t *const type, const bool top)
639 {
640         switch(type->kind) {
641         case TYPE_ERROR:
642                 fputs("<error>", out);
643                 return;
644         case TYPE_INVALID:
645                 fputs("<invalid>", out);
646                 return;
647         case TYPE_ENUM:
648                 print_type_enum(&type->enumt);
649                 return;
650         case TYPE_ATOMIC:
651                 print_atomic_type(&type->atomic);
652                 return;
653         case TYPE_COMPLEX:
654                 print_complex_type(&type->complex);
655                 return;
656         case TYPE_IMAGINARY:
657                 print_imaginary_type(&type->imaginary);
658                 return;
659         case TYPE_COMPOUND_STRUCT:
660         case TYPE_COMPOUND_UNION:
661                 print_compound_type(&type->compound);
662                 return;
663         case TYPE_BUILTIN:
664                 fputs(type->builtin.symbol->string, out);
665                 return;
666         case TYPE_FUNCTION:
667                 print_function_type_pre(&type->function, top);
668                 return;
669         case TYPE_POINTER:
670                 print_pointer_type_pre(&type->pointer);
671                 return;
672         case TYPE_REFERENCE:
673                 print_reference_type_pre(&type->reference);
674                 return;
675         case TYPE_BITFIELD:
676                 intern_print_type_pre(type->bitfield.base_type, top);
677                 return;
678         case TYPE_ARRAY:
679                 print_array_type_pre(&type->array);
680                 return;
681         case TYPE_TYPEDEF:
682                 print_typedef_type_pre(&type->typedeft);
683                 return;
684         case TYPE_TYPEOF:
685                 print_typeof_type_pre(&type->typeoft);
686                 return;
687         }
688         fputs("unknown", out);
689 }
690
691 /**
692  * Prints the postfix part of a type.
693  *
694  * @param type   The type.
695  * @param top    true if we print the toplevel type, false else.
696  */
697 static void intern_print_type_post(const type_t *const type, const bool top)
698 {
699         switch(type->kind) {
700         case TYPE_FUNCTION:
701                 print_function_type_post(&type->function, NULL, top);
702                 return;
703         case TYPE_POINTER:
704                 print_pointer_type_post(&type->pointer);
705                 return;
706         case TYPE_REFERENCE:
707                 print_reference_type_post(&type->reference);
708                 return;
709         case TYPE_ARRAY:
710                 print_array_type_post(&type->array);
711                 return;
712         case TYPE_BITFIELD:
713                 print_bitfield_type_post(&type->bitfield);
714                 return;
715         case TYPE_ERROR:
716         case TYPE_INVALID:
717         case TYPE_ATOMIC:
718         case TYPE_COMPLEX:
719         case TYPE_IMAGINARY:
720         case TYPE_ENUM:
721         case TYPE_COMPOUND_STRUCT:
722         case TYPE_COMPOUND_UNION:
723         case TYPE_BUILTIN:
724         case TYPE_TYPEOF:
725         case TYPE_TYPEDEF:
726                 break;
727         }
728
729         if (type->base.modifiers & DM_TRANSPARENT_UNION) {
730                 fputs("__attribute__((__transparent_union__))", out);
731         }
732 }
733
734 /**
735  * Prints a type.
736  *
737  * @param type   The type.
738  */
739 void print_type(const type_t *const type)
740 {
741         print_type_ext(type, NULL, NULL);
742 }
743
744 void print_type_ext(const type_t *const type, const symbol_t *symbol,
745                     const scope_t *parameters)
746 {
747         if (type == NULL) {
748                 fputs("nil type", out);
749                 return;
750         }
751
752         intern_print_type_pre(type, true);
753         if (symbol != NULL) {
754                 fputc(' ', out);
755                 fputs(symbol->string, out);
756         }
757         if (type->kind == TYPE_FUNCTION) {
758                 print_function_type_post(&type->function, parameters, true);
759         } else {
760                 intern_print_type_post(type, true);
761         }
762 }
763
764 /**
765  * Return the size of a type AST node.
766  *
767  * @param type  The type.
768  */
769 static size_t get_type_size(const type_t *type)
770 {
771         switch(type->kind) {
772         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
773         case TYPE_COMPLEX:         return sizeof(complex_type_t);
774         case TYPE_IMAGINARY:       return sizeof(imaginary_type_t);
775         case TYPE_COMPOUND_STRUCT:
776         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
777         case TYPE_ENUM:            return sizeof(enum_type_t);
778         case TYPE_FUNCTION:        return sizeof(function_type_t);
779         case TYPE_POINTER:         return sizeof(pointer_type_t);
780         case TYPE_REFERENCE:       return sizeof(reference_type_t);
781         case TYPE_ARRAY:           return sizeof(array_type_t);
782         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
783         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
784         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
785         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
786         case TYPE_ERROR:           panic("error type found");
787         case TYPE_INVALID:         panic("invalid type found");
788         }
789         panic("unknown type found");
790 }
791
792 /**
793  * Duplicates a type.
794  *
795  * @param type  The type to copy.
796  * @return A copy of the type.
797  *
798  * @note This does not produce a deep copy!
799  */
800 type_t *duplicate_type(const type_t *type)
801 {
802         size_t size = get_type_size(type);
803
804         type_t *copy = obstack_alloc(type_obst, size);
805         memcpy(copy, type, size);
806         copy->base.firm_type = NULL;
807
808         return copy;
809 }
810
811 /**
812  * Returns the unqualified type of a given type.
813  *
814  * @param type  The type.
815  * @returns The unqualified type.
816  */
817 type_t *get_unqualified_type(type_t *type)
818 {
819         assert(!is_typeref(type));
820
821         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
822                 return type;
823
824         type_t *unqualified_type          = duplicate_type(type);
825         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
826
827         return identify_new_type(unqualified_type);
828 }
829
830 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
831 {
832         type_t *type = skip_typeref(orig_type);
833
834         type_t *copy;
835         if (is_type_array(type)) {
836                 /* For array types the element type has to be adjusted */
837                 type_t *element_type      = type->array.element_type;
838                 type_t *qual_element_type = get_qualified_type(element_type, qual);
839
840                 if (qual_element_type == element_type)
841                         return orig_type;
842
843                 copy                     = duplicate_type(type);
844                 copy->array.element_type = qual_element_type;
845         } else if (is_type_valid(type)) {
846                 if ((type->base.qualifiers & qual) == qual)
847                         return orig_type;
848
849                 copy                   = duplicate_type(type);
850                 copy->base.qualifiers |= qual;
851         } else {
852                 return type;
853         }
854
855         return identify_new_type(copy);
856 }
857
858 /**
859  * Check if a type is valid.
860  *
861  * @param type  The type to check.
862  * @return true if type represents a valid type.
863  */
864 bool type_valid(const type_t *type)
865 {
866         return type->kind != TYPE_INVALID;
867 }
868
869 static bool test_atomic_type_flag(atomic_type_kind_t kind,
870                                   atomic_type_flag_t flag)
871 {
872         assert(kind <= ATOMIC_TYPE_LAST);
873         return (atomic_type_properties[kind].flags & flag) != 0;
874 }
875
876 /**
877  * Returns true if the given type is an integer type.
878  *
879  * @param type  The type to check.
880  * @return True if type is an integer type.
881  */
882 bool is_type_integer(const type_t *type)
883 {
884         assert(!is_typeref(type));
885
886         if (type->kind == TYPE_ENUM)
887                 return true;
888         if (type->kind == TYPE_BITFIELD)
889                 return true;
890
891         if (type->kind != TYPE_ATOMIC)
892                 return false;
893
894         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
895 }
896
897 /**
898  * Returns true if the given type is an enum type.
899  *
900  * @param type  The type to check.
901  * @return True if type is an enum type.
902  */
903 bool is_type_enum(const type_t *type)
904 {
905         assert(!is_typeref(type));
906         return type->kind == TYPE_ENUM;
907 }
908
909 /**
910  * Returns true if the given type is an floating point type.
911  *
912  * @param type  The type to check.
913  * @return True if type is a floating point type.
914  */
915 bool is_type_float(const type_t *type)
916 {
917         assert(!is_typeref(type));
918
919         if (type->kind != TYPE_ATOMIC)
920                 return false;
921
922         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
923 }
924
925 /**
926  * Returns true if the given type is an complex type.
927  *
928  * @param type  The type to check.
929  * @return True if type is a complex type.
930  */
931 bool is_type_complex(const type_t *type)
932 {
933         assert(!is_typeref(type));
934
935         if (type->kind != TYPE_ATOMIC)
936                 return false;
937
938         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
939 }
940
941 /**
942  * Returns true if the given type is a signed type.
943  *
944  * @param type  The type to check.
945  * @return True if type is a signed type.
946  */
947 bool is_type_signed(const type_t *type)
948 {
949         assert(!is_typeref(type));
950
951         /* enum types are int for now */
952         if (type->kind == TYPE_ENUM)
953                 return true;
954         if (type->kind == TYPE_BITFIELD)
955                 return is_type_signed(type->bitfield.base_type);
956
957         if (type->kind != TYPE_ATOMIC)
958                 return false;
959
960         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
961 }
962
963 /**
964  * Returns true if the given type represents an arithmetic type.
965  *
966  * @param type  The type to check.
967  * @return True if type represents an arithmetic type.
968  */
969 bool is_type_arithmetic(const type_t *type)
970 {
971         assert(!is_typeref(type));
972
973         switch(type->kind) {
974         case TYPE_BITFIELD:
975         case TYPE_ENUM:
976                 return true;
977         case TYPE_ATOMIC:
978                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
979         case TYPE_COMPLEX:
980                 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
981         case TYPE_IMAGINARY:
982                 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
983         default:
984                 return false;
985         }
986 }
987
988 /**
989  * Returns true if the given type is an integer or float type.
990  *
991  * @param type  The type to check.
992  * @return True if type is an integer or float type.
993  */
994 bool is_type_real(const type_t *type)
995 {
996         /* 6.2.5 (17) */
997         return is_type_integer(type) || is_type_float(type);
998 }
999
1000 /**
1001  * Returns true if the given type represents a scalar type.
1002  *
1003  * @param type  The type to check.
1004  * @return True if type represents a scalar type.
1005  */
1006 bool is_type_scalar(const type_t *type)
1007 {
1008         assert(!is_typeref(type));
1009
1010         switch (type->kind) {
1011                 case TYPE_POINTER: return true;
1012                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1013                 default:           break;
1014         }
1015
1016         return is_type_arithmetic(type);
1017 }
1018
1019 /**
1020  * Check if a given type is incomplete.
1021  *
1022  * @param type  The type to check.
1023  * @return True if the given type is incomplete (ie. just forward).
1024  */
1025 bool is_type_incomplete(const type_t *type)
1026 {
1027         assert(!is_typeref(type));
1028
1029         switch(type->kind) {
1030         case TYPE_COMPOUND_STRUCT:
1031         case TYPE_COMPOUND_UNION: {
1032                 const compound_type_t *compound_type = &type->compound;
1033                 return !compound_type->compound->complete;
1034         }
1035         case TYPE_ENUM:
1036                 return false;
1037
1038         case TYPE_ARRAY:
1039                 return type->array.size_expression == NULL
1040                         && !type->array.size_constant;
1041
1042         case TYPE_ATOMIC:
1043                 return type->atomic.akind == ATOMIC_TYPE_VOID;
1044
1045         case TYPE_COMPLEX:
1046                 return type->complex.akind == ATOMIC_TYPE_VOID;
1047
1048         case TYPE_IMAGINARY:
1049                 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1050
1051         case TYPE_BITFIELD:
1052         case TYPE_FUNCTION:
1053         case TYPE_POINTER:
1054         case TYPE_REFERENCE:
1055         case TYPE_BUILTIN:
1056         case TYPE_ERROR:
1057                 return false;
1058
1059         case TYPE_TYPEDEF:
1060         case TYPE_TYPEOF:
1061                 panic("is_type_incomplete called without typerefs skipped");
1062         case TYPE_INVALID:
1063                 break;
1064         }
1065
1066         panic("invalid type found");
1067 }
1068
1069 bool is_type_object(const type_t *type)
1070 {
1071         return !is_type_function(type) && !is_type_incomplete(type);
1072 }
1073
1074 /**
1075  * Check if two function types are compatible.
1076  */
1077 static bool function_types_compatible(const function_type_t *func1,
1078                                       const function_type_t *func2)
1079 {
1080         const type_t* const ret1 = skip_typeref(func1->return_type);
1081         const type_t* const ret2 = skip_typeref(func2->return_type);
1082         if (!types_compatible(ret1, ret2))
1083                 return false;
1084
1085         if (func1->linkage != func2->linkage)
1086                 return false;
1087
1088         if (func1->calling_convention != func2->calling_convention)
1089                 return false;
1090
1091         /* can parameters be compared? */
1092         if (func1->unspecified_parameters || func2->unspecified_parameters)
1093                 return true;
1094
1095         if (func1->variadic != func2->variadic)
1096                 return false;
1097
1098         /* TODO: handling of unspecified parameters not correct yet */
1099
1100         /* all argument types must be compatible */
1101         function_parameter_t *parameter1 = func1->parameters;
1102         function_parameter_t *parameter2 = func2->parameters;
1103         for ( ; parameter1 != NULL && parameter2 != NULL;
1104                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
1105                 type_t *parameter1_type = skip_typeref(parameter1->type);
1106                 type_t *parameter2_type = skip_typeref(parameter2->type);
1107
1108                 parameter1_type = get_unqualified_type(parameter1_type);
1109                 parameter2_type = get_unqualified_type(parameter2_type);
1110
1111                 if (!types_compatible(parameter1_type, parameter2_type))
1112                         return false;
1113         }
1114         /* same number of arguments? */
1115         if (parameter1 != NULL || parameter2 != NULL)
1116                 return false;
1117
1118         return true;
1119 }
1120
1121 /**
1122  * Check if two array types are compatible.
1123  */
1124 static bool array_types_compatible(const array_type_t *array1,
1125                                    const array_type_t *array2)
1126 {
1127         type_t *element_type1 = skip_typeref(array1->element_type);
1128         type_t *element_type2 = skip_typeref(array2->element_type);
1129         if (!types_compatible(element_type1, element_type2))
1130                 return false;
1131
1132         if (!array1->size_constant || !array2->size_constant)
1133                 return true;
1134
1135         return array1->size == array2->size;
1136 }
1137
1138 /**
1139  * Check if two types are compatible.
1140  */
1141 bool types_compatible(const type_t *type1, const type_t *type2)
1142 {
1143         assert(!is_typeref(type1));
1144         assert(!is_typeref(type2));
1145
1146         /* shortcut: the same type is always compatible */
1147         if (type1 == type2)
1148                 return true;
1149
1150         if (!is_type_valid(type1) || !is_type_valid(type2))
1151                 return true;
1152
1153         if (type1->base.qualifiers != type2->base.qualifiers)
1154                 return false;
1155         if (type1->kind != type2->kind)
1156                 return false;
1157
1158         switch (type1->kind) {
1159         case TYPE_FUNCTION:
1160                 return function_types_compatible(&type1->function, &type2->function);
1161         case TYPE_ATOMIC:
1162                 return type1->atomic.akind == type2->atomic.akind;
1163         case TYPE_COMPLEX:
1164                 return type1->complex.akind == type2->complex.akind;
1165         case TYPE_IMAGINARY:
1166                 return type1->imaginary.akind == type2->imaginary.akind;
1167         case TYPE_ARRAY:
1168                 return array_types_compatible(&type1->array, &type2->array);
1169
1170         case TYPE_POINTER: {
1171                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1172                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1173                 return types_compatible(to1, to2);
1174         }
1175
1176         case TYPE_REFERENCE: {
1177                 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1178                 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1179                 return types_compatible(to1, to2);
1180         }
1181
1182         case TYPE_COMPOUND_STRUCT:
1183         case TYPE_COMPOUND_UNION:
1184         case TYPE_ENUM:
1185         case TYPE_BUILTIN:
1186                 /* TODO: not implemented */
1187                 break;
1188
1189         case TYPE_BITFIELD:
1190                 /* not sure if this makes sense or is even needed, implement it if you
1191                  * really need it! */
1192                 panic("type compatibility check for bitfield type");
1193
1194         case TYPE_ERROR:
1195                 /* Hmm, the error type should be compatible to all other types */
1196                 return true;
1197         case TYPE_INVALID:
1198                 panic("invalid type found in compatible types");
1199         case TYPE_TYPEDEF:
1200         case TYPE_TYPEOF:
1201                 panic("typerefs not skipped in compatible types?!?");
1202         }
1203
1204         /* TODO: incomplete */
1205         return false;
1206 }
1207
1208 /**
1209  * Skip all typerefs and return the underlying type.
1210  */
1211 type_t *skip_typeref(type_t *type)
1212 {
1213         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1214         type_modifiers_t  modifiers  = TYPE_MODIFIER_NONE;
1215         il_alignment_t    alignment  = 0;
1216
1217         while (true) {
1218                 if (alignment < type->base.alignment)
1219                         alignment = type->base.alignment;
1220
1221                 switch (type->kind) {
1222                 case TYPE_ERROR:
1223                         return type;
1224                 case TYPE_TYPEDEF: {
1225                         qualifiers |= type->base.qualifiers;
1226                         modifiers  |= type->base.modifiers;
1227
1228                         const typedef_type_t *typedef_type = &type->typedeft;
1229                         if (typedef_type->resolved_type != NULL) {
1230                                 type = typedef_type->resolved_type;
1231                                 break;
1232                         }
1233                         type = typedef_type->typedefe->type;
1234                         continue;
1235                 }
1236                 case TYPE_TYPEOF: {
1237                         qualifiers |= type->base.qualifiers;
1238                         modifiers  |= type->base.modifiers;
1239
1240                         const typeof_type_t *typeof_type = &type->typeoft;
1241                         if (typeof_type->typeof_type != NULL) {
1242                                 type = typeof_type->typeof_type;
1243                         } else {
1244                                 type = typeof_type->expression->base.type;
1245                         }
1246                         continue;
1247                 }
1248                 default:
1249                         break;
1250                 }
1251                 break;
1252         }
1253
1254         if (qualifiers != TYPE_QUALIFIER_NONE ||
1255                         modifiers  != TYPE_MODIFIER_NONE  ||
1256                         alignment  >  type->base.alignment) {
1257                 type_t *const copy = duplicate_type(type);
1258
1259                 /* for const with typedefed array type the element type has to be
1260                  * adjusted */
1261                 if (is_type_array(copy)) {
1262                         type_t *element_type           = copy->array.element_type;
1263                         element_type                   = duplicate_type(element_type);
1264                         element_type->base.qualifiers |= qualifiers;
1265                         element_type->base.modifiers  |= modifiers;
1266                         element_type->base.alignment   = alignment;
1267                         copy->array.element_type       = element_type;
1268                 } else {
1269                         copy->base.qualifiers |= qualifiers;
1270                         copy->base.modifiers  |= modifiers;
1271                         copy->base.alignment   = alignment;
1272                 }
1273
1274                 type = identify_new_type(copy);
1275         }
1276
1277         return type;
1278 }
1279
1280 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1281 {
1282         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1283
1284         while (true) {
1285                 switch (type->base.kind) {
1286                 case TYPE_ERROR:
1287                         return TYPE_QUALIFIER_NONE;
1288                 case TYPE_TYPEDEF:
1289                         qualifiers |= type->base.qualifiers;
1290                         const typedef_type_t *typedef_type = &type->typedeft;
1291                         if (typedef_type->resolved_type != NULL)
1292                                 type = typedef_type->resolved_type;
1293                         else
1294                                 type = typedef_type->typedefe->type;
1295                         continue;
1296                 case TYPE_TYPEOF: {
1297                         const typeof_type_t *typeof_type = &type->typeoft;
1298                         if (typeof_type->typeof_type != NULL) {
1299                                 type = typeof_type->typeof_type;
1300                         } else {
1301                                 type = typeof_type->expression->base.type;
1302                         }
1303                         continue;
1304                 }
1305                 case TYPE_ARRAY:
1306                         if (skip_array_type) {
1307                                 type = type->array.element_type;
1308                                 continue;
1309                         }
1310                         break;
1311                 default:
1312                         break;
1313                 }
1314                 break;
1315         }
1316         return type->base.qualifiers | qualifiers;
1317 }
1318
1319 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1320 {
1321         assert(kind <= ATOMIC_TYPE_LAST);
1322         return atomic_type_properties[kind].size;
1323 }
1324
1325 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1326 {
1327         assert(kind <= ATOMIC_TYPE_LAST);
1328         return atomic_type_properties[kind].alignment;
1329 }
1330
1331 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1332 {
1333         assert(kind <= ATOMIC_TYPE_LAST);
1334         return atomic_type_properties[kind].flags;
1335 }
1336
1337 atomic_type_kind_t get_intptr_kind(void)
1338 {
1339         if (machine_size <= 32)
1340                 return ATOMIC_TYPE_INT;
1341         else if (machine_size <= 64)
1342                 return ATOMIC_TYPE_LONG;
1343         else
1344                 return ATOMIC_TYPE_LONGLONG;
1345 }
1346
1347 atomic_type_kind_t get_uintptr_kind(void)
1348 {
1349         if (machine_size <= 32)
1350                 return ATOMIC_TYPE_UINT;
1351         else if (machine_size <= 64)
1352                 return ATOMIC_TYPE_ULONG;
1353         else
1354                 return ATOMIC_TYPE_ULONGLONG;
1355 }
1356
1357 /**
1358  * Find the atomic type kind representing a given size (signed).
1359  */
1360 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1361 {
1362         static atomic_type_kind_t kinds[32];
1363
1364         assert(size < 32);
1365         atomic_type_kind_t kind = kinds[size];
1366         if (kind == ATOMIC_TYPE_INVALID) {
1367                 static const atomic_type_kind_t possible_kinds[] = {
1368                         ATOMIC_TYPE_SCHAR,
1369                         ATOMIC_TYPE_SHORT,
1370                         ATOMIC_TYPE_INT,
1371                         ATOMIC_TYPE_LONG,
1372                         ATOMIC_TYPE_LONGLONG
1373                 };
1374                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1375                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1376                                 kind = possible_kinds[i];
1377                                 break;
1378                         }
1379                 }
1380                 kinds[size] = kind;
1381         }
1382         return kind;
1383 }
1384
1385 /**
1386  * Find the atomic type kind representing a given size (signed).
1387  */
1388 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1389 {
1390         static atomic_type_kind_t kinds[32];
1391
1392         assert(size < 32);
1393         atomic_type_kind_t kind = kinds[size];
1394         if (kind == ATOMIC_TYPE_INVALID) {
1395                 static const atomic_type_kind_t possible_kinds[] = {
1396                         ATOMIC_TYPE_UCHAR,
1397                         ATOMIC_TYPE_USHORT,
1398                         ATOMIC_TYPE_UINT,
1399                         ATOMIC_TYPE_ULONG,
1400                         ATOMIC_TYPE_ULONGLONG
1401                 };
1402                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1403                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1404                                 kind = possible_kinds[i];
1405                                 break;
1406                         }
1407                 }
1408                 kinds[size] = kind;
1409         }
1410         return kind;
1411 }
1412
1413 /**
1414  * Hash the given type and return the "singleton" version
1415  * of it.
1416  */
1417 type_t *identify_new_type(type_t *type)
1418 {
1419         type_t *result = typehash_insert(type);
1420         if (result != type) {
1421                 obstack_free(type_obst, type);
1422         }
1423         return result;
1424 }
1425
1426 /**
1427  * Creates a new atomic type.
1428  *
1429  * @param akind       The kind of the atomic type.
1430  * @param qualifiers  Type qualifiers for the new type.
1431  */
1432 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1433 {
1434         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1435         memset(type, 0, sizeof(atomic_type_t));
1436
1437         type->kind            = TYPE_ATOMIC;
1438         type->base.size       = get_atomic_type_size(akind);
1439         type->base.alignment  = get_atomic_type_alignment(akind);
1440         type->base.qualifiers = qualifiers;
1441         type->atomic.akind    = akind;
1442
1443         return identify_new_type(type);
1444 }
1445
1446 /**
1447  * Creates a new complex type.
1448  *
1449  * @param akind       The kind of the atomic type.
1450  * @param qualifiers  Type qualifiers for the new type.
1451  */
1452 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1453 {
1454         type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1455         memset(type, 0, sizeof(complex_type_t));
1456
1457         type->kind            = TYPE_COMPLEX;
1458         type->base.qualifiers = qualifiers;
1459         type->base.alignment  = get_atomic_type_alignment(akind);
1460         type->complex.akind   = akind;
1461
1462         return identify_new_type(type);
1463 }
1464
1465 /**
1466  * Creates a new imaginary type.
1467  *
1468  * @param akind       The kind of the atomic type.
1469  * @param qualifiers  Type qualifiers for the new type.
1470  */
1471 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1472 {
1473         type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1474         memset(type, 0, sizeof(imaginary_type_t));
1475
1476         type->kind            = TYPE_IMAGINARY;
1477         type->base.qualifiers = qualifiers;
1478         type->base.alignment  = get_atomic_type_alignment(akind);
1479         type->imaginary.akind = akind;
1480
1481         return identify_new_type(type);
1482 }
1483
1484 /**
1485  * Creates a new pointer type.
1486  *
1487  * @param points_to   The points-to type for the new type.
1488  * @param qualifiers  Type qualifiers for the new type.
1489  */
1490 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1491 {
1492         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1493         memset(type, 0, sizeof(pointer_type_t));
1494
1495         type->kind                  = TYPE_POINTER;
1496         type->base.qualifiers       = qualifiers;
1497         type->base.alignment        = 0;
1498         type->pointer.points_to     = points_to;
1499         type->pointer.base_variable = NULL;
1500
1501         return identify_new_type(type);
1502 }
1503
1504 /**
1505  * Creates a new reference type.
1506  *
1507  * @param refers_to   The referred-to type for the new type.
1508  */
1509 type_t *make_reference_type(type_t *refers_to)
1510 {
1511         type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1512         memset(type, 0, sizeof(reference_type_t));
1513
1514         type->kind                = TYPE_REFERENCE;
1515         type->base.qualifiers     = 0;
1516         type->base.alignment      = 0;
1517         type->reference.refers_to = refers_to;
1518
1519         return identify_new_type(type);
1520 }
1521
1522 /**
1523  * Creates a new based pointer type.
1524  *
1525  * @param points_to   The points-to type for the new type.
1526  * @param qualifiers  Type qualifiers for the new type.
1527  * @param variable    The based variable
1528  */
1529 type_t *make_based_pointer_type(type_t *points_to,
1530                                                                 type_qualifiers_t qualifiers, variable_t *variable)
1531 {
1532         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1533         memset(type, 0, sizeof(pointer_type_t));
1534
1535         type->kind                  = TYPE_POINTER;
1536         type->base.qualifiers       = qualifiers;
1537         type->base.alignment        = 0;
1538         type->pointer.points_to     = points_to;
1539         type->pointer.base_variable = variable;
1540
1541         return identify_new_type(type);
1542 }
1543
1544
1545 type_t *make_array_type(type_t *element_type, size_t size,
1546                         type_qualifiers_t qualifiers)
1547 {
1548         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1549         memset(type, 0, sizeof(array_type_t));
1550
1551         type->kind                = TYPE_ARRAY;
1552         type->base.qualifiers     = qualifiers;
1553         type->base.alignment      = 0;
1554         type->array.element_type  = element_type;
1555         type->array.size          = size;
1556         type->array.size_constant = true;
1557
1558         return identify_new_type(type);
1559 }
1560
1561 /**
1562  * Debug helper. Prints the given type to stdout.
1563  */
1564 static __attribute__((unused))
1565 void dbg_type(const type_t *type)
1566 {
1567         FILE *old_out = out;
1568         out = stderr;
1569         print_type(type);
1570         puts("\n");
1571         fflush(stderr);
1572         out = old_out;
1573 }