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