implement support for complex types
[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         if (!is_type_arithmetic(type))
757                 return false;
758         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
759 }
760
761 bool is_type_enum(const type_t *type)
762 {
763         assert(!is_typeref(type));
764         return type->kind == TYPE_ENUM;
765 }
766
767 bool is_type_float(const type_t *type)
768 {
769         assert(!is_typeref(type));
770
771         if (type->kind != TYPE_ATOMIC)
772                 return false;
773
774         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
775 }
776
777 bool is_type_complex(const type_t *type)
778 {
779         assert(!is_typeref(type));
780         return type->kind == TYPE_COMPLEX;
781 }
782
783 bool is_type_signed(const type_t *type)
784 {
785         assert(!is_typeref(type));
786         if (!is_type_arithmetic(type))
787                 return false;
788         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
789 }
790
791 bool is_type_arithmetic(const type_t *type)
792 {
793         assert(!is_typeref(type));
794
795         switch (type->kind) {
796         case TYPE_ENUM:
797                 return true;
798         case TYPE_ATOMIC:
799         case TYPE_COMPLEX:
800         case TYPE_IMAGINARY:
801                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
802         default:
803                 return false;
804         }
805 }
806
807 bool is_type_real(const type_t *type)
808 {
809         /* 6.2.5 (17) */
810         return is_type_integer(type) || is_type_float(type);
811 }
812
813 bool is_type_scalar(const type_t *type)
814 {
815         assert(!is_typeref(type));
816
817         switch (type->kind) {
818         case TYPE_POINTER:
819         case TYPE_ENUM:
820                 return true;
821         case TYPE_ATOMIC:
822         case TYPE_COMPLEX:
823         case TYPE_IMAGINARY:
824                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
825         default:
826                 return false;
827         }
828 }
829
830 bool is_type_incomplete(const type_t *type)
831 {
832         assert(!is_typeref(type));
833
834         switch (type->kind) {
835         case TYPE_COMPOUND_STRUCT:
836         case TYPE_COMPOUND_UNION: {
837                 const compound_type_t *compound_type = &type->compound;
838                 return !compound_type->compound->complete;
839         }
840         case TYPE_ENUM:
841                 return false;
842
843         case TYPE_ARRAY:
844                 return type->array.size_expression == NULL
845                         && !type->array.size_constant;
846
847         case TYPE_ATOMIC:
848         case TYPE_IMAGINARY:
849         case TYPE_COMPLEX:
850                 return type->atomic.akind == ATOMIC_TYPE_VOID;
851
852         case TYPE_FUNCTION:
853         case TYPE_POINTER:
854         case TYPE_REFERENCE:
855         case TYPE_ERROR:
856                 return false;
857
858         case TYPE_TYPEDEF:
859         case TYPE_TYPEOF:
860                 panic("typedef not skipped");
861         }
862
863         panic("invalid type");
864 }
865
866 bool is_type_object(const type_t *type)
867 {
868         return !is_type_function(type) && !is_type_incomplete(type);
869 }
870
871 /**
872  * Check if two function types are compatible.
873  */
874 static bool function_types_compatible(const function_type_t *func1,
875                                       const function_type_t *func2)
876 {
877         const type_t* const ret1 = skip_typeref(func1->return_type);
878         const type_t* const ret2 = skip_typeref(func2->return_type);
879         if (!types_compatible(ret1, ret2))
880                 return false;
881
882         if (func1->linkage != func2->linkage)
883                 return false;
884
885         cc_kind_t cc1 = func1->calling_convention;
886         if (cc1 == CC_DEFAULT)
887                 cc1 = default_calling_convention;
888         cc_kind_t cc2 = func2->calling_convention;
889         if (cc2 == CC_DEFAULT)
890                 cc2 = default_calling_convention;
891
892         if (cc1 != cc2)
893                 return false;
894
895         if (func1->variadic != func2->variadic)
896                 return false;
897
898         /* can parameters be compared? */
899         if ((func1->unspecified_parameters && !func1->kr_style_parameters)
900                         || (func2->unspecified_parameters && !func2->kr_style_parameters))
901                 return true;
902
903         /* TODO: handling of unspecified parameters not correct yet */
904
905         /* all argument types must be compatible */
906         function_parameter_t *parameter1 = func1->parameters;
907         function_parameter_t *parameter2 = func2->parameters;
908         for ( ; parameter1 != NULL && parameter2 != NULL;
909                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
910                 type_t *parameter1_type = skip_typeref(parameter1->type);
911                 type_t *parameter2_type = skip_typeref(parameter2->type);
912
913                 parameter1_type = get_unqualified_type(parameter1_type);
914                 parameter2_type = get_unqualified_type(parameter2_type);
915
916                 if (!types_compatible(parameter1_type, parameter2_type))
917                         return false;
918         }
919         /* same number of arguments? */
920         if (parameter1 != NULL || parameter2 != NULL)
921                 return false;
922
923         return true;
924 }
925
926 /**
927  * Check if two array types are compatible.
928  */
929 static bool array_types_compatible(const array_type_t *array1,
930                                    const array_type_t *array2)
931 {
932         type_t *element_type1 = skip_typeref(array1->element_type);
933         type_t *element_type2 = skip_typeref(array2->element_type);
934         if (!types_compatible(element_type1, element_type2))
935                 return false;
936
937         if (!array1->size_constant || !array2->size_constant)
938                 return true;
939
940         return array1->size == array2->size;
941 }
942
943 bool types_compatible(const type_t *type1, const type_t *type2)
944 {
945         assert(!is_typeref(type1));
946         assert(!is_typeref(type2));
947
948         /* shortcut: the same type is always compatible */
949         if (type1 == type2)
950                 return true;
951
952         if (type1->base.qualifiers == type2->base.qualifiers &&
953             type1->kind            == type2->kind) {
954                 switch (type1->kind) {
955                 case TYPE_FUNCTION:
956                         return function_types_compatible(&type1->function, &type2->function);
957                 case TYPE_ATOMIC:
958                 case TYPE_IMAGINARY:
959                 case TYPE_COMPLEX:
960                         return type1->atomic.akind == type2->atomic.akind;
961                 case TYPE_ARRAY:
962                         return array_types_compatible(&type1->array, &type2->array);
963
964                 case TYPE_POINTER: {
965                         const type_t *const to1 = skip_typeref(type1->pointer.points_to);
966                         const type_t *const to2 = skip_typeref(type2->pointer.points_to);
967                         return types_compatible(to1, to2);
968                 }
969
970                 case TYPE_REFERENCE: {
971                         const type_t *const to1 = skip_typeref(type1->reference.refers_to);
972                         const type_t *const to2 = skip_typeref(type2->reference.refers_to);
973                         return types_compatible(to1, to2);
974                 }
975
976                 case TYPE_COMPOUND_STRUCT:
977                 case TYPE_COMPOUND_UNION:
978                         break;
979
980                 case TYPE_ENUM:
981                         /* TODO: not implemented */
982                         break;
983
984                 case TYPE_ERROR:
985                         /* Hmm, the error type should be compatible to all other types */
986                         return true;
987                 case TYPE_TYPEDEF:
988                 case TYPE_TYPEOF:
989                         panic("typeref not skipped");
990                 }
991         }
992
993         return !is_type_valid(type1) || !is_type_valid(type2);
994 }
995
996 /**
997  * Skip all typerefs and return the underlying type.
998  */
999 type_t *skip_typeref(type_t *type)
1000 {
1001         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1002
1003         while (true) {
1004                 switch (type->kind) {
1005                 case TYPE_ERROR:
1006                         return type;
1007                 case TYPE_TYPEDEF: {
1008                         qualifiers |= type->base.qualifiers;
1009
1010                         const typedef_type_t *typedef_type = &type->typedeft;
1011                         if (typedef_type->resolved_type != NULL) {
1012                                 type = typedef_type->resolved_type;
1013                                 break;
1014                         }
1015                         type = typedef_type->typedefe->type;
1016                         continue;
1017                 }
1018                 case TYPE_TYPEOF:
1019                         qualifiers |= type->base.qualifiers;
1020                         type        = type->typeoft.typeof_type;
1021                         continue;
1022                 default:
1023                         break;
1024                 }
1025                 break;
1026         }
1027
1028         if (qualifiers != TYPE_QUALIFIER_NONE) {
1029                 type_t *const copy = duplicate_type(type);
1030
1031                 /* for const with typedefed array type the element type has to be
1032                  * adjusted */
1033                 if (is_type_array(copy)) {
1034                         type_t *element_type           = copy->array.element_type;
1035                         element_type                   = duplicate_type(element_type);
1036                         element_type->base.qualifiers |= qualifiers;
1037                         copy->array.element_type       = element_type;
1038                 } else {
1039                         copy->base.qualifiers |= qualifiers;
1040                 }
1041
1042                 type = identify_new_type(copy);
1043         }
1044
1045         return type;
1046 }
1047
1048 unsigned get_type_size(type_t *type)
1049 {
1050         switch (type->kind) {
1051         case TYPE_ERROR:
1052                 return 0;
1053         case TYPE_ATOMIC:
1054         case TYPE_IMAGINARY:
1055         case TYPE_ENUM:
1056                 return get_atomic_type_size(type->atomic.akind);
1057         case TYPE_COMPLEX:
1058                 return get_atomic_type_size(type->atomic.akind) * 2;
1059         case TYPE_COMPOUND_UNION:
1060                 layout_union_type(&type->compound);
1061                 return type->compound.compound->size;
1062         case TYPE_COMPOUND_STRUCT:
1063                 layout_struct_type(&type->compound);
1064                 return type->compound.compound->size;
1065         case TYPE_FUNCTION:
1066                 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1067         case TYPE_REFERENCE:
1068         case TYPE_POINTER:
1069                 return pointer_properties.size;
1070         case TYPE_ARRAY: {
1071                 /* TODO: correct if element_type is aligned? */
1072                 il_size_t element_size = get_type_size(type->array.element_type);
1073                 return type->array.size * element_size;
1074         }
1075         case TYPE_TYPEDEF:
1076                 return get_type_size(type->typedeft.typedefe->type);
1077         case TYPE_TYPEOF:
1078                 return get_type_size(type->typeoft.typeof_type);
1079         }
1080         panic("invalid type");
1081 }
1082
1083 unsigned get_type_alignment(type_t *type)
1084 {
1085         switch (type->kind) {
1086         case TYPE_ERROR:
1087                 return 0;
1088         case TYPE_ATOMIC:
1089         case TYPE_IMAGINARY:
1090         case TYPE_COMPLEX:
1091         case TYPE_ENUM:
1092                 return get_atomic_type_alignment(type->atomic.akind);
1093         case TYPE_COMPOUND_UNION:
1094                 layout_union_type(&type->compound);
1095                 return type->compound.compound->alignment;
1096         case TYPE_COMPOUND_STRUCT:
1097                 layout_struct_type(&type->compound);
1098                 return type->compound.compound->alignment;
1099         case TYPE_FUNCTION:
1100                 /* gcc says 1 here... */
1101                 return 1;
1102         case TYPE_REFERENCE:
1103         case TYPE_POINTER:
1104                 return pointer_properties.alignment;
1105         case TYPE_ARRAY:
1106                 return get_type_alignment(type->array.element_type);
1107         case TYPE_TYPEDEF: {
1108                 il_alignment_t alignment
1109                         = get_type_alignment(type->typedeft.typedefe->type);
1110                 if (type->typedeft.typedefe->alignment > alignment)
1111                         alignment = type->typedeft.typedefe->alignment;
1112
1113                 return alignment;
1114         }
1115         case TYPE_TYPEOF:
1116                 return get_type_alignment(type->typeoft.typeof_type);
1117         }
1118         panic("invalid type");
1119 }
1120
1121 /**
1122  * get alignment of a type when used inside a compound.
1123  * Some ABIs are broken and alignment inside a compound is different from
1124  * recommended alignment of a type
1125  */
1126 static unsigned get_type_alignment_compound(type_t *const type)
1127 {
1128         assert(!is_typeref(type));
1129         if (type->kind == TYPE_ATOMIC)
1130                 return atomic_type_properties[type->atomic.akind].struct_alignment;
1131         return get_type_alignment(type);
1132 }
1133
1134 decl_modifiers_t get_type_modifiers(const type_t *type)
1135 {
1136         switch (type->kind) {
1137         case TYPE_ERROR:
1138                 break;
1139         case TYPE_COMPOUND_STRUCT:
1140         case TYPE_COMPOUND_UNION:
1141                 return type->compound.compound->modifiers;
1142         case TYPE_FUNCTION:
1143                 return type->function.modifiers;
1144         case TYPE_ENUM:
1145         case TYPE_ATOMIC:
1146         case TYPE_COMPLEX:
1147         case TYPE_IMAGINARY:
1148         case TYPE_REFERENCE:
1149         case TYPE_POINTER:
1150         case TYPE_ARRAY:
1151                 return 0;
1152         case TYPE_TYPEDEF: {
1153                 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1154                 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1155                 return modifiers;
1156         }
1157         case TYPE_TYPEOF:
1158                 return get_type_modifiers(type->typeoft.typeof_type);
1159         }
1160         panic("invalid type");
1161 }
1162
1163 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1164 {
1165         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1166
1167         while (true) {
1168                 switch (type->base.kind) {
1169                 case TYPE_ERROR:
1170                         return TYPE_QUALIFIER_NONE;
1171                 case TYPE_TYPEDEF:
1172                         qualifiers |= type->base.qualifiers;
1173                         const typedef_type_t *typedef_type = &type->typedeft;
1174                         if (typedef_type->resolved_type != NULL)
1175                                 type = typedef_type->resolved_type;
1176                         else
1177                                 type = typedef_type->typedefe->type;
1178                         continue;
1179                 case TYPE_TYPEOF:
1180                         type = type->typeoft.typeof_type;
1181                         continue;
1182                 case TYPE_ARRAY:
1183                         if (skip_array_type) {
1184                                 type = type->array.element_type;
1185                                 continue;
1186                         }
1187                         break;
1188                 default:
1189                         break;
1190                 }
1191                 break;
1192         }
1193         return type->base.qualifiers | qualifiers;
1194 }
1195
1196 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1197 {
1198         assert(kind <= ATOMIC_TYPE_LAST);
1199         return atomic_type_properties[kind].size;
1200 }
1201
1202 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1203 {
1204         assert(kind <= ATOMIC_TYPE_LAST);
1205         return atomic_type_properties[kind].alignment;
1206 }
1207
1208 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1209 {
1210         assert(kind <= ATOMIC_TYPE_LAST);
1211         return atomic_type_properties[kind].flags;
1212 }
1213
1214 /**
1215  * Find the atomic type kind representing a given size (signed).
1216  */
1217 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1218 {
1219         static atomic_type_kind_t kinds[32];
1220
1221         assert(size < 32);
1222         atomic_type_kind_t kind = kinds[size];
1223         if (kind == (atomic_type_kind_t)0) {
1224                 static const atomic_type_kind_t possible_kinds[] = {
1225                         ATOMIC_TYPE_SCHAR,
1226                         ATOMIC_TYPE_SHORT,
1227                         ATOMIC_TYPE_INT,
1228                         ATOMIC_TYPE_LONG,
1229                         ATOMIC_TYPE_LONGLONG
1230                 };
1231                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1232                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1233                                 kind = possible_kinds[i];
1234                                 break;
1235                         }
1236                 }
1237                 kinds[size] = kind;
1238         }
1239         return kind;
1240 }
1241
1242 /**
1243  * Find the atomic type kind representing a given size (signed).
1244  */
1245 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1246 {
1247         static atomic_type_kind_t kinds[32];
1248
1249         assert(size < 32);
1250         atomic_type_kind_t kind = kinds[size];
1251         if (kind == (atomic_type_kind_t)0) {
1252                 static const atomic_type_kind_t possible_kinds[] = {
1253                         ATOMIC_TYPE_UCHAR,
1254                         ATOMIC_TYPE_USHORT,
1255                         ATOMIC_TYPE_UINT,
1256                         ATOMIC_TYPE_ULONG,
1257                         ATOMIC_TYPE_ULONGLONG
1258                 };
1259                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1260                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1261                                 kind = possible_kinds[i];
1262                                 break;
1263                         }
1264                 }
1265                 kinds[size] = kind;
1266         }
1267         return kind;
1268 }
1269
1270 /**
1271  * Hash the given type and return the "singleton" version
1272  * of it.
1273  */
1274 type_t *identify_new_type(type_t *type)
1275 {
1276         type_t *result = typehash_insert(type);
1277         if (result != type) {
1278                 obstack_free(&type_obst, type);
1279         }
1280         return result;
1281 }
1282
1283 /**
1284  * Creates a new atomic type.
1285  *
1286  * @param akind       The kind of the atomic type.
1287  * @param qualifiers  Type qualifiers for the new type.
1288  */
1289 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1290 {
1291         type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1292         type->base.qualifiers = qualifiers;
1293         type->atomic.akind    = akind;
1294
1295         return identify_new_type(type);
1296 }
1297
1298 /**
1299  * Creates a new complex 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_complex_type(atomic_type_kind_t akind,
1305                           type_qualifiers_t qualifiers)
1306 {
1307         type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1308         type->base.qualifiers = qualifiers;
1309         type->atomic.akind   = akind;
1310
1311         return identify_new_type(type);
1312 }
1313
1314 /**
1315  * Creates a new imaginary type.
1316  *
1317  * @param akind       The kind of the atomic type.
1318  * @param qualifiers  Type qualifiers for the new type.
1319  */
1320 type_t *make_imaginary_type(atomic_type_kind_t akind,
1321                             type_qualifiers_t qualifiers)
1322 {
1323         type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1324         type->base.qualifiers = qualifiers;
1325         type->atomic.akind = akind;
1326
1327         return identify_new_type(type);
1328 }
1329
1330 /**
1331  * Creates a new pointer type.
1332  *
1333  * @param points_to   The points-to type for the new type.
1334  * @param qualifiers  Type qualifiers for the new type.
1335  */
1336 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1337 {
1338         type_t *const type = allocate_type_zero(TYPE_POINTER);
1339         type->base.qualifiers       = qualifiers;
1340         type->pointer.points_to     = points_to;
1341         type->pointer.base_variable = NULL;
1342
1343         return identify_new_type(type);
1344 }
1345
1346 /**
1347  * Creates a new reference type.
1348  *
1349  * @param refers_to   The referred-to type for the new type.
1350  */
1351 type_t *make_reference_type(type_t *refers_to)
1352 {
1353         type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1354         type->base.qualifiers     = TYPE_QUALIFIER_NONE;
1355         type->reference.refers_to = refers_to;
1356
1357         return identify_new_type(type);
1358 }
1359
1360 /**
1361  * Creates a new based pointer type.
1362  *
1363  * @param points_to   The points-to type for the new type.
1364  * @param qualifiers  Type qualifiers for the new type.
1365  * @param variable    The based variable
1366  */
1367 type_t *make_based_pointer_type(type_t *points_to,
1368                                                                 type_qualifiers_t qualifiers, variable_t *variable)
1369 {
1370         type_t *const type = allocate_type_zero(TYPE_POINTER);
1371         type->base.qualifiers       = qualifiers;
1372         type->pointer.points_to     = points_to;
1373         type->pointer.base_variable = variable;
1374
1375         return identify_new_type(type);
1376 }
1377
1378
1379 type_t *make_array_type(type_t *element_type, size_t size,
1380                         type_qualifiers_t qualifiers)
1381 {
1382         type_t *const type = allocate_type_zero(TYPE_ARRAY);
1383         type->base.qualifiers     = qualifiers;
1384         type->array.element_type  = element_type;
1385         type->array.size          = size;
1386         type->array.size_constant = true;
1387
1388         return identify_new_type(type);
1389 }
1390
1391 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1392                                        il_alignment_t *struct_alignment,
1393                                                                            bool packed, entity_t *first)
1394 {
1395         il_size_t      offset     = *struct_offset;
1396         il_alignment_t alignment  = *struct_alignment;
1397         size_t         bit_offset = 0;
1398
1399         entity_t *member;
1400         for (member = first; member != NULL; member = member->base.next) {
1401                 if (member->kind != ENTITY_COMPOUND_MEMBER)
1402                         continue;
1403                 if (!member->compound_member.bitfield)
1404                         break;
1405
1406                 type_t *const base_type = skip_typeref(member->declaration.type);
1407                 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1408                 il_alignment_t alignment_mask = base_alignment-1;
1409                 if (base_alignment > alignment)
1410                         alignment = base_alignment;
1411
1412                 size_t bit_size = member->compound_member.bit_size;
1413                 if (!packed) {
1414                         bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1415                         offset     &= ~alignment_mask;
1416                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1417
1418                         if (bit_offset + bit_size > base_size || bit_size == 0) {
1419                                 offset    += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1420                                 offset     = (offset + base_alignment-1) & ~alignment_mask;
1421                                 bit_offset = 0;
1422                         }
1423                 }
1424
1425                 if (byte_order_big_endian) {
1426                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1427                         member->compound_member.offset     = offset & ~alignment_mask;
1428                         member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1429                 } else {
1430                         member->compound_member.offset     = offset;
1431                         member->compound_member.bit_offset = bit_offset;
1432                 }
1433
1434                 bit_offset += bit_size;
1435                 offset     += bit_offset / BITS_PER_BYTE;
1436                 bit_offset %= BITS_PER_BYTE;
1437         }
1438
1439         if (bit_offset > 0)
1440                 offset += 1;
1441
1442         *struct_offset    = offset;
1443         *struct_alignment = alignment;
1444         return member;
1445 }
1446
1447 void layout_struct_type(compound_type_t *type)
1448 {
1449         assert(type->compound != NULL);
1450
1451         compound_t *compound = type->compound;
1452         if (!compound->complete)
1453                 return;
1454         if (type->compound->layouted)
1455                 return;
1456         compound->layouted = true;
1457
1458         il_size_t      offset    = 0;
1459         il_alignment_t alignment = compound->alignment;
1460         bool           need_pad  = false;
1461
1462         entity_t *entry = compound->members.entities;
1463         while (entry != NULL) {
1464                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1465                         goto next;
1466
1467                 type_t *const m_type = skip_typeref(entry->declaration.type);
1468                 if (!is_type_valid(m_type))
1469                         goto next;
1470
1471                 if (entry->compound_member.bitfield) {
1472                         entry = pack_bitfield_members(&offset, &alignment,
1473                                                       compound->packed, entry);
1474                         continue;
1475                 }
1476
1477                 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1478                 if (m_alignment > alignment)
1479                         alignment = m_alignment;
1480
1481                 if (!compound->packed) {
1482                         il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1483
1484                         if (new_offset > offset) {
1485                                 need_pad = true;
1486                                 offset   = new_offset;
1487                         }
1488                 }
1489
1490                 entry->compound_member.offset = offset;
1491                 offset += get_type_size(m_type);
1492
1493 next:
1494                 entry = entry->base.next;
1495         }
1496
1497         if (!compound->packed) {
1498                 il_size_t new_offset = (offset + alignment-1) & -alignment;
1499                 if (new_offset > offset) {
1500                         need_pad = true;
1501                         offset   = new_offset;
1502                 }
1503         }
1504
1505         position_t const *const pos = &compound->base.pos;
1506         if (need_pad) {
1507                 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1508         } else if (compound->packed) {
1509                 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1510         }
1511
1512         compound->size      = offset;
1513         compound->alignment = alignment;
1514 }
1515
1516 void layout_union_type(compound_type_t *type)
1517 {
1518         assert(type->compound != NULL);
1519
1520         compound_t *compound = type->compound;
1521         if (! compound->complete)
1522                 return;
1523         if (compound->layouted)
1524                 return;
1525         compound->layouted = true;
1526
1527         il_size_t      size      = 0;
1528         il_alignment_t alignment = compound->alignment;
1529
1530         entity_t *entry = compound->members.entities;
1531         for (; entry != NULL; entry = entry->base.next) {
1532                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1533                         continue;
1534
1535                 type_t *m_type = skip_typeref(entry->declaration.type);
1536                 if (! is_type_valid(skip_typeref(m_type)))
1537                         continue;
1538
1539                 entry->compound_member.offset = 0;
1540                 il_size_t m_size = get_type_size(m_type);
1541                 if (m_size > size)
1542                         size = m_size;
1543                 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1544                 if (m_alignment > alignment)
1545                         alignment = m_alignment;
1546         }
1547         size = (size + alignment - 1) & -alignment;
1548
1549         compound->size      = size;
1550         compound->alignment = alignment;
1551 }
1552
1553 function_parameter_t *allocate_parameter(type_t *const type)
1554 {
1555         function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1556         memset(param, 0, sizeof(*param));
1557         param->type = type;
1558         return param;
1559 }
1560
1561 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1562                              type_t *argument_type2, decl_modifiers_t modifiers)
1563 {
1564         function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1565         function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1566         parameter1->next = parameter2;
1567
1568         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1569         type->function.return_type = return_type;
1570         type->function.parameters  = parameter1;
1571         type->function.modifiers  |= modifiers;
1572         type->function.linkage     = LINKAGE_C;
1573
1574         return identify_new_type(type);
1575 }
1576
1577 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1578                              decl_modifiers_t modifiers)
1579 {
1580         function_parameter_t *const parameter = allocate_parameter(argument_type);
1581
1582         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1583         type->function.return_type = return_type;
1584         type->function.parameters  = parameter;
1585         type->function.modifiers  |= modifiers;
1586         type->function.linkage     = LINKAGE_C;
1587
1588         return identify_new_type(type);
1589 }
1590
1591 type_t *make_function_1_type_variadic(type_t *return_type,
1592                                       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.variadic    = true;
1601         type->function.modifiers  |= modifiers;
1602         type->function.linkage     = LINKAGE_C;
1603
1604         return identify_new_type(type);
1605 }
1606
1607 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1608 {
1609         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1610         type->function.return_type = return_type;
1611         type->function.parameters  = NULL;
1612         type->function.modifiers  |= modifiers;
1613         type->function.linkage     = LINKAGE_C;
1614
1615         return identify_new_type(type);
1616 }
1617
1618 type_t *make_function_type(type_t *return_type, int n_types,
1619                            type_t *const *argument_types,
1620                                                    decl_modifiers_t modifiers)
1621 {
1622         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1623         type->function.return_type = return_type;
1624         type->function.modifiers  |= modifiers;
1625         type->function.linkage     = LINKAGE_C;
1626
1627         function_parameter_t **anchor = &type->function.parameters;
1628         for (int i = 0; i < n_types; ++i) {
1629                 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1630                 *anchor = parameter;
1631                 anchor  = &parameter->next;
1632         }
1633
1634         return identify_new_type(type);
1635 }
1636
1637 /**
1638  * Debug helper. Prints the given type to stdout.
1639  */
1640 static __attribute__((unused))
1641 void dbg_type(const type_t *type)
1642 {
1643         print_to_file(stderr);
1644         print_type(type);
1645         print_char('\n');
1646         fflush(stderr);
1647 }