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