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