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