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