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