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