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