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