fix cparser warnings
[cparser] / type.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <assert.h>
24
25 #include "type_t.h"
26 #include "types.h"
27 #include "entity_t.h"
28 #include "symbol_t.h"
29 #include "type_hash.h"
30 #include "adt/error.h"
31 #include "adt/util.h"
32 #include "lang_features.h"
33 #include "warning.h"
34 #include "diagnostic.h"
35 #include "printer.h"
36
37 /** The default calling convention. */
38 cc_kind_t default_calling_convention = CC_CDECL;
39
40 static struct obstack   _type_obst;
41 struct obstack         *type_obst                 = &_type_obst;
42 static bool             print_implicit_array_size = false;
43
44 static void intern_print_type_pre(const type_t *type);
45 static void intern_print_type_post(const type_t *type);
46
47 typedef struct atomic_type_properties_t atomic_type_properties_t;
48 struct atomic_type_properties_t {
49         unsigned   size;              /**< type size in bytes */
50         unsigned   alignment;         /**< type alignment in bytes */
51         unsigned   flags;             /**< type flags from atomic_type_flag_t */
52 };
53
54 /**
55  * Returns the size of a type node.
56  *
57  * @param kind  the type kind
58  */
59 static size_t get_type_struct_size(type_kind_t kind)
60 {
61         static const size_t sizes[] = {
62                 [TYPE_ATOMIC]          = sizeof(atomic_type_t),
63                 [TYPE_COMPLEX]         = sizeof(complex_type_t),
64                 [TYPE_IMAGINARY]       = sizeof(imaginary_type_t),
65                 [TYPE_BITFIELD]        = sizeof(bitfield_type_t),
66                 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
67                 [TYPE_COMPOUND_UNION]  = sizeof(compound_type_t),
68                 [TYPE_ENUM]            = sizeof(enum_type_t),
69                 [TYPE_FUNCTION]        = sizeof(function_type_t),
70                 [TYPE_POINTER]         = sizeof(pointer_type_t),
71                 [TYPE_ARRAY]           = sizeof(array_type_t),
72                 [TYPE_TYPEDEF]         = sizeof(typedef_type_t),
73                 [TYPE_TYPEOF]          = sizeof(typeof_type_t),
74         };
75         assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
76         assert(kind <= TYPE_TYPEOF);
77         assert(sizes[kind] != 0);
78         return sizes[kind];
79 }
80
81 type_t *allocate_type_zero(type_kind_t kind)
82 {
83         size_t  size = get_type_struct_size(kind);
84         type_t *res  = obstack_alloc(type_obst, size);
85         memset(res, 0, size);
86         res->base.kind = kind;
87
88         return res;
89 }
90
91 /**
92  * Properties of atomic types.
93  */
94 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
95         //ATOMIC_TYPE_INVALID = 0,
96         [ATOMIC_TYPE_VOID] = {
97                 .size       = 0,
98                 .alignment  = 0,
99                 .flags      = ATOMIC_TYPE_FLAG_NONE
100         },
101         [ATOMIC_TYPE_WCHAR_T] = {
102                 .size       = (unsigned)-1,
103                 .alignment  = (unsigned)-1,
104                 /* signed flag will be set when known */
105                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
106         },
107         [ATOMIC_TYPE_CHAR] = {
108                 .size       = 1,
109                 .alignment  = 1,
110                 /* signed flag will be set when known */
111                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
112         },
113         [ATOMIC_TYPE_SCHAR] = {
114                 .size       = 1,
115                 .alignment  = 1,
116                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
117                               | ATOMIC_TYPE_FLAG_SIGNED,
118         },
119         [ATOMIC_TYPE_UCHAR] = {
120                 .size       = 1,
121                 .alignment  = 1,
122                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
123         },
124         [ATOMIC_TYPE_SHORT] = {
125                 .size       = 2,
126                 .alignment  = 2,
127                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
128                               | ATOMIC_TYPE_FLAG_SIGNED
129         },
130         [ATOMIC_TYPE_USHORT] = {
131                 .size       = 2,
132                 .alignment  = 2,
133                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
134         },
135         [ATOMIC_TYPE_INT] = {
136                 .size       = (unsigned) -1,
137                 .alignment  = (unsigned) -1,
138                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
139                               | ATOMIC_TYPE_FLAG_SIGNED,
140         },
141         [ATOMIC_TYPE_UINT] = {
142                 .size       = (unsigned) -1,
143                 .alignment  = (unsigned) -1,
144                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
145         },
146         [ATOMIC_TYPE_LONG] = {
147                 .size       = (unsigned) -1,
148                 .alignment  = (unsigned) -1,
149                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
150                               | ATOMIC_TYPE_FLAG_SIGNED,
151         },
152         [ATOMIC_TYPE_ULONG] = {
153                 .size       = (unsigned) -1,
154                 .alignment  = (unsigned) -1,
155                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
156         },
157         [ATOMIC_TYPE_LONGLONG] = {
158                 .size       = (unsigned) -1,
159                 .alignment  = (unsigned) -1,
160                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
161                               | ATOMIC_TYPE_FLAG_SIGNED,
162         },
163         [ATOMIC_TYPE_ULONGLONG] = {
164                 .size       = (unsigned) -1,
165                 .alignment  = (unsigned) -1,
166                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
167         },
168         [ATOMIC_TYPE_BOOL] = {
169                 .size       = (unsigned) -1,
170                 .alignment  = (unsigned) -1,
171                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
172         },
173         [ATOMIC_TYPE_FLOAT] = {
174                 .size       = 4,
175                 .alignment  = (unsigned) -1,
176                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
177                               | ATOMIC_TYPE_FLAG_SIGNED,
178         },
179         [ATOMIC_TYPE_DOUBLE] = {
180                 .size       = 8,
181                 .alignment  = (unsigned) -1,
182                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
183                               | ATOMIC_TYPE_FLAG_SIGNED,
184         },
185         [ATOMIC_TYPE_LONG_DOUBLE] = {
186                 .size       = (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                         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
578                                 expression = expression->unary.value;
579                         }
580                         print_expression(expression);
581                 }
582                 print_string(",\n");
583         }
584
585         change_indent(-1);
586         print_indent();
587         print_string("}");
588 }
589
590 /**
591  * Prints an enum type.
592  *
593  * @param type  The enum type.
594  */
595 static void print_type_enum(const enum_type_t *type)
596 {
597         print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
598         print_string("enum ");
599
600         enum_t   *enume  = type->enume;
601         symbol_t *symbol = enume->base.symbol;
602         if (symbol != NULL) {
603                 print_string(symbol->string);
604         } else {
605                 print_enum_definition(enume);
606         }
607 }
608
609 /**
610  * Print the compound part of a compound type.
611  */
612 void print_compound_definition(const compound_t *compound)
613 {
614         print_string("{\n");
615         change_indent(1);
616
617         entity_t *entity = compound->members.entities;
618         for( ; entity != NULL; entity = entity->base.next) {
619                 if (entity->kind != ENTITY_COMPOUND_MEMBER)
620                         continue;
621
622                 print_indent();
623                 print_entity(entity);
624                 print_string("\n");
625         }
626
627         change_indent(-1);
628         print_indent();
629         print_string("}");
630         if (compound->modifiers & DM_TRANSPARENT_UNION) {
631                 print_string("__attribute__((__transparent_union__))");
632         }
633 }
634
635 /**
636  * Prints a compound type.
637  *
638  * @param type  The compound type.
639  */
640 static void print_compound_type(const compound_type_t *type)
641 {
642         print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
643
644         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
645                 print_string("struct ");
646         } else {
647                 assert(type->base.kind == TYPE_COMPOUND_UNION);
648                 print_string("union ");
649         }
650
651         compound_t *compound = type->compound;
652         symbol_t   *symbol   = compound->base.symbol;
653         if (symbol != NULL) {
654                 print_string(symbol->string);
655         } else {
656                 print_compound_definition(compound);
657         }
658 }
659
660 /**
661  * Prints the prefix part of a typedef type.
662  *
663  * @param type   The typedef type.
664  */
665 static void print_typedef_type_pre(const typedef_type_t *const type)
666 {
667         print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
668         print_string(type->typedefe->base.symbol->string);
669 }
670
671 /**
672  * Prints the prefix part of a typeof type.
673  *
674  * @param type   The typeof type.
675  */
676 static void print_typeof_type_pre(const typeof_type_t *const type)
677 {
678         print_string("typeof(");
679         if (type->expression != NULL) {
680                 print_expression(type->expression);
681         } else {
682                 print_type(type->typeof_type);
683         }
684         print_string(")");
685 }
686
687 /**
688  * Prints the prefix part of a type.
689  *
690  * @param type   The type.
691  */
692 static void intern_print_type_pre(const type_t *const type)
693 {
694         switch(type->kind) {
695         case TYPE_ERROR:
696                 print_string("<error>");
697                 return;
698         case TYPE_INVALID:
699                 print_string("<invalid>");
700                 return;
701         case TYPE_ENUM:
702                 print_type_enum(&type->enumt);
703                 return;
704         case TYPE_ATOMIC:
705                 print_atomic_type(&type->atomic);
706                 return;
707         case TYPE_COMPLEX:
708                 print_complex_type(&type->complex);
709                 return;
710         case TYPE_IMAGINARY:
711                 print_imaginary_type(&type->imaginary);
712                 return;
713         case TYPE_COMPOUND_STRUCT:
714         case TYPE_COMPOUND_UNION:
715                 print_compound_type(&type->compound);
716                 return;
717         case TYPE_FUNCTION:
718                 print_function_type_pre(&type->function);
719                 return;
720         case TYPE_POINTER:
721                 print_pointer_type_pre(&type->pointer);
722                 return;
723         case TYPE_REFERENCE:
724                 print_reference_type_pre(&type->reference);
725                 return;
726         case TYPE_BITFIELD:
727                 intern_print_type_pre(type->bitfield.base_type);
728                 return;
729         case TYPE_ARRAY:
730                 print_array_type_pre(&type->array);
731                 return;
732         case TYPE_TYPEDEF:
733                 print_typedef_type_pre(&type->typedeft);
734                 return;
735         case TYPE_TYPEOF:
736                 print_typeof_type_pre(&type->typeoft);
737                 return;
738         }
739         print_string("unknown");
740 }
741
742 /**
743  * Prints the postfix part of a type.
744  *
745  * @param type   The type.
746  */
747 static void intern_print_type_post(const type_t *const type)
748 {
749         switch(type->kind) {
750         case TYPE_FUNCTION:
751                 print_function_type_post(&type->function, NULL);
752                 return;
753         case TYPE_POINTER:
754                 print_pointer_type_post(&type->pointer);
755                 return;
756         case TYPE_REFERENCE:
757                 print_reference_type_post(&type->reference);
758                 return;
759         case TYPE_ARRAY:
760                 print_array_type_post(&type->array);
761                 return;
762         case TYPE_BITFIELD:
763                 print_bitfield_type_post(&type->bitfield);
764                 return;
765         case TYPE_ERROR:
766         case TYPE_INVALID:
767         case TYPE_ATOMIC:
768         case TYPE_COMPLEX:
769         case TYPE_IMAGINARY:
770         case TYPE_ENUM:
771         case TYPE_COMPOUND_STRUCT:
772         case TYPE_COMPOUND_UNION:
773         case TYPE_TYPEOF:
774         case TYPE_TYPEDEF:
775                 break;
776         }
777 }
778
779 /**
780  * Prints a type.
781  *
782  * @param type   The type.
783  */
784 void print_type(const type_t *const type)
785 {
786         print_type_ext(type, NULL, NULL);
787 }
788
789 void print_type_ext(const type_t *const type, const symbol_t *symbol,
790                     const scope_t *parameters)
791 {
792         intern_print_type_pre(type);
793         if (symbol != NULL) {
794                 print_string(" ");
795                 print_string(symbol->string);
796         }
797         if (type->kind == TYPE_FUNCTION) {
798                 print_function_type_post(&type->function, parameters);
799         } else {
800                 intern_print_type_post(type);
801         }
802 }
803
804 /**
805  * Duplicates a type.
806  *
807  * @param type  The type to copy.
808  * @return A copy of the type.
809  *
810  * @note This does not produce a deep copy!
811  */
812 type_t *duplicate_type(const type_t *type)
813 {
814         size_t size = get_type_struct_size(type->kind);
815
816         type_t *copy = obstack_alloc(type_obst, size);
817         memcpy(copy, type, size);
818         copy->base.firm_type = NULL;
819
820         return copy;
821 }
822
823 /**
824  * Returns the unqualified type of a given type.
825  *
826  * @param type  The type.
827  * @returns The unqualified type.
828  */
829 type_t *get_unqualified_type(type_t *type)
830 {
831         assert(!is_typeref(type));
832
833         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
834                 return type;
835
836         type_t *unqualified_type          = duplicate_type(type);
837         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
838
839         return identify_new_type(unqualified_type);
840 }
841
842 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
843 {
844         type_t *type = skip_typeref(orig_type);
845
846         type_t *copy;
847         if (is_type_array(type)) {
848                 /* For array types the element type has to be adjusted */
849                 type_t *element_type      = type->array.element_type;
850                 type_t *qual_element_type = get_qualified_type(element_type, qual);
851
852                 if (qual_element_type == element_type)
853                         return orig_type;
854
855                 copy                     = duplicate_type(type);
856                 copy->array.element_type = qual_element_type;
857         } else if (is_type_valid(type)) {
858                 if ((type->base.qualifiers & qual) == (int)qual)
859                         return orig_type;
860
861                 copy                   = duplicate_type(type);
862                 copy->base.qualifiers |= qual;
863         } else {
864                 return type;
865         }
866
867         return identify_new_type(copy);
868 }
869
870 /**
871  * Check if a type is valid.
872  *
873  * @param type  The type to check.
874  * @return true if type represents a valid type.
875  */
876 bool type_valid(const type_t *type)
877 {
878         return type->kind != TYPE_INVALID;
879 }
880
881 static bool test_atomic_type_flag(atomic_type_kind_t kind,
882                                   atomic_type_flag_t flag)
883 {
884         assert(kind <= ATOMIC_TYPE_LAST);
885         return (atomic_type_properties[kind].flags & flag) != 0;
886 }
887
888 /**
889  * Returns true if the given type is an integer type.
890  *
891  * @param type  The type to check.
892  * @return True if type is an integer type.
893  */
894 bool is_type_integer(const type_t *type)
895 {
896         assert(!is_typeref(type));
897
898         if (type->kind == TYPE_ENUM)
899                 return true;
900         if (type->kind == TYPE_BITFIELD)
901                 return true;
902
903         if (type->kind != TYPE_ATOMIC)
904                 return false;
905
906         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
907 }
908
909 /**
910  * Returns true if the given type is an enum type.
911  *
912  * @param type  The type to check.
913  * @return True if type is an enum type.
914  */
915 bool is_type_enum(const type_t *type)
916 {
917         assert(!is_typeref(type));
918         return type->kind == TYPE_ENUM;
919 }
920
921 /**
922  * Returns true if the given type is an floating point type.
923  *
924  * @param type  The type to check.
925  * @return True if type is a floating point type.
926  */
927 bool is_type_float(const type_t *type)
928 {
929         assert(!is_typeref(type));
930
931         if (type->kind != TYPE_ATOMIC)
932                 return false;
933
934         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
935 }
936
937 /**
938  * Returns true if the given type is an complex type.
939  *
940  * @param type  The type to check.
941  * @return True if type is a complex type.
942  */
943 bool is_type_complex(const type_t *type)
944 {
945         assert(!is_typeref(type));
946
947         if (type->kind != TYPE_ATOMIC)
948                 return false;
949
950         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
951 }
952
953 /**
954  * Returns true if the given type is a signed type.
955  *
956  * @param type  The type to check.
957  * @return True if type is a signed type.
958  */
959 bool is_type_signed(const type_t *type)
960 {
961         assert(!is_typeref(type));
962
963         /* enum types are int for now */
964         if (type->kind == TYPE_ENUM)
965                 return true;
966         if (type->kind == TYPE_BITFIELD)
967                 return is_type_signed(type->bitfield.base_type);
968
969         if (type->kind != TYPE_ATOMIC)
970                 return false;
971
972         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
973 }
974
975 /**
976  * Returns true if the given type represents an arithmetic type.
977  *
978  * @param type  The type to check.
979  * @return True if type represents an arithmetic type.
980  */
981 bool is_type_arithmetic(const type_t *type)
982 {
983         assert(!is_typeref(type));
984
985         switch(type->kind) {
986         case TYPE_BITFIELD:
987         case TYPE_ENUM:
988                 return true;
989         case TYPE_ATOMIC:
990                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
991         case TYPE_COMPLEX:
992                 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
993         case TYPE_IMAGINARY:
994                 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
995         default:
996                 return false;
997         }
998 }
999
1000 /**
1001  * Returns true if the given type is an integer or float type.
1002  *
1003  * @param type  The type to check.
1004  * @return True if type is an integer or float type.
1005  */
1006 bool is_type_real(const type_t *type)
1007 {
1008         /* 6.2.5 (17) */
1009         return is_type_integer(type) || is_type_float(type);
1010 }
1011
1012 /**
1013  * Returns true if the given type represents a scalar type.
1014  *
1015  * @param type  The type to check.
1016  * @return True if type represents a scalar type.
1017  */
1018 bool is_type_scalar(const type_t *type)
1019 {
1020         assert(!is_typeref(type));
1021
1022         if (type->kind == TYPE_POINTER)
1023                 return true;
1024
1025         return is_type_arithmetic(type);
1026 }
1027
1028 /**
1029  * Check if a given type is incomplete.
1030  *
1031  * @param type  The type to check.
1032  * @return True if the given type is incomplete (ie. just forward).
1033  */
1034 bool is_type_incomplete(const type_t *type)
1035 {
1036         assert(!is_typeref(type));
1037
1038         switch(type->kind) {
1039         case TYPE_COMPOUND_STRUCT:
1040         case TYPE_COMPOUND_UNION: {
1041                 const compound_type_t *compound_type = &type->compound;
1042                 return !compound_type->compound->complete;
1043         }
1044         case TYPE_ENUM:
1045                 return false;
1046
1047         case TYPE_ARRAY:
1048                 return type->array.size_expression == NULL
1049                         && !type->array.size_constant;
1050
1051         case TYPE_ATOMIC:
1052                 return type->atomic.akind == ATOMIC_TYPE_VOID;
1053
1054         case TYPE_COMPLEX:
1055                 return type->complex.akind == ATOMIC_TYPE_VOID;
1056
1057         case TYPE_IMAGINARY:
1058                 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1059
1060         case TYPE_BITFIELD:
1061         case TYPE_FUNCTION:
1062         case TYPE_POINTER:
1063         case TYPE_REFERENCE:
1064         case TYPE_ERROR:
1065                 return false;
1066
1067         case TYPE_TYPEDEF:
1068         case TYPE_TYPEOF:
1069                 panic("is_type_incomplete called without typerefs skipped");
1070         case TYPE_INVALID:
1071                 break;
1072         }
1073
1074         panic("invalid type found");
1075 }
1076
1077 bool is_type_object(const type_t *type)
1078 {
1079         return !is_type_function(type) && !is_type_incomplete(type);
1080 }
1081
1082 /**
1083  * Check if two function types are compatible.
1084  */
1085 static bool function_types_compatible(const function_type_t *func1,
1086                                       const function_type_t *func2)
1087 {
1088         const type_t* const ret1 = skip_typeref(func1->return_type);
1089         const type_t* const ret2 = skip_typeref(func2->return_type);
1090         if (!types_compatible(ret1, ret2))
1091                 return false;
1092
1093         if (func1->linkage != func2->linkage)
1094                 return false;
1095
1096         cc_kind_t cc1 = func1->calling_convention;
1097         if (cc1 == CC_DEFAULT)
1098                 cc1 = default_calling_convention;
1099         cc_kind_t cc2 = func2->calling_convention;
1100         if (cc2 == CC_DEFAULT)
1101                 cc2 = default_calling_convention;
1102
1103         if (cc1 != cc2)
1104                 return false;
1105
1106         if (func1->variadic != func2->variadic)
1107                 return false;
1108
1109         /* can parameters be compared? */
1110         if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1111                         || (func2->unspecified_parameters && !func2->kr_style_parameters))
1112                 return true;
1113
1114         /* TODO: handling of unspecified parameters not correct yet */
1115
1116         /* all argument types must be compatible */
1117         function_parameter_t *parameter1 = func1->parameters;
1118         function_parameter_t *parameter2 = func2->parameters;
1119         for ( ; parameter1 != NULL && parameter2 != NULL;
1120                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
1121                 type_t *parameter1_type = skip_typeref(parameter1->type);
1122                 type_t *parameter2_type = skip_typeref(parameter2->type);
1123
1124                 parameter1_type = get_unqualified_type(parameter1_type);
1125                 parameter2_type = get_unqualified_type(parameter2_type);
1126
1127                 if (!types_compatible(parameter1_type, parameter2_type))
1128                         return false;
1129         }
1130         /* same number of arguments? */
1131         if (parameter1 != NULL || parameter2 != NULL)
1132                 return false;
1133
1134         return true;
1135 }
1136
1137 /**
1138  * Check if two array types are compatible.
1139  */
1140 static bool array_types_compatible(const array_type_t *array1,
1141                                    const array_type_t *array2)
1142 {
1143         type_t *element_type1 = skip_typeref(array1->element_type);
1144         type_t *element_type2 = skip_typeref(array2->element_type);
1145         if (!types_compatible(element_type1, element_type2))
1146                 return false;
1147
1148         if (!array1->size_constant || !array2->size_constant)
1149                 return true;
1150
1151         return array1->size == array2->size;
1152 }
1153
1154 /**
1155  * Check if two types are compatible.
1156  */
1157 bool types_compatible(const type_t *type1, const type_t *type2)
1158 {
1159         assert(!is_typeref(type1));
1160         assert(!is_typeref(type2));
1161
1162         /* shortcut: the same type is always compatible */
1163         if (type1 == type2)
1164                 return true;
1165
1166         if (!is_type_valid(type1) || !is_type_valid(type2))
1167                 return true;
1168
1169         if (type1->base.qualifiers != type2->base.qualifiers)
1170                 return false;
1171         if (type1->kind != type2->kind)
1172                 return false;
1173
1174         switch (type1->kind) {
1175         case TYPE_FUNCTION:
1176                 return function_types_compatible(&type1->function, &type2->function);
1177         case TYPE_ATOMIC:
1178                 return type1->atomic.akind == type2->atomic.akind;
1179         case TYPE_COMPLEX:
1180                 return type1->complex.akind == type2->complex.akind;
1181         case TYPE_IMAGINARY:
1182                 return type1->imaginary.akind == type2->imaginary.akind;
1183         case TYPE_ARRAY:
1184                 return array_types_compatible(&type1->array, &type2->array);
1185
1186         case TYPE_POINTER: {
1187                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1188                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1189                 return types_compatible(to1, to2);
1190         }
1191
1192         case TYPE_REFERENCE: {
1193                 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1194                 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1195                 return types_compatible(to1, to2);
1196         }
1197
1198         case TYPE_COMPOUND_STRUCT:
1199         case TYPE_COMPOUND_UNION: {
1200
1201
1202                 break;
1203         }
1204         case TYPE_ENUM:
1205                 /* TODO: not implemented */
1206                 break;
1207
1208         case TYPE_BITFIELD:
1209                 /* not sure if this makes sense or is even needed, implement it if you
1210                  * really need it! */
1211                 panic("type compatibility check for bitfield type");
1212
1213         case TYPE_ERROR:
1214                 /* Hmm, the error type should be compatible to all other types */
1215                 return true;
1216         case TYPE_INVALID:
1217                 panic("invalid type found in compatible types");
1218         case TYPE_TYPEDEF:
1219         case TYPE_TYPEOF:
1220                 panic("typerefs not skipped in compatible types?!?");
1221         }
1222
1223         /* TODO: incomplete */
1224         return false;
1225 }
1226
1227 /**
1228  * Skip all typerefs and return the underlying type.
1229  */
1230 type_t *skip_typeref(type_t *type)
1231 {
1232         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1233
1234         while (true) {
1235                 switch (type->kind) {
1236                 case TYPE_ERROR:
1237                         return type;
1238                 case TYPE_TYPEDEF: {
1239                         qualifiers |= type->base.qualifiers;
1240
1241                         const typedef_type_t *typedef_type = &type->typedeft;
1242                         if (typedef_type->resolved_type != NULL) {
1243                                 type = typedef_type->resolved_type;
1244                                 break;
1245                         }
1246                         type = typedef_type->typedefe->type;
1247                         continue;
1248                 }
1249                 case TYPE_TYPEOF:
1250                         qualifiers |= type->base.qualifiers;
1251                         type        = type->typeoft.typeof_type;
1252                         continue;
1253                 default:
1254                         break;
1255                 }
1256                 break;
1257         }
1258
1259         if (qualifiers != TYPE_QUALIFIER_NONE) {
1260                 type_t *const copy = duplicate_type(type);
1261
1262                 /* for const with typedefed array type the element type has to be
1263                  * adjusted */
1264                 if (is_type_array(copy)) {
1265                         type_t *element_type           = copy->array.element_type;
1266                         element_type                   = duplicate_type(element_type);
1267                         element_type->base.qualifiers |= qualifiers;
1268                         copy->array.element_type       = element_type;
1269                 } else {
1270                         copy->base.qualifiers |= qualifiers;
1271                 }
1272
1273                 type = identify_new_type(copy);
1274         }
1275
1276         return type;
1277 }
1278
1279 unsigned get_type_size(type_t *type)
1280 {
1281         switch (type->kind) {
1282         case TYPE_INVALID:
1283                 break;
1284         case TYPE_ERROR:
1285                 return 0;
1286         case TYPE_ATOMIC:
1287                 return get_atomic_type_size(type->atomic.akind);
1288         case TYPE_COMPLEX:
1289                 return get_atomic_type_size(type->complex.akind) * 2;
1290         case TYPE_IMAGINARY:
1291                 return get_atomic_type_size(type->imaginary.akind);
1292         case TYPE_COMPOUND_UNION:
1293                 layout_union_type(&type->compound);
1294                 return type->compound.compound->size;
1295         case TYPE_COMPOUND_STRUCT:
1296                 layout_struct_type(&type->compound);
1297                 return type->compound.compound->size;
1298         case TYPE_ENUM:
1299                 return get_atomic_type_size(type->enumt.akind);
1300         case TYPE_FUNCTION:
1301                 return 0; /* non-const (but "address-const") */
1302         case TYPE_REFERENCE:
1303         case TYPE_POINTER:
1304                 /* TODO: make configurable by backend */
1305                 return 4;
1306         case TYPE_ARRAY: {
1307                 /* TODO: correct if element_type is aligned? */
1308                 il_size_t element_size = get_type_size(type->array.element_type);
1309                 return type->array.size * element_size;
1310         }
1311         case TYPE_BITFIELD:
1312                 return 0;
1313         case TYPE_TYPEDEF:
1314                 return get_type_size(type->typedeft.typedefe->type);
1315         case TYPE_TYPEOF:
1316                 if (type->typeoft.typeof_type) {
1317                         return get_type_size(type->typeoft.typeof_type);
1318                 } else {
1319                         return get_type_size(type->typeoft.expression->base.type);
1320                 }
1321         }
1322         panic("invalid type in get_type_size");
1323 }
1324
1325 unsigned get_type_alignment(type_t *type)
1326 {
1327         switch (type->kind) {
1328         case TYPE_INVALID:
1329                 break;
1330         case TYPE_ERROR:
1331                 return 0;
1332         case TYPE_ATOMIC:
1333                 return get_atomic_type_alignment(type->atomic.akind);
1334         case TYPE_COMPLEX:
1335                 return get_atomic_type_alignment(type->complex.akind);
1336         case TYPE_IMAGINARY:
1337                 return get_atomic_type_alignment(type->imaginary.akind);
1338         case TYPE_COMPOUND_UNION:
1339                 layout_union_type(&type->compound);
1340                 return type->compound.compound->alignment;
1341         case TYPE_COMPOUND_STRUCT:
1342                 layout_struct_type(&type->compound);
1343                 return type->compound.compound->alignment;
1344         case TYPE_ENUM:
1345                 return get_atomic_type_alignment(type->enumt.akind);
1346         case TYPE_FUNCTION:
1347                 /* what is correct here? */
1348                 return 4;
1349         case TYPE_REFERENCE:
1350         case TYPE_POINTER:
1351                 /* TODO: make configurable by backend */
1352                 return 4;
1353         case TYPE_ARRAY:
1354                 return get_type_alignment(type->array.element_type);
1355         case TYPE_BITFIELD:
1356                 return 0;
1357         case TYPE_TYPEDEF: {
1358                 il_alignment_t alignment
1359                         = get_type_alignment(type->typedeft.typedefe->type);
1360                 if (type->typedeft.typedefe->alignment > alignment)
1361                         alignment = type->typedeft.typedefe->alignment;
1362
1363                 return alignment;
1364         }
1365         case TYPE_TYPEOF:
1366                 if (type->typeoft.typeof_type) {
1367                         return get_type_alignment(type->typeoft.typeof_type);
1368                 } else {
1369                         return get_type_alignment(type->typeoft.expression->base.type);
1370                 }
1371         }
1372         panic("invalid type in get_type_alignment");
1373 }
1374
1375 decl_modifiers_t get_type_modifiers(const type_t *type)
1376 {
1377         switch(type->kind) {
1378         case TYPE_INVALID:
1379         case TYPE_ERROR:
1380                 break;
1381         case TYPE_COMPOUND_STRUCT:
1382         case TYPE_COMPOUND_UNION:
1383                 return type->compound.compound->modifiers;
1384         case TYPE_FUNCTION:
1385                 return type->function.modifiers;
1386         case TYPE_ENUM:
1387         case TYPE_ATOMIC:
1388         case TYPE_COMPLEX:
1389         case TYPE_IMAGINARY:
1390         case TYPE_REFERENCE:
1391         case TYPE_POINTER:
1392         case TYPE_BITFIELD:
1393         case TYPE_ARRAY:
1394                 return 0;
1395         case TYPE_TYPEDEF: {
1396                 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1397                 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1398                 return modifiers;
1399         }
1400         case TYPE_TYPEOF:
1401                 if (type->typeoft.typeof_type) {
1402                         return get_type_modifiers(type->typeoft.typeof_type);
1403                 } else {
1404                         return get_type_modifiers(type->typeoft.expression->base.type);
1405                 }
1406         }
1407         panic("invalid type found in get_type_modifiers");
1408 }
1409
1410 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1411 {
1412         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1413
1414         while (true) {
1415                 switch (type->base.kind) {
1416                 case TYPE_ERROR:
1417                         return TYPE_QUALIFIER_NONE;
1418                 case TYPE_TYPEDEF:
1419                         qualifiers |= type->base.qualifiers;
1420                         const typedef_type_t *typedef_type = &type->typedeft;
1421                         if (typedef_type->resolved_type != NULL)
1422                                 type = typedef_type->resolved_type;
1423                         else
1424                                 type = typedef_type->typedefe->type;
1425                         continue;
1426                 case TYPE_TYPEOF:
1427                         type = type->typeoft.typeof_type;
1428                         continue;
1429                 case TYPE_ARRAY:
1430                         if (skip_array_type) {
1431                                 type = type->array.element_type;
1432                                 continue;
1433                         }
1434                         break;
1435                 default:
1436                         break;
1437                 }
1438                 break;
1439         }
1440         return type->base.qualifiers | qualifiers;
1441 }
1442
1443 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1444 {
1445         assert(kind <= ATOMIC_TYPE_LAST);
1446         return atomic_type_properties[kind].size;
1447 }
1448
1449 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1450 {
1451         assert(kind <= ATOMIC_TYPE_LAST);
1452         return atomic_type_properties[kind].alignment;
1453 }
1454
1455 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1456 {
1457         assert(kind <= ATOMIC_TYPE_LAST);
1458         return atomic_type_properties[kind].flags;
1459 }
1460
1461 atomic_type_kind_t get_intptr_kind(void)
1462 {
1463         if (machine_size <= 32)
1464                 return ATOMIC_TYPE_INT;
1465         else if (machine_size <= 64)
1466                 return ATOMIC_TYPE_LONG;
1467         else
1468                 return ATOMIC_TYPE_LONGLONG;
1469 }
1470
1471 atomic_type_kind_t get_uintptr_kind(void)
1472 {
1473         if (machine_size <= 32)
1474                 return ATOMIC_TYPE_UINT;
1475         else if (machine_size <= 64)
1476                 return ATOMIC_TYPE_ULONG;
1477         else
1478                 return ATOMIC_TYPE_ULONGLONG;
1479 }
1480
1481 /**
1482  * Find the atomic type kind representing a given size (signed).
1483  */
1484 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1485 {
1486         static atomic_type_kind_t kinds[32];
1487
1488         assert(size < 32);
1489         atomic_type_kind_t kind = kinds[size];
1490         if (kind == ATOMIC_TYPE_INVALID) {
1491                 static const atomic_type_kind_t possible_kinds[] = {
1492                         ATOMIC_TYPE_SCHAR,
1493                         ATOMIC_TYPE_SHORT,
1494                         ATOMIC_TYPE_INT,
1495                         ATOMIC_TYPE_LONG,
1496                         ATOMIC_TYPE_LONGLONG
1497                 };
1498                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1499                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1500                                 kind = possible_kinds[i];
1501                                 break;
1502                         }
1503                 }
1504                 kinds[size] = kind;
1505         }
1506         return kind;
1507 }
1508
1509 /**
1510  * Find the atomic type kind representing a given size (signed).
1511  */
1512 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1513 {
1514         static atomic_type_kind_t kinds[32];
1515
1516         assert(size < 32);
1517         atomic_type_kind_t kind = kinds[size];
1518         if (kind == ATOMIC_TYPE_INVALID) {
1519                 static const atomic_type_kind_t possible_kinds[] = {
1520                         ATOMIC_TYPE_UCHAR,
1521                         ATOMIC_TYPE_USHORT,
1522                         ATOMIC_TYPE_UINT,
1523                         ATOMIC_TYPE_ULONG,
1524                         ATOMIC_TYPE_ULONGLONG
1525                 };
1526                 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1527                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1528                                 kind = possible_kinds[i];
1529                                 break;
1530                         }
1531                 }
1532                 kinds[size] = kind;
1533         }
1534         return kind;
1535 }
1536
1537 /**
1538  * Hash the given type and return the "singleton" version
1539  * of it.
1540  */
1541 type_t *identify_new_type(type_t *type)
1542 {
1543         type_t *result = typehash_insert(type);
1544         if (result != type) {
1545                 obstack_free(type_obst, type);
1546         }
1547         return result;
1548 }
1549
1550 /**
1551  * Creates a new atomic type.
1552  *
1553  * @param akind       The kind of the atomic type.
1554  * @param qualifiers  Type qualifiers for the new type.
1555  */
1556 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1557 {
1558         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1559         memset(type, 0, sizeof(atomic_type_t));
1560
1561         type->kind            = TYPE_ATOMIC;
1562         type->base.qualifiers = qualifiers;
1563         type->atomic.akind    = akind;
1564
1565         return identify_new_type(type);
1566 }
1567
1568 /**
1569  * Creates a new complex type.
1570  *
1571  * @param akind       The kind of the atomic type.
1572  * @param qualifiers  Type qualifiers for the new type.
1573  */
1574 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1575 {
1576         type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1577         memset(type, 0, sizeof(complex_type_t));
1578
1579         type->kind            = TYPE_COMPLEX;
1580         type->base.qualifiers = qualifiers;
1581         type->complex.akind   = akind;
1582
1583         return identify_new_type(type);
1584 }
1585
1586 /**
1587  * Creates a new imaginary type.
1588  *
1589  * @param akind       The kind of the atomic type.
1590  * @param qualifiers  Type qualifiers for the new type.
1591  */
1592 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1593 {
1594         type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1595         memset(type, 0, sizeof(imaginary_type_t));
1596
1597         type->kind            = TYPE_IMAGINARY;
1598         type->base.qualifiers = qualifiers;
1599         type->imaginary.akind = akind;
1600
1601         return identify_new_type(type);
1602 }
1603
1604 /**
1605  * Creates a new pointer type.
1606  *
1607  * @param points_to   The points-to type for the new type.
1608  * @param qualifiers  Type qualifiers for the new type.
1609  */
1610 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1611 {
1612         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1613         memset(type, 0, sizeof(pointer_type_t));
1614
1615         type->kind                  = TYPE_POINTER;
1616         type->base.qualifiers       = qualifiers;
1617         type->pointer.points_to     = points_to;
1618         type->pointer.base_variable = NULL;
1619
1620         return identify_new_type(type);
1621 }
1622
1623 /**
1624  * Creates a new reference type.
1625  *
1626  * @param refers_to   The referred-to type for the new type.
1627  */
1628 type_t *make_reference_type(type_t *refers_to)
1629 {
1630         type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1631         memset(type, 0, sizeof(reference_type_t));
1632
1633         type->kind                = TYPE_REFERENCE;
1634         type->base.qualifiers     = 0;
1635         type->reference.refers_to = refers_to;
1636
1637         return identify_new_type(type);
1638 }
1639
1640 /**
1641  * Creates a new based pointer type.
1642  *
1643  * @param points_to   The points-to type for the new type.
1644  * @param qualifiers  Type qualifiers for the new type.
1645  * @param variable    The based variable
1646  */
1647 type_t *make_based_pointer_type(type_t *points_to,
1648                                                                 type_qualifiers_t qualifiers, variable_t *variable)
1649 {
1650         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1651         memset(type, 0, sizeof(pointer_type_t));
1652
1653         type->kind                  = TYPE_POINTER;
1654         type->base.qualifiers       = qualifiers;
1655         type->pointer.points_to     = points_to;
1656         type->pointer.base_variable = variable;
1657
1658         return identify_new_type(type);
1659 }
1660
1661
1662 type_t *make_array_type(type_t *element_type, size_t size,
1663                         type_qualifiers_t qualifiers)
1664 {
1665         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1666         memset(type, 0, sizeof(array_type_t));
1667
1668         type->kind                = TYPE_ARRAY;
1669         type->base.qualifiers     = qualifiers;
1670         type->array.element_type  = element_type;
1671         type->array.size          = size;
1672         type->array.size_constant = true;
1673
1674         return identify_new_type(type);
1675 }
1676
1677 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1678                                        il_alignment_t *struct_alignment,
1679                                                                            bool packed, entity_t *first)
1680 {
1681         il_size_t      offset     = *struct_offset;
1682         il_alignment_t alignment  = *struct_alignment;
1683         size_t         bit_offset = 0;
1684
1685         entity_t *member;
1686         for (member = first; member != NULL; member = member->base.next) {
1687                 if (member->kind != ENTITY_COMPOUND_MEMBER)
1688                         continue;
1689
1690                 type_t *type = member->declaration.type;
1691                 if (type->kind != TYPE_BITFIELD)
1692                         break;
1693
1694                 type_t *base_type = skip_typeref(type->bitfield.base_type);
1695                 il_alignment_t base_alignment = get_type_alignment(base_type);
1696                 il_alignment_t alignment_mask = base_alignment-1;
1697                 if (base_alignment > alignment)
1698                         alignment = base_alignment;
1699
1700                 size_t bit_size = type->bitfield.bit_size;
1701                 if (!packed) {
1702                         bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1703                         offset     &= ~alignment_mask;
1704                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1705
1706                         if (bit_offset + bit_size > base_size || bit_size == 0) {
1707                                 offset    += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1708                                 offset     = (offset + base_alignment-1) & ~alignment_mask;
1709                                 bit_offset = 0;
1710                         }
1711                 }
1712
1713                 if (byte_order_big_endian) {
1714                         size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1715                         member->compound_member.offset     = offset & ~alignment_mask;
1716                         member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1717                 } else {
1718                         member->compound_member.offset     = offset;
1719                         member->compound_member.bit_offset = bit_offset;
1720                 }
1721
1722                 bit_offset += bit_size;
1723                 offset     += bit_offset / BITS_PER_BYTE;
1724                 bit_offset %= BITS_PER_BYTE;
1725         }
1726
1727         if (bit_offset > 0)
1728                 offset += 1;
1729
1730         *struct_offset    = offset;
1731         *struct_alignment = alignment;
1732         return member;
1733 }
1734
1735 void layout_struct_type(compound_type_t *type)
1736 {
1737         assert(type->compound != NULL);
1738
1739         compound_t *compound = type->compound;
1740         if (!compound->complete)
1741                 return;
1742         if (type->compound->layouted)
1743                 return;
1744
1745         il_size_t      offset    = 0;
1746         il_alignment_t alignment = compound->alignment;
1747         bool           need_pad  = false;
1748
1749         entity_t *entry = compound->members.entities;
1750         while (entry != NULL) {
1751                 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1752                         entry = entry->base.next;
1753                         continue;
1754                 }
1755
1756                 type_t *m_type  = entry->declaration.type;
1757                 type_t *skipped = skip_typeref(m_type);
1758                 if (! is_type_valid(skipped)) {
1759                         entry = entry->base.next;
1760                         continue;
1761                 }
1762
1763                 if (skipped->kind == TYPE_BITFIELD) {
1764                         entry = pack_bitfield_members(&offset, &alignment,
1765                                                       compound->packed, entry);
1766                         continue;
1767                 }
1768
1769                 il_alignment_t m_alignment = get_type_alignment(m_type);
1770                 if (m_alignment > alignment)
1771                         alignment = m_alignment;
1772
1773                 if (!compound->packed) {
1774                         il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1775
1776                         if (new_offset > offset) {
1777                                 need_pad = true;
1778                                 offset   = new_offset;
1779                         }
1780                 }
1781
1782                 entry->compound_member.offset = offset;
1783                 offset += get_type_size(m_type);
1784
1785                 entry = entry->base.next;
1786         }
1787
1788         if (!compound->packed) {
1789                 il_size_t new_offset = (offset + alignment-1) & -alignment;
1790                 if (new_offset > offset) {
1791                         need_pad = true;
1792                         offset   = new_offset;
1793                 }
1794         }
1795
1796         if (need_pad) {
1797                 if (warning.padded) {
1798                         warningf(&compound->base.source_position, "'%T' needs padding",
1799                                  type);
1800                 }
1801         } else if (compound->packed && warning.packed) {
1802                 warningf(&compound->base.source_position,
1803                          "superfluous packed attribute on '%T'", type);
1804         }
1805
1806         compound->size      = offset;
1807         compound->alignment = alignment;
1808         compound->layouted  = true;
1809 }
1810
1811 void layout_union_type(compound_type_t *type)
1812 {
1813         assert(type->compound != NULL);
1814
1815         compound_t *compound = type->compound;
1816         if (! compound->complete)
1817                 return;
1818
1819         il_size_t      size      = 0;
1820         il_alignment_t alignment = compound->alignment;
1821
1822         entity_t *entry = compound->members.entities;
1823         for (; entry != NULL; entry = entry->base.next) {
1824                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1825                         continue;
1826
1827                 type_t *m_type = entry->declaration.type;
1828                 if (! is_type_valid(skip_typeref(m_type)))
1829                         continue;
1830
1831                 entry->compound_member.offset = 0;
1832                 il_size_t m_size = get_type_size(m_type);
1833                 if (m_size > size)
1834                         size = m_size;
1835                 il_alignment_t m_alignment = get_type_alignment(m_type);
1836                 if (m_alignment > alignment)
1837                         alignment = m_alignment;
1838         }
1839         size = (size + alignment - 1) & -alignment;
1840
1841         compound->size      = size;
1842         compound->alignment = alignment;
1843 }
1844
1845 static function_parameter_t *allocate_parameter(type_t *const type)
1846 {
1847         function_parameter_t *const param
1848                 = obstack_alloc(type_obst, sizeof(*param));
1849         memset(param, 0, sizeof(*param));
1850         param->type = type;
1851         return param;
1852 }
1853
1854 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1855                              type_t *argument_type2)
1856 {
1857         function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1858         function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1859         parameter1->next = parameter2;
1860
1861         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1862         type->function.return_type = return_type;
1863         type->function.parameters  = parameter1;
1864         type->function.linkage     = LINKAGE_C;
1865
1866         return identify_new_type(type);
1867 }
1868
1869 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1870 {
1871         function_parameter_t *const parameter = allocate_parameter(argument_type);
1872
1873         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1874         type->function.return_type = return_type;
1875         type->function.parameters  = parameter;
1876         type->function.linkage     = LINKAGE_C;
1877
1878         return identify_new_type(type);
1879 }
1880
1881 type_t *make_function_1_type_variadic(type_t *return_type,
1882                                       type_t *argument_type)
1883 {
1884         function_parameter_t *const parameter = allocate_parameter(argument_type);
1885
1886         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1887         type->function.return_type = return_type;
1888         type->function.parameters  = parameter;
1889         type->function.variadic    = true;
1890         type->function.linkage     = LINKAGE_C;
1891
1892         return identify_new_type(type);
1893 }
1894
1895 type_t *make_function_0_type(type_t *return_type)
1896 {
1897         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1898         type->function.return_type = return_type;
1899         type->function.parameters  = NULL;
1900         type->function.linkage     = LINKAGE_C;
1901
1902         return identify_new_type(type);
1903 }
1904
1905 type_t *make_function_type(type_t *return_type, int n_types,
1906                            type_t *const *argument_types,
1907                                                    decl_modifiers_t modifiers)
1908 {
1909         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
1910         type->function.return_type = return_type;
1911         type->function.modifiers  |= modifiers;
1912         type->function.linkage     = LINKAGE_C;
1913
1914         function_parameter_t *last  = NULL;
1915         for (int i = 0; i < n_types; ++i) {
1916                 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1917                 if (last == NULL) {
1918                         type->function.parameters = parameter;
1919                 } else {
1920                         last->next = parameter;
1921                 }
1922                 last = parameter;
1923         }
1924
1925         return identify_new_type(type);
1926 }
1927
1928 /**
1929  * Debug helper. Prints the given type to stdout.
1930  */
1931 static __attribute__((unused))
1932 void dbg_type(const type_t *type)
1933 {
1934         print_to_file(stderr);
1935         print_type(type);
1936         print_string("\n");
1937         fflush(stderr);
1938 }