The typeof-type is always set, no need to check and use the expression's type as...
[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 /**
515  * Prints an enum definition.
516  *
517  * @param declaration  The enum's type declaration.
518  */
519 void print_enum_definition(const enum_t *enume)
520 {
521         print_string("{\n");
522
523         change_indent(1);
524
525         entity_t *entry = enume->base.next;
526         for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
527                entry = entry->base.next) {
528
529                 print_indent();
530                 print_string(entry->base.symbol->string);
531                 if (entry->enum_value.value != NULL) {
532                         print_string(" = ");
533
534                         /* skip the implicit cast */
535                         expression_t *expression = entry->enum_value.value;
536                         print_expression(expression);
537                 }
538                 print_string(",\n");
539         }
540
541         change_indent(-1);
542         print_indent();
543         print_string("}");
544 }
545
546 /**
547  * Prints an enum type.
548  *
549  * @param type  The enum type.
550  */
551 static void print_type_enum(const enum_type_t *type)
552 {
553         print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
554         print_string("enum ");
555
556         enum_t   *enume  = type->enume;
557         symbol_t *symbol = enume->base.symbol;
558         if (symbol != NULL) {
559                 print_string(symbol->string);
560         } else {
561                 print_enum_definition(enume);
562         }
563 }
564
565 /**
566  * Print the compound part of a compound type.
567  */
568 void print_compound_definition(const compound_t *compound)
569 {
570         print_string("{\n");
571         change_indent(1);
572
573         entity_t *entity = compound->members.entities;
574         for( ; entity != NULL; entity = entity->base.next) {
575                 if (entity->kind != ENTITY_COMPOUND_MEMBER)
576                         continue;
577
578                 print_indent();
579                 print_entity(entity);
580                 print_string("\n");
581         }
582
583         change_indent(-1);
584         print_indent();
585         print_string("}");
586         if (compound->modifiers & DM_TRANSPARENT_UNION) {
587                 print_string("__attribute__((__transparent_union__))");
588         }
589 }
590
591 /**
592  * Prints a compound type.
593  *
594  * @param type  The compound type.
595  */
596 static void print_compound_type(const compound_type_t *type)
597 {
598         print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
599
600         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
601                 print_string("struct ");
602         } else {
603                 assert(type->base.kind == TYPE_COMPOUND_UNION);
604                 print_string("union ");
605         }
606
607         compound_t *compound = type->compound;
608         symbol_t   *symbol   = compound->base.symbol;
609         if (symbol != NULL) {
610                 print_string(symbol->string);
611         } else {
612                 print_compound_definition(compound);
613         }
614 }
615
616 /**
617  * Prints the prefix part of a typedef type.
618  *
619  * @param type   The typedef type.
620  */
621 static void print_typedef_type_pre(const typedef_type_t *const type)
622 {
623         print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
624         print_string(type->typedefe->base.symbol->string);
625 }
626
627 /**
628  * Prints the prefix part of a typeof type.
629  *
630  * @param type   The typeof type.
631  */
632 static void print_typeof_type_pre(const typeof_type_t *const type)
633 {
634         print_string("typeof(");
635         if (type->expression != NULL) {
636                 print_expression(type->expression);
637         } else {
638                 print_type(type->typeof_type);
639         }
640         print_string(")");
641 }
642
643 /**
644  * Prints the prefix part of a type.
645  *
646  * @param type   The type.
647  */
648 static void intern_print_type_pre(const type_t *const type)
649 {
650         switch(type->kind) {
651         case TYPE_ERROR:
652                 print_string("<error>");
653                 return;
654         case TYPE_ENUM:
655                 print_type_enum(&type->enumt);
656                 return;
657         case TYPE_ATOMIC:
658                 print_atomic_type(&type->atomic);
659                 return;
660         case TYPE_COMPLEX:
661                 print_complex_type(&type->atomic);
662                 return;
663         case TYPE_IMAGINARY:
664                 print_imaginary_type(&type->atomic);
665                 return;
666         case TYPE_COMPOUND_STRUCT:
667         case TYPE_COMPOUND_UNION:
668                 print_compound_type(&type->compound);
669                 return;
670         case TYPE_FUNCTION:
671                 print_function_type_pre(&type->function);
672                 return;
673         case TYPE_POINTER:
674                 print_pointer_type_pre(&type->pointer);
675                 return;
676         case TYPE_REFERENCE:
677                 print_reference_type_pre(&type->reference);
678                 return;
679         case TYPE_ARRAY:
680                 print_array_type_pre(&type->array);
681                 return;
682         case TYPE_TYPEDEF:
683                 print_typedef_type_pre(&type->typedeft);
684                 return;
685         case TYPE_TYPEOF:
686                 print_typeof_type_pre(&type->typeoft);
687                 return;
688         }
689         print_string("unknown");
690 }
691
692 /**
693  * Prints the postfix part of a type.
694  *
695  * @param type   The type.
696  */
697 static void intern_print_type_post(const type_t *const type)
698 {
699         switch(type->kind) {
700         case TYPE_FUNCTION:
701                 print_function_type_post(&type->function, NULL);
702                 return;
703         case TYPE_POINTER:
704                 print_pointer_type_post(&type->pointer);
705                 return;
706         case TYPE_REFERENCE:
707                 print_reference_type_post(&type->reference);
708                 return;
709         case TYPE_ARRAY:
710                 print_array_type_post(&type->array);
711                 return;
712         case TYPE_ERROR:
713         case TYPE_ATOMIC:
714         case TYPE_COMPLEX:
715         case TYPE_IMAGINARY:
716         case TYPE_ENUM:
717         case TYPE_COMPOUND_STRUCT:
718         case TYPE_COMPOUND_UNION:
719         case TYPE_TYPEOF:
720         case TYPE_TYPEDEF:
721                 break;
722         }
723 }
724
725 /**
726  * Prints a type.
727  *
728  * @param type   The type.
729  */
730 void print_type(const type_t *const type)
731 {
732         print_type_ext(type, NULL, NULL);
733 }
734
735 void print_type_ext(const type_t *const type, const symbol_t *symbol,
736                     const scope_t *parameters)
737 {
738         intern_print_type_pre(type);
739         if (symbol != NULL) {
740                 print_string(" ");
741                 print_string(symbol->string);
742         }
743         if (type->kind == TYPE_FUNCTION) {
744                 print_function_type_post(&type->function, parameters);
745         } else {
746                 intern_print_type_post(type);
747         }
748 }
749
750 /**
751  * Duplicates a type.
752  *
753  * @param type  The type to copy.
754  * @return A copy of the type.
755  *
756  * @note This does not produce a deep copy!
757  */
758 type_t *duplicate_type(const type_t *type)
759 {
760         size_t size = get_type_struct_size(type->kind);
761
762         type_t *const copy = obstack_alloc(&type_obst, size);
763         memcpy(copy, type, size);
764         copy->base.firm_type = NULL;
765
766         return copy;
767 }
768
769 /**
770  * Returns the unqualified type of a given type.
771  *
772  * @param type  The type.
773  * @returns The unqualified type.
774  */
775 type_t *get_unqualified_type(type_t *type)
776 {
777         assert(!is_typeref(type));
778
779         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
780                 return type;
781
782         type_t *unqualified_type          = duplicate_type(type);
783         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
784
785         return identify_new_type(unqualified_type);
786 }
787
788 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
789 {
790         type_t *type = skip_typeref(orig_type);
791
792         type_t *copy;
793         if (is_type_array(type)) {
794                 /* For array types the element type has to be adjusted */
795                 type_t *element_type      = type->array.element_type;
796                 type_t *qual_element_type = get_qualified_type(element_type, qual);
797
798                 if (qual_element_type == element_type)
799                         return orig_type;
800
801                 copy                     = duplicate_type(type);
802                 copy->array.element_type = qual_element_type;
803         } else if (is_type_valid(type)) {
804                 if ((type->base.qualifiers & qual) == (int)qual)
805                         return orig_type;
806
807                 copy                   = duplicate_type(type);
808                 copy->base.qualifiers |= qual;
809         } else {
810                 return type;
811         }
812
813         return identify_new_type(copy);
814 }
815
816 static bool test_atomic_type_flag(atomic_type_kind_t kind,
817                                   atomic_type_flag_t flag)
818 {
819         assert(kind <= ATOMIC_TYPE_LAST);
820         return (atomic_type_properties[kind].flags & flag) != 0;
821 }
822
823 /**
824  * Returns true if the given type is an integer type.
825  *
826  * @param type  The type to check.
827  * @return True if type is an integer type.
828  */
829 bool is_type_integer(const type_t *type)
830 {
831         assert(!is_typeref(type));
832
833         if (type->kind == TYPE_ENUM)
834                 return true;
835         if (type->kind != TYPE_ATOMIC)
836                 return false;
837
838         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
839 }
840
841 /**
842  * Returns true if the given type is an enum type.
843  *
844  * @param type  The type to check.
845  * @return True if type is an enum type.
846  */
847 bool is_type_enum(const type_t *type)
848 {
849         assert(!is_typeref(type));
850         return type->kind == TYPE_ENUM;
851 }
852
853 /**
854  * Returns true if the given type is an floating point type.
855  *
856  * @param type  The type to check.
857  * @return True if type is a floating point type.
858  */
859 bool is_type_float(const type_t *type)
860 {
861         assert(!is_typeref(type));
862
863         if (type->kind != TYPE_ATOMIC)
864                 return false;
865
866         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
867 }
868
869 /**
870  * Returns true if the given type is an complex type.
871  *
872  * @param type  The type to check.
873  * @return True if type is a complex type.
874  */
875 bool is_type_complex(const type_t *type)
876 {
877         assert(!is_typeref(type));
878
879         if (type->kind != TYPE_ATOMIC)
880                 return false;
881
882         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
883 }
884
885 /**
886  * Returns true if the given type is a signed type.
887  *
888  * @param type  The type to check.
889  * @return True if type is a signed type.
890  */
891 bool is_type_signed(const type_t *type)
892 {
893         assert(!is_typeref(type));
894
895         /* enum types are int for now */
896         if (type->kind == TYPE_ENUM)
897                 return true;
898         if (type->kind != TYPE_ATOMIC)
899                 return false;
900
901         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
902 }
903
904 /**
905  * Returns true if the given type represents an arithmetic type.
906  *
907  * @param type  The type to check.
908  * @return True if type represents an arithmetic type.
909  */
910 bool is_type_arithmetic(const type_t *type)
911 {
912         assert(!is_typeref(type));
913
914         switch(type->kind) {
915         case TYPE_ENUM:
916                 return true;
917         case TYPE_ATOMIC:
918         case TYPE_COMPLEX:
919         case TYPE_IMAGINARY:
920                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
921         default:
922                 return false;
923         }
924 }
925
926 /**
927  * Returns true if the given type is an integer or float type.
928  *
929  * @param type  The type to check.
930  * @return True if type is an integer or float type.
931  */
932 bool is_type_real(const type_t *type)
933 {
934         /* 6.2.5 (17) */
935         return is_type_integer(type) || is_type_float(type);
936 }
937
938 /**
939  * Returns true if the given type represents a scalar type.
940  *
941  * @param type  The type to check.
942  * @return True if type represents a scalar type.
943  */
944 bool is_type_scalar(const type_t *type)
945 {
946         assert(!is_typeref(type));
947
948         if (type->kind == TYPE_POINTER)
949                 return true;
950
951         return is_type_arithmetic(type);
952 }
953
954 /**
955  * Check if a given type is incomplete.
956  *
957  * @param type  The type to check.
958  * @return True if the given type is incomplete (ie. just forward).
959  */
960 bool is_type_incomplete(const type_t *type)
961 {
962         assert(!is_typeref(type));
963
964         switch(type->kind) {
965         case TYPE_COMPOUND_STRUCT:
966         case TYPE_COMPOUND_UNION: {
967                 const compound_type_t *compound_type = &type->compound;
968                 return !compound_type->compound->complete;
969         }
970         case TYPE_ENUM:
971                 return false;
972
973         case TYPE_ARRAY:
974                 return type->array.size_expression == NULL
975                         && !type->array.size_constant;
976
977         case TYPE_ATOMIC:
978         case TYPE_IMAGINARY:
979         case TYPE_COMPLEX:
980                 return type->atomic.akind == ATOMIC_TYPE_VOID;
981
982         case TYPE_FUNCTION:
983         case TYPE_POINTER:
984         case TYPE_REFERENCE:
985         case TYPE_ERROR:
986                 return false;
987
988         case TYPE_TYPEDEF:
989         case TYPE_TYPEOF:
990                 panic("is_type_incomplete called without typerefs skipped");
991         }
992
993         panic("invalid type found");
994 }
995
996 bool is_type_object(const type_t *type)
997 {
998         return !is_type_function(type) && !is_type_incomplete(type);
999 }
1000
1001 /**
1002  * Check if two function types are compatible.
1003  */
1004 static bool function_types_compatible(const function_type_t *func1,
1005                                       const function_type_t *func2)
1006 {
1007         const type_t* const ret1 = skip_typeref(func1->return_type);
1008         const type_t* const ret2 = skip_typeref(func2->return_type);
1009         if (!types_compatible(ret1, ret2))
1010                 return false;
1011
1012         if (func1->linkage != func2->linkage)
1013                 return false;
1014
1015         cc_kind_t cc1 = func1->calling_convention;
1016         if (cc1 == CC_DEFAULT)
1017                 cc1 = default_calling_convention;
1018         cc_kind_t cc2 = func2->calling_convention;
1019         if (cc2 == CC_DEFAULT)
1020                 cc2 = default_calling_convention;
1021
1022         if (cc1 != cc2)
1023                 return false;
1024
1025         if (func1->variadic != func2->variadic)
1026                 return false;
1027
1028         /* can parameters be compared? */
1029         if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1030                         || (func2->unspecified_parameters && !func2->kr_style_parameters))
1031                 return true;
1032
1033         /* TODO: handling of unspecified parameters not correct yet */
1034
1035         /* all argument types must be compatible */
1036         function_parameter_t *parameter1 = func1->parameters;
1037         function_parameter_t *parameter2 = func2->parameters;
1038         for ( ; parameter1 != NULL && parameter2 != NULL;
1039                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
1040                 type_t *parameter1_type = skip_typeref(parameter1->type);
1041                 type_t *parameter2_type = skip_typeref(parameter2->type);
1042
1043                 parameter1_type = get_unqualified_type(parameter1_type);
1044                 parameter2_type = get_unqualified_type(parameter2_type);
1045
1046                 if (!types_compatible(parameter1_type, parameter2_type))
1047                         return false;
1048         }
1049         /* same number of arguments? */
1050         if (parameter1 != NULL || parameter2 != NULL)
1051                 return false;
1052
1053         return true;
1054 }
1055
1056 /**
1057  * Check if two array types are compatible.
1058  */
1059 static bool array_types_compatible(const array_type_t *array1,
1060                                    const array_type_t *array2)
1061 {
1062         type_t *element_type1 = skip_typeref(array1->element_type);
1063         type_t *element_type2 = skip_typeref(array2->element_type);
1064         if (!types_compatible(element_type1, element_type2))
1065                 return false;
1066
1067         if (!array1->size_constant || !array2->size_constant)
1068                 return true;
1069
1070         return array1->size == array2->size;
1071 }
1072
1073 /**
1074  * Check if two types are compatible.
1075  */
1076 bool types_compatible(const type_t *type1, const type_t *type2)
1077 {
1078         assert(!is_typeref(type1));
1079         assert(!is_typeref(type2));
1080
1081         /* shortcut: the same type is always compatible */
1082         if (type1 == type2)
1083                 return true;
1084
1085         if (!is_type_valid(type1) || !is_type_valid(type2))
1086                 return true;
1087
1088         if (type1->base.qualifiers != type2->base.qualifiers)
1089                 return false;
1090         if (type1->kind != type2->kind)
1091                 return false;
1092
1093         switch (type1->kind) {
1094         case TYPE_FUNCTION:
1095                 return function_types_compatible(&type1->function, &type2->function);
1096         case TYPE_ATOMIC:
1097         case TYPE_IMAGINARY:
1098         case TYPE_COMPLEX:
1099                 return type1->atomic.akind == type2->atomic.akind;
1100         case TYPE_ARRAY:
1101                 return array_types_compatible(&type1->array, &type2->array);
1102
1103         case TYPE_POINTER: {
1104                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1105                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1106                 return types_compatible(to1, to2);
1107         }
1108
1109         case TYPE_REFERENCE: {
1110                 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1111                 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1112                 return types_compatible(to1, to2);
1113         }
1114
1115         case TYPE_COMPOUND_STRUCT:
1116         case TYPE_COMPOUND_UNION: {
1117                 break;
1118         }
1119         case TYPE_ENUM:
1120                 /* TODO: not implemented */
1121                 break;
1122
1123         case TYPE_ERROR:
1124                 /* Hmm, the error type should be compatible to all other types */
1125                 return true;
1126         case TYPE_TYPEDEF:
1127         case TYPE_TYPEOF:
1128                 panic("typerefs not skipped in compatible types?!?");
1129         }
1130
1131         return false;
1132 }
1133
1134 /**
1135  * Skip all typerefs and return the underlying type.
1136  */
1137 type_t *skip_typeref(type_t *type)
1138 {
1139         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1140
1141         while (true) {
1142                 switch (type->kind) {
1143                 case TYPE_ERROR:
1144                         return type;
1145                 case TYPE_TYPEDEF: {
1146                         qualifiers |= type->base.qualifiers;
1147
1148                         const typedef_type_t *typedef_type = &type->typedeft;
1149                         if (typedef_type->resolved_type != NULL) {
1150                                 type = typedef_type->resolved_type;
1151                                 break;
1152                         }
1153                         type = typedef_type->typedefe->type;
1154                         continue;
1155                 }
1156                 case TYPE_TYPEOF:
1157                         qualifiers |= type->base.qualifiers;
1158                         type        = type->typeoft.typeof_type;
1159                         continue;
1160                 default:
1161                         break;
1162                 }
1163                 break;
1164         }
1165
1166         if (qualifiers != TYPE_QUALIFIER_NONE) {
1167                 type_t *const copy = duplicate_type(type);
1168
1169                 /* for const with typedefed array type the element type has to be
1170                  * adjusted */
1171                 if (is_type_array(copy)) {
1172                         type_t *element_type           = copy->array.element_type;
1173                         element_type                   = duplicate_type(element_type);
1174                         element_type->base.qualifiers |= qualifiers;
1175                         copy->array.element_type       = element_type;
1176                 } else {
1177                         copy->base.qualifiers |= qualifiers;
1178                 }
1179
1180                 type = identify_new_type(copy);
1181         }
1182
1183         return type;
1184 }
1185
1186 unsigned get_type_size(type_t *type)
1187 {
1188         switch (type->kind) {
1189         case TYPE_ERROR:
1190                 return 0;
1191         case TYPE_ATOMIC:
1192         case TYPE_IMAGINARY:
1193         case TYPE_ENUM:
1194                 return get_atomic_type_size(type->atomic.akind);
1195         case TYPE_COMPLEX:
1196                 return get_atomic_type_size(type->atomic.akind) * 2;
1197         case TYPE_COMPOUND_UNION:
1198                 layout_union_type(&type->compound);
1199                 return type->compound.compound->size;
1200         case TYPE_COMPOUND_STRUCT:
1201                 layout_struct_type(&type->compound);
1202                 return type->compound.compound->size;
1203         case TYPE_FUNCTION:
1204                 return 0; /* non-const (but "address-const") */
1205         case TYPE_REFERENCE:
1206         case TYPE_POINTER:
1207                 return pointer_properties.size;
1208         case TYPE_ARRAY: {
1209                 /* TODO: correct if element_type is aligned? */
1210                 il_size_t element_size = get_type_size(type->array.element_type);
1211                 return type->array.size * element_size;
1212         }
1213         case TYPE_TYPEDEF:
1214                 return get_type_size(type->typedeft.typedefe->type);
1215         case TYPE_TYPEOF:
1216                 return get_type_size(type->typeoft.typeof_type);
1217         }
1218         panic("invalid type in get_type_size");
1219 }
1220
1221 unsigned get_type_alignment(type_t *type)
1222 {
1223         switch (type->kind) {
1224         case TYPE_ERROR:
1225                 return 0;
1226         case TYPE_ATOMIC:
1227         case TYPE_IMAGINARY:
1228         case TYPE_COMPLEX:
1229         case TYPE_ENUM:
1230                 return get_atomic_type_alignment(type->atomic.akind);
1231         case TYPE_COMPOUND_UNION:
1232                 layout_union_type(&type->compound);
1233                 return type->compound.compound->alignment;
1234         case TYPE_COMPOUND_STRUCT:
1235                 layout_struct_type(&type->compound);
1236                 return type->compound.compound->alignment;
1237         case TYPE_FUNCTION:
1238                 /* gcc says 1 here... */
1239                 return 1;
1240         case TYPE_REFERENCE:
1241         case TYPE_POINTER:
1242                 return pointer_properties.alignment;
1243         case TYPE_ARRAY:
1244                 return get_type_alignment(type->array.element_type);
1245         case TYPE_TYPEDEF: {
1246                 il_alignment_t alignment
1247                         = get_type_alignment(type->typedeft.typedefe->type);
1248                 if (type->typedeft.typedefe->alignment > alignment)
1249                         alignment = type->typedeft.typedefe->alignment;
1250
1251                 return alignment;
1252         }
1253         case TYPE_TYPEOF:
1254                 return get_type_alignment(type->typeoft.typeof_type);
1255         }
1256         panic("invalid type in get_type_alignment");
1257 }
1258
1259 /**
1260  * get alignment of a type when used inside a compound.
1261  * Some ABIs are broken and alignment inside a compound is different from
1262  * recommended alignment of a type
1263  */
1264 static unsigned get_type_alignment_compound(type_t *const type)
1265 {
1266         assert(!is_typeref(type));
1267         if (type->kind == TYPE_ATOMIC)
1268                 return atomic_type_properties[type->atomic.akind].struct_alignment;
1269         return get_type_alignment(type);
1270 }
1271
1272 decl_modifiers_t get_type_modifiers(const type_t *type)
1273 {
1274         switch(type->kind) {
1275         case TYPE_ERROR:
1276                 break;
1277         case TYPE_COMPOUND_STRUCT:
1278         case TYPE_COMPOUND_UNION:
1279                 return type->compound.compound->modifiers;
1280         case TYPE_FUNCTION:
1281                 return type->function.modifiers;
1282         case TYPE_ENUM:
1283         case TYPE_ATOMIC:
1284         case TYPE_COMPLEX:
1285         case TYPE_IMAGINARY:
1286         case TYPE_REFERENCE:
1287         case TYPE_POINTER:
1288         case TYPE_ARRAY:
1289                 return 0;
1290         case TYPE_TYPEDEF: {
1291                 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1292                 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1293                 return modifiers;
1294         }
1295         case TYPE_TYPEOF:
1296                 return get_type_modifiers(type->typeoft.typeof_type);
1297         }
1298         panic("invalid type found in get_type_modifiers");
1299 }
1300
1301 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1302 {
1303         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1304
1305         while (true) {
1306                 switch (type->base.kind) {
1307                 case TYPE_ERROR:
1308                         return TYPE_QUALIFIER_NONE;
1309                 case TYPE_TYPEDEF:
1310                         qualifiers |= type->base.qualifiers;
1311                         const typedef_type_t *typedef_type = &type->typedeft;
1312                         if (typedef_type->resolved_type != NULL)
1313                                 type = typedef_type->resolved_type;
1314                         else
1315                                 type = typedef_type->typedefe->type;
1316                         continue;
1317                 case TYPE_TYPEOF:
1318                         type = type->typeoft.typeof_type;
1319                         continue;
1320                 case TYPE_ARRAY:
1321                         if (skip_array_type) {
1322                                 type = type->array.element_type;
1323                                 continue;
1324                         }
1325                         break;
1326                 default:
1327                         break;
1328                 }
1329                 break;
1330         }
1331         return type->base.qualifiers | qualifiers;
1332 }
1333
1334 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1335 {
1336         assert(kind <= ATOMIC_TYPE_LAST);
1337         return atomic_type_properties[kind].size;
1338 }
1339
1340 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1341 {
1342         assert(kind <= ATOMIC_TYPE_LAST);
1343         return atomic_type_properties[kind].alignment;
1344 }
1345
1346 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1347 {
1348         assert(kind <= ATOMIC_TYPE_LAST);
1349         return atomic_type_properties[kind].flags;
1350 }
1351
1352 /**
1353  * Find the atomic type kind representing a given size (signed).
1354  */
1355 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1356 {
1357         static atomic_type_kind_t kinds[32];
1358
1359         assert(size < 32);
1360         atomic_type_kind_t kind = kinds[size];
1361         if (kind == ATOMIC_TYPE_INVALID) {
1362                 static const atomic_type_kind_t possible_kinds[] = {
1363                         ATOMIC_TYPE_SCHAR,
1364                         ATOMIC_TYPE_SHORT,
1365                         ATOMIC_TYPE_INT,
1366                         ATOMIC_TYPE_LONG,
1367                         ATOMIC_TYPE_LONGLONG
1368                 };
1369                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1370                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1371                                 kind = possible_kinds[i];
1372                                 break;
1373                         }
1374                 }
1375                 kinds[size] = kind;
1376         }
1377         return kind;
1378 }
1379
1380 /**
1381  * Find the atomic type kind representing a given size (signed).
1382  */
1383 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1384 {
1385         static atomic_type_kind_t kinds[32];
1386
1387         assert(size < 32);
1388         atomic_type_kind_t kind = kinds[size];
1389         if (kind == ATOMIC_TYPE_INVALID) {
1390                 static const atomic_type_kind_t possible_kinds[] = {
1391                         ATOMIC_TYPE_UCHAR,
1392                         ATOMIC_TYPE_USHORT,
1393                         ATOMIC_TYPE_UINT,
1394                         ATOMIC_TYPE_ULONG,
1395                         ATOMIC_TYPE_ULONGLONG
1396                 };
1397                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1398                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1399                                 kind = possible_kinds[i];
1400                                 break;
1401                         }
1402                 }
1403                 kinds[size] = kind;
1404         }
1405         return kind;
1406 }
1407
1408 /**
1409  * Hash the given type and return the "singleton" version
1410  * of it.
1411  */
1412 type_t *identify_new_type(type_t *type)
1413 {
1414         type_t *result = typehash_insert(type);
1415         if (result != type) {
1416                 obstack_free(&type_obst, type);
1417         }
1418         return result;
1419 }
1420
1421 /**
1422  * Creates a new atomic type.
1423  *
1424  * @param akind       The kind of the atomic type.
1425  * @param qualifiers  Type qualifiers for the new type.
1426  */
1427 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1428 {
1429         type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1430         type->base.qualifiers = qualifiers;
1431         type->atomic.akind    = akind;
1432
1433         return identify_new_type(type);
1434 }
1435
1436 /**
1437  * Creates a new complex type.
1438  *
1439  * @param akind       The kind of the atomic type.
1440  * @param qualifiers  Type qualifiers for the new type.
1441  */
1442 type_t *make_complex_type(atomic_type_kind_t akind,
1443                           type_qualifiers_t qualifiers)
1444 {
1445         type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1446         type->base.qualifiers = qualifiers;
1447         type->atomic.akind   = akind;
1448
1449         return identify_new_type(type);
1450 }
1451
1452 /**
1453  * Creates a new imaginary type.
1454  *
1455  * @param akind       The kind of the atomic type.
1456  * @param qualifiers  Type qualifiers for the new type.
1457  */
1458 type_t *make_imaginary_type(atomic_type_kind_t akind,
1459                             type_qualifiers_t qualifiers)
1460 {
1461         type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1462         type->base.qualifiers = qualifiers;
1463         type->atomic.akind = akind;
1464
1465         return identify_new_type(type);
1466 }
1467
1468 /**
1469  * Creates a new pointer type.
1470  *
1471  * @param points_to   The points-to type for the new type.
1472  * @param qualifiers  Type qualifiers for the new type.
1473  */
1474 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1475 {
1476         type_t *const type = allocate_type_zero(TYPE_POINTER);
1477         type->base.qualifiers       = qualifiers;
1478         type->pointer.points_to     = points_to;
1479         type->pointer.base_variable = NULL;
1480
1481         return identify_new_type(type);
1482 }
1483
1484 /**
1485  * Creates a new reference type.
1486  *
1487  * @param refers_to   The referred-to type for the new type.
1488  */
1489 type_t *make_reference_type(type_t *refers_to)
1490 {
1491         type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1492         type->base.qualifiers     = TYPE_QUALIFIER_NONE;
1493         type->reference.refers_to = refers_to;
1494
1495         return identify_new_type(type);
1496 }
1497
1498 /**
1499  * Creates a new based pointer type.
1500  *
1501  * @param points_to   The points-to type for the new type.
1502  * @param qualifiers  Type qualifiers for the new type.
1503  * @param variable    The based variable
1504  */
1505 type_t *make_based_pointer_type(type_t *points_to,
1506                                                                 type_qualifiers_t qualifiers, variable_t *variable)
1507 {
1508         type_t *const type = allocate_type_zero(TYPE_POINTER);
1509         type->base.qualifiers       = qualifiers;
1510         type->pointer.points_to     = points_to;
1511         type->pointer.base_variable = variable;
1512
1513         return identify_new_type(type);
1514 }
1515
1516
1517 type_t *make_array_type(type_t *element_type, size_t size,
1518                         type_qualifiers_t qualifiers)
1519 {
1520         type_t *const type = allocate_type_zero(TYPE_ARRAY);
1521         type->base.qualifiers     = qualifiers;
1522         type->array.element_type  = element_type;
1523         type->array.size          = size;
1524         type->array.size_constant = true;
1525
1526         return identify_new_type(type);
1527 }
1528
1529 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1530                                        il_alignment_t *struct_alignment,
1531                                                                            bool packed, entity_t *first)
1532 {
1533         il_size_t      offset     = *struct_offset;
1534         il_alignment_t alignment  = *struct_alignment;
1535         size_t         bit_offset = 0;
1536
1537         entity_t *member;
1538         for (member = first; member != NULL; member = member->base.next) {
1539                 if (member->kind != ENTITY_COMPOUND_MEMBER)
1540                         continue;
1541                 if (!member->compound_member.bitfield)
1542                         break;
1543
1544                 type_t *const base_type = skip_typeref(member->declaration.type);
1545                 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1546                 il_alignment_t alignment_mask = base_alignment-1;
1547                 if (base_alignment > alignment)
1548                         alignment = base_alignment;
1549
1550                 size_t bit_size = member->compound_member.bit_size;
1551                 if (!packed) {
1552                         bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1553                         offset     &= ~alignment_mask;
1554                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1555
1556                         if (bit_offset + bit_size > base_size || bit_size == 0) {
1557                                 offset    += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1558                                 offset     = (offset + base_alignment-1) & ~alignment_mask;
1559                                 bit_offset = 0;
1560                         }
1561                 }
1562
1563                 if (byte_order_big_endian) {
1564                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1565                         member->compound_member.offset     = offset & ~alignment_mask;
1566                         member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1567                 } else {
1568                         member->compound_member.offset     = offset;
1569                         member->compound_member.bit_offset = bit_offset;
1570                 }
1571
1572                 bit_offset += bit_size;
1573                 offset     += bit_offset / BITS_PER_BYTE;
1574                 bit_offset %= BITS_PER_BYTE;
1575         }
1576
1577         if (bit_offset > 0)
1578                 offset += 1;
1579
1580         *struct_offset    = offset;
1581         *struct_alignment = alignment;
1582         return member;
1583 }
1584
1585 void layout_struct_type(compound_type_t *type)
1586 {
1587         assert(type->compound != NULL);
1588
1589         compound_t *compound = type->compound;
1590         if (!compound->complete)
1591                 return;
1592         if (type->compound->layouted)
1593                 return;
1594
1595         il_size_t      offset    = 0;
1596         il_alignment_t alignment = compound->alignment;
1597         bool           need_pad  = false;
1598
1599         entity_t *entry = compound->members.entities;
1600         while (entry != NULL) {
1601                 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1602                         entry = entry->base.next;
1603                         continue;
1604                 }
1605
1606                 type_t *const m_type  = skip_typeref(entry->declaration.type);
1607                 if (!is_type_valid(m_type)) {
1608                         entry = entry->base.next;
1609                         continue;
1610                 }
1611
1612                 if (entry->compound_member.bitfield) {
1613                         entry = pack_bitfield_members(&offset, &alignment,
1614                                                       compound->packed, entry);
1615                         continue;
1616                 }
1617
1618                 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1619                 if (m_alignment > alignment)
1620                         alignment = m_alignment;
1621
1622                 if (!compound->packed) {
1623                         il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1624
1625                         if (new_offset > offset) {
1626                                 need_pad = true;
1627                                 offset   = new_offset;
1628                         }
1629                 }
1630
1631                 entry->compound_member.offset = offset;
1632                 offset += get_type_size(m_type);
1633
1634                 entry = entry->base.next;
1635         }
1636
1637         if (!compound->packed) {
1638                 il_size_t new_offset = (offset + alignment-1) & -alignment;
1639                 if (new_offset > offset) {
1640                         need_pad = true;
1641                         offset   = new_offset;
1642                 }
1643         }
1644
1645         source_position_t const *const pos = &compound->base.source_position;
1646         if (need_pad) {
1647                 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1648         } else if (compound->packed) {
1649                 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1650         }
1651
1652         compound->size      = offset;
1653         compound->alignment = alignment;
1654         compound->layouted  = true;
1655 }
1656
1657 void layout_union_type(compound_type_t *type)
1658 {
1659         assert(type->compound != NULL);
1660
1661         compound_t *compound = type->compound;
1662         if (! compound->complete)
1663                 return;
1664
1665         il_size_t      size      = 0;
1666         il_alignment_t alignment = compound->alignment;
1667
1668         entity_t *entry = compound->members.entities;
1669         for (; entry != NULL; entry = entry->base.next) {
1670                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1671                         continue;
1672
1673                 type_t *m_type = skip_typeref(entry->declaration.type);
1674                 if (! is_type_valid(skip_typeref(m_type)))
1675                         continue;
1676
1677                 entry->compound_member.offset = 0;
1678                 il_size_t m_size = get_type_size(m_type);
1679                 if (m_size > size)
1680                         size = m_size;
1681                 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1682                 if (m_alignment > alignment)
1683                         alignment = m_alignment;
1684         }
1685         size = (size + alignment - 1) & -alignment;
1686
1687         compound->size      = size;
1688         compound->alignment = alignment;
1689 }
1690
1691 function_parameter_t *allocate_parameter(type_t *const type)
1692 {
1693         function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1694         memset(param, 0, sizeof(*param));
1695         param->type = type;
1696         return param;
1697 }
1698
1699 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1700                              type_t *argument_type2, decl_modifiers_t modifiers)
1701 {
1702         function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1703         function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1704         parameter1->next = parameter2;
1705
1706         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1707         type->function.return_type = return_type;
1708         type->function.parameters  = parameter1;
1709         type->function.modifiers  |= modifiers;
1710         type->function.linkage     = LINKAGE_C;
1711
1712         return identify_new_type(type);
1713 }
1714
1715 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1716                              decl_modifiers_t modifiers)
1717 {
1718         function_parameter_t *const parameter = allocate_parameter(argument_type);
1719
1720         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1721         type->function.return_type = return_type;
1722         type->function.parameters  = parameter;
1723         type->function.modifiers  |= modifiers;
1724         type->function.linkage     = LINKAGE_C;
1725
1726         return identify_new_type(type);
1727 }
1728
1729 type_t *make_function_1_type_variadic(type_t *return_type,
1730                                       type_t *argument_type,
1731                                       decl_modifiers_t modifiers)
1732 {
1733         function_parameter_t *const parameter = allocate_parameter(argument_type);
1734
1735         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1736         type->function.return_type = return_type;
1737         type->function.parameters  = parameter;
1738         type->function.variadic    = true;
1739         type->function.modifiers  |= modifiers;
1740         type->function.linkage     = LINKAGE_C;
1741
1742         return identify_new_type(type);
1743 }
1744
1745 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1746 {
1747         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1748         type->function.return_type = return_type;
1749         type->function.parameters  = NULL;
1750         type->function.modifiers  |= modifiers;
1751         type->function.linkage     = LINKAGE_C;
1752
1753         return identify_new_type(type);
1754 }
1755
1756 type_t *make_function_type(type_t *return_type, int n_types,
1757                            type_t *const *argument_types,
1758                                                    decl_modifiers_t modifiers)
1759 {
1760         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1761         type->function.return_type = return_type;
1762         type->function.modifiers  |= modifiers;
1763         type->function.linkage     = LINKAGE_C;
1764
1765         function_parameter_t **anchor = &type->function.parameters;
1766         for (int i = 0; i < n_types; ++i) {
1767                 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1768                 *anchor = parameter;
1769                 anchor  = &parameter->next;
1770         }
1771
1772         return identify_new_type(type);
1773 }
1774
1775 /**
1776  * Debug helper. Prints the given type to stdout.
1777  */
1778 static __attribute__((unused))
1779 void dbg_type(const type_t *type)
1780 {
1781         print_to_file(stderr);
1782         print_type(type);
1783         print_string("\n");
1784         fflush(stderr);
1785 }