avoid endless recursion when printing anonymous enums with init expressions
[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
254                         /* skip the implicit cast */
255                         expression_t *expression = entry->init.enum_value;
256                         if(expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
257                                 expression = expression->unary.value;
258                         }
259                         print_expression(expression);
260                 }
261                 fprintf(out, ",\n");
262         }
263
264         change_indent(-1);
265         print_indent();
266         fputs("}", out);
267 }
268
269 /**
270  * Prints an enum type.
271  *
272  * @param type  The enum type.
273  */
274 static void print_type_enum(const enum_type_t *type)
275 {
276         print_type_qualifiers(type->type.qualifiers);
277         fputs("enum ", out);
278
279         declaration_t *declaration = type->declaration;
280         symbol_t      *symbol      = declaration->symbol;
281         if(symbol != NULL) {
282                 fputs(symbol->string, out);
283         } else {
284                 print_enum_definition(declaration);
285         }
286 }
287
288 /**
289  * Print the compound part of a compound type.
290  *
291  * @param declaration  The declaration of the compound type.
292  */
293 void print_compound_definition(const declaration_t *declaration)
294 {
295         fputs("{\n", out);
296         change_indent(1);
297
298         declaration_t *iter = declaration->scope.declarations;
299         for( ; iter != NULL; iter = iter->next) {
300                 print_indent();
301                 print_declaration(iter);
302                 fputc('\n', out);
303         }
304
305         change_indent(-1);
306         print_indent();
307         fputs("}", out);
308 }
309
310 /**
311  * Prints a compound type.
312  *
313  * @param type  The compound type.
314  */
315 static void print_compound_type(const compound_type_t *type)
316 {
317         print_type_qualifiers(type->type.qualifiers);
318
319         if(type->type.kind == TYPE_COMPOUND_STRUCT) {
320                 fputs("struct ", out);
321         } else {
322                 assert(type->type.kind == TYPE_COMPOUND_UNION);
323                 fputs("union ", out);
324         }
325
326         declaration_t *declaration = type->declaration;
327         symbol_t      *symbol      = declaration->symbol;
328         if(symbol != NULL) {
329                 fputs(symbol->string, out);
330         } else {
331                 print_compound_definition(declaration);
332         }
333 }
334
335 /**
336  * Prints the prefix part of a typedef type.
337  *
338  * @param type   The typedef type.
339  */
340 static void print_typedef_type_pre(const typedef_type_t *const type)
341 {
342         print_type_qualifiers(type->type.qualifiers);
343         fputs(type->declaration->symbol->string, out);
344 }
345
346 /**
347  * Prints the prefix part of a typeof type.
348  *
349  * @param type   The typeof type.
350  */
351 static void print_typeof_type_pre(const typeof_type_t *const type)
352 {
353         fputs("typeof(", out);
354         if(type->expression != NULL) {
355                 assert(type->typeof_type == NULL);
356                 print_expression(type->expression);
357         } else {
358                 print_type(type->typeof_type);
359         }
360         fputc(')', out);
361 }
362
363 /**
364  * Prints the prefix part of a type.
365  *
366  * @param type   The type.
367  * @param top    true if we print the toplevel type, false else.
368  */
369 static void intern_print_type_pre(const type_t *const type, const bool top)
370 {
371         switch(type->kind) {
372         case TYPE_ERROR:
373                 fputs("<error>", out);
374         case TYPE_INVALID:
375                 fputs("<invalid>", out);
376                 return;
377         case TYPE_ENUM:
378                 print_type_enum(&type->enumt);
379                 return;
380         case TYPE_ATOMIC:
381                 print_atomic_type(&type->atomic);
382                 return;
383         case TYPE_COMPOUND_STRUCT:
384         case TYPE_COMPOUND_UNION:
385                 print_compound_type(&type->compound);
386                 return;
387         case TYPE_BUILTIN:
388                 fputs(type->builtin.symbol->string, out);
389                 return;
390         case TYPE_FUNCTION:
391                 print_function_type_pre(&type->function, top);
392                 return;
393         case TYPE_POINTER:
394                 print_pointer_type_pre(&type->pointer);
395                 return;
396         case TYPE_BITFIELD:
397                 intern_print_type_pre(type->bitfield.base, top);
398                 return;
399         case TYPE_ARRAY:
400                 print_array_type_pre(&type->array);
401                 return;
402         case TYPE_TYPEDEF:
403                 print_typedef_type_pre(&type->typedeft);
404                 return;
405         case TYPE_TYPEOF:
406                 print_typeof_type_pre(&type->typeoft);
407                 return;
408         }
409         fputs("unknown", out);
410 }
411
412 /**
413  * Prints the postfix part of a type.
414  *
415  * @param type   The type.
416  * @param top    true if we print the toplevel type, false else.
417  */
418 static void intern_print_type_post(const type_t *const type, const bool top)
419 {
420         switch(type->kind) {
421         case TYPE_FUNCTION:
422                 print_function_type_post(&type->function, NULL, top);
423                 return;
424         case TYPE_POINTER:
425                 print_pointer_type_post(&type->pointer);
426                 return;
427         case TYPE_ARRAY:
428                 print_array_type_post(&type->array);
429                 return;
430         case TYPE_BITFIELD:
431                 print_bitfield_type_post(&type->bitfield);
432                 return;
433         case TYPE_ERROR:
434         case TYPE_INVALID:
435         case TYPE_ATOMIC:
436         case TYPE_ENUM:
437         case TYPE_COMPOUND_STRUCT:
438         case TYPE_COMPOUND_UNION:
439         case TYPE_BUILTIN:
440         case TYPE_TYPEOF:
441         case TYPE_TYPEDEF:
442                 break;
443         }
444 }
445
446 /**
447  * Prints a type.
448  *
449  * @param type   The type.
450  */
451 void print_type(const type_t *const type)
452 {
453         print_type_ext(type, NULL, NULL);
454 }
455
456 void print_type_ext(const type_t *const type, const symbol_t *symbol,
457                     const scope_t *scope)
458 {
459         if(type == NULL) {
460                 fputs("nil type", out);
461                 return;
462         }
463
464         intern_print_type_pre(type, true);
465         if(symbol != NULL) {
466                 fputc(' ', out);
467                 fputs(symbol->string, out);
468         }
469         if(type->kind == TYPE_FUNCTION) {
470                 print_function_type_post(&type->function, scope, true);
471         } else {
472                 intern_print_type_post(type, true);
473         }
474 }
475
476 /**
477  * Return the size of a type AST node.
478  *
479  * @param type  The type.
480  */
481 static size_t get_type_size(const type_t *type)
482 {
483         switch(type->kind) {
484         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
485         case TYPE_COMPOUND_STRUCT:
486         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
487         case TYPE_ENUM:            return sizeof(enum_type_t);
488         case TYPE_FUNCTION:        return sizeof(function_type_t);
489         case TYPE_POINTER:         return sizeof(pointer_type_t);
490         case TYPE_ARRAY:           return sizeof(array_type_t);
491         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
492         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
493         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
494         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
495         case TYPE_ERROR:           panic("error type found");
496         case TYPE_INVALID:         panic("invalid type found");
497         }
498         panic("unknown type found");
499 }
500
501 /**
502  * Duplicates a type.
503  *
504  * @param type  The type to copy.
505  * @return A copy of the type.
506  *
507  * @note This does not produce a deep copy!
508  */
509 type_t *duplicate_type(const type_t *type)
510 {
511         size_t size = get_type_size(type);
512
513         type_t *copy = obstack_alloc(type_obst, size);
514         memcpy(copy, type, size);
515
516         return copy;
517 }
518
519 /**
520  * Returns the unqualified type of a given type.
521  *
522  * @param type  The type.
523  * @returns The unqualified type.
524  */
525 type_t *get_unqualified_type(type_t *type)
526 {
527         if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
528                 return type;
529
530         type_t *unqualified_type          = duplicate_type(type);
531         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
532
533         type_t *result = typehash_insert(unqualified_type);
534         if(result != unqualified_type) {
535                 obstack_free(type_obst, unqualified_type);
536         }
537
538         return result;
539 }
540
541 /**
542  * Check if a type is valid.
543  *
544  * @param type  The type to check.
545  * @return true if type represents a valid type.
546  */
547 bool type_valid(const type_t *type)
548 {
549         return type->kind != TYPE_INVALID;
550 }
551
552 /**
553  * Returns true if the given type is an integer type.
554  *
555  * @param type  The type to check.
556  * @return True if type is an integer type.
557  */
558 bool is_type_integer(const type_t *type)
559 {
560         assert(!is_typeref(type));
561
562         if(type->kind == TYPE_ENUM)
563                 return true;
564
565         if(type->kind != TYPE_ATOMIC)
566                 return false;
567
568         switch(type->atomic.akind) {
569         case ATOMIC_TYPE_BOOL:
570         case ATOMIC_TYPE_CHAR:
571         case ATOMIC_TYPE_SCHAR:
572         case ATOMIC_TYPE_UCHAR:
573         case ATOMIC_TYPE_SHORT:
574         case ATOMIC_TYPE_USHORT:
575         case ATOMIC_TYPE_INT:
576         case ATOMIC_TYPE_UINT:
577         case ATOMIC_TYPE_LONG:
578         case ATOMIC_TYPE_ULONG:
579         case ATOMIC_TYPE_LONGLONG:
580         case ATOMIC_TYPE_ULONGLONG:
581                 return true;
582         default:
583                 return false;
584         }
585 }
586
587 /**
588  * Returns true if the given type is an floating point type.
589  *
590  * @param type  The type to check.
591  * @return True if type is a floating point type.
592  */
593 bool is_type_float(const type_t *type)
594 {
595         assert(!is_typeref(type));
596
597         if(type->kind != TYPE_ATOMIC)
598                 return false;
599
600         switch(type->atomic.akind) {
601         case ATOMIC_TYPE_FLOAT:
602         case ATOMIC_TYPE_DOUBLE:
603         case ATOMIC_TYPE_LONG_DOUBLE:
604 #ifdef PROVIDE_COMPLEX
605         case ATOMIC_TYPE_FLOAT_COMPLEX:
606         case ATOMIC_TYPE_DOUBLE_COMPLEX:
607         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
608         case ATOMIC_TYPE_FLOAT_IMAGINARY:
609         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
610         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
611 #endif
612                 return true;
613         default:
614                 return false;
615         }
616 }
617
618 /**
619  * Returns true if the given type is a signed type.
620  *
621  * @param type  The type to check.
622  * @return True if type is a signed type.
623  */
624 bool is_type_signed(const type_t *type)
625 {
626         assert(!is_typeref(type));
627
628         /* enum types are int for now */
629         if(type->kind == TYPE_ENUM)
630                 return true;
631
632         if(type->kind != TYPE_ATOMIC)
633                 return false;
634
635         switch(type->atomic.akind) {
636         case ATOMIC_TYPE_CHAR:
637         case ATOMIC_TYPE_SCHAR:
638         case ATOMIC_TYPE_SHORT:
639         case ATOMIC_TYPE_INT:
640         case ATOMIC_TYPE_LONG:
641         case ATOMIC_TYPE_LONGLONG:
642         case ATOMIC_TYPE_FLOAT:
643         case ATOMIC_TYPE_DOUBLE:
644         case ATOMIC_TYPE_LONG_DOUBLE:
645 #ifdef PROVIDE_COMPLEX
646         case ATOMIC_TYPE_FLOAT_COMPLEX:
647         case ATOMIC_TYPE_DOUBLE_COMPLEX:
648         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
649         case ATOMIC_TYPE_FLOAT_IMAGINARY:
650         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
651         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
652 #endif
653                 return true;
654
655         case ATOMIC_TYPE_BOOL:
656         case ATOMIC_TYPE_UCHAR:
657         case ATOMIC_TYPE_USHORT:
658         case ATOMIC_TYPE_UINT:
659         case ATOMIC_TYPE_ULONG:
660         case ATOMIC_TYPE_ULONGLONG:
661                 return false;
662
663         case ATOMIC_TYPE_VOID:
664         case ATOMIC_TYPE_INVALID:
665         case ATOMIC_TYPE_LAST:
666                 return false;
667         }
668
669         panic("invalid atomic type found");
670         return false;
671 }
672
673 /**
674  * Returns true if the given type represents an arithmetic type.
675  *
676  * @param type  The type to check.
677  * @return True if type represents an arithmetic type.
678  */
679 bool is_type_arithmetic(const type_t *type)
680 {
681         assert(!is_typeref(type));
682
683         if(type->kind == TYPE_BITFIELD)
684                 return true;
685
686         if(is_type_integer(type) || is_type_float(type))
687                 return true;
688
689         return false;
690 }
691
692 /**
693  * Returns true if the given type represents a scalar type.
694  *
695  * @param type  The type to check.
696  * @return True if type represents a scalar type.
697  */
698 bool is_type_scalar(const type_t *type)
699 {
700         assert(!is_typeref(type));
701
702         switch (type->kind) {
703                 case TYPE_POINTER: return true;
704                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
705                 default:            break;
706         }
707
708         return is_type_arithmetic(type);
709 }
710
711 /**
712  * Check if a given type is incomplete.
713  *
714  * @param type  The type to check.
715  * @return True if the given type is incomplete (ie. just forward).
716  */
717 bool is_type_incomplete(const type_t *type)
718 {
719         assert(!is_typeref(type));
720
721         switch(type->kind) {
722         case TYPE_COMPOUND_STRUCT:
723         case TYPE_COMPOUND_UNION: {
724                 const compound_type_t *compound_type = &type->compound;
725                 declaration_t         *declaration   = compound_type->declaration;
726                 return !declaration->init.is_defined;
727         }
728         case TYPE_ENUM: {
729                 const enum_type_t *enum_type   = &type->enumt;
730                 declaration_t     *declaration = enum_type->declaration;
731                 return !declaration->init.is_defined;
732         }
733         case TYPE_BITFIELD:
734         case TYPE_FUNCTION:
735                 return true;
736
737         case TYPE_ARRAY:
738                 return type->array.size_expression == NULL;
739
740         case TYPE_ATOMIC:
741                 return type->atomic.akind == ATOMIC_TYPE_VOID;
742
743         case TYPE_POINTER:
744         case TYPE_BUILTIN:
745         case TYPE_ERROR:
746                 return false;
747
748         case TYPE_TYPEDEF:
749         case TYPE_TYPEOF:
750                 panic("is_type_incomplete called without typerefs skipped");
751         case TYPE_INVALID:
752                 break;
753         }
754
755         panic("invalid type found");
756 }
757
758 /**
759  * Check if two function types are compatible.
760  */
761 static bool function_types_compatible(const function_type_t *func1,
762                                       const function_type_t *func2)
763 {
764         const type_t* const ret1 = skip_typeref(func1->return_type);
765         const type_t* const ret2 = skip_typeref(func2->return_type);
766         if (!types_compatible(ret1, ret2))
767                 return false;
768
769         /* can parameters be compared? */
770         if(func1->unspecified_parameters || func2->unspecified_parameters)
771                 return true;
772
773         if(func1->variadic != func2->variadic)
774                 return false;
775
776         /* TODO: handling of unspecified parameters not correct yet */
777
778         /* all argument types must be compatible */
779         function_parameter_t *parameter1 = func1->parameters;
780         function_parameter_t *parameter2 = func2->parameters;
781         for( ; parameter1 != NULL && parameter2 != NULL;
782                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
783                 type_t *parameter1_type = skip_typeref(parameter1->type);
784                 type_t *parameter2_type = skip_typeref(parameter2->type);
785
786                 parameter1_type = get_unqualified_type(parameter1_type);
787                 parameter2_type = get_unqualified_type(parameter2_type);
788
789                 if(!types_compatible(parameter1_type, parameter2_type))
790                         return false;
791         }
792         /* same number of arguments? */
793         if(parameter1 != NULL || parameter2 != NULL)
794                 return false;
795
796         return true;
797 }
798
799 /**
800  * Check if two array types are compatible.
801  */
802 static bool array_types_compatible(const array_type_t *array1,
803                                    const array_type_t *array2)
804 {
805         type_t *element_type1 = skip_typeref(array1->element_type);
806         type_t *element_type2 = skip_typeref(array2->element_type);
807         if(!types_compatible(element_type1, element_type2))
808                 return false;
809
810         if(!array1->size_constant || !array2->size_constant)
811                 return true;
812
813         return array1->size == array2->size;
814 }
815
816 /**
817  * Check if two types are compatible.
818  */
819 bool types_compatible(const type_t *type1, const type_t *type2)
820 {
821         assert(!is_typeref(type1));
822         assert(!is_typeref(type2));
823
824         /* shortcut: the same type is always compatible */
825         if(type1 == type2)
826                 return true;
827
828         if(type1->base.qualifiers != type2->base.qualifiers)
829                 return false;
830         if(type1->kind != type2->kind)
831                 return false;
832
833         switch(type1->kind) {
834         case TYPE_FUNCTION:
835                 return function_types_compatible(&type1->function, &type2->function);
836         case TYPE_ATOMIC:
837                 return type1->atomic.akind == type2->atomic.akind;
838         case TYPE_ARRAY:
839                 return array_types_compatible(&type1->array, &type2->array);
840
841         case TYPE_POINTER: {
842                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
843                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
844                 return types_compatible(to1, to2);
845         }
846
847         case TYPE_COMPOUND_STRUCT:
848         case TYPE_COMPOUND_UNION:
849         case TYPE_ENUM:
850         case TYPE_BUILTIN:
851                 /* TODO: not implemented */
852                 break;
853
854         case TYPE_BITFIELD:
855                 /* not sure if this makes sense or is even needed, implement it if you
856                  * really need it! */
857                 panic("type compatibility check for bitfield type");
858
859         case TYPE_ERROR:
860                 /* Hmm, the error type should be compatible to all other types */
861                 return true;
862         case TYPE_INVALID:
863                 panic("invalid type found in compatible types");
864         case TYPE_TYPEDEF:
865         case TYPE_TYPEOF:
866                 panic("typerefs not skipped in compatible types?!?");
867         }
868
869         /* TODO: incomplete */
870         return false;
871 }
872
873 /**
874  * Check if two pointer types are compatible.
875  */
876 bool pointers_compatible(const type_t *type1, const type_t *type2)
877 {
878         assert(!is_typeref(type1));
879         assert(!is_typeref(type2));
880
881         assert(type1->kind == TYPE_POINTER);
882         assert(type2->kind == TYPE_POINTER);
883         (void) type1;
884         (void) type2;
885         /* TODO */
886         return true;
887 }
888
889 /**
890  * Skip all typerefs and return the underlying type.
891  */
892 type_t *skip_typeref(type_t *type)
893 {
894         unsigned qualifiers = TYPE_QUALIFIER_NONE;
895
896         while(true) {
897                 switch(type->kind) {
898                 case TYPE_ERROR:
899                         return type;
900                 case TYPE_TYPEDEF: {
901                         qualifiers |= type->base.qualifiers;
902                         const typedef_type_t *typedef_type = &type->typedeft;
903                         if(typedef_type->resolved_type != NULL) {
904                                 type = typedef_type->resolved_type;
905                                 break;
906                         }
907                         type = typedef_type->declaration->type;
908                         continue;
909                 }
910                 case TYPE_TYPEOF: {
911                         const typeof_type_t *typeof_type = &type->typeoft;
912                         if(typeof_type->typeof_type != NULL) {
913                                 type = typeof_type->typeof_type;
914                         } else {
915                                 type = typeof_type->expression->base.type;
916                         }
917                         continue;
918                 }
919                 default:
920                         break;
921                 }
922                 break;
923         }
924
925         if (qualifiers != TYPE_QUALIFIER_NONE) {
926                 type_t *const copy     = duplicate_type(type);
927                 copy->base.qualifiers |= qualifiers;
928
929                 type = typehash_insert(copy);
930                 if (type != copy) {
931                         obstack_free(type_obst, copy);
932                 }
933         }
934
935         return type;
936 }
937
938 /**
939  * Hash the given type and return the "singleton" version
940  * of it.
941  */
942 static type_t *identify_new_type(type_t *type)
943 {
944         type_t *result = typehash_insert(type);
945         if(result != type) {
946                 obstack_free(type_obst, type);
947         }
948         return result;
949 }
950
951 /**
952  * Creates a new atomic type.
953  *
954  * @param akind       The kind of the atomic type.
955  * @param qualifiers  Type qualifiers for the new type.
956  */
957 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
958 {
959         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
960         memset(type, 0, sizeof(atomic_type_t));
961
962         type->kind            = TYPE_ATOMIC;
963         type->base.qualifiers = qualifiers;
964         type->base.alignment  = 0;
965         type->atomic.akind    = atype;
966
967         /* TODO: set the aligmnent depending on the atype here */
968
969         return identify_new_type(type);
970 }
971
972 /**
973  * Creates a new pointer type.
974  *
975  * @param points_to   The points-to type for teh new type.
976  * @param qualifiers  Type qualifiers for the new type.
977  */
978 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
979 {
980         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
981         memset(type, 0, sizeof(pointer_type_t));
982
983         type->kind              = TYPE_POINTER;
984         type->base.qualifiers   = qualifiers;
985         type->base.alignment    = 0;
986         type->pointer.points_to = points_to;
987
988         return identify_new_type(type);
989 }
990
991 type_t *make_array_type(type_t *element_type, size_t size,
992                         type_qualifiers_t qualifiers)
993 {
994         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
995         memset(type, 0, sizeof(array_type_t));
996
997         type->kind                = TYPE_ARRAY;
998         type->base.qualifiers     = qualifiers;
999         type->base.alignment      = 0;
1000         type->array.element_type  = element_type;
1001         type->array.size          = size;
1002         type->array.size_constant = true;
1003
1004         return identify_new_type(type);
1005 }
1006
1007 /**
1008  * Debug helper. Prints the given type to stdout.
1009  */
1010 static __attribute__((unused))
1011 void dbg_type(const type_t *type)
1012 {
1013         FILE *old_out = out;
1014         out = stderr;
1015         print_type(type);
1016         puts("\n");
1017         fflush(stderr);
1018         out = old_out;
1019 }