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