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