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