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