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