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