2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
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.
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.
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
29 #include "type_hash.h"
30 #include "adt/error.h"
32 #include "lang_features.h"
34 #include "diagnostic.h"
35 #include "driver/firm_cmdline.h"
37 static struct obstack _type_obst;
39 struct obstack *type_obst = &_type_obst;
40 static bool print_implicit_array_size = false;
42 static void intern_print_type_pre(const type_t *type);
43 static void intern_print_type_post(const type_t *type);
45 typedef struct atomic_type_properties_t atomic_type_properties_t;
46 struct atomic_type_properties_t {
47 unsigned size; /**< type size in bytes */
48 unsigned alignment; /**< type alignment in bytes */
49 unsigned flags; /**< type flags from atomic_type_flag_t */
53 * Properties of atomic types.
55 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
56 //ATOMIC_TYPE_INVALID = 0,
57 [ATOMIC_TYPE_VOID] = {
60 .flags = ATOMIC_TYPE_FLAG_NONE
62 [ATOMIC_TYPE_WCHAR_T] = {
64 .alignment = (unsigned)-1,
65 /* signed flag will be set when known */
66 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
68 [ATOMIC_TYPE_CHAR] = {
71 /* signed flag will be set when known */
72 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
74 [ATOMIC_TYPE_SCHAR] = {
77 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
78 | ATOMIC_TYPE_FLAG_SIGNED,
80 [ATOMIC_TYPE_UCHAR] = {
83 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
85 [ATOMIC_TYPE_SHORT] = {
88 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
89 | ATOMIC_TYPE_FLAG_SIGNED
91 [ATOMIC_TYPE_USHORT] = {
94 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
97 .size = (unsigned) -1,
98 .alignment = (unsigned) -1,
99 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
100 | ATOMIC_TYPE_FLAG_SIGNED,
102 [ATOMIC_TYPE_UINT] = {
103 .size = (unsigned) -1,
104 .alignment = (unsigned) -1,
105 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
107 [ATOMIC_TYPE_LONG] = {
108 .size = (unsigned) -1,
109 .alignment = (unsigned) -1,
110 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
111 | ATOMIC_TYPE_FLAG_SIGNED,
113 [ATOMIC_TYPE_ULONG] = {
114 .size = (unsigned) -1,
115 .alignment = (unsigned) -1,
116 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
118 [ATOMIC_TYPE_LONGLONG] = {
119 .size = (unsigned) -1,
120 .alignment = (unsigned) -1,
121 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
122 | ATOMIC_TYPE_FLAG_SIGNED,
124 [ATOMIC_TYPE_ULONGLONG] = {
125 .size = (unsigned) -1,
126 .alignment = (unsigned) -1,
127 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
129 [ATOMIC_TYPE_BOOL] = {
130 .size = (unsigned) -1,
131 .alignment = (unsigned) -1,
132 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
134 [ATOMIC_TYPE_FLOAT] = {
136 .alignment = (unsigned) -1,
137 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
138 | ATOMIC_TYPE_FLAG_SIGNED,
140 [ATOMIC_TYPE_DOUBLE] = {
142 .alignment = (unsigned) -1,
143 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
144 | ATOMIC_TYPE_FLAG_SIGNED,
146 [ATOMIC_TYPE_LONG_DOUBLE] = {
148 .alignment = (unsigned) -1,
149 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
150 | ATOMIC_TYPE_FLAG_SIGNED,
152 /* complex and imaginary types are set in init_types */
155 void init_types(void)
157 obstack_init(type_obst);
159 atomic_type_properties_t *props = atomic_type_properties;
161 if (char_is_signed) {
162 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
165 unsigned int_size = machine_size < 32 ? 2 : 4;
166 unsigned long_size = machine_size < 64 ? 4 : 8;
167 unsigned llong_size = machine_size < 32 ? 4 : 8;
169 props[ATOMIC_TYPE_INT].size = int_size;
170 props[ATOMIC_TYPE_INT].alignment = int_size;
171 props[ATOMIC_TYPE_UINT].size = int_size;
172 props[ATOMIC_TYPE_UINT].alignment = int_size;
173 props[ATOMIC_TYPE_LONG].size = long_size;
174 props[ATOMIC_TYPE_LONG].alignment = long_size;
175 props[ATOMIC_TYPE_ULONG].size = long_size;
176 props[ATOMIC_TYPE_ULONG].alignment = long_size;
177 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
178 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
179 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
180 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
182 /* TODO: backend specific, need a way to query the backend for this.
183 * The following are good settings for x86 */
184 props[ATOMIC_TYPE_FLOAT].alignment = 4;
185 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
186 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
187 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
188 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
189 if (firm_opt.os_support == OS_SUPPORT_MACHO) {
190 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
191 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
194 /* TODO: make this configurable for platforms which do not use byte sized
196 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
198 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
201 void exit_types(void)
203 obstack_free(type_obst, NULL);
206 void type_set_output(FILE *stream)
211 void print_type_qualifiers(type_qualifiers_t qualifiers)
214 if (qualifiers & TYPE_QUALIFIER_CONST) {
215 fputs(" const" + first, out);
218 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
219 fputs(" volatile" + first, out);
222 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
223 fputs(" restrict" + first, out);
228 const char *get_atomic_kind_name(atomic_type_kind_t kind)
231 case ATOMIC_TYPE_INVALID: break;
232 case ATOMIC_TYPE_VOID: return "void";
233 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
234 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
235 case ATOMIC_TYPE_CHAR: return "char";
236 case ATOMIC_TYPE_SCHAR: return "signed char";
237 case ATOMIC_TYPE_UCHAR: return "unsigned char";
238 case ATOMIC_TYPE_INT: return "int";
239 case ATOMIC_TYPE_UINT: return "unsigned int";
240 case ATOMIC_TYPE_SHORT: return "short";
241 case ATOMIC_TYPE_USHORT: return "unsigned short";
242 case ATOMIC_TYPE_LONG: return "long";
243 case ATOMIC_TYPE_ULONG: return "unsigned long";
244 case ATOMIC_TYPE_LONGLONG: return "long long";
245 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
246 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
247 case ATOMIC_TYPE_FLOAT: return "float";
248 case ATOMIC_TYPE_DOUBLE: return "double";
250 return "INVALIDATOMIC";
254 * Prints the name of an atomic type kinds.
256 * @param kind The type kind.
258 static void print_atomic_kinds(atomic_type_kind_t kind)
260 const char *s = get_atomic_kind_name(kind);
265 * Prints the name of an atomic type.
267 * @param type The type.
269 static void print_atomic_type(const atomic_type_t *type)
271 print_type_qualifiers(type->base.qualifiers);
272 if (type->base.qualifiers != 0)
274 print_atomic_kinds(type->akind);
278 * Prints the name of a complex type.
280 * @param type The type.
283 void print_complex_type(const complex_type_t *type)
285 int empty = type->base.qualifiers == 0;
286 print_type_qualifiers(type->base.qualifiers);
287 fputs(" _Complex " + empty, out);
288 print_atomic_kinds(type->akind);
292 * Prints the name of an imaginary type.
294 * @param type The type.
297 void print_imaginary_type(const imaginary_type_t *type)
299 int empty = type->base.qualifiers == 0;
300 print_type_qualifiers(type->base.qualifiers);
301 fputs(" _Imaginary " + empty, out);
302 print_atomic_kinds(type->akind);
306 * Print the first part (the prefix) of a type.
308 * @param type The type to print.
310 static void print_function_type_pre(const function_type_t *type)
312 switch (type->linkage) {
313 case LINKAGE_INVALID:
318 fputs("extern \"C\" ", out);
322 if (!(c_mode & _CXX))
323 fputs("extern \"C++\" ", out);
327 print_type_qualifiers(type->base.qualifiers);
328 if (type->base.qualifiers != 0)
331 intern_print_type_pre(type->return_type);
333 switch (type->calling_convention) {
334 case CC_CDECL: fputs(" __cdecl", out); break;
335 case CC_STDCALL: fputs(" __stdcall", out); break;
336 case CC_FASTCALL: fputs(" __fastcall", out); break;
337 case CC_THISCALL: fputs(" __thiscall", out); break;
338 case CC_DEFAULT: break;
343 * Print the second part (the postfix) of a type.
345 * @param type The type to print.
347 static void print_function_type_post(const function_type_t *type,
348 const scope_t *parameters)
352 if (parameters == NULL) {
353 function_parameter_t *parameter = type->parameters;
354 for( ; parameter != NULL; parameter = parameter->next) {
360 print_type(parameter->type);
363 entity_t *parameter = parameters->entities;
364 for (; parameter != NULL; parameter = parameter->base.next) {
365 if (parameter->kind != ENTITY_PARAMETER)
373 const type_t *const type = parameter->declaration.type;
375 fputs(parameter->base.symbol->string, out);
377 print_type_ext(type, parameter->base.symbol, NULL);
381 if (type->variadic) {
389 if (first && !type->unspecified_parameters) {
394 intern_print_type_post(type->return_type);
398 * Prints the prefix part of a pointer type.
400 * @param type The pointer type.
402 static void print_pointer_type_pre(const pointer_type_t *type)
404 type_t const *const points_to = type->points_to;
405 intern_print_type_pre(points_to);
406 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
408 variable_t *const variable = type->base_variable;
409 if (variable != NULL) {
410 fputs(" __based(", out);
411 fputs(variable->base.base.symbol->string, out);
415 type_qualifiers_t const qual = type->base.qualifiers;
418 print_type_qualifiers(qual);
422 * Prints the postfix part of a pointer type.
424 * @param type The pointer type.
426 static void print_pointer_type_post(const pointer_type_t *type)
428 type_t const *const points_to = type->points_to;
429 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
431 intern_print_type_post(points_to);
435 * Prints the prefix part of a reference type.
437 * @param type The reference type.
439 static void print_reference_type_pre(const reference_type_t *type)
441 type_t const *const refers_to = type->refers_to;
442 intern_print_type_pre(refers_to);
443 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
449 * Prints the postfix part of a reference type.
451 * @param type The reference type.
453 static void print_reference_type_post(const reference_type_t *type)
455 type_t const *const refers_to = type->refers_to;
456 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
458 intern_print_type_post(refers_to);
462 * Prints the prefix part of an array type.
464 * @param type The array type.
466 static void print_array_type_pre(const array_type_t *type)
468 intern_print_type_pre(type->element_type);
472 * Prints the postfix part of an array type.
474 * @param type The array type.
476 static void print_array_type_post(const array_type_t *type)
479 if (type->is_static) {
480 fputs("static ", out);
482 print_type_qualifiers(type->base.qualifiers);
483 if (type->base.qualifiers != 0)
485 if (type->size_expression != NULL
486 && (print_implicit_array_size || !type->has_implicit_size)) {
487 print_expression(type->size_expression);
490 intern_print_type_post(type->element_type);
494 * Prints the postfix part of a bitfield type.
496 * @param type The array type.
498 static void print_bitfield_type_post(const bitfield_type_t *type)
501 print_expression(type->size_expression);
502 intern_print_type_post(type->base_type);
506 * Prints an enum definition.
508 * @param declaration The enum's type declaration.
510 void print_enum_definition(const enum_t *enume)
516 entity_t *entry = enume->base.next;
517 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
518 entry = entry->base.next) {
521 fputs(entry->base.symbol->string, out);
522 if (entry->enum_value.value != NULL) {
525 /* skip the implicit cast */
526 expression_t *expression = entry->enum_value.value;
527 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
528 expression = expression->unary.value;
530 print_expression(expression);
541 * Prints an enum type.
543 * @param type The enum type.
545 static void print_type_enum(const enum_type_t *type)
547 int empty = type->base.qualifiers == 0;
548 print_type_qualifiers(type->base.qualifiers);
549 fputs(" enum " + empty, out);
551 enum_t *enume = type->enume;
552 symbol_t *symbol = enume->base.symbol;
553 if (symbol != NULL) {
554 fputs(symbol->string, out);
556 print_enum_definition(enume);
561 * Print the compound part of a compound type.
563 void print_compound_definition(const compound_t *compound)
568 entity_t *entity = compound->members.entities;
569 for( ; entity != NULL; entity = entity->base.next) {
570 if (entity->kind != ENTITY_COMPOUND_MEMBER)
574 print_entity(entity);
581 if (compound->modifiers & DM_TRANSPARENT_UNION) {
582 fputs("__attribute__((__transparent_union__))", out);
587 * Prints a compound type.
589 * @param type The compound type.
591 static void print_compound_type(const compound_type_t *type)
593 int empty = type->base.qualifiers == 0;
594 print_type_qualifiers(type->base.qualifiers);
596 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
597 fputs(" struct " + empty, out);
599 assert(type->base.kind == TYPE_COMPOUND_UNION);
600 fputs(" union " + empty, out);
603 compound_t *compound = type->compound;
604 symbol_t *symbol = compound->base.symbol;
605 if (symbol != NULL) {
606 fputs(symbol->string, out);
608 print_compound_definition(compound);
613 * Prints the prefix part of a typedef type.
615 * @param type The typedef type.
617 static void print_typedef_type_pre(const typedef_type_t *const type)
619 print_type_qualifiers(type->base.qualifiers);
620 if (type->base.qualifiers != 0)
622 fputs(type->typedefe->base.symbol->string, out);
626 * Prints the prefix part of a typeof type.
628 * @param type The typeof type.
630 static void print_typeof_type_pre(const typeof_type_t *const type)
632 fputs("typeof(", out);
633 if (type->expression != NULL) {
634 print_expression(type->expression);
636 print_type(type->typeof_type);
642 * Prints the prefix part of a type.
644 * @param type The type.
646 static void intern_print_type_pre(const type_t *const type)
650 fputs("<error>", out);
653 fputs("<invalid>", out);
656 print_type_enum(&type->enumt);
659 print_atomic_type(&type->atomic);
662 print_complex_type(&type->complex);
665 print_imaginary_type(&type->imaginary);
667 case TYPE_COMPOUND_STRUCT:
668 case TYPE_COMPOUND_UNION:
669 print_compound_type(&type->compound);
672 fputs(type->builtin.symbol->string, out);
675 print_function_type_pre(&type->function);
678 print_pointer_type_pre(&type->pointer);
681 print_reference_type_pre(&type->reference);
684 intern_print_type_pre(type->bitfield.base_type);
687 print_array_type_pre(&type->array);
690 print_typedef_type_pre(&type->typedeft);
693 print_typeof_type_pre(&type->typeoft);
696 fputs("unknown", out);
700 * Prints the postfix part of a type.
702 * @param type The type.
704 static void intern_print_type_post(const type_t *const type)
708 print_function_type_post(&type->function, NULL);
711 print_pointer_type_post(&type->pointer);
714 print_reference_type_post(&type->reference);
717 print_array_type_post(&type->array);
720 print_bitfield_type_post(&type->bitfield);
728 case TYPE_COMPOUND_STRUCT:
729 case TYPE_COMPOUND_UNION:
740 * @param type The type.
742 void print_type(const type_t *const type)
744 print_type_ext(type, NULL, NULL);
747 void print_type_ext(const type_t *const type, const symbol_t *symbol,
748 const scope_t *parameters)
751 fputs("nil type", out);
755 intern_print_type_pre(type);
756 if (symbol != NULL) {
758 fputs(symbol->string, out);
760 if (type->kind == TYPE_FUNCTION) {
761 print_function_type_post(&type->function, parameters);
763 intern_print_type_post(type);
768 * Return the size of a type AST node.
770 * @param type The type.
772 static size_t get_type_struct_size(const type_t *type)
775 case TYPE_ATOMIC: return sizeof(atomic_type_t);
776 case TYPE_COMPLEX: return sizeof(complex_type_t);
777 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
778 case TYPE_COMPOUND_STRUCT:
779 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
780 case TYPE_ENUM: return sizeof(enum_type_t);
781 case TYPE_FUNCTION: return sizeof(function_type_t);
782 case TYPE_POINTER: return sizeof(pointer_type_t);
783 case TYPE_REFERENCE: return sizeof(reference_type_t);
784 case TYPE_ARRAY: return sizeof(array_type_t);
785 case TYPE_BUILTIN: return sizeof(builtin_type_t);
786 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
787 case TYPE_TYPEOF: return sizeof(typeof_type_t);
788 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
789 case TYPE_ERROR: panic("error type found");
790 case TYPE_INVALID: panic("invalid type found");
792 panic("unknown type found");
798 * @param type The type to copy.
799 * @return A copy of the type.
801 * @note This does not produce a deep copy!
803 type_t *duplicate_type(const type_t *type)
805 size_t size = get_type_struct_size(type);
807 type_t *copy = obstack_alloc(type_obst, size);
808 memcpy(copy, type, size);
809 copy->base.firm_type = NULL;
815 * Returns the unqualified type of a given type.
817 * @param type The type.
818 * @returns The unqualified type.
820 type_t *get_unqualified_type(type_t *type)
822 assert(!is_typeref(type));
824 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
827 type_t *unqualified_type = duplicate_type(type);
828 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
830 return identify_new_type(unqualified_type);
833 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
835 type_t *type = skip_typeref(orig_type);
838 if (is_type_array(type)) {
839 /* For array types the element type has to be adjusted */
840 type_t *element_type = type->array.element_type;
841 type_t *qual_element_type = get_qualified_type(element_type, qual);
843 if (qual_element_type == element_type)
846 copy = duplicate_type(type);
847 copy->array.element_type = qual_element_type;
848 } else if (is_type_valid(type)) {
849 if ((type->base.qualifiers & qual) == qual)
852 copy = duplicate_type(type);
853 copy->base.qualifiers |= qual;
858 return identify_new_type(copy);
862 * Check if a type is valid.
864 * @param type The type to check.
865 * @return true if type represents a valid type.
867 bool type_valid(const type_t *type)
869 return type->kind != TYPE_INVALID;
872 static bool test_atomic_type_flag(atomic_type_kind_t kind,
873 atomic_type_flag_t flag)
875 assert(kind <= ATOMIC_TYPE_LAST);
876 return (atomic_type_properties[kind].flags & flag) != 0;
880 * Returns true if the given type is an integer type.
882 * @param type The type to check.
883 * @return True if type is an integer type.
885 bool is_type_integer(const type_t *type)
887 assert(!is_typeref(type));
889 if (type->kind == TYPE_ENUM)
891 if (type->kind == TYPE_BITFIELD)
894 if (type->kind != TYPE_ATOMIC)
897 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
901 * Returns true if the given type is an enum type.
903 * @param type The type to check.
904 * @return True if type is an enum type.
906 bool is_type_enum(const type_t *type)
908 assert(!is_typeref(type));
909 return type->kind == TYPE_ENUM;
913 * Returns true if the given type is an floating point type.
915 * @param type The type to check.
916 * @return True if type is a floating point type.
918 bool is_type_float(const type_t *type)
920 assert(!is_typeref(type));
922 if (type->kind != TYPE_ATOMIC)
925 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
929 * Returns true if the given type is an complex type.
931 * @param type The type to check.
932 * @return True if type is a complex type.
934 bool is_type_complex(const type_t *type)
936 assert(!is_typeref(type));
938 if (type->kind != TYPE_ATOMIC)
941 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
945 * Returns true if the given type is a signed type.
947 * @param type The type to check.
948 * @return True if type is a signed type.
950 bool is_type_signed(const type_t *type)
952 assert(!is_typeref(type));
954 /* enum types are int for now */
955 if (type->kind == TYPE_ENUM)
957 if (type->kind == TYPE_BITFIELD)
958 return is_type_signed(type->bitfield.base_type);
960 if (type->kind != TYPE_ATOMIC)
963 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
967 * Returns true if the given type represents an arithmetic type.
969 * @param type The type to check.
970 * @return True if type represents an arithmetic type.
972 bool is_type_arithmetic(const type_t *type)
974 assert(!is_typeref(type));
981 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
983 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
985 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
992 * Returns true if the given type is an integer or float type.
994 * @param type The type to check.
995 * @return True if type is an integer or float type.
997 bool is_type_real(const type_t *type)
1000 return is_type_integer(type) || is_type_float(type);
1004 * Returns true if the given type represents a scalar type.
1006 * @param type The type to check.
1007 * @return True if type represents a scalar type.
1009 bool is_type_scalar(const type_t *type)
1011 assert(!is_typeref(type));
1013 switch (type->kind) {
1014 case TYPE_POINTER: return true;
1015 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1019 return is_type_arithmetic(type);
1023 * Check if a given type is incomplete.
1025 * @param type The type to check.
1026 * @return True if the given type is incomplete (ie. just forward).
1028 bool is_type_incomplete(const type_t *type)
1030 assert(!is_typeref(type));
1032 switch(type->kind) {
1033 case TYPE_COMPOUND_STRUCT:
1034 case TYPE_COMPOUND_UNION: {
1035 const compound_type_t *compound_type = &type->compound;
1036 return !compound_type->compound->complete;
1042 return type->array.size_expression == NULL
1043 && !type->array.size_constant;
1046 return type->atomic.akind == ATOMIC_TYPE_VOID;
1049 return type->complex.akind == ATOMIC_TYPE_VOID;
1051 case TYPE_IMAGINARY:
1052 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1057 case TYPE_REFERENCE:
1064 panic("is_type_incomplete called without typerefs skipped");
1069 panic("invalid type found");
1072 bool is_type_object(const type_t *type)
1074 return !is_type_function(type) && !is_type_incomplete(type);
1077 bool is_builtin_va_list(type_t *type)
1079 type_t *tp = skip_typeref(type);
1081 return tp->kind == type_valist->kind &&
1082 tp->builtin.symbol == type_valist->builtin.symbol;
1086 * Check if two function types are compatible.
1088 static bool function_types_compatible(const function_type_t *func1,
1089 const function_type_t *func2)
1091 const type_t* const ret1 = skip_typeref(func1->return_type);
1092 const type_t* const ret2 = skip_typeref(func2->return_type);
1093 if (!types_compatible(ret1, ret2))
1096 if (func1->linkage != func2->linkage)
1099 if (func1->calling_convention != func2->calling_convention)
1102 /* can parameters be compared? */
1103 if (func1->unspecified_parameters || func2->unspecified_parameters)
1106 if (func1->variadic != func2->variadic)
1109 /* TODO: handling of unspecified parameters not correct yet */
1111 /* all argument types must be compatible */
1112 function_parameter_t *parameter1 = func1->parameters;
1113 function_parameter_t *parameter2 = func2->parameters;
1114 for ( ; parameter1 != NULL && parameter2 != NULL;
1115 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1116 type_t *parameter1_type = skip_typeref(parameter1->type);
1117 type_t *parameter2_type = skip_typeref(parameter2->type);
1119 parameter1_type = get_unqualified_type(parameter1_type);
1120 parameter2_type = get_unqualified_type(parameter2_type);
1122 if (!types_compatible(parameter1_type, parameter2_type))
1125 /* same number of arguments? */
1126 if (parameter1 != NULL || parameter2 != NULL)
1133 * Check if two array types are compatible.
1135 static bool array_types_compatible(const array_type_t *array1,
1136 const array_type_t *array2)
1138 type_t *element_type1 = skip_typeref(array1->element_type);
1139 type_t *element_type2 = skip_typeref(array2->element_type);
1140 if (!types_compatible(element_type1, element_type2))
1143 if (!array1->size_constant || !array2->size_constant)
1146 return array1->size == array2->size;
1150 * Check if two types are compatible.
1152 bool types_compatible(const type_t *type1, const type_t *type2)
1154 assert(!is_typeref(type1));
1155 assert(!is_typeref(type2));
1157 /* shortcut: the same type is always compatible */
1161 if (!is_type_valid(type1) || !is_type_valid(type2))
1164 if (type1->base.qualifiers != type2->base.qualifiers)
1166 if (type1->kind != type2->kind)
1169 switch (type1->kind) {
1171 return function_types_compatible(&type1->function, &type2->function);
1173 return type1->atomic.akind == type2->atomic.akind;
1175 return type1->complex.akind == type2->complex.akind;
1176 case TYPE_IMAGINARY:
1177 return type1->imaginary.akind == type2->imaginary.akind;
1179 return array_types_compatible(&type1->array, &type2->array);
1181 case TYPE_POINTER: {
1182 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1183 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1184 return types_compatible(to1, to2);
1187 case TYPE_REFERENCE: {
1188 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1189 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1190 return types_compatible(to1, to2);
1193 case TYPE_COMPOUND_STRUCT:
1194 case TYPE_COMPOUND_UNION: {
1201 /* TODO: not implemented */
1205 /* not sure if this makes sense or is even needed, implement it if you
1206 * really need it! */
1207 panic("type compatibility check for bitfield type");
1210 /* Hmm, the error type should be compatible to all other types */
1213 panic("invalid type found in compatible types");
1216 panic("typerefs not skipped in compatible types?!?");
1219 /* TODO: incomplete */
1224 * Skip all typerefs and return the underlying type.
1226 type_t *skip_typeref(type_t *type)
1228 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1231 switch (type->kind) {
1234 case TYPE_TYPEDEF: {
1235 qualifiers |= type->base.qualifiers;
1237 const typedef_type_t *typedef_type = &type->typedeft;
1238 if (typedef_type->resolved_type != NULL) {
1239 type = typedef_type->resolved_type;
1242 type = typedef_type->typedefe->type;
1246 qualifiers |= type->base.qualifiers;
1247 type = type->typeoft.typeof_type;
1255 if (qualifiers != TYPE_QUALIFIER_NONE) {
1256 type_t *const copy = duplicate_type(type);
1258 /* for const with typedefed array type the element type has to be
1260 if (is_type_array(copy)) {
1261 type_t *element_type = copy->array.element_type;
1262 element_type = duplicate_type(element_type);
1263 element_type->base.qualifiers |= qualifiers;
1264 copy->array.element_type = element_type;
1266 copy->base.qualifiers |= qualifiers;
1269 type = identify_new_type(copy);
1275 unsigned get_type_size(type_t *type)
1277 switch (type->kind) {
1283 return get_atomic_type_size(type->atomic.akind);
1285 return get_atomic_type_size(type->complex.akind) * 2;
1286 case TYPE_IMAGINARY:
1287 return get_atomic_type_size(type->imaginary.akind);
1288 case TYPE_COMPOUND_UNION:
1289 layout_union_type(&type->compound);
1290 return type->compound.compound->size;
1291 case TYPE_COMPOUND_STRUCT:
1292 layout_struct_type(&type->compound);
1293 return type->compound.compound->size;
1295 return get_atomic_type_size(type->enumt.akind);
1297 return 0; /* non-const (but "address-const") */
1298 case TYPE_REFERENCE:
1300 /* TODO: make configurable by backend */
1303 /* TODO: correct if element_type is aligned? */
1304 il_size_t element_size = get_type_size(type->array.element_type);
1305 return type->array.size * element_size;
1310 return get_type_size(type->builtin.real_type);
1312 return get_type_size(type->typedeft.typedefe->type);
1314 if (type->typeoft.typeof_type) {
1315 return get_type_size(type->typeoft.typeof_type);
1317 return get_type_size(type->typeoft.expression->base.type);
1320 panic("invalid type in get_type_size");
1323 unsigned get_type_alignment(type_t *type)
1325 switch (type->kind) {
1331 return get_atomic_type_alignment(type->atomic.akind);
1333 return get_atomic_type_alignment(type->complex.akind);
1334 case TYPE_IMAGINARY:
1335 return get_atomic_type_alignment(type->imaginary.akind);
1336 case TYPE_COMPOUND_UNION:
1337 layout_union_type(&type->compound);
1338 return type->compound.compound->alignment;
1339 case TYPE_COMPOUND_STRUCT:
1340 layout_struct_type(&type->compound);
1341 return type->compound.compound->alignment;
1343 return get_atomic_type_alignment(type->enumt.akind);
1345 /* what is correct here? */
1347 case TYPE_REFERENCE:
1349 /* TODO: make configurable by backend */
1352 return get_type_alignment(type->array.element_type);
1356 return get_type_alignment(type->builtin.real_type);
1357 case TYPE_TYPEDEF: {
1358 il_alignment_t alignment
1359 = get_type_alignment(type->typedeft.typedefe->type);
1360 if (type->typedeft.typedefe->alignment > alignment)
1361 alignment = type->typedeft.typedefe->alignment;
1366 if (type->typeoft.typeof_type) {
1367 return get_type_alignment(type->typeoft.typeof_type);
1369 return get_type_alignment(type->typeoft.expression->base.type);
1372 panic("invalid type in get_type_alignment");
1375 decl_modifiers_t get_type_modifiers(const type_t *type)
1377 switch(type->kind) {
1381 case TYPE_COMPOUND_STRUCT:
1382 case TYPE_COMPOUND_UNION:
1383 return type->compound.compound->modifiers;
1385 return type->function.modifiers;
1389 case TYPE_IMAGINARY:
1390 case TYPE_REFERENCE:
1396 return get_type_modifiers(type->builtin.real_type);
1397 case TYPE_TYPEDEF: {
1398 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1399 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1403 if (type->typeoft.typeof_type) {
1404 return get_type_modifiers(type->typeoft.typeof_type);
1406 return get_type_modifiers(type->typeoft.expression->base.type);
1409 panic("invalid type found in get_type_modifiers");
1412 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1414 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1417 switch (type->base.kind) {
1419 return TYPE_QUALIFIER_NONE;
1421 qualifiers |= type->base.qualifiers;
1422 const typedef_type_t *typedef_type = &type->typedeft;
1423 if (typedef_type->resolved_type != NULL)
1424 type = typedef_type->resolved_type;
1426 type = typedef_type->typedefe->type;
1429 type = type->typeoft.typeof_type;
1432 if (skip_array_type) {
1433 type = type->array.element_type;
1442 return type->base.qualifiers | qualifiers;
1445 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1447 assert(kind <= ATOMIC_TYPE_LAST);
1448 return atomic_type_properties[kind].size;
1451 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1453 assert(kind <= ATOMIC_TYPE_LAST);
1454 return atomic_type_properties[kind].alignment;
1457 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1459 assert(kind <= ATOMIC_TYPE_LAST);
1460 return atomic_type_properties[kind].flags;
1463 atomic_type_kind_t get_intptr_kind(void)
1465 if (machine_size <= 32)
1466 return ATOMIC_TYPE_INT;
1467 else if (machine_size <= 64)
1468 return ATOMIC_TYPE_LONG;
1470 return ATOMIC_TYPE_LONGLONG;
1473 atomic_type_kind_t get_uintptr_kind(void)
1475 if (machine_size <= 32)
1476 return ATOMIC_TYPE_UINT;
1477 else if (machine_size <= 64)
1478 return ATOMIC_TYPE_ULONG;
1480 return ATOMIC_TYPE_ULONGLONG;
1484 * Find the atomic type kind representing a given size (signed).
1486 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1488 static atomic_type_kind_t kinds[32];
1491 atomic_type_kind_t kind = kinds[size];
1492 if (kind == ATOMIC_TYPE_INVALID) {
1493 static const atomic_type_kind_t possible_kinds[] = {
1498 ATOMIC_TYPE_LONGLONG
1500 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1501 if (get_atomic_type_size(possible_kinds[i]) == size) {
1502 kind = possible_kinds[i];
1512 * Find the atomic type kind representing a given size (signed).
1514 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1516 static atomic_type_kind_t kinds[32];
1519 atomic_type_kind_t kind = kinds[size];
1520 if (kind == ATOMIC_TYPE_INVALID) {
1521 static const atomic_type_kind_t possible_kinds[] = {
1526 ATOMIC_TYPE_ULONGLONG
1528 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1529 if (get_atomic_type_size(possible_kinds[i]) == size) {
1530 kind = possible_kinds[i];
1540 * Hash the given type and return the "singleton" version
1543 type_t *identify_new_type(type_t *type)
1545 type_t *result = typehash_insert(type);
1546 if (result != type) {
1547 obstack_free(type_obst, type);
1553 * Creates a new atomic type.
1555 * @param akind The kind of the atomic type.
1556 * @param qualifiers Type qualifiers for the new type.
1558 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1560 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1561 memset(type, 0, sizeof(atomic_type_t));
1563 type->kind = TYPE_ATOMIC;
1564 type->base.qualifiers = qualifiers;
1565 type->atomic.akind = akind;
1567 return identify_new_type(type);
1571 * Creates a new complex type.
1573 * @param akind The kind of the atomic type.
1574 * @param qualifiers Type qualifiers for the new type.
1576 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1578 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1579 memset(type, 0, sizeof(complex_type_t));
1581 type->kind = TYPE_COMPLEX;
1582 type->base.qualifiers = qualifiers;
1583 type->complex.akind = akind;
1585 return identify_new_type(type);
1589 * Creates a new imaginary type.
1591 * @param akind The kind of the atomic type.
1592 * @param qualifiers Type qualifiers for the new type.
1594 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1596 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1597 memset(type, 0, sizeof(imaginary_type_t));
1599 type->kind = TYPE_IMAGINARY;
1600 type->base.qualifiers = qualifiers;
1601 type->imaginary.akind = akind;
1603 return identify_new_type(type);
1607 * Creates a new pointer type.
1609 * @param points_to The points-to type for the new type.
1610 * @param qualifiers Type qualifiers for the new type.
1612 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1614 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1615 memset(type, 0, sizeof(pointer_type_t));
1617 type->kind = TYPE_POINTER;
1618 type->base.qualifiers = qualifiers;
1619 type->pointer.points_to = points_to;
1620 type->pointer.base_variable = NULL;
1622 return identify_new_type(type);
1626 * Creates a new reference type.
1628 * @param refers_to The referred-to type for the new type.
1630 type_t *make_reference_type(type_t *refers_to)
1632 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1633 memset(type, 0, sizeof(reference_type_t));
1635 type->kind = TYPE_REFERENCE;
1636 type->base.qualifiers = 0;
1637 type->reference.refers_to = refers_to;
1639 return identify_new_type(type);
1643 * Creates a new based pointer type.
1645 * @param points_to The points-to type for the new type.
1646 * @param qualifiers Type qualifiers for the new type.
1647 * @param variable The based variable
1649 type_t *make_based_pointer_type(type_t *points_to,
1650 type_qualifiers_t qualifiers, variable_t *variable)
1652 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1653 memset(type, 0, sizeof(pointer_type_t));
1655 type->kind = TYPE_POINTER;
1656 type->base.qualifiers = qualifiers;
1657 type->pointer.points_to = points_to;
1658 type->pointer.base_variable = variable;
1660 return identify_new_type(type);
1664 type_t *make_array_type(type_t *element_type, size_t size,
1665 type_qualifiers_t qualifiers)
1667 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1668 memset(type, 0, sizeof(array_type_t));
1670 type->kind = TYPE_ARRAY;
1671 type->base.qualifiers = qualifiers;
1672 type->array.element_type = element_type;
1673 type->array.size = size;
1674 type->array.size_constant = true;
1676 return identify_new_type(type);
1679 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1680 il_alignment_t *struct_alignment,
1681 bool packed, entity_t *first)
1683 il_size_t offset = *struct_offset;
1684 il_alignment_t alignment = *struct_alignment;
1685 size_t bit_offset = 0;
1688 for (member = first; member != NULL; member = member->base.next) {
1689 if (member->kind != ENTITY_COMPOUND_MEMBER)
1692 type_t *type = member->declaration.type;
1693 if (type->kind != TYPE_BITFIELD)
1696 type_t *base_type = skip_typeref(type->bitfield.base_type);
1697 il_alignment_t base_alignment = get_type_alignment(base_type);
1698 il_alignment_t alignment_mask = base_alignment-1;
1699 if (base_alignment > alignment)
1700 alignment = base_alignment;
1702 size_t bit_size = type->bitfield.bit_size;
1704 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1705 offset &= ~alignment_mask;
1706 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1708 if (bit_offset + bit_size > base_size || bit_size == 0) {
1709 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1710 offset = (offset + base_alignment-1) & ~alignment_mask;
1715 member->compound_member.offset = offset;
1716 member->compound_member.bit_offset = bit_offset;
1718 bit_offset += bit_size;
1719 offset += bit_offset / BITS_PER_BYTE;
1720 bit_offset %= BITS_PER_BYTE;
1726 *struct_offset = offset;
1727 *struct_alignment = alignment;
1733 * Finish the construction of a struct type by calculating its size, offsets,
1736 void layout_struct_type(compound_type_t *type)
1738 assert(type->compound != NULL);
1740 compound_t *compound = type->compound;
1741 if (!compound->complete)
1743 if (type->compound->layouted)
1746 il_size_t offset = 0;
1747 il_alignment_t alignment = compound->alignment;
1748 bool need_pad = false;
1750 entity_t *entry = compound->members.entities;
1751 while (entry != NULL) {
1752 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1753 entry = entry->base.next;
1757 type_t *m_type = entry->declaration.type;
1758 type_t *skipped = skip_typeref(m_type);
1759 if (! is_type_valid(skipped)) {
1760 entry = entry->base.next;
1764 if (skipped->kind == TYPE_BITFIELD) {
1765 entry = pack_bitfield_members(&offset, &alignment,
1766 compound->packed, entry);
1770 il_alignment_t m_alignment = get_type_alignment(m_type);
1771 if (m_alignment > alignment)
1772 alignment = m_alignment;
1774 if (!compound->packed) {
1775 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1777 if (new_offset > offset) {
1779 offset = new_offset;
1783 entry->compound_member.offset = offset;
1784 offset += get_type_size(m_type);
1786 entry = entry->base.next;
1789 if (!compound->packed) {
1790 il_size_t new_offset = (offset + alignment-1) & -alignment;
1791 if (new_offset > offset) {
1793 offset = new_offset;
1798 if (warning.padded) {
1799 warningf(&compound->base.source_position, "'%T' needs padding",
1802 } else if (compound->packed && warning.packed) {
1803 warningf(&compound->base.source_position,
1804 "superfluous packed attribute on '%T'", type);
1807 compound->size = offset;
1808 compound->alignment = alignment;
1809 compound->layouted = true;
1813 * Finish the construction of an union type by calculating
1814 * its size and alignment.
1816 void layout_union_type(compound_type_t *type)
1818 assert(type->compound != NULL);
1820 compound_t *compound = type->compound;
1821 if (! compound->complete)
1825 il_alignment_t alignment = compound->alignment;
1827 entity_t *entry = compound->members.entities;
1828 for (; entry != NULL; entry = entry->base.next) {
1829 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1832 type_t *m_type = entry->declaration.type;
1833 if (! is_type_valid(skip_typeref(m_type)))
1836 entry->compound_member.offset = 0;
1837 il_size_t m_size = get_type_size(m_type);
1840 il_alignment_t m_alignment = get_type_alignment(m_type);
1841 if (m_alignment > alignment)
1842 alignment = m_alignment;
1844 size = (size + alignment - 1) & -alignment;
1846 compound->size = size;
1847 compound->alignment = alignment;
1851 * Debug helper. Prints the given type to stdout.
1853 static __attribute__((unused))
1854 void dbg_type(const type_t *type)
1856 FILE *old_out = out;