add testcase for empty structs
[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 "symbol_t.h"
27 #include "type_hash.h"
28 #include "adt/error.h"
29 #include "lang_features.h"
30
31 static struct obstack   _type_obst;
32 static FILE            *out;
33 struct obstack         *type_obst                 = &_type_obst;
34 static int              type_visited              = 0;
35 static bool             print_implicit_array_size = false;
36
37 static void intern_print_type_pre(const type_t *type, bool top);
38 static void intern_print_type_post(const type_t *type, bool top);
39
40 typedef struct atomic_type_properties_t atomic_type_properties_t;
41 struct atomic_type_properties_t {
42         unsigned   size;              /**< type size in bytes */
43         unsigned   alignment;         /**< type alignment in bytes */
44         unsigned   flags;             /**< type flags from atomic_type_flag_t */
45 };
46
47 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
48         //ATOMIC_TYPE_INVALID = 0,
49         [ATOMIC_TYPE_VOID] = {
50                 .size       = 0,
51                 .alignment  = 0,
52                 .flags      = ATOMIC_TYPE_FLAG_NONE
53         },
54         [ATOMIC_TYPE_CHAR] = {
55                 .size       = 1,
56                 .alignment  = 1,
57                 /* signed flag will be set when known */
58                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
59         },
60         [ATOMIC_TYPE_SCHAR] = {
61                 .size       = 1,
62                 .alignment  = 1,
63                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
64                               | ATOMIC_TYPE_FLAG_SIGNED,
65         },
66         [ATOMIC_TYPE_UCHAR] = {
67                 .size       = 1,
68                 .alignment  = 1,
69                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
70         },
71         [ATOMIC_TYPE_SHORT] = {
72                 .size       = 2,
73                 .alignment  = 2,
74                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
75                               | ATOMIC_TYPE_FLAG_SIGNED
76         },
77         [ATOMIC_TYPE_USHORT] = {
78                 .size       = 2,
79                 .alignment  = 2,
80                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
81         },
82         [ATOMIC_TYPE_INT] = {
83                 .size       = (unsigned) -1,
84                 .alignment  = (unsigned) -1,
85                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
86                               | ATOMIC_TYPE_FLAG_SIGNED,
87         },
88         [ATOMIC_TYPE_UINT] = {
89                 .size       = (unsigned) -1,
90                 .alignment  = (unsigned) -1,
91                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
92         },
93         [ATOMIC_TYPE_LONG] = {
94                 .size       = (unsigned) -1,
95                 .alignment  = (unsigned) -1,
96                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
97                               | ATOMIC_TYPE_FLAG_SIGNED,
98         },
99         [ATOMIC_TYPE_ULONG] = {
100                 .size       = (unsigned) -1,
101                 .alignment  = (unsigned) -1,
102                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
103         },
104         [ATOMIC_TYPE_LONGLONG] = {
105                 .size       = (unsigned) -1,
106                 .alignment  = (unsigned) -1,
107                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
108                               | ATOMIC_TYPE_FLAG_SIGNED,
109         },
110         [ATOMIC_TYPE_ULONGLONG] = {
111                 .size       = (unsigned) -1,
112                 .alignment  = (unsigned) -1,
113                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
114         },
115         [ATOMIC_TYPE_BOOL] = {
116                 .size       = (unsigned) -1,
117                 .alignment  = (unsigned) -1,
118                 .flags      = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
119         },
120         [ATOMIC_TYPE_FLOAT] = {
121                 .size       = 4,
122                 .alignment  = (unsigned) -1,
123                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
124                               | ATOMIC_TYPE_FLAG_SIGNED,
125         },
126         [ATOMIC_TYPE_DOUBLE] = {
127                 .size       = 8,
128                 .alignment  = (unsigned) -1,
129                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
130                               | ATOMIC_TYPE_FLAG_SIGNED,
131         },
132         [ATOMIC_TYPE_LONG_DOUBLE] = {
133                 .size       = 12,
134                 .alignment  = (unsigned) -1,
135                 .flags      = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
136                               | ATOMIC_TYPE_FLAG_SIGNED,
137         },
138         /* complex and imaginary types are set in init_types */
139 };
140
141 void init_types(void)
142 {
143         obstack_init(type_obst);
144
145         atomic_type_properties_t *props = atomic_type_properties;
146
147         if (char_is_signed) {
148                 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
149         }
150
151         unsigned int_size   = machine_size < 32 ? 2 : 4;
152         unsigned long_size  = machine_size < 64 ? 4 : 8;
153         unsigned llong_size = machine_size < 32 ? 4 : 8;
154
155         props[ATOMIC_TYPE_INT].size            = int_size;
156         props[ATOMIC_TYPE_INT].alignment       = int_size;
157         props[ATOMIC_TYPE_UINT].size           = int_size;
158         props[ATOMIC_TYPE_UINT].alignment      = int_size;
159         props[ATOMIC_TYPE_LONG].size           = long_size;
160         props[ATOMIC_TYPE_LONG].alignment      = long_size;
161         props[ATOMIC_TYPE_ULONG].size          = long_size;
162         props[ATOMIC_TYPE_ULONG].alignment     = long_size;
163         props[ATOMIC_TYPE_LONGLONG].size       = llong_size;
164         props[ATOMIC_TYPE_LONGLONG].alignment  = llong_size;
165         props[ATOMIC_TYPE_ULONGLONG].size      = llong_size;
166         props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
167
168         /* TODO: backend specific, need a way to query the backend for this.
169          * The following are good settings for x86 */
170         props[ATOMIC_TYPE_FLOAT].alignment       = 4;
171         props[ATOMIC_TYPE_DOUBLE].alignment      = 4;
172         props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
173         props[ATOMIC_TYPE_LONGLONG].alignment    = 4;
174         props[ATOMIC_TYPE_ULONGLONG].alignment   = 4;
175
176         props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UINT];
177 }
178
179 void exit_types(void)
180 {
181         obstack_free(type_obst, NULL);
182 }
183
184 void type_set_output(FILE *stream)
185 {
186         out = stream;
187 }
188
189 void inc_type_visited(void)
190 {
191         type_visited++;
192 }
193
194 void print_type_qualifiers(type_qualifiers_t qualifiers)
195 {
196         if (qualifiers & TYPE_QUALIFIER_CONST)    fputs("const ",    out);
197         if (qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
198         if (qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
199 }
200
201 /**
202  * Prints the name of an atomic type kinds.
203  *
204  * @param kind  The type kind.
205  */
206 static
207 void print_atomic_kinds(atomic_type_kind_t kind)
208 {
209         const char *s = "INVALIDATOMIC";
210         switch(kind) {
211         case ATOMIC_TYPE_INVALID:                               break;
212         case ATOMIC_TYPE_VOID:        s = "void";               break;
213         case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
214         case ATOMIC_TYPE_CHAR:        s = "char";               break;
215         case ATOMIC_TYPE_SCHAR:       s = "signed char";        break;
216         case ATOMIC_TYPE_UCHAR:       s = "unsigned char";      break;
217         case ATOMIC_TYPE_INT:         s = "int";                break;
218         case ATOMIC_TYPE_UINT:        s = "unsigned int";       break;
219         case ATOMIC_TYPE_SHORT:       s = "short";              break;
220         case ATOMIC_TYPE_USHORT:      s = "unsigned short";     break;
221         case ATOMIC_TYPE_LONG:        s = "long";               break;
222         case ATOMIC_TYPE_ULONG:       s = "unsigned long";      break;
223         case ATOMIC_TYPE_LONGLONG:    s = "long long";          break;
224         case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long"; break;
225         case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";        break;
226         case ATOMIC_TYPE_FLOAT:       s = "float";              break;
227         case ATOMIC_TYPE_DOUBLE:      s = "double";             break;
228         }
229         fputs(s, out);
230 }
231
232 /**
233  * Prints the name of an atomic type.
234  *
235  * @param type  The type.
236  */
237 static
238 void print_atomic_type(const atomic_type_t *type)
239 {
240         print_type_qualifiers(type->base.qualifiers);
241         print_atomic_kinds(type->akind);
242 }
243
244 /**
245  * Prints the name of a complex type.
246  *
247  * @param type  The type.
248  */
249 static
250 void print_complex_type(const complex_type_t *type)
251 {
252         print_type_qualifiers(type->base.qualifiers);
253         fputs("_Complex ", out);
254         print_atomic_kinds(type->akind);
255 }
256
257 /**
258  * Prints the name of an imaginary type.
259  *
260  * @param type  The type.
261  */
262 static
263 void print_imaginary_type(const imaginary_type_t *type)
264 {
265         print_type_qualifiers(type->base.qualifiers);
266         fputs("_Imaginary ", out);
267         print_atomic_kinds(type->akind);
268 }
269
270 /**
271  * Print the first part (the prefix) of a type.
272  *
273  * @param type   The type to print.
274  * @param top    true, if this is the top type, false if it's an embedded type.
275  */
276 static void print_function_type_pre(const function_type_t *type, bool top)
277 {
278         print_type_qualifiers(type->base.qualifiers);
279
280         intern_print_type_pre(type->return_type, false);
281
282         switch (type->calling_convention) {
283         case CC_CDECL:
284                 fputs(" __cdecl ", out);
285                 break;
286         case CC_STDCALL:
287                 fputs(" __stdcall ", out);
288                 break;
289         case CC_FASTCALL:
290                 fputs(" __fastcall ", out);
291                 break;
292         case CC_THISCALL:
293                 fputs(" __thiscall ", out);
294                 break;
295         case CC_DEFAULT:
296                 break;
297         }
298
299         /* don't emit braces if we're the toplevel type... */
300         if (!top)
301                 fputc('(', out);
302 }
303
304 /**
305  * Print the second part (the postfix) of a type.
306  *
307  * @param type   The type to print.
308  * @param top    true, if this is the top type, false if it's an embedded type.
309  */
310 static void print_function_type_post(const function_type_t *type,
311                                      const scope_t *scope, bool top)
312 {
313         intern_print_type_post(type->return_type, false);
314
315         fputc('(', out);
316         bool first = true;
317         if (scope == NULL) {
318                 function_parameter_t *parameter = type->parameters;
319                 for( ; parameter != NULL; parameter = parameter->next) {
320                         if (first) {
321                                 first = false;
322                         } else {
323                                 fputs(", ", out);
324                         }
325                         print_type(parameter->type);
326                 }
327         } else {
328                 declaration_t *parameter = scope->declarations;
329                 for( ; parameter != NULL; parameter = parameter->next) {
330                         if (first) {
331                                 first = false;
332                         } else {
333                                 fputs(", ", out);
334                         }
335                         print_type_ext(parameter->type, parameter->symbol,
336                                        &parameter->scope);
337                 }
338         }
339         if (type->variadic) {
340                 if (first) {
341                         first = false;
342                 } else {
343                         fputs(", ", out);
344                 }
345                 fputs("...", out);
346         }
347         if (first && !type->unspecified_parameters) {
348                 fputs("void", out);
349         }
350         fputc(')', out);
351
352         /* don't emit braces if we're the toplevel type... */
353         if (!top)
354                 fputc(')', out);
355 }
356
357 /**
358  * Prints the prefix part of a pointer type.
359  *
360  * @param type   The pointer type.
361  */
362 static void print_pointer_type_pre(const pointer_type_t *type)
363 {
364         intern_print_type_pre(type->points_to, false);
365         fputs("*", out);
366         print_type_qualifiers(type->base.qualifiers);
367 }
368
369 /**
370  * Prints the postfix part of a pointer type.
371  *
372  * @param type   The pointer type.
373  */
374 static void print_pointer_type_post(const pointer_type_t *type)
375 {
376         intern_print_type_post(type->points_to, false);
377 }
378
379 /**
380  * Prints the prefix part of an array type.
381  *
382  * @param type   The array type.
383  */
384 static void print_array_type_pre(const array_type_t *type)
385 {
386         intern_print_type_pre(type->element_type, false);
387 }
388
389 /**
390  * Prints the postfix part of an array type.
391  *
392  * @param type   The array type.
393  */
394 static void print_array_type_post(const array_type_t *type)
395 {
396         fputc('[', out);
397         if (type->is_static) {
398                 fputs("static ", out);
399         }
400         print_type_qualifiers(type->base.qualifiers);
401         if (type->size_expression != NULL
402                         && (print_implicit_array_size || !type->has_implicit_size)) {
403                 print_expression(type->size_expression);
404         }
405         fputc(']', out);
406         intern_print_type_post(type->element_type, false);
407 }
408
409 /**
410  * Prints the postfix part of a bitfield type.
411  *
412  * @param type   The array type.
413  */
414 static void print_bitfield_type_post(const bitfield_type_t *type)
415 {
416         fputs(" : ", out);
417         print_expression(type->size);
418         intern_print_type_post(type->base_type, false);
419 }
420
421 /**
422  * Prints an enum definition.
423  *
424  * @param declaration  The enum's type declaration.
425  */
426 void print_enum_definition(const declaration_t *declaration)
427 {
428         fputs("{\n", out);
429
430         change_indent(1);
431
432         declaration_t *entry = declaration->next;
433         for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
434                entry = entry->next) {
435
436                 print_indent();
437                 fprintf(out, "%s", entry->symbol->string);
438                 if (entry->init.initializer != NULL) {
439                         fprintf(out, " = ");
440
441                         /* skip the implicit cast */
442                         expression_t *expression = entry->init.enum_value;
443                         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
444                                 expression = expression->unary.value;
445                         }
446                         print_expression(expression);
447                 }
448                 fprintf(out, ",\n");
449         }
450
451         change_indent(-1);
452         print_indent();
453         fputs("}", out);
454 }
455
456 /**
457  * Prints an enum type.
458  *
459  * @param type  The enum type.
460  */
461 static void print_type_enum(const enum_type_t *type)
462 {
463         print_type_qualifiers(type->base.qualifiers);
464         fputs("enum ", out);
465
466         declaration_t *declaration = type->declaration;
467         symbol_t      *symbol      = declaration->symbol;
468         if (symbol != NULL) {
469                 fputs(symbol->string, out);
470         } else {
471                 print_enum_definition(declaration);
472         }
473 }
474
475 /**
476  * Print the compound part of a compound type.
477  *
478  * @param declaration  The declaration of the compound type.
479  */
480 void print_compound_definition(const declaration_t *declaration)
481 {
482         fputs("{\n", out);
483         change_indent(1);
484
485         declaration_t *iter = declaration->scope.declarations;
486         for( ; iter != NULL; iter = iter->next) {
487                 print_indent();
488                 print_declaration(iter);
489                 fputc('\n', out);
490         }
491
492         change_indent(-1);
493         print_indent();
494         fputs("}", out);
495 }
496
497 /**
498  * Prints a compound type.
499  *
500  * @param type  The compound type.
501  */
502 static void print_compound_type(const compound_type_t *type)
503 {
504         print_type_qualifiers(type->base.qualifiers);
505
506         if (type->base.kind == TYPE_COMPOUND_STRUCT) {
507                 fputs("struct ", out);
508         } else {
509                 assert(type->base.kind == TYPE_COMPOUND_UNION);
510                 fputs("union ", out);
511         }
512
513         declaration_t *declaration = type->declaration;
514         symbol_t      *symbol      = declaration->symbol;
515         if (symbol != NULL) {
516                 fputs(symbol->string, out);
517         } else {
518                 print_compound_definition(declaration);
519         }
520 }
521
522 /**
523  * Prints the prefix part of a typedef type.
524  *
525  * @param type   The typedef type.
526  */
527 static void print_typedef_type_pre(const typedef_type_t *const type)
528 {
529         print_type_qualifiers(type->base.qualifiers);
530         fputs(type->declaration->symbol->string, out);
531 }
532
533 /**
534  * Prints the prefix part of a typeof type.
535  *
536  * @param type   The typeof type.
537  */
538 static void print_typeof_type_pre(const typeof_type_t *const type)
539 {
540         fputs("typeof(", out);
541         if (type->expression != NULL) {
542                 assert(type->typeof_type == NULL);
543                 print_expression(type->expression);
544         } else {
545                 print_type(type->typeof_type);
546         }
547         fputc(')', out);
548 }
549
550 /**
551  * Prints the prefix part of a type.
552  *
553  * @param type   The type.
554  * @param top    true if we print the toplevel type, false else.
555  */
556 static void intern_print_type_pre(const type_t *const type, const bool top)
557 {
558         switch(type->kind) {
559         case TYPE_ERROR:
560                 fputs("<error>", out);
561                 return;
562         case TYPE_INVALID:
563                 fputs("<invalid>", out);
564                 return;
565         case TYPE_ENUM:
566                 print_type_enum(&type->enumt);
567                 return;
568         case TYPE_ATOMIC:
569                 print_atomic_type(&type->atomic);
570                 return;
571         case TYPE_COMPLEX:
572                 print_complex_type(&type->complex);
573                 return;
574         case TYPE_IMAGINARY:
575                 print_imaginary_type(&type->imaginary);
576                 return;
577         case TYPE_COMPOUND_STRUCT:
578         case TYPE_COMPOUND_UNION:
579                 print_compound_type(&type->compound);
580                 return;
581         case TYPE_BUILTIN:
582                 fputs(type->builtin.symbol->string, out);
583                 return;
584         case TYPE_FUNCTION:
585                 print_function_type_pre(&type->function, top);
586                 return;
587         case TYPE_POINTER:
588                 print_pointer_type_pre(&type->pointer);
589                 return;
590         case TYPE_BITFIELD:
591                 intern_print_type_pre(type->bitfield.base_type, top);
592                 return;
593         case TYPE_ARRAY:
594                 print_array_type_pre(&type->array);
595                 return;
596         case TYPE_TYPEDEF:
597                 print_typedef_type_pre(&type->typedeft);
598                 return;
599         case TYPE_TYPEOF:
600                 print_typeof_type_pre(&type->typeoft);
601                 return;
602         }
603         fputs("unknown", out);
604 }
605
606 /**
607  * Prints the postfix part of a type.
608  *
609  * @param type   The type.
610  * @param top    true if we print the toplevel type, false else.
611  */
612 static void intern_print_type_post(const type_t *const type, const bool top)
613 {
614         switch(type->kind) {
615         case TYPE_FUNCTION:
616                 print_function_type_post(&type->function, NULL, top);
617                 return;
618         case TYPE_POINTER:
619                 print_pointer_type_post(&type->pointer);
620                 return;
621         case TYPE_ARRAY:
622                 print_array_type_post(&type->array);
623                 return;
624         case TYPE_BITFIELD:
625                 print_bitfield_type_post(&type->bitfield);
626                 return;
627         case TYPE_ERROR:
628         case TYPE_INVALID:
629         case TYPE_ATOMIC:
630         case TYPE_COMPLEX:
631         case TYPE_IMAGINARY:
632         case TYPE_ENUM:
633         case TYPE_COMPOUND_STRUCT:
634         case TYPE_COMPOUND_UNION:
635         case TYPE_BUILTIN:
636         case TYPE_TYPEOF:
637         case TYPE_TYPEDEF:
638                 break;
639         }
640 }
641
642 /**
643  * Prints a type.
644  *
645  * @param type   The type.
646  */
647 void print_type(const type_t *const type)
648 {
649         print_type_ext(type, NULL, NULL);
650 }
651
652 void print_type_ext(const type_t *const type, const symbol_t *symbol,
653                     const scope_t *scope)
654 {
655         if (type == NULL) {
656                 fputs("nil type", out);
657                 return;
658         }
659
660         intern_print_type_pre(type, true);
661         if (symbol != NULL) {
662                 fputc(' ', out);
663                 fputs(symbol->string, out);
664         }
665         if (type->kind == TYPE_FUNCTION) {
666                 print_function_type_post(&type->function, scope, true);
667         } else {
668                 intern_print_type_post(type, true);
669         }
670 }
671
672 /**
673  * Return the size of a type AST node.
674  *
675  * @param type  The type.
676  */
677 static size_t get_type_size(const type_t *type)
678 {
679         switch(type->kind) {
680         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
681         case TYPE_COMPLEX:         return sizeof(complex_type_t);
682         case TYPE_IMAGINARY:       return sizeof(imaginary_type_t);
683         case TYPE_COMPOUND_STRUCT:
684         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
685         case TYPE_ENUM:            return sizeof(enum_type_t);
686         case TYPE_FUNCTION:        return sizeof(function_type_t);
687         case TYPE_POINTER:         return sizeof(pointer_type_t);
688         case TYPE_ARRAY:           return sizeof(array_type_t);
689         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
690         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
691         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
692         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
693         case TYPE_ERROR:           panic("error type found");
694         case TYPE_INVALID:         panic("invalid type found");
695         }
696         panic("unknown type found");
697 }
698
699 /**
700  * Duplicates a type.
701  *
702  * @param type  The type to copy.
703  * @return A copy of the type.
704  *
705  * @note This does not produce a deep copy!
706  */
707 type_t *duplicate_type(const type_t *type)
708 {
709         size_t size = get_type_size(type);
710
711         type_t *copy = obstack_alloc(type_obst, size);
712         memcpy(copy, type, size);
713
714         return copy;
715 }
716
717 /**
718  * Returns the unqualified type of a given type.
719  *
720  * @param type  The type.
721  * @returns The unqualified type.
722  */
723 type_t *get_unqualified_type(type_t *type)
724 {
725         if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
726                 return type;
727
728         type_t *unqualified_type          = duplicate_type(type);
729         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
730
731         type_t *result = typehash_insert(unqualified_type);
732         if (result != unqualified_type) {
733                 obstack_free(type_obst, unqualified_type);
734         }
735
736         return result;
737 }
738
739 /**
740  * Check if a type is valid.
741  *
742  * @param type  The type to check.
743  * @return true if type represents a valid type.
744  */
745 bool type_valid(const type_t *type)
746 {
747         return type->kind != TYPE_INVALID;
748 }
749
750 static bool test_atomic_type_flag(atomic_type_kind_t kind,
751                                   atomic_type_flag_t flag)
752 {
753         assert(kind <= ATOMIC_TYPE_LAST);
754         return (atomic_type_properties[kind].flags & flag) != 0;
755 }
756
757 /**
758  * Returns true if the given type is an integer type.
759  *
760  * @param type  The type to check.
761  * @return True if type is an integer type.
762  */
763 bool is_type_integer(const type_t *type)
764 {
765         assert(!is_typeref(type));
766
767         if (type->kind == TYPE_ENUM)
768                 return true;
769         if (type->kind == TYPE_BITFIELD)
770                 return true;
771
772         if (type->kind != TYPE_ATOMIC)
773                 return false;
774
775         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
776 }
777
778 /**
779  * Returns true if the given type is an enum type.
780  *
781  * @param type  The type to check.
782  * @return True if type is an enum type.
783  */
784 bool is_type_enum(const type_t *type)
785 {
786         assert(!is_typeref(type));
787         return type->kind == TYPE_ENUM;
788 }
789
790 /**
791  * Returns true if the given type is an floating point type.
792  *
793  * @param type  The type to check.
794  * @return True if type is a floating point type.
795  */
796 bool is_type_float(const type_t *type)
797 {
798         assert(!is_typeref(type));
799
800         if (type->kind != TYPE_ATOMIC)
801                 return false;
802
803         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
804 }
805
806 /**
807  * Returns true if the given type is a signed type.
808  *
809  * @param type  The type to check.
810  * @return True if type is a signed type.
811  */
812 bool is_type_signed(const type_t *type)
813 {
814         assert(!is_typeref(type));
815
816         /* enum types are int for now */
817         if (type->kind == TYPE_ENUM)
818                 return true;
819         if (type->kind == TYPE_BITFIELD)
820                 return is_type_signed(type->bitfield.base_type);
821
822         if (type->kind != TYPE_ATOMIC)
823                 return false;
824
825         return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
826 }
827
828 /**
829  * Returns true if the given type represents an arithmetic type.
830  *
831  * @param type  The type to check.
832  * @return True if type represents an arithmetic type.
833  */
834 bool is_type_arithmetic(const type_t *type)
835 {
836         assert(!is_typeref(type));
837
838         switch(type->kind) {
839         case TYPE_BITFIELD:
840         case TYPE_ENUM:
841                 return true;
842         case TYPE_ATOMIC:
843                 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
844         case TYPE_COMPLEX:
845                 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
846         case TYPE_IMAGINARY:
847                 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
848         default:
849                 return false;
850         }
851 }
852
853 /**
854  * Returns true if the given type is an integer or float type.
855  *
856  * @param type  The type to check.
857  * @return True if type is an integer or float type.
858  */
859 bool is_type_real(const type_t *type)
860 {
861         /* 6.2.5.17 */
862         return is_type_integer(type)
863                 || (type->kind == TYPE_ATOMIC && is_type_float(type));
864 }
865
866 /**
867  * Returns true if the given type represents a scalar type.
868  *
869  * @param type  The type to check.
870  * @return True if type represents a scalar type.
871  */
872 bool is_type_scalar(const type_t *type)
873 {
874         assert(!is_typeref(type));
875
876         switch (type->kind) {
877                 case TYPE_POINTER: return true;
878                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
879                 default:           break;
880         }
881
882         return is_type_arithmetic(type);
883 }
884
885 /**
886  * Check if a given type is incomplete.
887  *
888  * @param type  The type to check.
889  * @return True if the given type is incomplete (ie. just forward).
890  */
891 bool is_type_incomplete(const type_t *type)
892 {
893         assert(!is_typeref(type));
894
895         switch(type->kind) {
896         case TYPE_COMPOUND_STRUCT:
897         case TYPE_COMPOUND_UNION: {
898                 const compound_type_t *compound_type = &type->compound;
899                 declaration_t         *declaration   = compound_type->declaration;
900                 return !declaration->init.complete;
901         }
902         case TYPE_ENUM: {
903                 const enum_type_t *enum_type   = &type->enumt;
904                 declaration_t     *declaration = enum_type->declaration;
905                 return !declaration->init.complete;
906         }
907
908         case TYPE_ARRAY:
909                 return type->array.size_expression == NULL
910                         && !type->array.size_constant;
911
912         case TYPE_ATOMIC:
913                 return type->atomic.akind == ATOMIC_TYPE_VOID;
914
915         case TYPE_COMPLEX:
916                 return type->complex.akind == ATOMIC_TYPE_VOID;
917
918         case TYPE_IMAGINARY:
919                 return type->imaginary.akind == ATOMIC_TYPE_VOID;
920
921         case TYPE_BITFIELD:
922         case TYPE_FUNCTION:
923         case TYPE_POINTER:
924         case TYPE_BUILTIN:
925         case TYPE_ERROR:
926                 return false;
927
928         case TYPE_TYPEDEF:
929         case TYPE_TYPEOF:
930                 panic("is_type_incomplete called without typerefs skipped");
931         case TYPE_INVALID:
932                 break;
933         }
934
935         panic("invalid type found");
936 }
937
938 bool is_type_object(const type_t *type)
939 {
940         return !is_type_function(type) && !is_type_incomplete(type);
941 }
942
943 /**
944  * Check if two function types are compatible.
945  */
946 static bool function_types_compatible(const function_type_t *func1,
947                                       const function_type_t *func2)
948 {
949         const type_t* const ret1 = skip_typeref(func1->return_type);
950         const type_t* const ret2 = skip_typeref(func2->return_type);
951         if (!types_compatible(ret1, ret2))
952                 return false;
953
954         /* can parameters be compared? */
955         if (func1->unspecified_parameters || func2->unspecified_parameters)
956                 return true;
957
958         if (func1->variadic != func2->variadic)
959                 return false;
960
961         if (func1->calling_convention != func2->calling_convention)
962                 return false;
963
964         /* TODO: handling of unspecified parameters not correct yet */
965
966         /* all argument types must be compatible */
967         function_parameter_t *parameter1 = func1->parameters;
968         function_parameter_t *parameter2 = func2->parameters;
969         for ( ; parameter1 != NULL && parameter2 != NULL;
970                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
971                 type_t *parameter1_type = skip_typeref(parameter1->type);
972                 type_t *parameter2_type = skip_typeref(parameter2->type);
973
974                 parameter1_type = get_unqualified_type(parameter1_type);
975                 parameter2_type = get_unqualified_type(parameter2_type);
976
977                 if (!types_compatible(parameter1_type, parameter2_type))
978                         return false;
979         }
980         /* same number of arguments? */
981         if (parameter1 != NULL || parameter2 != NULL)
982                 return false;
983
984         return true;
985 }
986
987 /**
988  * Check if two array types are compatible.
989  */
990 static bool array_types_compatible(const array_type_t *array1,
991                                    const array_type_t *array2)
992 {
993         type_t *element_type1 = skip_typeref(array1->element_type);
994         type_t *element_type2 = skip_typeref(array2->element_type);
995         if (!types_compatible(element_type1, element_type2))
996                 return false;
997
998         if (!array1->size_constant || !array2->size_constant)
999                 return true;
1000
1001         return array1->size == array2->size;
1002 }
1003
1004 /**
1005  * Check if two types are compatible.
1006  */
1007 bool types_compatible(const type_t *type1, const type_t *type2)
1008 {
1009         assert(!is_typeref(type1));
1010         assert(!is_typeref(type2));
1011
1012         /* shortcut: the same type is always compatible */
1013         if (type1 == type2)
1014                 return true;
1015
1016         if (type1->base.qualifiers != type2->base.qualifiers)
1017                 return false;
1018         if (type1->kind != type2->kind)
1019                 return false;
1020
1021         switch(type1->kind) {
1022         case TYPE_FUNCTION:
1023                 return function_types_compatible(&type1->function, &type2->function);
1024         case TYPE_ATOMIC:
1025                 return type1->atomic.akind == type2->atomic.akind;
1026         case TYPE_COMPLEX:
1027                 return type1->complex.akind == type2->complex.akind;
1028         case TYPE_IMAGINARY:
1029                 return type1->imaginary.akind == type2->imaginary.akind;
1030         case TYPE_ARRAY:
1031                 return array_types_compatible(&type1->array, &type2->array);
1032
1033         case TYPE_POINTER: {
1034                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1035                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1036                 return types_compatible(to1, to2);
1037         }
1038
1039         case TYPE_COMPOUND_STRUCT:
1040         case TYPE_COMPOUND_UNION:
1041         case TYPE_ENUM:
1042         case TYPE_BUILTIN:
1043                 /* TODO: not implemented */
1044                 break;
1045
1046         case TYPE_BITFIELD:
1047                 /* not sure if this makes sense or is even needed, implement it if you
1048                  * really need it! */
1049                 panic("type compatibility check for bitfield type");
1050
1051         case TYPE_ERROR:
1052                 /* Hmm, the error type should be compatible to all other types */
1053                 return true;
1054         case TYPE_INVALID:
1055                 panic("invalid type found in compatible types");
1056         case TYPE_TYPEDEF:
1057         case TYPE_TYPEOF:
1058                 panic("typerefs not skipped in compatible types?!?");
1059         }
1060
1061         /* TODO: incomplete */
1062         return false;
1063 }
1064
1065 /**
1066  * Skip all typerefs and return the underlying type.
1067  */
1068 type_t *skip_typeref(type_t *type)
1069 {
1070         type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1071         type_modifiers_t  modifiers  = TYPE_MODIFIER_NONE;
1072
1073         while(true) {
1074                 switch(type->kind) {
1075                 case TYPE_ERROR:
1076                         return type;
1077                 case TYPE_TYPEDEF: {
1078                         qualifiers |= type->base.qualifiers;
1079                         modifiers  |= type->base.modifiers;
1080                         const typedef_type_t *typedef_type = &type->typedeft;
1081                         if (typedef_type->resolved_type != NULL) {
1082                                 type = typedef_type->resolved_type;
1083                                 break;
1084                         }
1085                         type = typedef_type->declaration->type;
1086                         continue;
1087                 }
1088                 case TYPE_TYPEOF: {
1089                         const typeof_type_t *typeof_type = &type->typeoft;
1090                         if (typeof_type->typeof_type != NULL) {
1091                                 type = typeof_type->typeof_type;
1092                         } else {
1093                                 type = typeof_type->expression->base.type;
1094                         }
1095                         continue;
1096                 }
1097                 default:
1098                         break;
1099                 }
1100                 break;
1101         }
1102
1103         if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1104                 type_t *const copy = duplicate_type(type);
1105
1106                 /* for const with typedefed array type the element type has to be
1107                  * adjusted */
1108                 if (is_type_array(copy)) {
1109                         type_t *element_type           = copy->array.element_type;
1110                         element_type                   = duplicate_type(element_type);
1111                         element_type->base.qualifiers |= qualifiers;
1112                         element_type->base.modifiers  |= modifiers;
1113                         copy->array.element_type       = element_type;
1114                 } else {
1115                         copy->base.qualifiers |= qualifiers;
1116                         copy->base.modifiers  |= modifiers;
1117                 }
1118
1119                 type = typehash_insert(copy);
1120                 if (type != copy) {
1121                         obstack_free(type_obst, copy);
1122                 }
1123         }
1124
1125         return type;
1126 }
1127
1128 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1129 {
1130         assert(kind <= ATOMIC_TYPE_LAST);
1131         return atomic_type_properties[kind].size;
1132 }
1133
1134 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1135 {
1136         assert(kind <= ATOMIC_TYPE_LAST);
1137         return atomic_type_properties[kind].alignment;
1138 }
1139
1140 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1141 {
1142         assert(kind <= ATOMIC_TYPE_LAST);
1143         return atomic_type_properties[kind].flags;
1144 }
1145
1146 atomic_type_kind_t get_intptr_kind(void)
1147 {
1148         if (machine_size <= 32)
1149                 return ATOMIC_TYPE_INT;
1150         else if (machine_size <= 64)
1151                 return ATOMIC_TYPE_LONG;
1152         else
1153                 return ATOMIC_TYPE_LONGLONG;
1154 }
1155
1156 atomic_type_kind_t get_uintptr_kind(void)
1157 {
1158         if (machine_size <= 32)
1159                 return ATOMIC_TYPE_UINT;
1160         else if (machine_size <= 64)
1161                 return ATOMIC_TYPE_ULONG;
1162         else
1163                 return ATOMIC_TYPE_ULONGLONG;
1164 }
1165
1166 /**
1167  * Find the atomic type kind representing a given size (signed).
1168  */
1169 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1170         static atomic_type_kind_t kinds[32];
1171
1172         assert(size < 32);
1173         atomic_type_kind_t kind = kinds[size];
1174         if (kind == ATOMIC_TYPE_INVALID) {
1175                 static const atomic_type_kind_t possible_kinds[] = {
1176                         ATOMIC_TYPE_SCHAR,
1177                         ATOMIC_TYPE_SHORT,
1178                         ATOMIC_TYPE_INT,
1179                         ATOMIC_TYPE_LONG,
1180                         ATOMIC_TYPE_LONGLONG
1181                 };
1182                 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1183                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1184                                 kind = possible_kinds[i];
1185                                 break;
1186                         }
1187                 }
1188                 kinds[size] = kind;
1189         }
1190         return kind;
1191 }
1192
1193 /**
1194  * Find the atomic type kind representing a given size (signed).
1195  */
1196 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1197         static atomic_type_kind_t kinds[32];
1198
1199         assert(size < 32);
1200         atomic_type_kind_t kind = kinds[size];
1201         if (kind == ATOMIC_TYPE_INVALID) {
1202                 static const atomic_type_kind_t possible_kinds[] = {
1203                         ATOMIC_TYPE_UCHAR,
1204                         ATOMIC_TYPE_USHORT,
1205                         ATOMIC_TYPE_UINT,
1206                         ATOMIC_TYPE_ULONG,
1207                         ATOMIC_TYPE_ULONGLONG
1208                 };
1209                 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1210                         if (get_atomic_type_size(possible_kinds[i]) == size) {
1211                                 kind = possible_kinds[i];
1212                                 break;
1213                         }
1214                 }
1215                 kinds[size] = kind;
1216         }
1217         return kind;
1218 }
1219
1220 /**
1221  * Hash the given type and return the "singleton" version
1222  * of it.
1223  */
1224 static type_t *identify_new_type(type_t *type)
1225 {
1226         type_t *result = typehash_insert(type);
1227         if (result != type) {
1228                 obstack_free(type_obst, type);
1229         }
1230         return result;
1231 }
1232
1233 /**
1234  * Creates a new atomic type.
1235  *
1236  * @param akind       The kind of the atomic type.
1237  * @param qualifiers  Type qualifiers for the new type.
1238  */
1239 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1240 {
1241         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1242         memset(type, 0, sizeof(atomic_type_t));
1243
1244         type->kind            = TYPE_ATOMIC;
1245         type->base.qualifiers = qualifiers;
1246         type->base.alignment  = get_atomic_type_alignment(akind);
1247         type->atomic.akind    = akind;
1248
1249         return identify_new_type(type);
1250 }
1251
1252 /**
1253  * Creates a new complex type.
1254  *
1255  * @param akind       The kind of the atomic type.
1256  * @param qualifiers  Type qualifiers for the new type.
1257  */
1258 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1259 {
1260         type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1261         memset(type, 0, sizeof(complex_type_t));
1262
1263         type->kind            = TYPE_COMPLEX;
1264         type->base.qualifiers = qualifiers;
1265         type->base.alignment  = get_atomic_type_alignment(akind);
1266         type->complex.akind   = akind;
1267
1268         return identify_new_type(type);
1269 }
1270
1271 /**
1272  * Creates a new imaginary type.
1273  *
1274  * @param akind       The kind of the atomic type.
1275  * @param qualifiers  Type qualifiers for the new type.
1276  */
1277 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1278 {
1279         type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1280         memset(type, 0, sizeof(imaginary_type_t));
1281
1282         type->kind            = TYPE_IMAGINARY;
1283         type->base.qualifiers = qualifiers;
1284         type->base.alignment  = get_atomic_type_alignment(akind);
1285         type->imaginary.akind = akind;
1286
1287         return identify_new_type(type);
1288 }
1289
1290 /**
1291  * Creates a new pointer type.
1292  *
1293  * @param points_to   The points-to type for the new type.
1294  * @param qualifiers  Type qualifiers for the new type.
1295  */
1296 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1297 {
1298         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1299         memset(type, 0, sizeof(pointer_type_t));
1300
1301         type->kind              = TYPE_POINTER;
1302         type->base.qualifiers   = qualifiers;
1303         type->base.alignment    = 0;
1304         type->pointer.points_to = points_to;
1305
1306         return identify_new_type(type);
1307 }
1308
1309 type_t *make_array_type(type_t *element_type, size_t size,
1310                         type_qualifiers_t qualifiers)
1311 {
1312         type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1313         memset(type, 0, sizeof(array_type_t));
1314
1315         type->kind                = TYPE_ARRAY;
1316         type->base.qualifiers     = qualifiers;
1317         type->base.alignment      = 0;
1318         type->array.element_type  = element_type;
1319         type->array.size          = size;
1320         type->array.size_constant = true;
1321
1322         return identify_new_type(type);
1323 }
1324
1325 /**
1326  * Debug helper. Prints the given type to stdout.
1327  */
1328 static __attribute__((unused))
1329 void dbg_type(const type_t *type)
1330 {
1331         FILE *old_out = out;
1332         out = stderr;
1333         print_type(type);
1334         puts("\n");
1335         fflush(stderr);
1336         out = old_out;
1337 }