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