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