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