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