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