The calling convention is orthogonal to the linkage specification.
[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         if (type->linkage != NULL) {
299                 fputs("extern \"", out);
300                 fputs(type->linkage->string, out);
301                 fputs("\" ", out);
302         }
303
304         print_type_qualifiers(type->base.qualifiers);
305         if (type->base.qualifiers != 0)
306                 fputc(' ', out);
307
308         intern_print_type_pre(type->return_type, false);
309
310         switch (type->calling_convention) {
311         case CC_CDECL:    fputs("__cdecl ",    out); break;
312         case CC_STDCALL:  fputs("__stdcall ",  out); break;
313         case CC_FASTCALL: fputs("__fastcall ", out); break;
314         case CC_THISCALL: fputs("__thiscall ", out); break;
315         case CC_DEFAULT:  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 *parameters, 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 (parameters == 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                 entity_t *parameter = parameters->entities;
350                 for( ; parameter != NULL; parameter = parameter->base.next) {
351                         if (first) {
352                                 first = false;
353                         } else {
354                                 fputs(", ", out);
355                         }
356                         assert(is_declaration(parameter));
357                         print_type_ext(parameter->declaration.type, parameter->base.symbol,
358                                        NULL);
359                 }
360         }
361         if (type->variadic) {
362                 if (first) {
363                         first = false;
364                 } else {
365                         fputs(", ", out);
366                 }
367                 fputs("...", out);
368         }
369         if (first && !type->unspecified_parameters) {
370                 fputs("void", out);
371         }
372         fputc(')', out);
373
374         intern_print_type_post(type->return_type, false);
375 }
376
377 /**
378  * Prints the prefix part of a pointer type.
379  *
380  * @param type   The pointer type.
381  */
382 static void print_pointer_type_pre(const pointer_type_t *type)
383 {
384         intern_print_type_pre(type->points_to, false);
385         fputs("*", out);
386         print_type_qualifiers(type->base.qualifiers);
387         if (type->base.qualifiers != 0)
388                 fputc(' ', out);
389 }
390
391 /**
392  * Prints the postfix part of a pointer type.
393  *
394  * @param type   The pointer type.
395  */
396 static void print_pointer_type_post(const pointer_type_t *type)
397 {
398         intern_print_type_post(type->points_to, false);
399 }
400
401 /**
402  * Prints the prefix part of an array type.
403  *
404  * @param type   The array type.
405  */
406 static void print_array_type_pre(const array_type_t *type)
407 {
408         intern_print_type_pre(type->element_type, false);
409 }
410
411 /**
412  * Prints the postfix part of an array type.
413  *
414  * @param type   The array type.
415  */
416 static void print_array_type_post(const array_type_t *type)
417 {
418         fputc('[', out);
419         if (type->is_static) {
420                 fputs("static ", out);
421         }
422         print_type_qualifiers(type->base.qualifiers);
423         if (type->base.qualifiers != 0)
424                 fputc(' ', out);
425         if (type->size_expression != NULL
426                         && (print_implicit_array_size || !type->has_implicit_size)) {
427                 print_expression(type->size_expression);
428         }
429         fputc(']', out);
430         intern_print_type_post(type->element_type, false);
431 }
432
433 /**
434  * Prints the postfix part of a bitfield type.
435  *
436  * @param type   The array type.
437  */
438 static void print_bitfield_type_post(const bitfield_type_t *type)
439 {
440         fputs(" : ", out);
441         print_expression(type->size_expression);
442         intern_print_type_post(type->base_type, false);
443 }
444
445 /**
446  * Prints an enum definition.
447  *
448  * @param declaration  The enum's type declaration.
449  */
450 void print_enum_definition(const enum_t *enume)
451 {
452         fputs("{\n", out);
453
454         change_indent(1);
455
456         entity_t *entry = enume->base.next;
457         for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
458                entry = entry->base.next) {
459
460                 print_indent();
461                 fprintf(out, "%s", entry->base.symbol->string);
462                 if (entry->enum_value.value != NULL) {
463                         fprintf(out, " = ");
464
465                         /* skip the implicit cast */
466                         expression_t *expression = entry->enum_value.value;
467                         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
468                                 expression = expression->unary.value;
469                         }
470                         print_expression(expression);
471                 }
472                 fprintf(out, ",\n");
473         }
474
475         change_indent(-1);
476         print_indent();
477         fputs("}", out);
478 }
479
480 /**
481  * Prints an enum type.
482  *
483  * @param type  The enum type.
484  */
485 static void print_type_enum(const enum_type_t *type)
486 {
487         int empty = type->base.qualifiers == 0;
488         print_type_qualifiers(type->base.qualifiers);
489         fputs(" enum " + empty, out);
490
491         enum_t   *enume  = type->enume;
492         symbol_t *symbol = enume->base.symbol;
493         if (symbol != NULL) {
494                 fputs(symbol->string, out);
495         } else {
496                 print_enum_definition(enume);
497         }
498 }
499
500 /**
501  * Print the compound part of a compound type.
502  */
503 void print_compound_definition(const compound_t *compound)
504 {
505         fputs("{\n", out);
506         change_indent(1);
507
508         entity_t *entity = compound->members.entities;
509         for( ; entity != NULL; entity = entity->base.next) {
510                 if (entity->kind != ENTITY_COMPOUND_MEMBER)
511                         continue;
512
513                 print_indent();
514                 print_entity(entity);
515                 fputc('\n', out);
516         }
517
518         change_indent(-1);
519         print_indent();
520         fputs("}", out);
521 }
522
523 /**
524  * Prints a compound type.
525  *
526  * @param type  The compound type.
527  */
528 static void print_compound_type(const compound_type_t *type)
529 {
530         int empty = type->base.qualifiers == 0;
531         print_type_qualifiers(type->base.qualifiers);
532
533         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
534                 fputs(" struct " + empty, out);
535         } else {
536                 assert(type->base.kind == TYPE_COMPOUND_UNION);
537                 fputs(" union " + empty, out);
538         }
539
540         compound_t *compound = type->compound;
541         symbol_t   *symbol   = compound->base.symbol;
542         if (symbol != NULL) {
543                 fputs(symbol->string, out);
544         } else {
545                 print_compound_definition(compound);
546         }
547 }
548
549 /**
550  * Prints the prefix part of a typedef type.
551  *
552  * @param type   The typedef type.
553  */
554 static void print_typedef_type_pre(const typedef_type_t *const type)
555 {
556         print_type_qualifiers(type->base.qualifiers);
557         if (type->base.qualifiers != 0)
558                 fputc(' ', out);
559         fputs(type->typedefe->base.symbol->string, out);
560 }
561
562 /**
563  * Prints the prefix part of a typeof type.
564  *
565  * @param type   The typeof type.
566  */
567 static void print_typeof_type_pre(const typeof_type_t *const type)
568 {
569         fputs("typeof(", out);
570         if (type->expression != NULL) {
571                 assert(type->typeof_type == NULL);
572                 print_expression(type->expression);
573         } else {
574                 print_type(type->typeof_type);
575         }
576         fputc(')', out);
577 }
578
579 /**
580  * Prints the prefix part of a type.
581  *
582  * @param type   The type.
583  * @param top    true if we print the toplevel type, false else.
584  */
585 static void intern_print_type_pre(const type_t *const type, const bool top)
586 {
587         switch(type->kind) {
588         case TYPE_ERROR:
589                 fputs("<error>", out);
590                 return;
591         case TYPE_INVALID:
592                 fputs("<invalid>", out);
593                 return;
594         case TYPE_ENUM:
595                 print_type_enum(&type->enumt);
596                 return;
597         case TYPE_ATOMIC:
598                 print_atomic_type(&type->atomic);
599                 return;
600         case TYPE_COMPLEX:
601                 print_complex_type(&type->complex);
602                 return;
603         case TYPE_IMAGINARY:
604                 print_imaginary_type(&type->imaginary);
605                 return;
606         case TYPE_COMPOUND_STRUCT:
607         case TYPE_COMPOUND_UNION:
608                 print_compound_type(&type->compound);
609                 return;
610         case TYPE_BUILTIN:
611                 fputs(type->builtin.symbol->string, out);
612                 return;
613         case TYPE_FUNCTION:
614                 print_function_type_pre(&type->function, top);
615                 return;
616         case TYPE_POINTER:
617                 print_pointer_type_pre(&type->pointer);
618                 return;
619         case TYPE_BITFIELD:
620                 intern_print_type_pre(type->bitfield.base_type, top);
621                 return;
622         case TYPE_ARRAY:
623                 print_array_type_pre(&type->array);
624                 return;
625         case TYPE_TYPEDEF:
626                 print_typedef_type_pre(&type->typedeft);
627                 return;
628         case TYPE_TYPEOF:
629                 print_typeof_type_pre(&type->typeoft);
630                 return;
631         }
632         fputs("unknown", out);
633 }
634
635 /**
636  * Prints the postfix part of a type.
637  *
638  * @param type   The type.
639  * @param top    true if we print the toplevel type, false else.
640  */
641 static void intern_print_type_post(const type_t *const type, const bool top)
642 {
643         switch(type->kind) {
644         case TYPE_FUNCTION:
645                 print_function_type_post(&type->function, NULL, top);
646                 return;
647         case TYPE_POINTER:
648                 print_pointer_type_post(&type->pointer);
649                 return;
650         case TYPE_ARRAY:
651                 print_array_type_post(&type->array);
652                 return;
653         case TYPE_BITFIELD:
654                 print_bitfield_type_post(&type->bitfield);
655                 return;
656         case TYPE_ERROR:
657         case TYPE_INVALID:
658         case TYPE_ATOMIC:
659         case TYPE_COMPLEX:
660         case TYPE_IMAGINARY:
661         case TYPE_ENUM:
662         case TYPE_COMPOUND_STRUCT:
663         case TYPE_COMPOUND_UNION:
664         case TYPE_BUILTIN:
665         case TYPE_TYPEOF:
666         case TYPE_TYPEDEF:
667                 break;
668         }
669 }
670
671 /**
672  * Prints a type.
673  *
674  * @param type   The type.
675  */
676 void print_type(const type_t *const type)
677 {
678         print_type_ext(type, NULL, NULL);
679 }
680
681 void print_type_ext(const type_t *const type, const symbol_t *symbol,
682                     const scope_t *parameters)
683 {
684         if (type == NULL) {
685                 fputs("nil type", out);
686                 return;
687         }
688
689         intern_print_type_pre(type, true);
690         if (symbol != NULL) {
691                 fputc(' ', out);
692                 fputs(symbol->string, out);
693         }
694         if (type->kind == TYPE_FUNCTION) {
695                 print_function_type_post(&type->function, parameters, true);
696         } else {
697                 intern_print_type_post(type, true);
698         }
699 }
700
701 /**
702  * Return the size of a type AST node.
703  *
704  * @param type  The type.
705  */
706 static size_t get_type_size(const type_t *type)
707 {
708         switch(type->kind) {
709         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
710         case TYPE_COMPLEX:         return sizeof(complex_type_t);
711         case TYPE_IMAGINARY:       return sizeof(imaginary_type_t);
712         case TYPE_COMPOUND_STRUCT:
713         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
714         case TYPE_ENUM:            return sizeof(enum_type_t);
715         case TYPE_FUNCTION:        return sizeof(function_type_t);
716         case TYPE_POINTER:         return sizeof(pointer_type_t);
717         case TYPE_ARRAY:           return sizeof(array_type_t);
718         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
719         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
720         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
721         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
722         case TYPE_ERROR:           panic("error type found");
723         case TYPE_INVALID:         panic("invalid type found");
724         }
725         panic("unknown type found");
726 }
727
728 /**
729  * Duplicates a type.
730  *
731  * @param type  The type to copy.
732  * @return A copy of the type.
733  *
734  * @note This does not produce a deep copy!
735  */
736 type_t *duplicate_type(const type_t *type)
737 {
738         size_t size = get_type_size(type);
739
740         type_t *copy = obstack_alloc(type_obst, size);
741         memcpy(copy, type, size);
742
743         return copy;
744 }
745
746 /**
747  * Returns the unqualified type of a given type.
748  *
749  * @param type  The type.
750  * @returns The unqualified type.
751  */
752 type_t *get_unqualified_type(type_t *type)
753 {
754         assert(!is_typeref(type));
755
756         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
757                 return type;
758
759         type_t *unqualified_type          = duplicate_type(type);
760         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
761
762         type_t *result = typehash_insert(unqualified_type);
763         if (result != unqualified_type) {
764                 obstack_free(type_obst, unqualified_type);
765         }
766
767         return result;
768 }
769
770 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
771 {
772         type_t *type = skip_typeref(orig_type);
773
774         type_t *copy;
775         if (is_type_array(type)) {
776                 /* For array types the element type has to be adjusted */
777                 type_t *element_type      = type->array.element_type;
778                 type_t *qual_element_type = get_qualified_type(element_type, qual);
779
780                 if (qual_element_type == element_type)
781                         return orig_type;
782
783                 copy                     = duplicate_type(type);
784                 copy->array.element_type = qual_element_type;
785         } else if (is_type_valid(type)) {
786                 if ((type->base.qualifiers & qual) == qual)
787                         return orig_type;
788
789                 copy                   = duplicate_type(type);
790                 copy->base.qualifiers |= qual;
791         } else {
792                 return type;
793         }
794
795         type = typehash_insert(copy);
796         if (type != copy)
797                 obstack_free(type_obst, copy);
798
799         return type;
800 }
801
802 /**
803  * Check if a type is valid.
804  *
805  * @param type  The type to check.
806  * @return true if type represents a valid type.
807  */
808 bool type_valid(const type_t *type)
809 {
810         return type->kind != TYPE_INVALID;
811 }
812
813 static bool test_atomic_type_flag(atomic_type_kind_t kind,
814                                   atomic_type_flag_t flag)
815 {
816         assert(kind <= ATOMIC_TYPE_LAST);
817         return (atomic_type_properties[kind].flags & flag) != 0;
818 }
819
820 /**
821  * Returns true if the given type is an integer type.
822  *
823  * @param type  The type to check.
824  * @return True if type is an integer type.
825  */
826 bool is_type_integer(const type_t *type)
827 {
828         assert(!is_typeref(type));
829
830         if (type->kind == TYPE_ENUM)
831                 return true;
832         if (type->kind == TYPE_BITFIELD)
833                 return true;
834
835         if (type->kind != TYPE_ATOMIC)
836                 return false;
837
838         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
839 }
840
841 /**
842  * Returns true if the given type is an enum type.
843  *
844  * @param type  The type to check.
845  * @return True if type is an enum type.
846  */
847 bool is_type_enum(const type_t *type)
848 {
849         assert(!is_typeref(type));
850         return type->kind == TYPE_ENUM;
851 }
852
853 /**
854  * Returns true if the given type is an floating point type.
855  *
856  * @param type  The type to check.
857  * @return True if type is a floating point type.
858  */
859 bool is_type_float(const type_t *type)
860 {
861         assert(!is_typeref(type));
862
863         if (type->kind != TYPE_ATOMIC)
864                 return false;
865
866         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
867 }
868
869 /**
870  * Returns true if the given type is an complex type.
871  *
872  * @param type  The type to check.
873  * @return True if type is a complex type.
874  */
875 bool is_type_complex(const type_t *type)
876 {
877         assert(!is_typeref(type));
878
879         if (type->kind != TYPE_ATOMIC)
880                 return false;
881
882         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
883 }
884
885 /**
886  * Returns true if the given type is a signed type.
887  *
888  * @param type  The type to check.
889  * @return True if type is a signed type.
890  */
891 bool is_type_signed(const type_t *type)
892 {
893         assert(!is_typeref(type));
894
895         /* enum types are int for now */
896         if (type->kind == TYPE_ENUM)
897                 return true;
898         if (type->kind == TYPE_BITFIELD)
899                 return is_type_signed(type->bitfield.base_type);
900
901         if (type->kind != TYPE_ATOMIC)
902                 return false;
903
904         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
905 }
906
907 /**
908  * Returns true if the given type represents an arithmetic type.
909  *
910  * @param type  The type to check.
911  * @return True if type represents an arithmetic type.
912  */
913 bool is_type_arithmetic(const type_t *type)
914 {
915         assert(!is_typeref(type));
916
917         switch(type->kind) {
918         case TYPE_BITFIELD:
919         case TYPE_ENUM:
920                 return true;
921         case TYPE_ATOMIC:
922                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
923         case TYPE_COMPLEX:
924                 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
925         case TYPE_IMAGINARY:
926                 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
927         default:
928                 return false;
929         }
930 }
931
932 /**
933  * Returns true if the given type is an integer or float type.
934  *
935  * @param type  The type to check.
936  * @return True if type is an integer or float type.
937  */
938 bool is_type_real(const type_t *type)
939 {
940         /* 6.2.5 (17) */
941         return is_type_integer(type) || is_type_float(type);
942 }
943
944 /**
945  * Returns true if the given type represents a scalar type.
946  *
947  * @param type  The type to check.
948  * @return True if type represents a scalar type.
949  */
950 bool is_type_scalar(const type_t *type)
951 {
952         assert(!is_typeref(type));
953
954         switch (type->kind) {
955                 case TYPE_POINTER: return true;
956                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
957                 default:           break;
958         }
959
960         return is_type_arithmetic(type);
961 }
962
963 /**
964  * Check if a given type is incomplete.
965  *
966  * @param type  The type to check.
967  * @return True if the given type is incomplete (ie. just forward).
968  */
969 bool is_type_incomplete(const type_t *type)
970 {
971         assert(!is_typeref(type));
972
973         switch(type->kind) {
974         case TYPE_COMPOUND_STRUCT:
975         case TYPE_COMPOUND_UNION: {
976                 const compound_type_t *compound_type = &type->compound;
977                 return !compound_type->compound->complete;
978         }
979         case TYPE_ENUM:
980                 return false;
981
982         case TYPE_ARRAY:
983                 return type->array.size_expression == NULL
984                         && !type->array.size_constant;
985
986         case TYPE_ATOMIC:
987                 return type->atomic.akind == ATOMIC_TYPE_VOID;
988
989         case TYPE_COMPLEX:
990                 return type->complex.akind == ATOMIC_TYPE_VOID;
991
992         case TYPE_IMAGINARY:
993                 return type->imaginary.akind == ATOMIC_TYPE_VOID;
994
995         case TYPE_BITFIELD:
996         case TYPE_FUNCTION:
997         case TYPE_POINTER:
998         case TYPE_BUILTIN:
999         case TYPE_ERROR:
1000                 return false;
1001
1002         case TYPE_TYPEDEF:
1003         case TYPE_TYPEOF:
1004                 panic("is_type_incomplete called without typerefs skipped");
1005         case TYPE_INVALID:
1006                 break;
1007         }
1008
1009         panic("invalid type found");
1010 }
1011
1012 bool is_type_object(const type_t *type)
1013 {
1014         return !is_type_function(type) && !is_type_incomplete(type);
1015 }
1016
1017 /**
1018  * Check if two function types are compatible.
1019  */
1020 static bool function_types_compatible(const function_type_t *func1,
1021                                       const function_type_t *func2)
1022 {
1023         const type_t* const ret1 = skip_typeref(func1->return_type);
1024         const type_t* const ret2 = skip_typeref(func2->return_type);
1025         if (!types_compatible(ret1, ret2))
1026                 return false;
1027
1028         if (func1->linkage != func2->linkage)
1029                 return false;
1030
1031         if (func1->calling_convention != func2->calling_convention)
1032                 return false;
1033
1034         /* can parameters be compared? */
1035         if (func1->unspecified_parameters || func2->unspecified_parameters)
1036                 return true;
1037
1038         if (func1->variadic != func2->variadic)
1039                 return false;
1040
1041         /* TODO: handling of unspecified parameters not correct yet */
1042
1043         /* all argument types must be compatible */
1044         function_parameter_t *parameter1 = func1->parameters;
1045         function_parameter_t *parameter2 = func2->parameters;
1046         for ( ; parameter1 != NULL && parameter2 != NULL;
1047                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
1048                 type_t *parameter1_type = skip_typeref(parameter1->type);
1049                 type_t *parameter2_type = skip_typeref(parameter2->type);
1050
1051                 parameter1_type = get_unqualified_type(parameter1_type);
1052                 parameter2_type = get_unqualified_type(parameter2_type);
1053
1054                 if (!types_compatible(parameter1_type, parameter2_type))
1055                         return false;
1056         }
1057         /* same number of arguments? */
1058         if (parameter1 != NULL || parameter2 != NULL)
1059                 return false;
1060
1061         return true;
1062 }
1063
1064 /**
1065  * Check if two array types are compatible.
1066  */
1067 static bool array_types_compatible(const array_type_t *array1,
1068                                    const array_type_t *array2)
1069 {
1070         type_t *element_type1 = skip_typeref(array1->element_type);
1071         type_t *element_type2 = skip_typeref(array2->element_type);
1072         if (!types_compatible(element_type1, element_type2))
1073                 return false;
1074
1075         if (!array1->size_constant || !array2->size_constant)
1076                 return true;
1077
1078         return array1->size == array2->size;
1079 }
1080
1081 /**
1082  * Check if two types are compatible.
1083  */
1084 bool types_compatible(const type_t *type1, const type_t *type2)
1085 {
1086         assert(!is_typeref(type1));
1087         assert(!is_typeref(type2));
1088
1089         /* shortcut: the same type is always compatible */
1090         if (type1 == type2)
1091                 return true;
1092
1093         if (!is_type_valid(type1) || !is_type_valid(type2))
1094                 return true;
1095
1096         if (type1->base.qualifiers != type2->base.qualifiers)
1097                 return false;
1098         if (type1->kind != type2->kind)
1099                 return false;
1100
1101         switch (type1->kind) {
1102         case TYPE_FUNCTION:
1103                 return function_types_compatible(&type1->function, &type2->function);
1104         case TYPE_ATOMIC:
1105                 return type1->atomic.akind == type2->atomic.akind;
1106         case TYPE_COMPLEX:
1107                 return type1->complex.akind == type2->complex.akind;
1108         case TYPE_IMAGINARY:
1109                 return type1->imaginary.akind == type2->imaginary.akind;
1110         case TYPE_ARRAY:
1111                 return array_types_compatible(&type1->array, &type2->array);
1112
1113         case TYPE_POINTER: {
1114                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1115                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1116                 return types_compatible(to1, to2);
1117         }
1118
1119         case TYPE_COMPOUND_STRUCT:
1120         case TYPE_COMPOUND_UNION:
1121         case TYPE_ENUM:
1122         case TYPE_BUILTIN:
1123                 /* TODO: not implemented */
1124                 break;
1125
1126         case TYPE_BITFIELD:
1127                 /* not sure if this makes sense or is even needed, implement it if you
1128                  * really need it! */
1129                 panic("type compatibility check for bitfield type");
1130
1131         case TYPE_ERROR:
1132                 /* Hmm, the error type should be compatible to all other types */
1133                 return true;
1134         case TYPE_INVALID:
1135                 panic("invalid type found in compatible types");
1136         case TYPE_TYPEDEF:
1137         case TYPE_TYPEOF:
1138                 panic("typerefs not skipped in compatible types?!?");
1139         }
1140
1141         /* TODO: incomplete */
1142         return false;
1143 }
1144
1145 /**
1146  * Skip all typerefs and return the underlying type.
1147  */
1148 type_t *skip_typeref(type_t *type)
1149 {
1150         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1151         type_modifiers_t  modifiers  = TYPE_MODIFIER_NONE;
1152
1153         while (true) {
1154                 switch (type->kind) {
1155                 case TYPE_ERROR:
1156                         return type;
1157                 case TYPE_TYPEDEF: {
1158                         qualifiers |= type->base.qualifiers;
1159                         modifiers  |= type->base.modifiers;
1160                         const typedef_type_t *typedef_type = &type->typedeft;
1161                         if (typedef_type->resolved_type != NULL) {
1162                                 type = typedef_type->resolved_type;
1163                                 break;
1164                         }
1165                         type = typedef_type->typedefe->type;
1166                         continue;
1167                 }
1168                 case TYPE_TYPEOF: {
1169                         const typeof_type_t *typeof_type = &type->typeoft;
1170                         if (typeof_type->typeof_type != NULL) {
1171                                 type = typeof_type->typeof_type;
1172                         } else {
1173                                 type = typeof_type->expression->base.type;
1174                         }
1175                         continue;
1176                 }
1177                 default:
1178                         break;
1179                 }
1180                 break;
1181         }
1182
1183         if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1184                 type_t *const copy = duplicate_type(type);
1185
1186                 /* for const with typedefed array type the element type has to be
1187                  * adjusted */
1188                 if (is_type_array(copy)) {
1189                         type_t *element_type           = copy->array.element_type;
1190                         element_type                   = duplicate_type(element_type);
1191                         element_type->base.qualifiers |= qualifiers;
1192                         element_type->base.modifiers  |= modifiers;
1193                         copy->array.element_type       = element_type;
1194                 } else {
1195                         copy->base.qualifiers |= qualifiers;
1196                         copy->base.modifiers  |= modifiers;
1197                 }
1198
1199                 type = typehash_insert(copy);
1200                 if (type != copy) {
1201                         obstack_free(type_obst, copy);
1202                 }
1203         }
1204
1205         return type;
1206 }
1207
1208 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type) {
1209         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1210
1211         while (true) {
1212                 switch (type->base.kind) {
1213                 case TYPE_ERROR:
1214                         return TYPE_QUALIFIER_NONE;
1215                 case TYPE_TYPEDEF:
1216                         qualifiers |= type->base.qualifiers;
1217                         const typedef_type_t *typedef_type = &type->typedeft;
1218                         if (typedef_type->resolved_type != NULL)
1219                                 type = typedef_type->resolved_type;
1220                         else
1221                                 type = typedef_type->typedefe->type;
1222                         continue;
1223                 case TYPE_TYPEOF: {
1224                         const typeof_type_t *typeof_type = &type->typeoft;
1225                         if (typeof_type->typeof_type != NULL) {
1226                                 type = typeof_type->typeof_type;
1227                         } else {
1228                                 type = typeof_type->expression->base.type;
1229                         }
1230                         continue;
1231                 }
1232                 case TYPE_ARRAY:
1233                         if (skip_array_type) {
1234                                 type = type->array.element_type;
1235                                 continue;
1236                         }
1237                         break;
1238                 default:
1239                         break;
1240                 }
1241                 break;
1242         }
1243         return type->base.qualifiers | qualifiers;
1244 }
1245
1246 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1247 {
1248         assert(kind <= ATOMIC_TYPE_LAST);
1249         return atomic_type_properties[kind].size;
1250 }
1251
1252 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1253 {
1254         assert(kind <= ATOMIC_TYPE_LAST);
1255         return atomic_type_properties[kind].alignment;
1256 }
1257
1258 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1259 {
1260         assert(kind <= ATOMIC_TYPE_LAST);
1261         return atomic_type_properties[kind].flags;
1262 }
1263
1264 atomic_type_kind_t get_intptr_kind(void)
1265 {
1266         if (machine_size <= 32)
1267                 return ATOMIC_TYPE_INT;
1268         else if (machine_size <= 64)
1269                 return ATOMIC_TYPE_LONG;
1270         else
1271                 return ATOMIC_TYPE_LONGLONG;
1272 }
1273
1274 atomic_type_kind_t get_uintptr_kind(void)
1275 {
1276         if (machine_size <= 32)
1277                 return ATOMIC_TYPE_UINT;
1278         else if (machine_size <= 64)
1279                 return ATOMIC_TYPE_ULONG;
1280         else
1281                 return ATOMIC_TYPE_ULONGLONG;
1282 }
1283
1284 /**
1285  * Find the atomic type kind representing a given size (signed).
1286  */
1287 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1288         static atomic_type_kind_t kinds[32];
1289
1290         assert(size < 32);
1291         atomic_type_kind_t kind = kinds[size];
1292         if (kind == ATOMIC_TYPE_INVALID) {
1293                 static const atomic_type_kind_t possible_kinds[] = {
1294                         ATOMIC_TYPE_SCHAR,
1295                         ATOMIC_TYPE_SHORT,
1296                         ATOMIC_TYPE_INT,
1297                         ATOMIC_TYPE_LONG,
1298                         ATOMIC_TYPE_LONGLONG
1299                 };
1300                 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1301                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1302                                 kind = possible_kinds[i];
1303                                 break;
1304                         }
1305                 }
1306                 kinds[size] = kind;
1307         }
1308         return kind;
1309 }
1310
1311 /**
1312  * Find the atomic type kind representing a given size (signed).
1313  */
1314 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1315         static atomic_type_kind_t kinds[32];
1316
1317         assert(size < 32);
1318         atomic_type_kind_t kind = kinds[size];
1319         if (kind == ATOMIC_TYPE_INVALID) {
1320                 static const atomic_type_kind_t possible_kinds[] = {
1321                         ATOMIC_TYPE_UCHAR,
1322                         ATOMIC_TYPE_USHORT,
1323                         ATOMIC_TYPE_UINT,
1324                         ATOMIC_TYPE_ULONG,
1325                         ATOMIC_TYPE_ULONGLONG
1326                 };
1327                 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1328                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1329                                 kind = possible_kinds[i];
1330                                 break;
1331                         }
1332                 }
1333                 kinds[size] = kind;
1334         }
1335         return kind;
1336 }
1337
1338 /**
1339  * Hash the given type and return the "singleton" version
1340  * of it.
1341  */
1342 static type_t *identify_new_type(type_t *type)
1343 {
1344         type_t *result = typehash_insert(type);
1345         if (result != type) {
1346                 obstack_free(type_obst, type);
1347         }
1348         return result;
1349 }
1350
1351 /**
1352  * Creates a new atomic type.
1353  *
1354  * @param akind       The kind of the atomic type.
1355  * @param qualifiers  Type qualifiers for the new type.
1356  */
1357 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1358 {
1359         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1360         memset(type, 0, sizeof(atomic_type_t));
1361
1362         type->kind            = TYPE_ATOMIC;
1363         type->base.size       = get_atomic_type_size(akind);
1364         type->base.alignment  = get_atomic_type_alignment(akind);
1365         type->base.qualifiers = qualifiers;
1366         type->atomic.akind    = akind;
1367
1368         return identify_new_type(type);
1369 }
1370
1371 /**
1372  * Creates a new complex type.
1373  *
1374  * @param akind       The kind of the atomic type.
1375  * @param qualifiers  Type qualifiers for the new type.
1376  */
1377 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1378 {
1379         type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1380         memset(type, 0, sizeof(complex_type_t));
1381
1382         type->kind            = TYPE_COMPLEX;
1383         type->base.qualifiers = qualifiers;
1384         type->base.alignment  = get_atomic_type_alignment(akind);
1385         type->complex.akind   = akind;
1386
1387         return identify_new_type(type);
1388 }
1389
1390 /**
1391  * Creates a new imaginary type.
1392  *
1393  * @param akind       The kind of the atomic type.
1394  * @param qualifiers  Type qualifiers for the new type.
1395  */
1396 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1397 {
1398         type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1399         memset(type, 0, sizeof(imaginary_type_t));
1400
1401         type->kind            = TYPE_IMAGINARY;
1402         type->base.qualifiers = qualifiers;
1403         type->base.alignment  = get_atomic_type_alignment(akind);
1404         type->imaginary.akind = akind;
1405
1406         return identify_new_type(type);
1407 }
1408
1409 /**
1410  * Creates a new pointer type.
1411  *
1412  * @param points_to   The points-to type for the new type.
1413  * @param qualifiers  Type qualifiers for the new type.
1414  */
1415 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1416 {
1417         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1418         memset(type, 0, sizeof(pointer_type_t));
1419
1420         type->kind              = TYPE_POINTER;
1421         type->base.qualifiers   = qualifiers;
1422         type->base.alignment    = 0;
1423         type->pointer.points_to = points_to;
1424
1425         return identify_new_type(type);
1426 }
1427
1428 type_t *make_array_type(type_t *element_type, size_t size,
1429                         type_qualifiers_t qualifiers)
1430 {
1431         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1432         memset(type, 0, sizeof(array_type_t));
1433
1434         type->kind                = TYPE_ARRAY;
1435         type->base.qualifiers     = qualifiers;
1436         type->base.alignment      = 0;
1437         type->array.element_type  = element_type;
1438         type->array.size          = size;
1439         type->array.size_constant = true;
1440
1441         return identify_new_type(type);
1442 }
1443
1444 /**
1445  * Debug helper. Prints the given type to stdout.
1446  */
1447 static __attribute__((unused))
1448 void dbg_type(const type_t *type)
1449 {
1450         FILE *old_out = out;
1451         out = stderr;
1452         print_type(type);
1453         puts("\n");
1454         fflush(stderr);
1455         out = old_out;
1456 }