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