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