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