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