- fixed handling of calling convention in function_types_compatible()
[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 "types.h"
27 #include "entity_t.h"
28 #include "symbol_t.h"
29 #include "type_hash.h"
30 #include "adt/error.h"
31 #include "adt/util.h"
32 #include "lang_features.h"
33 #include "warning.h"
34 #include "diagnostic.h"
35 #include "driver/firm_cmdline.h"
36
37 /** The default calling convention. */
38 cc_kind_t default_calling_convention = CC_CDECL;
39
40 static struct obstack   _type_obst;
41 static FILE            *out;
42 struct obstack         *type_obst                 = &_type_obst;
43 static bool             print_implicit_array_size = false;
44
45 static void intern_print_type_pre(const type_t *type);
46 static void intern_print_type_post(const type_t *type);
47
48 typedef struct atomic_type_properties_t atomic_type_properties_t;
49 struct atomic_type_properties_t {
50         unsigned   size;              /**< type size in bytes */
51         unsigned   alignment;         /**< type alignment in bytes */
52         unsigned   flags;             /**< type flags from atomic_type_flag_t */
53 };
54
55 /**
56  * Properties of atomic types.
57  */
58 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
59         //ATOMIC_TYPE_INVALID = 0,
60         [ATOMIC_TYPE_VOID] = {
61                 .size       = 0,
62                 .alignment  = 0,
63                 .flags      = ATOMIC_TYPE_FLAG_NONE
64         },
65         [ATOMIC_TYPE_WCHAR_T] = {
66                 .size       = (unsigned)-1,
67                 .alignment  = (unsigned)-1,
68                 /* signed flag will be set when known */
69                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
70         },
71         [ATOMIC_TYPE_CHAR] = {
72                 .size       = 1,
73                 .alignment  = 1,
74                 /* signed flag will be set when known */
75                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
76         },
77         [ATOMIC_TYPE_SCHAR] = {
78                 .size       = 1,
79                 .alignment  = 1,
80                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
81                               | ATOMIC_TYPE_FLAG_SIGNED,
82         },
83         [ATOMIC_TYPE_UCHAR] = {
84                 .size       = 1,
85                 .alignment  = 1,
86                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
87         },
88         [ATOMIC_TYPE_SHORT] = {
89                 .size       = 2,
90                 .alignment  = 2,
91                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
92                               | ATOMIC_TYPE_FLAG_SIGNED
93         },
94         [ATOMIC_TYPE_USHORT] = {
95                 .size       = 2,
96                 .alignment  = 2,
97                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
98         },
99         [ATOMIC_TYPE_INT] = {
100                 .size       = (unsigned) -1,
101                 .alignment  = (unsigned) -1,
102                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
103                               | ATOMIC_TYPE_FLAG_SIGNED,
104         },
105         [ATOMIC_TYPE_UINT] = {
106                 .size       = (unsigned) -1,
107                 .alignment  = (unsigned) -1,
108                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
109         },
110         [ATOMIC_TYPE_LONG] = {
111                 .size       = (unsigned) -1,
112                 .alignment  = (unsigned) -1,
113                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
114                               | ATOMIC_TYPE_FLAG_SIGNED,
115         },
116         [ATOMIC_TYPE_ULONG] = {
117                 .size       = (unsigned) -1,
118                 .alignment  = (unsigned) -1,
119                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
120         },
121         [ATOMIC_TYPE_LONGLONG] = {
122                 .size       = (unsigned) -1,
123                 .alignment  = (unsigned) -1,
124                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
125                               | ATOMIC_TYPE_FLAG_SIGNED,
126         },
127         [ATOMIC_TYPE_ULONGLONG] = {
128                 .size       = (unsigned) -1,
129                 .alignment  = (unsigned) -1,
130                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
131         },
132         [ATOMIC_TYPE_BOOL] = {
133                 .size       = (unsigned) -1,
134                 .alignment  = (unsigned) -1,
135                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
136         },
137         [ATOMIC_TYPE_FLOAT] = {
138                 .size       = 4,
139                 .alignment  = (unsigned) -1,
140                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
141                               | ATOMIC_TYPE_FLAG_SIGNED,
142         },
143         [ATOMIC_TYPE_DOUBLE] = {
144                 .size       = 8,
145                 .alignment  = (unsigned) -1,
146                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
147                               | ATOMIC_TYPE_FLAG_SIGNED,
148         },
149         [ATOMIC_TYPE_LONG_DOUBLE] = {
150                 .size       = 12,
151                 .alignment  = (unsigned) -1,
152                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
153                               | ATOMIC_TYPE_FLAG_SIGNED,
154         },
155         /* complex and imaginary types are set in init_types */
156 };
157
158 void init_types(void)
159 {
160         obstack_init(type_obst);
161
162         atomic_type_properties_t *props = atomic_type_properties;
163
164         if (char_is_signed) {
165                 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
166         }
167
168         unsigned int_size   = machine_size < 32 ? 2 : 4;
169         unsigned long_size  = machine_size < 64 ? 4 : 8;
170         unsigned llong_size = machine_size < 32 ? 4 : 8;
171
172         props[ATOMIC_TYPE_INT].size            = int_size;
173         props[ATOMIC_TYPE_INT].alignment       = int_size;
174         props[ATOMIC_TYPE_UINT].size           = int_size;
175         props[ATOMIC_TYPE_UINT].alignment      = int_size;
176         props[ATOMIC_TYPE_LONG].size           = long_size;
177         props[ATOMIC_TYPE_LONG].alignment      = long_size;
178         props[ATOMIC_TYPE_ULONG].size          = long_size;
179         props[ATOMIC_TYPE_ULONG].alignment     = long_size;
180         props[ATOMIC_TYPE_LONGLONG].size       = llong_size;
181         props[ATOMIC_TYPE_LONGLONG].alignment  = llong_size;
182         props[ATOMIC_TYPE_ULONGLONG].size      = llong_size;
183         props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
184
185         /* TODO: backend specific, need a way to query the backend for this.
186          * The following are good settings for x86 */
187         props[ATOMIC_TYPE_FLOAT].alignment       = 4;
188         props[ATOMIC_TYPE_DOUBLE].alignment      = 4;
189         props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
190         props[ATOMIC_TYPE_LONGLONG].alignment    = 4;
191         props[ATOMIC_TYPE_ULONGLONG].alignment   = 4;
192         if (firm_opt.os_support == OS_SUPPORT_MACHO) {
193                 props[ATOMIC_TYPE_LONG_DOUBLE].size      = 16;
194                 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
195         }
196
197         /* TODO: make this configurable for platforms which do not use byte sized
198          * bools. */
199         props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
200
201         props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
202 }
203
204 void exit_types(void)
205 {
206         obstack_free(type_obst, NULL);
207 }
208
209 void type_set_output(FILE *stream)
210 {
211         out = stream;
212 }
213
214 void print_type_qualifiers(type_qualifiers_t qualifiers)
215 {
216         int first = 1;
217         if (qualifiers & TYPE_QUALIFIER_CONST) {
218                 fputs(" const" + first,    out);
219                 first = 0;
220         }
221         if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
222                 fputs(" volatile" + first, out);
223                 first = 0;
224         }
225         if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
226                 fputs(" restrict" + first, out);
227                 first = 0;
228         }
229 }
230
231 const char *get_atomic_kind_name(atomic_type_kind_t kind)
232 {
233         switch(kind) {
234         case ATOMIC_TYPE_INVALID: break;
235         case ATOMIC_TYPE_VOID:        return "void";
236         case ATOMIC_TYPE_WCHAR_T:     return "wchar_t";
237         case ATOMIC_TYPE_BOOL:        return c_mode & _CXX ? "bool" : "_Bool";
238         case ATOMIC_TYPE_CHAR:        return "char";
239         case ATOMIC_TYPE_SCHAR:       return "signed char";
240         case ATOMIC_TYPE_UCHAR:       return "unsigned char";
241         case ATOMIC_TYPE_INT:         return "int";
242         case ATOMIC_TYPE_UINT:        return "unsigned int";
243         case ATOMIC_TYPE_SHORT:       return "short";
244         case ATOMIC_TYPE_USHORT:      return "unsigned short";
245         case ATOMIC_TYPE_LONG:        return "long";
246         case ATOMIC_TYPE_ULONG:       return "unsigned long";
247         case ATOMIC_TYPE_LONGLONG:    return "long long";
248         case ATOMIC_TYPE_ULONGLONG:   return "unsigned long long";
249         case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
250         case ATOMIC_TYPE_FLOAT:       return "float";
251         case ATOMIC_TYPE_DOUBLE:      return "double";
252         }
253         return "INVALIDATOMIC";
254 }
255
256 /**
257  * Prints the name of an atomic type kinds.
258  *
259  * @param kind  The type kind.
260  */
261 static void print_atomic_kinds(atomic_type_kind_t kind)
262 {
263         const char *s = get_atomic_kind_name(kind);
264         fputs(s, out);
265 }
266
267 /**
268  * Prints the name of an atomic type.
269  *
270  * @param type  The type.
271  */
272 static void print_atomic_type(const atomic_type_t *type)
273 {
274         print_type_qualifiers(type->base.qualifiers);
275         if (type->base.qualifiers != 0)
276                 fputc(' ', out);
277         print_atomic_kinds(type->akind);
278 }
279
280 /**
281  * Prints the name of a complex type.
282  *
283  * @param type  The type.
284  */
285 static
286 void print_complex_type(const complex_type_t *type)
287 {
288         int empty = type->base.qualifiers == 0;
289         print_type_qualifiers(type->base.qualifiers);
290         fputs(" _Complex " + empty, out);
291         print_atomic_kinds(type->akind);
292 }
293
294 /**
295  * Prints the name of an imaginary type.
296  *
297  * @param type  The type.
298  */
299 static
300 void print_imaginary_type(const imaginary_type_t *type)
301 {
302         int empty = type->base.qualifiers == 0;
303         print_type_qualifiers(type->base.qualifiers);
304         fputs(" _Imaginary " + empty, out);
305         print_atomic_kinds(type->akind);
306 }
307
308 /**
309  * Print the first part (the prefix) of a type.
310  *
311  * @param type   The type to print.
312  */
313 static void print_function_type_pre(const function_type_t *type)
314 {
315         switch (type->linkage) {
316                 case LINKAGE_INVALID:
317                         break;
318
319                 case LINKAGE_C:
320                         if (c_mode & _CXX)
321                                 fputs("extern \"C\" ",   out);
322                         break;
323
324                 case LINKAGE_CXX:
325                         if (!(c_mode & _CXX))
326                                 fputs("extern \"C++\" ", out);
327                         break;
328         }
329
330         print_type_qualifiers(type->base.qualifiers);
331         if (type->base.qualifiers != 0)
332                 fputc(' ', out);
333
334         intern_print_type_pre(type->return_type);
335
336         switch (type->calling_convention) {
337         case CC_CDECL:    fputs(" __cdecl",    out); break;
338         case CC_STDCALL:  fputs(" __stdcall",  out); break;
339         case CC_FASTCALL: fputs(" __fastcall", out); break;
340         case CC_THISCALL: fputs(" __thiscall", out); break;
341         case CC_DEFAULT:  break;
342         }
343 }
344
345 /**
346  * Print the second part (the postfix) of a type.
347  *
348  * @param type   The type to print.
349  */
350 static void print_function_type_post(const function_type_t *type,
351                                      const scope_t *parameters)
352 {
353         fputc('(', out);
354         bool first = true;
355         if (parameters == NULL) {
356                 function_parameter_t *parameter = type->parameters;
357                 for( ; parameter != NULL; parameter = parameter->next) {
358                         if (first) {
359                                 first = false;
360                         } else {
361                                 fputs(", ", out);
362                         }
363                         print_type(parameter->type);
364                 }
365         } else {
366                 entity_t *parameter = parameters->entities;
367                 for (; parameter != NULL; parameter = parameter->base.next) {
368                         if (parameter->kind != ENTITY_PARAMETER)
369                                 continue;
370
371                         if (first) {
372                                 first = false;
373                         } else {
374                                 fputs(", ", out);
375                         }
376                         const type_t *const type = parameter->declaration.type;
377                         if (type == NULL) {
378                                 fputs(parameter->base.symbol->string, out);
379                         } else {
380                                 print_type_ext(type, parameter->base.symbol, NULL);
381                         }
382                 }
383         }
384         if (type->variadic) {
385                 if (first) {
386                         first = false;
387                 } else {
388                         fputs(", ", out);
389                 }
390                 fputs("...", out);
391         }
392         if (first && !type->unspecified_parameters) {
393                 fputs("void", out);
394         }
395         fputc(')', out);
396
397         intern_print_type_post(type->return_type);
398 }
399
400 /**
401  * Prints the prefix part of a pointer type.
402  *
403  * @param type   The pointer type.
404  */
405 static void print_pointer_type_pre(const pointer_type_t *type)
406 {
407         type_t const *const points_to = type->points_to;
408         intern_print_type_pre(points_to);
409         if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
410                 fputs(" (", out);
411         variable_t *const variable = type->base_variable;
412         if (variable != NULL) {
413                 fputs(" __based(", out);
414                 fputs(variable->base.base.symbol->string, out);
415                 fputs(") ", out);
416         }
417         fputc('*', out);
418         type_qualifiers_t const qual = type->base.qualifiers;
419         if (qual != 0)
420                 fputc(' ', out);
421         print_type_qualifiers(qual);
422 }
423
424 /**
425  * Prints the postfix part of a pointer type.
426  *
427  * @param type   The pointer type.
428  */
429 static void print_pointer_type_post(const pointer_type_t *type)
430 {
431         type_t const *const points_to = type->points_to;
432         if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
433                 fputc(')', out);
434         intern_print_type_post(points_to);
435 }
436
437 /**
438  * Prints the prefix part of a reference type.
439  *
440  * @param type   The reference type.
441  */
442 static void print_reference_type_pre(const reference_type_t *type)
443 {
444         type_t const *const refers_to = type->refers_to;
445         intern_print_type_pre(refers_to);
446         if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
447                 fputs(" (", out);
448         fputc('&', out);
449 }
450
451 /**
452  * Prints the postfix part of a reference type.
453  *
454  * @param type   The reference type.
455  */
456 static void print_reference_type_post(const reference_type_t *type)
457 {
458         type_t const *const refers_to = type->refers_to;
459         if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
460                 fputc(')', out);
461         intern_print_type_post(refers_to);
462 }
463
464 /**
465  * Prints the prefix part of an array type.
466  *
467  * @param type   The array type.
468  */
469 static void print_array_type_pre(const array_type_t *type)
470 {
471         intern_print_type_pre(type->element_type);
472 }
473
474 /**
475  * Prints the postfix part of an array type.
476  *
477  * @param type   The array type.
478  */
479 static void print_array_type_post(const array_type_t *type)
480 {
481         fputc('[', out);
482         if (type->is_static) {
483                 fputs("static ", out);
484         }
485         print_type_qualifiers(type->base.qualifiers);
486         if (type->base.qualifiers != 0)
487                 fputc(' ', out);
488         if (type->size_expression != NULL
489                         && (print_implicit_array_size || !type->has_implicit_size)) {
490                 print_expression(type->size_expression);
491         }
492         fputc(']', out);
493         intern_print_type_post(type->element_type);
494 }
495
496 /**
497  * Prints the postfix part of a bitfield type.
498  *
499  * @param type   The array type.
500  */
501 static void print_bitfield_type_post(const bitfield_type_t *type)
502 {
503         fputs(" : ", out);
504         print_expression(type->size_expression);
505         intern_print_type_post(type->base_type);
506 }
507
508 /**
509  * Prints an enum definition.
510  *
511  * @param declaration  The enum's type declaration.
512  */
513 void print_enum_definition(const enum_t *enume)
514 {
515         fputs("{\n", out);
516
517         change_indent(1);
518
519         entity_t *entry = enume->base.next;
520         for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
521                entry = entry->base.next) {
522
523                 print_indent();
524                 fputs(entry->base.symbol->string, out);
525                 if (entry->enum_value.value != NULL) {
526                         fputs(" = ", out);
527
528                         /* skip the implicit cast */
529                         expression_t *expression = entry->enum_value.value;
530                         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
531                                 expression = expression->unary.value;
532                         }
533                         print_expression(expression);
534                 }
535                 fputs(",\n", out);
536         }
537
538         change_indent(-1);
539         print_indent();
540         fputc('}', out);
541 }
542
543 /**
544  * Prints an enum type.
545  *
546  * @param type  The enum type.
547  */
548 static void print_type_enum(const enum_type_t *type)
549 {
550         int empty = type->base.qualifiers == 0;
551         print_type_qualifiers(type->base.qualifiers);
552         fputs(" enum " + empty, out);
553
554         enum_t   *enume  = type->enume;
555         symbol_t *symbol = enume->base.symbol;
556         if (symbol != NULL) {
557                 fputs(symbol->string, out);
558         } else {
559                 print_enum_definition(enume);
560         }
561 }
562
563 /**
564  * Print the compound part of a compound type.
565  */
566 void print_compound_definition(const compound_t *compound)
567 {
568         fputs("{\n", out);
569         change_indent(1);
570
571         entity_t *entity = compound->members.entities;
572         for( ; entity != NULL; entity = entity->base.next) {
573                 if (entity->kind != ENTITY_COMPOUND_MEMBER)
574                         continue;
575
576                 print_indent();
577                 print_entity(entity);
578                 fputc('\n', out);
579         }
580
581         change_indent(-1);
582         print_indent();
583         fputc('}', out);
584         if (compound->modifiers & DM_TRANSPARENT_UNION) {
585                 fputs("__attribute__((__transparent_union__))", out);
586         }
587 }
588
589 /**
590  * Prints a compound type.
591  *
592  * @param type  The compound type.
593  */
594 static void print_compound_type(const compound_type_t *type)
595 {
596         int empty = type->base.qualifiers == 0;
597         print_type_qualifiers(type->base.qualifiers);
598
599         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
600                 fputs(" struct " + empty, out);
601         } else {
602                 assert(type->base.kind == TYPE_COMPOUND_UNION);
603                 fputs(" union " + empty, out);
604         }
605
606         compound_t *compound = type->compound;
607         symbol_t   *symbol   = compound->base.symbol;
608         if (symbol != NULL) {
609                 fputs(symbol->string, out);
610         } else {
611                 print_compound_definition(compound);
612         }
613 }
614
615 /**
616  * Prints the prefix part of a typedef type.
617  *
618  * @param type   The typedef type.
619  */
620 static void print_typedef_type_pre(const typedef_type_t *const type)
621 {
622         print_type_qualifiers(type->base.qualifiers);
623         if (type->base.qualifiers != 0)
624                 fputc(' ', out);
625         fputs(type->typedefe->base.symbol->string, out);
626 }
627
628 /**
629  * Prints the prefix part of a typeof type.
630  *
631  * @param type   The typeof type.
632  */
633 static void print_typeof_type_pre(const typeof_type_t *const type)
634 {
635         fputs("typeof(", out);
636         if (type->expression != NULL) {
637                 print_expression(type->expression);
638         } else {
639                 print_type(type->typeof_type);
640         }
641         fputc(')', out);
642 }
643
644 /**
645  * Prints the prefix part of a type.
646  *
647  * @param type   The type.
648  */
649 static void intern_print_type_pre(const type_t *const type)
650 {
651         switch(type->kind) {
652         case TYPE_ERROR:
653                 fputs("<error>", out);
654                 return;
655         case TYPE_INVALID:
656                 fputs("<invalid>", out);
657                 return;
658         case TYPE_ENUM:
659                 print_type_enum(&type->enumt);
660                 return;
661         case TYPE_ATOMIC:
662                 print_atomic_type(&type->atomic);
663                 return;
664         case TYPE_COMPLEX:
665                 print_complex_type(&type->complex);
666                 return;
667         case TYPE_IMAGINARY:
668                 print_imaginary_type(&type->imaginary);
669                 return;
670         case TYPE_COMPOUND_STRUCT:
671         case TYPE_COMPOUND_UNION:
672                 print_compound_type(&type->compound);
673                 return;
674         case TYPE_BUILTIN:
675                 fputs(type->builtin.symbol->string, out);
676                 return;
677         case TYPE_FUNCTION:
678                 print_function_type_pre(&type->function);
679                 return;
680         case TYPE_POINTER:
681                 print_pointer_type_pre(&type->pointer);
682                 return;
683         case TYPE_REFERENCE:
684                 print_reference_type_pre(&type->reference);
685                 return;
686         case TYPE_BITFIELD:
687                 intern_print_type_pre(type->bitfield.base_type);
688                 return;
689         case TYPE_ARRAY:
690                 print_array_type_pre(&type->array);
691                 return;
692         case TYPE_TYPEDEF:
693                 print_typedef_type_pre(&type->typedeft);
694                 return;
695         case TYPE_TYPEOF:
696                 print_typeof_type_pre(&type->typeoft);
697                 return;
698         }
699         fputs("unknown", out);
700 }
701
702 /**
703  * Prints the postfix part of a type.
704  *
705  * @param type   The type.
706  */
707 static void intern_print_type_post(const type_t *const type)
708 {
709         switch(type->kind) {
710         case TYPE_FUNCTION:
711                 print_function_type_post(&type->function, NULL);
712                 return;
713         case TYPE_POINTER:
714                 print_pointer_type_post(&type->pointer);
715                 return;
716         case TYPE_REFERENCE:
717                 print_reference_type_post(&type->reference);
718                 return;
719         case TYPE_ARRAY:
720                 print_array_type_post(&type->array);
721                 return;
722         case TYPE_BITFIELD:
723                 print_bitfield_type_post(&type->bitfield);
724                 return;
725         case TYPE_ERROR:
726         case TYPE_INVALID:
727         case TYPE_ATOMIC:
728         case TYPE_COMPLEX:
729         case TYPE_IMAGINARY:
730         case TYPE_ENUM:
731         case TYPE_COMPOUND_STRUCT:
732         case TYPE_COMPOUND_UNION:
733         case TYPE_BUILTIN:
734         case TYPE_TYPEOF:
735         case TYPE_TYPEDEF:
736                 break;
737         }
738 }
739
740 /**
741  * Prints a type.
742  *
743  * @param type   The type.
744  */
745 void print_type(const type_t *const type)
746 {
747         print_type_ext(type, NULL, NULL);
748 }
749
750 void print_type_ext(const type_t *const type, const symbol_t *symbol,
751                     const scope_t *parameters)
752 {
753         if (type == NULL) {
754                 fputs("nil type", out);
755                 return;
756         }
757
758         intern_print_type_pre(type);
759         if (symbol != NULL) {
760                 fputc(' ', out);
761                 fputs(symbol->string, out);
762         }
763         if (type->kind == TYPE_FUNCTION) {
764                 print_function_type_post(&type->function, parameters);
765         } else {
766                 intern_print_type_post(type);
767         }
768 }
769
770 /**
771  * Return the size of a type AST node.
772  *
773  * @param type  The type.
774  */
775 static size_t get_type_struct_size(const type_t *type)
776 {
777         switch(type->kind) {
778         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
779         case TYPE_COMPLEX:         return sizeof(complex_type_t);
780         case TYPE_IMAGINARY:       return sizeof(imaginary_type_t);
781         case TYPE_COMPOUND_STRUCT:
782         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
783         case TYPE_ENUM:            return sizeof(enum_type_t);
784         case TYPE_FUNCTION:        return sizeof(function_type_t);
785         case TYPE_POINTER:         return sizeof(pointer_type_t);
786         case TYPE_REFERENCE:       return sizeof(reference_type_t);
787         case TYPE_ARRAY:           return sizeof(array_type_t);
788         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
789         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
790         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
791         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
792         case TYPE_ERROR:           panic("error type found");
793         case TYPE_INVALID:         panic("invalid type found");
794         }
795         panic("unknown type found");
796 }
797
798 /**
799  * Duplicates a type.
800  *
801  * @param type  The type to copy.
802  * @return A copy of the type.
803  *
804  * @note This does not produce a deep copy!
805  */
806 type_t *duplicate_type(const type_t *type)
807 {
808         size_t size = get_type_struct_size(type);
809
810         type_t *copy = obstack_alloc(type_obst, size);
811         memcpy(copy, type, size);
812         copy->base.firm_type = NULL;
813
814         return copy;
815 }
816
817 /**
818  * Returns the unqualified type of a given type.
819  *
820  * @param type  The type.
821  * @returns The unqualified type.
822  */
823 type_t *get_unqualified_type(type_t *type)
824 {
825         assert(!is_typeref(type));
826
827         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
828                 return type;
829
830         type_t *unqualified_type          = duplicate_type(type);
831         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
832
833         return identify_new_type(unqualified_type);
834 }
835
836 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
837 {
838         type_t *type = skip_typeref(orig_type);
839
840         type_t *copy;
841         if (is_type_array(type)) {
842                 /* For array types the element type has to be adjusted */
843                 type_t *element_type      = type->array.element_type;
844                 type_t *qual_element_type = get_qualified_type(element_type, qual);
845
846                 if (qual_element_type == element_type)
847                         return orig_type;
848
849                 copy                     = duplicate_type(type);
850                 copy->array.element_type = qual_element_type;
851         } else if (is_type_valid(type)) {
852                 if ((type->base.qualifiers & qual) == qual)
853                         return orig_type;
854
855                 copy                   = duplicate_type(type);
856                 copy->base.qualifiers |= qual;
857         } else {
858                 return type;
859         }
860
861         return identify_new_type(copy);
862 }
863
864 /**
865  * Check if a type is valid.
866  *
867  * @param type  The type to check.
868  * @return true if type represents a valid type.
869  */
870 bool type_valid(const type_t *type)
871 {
872         return type->kind != TYPE_INVALID;
873 }
874
875 static bool test_atomic_type_flag(atomic_type_kind_t kind,
876                                   atomic_type_flag_t flag)
877 {
878         assert(kind <= ATOMIC_TYPE_LAST);
879         return (atomic_type_properties[kind].flags & flag) != 0;
880 }
881
882 /**
883  * Returns true if the given type is an integer type.
884  *
885  * @param type  The type to check.
886  * @return True if type is an integer type.
887  */
888 bool is_type_integer(const type_t *type)
889 {
890         assert(!is_typeref(type));
891
892         if (type->kind == TYPE_ENUM)
893                 return true;
894         if (type->kind == TYPE_BITFIELD)
895                 return true;
896
897         if (type->kind != TYPE_ATOMIC)
898                 return false;
899
900         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
901 }
902
903 /**
904  * Returns true if the given type is an enum type.
905  *
906  * @param type  The type to check.
907  * @return True if type is an enum type.
908  */
909 bool is_type_enum(const type_t *type)
910 {
911         assert(!is_typeref(type));
912         return type->kind == TYPE_ENUM;
913 }
914
915 /**
916  * Returns true if the given type is an floating point type.
917  *
918  * @param type  The type to check.
919  * @return True if type is a floating point type.
920  */
921 bool is_type_float(const type_t *type)
922 {
923         assert(!is_typeref(type));
924
925         if (type->kind != TYPE_ATOMIC)
926                 return false;
927
928         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
929 }
930
931 /**
932  * Returns true if the given type is an complex type.
933  *
934  * @param type  The type to check.
935  * @return True if type is a complex type.
936  */
937 bool is_type_complex(const type_t *type)
938 {
939         assert(!is_typeref(type));
940
941         if (type->kind != TYPE_ATOMIC)
942                 return false;
943
944         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
945 }
946
947 /**
948  * Returns true if the given type is a signed type.
949  *
950  * @param type  The type to check.
951  * @return True if type is a signed type.
952  */
953 bool is_type_signed(const type_t *type)
954 {
955         assert(!is_typeref(type));
956
957         /* enum types are int for now */
958         if (type->kind == TYPE_ENUM)
959                 return true;
960         if (type->kind == TYPE_BITFIELD)
961                 return is_type_signed(type->bitfield.base_type);
962
963         if (type->kind != TYPE_ATOMIC)
964                 return false;
965
966         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
967 }
968
969 /**
970  * Returns true if the given type represents an arithmetic type.
971  *
972  * @param type  The type to check.
973  * @return True if type represents an arithmetic type.
974  */
975 bool is_type_arithmetic(const type_t *type)
976 {
977         assert(!is_typeref(type));
978
979         switch(type->kind) {
980         case TYPE_BITFIELD:
981         case TYPE_ENUM:
982                 return true;
983         case TYPE_ATOMIC:
984                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
985         case TYPE_COMPLEX:
986                 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
987         case TYPE_IMAGINARY:
988                 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
989         default:
990                 return false;
991         }
992 }
993
994 /**
995  * Returns true if the given type is an integer or float type.
996  *
997  * @param type  The type to check.
998  * @return True if type is an integer or float type.
999  */
1000 bool is_type_real(const type_t *type)
1001 {
1002         /* 6.2.5 (17) */
1003         return is_type_integer(type) || is_type_float(type);
1004 }
1005
1006 /**
1007  * Returns true if the given type represents a scalar type.
1008  *
1009  * @param type  The type to check.
1010  * @return True if type represents a scalar type.
1011  */
1012 bool is_type_scalar(const type_t *type)
1013 {
1014         assert(!is_typeref(type));
1015
1016         switch (type->kind) {
1017                 case TYPE_POINTER: return true;
1018                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1019                 default:           break;
1020         }
1021
1022         return is_type_arithmetic(type);
1023 }
1024
1025 /**
1026  * Check if a given type is incomplete.
1027  *
1028  * @param type  The type to check.
1029  * @return True if the given type is incomplete (ie. just forward).
1030  */
1031 bool is_type_incomplete(const type_t *type)
1032 {
1033         assert(!is_typeref(type));
1034
1035         switch(type->kind) {
1036         case TYPE_COMPOUND_STRUCT:
1037         case TYPE_COMPOUND_UNION: {
1038                 const compound_type_t *compound_type = &type->compound;
1039                 return !compound_type->compound->complete;
1040         }
1041         case TYPE_ENUM:
1042                 return false;
1043
1044         case TYPE_ARRAY:
1045                 return type->array.size_expression == NULL
1046                         && !type->array.size_constant;
1047
1048         case TYPE_ATOMIC:
1049                 return type->atomic.akind == ATOMIC_TYPE_VOID;
1050
1051         case TYPE_COMPLEX:
1052                 return type->complex.akind == ATOMIC_TYPE_VOID;
1053
1054         case TYPE_IMAGINARY:
1055                 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1056
1057         case TYPE_BITFIELD:
1058         case TYPE_FUNCTION:
1059         case TYPE_POINTER:
1060         case TYPE_REFERENCE:
1061         case TYPE_BUILTIN:
1062         case TYPE_ERROR:
1063                 return false;
1064
1065         case TYPE_TYPEDEF:
1066         case TYPE_TYPEOF:
1067                 panic("is_type_incomplete called without typerefs skipped");
1068         case TYPE_INVALID:
1069                 break;
1070         }
1071
1072         panic("invalid type found");
1073 }
1074
1075 bool is_type_object(const type_t *type)
1076 {
1077         return !is_type_function(type) && !is_type_incomplete(type);
1078 }
1079
1080 bool is_builtin_va_list(type_t *type)
1081 {
1082         type_t *tp = skip_typeref(type);
1083
1084         return tp->kind == type_valist->kind &&
1085                tp->builtin.symbol == type_valist->builtin.symbol;
1086 }
1087
1088 /**
1089  * Check if two function types are compatible.
1090  */
1091 static bool function_types_compatible(const function_type_t *func1,
1092                                       const function_type_t *func2)
1093 {
1094         const type_t* const ret1 = skip_typeref(func1->return_type);
1095         const type_t* const ret2 = skip_typeref(func2->return_type);
1096         if (!types_compatible(ret1, ret2))
1097                 return false;
1098
1099         if (func1->linkage != func2->linkage)
1100                 return false;
1101
1102         cc_kind_t cc1 = func1->calling_convention;
1103         if (cc1 == CC_DEFAULT)
1104                 cc1 = default_calling_convention;
1105         cc_kind_t cc2 = func2->calling_convention;
1106         if (cc2 == CC_DEFAULT)
1107                 cc2 = default_calling_convention;
1108
1109         if (cc1 != cc2)
1110                 return false;
1111
1112         /* can parameters be compared? */
1113         if (func1->unspecified_parameters || func2->unspecified_parameters)
1114                 return true;
1115
1116         if (func1->variadic != func2->variadic)
1117                 return false;
1118
1119         /* TODO: handling of unspecified parameters not correct yet */
1120
1121         /* all argument types must be compatible */
1122         function_parameter_t *parameter1 = func1->parameters;
1123         function_parameter_t *parameter2 = func2->parameters;
1124         for ( ; parameter1 != NULL && parameter2 != NULL;
1125                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
1126                 type_t *parameter1_type = skip_typeref(parameter1->type);
1127                 type_t *parameter2_type = skip_typeref(parameter2->type);
1128
1129                 parameter1_type = get_unqualified_type(parameter1_type);
1130                 parameter2_type = get_unqualified_type(parameter2_type);
1131
1132                 if (!types_compatible(parameter1_type, parameter2_type))
1133                         return false;
1134         }
1135         /* same number of arguments? */
1136         if (parameter1 != NULL || parameter2 != NULL)
1137                 return false;
1138
1139         return true;
1140 }
1141
1142 /**
1143  * Check if two array types are compatible.
1144  */
1145 static bool array_types_compatible(const array_type_t *array1,
1146                                    const array_type_t *array2)
1147 {
1148         type_t *element_type1 = skip_typeref(array1->element_type);
1149         type_t *element_type2 = skip_typeref(array2->element_type);
1150         if (!types_compatible(element_type1, element_type2))
1151                 return false;
1152
1153         if (!array1->size_constant || !array2->size_constant)
1154                 return true;
1155
1156         return array1->size == array2->size;
1157 }
1158
1159 /**
1160  * Check if two types are compatible.
1161  */
1162 bool types_compatible(const type_t *type1, const type_t *type2)
1163 {
1164         assert(!is_typeref(type1));
1165         assert(!is_typeref(type2));
1166
1167         /* shortcut: the same type is always compatible */
1168         if (type1 == type2)
1169                 return true;
1170
1171         if (!is_type_valid(type1) || !is_type_valid(type2))
1172                 return true;
1173
1174         if (type1->base.qualifiers != type2->base.qualifiers)
1175                 return false;
1176         if (type1->kind != type2->kind)
1177                 return false;
1178
1179         switch (type1->kind) {
1180         case TYPE_FUNCTION:
1181                 return function_types_compatible(&type1->function, &type2->function);
1182         case TYPE_ATOMIC:
1183                 return type1->atomic.akind == type2->atomic.akind;
1184         case TYPE_COMPLEX:
1185                 return type1->complex.akind == type2->complex.akind;
1186         case TYPE_IMAGINARY:
1187                 return type1->imaginary.akind == type2->imaginary.akind;
1188         case TYPE_ARRAY:
1189                 return array_types_compatible(&type1->array, &type2->array);
1190
1191         case TYPE_POINTER: {
1192                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1193                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1194                 return types_compatible(to1, to2);
1195         }
1196
1197         case TYPE_REFERENCE: {
1198                 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1199                 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1200                 return types_compatible(to1, to2);
1201         }
1202
1203         case TYPE_COMPOUND_STRUCT:
1204         case TYPE_COMPOUND_UNION: {
1205
1206
1207                 break;
1208         }
1209         case TYPE_ENUM:
1210         case TYPE_BUILTIN:
1211                 /* TODO: not implemented */
1212                 break;
1213
1214         case TYPE_BITFIELD:
1215                 /* not sure if this makes sense or is even needed, implement it if you
1216                  * really need it! */
1217                 panic("type compatibility check for bitfield type");
1218
1219         case TYPE_ERROR:
1220                 /* Hmm, the error type should be compatible to all other types */
1221                 return true;
1222         case TYPE_INVALID:
1223                 panic("invalid type found in compatible types");
1224         case TYPE_TYPEDEF:
1225         case TYPE_TYPEOF:
1226                 panic("typerefs not skipped in compatible types?!?");
1227         }
1228
1229         /* TODO: incomplete */
1230         return false;
1231 }
1232
1233 /**
1234  * Skip all typerefs and return the underlying type.
1235  */
1236 type_t *skip_typeref(type_t *type)
1237 {
1238         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1239
1240         while (true) {
1241                 switch (type->kind) {
1242                 case TYPE_ERROR:
1243                         return type;
1244                 case TYPE_TYPEDEF: {
1245                         qualifiers |= type->base.qualifiers;
1246
1247                         const typedef_type_t *typedef_type = &type->typedeft;
1248                         if (typedef_type->resolved_type != NULL) {
1249                                 type = typedef_type->resolved_type;
1250                                 break;
1251                         }
1252                         type = typedef_type->typedefe->type;
1253                         continue;
1254                 }
1255                 case TYPE_TYPEOF:
1256                         qualifiers |= type->base.qualifiers;
1257                         type        = type->typeoft.typeof_type;
1258                         continue;
1259                 default:
1260                         break;
1261                 }
1262                 break;
1263         }
1264
1265         if (qualifiers != TYPE_QUALIFIER_NONE) {
1266                 type_t *const copy = duplicate_type(type);
1267
1268                 /* for const with typedefed array type the element type has to be
1269                  * adjusted */
1270                 if (is_type_array(copy)) {
1271                         type_t *element_type           = copy->array.element_type;
1272                         element_type                   = duplicate_type(element_type);
1273                         element_type->base.qualifiers |= qualifiers;
1274                         copy->array.element_type       = element_type;
1275                 } else {
1276                         copy->base.qualifiers |= qualifiers;
1277                 }
1278
1279                 type = identify_new_type(copy);
1280         }
1281
1282         return type;
1283 }
1284
1285 unsigned get_type_size(type_t *type)
1286 {
1287         switch (type->kind) {
1288         case TYPE_INVALID:
1289                 break;
1290         case TYPE_ERROR:
1291                 return 0;
1292         case TYPE_ATOMIC:
1293                 return get_atomic_type_size(type->atomic.akind);
1294         case TYPE_COMPLEX:
1295                 return get_atomic_type_size(type->complex.akind) * 2;
1296         case TYPE_IMAGINARY:
1297                 return get_atomic_type_size(type->imaginary.akind);
1298         case TYPE_COMPOUND_UNION:
1299                 layout_union_type(&type->compound);
1300                 return type->compound.compound->size;
1301         case TYPE_COMPOUND_STRUCT:
1302                 layout_struct_type(&type->compound);
1303                 return type->compound.compound->size;
1304         case TYPE_ENUM:
1305                 return get_atomic_type_size(type->enumt.akind);
1306         case TYPE_FUNCTION:
1307                 return 0; /* non-const (but "address-const") */
1308         case TYPE_REFERENCE:
1309         case TYPE_POINTER:
1310                 /* TODO: make configurable by backend */
1311                 return 4;
1312         case TYPE_ARRAY: {
1313                 /* TODO: correct if element_type is aligned? */
1314                 il_size_t element_size = get_type_size(type->array.element_type);
1315                 return type->array.size * element_size;
1316         }
1317         case TYPE_BITFIELD:
1318                 return 0;
1319         case TYPE_BUILTIN:
1320                 return get_type_size(type->builtin.real_type);
1321         case TYPE_TYPEDEF:
1322                 return get_type_size(type->typedeft.typedefe->type);
1323         case TYPE_TYPEOF:
1324                 if (type->typeoft.typeof_type) {
1325                         return get_type_size(type->typeoft.typeof_type);
1326                 } else {
1327                         return get_type_size(type->typeoft.expression->base.type);
1328                 }
1329         }
1330         panic("invalid type in get_type_size");
1331 }
1332
1333 unsigned get_type_alignment(type_t *type)
1334 {
1335         switch (type->kind) {
1336         case TYPE_INVALID:
1337                 break;
1338         case TYPE_ERROR:
1339                 return 0;
1340         case TYPE_ATOMIC:
1341                 return get_atomic_type_alignment(type->atomic.akind);
1342         case TYPE_COMPLEX:
1343                 return get_atomic_type_alignment(type->complex.akind);
1344         case TYPE_IMAGINARY:
1345                 return get_atomic_type_alignment(type->imaginary.akind);
1346         case TYPE_COMPOUND_UNION:
1347                 layout_union_type(&type->compound);
1348                 return type->compound.compound->alignment;
1349         case TYPE_COMPOUND_STRUCT:
1350                 layout_struct_type(&type->compound);
1351                 return type->compound.compound->alignment;
1352         case TYPE_ENUM:
1353                 return get_atomic_type_alignment(type->enumt.akind);
1354         case TYPE_FUNCTION:
1355                 /* what is correct here? */
1356                 return 4;
1357         case TYPE_REFERENCE:
1358         case TYPE_POINTER:
1359                 /* TODO: make configurable by backend */
1360                 return 4;
1361         case TYPE_ARRAY:
1362                 return get_type_alignment(type->array.element_type);
1363         case TYPE_BITFIELD:
1364                 return 0;
1365         case TYPE_BUILTIN:
1366                 return get_type_alignment(type->builtin.real_type);
1367         case TYPE_TYPEDEF: {
1368                 il_alignment_t alignment
1369                         = get_type_alignment(type->typedeft.typedefe->type);
1370                 if (type->typedeft.typedefe->alignment > alignment)
1371                         alignment = type->typedeft.typedefe->alignment;
1372
1373                 return alignment;
1374         }
1375         case TYPE_TYPEOF:
1376                 if (type->typeoft.typeof_type) {
1377                         return get_type_alignment(type->typeoft.typeof_type);
1378                 } else {
1379                         return get_type_alignment(type->typeoft.expression->base.type);
1380                 }
1381         }
1382         panic("invalid type in get_type_alignment");
1383 }
1384
1385 decl_modifiers_t get_type_modifiers(const type_t *type)
1386 {
1387         switch(type->kind) {
1388         case TYPE_INVALID:
1389         case TYPE_ERROR:
1390                 break;
1391         case TYPE_COMPOUND_STRUCT:
1392         case TYPE_COMPOUND_UNION:
1393                 return type->compound.compound->modifiers;
1394         case TYPE_FUNCTION:
1395                 return type->function.modifiers;
1396         case TYPE_ENUM:
1397         case TYPE_ATOMIC:
1398         case TYPE_COMPLEX:
1399         case TYPE_IMAGINARY:
1400         case TYPE_REFERENCE:
1401         case TYPE_POINTER:
1402         case TYPE_BITFIELD:
1403         case TYPE_ARRAY:
1404                 return 0;
1405         case TYPE_BUILTIN:
1406                 return get_type_modifiers(type->builtin.real_type);
1407         case TYPE_TYPEDEF: {
1408                 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1409                 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1410                 return modifiers;
1411         }
1412         case TYPE_TYPEOF:
1413                 if (type->typeoft.typeof_type) {
1414                         return get_type_modifiers(type->typeoft.typeof_type);
1415                 } else {
1416                         return get_type_modifiers(type->typeoft.expression->base.type);
1417                 }
1418         }
1419         panic("invalid type found in get_type_modifiers");
1420 }
1421
1422 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1423 {
1424         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1425
1426         while (true) {
1427                 switch (type->base.kind) {
1428                 case TYPE_ERROR:
1429                         return TYPE_QUALIFIER_NONE;
1430                 case TYPE_TYPEDEF:
1431                         qualifiers |= type->base.qualifiers;
1432                         const typedef_type_t *typedef_type = &type->typedeft;
1433                         if (typedef_type->resolved_type != NULL)
1434                                 type = typedef_type->resolved_type;
1435                         else
1436                                 type = typedef_type->typedefe->type;
1437                         continue;
1438                 case TYPE_TYPEOF:
1439                         type = type->typeoft.typeof_type;
1440                         continue;
1441                 case TYPE_ARRAY:
1442                         if (skip_array_type) {
1443                                 type = type->array.element_type;
1444                                 continue;
1445                         }
1446                         break;
1447                 default:
1448                         break;
1449                 }
1450                 break;
1451         }
1452         return type->base.qualifiers | qualifiers;
1453 }
1454
1455 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1456 {
1457         assert(kind <= ATOMIC_TYPE_LAST);
1458         return atomic_type_properties[kind].size;
1459 }
1460
1461 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1462 {
1463         assert(kind <= ATOMIC_TYPE_LAST);
1464         return atomic_type_properties[kind].alignment;
1465 }
1466
1467 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1468 {
1469         assert(kind <= ATOMIC_TYPE_LAST);
1470         return atomic_type_properties[kind].flags;
1471 }
1472
1473 atomic_type_kind_t get_intptr_kind(void)
1474 {
1475         if (machine_size <= 32)
1476                 return ATOMIC_TYPE_INT;
1477         else if (machine_size <= 64)
1478                 return ATOMIC_TYPE_LONG;
1479         else
1480                 return ATOMIC_TYPE_LONGLONG;
1481 }
1482
1483 atomic_type_kind_t get_uintptr_kind(void)
1484 {
1485         if (machine_size <= 32)
1486                 return ATOMIC_TYPE_UINT;
1487         else if (machine_size <= 64)
1488                 return ATOMIC_TYPE_ULONG;
1489         else
1490                 return ATOMIC_TYPE_ULONGLONG;
1491 }
1492
1493 /**
1494  * Find the atomic type kind representing a given size (signed).
1495  */
1496 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1497 {
1498         static atomic_type_kind_t kinds[32];
1499
1500         assert(size < 32);
1501         atomic_type_kind_t kind = kinds[size];
1502         if (kind == ATOMIC_TYPE_INVALID) {
1503                 static const atomic_type_kind_t possible_kinds[] = {
1504                         ATOMIC_TYPE_SCHAR,
1505                         ATOMIC_TYPE_SHORT,
1506                         ATOMIC_TYPE_INT,
1507                         ATOMIC_TYPE_LONG,
1508                         ATOMIC_TYPE_LONGLONG
1509                 };
1510                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1511                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1512                                 kind = possible_kinds[i];
1513                                 break;
1514                         }
1515                 }
1516                 kinds[size] = kind;
1517         }
1518         return kind;
1519 }
1520
1521 /**
1522  * Find the atomic type kind representing a given size (signed).
1523  */
1524 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1525 {
1526         static atomic_type_kind_t kinds[32];
1527
1528         assert(size < 32);
1529         atomic_type_kind_t kind = kinds[size];
1530         if (kind == ATOMIC_TYPE_INVALID) {
1531                 static const atomic_type_kind_t possible_kinds[] = {
1532                         ATOMIC_TYPE_UCHAR,
1533                         ATOMIC_TYPE_USHORT,
1534                         ATOMIC_TYPE_UINT,
1535                         ATOMIC_TYPE_ULONG,
1536                         ATOMIC_TYPE_ULONGLONG
1537                 };
1538                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1539                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1540                                 kind = possible_kinds[i];
1541                                 break;
1542                         }
1543                 }
1544                 kinds[size] = kind;
1545         }
1546         return kind;
1547 }
1548
1549 /**
1550  * Hash the given type and return the "singleton" version
1551  * of it.
1552  */
1553 type_t *identify_new_type(type_t *type)
1554 {
1555         type_t *result = typehash_insert(type);
1556         if (result != type) {
1557                 obstack_free(type_obst, type);
1558         }
1559         return result;
1560 }
1561
1562 /**
1563  * Creates a new atomic type.
1564  *
1565  * @param akind       The kind of the atomic type.
1566  * @param qualifiers  Type qualifiers for the new type.
1567  */
1568 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1569 {
1570         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1571         memset(type, 0, sizeof(atomic_type_t));
1572
1573         type->kind            = TYPE_ATOMIC;
1574         type->base.qualifiers = qualifiers;
1575         type->atomic.akind    = akind;
1576
1577         return identify_new_type(type);
1578 }
1579
1580 /**
1581  * Creates a new complex type.
1582  *
1583  * @param akind       The kind of the atomic type.
1584  * @param qualifiers  Type qualifiers for the new type.
1585  */
1586 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1587 {
1588         type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1589         memset(type, 0, sizeof(complex_type_t));
1590
1591         type->kind            = TYPE_COMPLEX;
1592         type->base.qualifiers = qualifiers;
1593         type->complex.akind   = akind;
1594
1595         return identify_new_type(type);
1596 }
1597
1598 /**
1599  * Creates a new imaginary type.
1600  *
1601  * @param akind       The kind of the atomic type.
1602  * @param qualifiers  Type qualifiers for the new type.
1603  */
1604 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1605 {
1606         type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1607         memset(type, 0, sizeof(imaginary_type_t));
1608
1609         type->kind            = TYPE_IMAGINARY;
1610         type->base.qualifiers = qualifiers;
1611         type->imaginary.akind = akind;
1612
1613         return identify_new_type(type);
1614 }
1615
1616 /**
1617  * Creates a new pointer type.
1618  *
1619  * @param points_to   The points-to type for the new type.
1620  * @param qualifiers  Type qualifiers for the new type.
1621  */
1622 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1623 {
1624         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1625         memset(type, 0, sizeof(pointer_type_t));
1626
1627         type->kind                  = TYPE_POINTER;
1628         type->base.qualifiers       = qualifiers;
1629         type->pointer.points_to     = points_to;
1630         type->pointer.base_variable = NULL;
1631
1632         return identify_new_type(type);
1633 }
1634
1635 /**
1636  * Creates a new reference type.
1637  *
1638  * @param refers_to   The referred-to type for the new type.
1639  */
1640 type_t *make_reference_type(type_t *refers_to)
1641 {
1642         type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1643         memset(type, 0, sizeof(reference_type_t));
1644
1645         type->kind                = TYPE_REFERENCE;
1646         type->base.qualifiers     = 0;
1647         type->reference.refers_to = refers_to;
1648
1649         return identify_new_type(type);
1650 }
1651
1652 /**
1653  * Creates a new based pointer type.
1654  *
1655  * @param points_to   The points-to type for the new type.
1656  * @param qualifiers  Type qualifiers for the new type.
1657  * @param variable    The based variable
1658  */
1659 type_t *make_based_pointer_type(type_t *points_to,
1660                                                                 type_qualifiers_t qualifiers, variable_t *variable)
1661 {
1662         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1663         memset(type, 0, sizeof(pointer_type_t));
1664
1665         type->kind                  = TYPE_POINTER;
1666         type->base.qualifiers       = qualifiers;
1667         type->pointer.points_to     = points_to;
1668         type->pointer.base_variable = variable;
1669
1670         return identify_new_type(type);
1671 }
1672
1673
1674 type_t *make_array_type(type_t *element_type, size_t size,
1675                         type_qualifiers_t qualifiers)
1676 {
1677         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1678         memset(type, 0, sizeof(array_type_t));
1679
1680         type->kind                = TYPE_ARRAY;
1681         type->base.qualifiers     = qualifiers;
1682         type->array.element_type  = element_type;
1683         type->array.size          = size;
1684         type->array.size_constant = true;
1685
1686         return identify_new_type(type);
1687 }
1688
1689 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1690                                        il_alignment_t *struct_alignment,
1691                                                                            bool packed, entity_t *first)
1692 {
1693         il_size_t      offset     = *struct_offset;
1694         il_alignment_t alignment  = *struct_alignment;
1695         size_t         bit_offset = 0;
1696
1697         entity_t *member;
1698         for (member = first; member != NULL; member = member->base.next) {
1699                 if (member->kind != ENTITY_COMPOUND_MEMBER)
1700                         break;
1701
1702                 type_t *type = member->declaration.type;
1703                 if (type->kind != TYPE_BITFIELD)
1704                         break;
1705
1706                 type_t *base_type = skip_typeref(type->bitfield.base_type);
1707                 il_alignment_t base_alignment = get_type_alignment(base_type);
1708                 il_alignment_t alignment_mask = base_alignment-1;
1709                 if (base_alignment > alignment)
1710                         alignment = base_alignment;
1711
1712                 size_t bit_size = type->bitfield.bit_size;
1713                 if (!packed) {
1714                         bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1715                         offset     &= ~alignment_mask;
1716                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1717
1718                         if (bit_offset + bit_size > base_size || bit_size == 0) {
1719                                 offset    += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1720                                 offset     = (offset + base_alignment-1) & ~alignment_mask;
1721                                 bit_offset = 0;
1722                         }
1723                 }
1724
1725                 member->compound_member.offset     = offset;
1726                 member->compound_member.bit_offset = bit_offset;
1727
1728                 bit_offset += bit_size;
1729                 offset     += bit_offset / BITS_PER_BYTE;
1730                 bit_offset %= BITS_PER_BYTE;
1731         }
1732
1733         if (bit_offset > 0)
1734                 offset += 1;
1735
1736         *struct_offset    = offset;
1737         *struct_alignment = alignment;
1738
1739         return member;
1740 }
1741
1742 /**
1743  * Finish the construction of a struct type by calculating its size, offsets,
1744  * alignment.
1745  */
1746 void layout_struct_type(compound_type_t *type)
1747 {
1748         assert(type->compound != NULL);
1749
1750         compound_t *compound = type->compound;
1751         if (!compound->complete)
1752                 return;
1753         if (type->compound->layouted)
1754                 return;
1755
1756         il_size_t      offset    = 0;
1757         il_alignment_t alignment = compound->alignment;
1758         bool           need_pad  = false;
1759
1760         entity_t *entry = compound->members.entities;
1761         while (entry != NULL) {
1762                 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1763                         entry = entry->base.next;
1764                         continue;
1765                 }
1766
1767                 type_t *m_type  = entry->declaration.type;
1768                 type_t *skipped = skip_typeref(m_type);
1769                 if (! is_type_valid(skipped)) {
1770                         entry = entry->base.next;
1771                         continue;
1772                 }
1773
1774                 if (skipped->kind == TYPE_BITFIELD) {
1775                         entry = pack_bitfield_members(&offset, &alignment,
1776                                                       compound->packed, entry);
1777                         continue;
1778                 }
1779
1780                 il_alignment_t m_alignment = get_type_alignment(m_type);
1781                 if (m_alignment > alignment)
1782                         alignment = m_alignment;
1783
1784                 if (!compound->packed) {
1785                         il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1786
1787                         if (new_offset > offset) {
1788                                 need_pad = true;
1789                                 offset   = new_offset;
1790                         }
1791                 }
1792
1793                 entry->compound_member.offset = offset;
1794                 offset += get_type_size(m_type);
1795
1796                 entry = entry->base.next;
1797         }
1798
1799         if (!compound->packed) {
1800                 il_size_t new_offset = (offset + alignment-1) & -alignment;
1801                 if (new_offset > offset) {
1802                         need_pad = true;
1803                         offset   = new_offset;
1804                 }
1805         }
1806
1807         if (need_pad) {
1808                 if (warning.padded) {
1809                         warningf(&compound->base.source_position, "'%T' needs padding",
1810                                  type);
1811                 }
1812         } else if (compound->packed && warning.packed) {
1813                 warningf(&compound->base.source_position,
1814                          "superfluous packed attribute on '%T'", type);
1815         }
1816
1817         compound->size      = offset;
1818         compound->alignment = alignment;
1819         compound->layouted  = true;
1820 }
1821
1822 /**
1823  * Finish the construction of an union type by calculating
1824  * its size and alignment.
1825  */
1826 void layout_union_type(compound_type_t *type)
1827 {
1828         assert(type->compound != NULL);
1829
1830         compound_t *compound = type->compound;
1831         if (! compound->complete)
1832                 return;
1833
1834         il_size_t      size      = 0;
1835         il_alignment_t alignment = compound->alignment;
1836
1837         entity_t *entry = compound->members.entities;
1838         for (; entry != NULL; entry = entry->base.next) {
1839                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1840                         continue;
1841
1842                 type_t *m_type = entry->declaration.type;
1843                 if (! is_type_valid(skip_typeref(m_type)))
1844                         continue;
1845
1846                 entry->compound_member.offset = 0;
1847                 il_size_t m_size = get_type_size(m_type);
1848                 if (m_size > size)
1849                         size = m_size;
1850                 il_alignment_t m_alignment = get_type_alignment(m_type);
1851                 if (m_alignment > alignment)
1852                         alignment = m_alignment;
1853         }
1854         size = (size + alignment - 1) & -alignment;
1855
1856         compound->size      = size;
1857         compound->alignment = alignment;
1858 }
1859
1860 /**
1861  * Debug helper. Prints the given type to stdout.
1862  */
1863 static __attribute__((unused))
1864 void dbg_type(const type_t *type)
1865 {
1866         FILE *old_out = out;
1867         out = stderr;
1868         print_type(type);
1869         puts("\n");
1870         fflush(stderr);
1871         out = old_out;
1872 }