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