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