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