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