2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
14 #include "type_hash.h"
15 #include "adt/error.h"
17 #include "lang_features.h"
19 #include "diagnostic.h"
21 #include "separator_t.h"
23 /** The default calling convention. */
24 cc_kind_t default_calling_convention = CC_CDECL;
26 static struct obstack type_obst;
27 static bool print_implicit_array_size = false;
29 static void intern_print_type_pre(const type_t *type);
30 static void intern_print_type_post(const type_t *type);
33 * Returns the size of a type node.
35 * @param kind the type kind
37 static size_t get_type_struct_size(type_kind_t kind)
39 static const size_t sizes[] = {
40 [TYPE_ATOMIC] = sizeof(atomic_type_t),
41 [TYPE_IMAGINARY] = sizeof(atomic_type_t),
42 [TYPE_COMPLEX] = sizeof(atomic_type_t),
43 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
44 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
45 [TYPE_ENUM] = sizeof(enum_type_t),
46 [TYPE_FUNCTION] = sizeof(function_type_t),
47 [TYPE_POINTER] = sizeof(pointer_type_t),
48 [TYPE_REFERENCE] = sizeof(reference_type_t),
49 [TYPE_ARRAY] = sizeof(array_type_t),
50 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
51 [TYPE_TYPEOF] = sizeof(typeof_type_t),
53 assert((size_t)kind < lengthof(sizes));
54 assert(sizes[kind] != 0);
58 type_t *allocate_type_zero(type_kind_t kind)
60 size_t const size = get_type_struct_size(kind);
61 type_t *const res = obstack_alloc(&type_obst, size);
63 res->base.kind = kind;
69 * Properties of atomic types.
71 atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
72 [ATOMIC_TYPE_VOID] = {
75 .flags = ATOMIC_TYPE_FLAG_NONE,
78 [ATOMIC_TYPE_BOOL] = {
81 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
84 [ATOMIC_TYPE_CHAR] = {
87 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
90 [ATOMIC_TYPE_SCHAR] = {
93 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
94 | ATOMIC_TYPE_FLAG_SIGNED,
97 [ATOMIC_TYPE_UCHAR] = {
100 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
103 [ATOMIC_TYPE_SHORT] = {
106 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
107 | ATOMIC_TYPE_FLAG_SIGNED,
110 [ATOMIC_TYPE_USHORT] = {
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
116 [ATOMIC_TYPE_INT] = {
117 .size = (unsigned) -1,
118 .alignment = (unsigned) -1,
119 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
120 | ATOMIC_TYPE_FLAG_SIGNED,
123 [ATOMIC_TYPE_UINT] = {
124 .size = (unsigned) -1,
125 .alignment = (unsigned) -1,
126 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
129 [ATOMIC_TYPE_LONG] = {
130 .size = (unsigned) -1,
131 .alignment = (unsigned) -1,
132 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
133 | ATOMIC_TYPE_FLAG_SIGNED,
136 [ATOMIC_TYPE_ULONG] = {
137 .size = (unsigned) -1,
138 .alignment = (unsigned) -1,
139 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
142 [ATOMIC_TYPE_LONGLONG] = {
145 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
146 | ATOMIC_TYPE_FLAG_SIGNED,
149 [ATOMIC_TYPE_ULONGLONG] = {
152 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
155 [ATOMIC_TYPE_FLOAT] = {
158 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
159 | ATOMIC_TYPE_FLAG_SIGNED,
162 [ATOMIC_TYPE_DOUBLE] = {
165 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
166 | ATOMIC_TYPE_FLAG_SIGNED,
169 [ATOMIC_TYPE_WCHAR_T] = {
170 .size = (unsigned)-1,
171 .alignment = (unsigned)-1,
172 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
173 .rank = (unsigned)-1,
176 atomic_type_properties_t pointer_properties = {
179 .flags = ATOMIC_TYPE_FLAG_NONE,
182 static inline bool is_po2(unsigned x)
184 return (x & (x-1)) == 0;
187 void init_types(unsigned machine_size)
189 obstack_init(&type_obst);
191 atomic_type_properties_t *props = atomic_type_properties;
193 /* atempt to set some sane defaults based on machine size */
195 unsigned int_size = machine_size < 32 ? 2 : 4;
196 unsigned long_size = machine_size < 64 ? 4 : 8;
198 props[ATOMIC_TYPE_INT].size = int_size;
199 props[ATOMIC_TYPE_INT].alignment = int_size;
200 props[ATOMIC_TYPE_UINT].size = int_size;
201 props[ATOMIC_TYPE_UINT].alignment = int_size;
202 props[ATOMIC_TYPE_LONG].size = long_size;
203 props[ATOMIC_TYPE_LONG].alignment = long_size;
204 props[ATOMIC_TYPE_ULONG].size = long_size;
205 props[ATOMIC_TYPE_ULONG].alignment = long_size;
207 pointer_properties.size = long_size;
208 pointer_properties.alignment = long_size;
209 pointer_properties.struct_alignment = long_size;
211 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
212 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
214 /* set struct alignments to the same value as alignment */
215 for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
216 props[i].struct_alignment = props[i].alignment;
220 void exit_types(void)
222 obstack_free(&type_obst, NULL);
225 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
227 size_t sep = q & QUAL_SEP_START ? 0 : 1;
228 if (qualifiers & TYPE_QUALIFIER_CONST) {
229 print_string(&" const"[sep]);
232 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
233 print_string(&" volatile"[sep]);
236 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
237 print_string(&" restrict"[sep]);
240 if (sep == 0 && q & QUAL_SEP_END)
244 const char *get_atomic_kind_name(atomic_type_kind_t kind)
247 case ATOMIC_TYPE_VOID: return "void";
248 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
249 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
250 case ATOMIC_TYPE_CHAR: return "char";
251 case ATOMIC_TYPE_SCHAR: return "signed char";
252 case ATOMIC_TYPE_UCHAR: return "unsigned char";
253 case ATOMIC_TYPE_INT: return "int";
254 case ATOMIC_TYPE_UINT: return "unsigned int";
255 case ATOMIC_TYPE_SHORT: return "short";
256 case ATOMIC_TYPE_USHORT: return "unsigned short";
257 case ATOMIC_TYPE_LONG: return "long";
258 case ATOMIC_TYPE_ULONG: return "unsigned long";
259 case ATOMIC_TYPE_LONGLONG: return "long long";
260 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
261 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
262 case ATOMIC_TYPE_FLOAT: return "float";
263 case ATOMIC_TYPE_DOUBLE: return "double";
265 return "INVALIDATOMIC";
269 * Prints the name of an atomic type kinds.
271 * @param kind The type kind.
273 static void print_atomic_kinds(atomic_type_kind_t kind)
275 const char *s = get_atomic_kind_name(kind);
280 * Prints the name of an atomic type.
282 * @param type The type.
284 static void print_atomic_type(const atomic_type_t *type)
286 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
287 print_atomic_kinds(type->akind);
291 * Prints the name of a complex type.
293 * @param type The type.
295 static void print_complex_type(const atomic_type_t *type)
297 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
298 print_string("_Complex ");
299 print_atomic_kinds(type->akind);
303 * Prints the name of an imaginary type.
305 * @param type The type.
307 static void print_imaginary_type(const atomic_type_t *type)
309 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
310 print_string("_Imaginary ");
311 print_atomic_kinds(type->akind);
315 * Print the first part (the prefix) of a type.
317 * @param type The type to print.
319 static void print_function_type_pre(const function_type_t *type)
321 switch (type->linkage) {
324 print_string("extern \"C\" ");
328 if (!(c_mode & _CXX))
329 print_string("extern \"C++\" ");
333 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
335 intern_print_type_pre(type->return_type);
337 cc_kind_t cc = type->calling_convention;
340 case CC_CDECL: print_string(" __cdecl"); break;
341 case CC_STDCALL: print_string(" __stdcall"); break;
342 case CC_FASTCALL: print_string(" __fastcall"); break;
343 case CC_THISCALL: print_string(" __thiscall"); break;
345 if (default_calling_convention != CC_CDECL) {
346 /* show the default calling convention if its not cdecl */
347 cc = default_calling_convention;
355 * Print the second part (the postfix) of a type.
357 * @param type The type to print.
359 static void print_function_type_post(const function_type_t *type,
360 const scope_t *parameters)
363 separator_t sep = { "", ", " };
364 if (parameters == NULL) {
365 function_parameter_t *parameter = type->parameters;
366 for ( ; parameter != NULL; parameter = parameter->next) {
367 print_string(sep_next(&sep));
368 print_type(parameter->type);
371 entity_t *parameter = parameters->entities;
372 for (; parameter != NULL; parameter = parameter->base.next) {
373 if (parameter->kind != ENTITY_PARAMETER)
376 print_string(sep_next(&sep));
377 const type_t *const param_type = parameter->declaration.type;
378 if (param_type == NULL) {
379 print_string(parameter->base.symbol->string);
381 print_type_ext(param_type, parameter->base.symbol, NULL);
385 if (type->variadic) {
386 print_string(sep_next(&sep));
389 if (sep_at_first(&sep) && !type->unspecified_parameters) {
390 print_string("void");
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 print_string(" __based(");
411 print_string(variable->base.base.symbol->string);
415 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
419 * Prints the postfix part of a pointer type.
421 * @param type The pointer type.
423 static void print_pointer_type_post(const pointer_type_t *type)
425 type_t const *const points_to = type->points_to;
426 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
428 intern_print_type_post(points_to);
432 * Prints the prefix part of a reference type.
434 * @param type The reference type.
436 static void print_reference_type_pre(const reference_type_t *type)
438 type_t const *const refers_to = type->refers_to;
439 intern_print_type_pre(refers_to);
440 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
446 * Prints the postfix part of a reference type.
448 * @param type The reference type.
450 static void print_reference_type_post(const reference_type_t *type)
452 type_t const *const refers_to = type->refers_to;
453 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
455 intern_print_type_post(refers_to);
459 * Prints the prefix part of an array type.
461 * @param type The array type.
463 static void print_array_type_pre(const array_type_t *type)
465 intern_print_type_pre(type->element_type);
469 * Prints the postfix part of an array type.
471 * @param type The array type.
473 static void print_array_type_post(const array_type_t *type)
476 if (type->is_static) {
477 print_string("static ");
479 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
480 if (type->size_expression != NULL
481 && (print_implicit_array_size || !type->has_implicit_size)) {
482 print_expression(type->size_expression);
485 intern_print_type_post(type->element_type);
488 void print_enum_definition(const enum_t *enume)
494 entity_t *entry = enume->base.next;
495 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
496 entry = entry->base.next) {
499 print_string(entry->base.symbol->string);
500 if (entry->enum_value.value != NULL) {
502 print_expression(entry->enum_value.value);
513 * Prints an enum type.
515 * @param type The enum type.
517 static void print_type_enum(const enum_type_t *type)
519 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
520 print_string("enum ");
522 enum_t *enume = type->enume;
523 symbol_t *symbol = enume->base.symbol;
524 if (symbol != NULL) {
525 print_string(symbol->string);
527 print_enum_definition(enume);
531 void print_compound_definition(const compound_t *compound)
536 entity_t *entity = compound->members.entities;
537 for ( ; entity != NULL; entity = entity->base.next) {
538 if (entity->kind != ENTITY_COMPOUND_MEMBER)
542 print_entity(entity);
549 if (compound->modifiers & DM_TRANSPARENT_UNION) {
550 print_string("__attribute__((__transparent_union__))");
555 * Prints a compound type.
557 * @param kind The name of the compound kind.
558 * @param type The compound type.
560 static void print_compound_type(char const *const kind, compound_type_t const *const type)
562 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
565 compound_t *compound = type->compound;
566 symbol_t *symbol = compound->base.symbol;
567 if (symbol != NULL) {
568 print_string(symbol->string);
570 print_compound_definition(compound);
575 * Prints the prefix part of a typedef type.
577 * @param type The typedef type.
579 static void print_typedef_type_pre(const typedef_type_t *const type)
581 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
582 print_string(type->typedefe->base.symbol->string);
586 * Prints the prefix part of a typeof type.
588 * @param type The typeof type.
590 static void print_typeof_type_pre(const typeof_type_t *const type)
592 print_string("typeof(");
593 if (type->expression != NULL) {
594 print_expression(type->expression);
596 print_type(type->typeof_type);
602 * Prints the prefix part of a type.
604 * @param type The type.
606 static void intern_print_type_pre(const type_t *const type)
608 switch (type->kind) {
609 case TYPE_ARRAY: print_array_type_pre( &type->array); return;
610 case TYPE_ATOMIC: print_atomic_type( &type->atomic); return;
611 case TYPE_COMPLEX: print_complex_type( &type->atomic); return;
612 case TYPE_COMPOUND_STRUCT: print_compound_type("struct ", &type->compound); return;
613 case TYPE_COMPOUND_UNION: print_compound_type("union ", &type->compound); return;
614 case TYPE_ENUM: print_type_enum( &type->enumt); return;
615 case TYPE_ERROR: print_string("<error>"); return;
616 case TYPE_FUNCTION: print_function_type_pre( &type->function); return;
617 case TYPE_IMAGINARY: print_imaginary_type( &type->atomic); return;
618 case TYPE_POINTER: print_pointer_type_pre( &type->pointer); return;
619 case TYPE_REFERENCE: print_reference_type_pre( &type->reference); return;
620 case TYPE_TYPEDEF: print_typedef_type_pre( &type->typedeft); return;
621 case TYPE_TYPEOF: print_typeof_type_pre( &type->typeoft); return;
623 print_string("unknown");
627 * Prints the postfix part of a type.
629 * @param type The type.
631 static void intern_print_type_post(const type_t *const type)
633 switch (type->kind) {
635 print_function_type_post(&type->function, NULL);
638 print_pointer_type_post(&type->pointer);
641 print_reference_type_post(&type->reference);
644 print_array_type_post(&type->array);
651 case TYPE_COMPOUND_STRUCT:
652 case TYPE_COMPOUND_UNION:
659 void print_type(const type_t *const type)
661 print_type_ext(type, NULL, NULL);
664 void print_type_ext(const type_t *const type, const symbol_t *symbol,
665 const scope_t *parameters)
667 intern_print_type_pre(type);
668 if (symbol != NULL) {
670 print_string(symbol->string);
672 if (type->kind == TYPE_FUNCTION) {
673 print_function_type_post(&type->function, parameters);
675 intern_print_type_post(type);
679 type_t *duplicate_type(const type_t *type)
681 size_t size = get_type_struct_size(type->kind);
683 type_t *const copy = obstack_copy(&type_obst, type, size);
684 copy->base.firm_type = NULL;
689 type_t *get_unqualified_type(type_t *type)
691 assert(!is_typeref(type));
693 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
696 type_t *unqualified_type = duplicate_type(type);
697 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
699 return identify_new_type(unqualified_type);
702 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
704 type_t *type = skip_typeref(orig_type);
707 if (is_type_array(type)) {
708 /* For array types the element type has to be adjusted */
709 type_t *element_type = type->array.element_type;
710 type_t *qual_element_type = get_qualified_type(element_type, qual);
712 if (qual_element_type == element_type)
715 copy = duplicate_type(type);
716 copy->array.element_type = qual_element_type;
717 } else if (is_type_valid(type)) {
718 if ((type->base.qualifiers & qual) == (int)qual)
721 copy = duplicate_type(type);
722 copy->base.qualifiers |= qual;
727 return identify_new_type(copy);
730 static bool test_atomic_type_flag(atomic_type_kind_t kind,
731 atomic_type_flag_t flag)
733 assert(kind <= ATOMIC_TYPE_LAST);
734 return (atomic_type_properties[kind].flags & flag) != 0;
737 bool is_type_integer(const type_t *type)
739 assert(!is_typeref(type));
740 if (!is_type_arithmetic(type))
742 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
745 bool is_type_enum(const type_t *type)
747 assert(!is_typeref(type));
748 return type->kind == TYPE_ENUM;
751 bool is_type_float(const type_t *type)
753 assert(!is_typeref(type));
755 if (type->kind != TYPE_ATOMIC)
758 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
761 bool is_type_complex(const type_t *type)
763 assert(!is_typeref(type));
764 return type->kind == TYPE_COMPLEX;
767 bool is_type_signed(const type_t *type)
769 assert(!is_typeref(type));
770 if (!is_type_arithmetic(type))
772 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
775 bool is_type_arithmetic(const type_t *type)
777 assert(!is_typeref(type));
779 switch (type->kind) {
785 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
791 bool is_type_real(const type_t *type)
794 return is_type_integer(type) || is_type_float(type);
797 bool is_type_scalar(const type_t *type)
799 assert(!is_typeref(type));
801 switch (type->kind) {
808 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
814 bool is_type_incomplete(const type_t *type)
816 assert(!is_typeref(type));
818 switch (type->kind) {
819 case TYPE_COMPOUND_STRUCT:
820 case TYPE_COMPOUND_UNION: {
821 const compound_type_t *compound_type = &type->compound;
822 return !compound_type->compound->complete;
828 return type->array.size_expression == NULL
829 && !type->array.size_constant;
834 return type->atomic.akind == ATOMIC_TYPE_VOID;
844 panic("typedef not skipped");
847 panic("invalid type");
850 bool is_type_object(const type_t *type)
852 return !is_type_function(type) && !is_type_incomplete(type);
856 * Check if two function types are compatible.
858 static bool function_types_compatible(const function_type_t *func1,
859 const function_type_t *func2)
861 const type_t* const ret1 = skip_typeref(func1->return_type);
862 const type_t* const ret2 = skip_typeref(func2->return_type);
863 if (!types_compatible(ret1, ret2))
866 if (func1->linkage != func2->linkage)
869 cc_kind_t cc1 = func1->calling_convention;
870 if (cc1 == CC_DEFAULT)
871 cc1 = default_calling_convention;
872 cc_kind_t cc2 = func2->calling_convention;
873 if (cc2 == CC_DEFAULT)
874 cc2 = default_calling_convention;
879 if (func1->variadic != func2->variadic)
882 /* can parameters be compared? */
883 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
884 || (func2->unspecified_parameters && !func2->kr_style_parameters))
887 /* TODO: handling of unspecified parameters not correct yet */
889 /* all argument types must be compatible */
890 function_parameter_t *parameter1 = func1->parameters;
891 function_parameter_t *parameter2 = func2->parameters;
892 for ( ; parameter1 != NULL && parameter2 != NULL;
893 parameter1 = parameter1->next, parameter2 = parameter2->next) {
894 type_t *parameter1_type = skip_typeref(parameter1->type);
895 type_t *parameter2_type = skip_typeref(parameter2->type);
897 parameter1_type = get_unqualified_type(parameter1_type);
898 parameter2_type = get_unqualified_type(parameter2_type);
900 if (!types_compatible(parameter1_type, parameter2_type))
903 /* same number of arguments? */
904 if (parameter1 != NULL || parameter2 != NULL)
911 * Check if two array types are compatible.
913 static bool array_types_compatible(const array_type_t *array1,
914 const array_type_t *array2)
916 type_t *element_type1 = skip_typeref(array1->element_type);
917 type_t *element_type2 = skip_typeref(array2->element_type);
918 if (!types_compatible(element_type1, element_type2))
921 if (!array1->size_constant || !array2->size_constant)
924 return array1->size == array2->size;
927 bool types_compatible(const type_t *type1, const type_t *type2)
929 assert(!is_typeref(type1));
930 assert(!is_typeref(type2));
932 /* shortcut: the same type is always compatible */
936 if (type1->base.qualifiers == type2->base.qualifiers &&
937 type1->kind == type2->kind) {
938 switch (type1->kind) {
940 return function_types_compatible(&type1->function, &type2->function);
944 return type1->atomic.akind == type2->atomic.akind;
946 return array_types_compatible(&type1->array, &type2->array);
949 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
950 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
951 return types_compatible(to1, to2);
954 case TYPE_REFERENCE: {
955 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
956 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
957 return types_compatible(to1, to2);
960 case TYPE_COMPOUND_STRUCT:
961 case TYPE_COMPOUND_UNION:
965 /* TODO: not implemented */
969 /* Hmm, the error type should be compatible to all other types */
973 panic("typeref not skipped");
977 return !is_type_valid(type1) || !is_type_valid(type2);
981 * Skip all typerefs and return the underlying type.
983 type_t *skip_typeref(type_t *type)
985 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
988 switch (type->kind) {
992 qualifiers |= type->base.qualifiers;
994 const typedef_type_t *typedef_type = &type->typedeft;
995 if (typedef_type->resolved_type != NULL) {
996 type = typedef_type->resolved_type;
999 type = typedef_type->typedefe->type;
1003 qualifiers |= type->base.qualifiers;
1004 type = type->typeoft.typeof_type;
1012 if (qualifiers != TYPE_QUALIFIER_NONE) {
1013 type_t *const copy = duplicate_type(type);
1015 /* for const with typedefed array type the element type has to be
1017 if (is_type_array(copy)) {
1018 type_t *element_type = copy->array.element_type;
1019 element_type = duplicate_type(element_type);
1020 element_type->base.qualifiers |= qualifiers;
1021 copy->array.element_type = element_type;
1023 copy->base.qualifiers |= qualifiers;
1026 type = identify_new_type(copy);
1032 unsigned get_type_size(type_t *type)
1034 switch (type->kind) {
1038 case TYPE_IMAGINARY:
1040 return get_atomic_type_size(type->atomic.akind);
1042 return get_atomic_type_size(type->atomic.akind) * 2;
1043 case TYPE_COMPOUND_UNION:
1044 layout_union_type(&type->compound);
1045 return type->compound.compound->size;
1046 case TYPE_COMPOUND_STRUCT:
1047 layout_struct_type(&type->compound);
1048 return type->compound.compound->size;
1050 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1051 case TYPE_REFERENCE:
1053 return pointer_properties.size;
1055 /* TODO: correct if element_type is aligned? */
1056 il_size_t element_size = get_type_size(type->array.element_type);
1057 return type->array.size * element_size;
1060 return get_type_size(type->typedeft.typedefe->type);
1062 return get_type_size(type->typeoft.typeof_type);
1064 panic("invalid type");
1067 unsigned get_type_alignment(type_t *type)
1069 switch (type->kind) {
1073 case TYPE_IMAGINARY:
1076 return get_atomic_type_alignment(type->atomic.akind);
1077 case TYPE_COMPOUND_UNION:
1078 layout_union_type(&type->compound);
1079 return type->compound.compound->alignment;
1080 case TYPE_COMPOUND_STRUCT:
1081 layout_struct_type(&type->compound);
1082 return type->compound.compound->alignment;
1084 /* gcc says 1 here... */
1086 case TYPE_REFERENCE:
1088 return pointer_properties.alignment;
1090 return get_type_alignment(type->array.element_type);
1091 case TYPE_TYPEDEF: {
1092 il_alignment_t alignment
1093 = get_type_alignment(type->typedeft.typedefe->type);
1094 if (type->typedeft.typedefe->alignment > alignment)
1095 alignment = type->typedeft.typedefe->alignment;
1100 return get_type_alignment(type->typeoft.typeof_type);
1102 panic("invalid type");
1106 * get alignment of a type when used inside a compound.
1107 * Some ABIs are broken and alignment inside a compound is different from
1108 * recommended alignment of a type
1110 static unsigned get_type_alignment_compound(type_t *const type)
1112 assert(!is_typeref(type));
1113 if (type->kind == TYPE_ATOMIC)
1114 return atomic_type_properties[type->atomic.akind].struct_alignment;
1115 return get_type_alignment(type);
1118 decl_modifiers_t get_type_modifiers(const type_t *type)
1120 switch (type->kind) {
1123 case TYPE_COMPOUND_STRUCT:
1124 case TYPE_COMPOUND_UNION:
1125 return type->compound.compound->modifiers;
1127 return type->function.modifiers;
1131 case TYPE_IMAGINARY:
1132 case TYPE_REFERENCE:
1136 case TYPE_TYPEDEF: {
1137 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1138 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1142 return get_type_modifiers(type->typeoft.typeof_type);
1144 panic("invalid type");
1147 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1149 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1152 switch (type->base.kind) {
1154 return TYPE_QUALIFIER_NONE;
1156 qualifiers |= type->base.qualifiers;
1157 const typedef_type_t *typedef_type = &type->typedeft;
1158 if (typedef_type->resolved_type != NULL)
1159 type = typedef_type->resolved_type;
1161 type = typedef_type->typedefe->type;
1164 type = type->typeoft.typeof_type;
1167 if (skip_array_type) {
1168 type = type->array.element_type;
1177 return type->base.qualifiers | qualifiers;
1180 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1182 assert(kind <= ATOMIC_TYPE_LAST);
1183 return atomic_type_properties[kind].size;
1186 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1188 assert(kind <= ATOMIC_TYPE_LAST);
1189 return atomic_type_properties[kind].alignment;
1192 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1194 assert(kind <= ATOMIC_TYPE_LAST);
1195 return atomic_type_properties[kind].flags;
1199 * Find the atomic type kind representing a given size (signed).
1201 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1203 static atomic_type_kind_t kinds[32];
1206 atomic_type_kind_t kind = kinds[size];
1207 if (kind == (atomic_type_kind_t)0) {
1208 static const atomic_type_kind_t possible_kinds[] = {
1213 ATOMIC_TYPE_LONGLONG
1215 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1216 if (get_atomic_type_size(possible_kinds[i]) == size) {
1217 kind = possible_kinds[i];
1227 * Find the atomic type kind representing a given size (signed).
1229 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1231 static atomic_type_kind_t kinds[32];
1234 atomic_type_kind_t kind = kinds[size];
1235 if (kind == (atomic_type_kind_t)0) {
1236 static const atomic_type_kind_t possible_kinds[] = {
1241 ATOMIC_TYPE_ULONGLONG
1243 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1244 if (get_atomic_type_size(possible_kinds[i]) == size) {
1245 kind = possible_kinds[i];
1255 * Hash the given type and return the "singleton" version
1258 type_t *identify_new_type(type_t *type)
1260 type_t *result = typehash_insert(type);
1261 if (result != type) {
1262 obstack_free(&type_obst, type);
1268 * Creates a new atomic type.
1270 * @param akind The kind of the atomic type.
1271 * @param qualifiers Type qualifiers for the new type.
1273 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1275 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1276 type->base.qualifiers = qualifiers;
1277 type->atomic.akind = akind;
1279 return identify_new_type(type);
1283 * Creates a new complex type.
1285 * @param akind The kind of the atomic type.
1286 * @param qualifiers Type qualifiers for the new type.
1288 type_t *make_complex_type(atomic_type_kind_t akind,
1289 type_qualifiers_t qualifiers)
1291 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1292 type->base.qualifiers = qualifiers;
1293 type->atomic.akind = akind;
1295 return identify_new_type(type);
1299 * Creates a new imaginary type.
1301 * @param akind The kind of the atomic type.
1302 * @param qualifiers Type qualifiers for the new type.
1304 type_t *make_imaginary_type(atomic_type_kind_t akind,
1305 type_qualifiers_t qualifiers)
1307 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1308 type->base.qualifiers = qualifiers;
1309 type->atomic.akind = akind;
1311 return identify_new_type(type);
1315 * Creates a new pointer type.
1317 * @param points_to The points-to type for the new type.
1318 * @param qualifiers Type qualifiers for the new type.
1320 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1322 type_t *const type = allocate_type_zero(TYPE_POINTER);
1323 type->base.qualifiers = qualifiers;
1324 type->pointer.points_to = points_to;
1325 type->pointer.base_variable = NULL;
1327 return identify_new_type(type);
1331 * Creates a new reference type.
1333 * @param refers_to The referred-to type for the new type.
1335 type_t *make_reference_type(type_t *refers_to)
1337 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1338 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1339 type->reference.refers_to = refers_to;
1341 return identify_new_type(type);
1345 * Creates a new based pointer type.
1347 * @param points_to The points-to type for the new type.
1348 * @param qualifiers Type qualifiers for the new type.
1349 * @param variable The based variable
1351 type_t *make_based_pointer_type(type_t *points_to,
1352 type_qualifiers_t qualifiers, variable_t *variable)
1354 type_t *const type = allocate_type_zero(TYPE_POINTER);
1355 type->base.qualifiers = qualifiers;
1356 type->pointer.points_to = points_to;
1357 type->pointer.base_variable = variable;
1359 return identify_new_type(type);
1363 type_t *make_array_type(type_t *element_type, size_t size,
1364 type_qualifiers_t qualifiers)
1366 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1367 type->base.qualifiers = qualifiers;
1368 type->array.element_type = element_type;
1369 type->array.size = size;
1370 type->array.size_constant = true;
1372 return identify_new_type(type);
1375 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1376 il_alignment_t *struct_alignment,
1377 bool packed, entity_t *first)
1379 il_size_t offset = *struct_offset;
1380 il_alignment_t alignment = *struct_alignment;
1381 size_t bit_offset = 0;
1384 for (member = first; member != NULL; member = member->base.next) {
1385 if (member->kind != ENTITY_COMPOUND_MEMBER)
1387 if (!member->compound_member.bitfield)
1390 type_t *const base_type = skip_typeref(member->declaration.type);
1391 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1392 il_alignment_t alignment_mask = base_alignment-1;
1393 if (base_alignment > alignment)
1394 alignment = base_alignment;
1396 size_t bit_size = member->compound_member.bit_size;
1398 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1399 offset &= ~alignment_mask;
1400 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1402 if (bit_offset + bit_size > base_size || bit_size == 0) {
1403 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1404 offset = (offset + base_alignment-1) & ~alignment_mask;
1409 if (byte_order_big_endian) {
1410 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1411 member->compound_member.offset = offset & ~alignment_mask;
1412 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1414 member->compound_member.offset = offset;
1415 member->compound_member.bit_offset = bit_offset;
1418 bit_offset += bit_size;
1419 offset += bit_offset / BITS_PER_BYTE;
1420 bit_offset %= BITS_PER_BYTE;
1426 *struct_offset = offset;
1427 *struct_alignment = alignment;
1431 void layout_struct_type(compound_type_t *type)
1433 assert(type->compound != NULL);
1435 compound_t *compound = type->compound;
1436 if (!compound->complete)
1438 if (type->compound->layouted)
1440 compound->layouted = true;
1442 il_size_t offset = 0;
1443 il_alignment_t alignment = compound->alignment;
1444 bool need_pad = false;
1446 entity_t *entry = compound->members.entities;
1447 while (entry != NULL) {
1448 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1451 type_t *const m_type = skip_typeref(entry->declaration.type);
1452 if (!is_type_valid(m_type))
1455 if (entry->compound_member.bitfield) {
1456 entry = pack_bitfield_members(&offset, &alignment,
1457 compound->packed, entry);
1461 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1462 if (m_alignment > alignment)
1463 alignment = m_alignment;
1465 if (!compound->packed) {
1466 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1468 if (new_offset > offset) {
1470 offset = new_offset;
1474 entry->compound_member.offset = offset;
1475 offset += get_type_size(m_type);
1478 entry = entry->base.next;
1481 if (!compound->packed) {
1482 il_size_t new_offset = (offset + alignment-1) & -alignment;
1483 if (new_offset > offset) {
1485 offset = new_offset;
1489 position_t const *const pos = &compound->base.pos;
1491 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1492 } else if (compound->packed) {
1493 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1496 compound->size = offset;
1497 compound->alignment = alignment;
1500 void layout_union_type(compound_type_t *type)
1502 assert(type->compound != NULL);
1504 compound_t *compound = type->compound;
1505 if (! compound->complete)
1507 if (compound->layouted)
1509 compound->layouted = true;
1512 il_alignment_t alignment = compound->alignment;
1514 entity_t *entry = compound->members.entities;
1515 for (; entry != NULL; entry = entry->base.next) {
1516 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1519 type_t *m_type = skip_typeref(entry->declaration.type);
1520 if (! is_type_valid(skip_typeref(m_type)))
1523 entry->compound_member.offset = 0;
1524 il_size_t m_size = get_type_size(m_type);
1527 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1528 if (m_alignment > alignment)
1529 alignment = m_alignment;
1531 size = (size + alignment - 1) & -alignment;
1533 compound->size = size;
1534 compound->alignment = alignment;
1537 function_parameter_t *allocate_parameter(type_t *const type)
1539 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1540 memset(param, 0, sizeof(*param));
1545 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1546 type_t *argument_type2, decl_modifiers_t modifiers)
1548 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1549 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1550 parameter1->next = parameter2;
1552 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1553 type->function.return_type = return_type;
1554 type->function.parameters = parameter1;
1555 type->function.modifiers |= modifiers;
1556 type->function.linkage = LINKAGE_C;
1558 return identify_new_type(type);
1561 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1562 decl_modifiers_t modifiers)
1564 function_parameter_t *const parameter = allocate_parameter(argument_type);
1566 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1567 type->function.return_type = return_type;
1568 type->function.parameters = parameter;
1569 type->function.modifiers |= modifiers;
1570 type->function.linkage = LINKAGE_C;
1572 return identify_new_type(type);
1575 type_t *make_function_1_type_variadic(type_t *return_type,
1576 type_t *argument_type,
1577 decl_modifiers_t modifiers)
1579 function_parameter_t *const parameter = allocate_parameter(argument_type);
1581 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1582 type->function.return_type = return_type;
1583 type->function.parameters = parameter;
1584 type->function.variadic = true;
1585 type->function.modifiers |= modifiers;
1586 type->function.linkage = LINKAGE_C;
1588 return identify_new_type(type);
1591 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1593 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1594 type->function.return_type = return_type;
1595 type->function.parameters = NULL;
1596 type->function.modifiers |= modifiers;
1597 type->function.linkage = LINKAGE_C;
1599 return identify_new_type(type);
1602 type_t *make_function_type(type_t *return_type, int n_types,
1603 type_t *const *argument_types,
1604 decl_modifiers_t modifiers)
1606 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1607 type->function.return_type = return_type;
1608 type->function.modifiers |= modifiers;
1609 type->function.linkage = LINKAGE_C;
1611 function_parameter_t **anchor = &type->function.parameters;
1612 for (int i = 0; i < n_types; ++i) {
1613 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1614 *anchor = parameter;
1615 anchor = ¶meter->next;
1618 return identify_new_type(type);
1622 * Debug helper. Prints the given type to stdout.
1624 static __attribute__((unused))
1625 void dbg_type(const type_t *type)
1627 print_to_file(stderr);