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