float type alignment will be set later
[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_LONGLONG].alignment  = 4;
173         props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
174
175         props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UINT];
176
177         /* initialize complex/imaginary types */
178         props[ATOMIC_TYPE_FLOAT_COMPLEX]              = props[ATOMIC_TYPE_FLOAT];
179         props[ATOMIC_TYPE_FLOAT_COMPLEX].flags       |= ATOMIC_TYPE_FLAG_COMPLEX;
180         props[ATOMIC_TYPE_FLOAT_COMPLEX].size        *= 2;
181         props[ATOMIC_TYPE_DOUBLE_COMPLEX]             = props[ATOMIC_TYPE_DOUBLE];
182         props[ATOMIC_TYPE_DOUBLE_COMPLEX].flags      |= ATOMIC_TYPE_FLAG_COMPLEX;
183         props[ATOMIC_TYPE_DOUBLE_COMPLEX].size       *= 2;
184         props[ATOMIC_TYPE_LONG_DOUBLE_COMPLEX]
185                 = props[ATOMIC_TYPE_LONG_DOUBLE];
186         props[ATOMIC_TYPE_LONG_DOUBLE_COMPLEX].flags |= ATOMIC_TYPE_FLAG_COMPLEX;
187         props[ATOMIC_TYPE_LONG_DOUBLE_COMPLEX].size  *= 2;
188
189         props[ATOMIC_TYPE_FLOAT_IMAGINARY]       = props[ATOMIC_TYPE_FLOAT];
190         props[ATOMIC_TYPE_DOUBLE_IMAGINARY]      = props[ATOMIC_TYPE_DOUBLE];
191         props[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = props[ATOMIC_TYPE_LONG_DOUBLE];
192 }
193
194 void exit_types(void)
195 {
196         obstack_free(type_obst, NULL);
197 }
198
199 void type_set_output(FILE *stream)
200 {
201         out = stream;
202 }
203
204 void inc_type_visited(void)
205 {
206         type_visited++;
207 }
208
209 void print_type_qualifiers(type_qualifiers_t qualifiers)
210 {
211         if(qualifiers & TYPE_QUALIFIER_CONST)    fputs("const ",    out);
212         if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
213         if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
214 }
215
216 /**
217  * Prints the name of a atomic type.
218  *
219  * @param type  The type.
220  */
221 static
222 void print_atomic_type(const atomic_type_t *type)
223 {
224         print_type_qualifiers(type->base.qualifiers);
225
226         const char *s = "INVALIDATOMIC";
227         switch((atomic_type_kind_t) type->akind) {
228         case ATOMIC_TYPE_INVALID:     break;
229         case ATOMIC_TYPE_VOID:                  s = "void";               break;
230         case ATOMIC_TYPE_BOOL:                  s = "_Bool";              break;
231         case ATOMIC_TYPE_CHAR:                  s = "char";               break;
232         case ATOMIC_TYPE_SCHAR:                 s = "signed char";        break;
233         case ATOMIC_TYPE_UCHAR:                 s = "unsigned char";      break;
234         case ATOMIC_TYPE_INT:                   s = "int";                break;
235         case ATOMIC_TYPE_UINT:                  s = "unsigned int";       break;
236         case ATOMIC_TYPE_SHORT:                 s = "short";              break;
237         case ATOMIC_TYPE_USHORT:                s = "unsigned short";     break;
238         case ATOMIC_TYPE_LONG:                  s = "long";               break;
239         case ATOMIC_TYPE_ULONG:                 s = "unsigned long";      break;
240         case ATOMIC_TYPE_LONGLONG:              s = "long long";          break;
241         case ATOMIC_TYPE_ULONGLONG:             s = "unsigned long long"; break;
242         case ATOMIC_TYPE_LONG_DOUBLE:           s = "long double";        break;
243         case ATOMIC_TYPE_FLOAT:                 s = "float";              break;
244         case ATOMIC_TYPE_DOUBLE:                s = "double";             break;
245         case ATOMIC_TYPE_FLOAT_COMPLEX:         s = "_Complex float";     break;
246         case ATOMIC_TYPE_DOUBLE_COMPLEX:        s = "_Complex float";     break;
247         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:   s = "_Complex float";     break;
248         case ATOMIC_TYPE_FLOAT_IMAGINARY:       s = "_Imaginary float";   break;
249         case ATOMIC_TYPE_DOUBLE_IMAGINARY:      s = "_Imaginary float";   break;
250         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY: s = "_Imaginary float";   break;
251         }
252         fputs(s, out);
253 }
254
255 /**
256  * Print the first part (the prefix) of a type.
257  *
258  * @param type   The type to print.
259  * @param top    true, if this is the top type, false if it's an embedded type.
260  */
261 static void print_function_type_pre(const function_type_t *type, bool top)
262 {
263         print_type_qualifiers(type->base.qualifiers);
264
265         intern_print_type_pre(type->return_type, false);
266
267         /* don't emit braces if we're the toplevel type... */
268         if(!top)
269                 fputc('(', out);
270 }
271
272 /**
273  * Print the second part (the postfix) of a type.
274  *
275  * @param type   The type to print.
276  * @param top    true, if this is the top type, false if it's an embedded type.
277  */
278 static void print_function_type_post(const function_type_t *type,
279                                      const scope_t *scope, bool top)
280 {
281         intern_print_type_post(type->return_type, false);
282         /* don't emit braces if we're the toplevel type... */
283         if(!top)
284                 fputc(')', out);
285
286         fputc('(', out);
287
288         int first = 1;
289         if(scope == NULL) {
290                 function_parameter_t *parameter = type->parameters;
291                 for( ; parameter != NULL; parameter = parameter->next) {
292                         if(first) {
293                                 first = 0;
294                         } else {
295                                 fputs(", ", out);
296                         }
297                         print_type(parameter->type);
298                 }
299         } else {
300                 declaration_t *parameter = scope->declarations;
301                 for( ; parameter != NULL; parameter = parameter->next) {
302                         if(first) {
303                                 first = 0;
304                         } else {
305                                 fputs(", ", out);
306                         }
307                         print_type_ext(parameter->type, parameter->symbol,
308                                        &parameter->scope);
309                 }
310         }
311         if(type->variadic) {
312                 if(first) {
313                         first = 0;
314                 } else {
315                         fputs(", ", out);
316                 }
317                 fputs("...", out);
318         }
319         if(first && !type->unspecified_parameters) {
320                 fputs("void", out);
321         }
322         fputc(')', out);
323 }
324
325 /**
326  * Prints the prefix part of a pointer type.
327  *
328  * @param type   The pointer type.
329  */
330 static void print_pointer_type_pre(const pointer_type_t *type)
331 {
332         intern_print_type_pre(type->points_to, false);
333         fputs("*", out);
334         print_type_qualifiers(type->base.qualifiers);
335 }
336
337 /**
338  * Prints the postfix part of a pointer type.
339  *
340  * @param type   The pointer type.
341  */
342 static void print_pointer_type_post(const pointer_type_t *type)
343 {
344         intern_print_type_post(type->points_to, false);
345 }
346
347 /**
348  * Prints the prefix part of an array type.
349  *
350  * @param type   The array type.
351  */
352 static void print_array_type_pre(const array_type_t *type)
353 {
354         intern_print_type_pre(type->element_type, false);
355 }
356
357 /**
358  * Prints the postfix part of an array type.
359  *
360  * @param type   The array type.
361  */
362 static void print_array_type_post(const array_type_t *type)
363 {
364         fputc('[', out);
365         if(type->is_static) {
366                 fputs("static ", out);
367         }
368         print_type_qualifiers(type->base.qualifiers);
369         if(type->size_expression != NULL
370                         && (print_implicit_array_size || !type->has_implicit_size)) {
371                 print_expression(type->size_expression);
372         }
373         fputc(']', out);
374         intern_print_type_post(type->element_type, false);
375 }
376
377 /**
378  * Prints the postfix part of a bitfield type.
379  *
380  * @param type   The array type.
381  */
382 static void print_bitfield_type_post(const bitfield_type_t *type)
383 {
384         fputs(" : ", out);
385         print_expression(type->size);
386         intern_print_type_post(type->base_type, false);
387 }
388
389 /**
390  * Prints an enum definition.
391  *
392  * @param declaration  The enum's type declaration.
393  */
394 void print_enum_definition(const declaration_t *declaration)
395 {
396         fputs("{\n", out);
397
398         change_indent(1);
399
400         declaration_t *entry = declaration->next;
401         for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
402                entry = entry->next) {
403
404                 print_indent();
405                 fprintf(out, "%s", entry->symbol->string);
406                 if(entry->init.initializer != NULL) {
407                         fprintf(out, " = ");
408
409                         /* skip the implicit cast */
410                         expression_t *expression = entry->init.enum_value;
411                         if(expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
412                                 expression = expression->unary.value;
413                         }
414                         print_expression(expression);
415                 }
416                 fprintf(out, ",\n");
417         }
418
419         change_indent(-1);
420         print_indent();
421         fputs("}", out);
422 }
423
424 /**
425  * Prints an enum type.
426  *
427  * @param type  The enum type.
428  */
429 static void print_type_enum(const enum_type_t *type)
430 {
431         print_type_qualifiers(type->base.qualifiers);
432         fputs("enum ", out);
433
434         declaration_t *declaration = type->declaration;
435         symbol_t      *symbol      = declaration->symbol;
436         if(symbol != NULL) {
437                 fputs(symbol->string, out);
438         } else {
439                 print_enum_definition(declaration);
440         }
441 }
442
443 /**
444  * Print the compound part of a compound type.
445  *
446  * @param declaration  The declaration of the compound type.
447  */
448 void print_compound_definition(const declaration_t *declaration)
449 {
450         fputs("{\n", out);
451         change_indent(1);
452
453         declaration_t *iter = declaration->scope.declarations;
454         for( ; iter != NULL; iter = iter->next) {
455                 print_indent();
456                 print_declaration(iter);
457                 fputc('\n', out);
458         }
459
460         change_indent(-1);
461         print_indent();
462         fputs("}", out);
463 }
464
465 /**
466  * Prints a compound type.
467  *
468  * @param type  The compound type.
469  */
470 static void print_compound_type(const compound_type_t *type)
471 {
472         print_type_qualifiers(type->base.qualifiers);
473
474         if(type->base.kind == TYPE_COMPOUND_STRUCT) {
475                 fputs("struct ", out);
476         } else {
477                 assert(type->base.kind == TYPE_COMPOUND_UNION);
478                 fputs("union ", out);
479         }
480
481         declaration_t *declaration = type->declaration;
482         symbol_t      *symbol      = declaration->symbol;
483         if(symbol != NULL) {
484                 fputs(symbol->string, out);
485         } else {
486                 print_compound_definition(declaration);
487         }
488 }
489
490 /**
491  * Prints the prefix part of a typedef type.
492  *
493  * @param type   The typedef type.
494  */
495 static void print_typedef_type_pre(const typedef_type_t *const type)
496 {
497         print_type_qualifiers(type->base.qualifiers);
498         fputs(type->declaration->symbol->string, out);
499 }
500
501 /**
502  * Prints the prefix part of a typeof type.
503  *
504  * @param type   The typeof type.
505  */
506 static void print_typeof_type_pre(const typeof_type_t *const type)
507 {
508         fputs("typeof(", out);
509         if(type->expression != NULL) {
510                 assert(type->typeof_type == NULL);
511                 print_expression(type->expression);
512         } else {
513                 print_type(type->typeof_type);
514         }
515         fputc(')', out);
516 }
517
518 /**
519  * Prints the prefix part of a type.
520  *
521  * @param type   The type.
522  * @param top    true if we print the toplevel type, false else.
523  */
524 static void intern_print_type_pre(const type_t *const type, const bool top)
525 {
526         switch(type->kind) {
527         case TYPE_ERROR:
528                 fputs("<error>", out);
529         case TYPE_INVALID:
530                 fputs("<invalid>", out);
531                 return;
532         case TYPE_ENUM:
533                 print_type_enum(&type->enumt);
534                 return;
535         case TYPE_ATOMIC:
536                 print_atomic_type(&type->atomic);
537                 return;
538         case TYPE_COMPOUND_STRUCT:
539         case TYPE_COMPOUND_UNION:
540                 print_compound_type(&type->compound);
541                 return;
542         case TYPE_BUILTIN:
543                 fputs(type->builtin.symbol->string, out);
544                 return;
545         case TYPE_FUNCTION:
546                 print_function_type_pre(&type->function, top);
547                 return;
548         case TYPE_POINTER:
549                 print_pointer_type_pre(&type->pointer);
550                 return;
551         case TYPE_BITFIELD:
552                 intern_print_type_pre(type->bitfield.base_type, top);
553                 return;
554         case TYPE_ARRAY:
555                 print_array_type_pre(&type->array);
556                 return;
557         case TYPE_TYPEDEF:
558                 print_typedef_type_pre(&type->typedeft);
559                 return;
560         case TYPE_TYPEOF:
561                 print_typeof_type_pre(&type->typeoft);
562                 return;
563         }
564         fputs("unknown", out);
565 }
566
567 /**
568  * Prints the postfix part of a type.
569  *
570  * @param type   The type.
571  * @param top    true if we print the toplevel type, false else.
572  */
573 static void intern_print_type_post(const type_t *const type, const bool top)
574 {
575         switch(type->kind) {
576         case TYPE_FUNCTION:
577                 print_function_type_post(&type->function, NULL, top);
578                 return;
579         case TYPE_POINTER:
580                 print_pointer_type_post(&type->pointer);
581                 return;
582         case TYPE_ARRAY:
583                 print_array_type_post(&type->array);
584                 return;
585         case TYPE_BITFIELD:
586                 print_bitfield_type_post(&type->bitfield);
587                 return;
588         case TYPE_ERROR:
589         case TYPE_INVALID:
590         case TYPE_ATOMIC:
591         case TYPE_ENUM:
592         case TYPE_COMPOUND_STRUCT:
593         case TYPE_COMPOUND_UNION:
594         case TYPE_BUILTIN:
595         case TYPE_TYPEOF:
596         case TYPE_TYPEDEF:
597                 break;
598         }
599 }
600
601 /**
602  * Prints a type.
603  *
604  * @param type   The type.
605  */
606 void print_type(const type_t *const type)
607 {
608         print_type_ext(type, NULL, NULL);
609 }
610
611 void print_type_ext(const type_t *const type, const symbol_t *symbol,
612                     const scope_t *scope)
613 {
614         if(type == NULL) {
615                 fputs("nil type", out);
616                 return;
617         }
618
619         intern_print_type_pre(type, true);
620         if(symbol != NULL) {
621                 fputc(' ', out);
622                 fputs(symbol->string, out);
623         }
624         if(type->kind == TYPE_FUNCTION) {
625                 print_function_type_post(&type->function, scope, true);
626         } else {
627                 intern_print_type_post(type, true);
628         }
629 }
630
631 /**
632  * Return the size of a type AST node.
633  *
634  * @param type  The type.
635  */
636 static size_t get_type_size(const type_t *type)
637 {
638         switch(type->kind) {
639         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
640         case TYPE_COMPOUND_STRUCT:
641         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
642         case TYPE_ENUM:            return sizeof(enum_type_t);
643         case TYPE_FUNCTION:        return sizeof(function_type_t);
644         case TYPE_POINTER:         return sizeof(pointer_type_t);
645         case TYPE_ARRAY:           return sizeof(array_type_t);
646         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
647         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
648         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
649         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
650         case TYPE_ERROR:           panic("error type found");
651         case TYPE_INVALID:         panic("invalid type found");
652         }
653         panic("unknown type found");
654 }
655
656 /**
657  * Duplicates a type.
658  *
659  * @param type  The type to copy.
660  * @return A copy of the type.
661  *
662  * @note This does not produce a deep copy!
663  */
664 type_t *duplicate_type(const type_t *type)
665 {
666         size_t size = get_type_size(type);
667
668         type_t *copy = obstack_alloc(type_obst, size);
669         memcpy(copy, type, size);
670
671         return copy;
672 }
673
674 /**
675  * Returns the unqualified type of a given type.
676  *
677  * @param type  The type.
678  * @returns The unqualified type.
679  */
680 type_t *get_unqualified_type(type_t *type)
681 {
682         if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
683                 return type;
684
685         type_t *unqualified_type          = duplicate_type(type);
686         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
687
688         type_t *result = typehash_insert(unqualified_type);
689         if(result != unqualified_type) {
690                 obstack_free(type_obst, unqualified_type);
691         }
692
693         return result;
694 }
695
696 /**
697  * Check if a type is valid.
698  *
699  * @param type  The type to check.
700  * @return true if type represents a valid type.
701  */
702 bool type_valid(const type_t *type)
703 {
704         return type->kind != TYPE_INVALID;
705 }
706
707 static bool test_atomic_type_flag(atomic_type_kind_t kind,
708                                   atomic_type_flag_t flag)
709 {
710         assert(kind <= ATOMIC_TYPE_LAST);
711         return (atomic_type_properties[kind].flags & flag) != 0;
712 }
713
714 /**
715  * Returns true if the given type is an integer type.
716  *
717  * @param type  The type to check.
718  * @return True if type is an integer type.
719  */
720 bool is_type_integer(const type_t *type)
721 {
722         assert(!is_typeref(type));
723
724         if(type->kind == TYPE_ENUM)
725                 return true;
726
727         if(type->kind != TYPE_ATOMIC)
728                 return false;
729
730         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
731 }
732
733 /**
734  * Returns true if the given type is an floating point type.
735  *
736  * @param type  The type to check.
737  * @return True if type is a floating point type.
738  */
739 bool is_type_float(const type_t *type)
740 {
741         assert(!is_typeref(type));
742
743         if(type->kind != TYPE_ATOMIC)
744                 return false;
745
746         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
747 }
748
749 /**
750  * Returns true if the given type is a signed type.
751  *
752  * @param type  The type to check.
753  * @return True if type is a signed type.
754  */
755 bool is_type_signed(const type_t *type)
756 {
757         assert(!is_typeref(type));
758
759         /* enum types are int for now */
760         if(type->kind == TYPE_ENUM)
761                 return true;
762
763         if(type->kind != TYPE_ATOMIC)
764                 return false;
765
766         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
767 }
768
769 /**
770  * Returns true if the given type represents an arithmetic type.
771  *
772  * @param type  The type to check.
773  * @return True if type represents an arithmetic type.
774  */
775 bool is_type_arithmetic(const type_t *type)
776 {
777         assert(!is_typeref(type));
778
779         if(type->kind == TYPE_BITFIELD || type->kind == TYPE_ENUM)
780                 return true;
781         if(type->kind != TYPE_ATOMIC)
782                 return false;
783
784         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
785 }
786
787 /**
788  * Returns true if the given type represents a scalar type.
789  *
790  * @param type  The type to check.
791  * @return True if type represents a scalar type.
792  */
793 bool is_type_scalar(const type_t *type)
794 {
795         assert(!is_typeref(type));
796
797         switch (type->kind) {
798                 case TYPE_POINTER: return true;
799                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
800                 default:           break;
801         }
802
803         return is_type_arithmetic(type);
804 }
805
806 /**
807  * Check if a given type is incomplete.
808  *
809  * @param type  The type to check.
810  * @return True if the given type is incomplete (ie. just forward).
811  */
812 bool is_type_incomplete(const type_t *type)
813 {
814         assert(!is_typeref(type));
815
816         switch(type->kind) {
817         case TYPE_COMPOUND_STRUCT:
818         case TYPE_COMPOUND_UNION: {
819                 const compound_type_t *compound_type = &type->compound;
820                 declaration_t         *declaration   = compound_type->declaration;
821                 return !declaration->init.is_defined;
822         }
823         case TYPE_ENUM: {
824                 const enum_type_t *enum_type   = &type->enumt;
825                 declaration_t     *declaration = enum_type->declaration;
826                 return !declaration->init.is_defined;
827         }
828         case TYPE_BITFIELD:
829         case TYPE_FUNCTION:
830                 return true;
831
832         case TYPE_ARRAY:
833                 return type->array.size_expression == NULL;
834
835         case TYPE_ATOMIC:
836                 return type->atomic.akind == ATOMIC_TYPE_VOID;
837
838         case TYPE_POINTER:
839         case TYPE_BUILTIN:
840         case TYPE_ERROR:
841                 return false;
842
843         case TYPE_TYPEDEF:
844         case TYPE_TYPEOF:
845                 panic("is_type_incomplete called without typerefs skipped");
846         case TYPE_INVALID:
847                 break;
848         }
849
850         panic("invalid type found");
851 }
852
853 /**
854  * Check if two function types are compatible.
855  */
856 static bool function_types_compatible(const function_type_t *func1,
857                                       const function_type_t *func2)
858 {
859         const type_t* const ret1 = skip_typeref(func1->return_type);
860         const type_t* const ret2 = skip_typeref(func2->return_type);
861         if (!types_compatible(ret1, ret2))
862                 return false;
863
864         /* can parameters be compared? */
865         if(func1->unspecified_parameters || func2->unspecified_parameters)
866                 return true;
867
868         if(func1->variadic != func2->variadic)
869                 return false;
870
871         /* TODO: handling of unspecified parameters not correct yet */
872
873         /* all argument types must be compatible */
874         function_parameter_t *parameter1 = func1->parameters;
875         function_parameter_t *parameter2 = func2->parameters;
876         for( ; parameter1 != NULL && parameter2 != NULL;
877                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
878                 type_t *parameter1_type = skip_typeref(parameter1->type);
879                 type_t *parameter2_type = skip_typeref(parameter2->type);
880
881                 parameter1_type = get_unqualified_type(parameter1_type);
882                 parameter2_type = get_unqualified_type(parameter2_type);
883
884                 if(!types_compatible(parameter1_type, parameter2_type))
885                         return false;
886         }
887         /* same number of arguments? */
888         if(parameter1 != NULL || parameter2 != NULL)
889                 return false;
890
891         return true;
892 }
893
894 /**
895  * Check if two array types are compatible.
896  */
897 static bool array_types_compatible(const array_type_t *array1,
898                                    const array_type_t *array2)
899 {
900         type_t *element_type1 = skip_typeref(array1->element_type);
901         type_t *element_type2 = skip_typeref(array2->element_type);
902         if(!types_compatible(element_type1, element_type2))
903                 return false;
904
905         if(!array1->size_constant || !array2->size_constant)
906                 return true;
907
908         return array1->size == array2->size;
909 }
910
911 /**
912  * Check if two types are compatible.
913  */
914 bool types_compatible(const type_t *type1, const type_t *type2)
915 {
916         assert(!is_typeref(type1));
917         assert(!is_typeref(type2));
918
919         /* shortcut: the same type is always compatible */
920         if(type1 == type2)
921                 return true;
922
923         if(type1->base.qualifiers != type2->base.qualifiers)
924                 return false;
925         if(type1->kind != type2->kind)
926                 return false;
927
928         switch(type1->kind) {
929         case TYPE_FUNCTION:
930                 return function_types_compatible(&type1->function, &type2->function);
931         case TYPE_ATOMIC:
932                 return type1->atomic.akind == type2->atomic.akind;
933         case TYPE_ARRAY:
934                 return array_types_compatible(&type1->array, &type2->array);
935
936         case TYPE_POINTER: {
937                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
938                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
939                 return types_compatible(to1, to2);
940         }
941
942         case TYPE_COMPOUND_STRUCT:
943         case TYPE_COMPOUND_UNION:
944         case TYPE_ENUM:
945         case TYPE_BUILTIN:
946                 /* TODO: not implemented */
947                 break;
948
949         case TYPE_BITFIELD:
950                 /* not sure if this makes sense or is even needed, implement it if you
951                  * really need it! */
952                 panic("type compatibility check for bitfield type");
953
954         case TYPE_ERROR:
955                 /* Hmm, the error type should be compatible to all other types */
956                 return true;
957         case TYPE_INVALID:
958                 panic("invalid type found in compatible types");
959         case TYPE_TYPEDEF:
960         case TYPE_TYPEOF:
961                 panic("typerefs not skipped in compatible types?!?");
962         }
963
964         /* TODO: incomplete */
965         return false;
966 }
967
968 /**
969  * Check if two pointer types are compatible.
970  */
971 bool pointers_compatible(const type_t *type1, const type_t *type2)
972 {
973         assert(!is_typeref(type1));
974         assert(!is_typeref(type2));
975
976         assert(type1->kind == TYPE_POINTER);
977         assert(type2->kind == TYPE_POINTER);
978         (void) type1;
979         (void) type2;
980         /* TODO */
981         return true;
982 }
983
984 /**
985  * Skip all typerefs and return the underlying type.
986  */
987 type_t *skip_typeref(type_t *type)
988 {
989         unsigned qualifiers = TYPE_QUALIFIER_NONE;
990
991         while(true) {
992                 switch(type->kind) {
993                 case TYPE_ERROR:
994                         return type;
995                 case TYPE_TYPEDEF: {
996                         qualifiers |= type->base.qualifiers;
997                         const typedef_type_t *typedef_type = &type->typedeft;
998                         if(typedef_type->resolved_type != NULL) {
999                                 type = typedef_type->resolved_type;
1000                                 break;
1001                         }
1002                         type = typedef_type->declaration->type;
1003                         continue;
1004                 }
1005                 case TYPE_TYPEOF: {
1006                         const typeof_type_t *typeof_type = &type->typeoft;
1007                         if(typeof_type->typeof_type != NULL) {
1008                                 type = typeof_type->typeof_type;
1009                         } else {
1010                                 type = typeof_type->expression->base.type;
1011                         }
1012                         continue;
1013                 }
1014                 default:
1015                         break;
1016                 }
1017                 break;
1018         }
1019
1020         if (qualifiers != TYPE_QUALIFIER_NONE) {
1021                 type_t *const copy     = duplicate_type(type);
1022                 copy->base.qualifiers |= qualifiers;
1023
1024                 type = typehash_insert(copy);
1025                 if (type != copy) {
1026                         obstack_free(type_obst, copy);
1027                 }
1028         }
1029
1030         return type;
1031 }
1032
1033 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1034 {
1035         assert(kind <= ATOMIC_TYPE_LAST);
1036         return atomic_type_properties[kind].size;
1037 }
1038
1039 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1040 {
1041         assert(kind <= ATOMIC_TYPE_LAST);
1042         return atomic_type_properties[kind].alignment;
1043 }
1044
1045 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1046 {
1047         assert(kind <= ATOMIC_TYPE_LAST);
1048         return atomic_type_properties[kind].flags;
1049 }
1050
1051 atomic_type_kind_t get_intptr_kind(void)
1052 {
1053         if(machine_size <= 32)
1054                 return ATOMIC_TYPE_INT;
1055         else if(machine_size <= 64)
1056                 return ATOMIC_TYPE_LONG;
1057         else
1058                 return ATOMIC_TYPE_LONGLONG;
1059 }
1060
1061 atomic_type_kind_t get_uintptr_kind(void)
1062 {
1063         if(machine_size <= 32)
1064                 return ATOMIC_TYPE_UINT;
1065         else if(machine_size <= 64)
1066                 return ATOMIC_TYPE_ULONG;
1067         else
1068                 return ATOMIC_TYPE_ULONGLONG;
1069 }
1070
1071 /**
1072  * Find the atomic type kind representing a given size (signed).
1073  */
1074 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1075         static atomic_type_kind_t kinds[32];
1076
1077         assert(size < 32);
1078         atomic_type_kind_t kind = kinds[size];
1079         if(kind == ATOMIC_TYPE_INVALID) {
1080                 static const atomic_type_kind_t possible_kinds[] = {
1081                         ATOMIC_TYPE_SCHAR,
1082                         ATOMIC_TYPE_SHORT,
1083                         ATOMIC_TYPE_INT,
1084                         ATOMIC_TYPE_LONG,
1085                         ATOMIC_TYPE_LONGLONG
1086                 };
1087                 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1088                         if(get_atomic_type_size(possible_kinds[i]) == size) {
1089                                 kind = possible_kinds[i];
1090                                 break;
1091                         }
1092                 }
1093                 kinds[size] = kind;
1094         }
1095         return kind;
1096 }
1097
1098 /**
1099  * Find the atomic type kind representing a given size (signed).
1100  */
1101 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1102         static atomic_type_kind_t kinds[32];
1103
1104         assert(size < 32);
1105         atomic_type_kind_t kind = kinds[size];
1106         if(kind == ATOMIC_TYPE_INVALID) {
1107                 static const atomic_type_kind_t possible_kinds[] = {
1108                         ATOMIC_TYPE_UCHAR,
1109                         ATOMIC_TYPE_USHORT,
1110                         ATOMIC_TYPE_UINT,
1111                         ATOMIC_TYPE_ULONG,
1112                         ATOMIC_TYPE_ULONGLONG
1113                 };
1114                 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1115                         if(get_atomic_type_size(possible_kinds[i]) == size) {
1116                                 kind = possible_kinds[i];
1117                                 break;
1118                         }
1119                 }
1120                 kinds[size] = kind;
1121         }
1122         return kind;
1123 }
1124
1125 /**
1126  * Hash the given type and return the "singleton" version
1127  * of it.
1128  */
1129 static type_t *identify_new_type(type_t *type)
1130 {
1131         type_t *result = typehash_insert(type);
1132         if(result != type) {
1133                 obstack_free(type_obst, type);
1134         }
1135         return result;
1136 }
1137
1138 /**
1139  * Creates a new atomic type.
1140  *
1141  * @param akind       The kind of the atomic type.
1142  * @param qualifiers  Type qualifiers for the new type.
1143  */
1144 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
1145 {
1146         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1147         memset(type, 0, sizeof(atomic_type_t));
1148
1149         type->kind            = TYPE_ATOMIC;
1150         type->base.qualifiers = qualifiers;
1151         type->base.alignment  = 0;
1152         type->atomic.akind    = atype;
1153
1154         /* TODO: set the alignment depending on the atype here */
1155
1156         return identify_new_type(type);
1157 }
1158
1159 /**
1160  * Creates a new pointer type.
1161  *
1162  * @param points_to   The points-to type for the new type.
1163  * @param qualifiers  Type qualifiers for the new type.
1164  */
1165 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1166 {
1167         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1168         memset(type, 0, sizeof(pointer_type_t));
1169
1170         type->kind              = TYPE_POINTER;
1171         type->base.qualifiers   = qualifiers;
1172         type->base.alignment    = 0;
1173         type->pointer.points_to = points_to;
1174
1175         return identify_new_type(type);
1176 }
1177
1178 type_t *make_array_type(type_t *element_type, size_t size,
1179                         type_qualifiers_t qualifiers)
1180 {
1181         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1182         memset(type, 0, sizeof(array_type_t));
1183
1184         type->kind                = TYPE_ARRAY;
1185         type->base.qualifiers     = qualifiers;
1186         type->base.alignment      = 0;
1187         type->array.element_type  = element_type;
1188         type->array.size          = size;
1189         type->array.size_constant = true;
1190
1191         return identify_new_type(type);
1192 }
1193
1194 /**
1195  * Debug helper. Prints the given type to stdout.
1196  */
1197 static __attribute__((unused))
1198 void dbg_type(const type_t *type)
1199 {
1200         FILE *old_out = out;
1201         out = stderr;
1202         print_type(type);
1203         puts("\n");
1204         fflush(stderr);
1205         out = old_out;
1206 }