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