some refactoring in preparation for a preprocessor
[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
30 static struct obstack   _type_obst;
31 static FILE            *out;
32 struct obstack         *type_obst                 = &_type_obst;
33 static int              type_visited              = 0;
34 static bool             print_implicit_array_size = true;
35
36 static void intern_print_type_pre(const type_t *type, bool top);
37 static void intern_print_type_post(const type_t *type, bool top);
38
39 void init_types(void)
40 {
41         obstack_init(type_obst);
42 }
43
44 void exit_types(void)
45 {
46         obstack_free(type_obst, NULL);
47 }
48
49 void type_set_output(FILE *stream)
50 {
51         out = stream;
52 }
53
54 void inc_type_visited(void)
55 {
56         type_visited++;
57 }
58
59 void print_type_qualifiers(type_qualifiers_t qualifiers)
60 {
61         if(qualifiers & TYPE_QUALIFIER_CONST)    fputs("const ",    out);
62         if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
63         if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
64 }
65
66 /**
67  * Prints the name of a atomic type.
68  *
69  * @param type  The type.
70  */
71 static
72 void print_atomic_type(const atomic_type_t *type)
73 {
74         print_type_qualifiers(type->type.qualifiers);
75
76         const char *s;
77         switch(type->akind) {
78         case ATOMIC_TYPE_INVALID:     s = "INVALIDATOMIC";      break;
79         case ATOMIC_TYPE_VOID:        s = "void";               break;
80         case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
81         case ATOMIC_TYPE_CHAR:        s = "char";               break;
82         case ATOMIC_TYPE_SCHAR:       s = "signed char";        break;
83         case ATOMIC_TYPE_UCHAR:       s = "unsigned char";      break;
84         case ATOMIC_TYPE_INT:         s = "int";                break;
85         case ATOMIC_TYPE_UINT:        s = "unsigned int";       break;
86         case ATOMIC_TYPE_SHORT:       s = "short";              break;
87         case ATOMIC_TYPE_USHORT:      s = "unsigned short";     break;
88         case ATOMIC_TYPE_LONG:        s = "long";               break;
89         case ATOMIC_TYPE_ULONG:       s = "unsigned long";      break;
90         case ATOMIC_TYPE_LONGLONG:    s = "long long";          break;
91         case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long"; break;
92         case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";        break;
93         case ATOMIC_TYPE_FLOAT:       s = "float";              break;
94         case ATOMIC_TYPE_DOUBLE:      s = "double";             break;
95         default:                      s = "UNKNOWNATOMIC";      break;
96         }
97         fputs(s, out);
98 }
99
100 /**
101  * Print the first part (the prefix) of a type.
102  *
103  * @param type   The type to print.
104  * @param top    true, if this is the top type, false if it's an embedded type.
105  */
106 static void print_function_type_pre(const function_type_t *type, bool top)
107 {
108         print_type_qualifiers(type->type.qualifiers);
109
110         intern_print_type_pre(type->return_type, false);
111
112         /* don't emit braces if we're the toplevel type... */
113         if(!top)
114                 fputc('(', out);
115 }
116
117 /**
118  * Print the second part (the postfix) of a type.
119  *
120  * @param type   The type to print.
121  * @param top    true, if this is the top type, false if it's an embedded type.
122  */
123 static void print_function_type_post(const function_type_t *type,
124                                      const scope_t *scope, bool top)
125 {
126         intern_print_type_post(type->return_type, false);
127         /* don't emit braces if we're the toplevel type... */
128         if(!top)
129                 fputc(')', out);
130
131         fputc('(', out);
132
133         int first = 1;
134         if(scope == NULL) {
135                 function_parameter_t *parameter = type->parameters;
136                 for( ; parameter != NULL; parameter = parameter->next) {
137                         if(first) {
138                                 first = 0;
139                         } else {
140                                 fputs(", ", out);
141                         }
142                         print_type(parameter->type);
143                 }
144         } else {
145                 declaration_t *parameter = scope->declarations;
146                 for( ; parameter != NULL; parameter = parameter->next) {
147                         if(first) {
148                                 first = 0;
149                         } else {
150                                 fputs(", ", out);
151                         }
152                         print_type_ext(parameter->type, parameter->symbol,
153                                        &parameter->scope);
154                 }
155         }
156         if(type->variadic) {
157                 if(first) {
158                         first = 0;
159                 } else {
160                         fputs(", ", out);
161                 }
162                 fputs("...", out);
163         }
164         if(first && !type->unspecified_parameters) {
165                 fputs("void", out);
166         }
167         fputc(')', out);
168 }
169
170 /**
171  * Prints the prefix part of a pointer type.
172  *
173  * @param type   The pointer type.
174  */
175 static void print_pointer_type_pre(const pointer_type_t *type)
176 {
177         intern_print_type_pre(type->points_to, false);
178         fputs("*", out);
179         print_type_qualifiers(type->type.qualifiers);
180 }
181
182 /**
183  * Prints the postfix part of a pointer type.
184  *
185  * @param type   The pointer type.
186  */
187 static void print_pointer_type_post(const pointer_type_t *type)
188 {
189         intern_print_type_post(type->points_to, false);
190 }
191
192 /**
193  * Prints the prefix part of an array type.
194  *
195  * @param type   The array type.
196  */
197 static void print_array_type_pre(const array_type_t *type)
198 {
199         intern_print_type_pre(type->element_type, false);
200 }
201
202 /**
203  * Prints the postfix part of an array type.
204  *
205  * @param type   The array type.
206  */
207 static void print_array_type_post(const array_type_t *type)
208 {
209         fputc('[', out);
210         if(type->is_static) {
211                 fputs("static ", out);
212         }
213         print_type_qualifiers(type->type.qualifiers);
214         if(type->size_expression != NULL
215                         && (print_implicit_array_size || !type->has_implicit_size)) {
216                 print_expression(type->size_expression);
217         }
218         fputc(']', out);
219         intern_print_type_post(type->element_type, false);
220 }
221
222 /**
223  * Prints the postfix part of a bitfield type.
224  *
225  * @param type   The array type.
226  */
227 static void print_bitfield_type_post(const bitfield_type_t *type)
228 {
229         fputs(" : ", out);
230         print_expression(type->size);
231         intern_print_type_post(type->base, false);
232 }
233
234 /**
235  * Prints an enum definition.
236  *
237  * @param declaration  The enum's type declaration.
238  */
239 void print_enum_definition(const declaration_t *declaration)
240 {
241         fputs("{\n", out);
242
243         change_indent(1);
244
245         declaration_t *entry = declaration->next;
246         for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
247                entry = entry->next) {
248
249                 print_indent();
250                 fprintf(out, "%s", entry->symbol->string);
251                 if(entry->init.initializer != NULL) {
252                         fprintf(out, " = ");
253                         print_expression(entry->init.enum_value);
254                 }
255                 fprintf(out, ",\n");
256         }
257
258         change_indent(-1);
259         print_indent();
260         fputs("}", out);
261 }
262
263 /**
264  * Prints an enum type.
265  *
266  * @param type  The enum type.
267  */
268 static void print_type_enum(const enum_type_t *type)
269 {
270         print_type_qualifiers(type->type.qualifiers);
271         fputs("enum ", out);
272
273         declaration_t *declaration = type->declaration;
274         symbol_t      *symbol      = declaration->symbol;
275         if(symbol != NULL) {
276                 fputs(symbol->string, out);
277         } else {
278                 print_enum_definition(declaration);
279         }
280 }
281
282 /**
283  * Print the compound part of a compound type.
284  *
285  * @param declaration  The declaration of the compound type.
286  */
287 void print_compound_definition(const declaration_t *declaration)
288 {
289         fputs("{\n", out);
290         change_indent(1);
291
292         declaration_t *iter = declaration->scope.declarations;
293         for( ; iter != NULL; iter = iter->next) {
294                 print_indent();
295                 print_declaration(iter);
296                 fputc('\n', out);
297         }
298
299         change_indent(-1);
300         print_indent();
301         fputs("}", out);
302 }
303
304 /**
305  * Prints a compound type.
306  *
307  * @param type  The compound type.
308  */
309 static void print_compound_type(const compound_type_t *type)
310 {
311         print_type_qualifiers(type->type.qualifiers);
312
313         if(type->type.kind == TYPE_COMPOUND_STRUCT) {
314                 fputs("struct ", out);
315         } else {
316                 assert(type->type.kind == TYPE_COMPOUND_UNION);
317                 fputs("union ", out);
318         }
319
320         declaration_t *declaration = type->declaration;
321         symbol_t      *symbol      = declaration->symbol;
322         if(symbol != NULL) {
323                 fputs(symbol->string, out);
324         } else {
325                 print_compound_definition(declaration);
326         }
327 }
328
329 /**
330  * Prints the prefix part of a typedef type.
331  *
332  * @param type   The typedef type.
333  */
334 static void print_typedef_type_pre(const typedef_type_t *const type)
335 {
336         print_type_qualifiers(type->type.qualifiers);
337         fputs(type->declaration->symbol->string, out);
338 }
339
340 /**
341  * Prints the prefix part of a typeof type.
342  *
343  * @param type   The typeof type.
344  */
345 static void print_typeof_type_pre(const typeof_type_t *const type)
346 {
347         fputs("typeof(", out);
348         if(type->expression != NULL) {
349                 assert(type->typeof_type == NULL);
350                 print_expression(type->expression);
351         } else {
352                 print_type(type->typeof_type);
353         }
354         fputc(')', out);
355 }
356
357 /**
358  * Prints the prefix part of a type.
359  *
360  * @param type   The type.
361  * @param top    true if we print the toplevel type, false else.
362  */
363 static void intern_print_type_pre(const type_t *const type, const bool top)
364 {
365         switch(type->kind) {
366         case TYPE_ERROR:
367                 fputs("<error>", out);
368         case TYPE_INVALID:
369                 fputs("<invalid>", out);
370                 return;
371         case TYPE_ENUM:
372                 print_type_enum(&type->enumt);
373                 return;
374         case TYPE_ATOMIC:
375                 print_atomic_type(&type->atomic);
376                 return;
377         case TYPE_COMPOUND_STRUCT:
378         case TYPE_COMPOUND_UNION:
379                 print_compound_type(&type->compound);
380                 return;
381         case TYPE_BUILTIN:
382                 fputs(type->builtin.symbol->string, out);
383                 return;
384         case TYPE_FUNCTION:
385                 print_function_type_pre(&type->function, top);
386                 return;
387         case TYPE_POINTER:
388                 print_pointer_type_pre(&type->pointer);
389                 return;
390         case TYPE_BITFIELD:
391                 intern_print_type_pre(type->bitfield.base, top);
392                 return;
393         case TYPE_ARRAY:
394                 print_array_type_pre(&type->array);
395                 return;
396         case TYPE_TYPEDEF:
397                 print_typedef_type_pre(&type->typedeft);
398                 return;
399         case TYPE_TYPEOF:
400                 print_typeof_type_pre(&type->typeoft);
401                 return;
402         }
403         fputs("unknown", out);
404 }
405
406 /**
407  * Prints the postfix part of a type.
408  *
409  * @param type   The type.
410  * @param top    true if we print the toplevel type, false else.
411  */
412 static void intern_print_type_post(const type_t *const type, const bool top)
413 {
414         switch(type->kind) {
415         case TYPE_FUNCTION:
416                 print_function_type_post(&type->function, NULL, top);
417                 return;
418         case TYPE_POINTER:
419                 print_pointer_type_post(&type->pointer);
420                 return;
421         case TYPE_ARRAY:
422                 print_array_type_post(&type->array);
423                 return;
424         case TYPE_BITFIELD:
425                 print_bitfield_type_post(&type->bitfield);
426                 return;
427         case TYPE_ERROR:
428         case TYPE_INVALID:
429         case TYPE_ATOMIC:
430         case TYPE_ENUM:
431         case TYPE_COMPOUND_STRUCT:
432         case TYPE_COMPOUND_UNION:
433         case TYPE_BUILTIN:
434         case TYPE_TYPEOF:
435         case TYPE_TYPEDEF:
436                 break;
437         }
438 }
439
440 /**
441  * Prints a type.
442  *
443  * @param type   The type.
444  */
445 void print_type(const type_t *const type)
446 {
447         print_type_ext(type, NULL, NULL);
448 }
449
450 void print_type_ext(const type_t *const type, const symbol_t *symbol,
451                     const scope_t *scope)
452 {
453         if(type == NULL) {
454                 fputs("nil type", out);
455                 return;
456         }
457
458         intern_print_type_pre(type, true);
459         if(symbol != NULL) {
460                 fputc(' ', out);
461                 fputs(symbol->string, out);
462         }
463         if(type->kind == TYPE_FUNCTION) {
464                 print_function_type_post(&type->function, scope, true);
465         } else {
466                 intern_print_type_post(type, true);
467         }
468 }
469
470 /**
471  * Return the size of a type AST node.
472  *
473  * @param type  The type.
474  */
475 static size_t get_type_size(const type_t *type)
476 {
477         switch(type->kind) {
478         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
479         case TYPE_COMPOUND_STRUCT:
480         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
481         case TYPE_ENUM:            return sizeof(enum_type_t);
482         case TYPE_FUNCTION:        return sizeof(function_type_t);
483         case TYPE_POINTER:         return sizeof(pointer_type_t);
484         case TYPE_ARRAY:           return sizeof(array_type_t);
485         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
486         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
487         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
488         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
489         case TYPE_ERROR:           panic("error type found");
490         case TYPE_INVALID:         panic("invalid type found");
491         }
492         panic("unknown type found");
493 }
494
495 /**
496  * Duplicates a type.
497  *
498  * @param type  The type to copy.
499  * @return A copy of the type.
500  *
501  * @note This does not produce a deep copy!
502  */
503 type_t *duplicate_type(const type_t *type)
504 {
505         size_t size = get_type_size(type);
506
507         type_t *copy = obstack_alloc(type_obst, size);
508         memcpy(copy, type, size);
509
510         return copy;
511 }
512
513 /**
514  * Returns the unqualified type of a given type.
515  *
516  * @param type  The type.
517  * @returns The unqualified type.
518  */
519 type_t *get_unqualified_type(type_t *type)
520 {
521         if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
522                 return type;
523
524         type_t *unqualified_type          = duplicate_type(type);
525         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
526
527         type_t *result = typehash_insert(unqualified_type);
528         if(result != unqualified_type) {
529                 obstack_free(type_obst, unqualified_type);
530         }
531
532         return result;
533 }
534
535 /**
536  * Check if a type is valid.
537  *
538  * @param type  The type to check.
539  * @return true if type represents a valid type.
540  */
541 bool type_valid(const type_t *type)
542 {
543         return type->kind != TYPE_INVALID;
544 }
545
546 /**
547  * Returns true if the given type is an integer type.
548  *
549  * @param type  The type to check.
550  * @return True if type is an integer type.
551  */
552 bool is_type_integer(const type_t *type)
553 {
554         assert(!is_typeref(type));
555
556         if(type->kind == TYPE_ENUM)
557                 return true;
558
559         if(type->kind != TYPE_ATOMIC)
560                 return false;
561
562         switch(type->atomic.akind) {
563         case ATOMIC_TYPE_BOOL:
564         case ATOMIC_TYPE_CHAR:
565         case ATOMIC_TYPE_SCHAR:
566         case ATOMIC_TYPE_UCHAR:
567         case ATOMIC_TYPE_SHORT:
568         case ATOMIC_TYPE_USHORT:
569         case ATOMIC_TYPE_INT:
570         case ATOMIC_TYPE_UINT:
571         case ATOMIC_TYPE_LONG:
572         case ATOMIC_TYPE_ULONG:
573         case ATOMIC_TYPE_LONGLONG:
574         case ATOMIC_TYPE_ULONGLONG:
575                 return true;
576         default:
577                 return false;
578         }
579 }
580
581 /**
582  * Returns true if the given type is an floating point type.
583  *
584  * @param type  The type to check.
585  * @return True if type is a floating point type.
586  */
587 bool is_type_float(const type_t *type)
588 {
589         assert(!is_typeref(type));
590
591         if(type->kind != TYPE_ATOMIC)
592                 return false;
593
594         switch(type->atomic.akind) {
595         case ATOMIC_TYPE_FLOAT:
596         case ATOMIC_TYPE_DOUBLE:
597         case ATOMIC_TYPE_LONG_DOUBLE:
598 #ifdef PROVIDE_COMPLEX
599         case ATOMIC_TYPE_FLOAT_COMPLEX:
600         case ATOMIC_TYPE_DOUBLE_COMPLEX:
601         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
602         case ATOMIC_TYPE_FLOAT_IMAGINARY:
603         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
604         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
605 #endif
606                 return true;
607         default:
608                 return false;
609         }
610 }
611
612 /**
613  * Returns true if the given type is a signed type.
614  *
615  * @param type  The type to check.
616  * @return True if type is a signed type.
617  */
618 bool is_type_signed(const type_t *type)
619 {
620         assert(!is_typeref(type));
621
622         /* enum types are int for now */
623         if(type->kind == TYPE_ENUM)
624                 return true;
625
626         if(type->kind != TYPE_ATOMIC)
627                 return false;
628
629         switch(type->atomic.akind) {
630         case ATOMIC_TYPE_CHAR:
631         case ATOMIC_TYPE_SCHAR:
632         case ATOMIC_TYPE_SHORT:
633         case ATOMIC_TYPE_INT:
634         case ATOMIC_TYPE_LONG:
635         case ATOMIC_TYPE_LONGLONG:
636         case ATOMIC_TYPE_FLOAT:
637         case ATOMIC_TYPE_DOUBLE:
638         case ATOMIC_TYPE_LONG_DOUBLE:
639 #ifdef PROVIDE_COMPLEX
640         case ATOMIC_TYPE_FLOAT_COMPLEX:
641         case ATOMIC_TYPE_DOUBLE_COMPLEX:
642         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
643         case ATOMIC_TYPE_FLOAT_IMAGINARY:
644         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
645         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
646 #endif
647                 return true;
648
649         case ATOMIC_TYPE_BOOL:
650         case ATOMIC_TYPE_UCHAR:
651         case ATOMIC_TYPE_USHORT:
652         case ATOMIC_TYPE_UINT:
653         case ATOMIC_TYPE_ULONG:
654         case ATOMIC_TYPE_ULONGLONG:
655                 return false;
656
657         case ATOMIC_TYPE_VOID:
658         case ATOMIC_TYPE_INVALID:
659         case ATOMIC_TYPE_LAST:
660                 return false;
661         }
662
663         panic("invalid atomic type found");
664         return false;
665 }
666
667 /**
668  * Returns true if the given type represents an arithmetic type.
669  *
670  * @param type  The type to check.
671  * @return True if type represents an arithmetic type.
672  */
673 bool is_type_arithmetic(const type_t *type)
674 {
675         assert(!is_typeref(type));
676
677         if(type->kind == TYPE_BITFIELD)
678                 return true;
679
680         if(is_type_integer(type) || is_type_float(type))
681                 return true;
682
683         return false;
684 }
685
686 /**
687  * Returns true if the given type represents a scalar type.
688  *
689  * @param type  The type to check.
690  * @return True if type represents a scalar type.
691  */
692 bool is_type_scalar(const type_t *type)
693 {
694         assert(!is_typeref(type));
695
696         switch (type->kind) {
697                 case TYPE_POINTER: return true;
698                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
699                 default:            break;
700         }
701
702         return is_type_arithmetic(type);
703 }
704
705 /**
706  * Check if a given type is incomplete.
707  *
708  * @param type  The type to check.
709  * @return True if the given type is incomplete (ie. just forward).
710  */
711 bool is_type_incomplete(const type_t *type)
712 {
713         assert(!is_typeref(type));
714
715         switch(type->kind) {
716         case TYPE_COMPOUND_STRUCT:
717         case TYPE_COMPOUND_UNION: {
718                 const compound_type_t *compound_type = &type->compound;
719                 declaration_t         *declaration   = compound_type->declaration;
720                 return !declaration->init.is_defined;
721         }
722         case TYPE_ENUM: {
723                 const enum_type_t *enum_type   = &type->enumt;
724                 declaration_t     *declaration = enum_type->declaration;
725                 return !declaration->init.is_defined;
726         }
727         case TYPE_BITFIELD:
728         case TYPE_FUNCTION:
729                 return true;
730
731         case TYPE_ARRAY:
732                 return type->array.size_expression == NULL;
733
734         case TYPE_ATOMIC:
735                 return type->atomic.akind == ATOMIC_TYPE_VOID;
736
737         case TYPE_POINTER:
738         case TYPE_BUILTIN:
739         case TYPE_ERROR:
740                 return false;
741
742         case TYPE_TYPEDEF:
743         case TYPE_TYPEOF:
744                 panic("is_type_incomplete called without typerefs skipped");
745         case TYPE_INVALID:
746                 break;
747         }
748
749         panic("invalid type found");
750 }
751
752 /**
753  * Check if two function types are compatible.
754  */
755 static bool function_types_compatible(const function_type_t *func1,
756                                       const function_type_t *func2)
757 {
758         const type_t* const ret1 = skip_typeref(func1->return_type);
759         const type_t* const ret2 = skip_typeref(func2->return_type);
760         if (!types_compatible(ret1, ret2))
761                 return false;
762
763         /* can parameters be compared? */
764         if(func1->unspecified_parameters || func2->unspecified_parameters)
765                 return true;
766
767         if(func1->variadic != func2->variadic)
768                 return false;
769
770         /* TODO: handling of unspecified parameters not correct yet */
771
772         /* all argument types must be compatible */
773         function_parameter_t *parameter1 = func1->parameters;
774         function_parameter_t *parameter2 = func2->parameters;
775         for( ; parameter1 != NULL && parameter2 != NULL;
776                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
777                 type_t *parameter1_type = skip_typeref(parameter1->type);
778                 type_t *parameter2_type = skip_typeref(parameter2->type);
779
780                 parameter1_type = get_unqualified_type(parameter1_type);
781                 parameter2_type = get_unqualified_type(parameter2_type);
782
783                 if(!types_compatible(parameter1_type, parameter2_type))
784                         return false;
785         }
786         /* same number of arguments? */
787         if(parameter1 != NULL || parameter2 != NULL)
788                 return false;
789
790         return true;
791 }
792
793 /**
794  * Check if two array types are compatible.
795  */
796 static bool array_types_compatible(const array_type_t *array1,
797                                    const array_type_t *array2)
798 {
799         type_t *element_type1 = skip_typeref(array1->element_type);
800         type_t *element_type2 = skip_typeref(array2->element_type);
801         if(!types_compatible(element_type1, element_type2))
802                 return false;
803
804         if(!array1->size_constant || !array2->size_constant)
805                 return true;
806
807         return array1->size == array2->size;
808 }
809
810 /**
811  * Check if two types are compatible.
812  */
813 bool types_compatible(const type_t *type1, const type_t *type2)
814 {
815         assert(!is_typeref(type1));
816         assert(!is_typeref(type2));
817
818         /* shortcut: the same type is always compatible */
819         if(type1 == type2)
820                 return true;
821
822         if(type1->base.qualifiers != type2->base.qualifiers)
823                 return false;
824         if(type1->kind != type2->kind)
825                 return false;
826
827         switch(type1->kind) {
828         case TYPE_FUNCTION:
829                 return function_types_compatible(&type1->function, &type2->function);
830         case TYPE_ATOMIC:
831                 return type1->atomic.akind == type2->atomic.akind;
832         case TYPE_ARRAY:
833                 return array_types_compatible(&type1->array, &type2->array);
834
835         case TYPE_POINTER: {
836                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
837                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
838                 return types_compatible(to1, to2);
839         }
840
841         case TYPE_COMPOUND_STRUCT:
842         case TYPE_COMPOUND_UNION:
843         case TYPE_ENUM:
844         case TYPE_BUILTIN:
845                 /* TODO: not implemented */
846                 break;
847
848         case TYPE_BITFIELD:
849                 /* not sure if this makes sense or is even needed, implement it if you
850                  * really need it! */
851                 panic("type compatibility check for bitfield type");
852
853         case TYPE_ERROR:
854                 /* Hmm, the error type should be compatible to all other types */
855                 return true;
856         case TYPE_INVALID:
857                 panic("invalid type found in compatible types");
858         case TYPE_TYPEDEF:
859         case TYPE_TYPEOF:
860                 panic("typerefs not skipped in compatible types?!?");
861         }
862
863         /* TODO: incomplete */
864         return false;
865 }
866
867 /**
868  * Check if two pointer types are compatible.
869  */
870 bool pointers_compatible(const type_t *type1, const type_t *type2)
871 {
872         assert(!is_typeref(type1));
873         assert(!is_typeref(type2));
874
875         assert(type1->kind == TYPE_POINTER);
876         assert(type2->kind == TYPE_POINTER);
877         (void) type1;
878         (void) type2;
879         /* TODO */
880         return true;
881 }
882
883 /**
884  * Skip all typerefs and return the underlying type.
885  */
886 type_t *skip_typeref(type_t *type)
887 {
888         unsigned qualifiers = TYPE_QUALIFIER_NONE;
889
890         while(true) {
891                 switch(type->kind) {
892                 case TYPE_ERROR:
893                         return type;
894                 case TYPE_TYPEDEF: {
895                         qualifiers |= type->base.qualifiers;
896                         const typedef_type_t *typedef_type = &type->typedeft;
897                         if(typedef_type->resolved_type != NULL) {
898                                 type = typedef_type->resolved_type;
899                                 break;
900                         }
901                         type = typedef_type->declaration->type;
902                         continue;
903                 }
904                 case TYPE_TYPEOF: {
905                         const typeof_type_t *typeof_type = &type->typeoft;
906                         if(typeof_type->typeof_type != NULL) {
907                                 type = typeof_type->typeof_type;
908                         } else {
909                                 type = typeof_type->expression->base.type;
910                         }
911                         continue;
912                 }
913                 default:
914                         break;
915                 }
916                 break;
917         }
918
919         if (qualifiers != TYPE_QUALIFIER_NONE) {
920                 type_t *const copy     = duplicate_type(type);
921                 copy->base.qualifiers |= qualifiers;
922
923                 type = typehash_insert(copy);
924                 if (type != copy) {
925                         obstack_free(type_obst, copy);
926                 }
927         }
928
929         return type;
930 }
931
932 /**
933  * Hash the given type and return the "singleton" version
934  * of it.
935  */
936 static type_t *identify_new_type(type_t *type)
937 {
938         type_t *result = typehash_insert(type);
939         if(result != type) {
940                 obstack_free(type_obst, type);
941         }
942         return result;
943 }
944
945 /**
946  * Creates a new atomic type.
947  *
948  * @param akind       The kind of the atomic type.
949  * @param qualifiers  Type qualifiers for the new type.
950  */
951 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
952 {
953         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
954         memset(type, 0, sizeof(atomic_type_t));
955
956         type->kind            = TYPE_ATOMIC;
957         type->base.qualifiers = qualifiers;
958         type->atomic.akind    = atype;
959
960         return identify_new_type(type);
961 }
962
963 /**
964  * Creates a new pointer type.
965  *
966  * @param points_to   The points-to type for teh new type.
967  * @param qualifiers  Type qualifiers for the new type.
968  */
969 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
970 {
971         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
972         memset(type, 0, sizeof(pointer_type_t));
973
974         type->kind              = TYPE_POINTER;
975         type->base.qualifiers   = qualifiers;
976         type->pointer.points_to = points_to;
977
978         return identify_new_type(type);
979 }
980
981 type_t *make_array_type(type_t *element_type, size_t size,
982                         type_qualifiers_t qualifiers)
983 {
984         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
985         memset(type, 0, sizeof(array_type_t));
986
987         type->kind                = TYPE_ARRAY;
988         type->base.qualifiers     = qualifiers;
989         type->array.element_type  = element_type;
990         type->array.size          = size;
991         type->array.size_constant = true;
992
993         return identify_new_type(type);
994 }
995
996 /**
997  * Debug helper. Prints the given type to stdout.
998  */
999 static __attribute__((unused))
1000 void dbg_type(const type_t *type)
1001 {
1002         FILE *old_out = out;
1003         out = stderr;
1004         print_type(type);
1005         puts("\n");
1006         fflush(stderr);
1007         out = old_out;
1008 }