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