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