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