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