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