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(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
54 assert(kind <= TYPE_TYPEOF);
55 assert(sizes[kind] != 0);
59 type_t *allocate_type_zero(type_kind_t kind)
61 size_t const size = get_type_struct_size(kind);
62 type_t *const res = obstack_alloc(&type_obst, size);
64 res->base.kind = kind;
70 * Properties of atomic types.
72 atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
73 [ATOMIC_TYPE_VOID] = {
76 .flags = ATOMIC_TYPE_FLAG_NONE,
79 [ATOMIC_TYPE_BOOL] = {
82 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
85 [ATOMIC_TYPE_CHAR] = {
88 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
91 [ATOMIC_TYPE_SCHAR] = {
94 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
95 | ATOMIC_TYPE_FLAG_SIGNED,
98 [ATOMIC_TYPE_UCHAR] = {
101 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
104 [ATOMIC_TYPE_SHORT] = {
107 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
108 | ATOMIC_TYPE_FLAG_SIGNED,
111 [ATOMIC_TYPE_USHORT] = {
114 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
117 [ATOMIC_TYPE_INT] = {
118 .size = (unsigned) -1,
119 .alignment = (unsigned) -1,
120 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
121 | ATOMIC_TYPE_FLAG_SIGNED,
124 [ATOMIC_TYPE_UINT] = {
125 .size = (unsigned) -1,
126 .alignment = (unsigned) -1,
127 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
130 [ATOMIC_TYPE_LONG] = {
131 .size = (unsigned) -1,
132 .alignment = (unsigned) -1,
133 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
134 | ATOMIC_TYPE_FLAG_SIGNED,
137 [ATOMIC_TYPE_ULONG] = {
138 .size = (unsigned) -1,
139 .alignment = (unsigned) -1,
140 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
143 [ATOMIC_TYPE_LONGLONG] = {
146 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
147 | ATOMIC_TYPE_FLAG_SIGNED,
150 [ATOMIC_TYPE_ULONGLONG] = {
153 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
156 [ATOMIC_TYPE_FLOAT] = {
159 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
160 | ATOMIC_TYPE_FLAG_SIGNED,
163 [ATOMIC_TYPE_DOUBLE] = {
166 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
167 | ATOMIC_TYPE_FLAG_SIGNED,
170 [ATOMIC_TYPE_WCHAR_T] = {
171 .size = (unsigned)-1,
172 .alignment = (unsigned)-1,
173 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
174 .rank = (unsigned)-1,
177 atomic_type_properties_t pointer_properties = {
180 .flags = ATOMIC_TYPE_FLAG_NONE,
183 static inline bool is_po2(unsigned x)
185 return (x & (x-1)) == 0;
188 void init_types(unsigned machine_size)
190 obstack_init(&type_obst);
192 atomic_type_properties_t *props = atomic_type_properties;
194 /* atempt to set some sane defaults based on machine size */
196 unsigned int_size = machine_size < 32 ? 2 : 4;
197 unsigned long_size = machine_size < 64 ? 4 : 8;
199 props[ATOMIC_TYPE_INT].size = int_size;
200 props[ATOMIC_TYPE_INT].alignment = int_size;
201 props[ATOMIC_TYPE_UINT].size = int_size;
202 props[ATOMIC_TYPE_UINT].alignment = int_size;
203 props[ATOMIC_TYPE_LONG].size = long_size;
204 props[ATOMIC_TYPE_LONG].alignment = long_size;
205 props[ATOMIC_TYPE_ULONG].size = long_size;
206 props[ATOMIC_TYPE_ULONG].alignment = long_size;
208 pointer_properties.size = long_size;
209 pointer_properties.alignment = long_size;
210 pointer_properties.struct_alignment = long_size;
212 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
213 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
215 /* set struct alignments to the same value as alignment */
216 for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
217 props[i].struct_alignment = props[i].alignment;
221 void exit_types(void)
223 obstack_free(&type_obst, NULL);
226 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
228 size_t sep = q & QUAL_SEP_START ? 0 : 1;
229 if (qualifiers & TYPE_QUALIFIER_CONST) {
230 print_string(&" const"[sep]);
233 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
234 print_string(&" volatile"[sep]);
237 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
238 print_string(&" restrict"[sep]);
241 if (sep == 0 && q & QUAL_SEP_END)
245 const char *get_atomic_kind_name(atomic_type_kind_t kind)
248 case ATOMIC_TYPE_VOID: return "void";
249 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
250 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
251 case ATOMIC_TYPE_CHAR: return "char";
252 case ATOMIC_TYPE_SCHAR: return "signed char";
253 case ATOMIC_TYPE_UCHAR: return "unsigned char";
254 case ATOMIC_TYPE_INT: return "int";
255 case ATOMIC_TYPE_UINT: return "unsigned int";
256 case ATOMIC_TYPE_SHORT: return "short";
257 case ATOMIC_TYPE_USHORT: return "unsigned short";
258 case ATOMIC_TYPE_LONG: return "long";
259 case ATOMIC_TYPE_ULONG: return "unsigned long";
260 case ATOMIC_TYPE_LONGLONG: return "long long";
261 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
262 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
263 case ATOMIC_TYPE_FLOAT: return "float";
264 case ATOMIC_TYPE_DOUBLE: return "double";
266 return "INVALIDATOMIC";
270 * Prints the name of an atomic type kinds.
272 * @param kind The type kind.
274 static void print_atomic_kinds(atomic_type_kind_t kind)
276 const char *s = get_atomic_kind_name(kind);
281 * Prints the name of an atomic type.
283 * @param type The type.
285 static void print_atomic_type(const atomic_type_t *type)
287 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
288 print_atomic_kinds(type->akind);
292 * Prints the name of a complex type.
294 * @param type The type.
296 static void print_complex_type(const atomic_type_t *type)
298 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
299 print_string("_Complex ");
300 print_atomic_kinds(type->akind);
304 * Prints the name of an imaginary type.
306 * @param type The type.
308 static void print_imaginary_type(const atomic_type_t *type)
310 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
311 print_string("_Imaginary ");
312 print_atomic_kinds(type->akind);
316 * Print the first part (the prefix) of a type.
318 * @param type The type to print.
320 static void print_function_type_pre(const function_type_t *type)
322 switch (type->linkage) {
325 print_string("extern \"C\" ");
329 if (!(c_mode & _CXX))
330 print_string("extern \"C++\" ");
334 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
336 intern_print_type_pre(type->return_type);
338 cc_kind_t cc = type->calling_convention;
341 case CC_CDECL: print_string(" __cdecl"); break;
342 case CC_STDCALL: print_string(" __stdcall"); break;
343 case CC_FASTCALL: print_string(" __fastcall"); break;
344 case CC_THISCALL: print_string(" __thiscall"); break;
346 if (default_calling_convention != CC_CDECL) {
347 /* show the default calling convention if its not cdecl */
348 cc = default_calling_convention;
356 * Print the second part (the postfix) of a type.
358 * @param type The type to print.
360 static void print_function_type_post(const function_type_t *type,
361 const scope_t *parameters)
364 separator_t sep = { "", ", " };
365 if (parameters == NULL) {
366 function_parameter_t *parameter = type->parameters;
367 for ( ; parameter != NULL; parameter = parameter->next) {
368 print_string(sep_next(&sep));
369 print_type(parameter->type);
372 entity_t *parameter = parameters->entities;
373 for (; parameter != NULL; parameter = parameter->base.next) {
374 if (parameter->kind != ENTITY_PARAMETER)
377 print_string(sep_next(&sep));
378 const type_t *const param_type = parameter->declaration.type;
379 if (param_type == NULL) {
380 print_string(parameter->base.symbol->string);
382 print_type_ext(param_type, parameter->base.symbol, NULL);
386 if (type->variadic) {
387 print_string(sep_next(&sep));
390 if (sep_at_first(&sep) && !type->unspecified_parameters) {
391 print_string("void");
395 intern_print_type_post(type->return_type);
399 * Prints the prefix part of a pointer type.
401 * @param type The pointer type.
403 static void print_pointer_type_pre(const pointer_type_t *type)
405 type_t const *const points_to = type->points_to;
406 intern_print_type_pre(points_to);
407 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
409 variable_t *const variable = type->base_variable;
410 if (variable != NULL) {
411 print_string(" __based(");
412 print_string(variable->base.base.symbol->string);
416 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
420 * Prints the postfix part of a pointer type.
422 * @param type The pointer type.
424 static void print_pointer_type_post(const pointer_type_t *type)
426 type_t const *const points_to = type->points_to;
427 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
429 intern_print_type_post(points_to);
433 * Prints the prefix part of a reference type.
435 * @param type The reference type.
437 static void print_reference_type_pre(const reference_type_t *type)
439 type_t const *const refers_to = type->refers_to;
440 intern_print_type_pre(refers_to);
441 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
447 * Prints the postfix part of a reference type.
449 * @param type The reference type.
451 static void print_reference_type_post(const reference_type_t *type)
453 type_t const *const refers_to = type->refers_to;
454 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
456 intern_print_type_post(refers_to);
460 * Prints the prefix part of an array type.
462 * @param type The array type.
464 static void print_array_type_pre(const array_type_t *type)
466 intern_print_type_pre(type->element_type);
470 * Prints the postfix part of an array type.
472 * @param type The array type.
474 static void print_array_type_post(const array_type_t *type)
477 if (type->is_static) {
478 print_string("static ");
480 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
481 if (type->size_expression != NULL
482 && (print_implicit_array_size || !type->has_implicit_size)) {
483 print_expression(type->size_expression);
486 intern_print_type_post(type->element_type);
489 void print_enum_definition(const enum_t *enume)
495 entity_t *entry = enume->base.next;
496 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
497 entry = entry->base.next) {
500 print_string(entry->base.symbol->string);
501 if (entry->enum_value.value != NULL) {
503 print_expression(entry->enum_value.value);
514 * Prints an enum type.
516 * @param type The enum type.
518 static void print_type_enum(const enum_type_t *type)
520 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
521 print_string("enum ");
523 enum_t *enume = type->enume;
524 symbol_t *symbol = enume->base.symbol;
525 if (symbol != NULL) {
526 print_string(symbol->string);
528 print_enum_definition(enume);
532 void print_compound_definition(const compound_t *compound)
537 entity_t *entity = compound->members.entities;
538 for ( ; entity != NULL; entity = entity->base.next) {
539 if (entity->kind != ENTITY_COMPOUND_MEMBER)
543 print_entity(entity);
550 if (compound->modifiers & DM_TRANSPARENT_UNION) {
551 print_string("__attribute__((__transparent_union__))");
556 * Prints a compound type.
558 * @param kind The name of the compound kind.
559 * @param type The compound type.
561 static void print_compound_type(char const *const kind, compound_type_t const *const type)
563 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
566 compound_t *compound = type->compound;
567 symbol_t *symbol = compound->base.symbol;
568 if (symbol != NULL) {
569 print_string(symbol->string);
571 print_compound_definition(compound);
576 * Prints the prefix part of a typedef type.
578 * @param type The typedef type.
580 static void print_typedef_type_pre(const typedef_type_t *const type)
582 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
583 print_string(type->typedefe->base.symbol->string);
587 * Prints the prefix part of a typeof type.
589 * @param type The typeof type.
591 static void print_typeof_type_pre(const typeof_type_t *const type)
593 print_string("typeof(");
594 if (type->expression != NULL) {
595 print_expression(type->expression);
597 print_type(type->typeof_type);
603 * Prints the prefix part of a type.
605 * @param type The type.
607 static void intern_print_type_pre(const type_t *const type)
609 switch (type->kind) {
610 case TYPE_ARRAY: print_array_type_pre( &type->array); return;
611 case TYPE_ATOMIC: print_atomic_type( &type->atomic); return;
612 case TYPE_COMPLEX: print_complex_type( &type->atomic); return;
613 case TYPE_COMPOUND_STRUCT: print_compound_type("struct ", &type->compound); return;
614 case TYPE_COMPOUND_UNION: print_compound_type("union ", &type->compound); return;
615 case TYPE_ENUM: print_type_enum( &type->enumt); return;
616 case TYPE_ERROR: print_string("<error>"); return;
617 case TYPE_FUNCTION: print_function_type_pre( &type->function); return;
618 case TYPE_IMAGINARY: print_imaginary_type( &type->atomic); return;
619 case TYPE_POINTER: print_pointer_type_pre( &type->pointer); return;
620 case TYPE_REFERENCE: print_reference_type_pre( &type->reference); return;
621 case TYPE_TYPEDEF: print_typedef_type_pre( &type->typedeft); return;
622 case TYPE_TYPEOF: print_typeof_type_pre( &type->typeoft); return;
624 print_string("unknown");
628 * Prints the postfix part of a type.
630 * @param type The type.
632 static void intern_print_type_post(const type_t *const type)
634 switch (type->kind) {
636 print_function_type_post(&type->function, NULL);
639 print_pointer_type_post(&type->pointer);
642 print_reference_type_post(&type->reference);
645 print_array_type_post(&type->array);
652 case TYPE_COMPOUND_STRUCT:
653 case TYPE_COMPOUND_UNION:
660 void print_type(const type_t *const type)
662 print_type_ext(type, NULL, NULL);
665 void print_type_ext(const type_t *const type, const symbol_t *symbol,
666 const scope_t *parameters)
668 intern_print_type_pre(type);
669 if (symbol != NULL) {
671 print_string(symbol->string);
673 if (type->kind == TYPE_FUNCTION) {
674 print_function_type_post(&type->function, parameters);
676 intern_print_type_post(type);
680 type_t *duplicate_type(const type_t *type)
682 size_t size = get_type_struct_size(type->kind);
684 type_t *const copy = obstack_copy(&type_obst, type, size);
685 copy->base.firm_type = NULL;
690 type_t *get_unqualified_type(type_t *type)
692 assert(!is_typeref(type));
694 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
697 type_t *unqualified_type = duplicate_type(type);
698 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
700 return identify_new_type(unqualified_type);
703 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
705 type_t *type = skip_typeref(orig_type);
708 if (is_type_array(type)) {
709 /* For array types the element type has to be adjusted */
710 type_t *element_type = type->array.element_type;
711 type_t *qual_element_type = get_qualified_type(element_type, qual);
713 if (qual_element_type == element_type)
716 copy = duplicate_type(type);
717 copy->array.element_type = qual_element_type;
718 } else if (is_type_valid(type)) {
719 if ((type->base.qualifiers & qual) == (int)qual)
722 copy = duplicate_type(type);
723 copy->base.qualifiers |= qual;
728 return identify_new_type(copy);
731 static bool test_atomic_type_flag(atomic_type_kind_t kind,
732 atomic_type_flag_t flag)
734 assert(kind <= ATOMIC_TYPE_LAST);
735 return (atomic_type_properties[kind].flags & flag) != 0;
738 bool is_type_integer(const type_t *type)
740 assert(!is_typeref(type));
741 if (!is_type_arithmetic(type))
743 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
746 bool is_type_enum(const type_t *type)
748 assert(!is_typeref(type));
749 return type->kind == TYPE_ENUM;
752 bool is_type_float(const type_t *type)
754 assert(!is_typeref(type));
756 if (type->kind != TYPE_ATOMIC)
759 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
762 bool is_type_complex(const type_t *type)
764 assert(!is_typeref(type));
765 return type->kind == TYPE_COMPLEX;
768 bool is_type_signed(const type_t *type)
770 assert(!is_typeref(type));
771 if (!is_type_arithmetic(type))
773 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
776 bool is_type_arithmetic(const type_t *type)
778 assert(!is_typeref(type));
780 switch (type->kind) {
786 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
792 bool is_type_real(const type_t *type)
795 return is_type_integer(type) || is_type_float(type);
798 bool is_type_scalar(const type_t *type)
800 assert(!is_typeref(type));
802 switch (type->kind) {
809 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
815 bool is_type_incomplete(const type_t *type)
817 assert(!is_typeref(type));
819 switch (type->kind) {
820 case TYPE_COMPOUND_STRUCT:
821 case TYPE_COMPOUND_UNION: {
822 const compound_type_t *compound_type = &type->compound;
823 return !compound_type->compound->complete;
829 return type->array.size_expression == NULL
830 && !type->array.size_constant;
835 return type->atomic.akind == ATOMIC_TYPE_VOID;
845 panic("typedef not skipped");
848 panic("invalid type");
851 bool is_type_object(const type_t *type)
853 return !is_type_function(type) && !is_type_incomplete(type);
857 * Check if two function types are compatible.
859 static bool function_types_compatible(const function_type_t *func1,
860 const function_type_t *func2)
862 const type_t* const ret1 = skip_typeref(func1->return_type);
863 const type_t* const ret2 = skip_typeref(func2->return_type);
864 if (!types_compatible(ret1, ret2))
867 if (func1->linkage != func2->linkage)
870 cc_kind_t cc1 = func1->calling_convention;
871 if (cc1 == CC_DEFAULT)
872 cc1 = default_calling_convention;
873 cc_kind_t cc2 = func2->calling_convention;
874 if (cc2 == CC_DEFAULT)
875 cc2 = default_calling_convention;
880 if (func1->variadic != func2->variadic)
883 /* can parameters be compared? */
884 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
885 || (func2->unspecified_parameters && !func2->kr_style_parameters))
888 /* TODO: handling of unspecified parameters not correct yet */
890 /* all argument types must be compatible */
891 function_parameter_t *parameter1 = func1->parameters;
892 function_parameter_t *parameter2 = func2->parameters;
893 for ( ; parameter1 != NULL && parameter2 != NULL;
894 parameter1 = parameter1->next, parameter2 = parameter2->next) {
895 type_t *parameter1_type = skip_typeref(parameter1->type);
896 type_t *parameter2_type = skip_typeref(parameter2->type);
898 parameter1_type = get_unqualified_type(parameter1_type);
899 parameter2_type = get_unqualified_type(parameter2_type);
901 if (!types_compatible(parameter1_type, parameter2_type))
904 /* same number of arguments? */
905 if (parameter1 != NULL || parameter2 != NULL)
912 * Check if two array types are compatible.
914 static bool array_types_compatible(const array_type_t *array1,
915 const array_type_t *array2)
917 type_t *element_type1 = skip_typeref(array1->element_type);
918 type_t *element_type2 = skip_typeref(array2->element_type);
919 if (!types_compatible(element_type1, element_type2))
922 if (!array1->size_constant || !array2->size_constant)
925 return array1->size == array2->size;
928 bool types_compatible(const type_t *type1, const type_t *type2)
930 assert(!is_typeref(type1));
931 assert(!is_typeref(type2));
933 /* shortcut: the same type is always compatible */
937 if (type1->base.qualifiers == type2->base.qualifiers &&
938 type1->kind == type2->kind) {
939 switch (type1->kind) {
941 return function_types_compatible(&type1->function, &type2->function);
945 return type1->atomic.akind == type2->atomic.akind;
947 return array_types_compatible(&type1->array, &type2->array);
950 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
951 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
952 return types_compatible(to1, to2);
955 case TYPE_REFERENCE: {
956 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
957 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
958 return types_compatible(to1, to2);
961 case TYPE_COMPOUND_STRUCT:
962 case TYPE_COMPOUND_UNION:
966 /* TODO: not implemented */
970 /* Hmm, the error type should be compatible to all other types */
974 panic("typeref not skipped");
978 return !is_type_valid(type1) || !is_type_valid(type2);
982 * Skip all typerefs and return the underlying type.
984 type_t *skip_typeref(type_t *type)
986 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
989 switch (type->kind) {
993 qualifiers |= type->base.qualifiers;
995 const typedef_type_t *typedef_type = &type->typedeft;
996 if (typedef_type->resolved_type != NULL) {
997 type = typedef_type->resolved_type;
1000 type = typedef_type->typedefe->type;
1004 qualifiers |= type->base.qualifiers;
1005 type = type->typeoft.typeof_type;
1013 if (qualifiers != TYPE_QUALIFIER_NONE) {
1014 type_t *const copy = duplicate_type(type);
1016 /* for const with typedefed array type the element type has to be
1018 if (is_type_array(copy)) {
1019 type_t *element_type = copy->array.element_type;
1020 element_type = duplicate_type(element_type);
1021 element_type->base.qualifiers |= qualifiers;
1022 copy->array.element_type = element_type;
1024 copy->base.qualifiers |= qualifiers;
1027 type = identify_new_type(copy);
1033 unsigned get_type_size(type_t *type)
1035 switch (type->kind) {
1039 case TYPE_IMAGINARY:
1041 return get_atomic_type_size(type->atomic.akind);
1043 return get_atomic_type_size(type->atomic.akind) * 2;
1044 case TYPE_COMPOUND_UNION:
1045 layout_union_type(&type->compound);
1046 return type->compound.compound->size;
1047 case TYPE_COMPOUND_STRUCT:
1048 layout_struct_type(&type->compound);
1049 return type->compound.compound->size;
1051 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1052 case TYPE_REFERENCE:
1054 return pointer_properties.size;
1056 /* TODO: correct if element_type is aligned? */
1057 il_size_t element_size = get_type_size(type->array.element_type);
1058 return type->array.size * element_size;
1061 return get_type_size(type->typedeft.typedefe->type);
1063 return get_type_size(type->typeoft.typeof_type);
1065 panic("invalid type");
1068 unsigned get_type_alignment(type_t *type)
1070 switch (type->kind) {
1074 case TYPE_IMAGINARY:
1077 return get_atomic_type_alignment(type->atomic.akind);
1078 case TYPE_COMPOUND_UNION:
1079 layout_union_type(&type->compound);
1080 return type->compound.compound->alignment;
1081 case TYPE_COMPOUND_STRUCT:
1082 layout_struct_type(&type->compound);
1083 return type->compound.compound->alignment;
1085 /* gcc says 1 here... */
1087 case TYPE_REFERENCE:
1089 return pointer_properties.alignment;
1091 return get_type_alignment(type->array.element_type);
1092 case TYPE_TYPEDEF: {
1093 il_alignment_t alignment
1094 = get_type_alignment(type->typedeft.typedefe->type);
1095 if (type->typedeft.typedefe->alignment > alignment)
1096 alignment = type->typedeft.typedefe->alignment;
1101 return get_type_alignment(type->typeoft.typeof_type);
1103 panic("invalid type");
1107 * get alignment of a type when used inside a compound.
1108 * Some ABIs are broken and alignment inside a compound is different from
1109 * recommended alignment of a type
1111 static unsigned get_type_alignment_compound(type_t *const type)
1113 assert(!is_typeref(type));
1114 if (type->kind == TYPE_ATOMIC)
1115 return atomic_type_properties[type->atomic.akind].struct_alignment;
1116 return get_type_alignment(type);
1119 decl_modifiers_t get_type_modifiers(const type_t *type)
1121 switch (type->kind) {
1124 case TYPE_COMPOUND_STRUCT:
1125 case TYPE_COMPOUND_UNION:
1126 return type->compound.compound->modifiers;
1128 return type->function.modifiers;
1132 case TYPE_IMAGINARY:
1133 case TYPE_REFERENCE:
1137 case TYPE_TYPEDEF: {
1138 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1139 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1143 return get_type_modifiers(type->typeoft.typeof_type);
1145 panic("invalid type");
1148 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1150 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1153 switch (type->base.kind) {
1155 return TYPE_QUALIFIER_NONE;
1157 qualifiers |= type->base.qualifiers;
1158 const typedef_type_t *typedef_type = &type->typedeft;
1159 if (typedef_type->resolved_type != NULL)
1160 type = typedef_type->resolved_type;
1162 type = typedef_type->typedefe->type;
1165 type = type->typeoft.typeof_type;
1168 if (skip_array_type) {
1169 type = type->array.element_type;
1178 return type->base.qualifiers | qualifiers;
1181 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1183 assert(kind <= ATOMIC_TYPE_LAST);
1184 return atomic_type_properties[kind].size;
1187 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1189 assert(kind <= ATOMIC_TYPE_LAST);
1190 return atomic_type_properties[kind].alignment;
1193 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1195 assert(kind <= ATOMIC_TYPE_LAST);
1196 return atomic_type_properties[kind].flags;
1200 * Find the atomic type kind representing a given size (signed).
1202 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1204 static atomic_type_kind_t kinds[32];
1207 atomic_type_kind_t kind = kinds[size];
1208 if (kind == (atomic_type_kind_t)0) {
1209 static const atomic_type_kind_t possible_kinds[] = {
1214 ATOMIC_TYPE_LONGLONG
1216 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1217 if (get_atomic_type_size(possible_kinds[i]) == size) {
1218 kind = possible_kinds[i];
1228 * Find the atomic type kind representing a given size (signed).
1230 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1232 static atomic_type_kind_t kinds[32];
1235 atomic_type_kind_t kind = kinds[size];
1236 if (kind == (atomic_type_kind_t)0) {
1237 static const atomic_type_kind_t possible_kinds[] = {
1242 ATOMIC_TYPE_ULONGLONG
1244 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1245 if (get_atomic_type_size(possible_kinds[i]) == size) {
1246 kind = possible_kinds[i];
1256 * Hash the given type and return the "singleton" version
1259 type_t *identify_new_type(type_t *type)
1261 type_t *result = typehash_insert(type);
1262 if (result != type) {
1263 obstack_free(&type_obst, type);
1269 * Creates a new atomic type.
1271 * @param akind The kind of the atomic type.
1272 * @param qualifiers Type qualifiers for the new type.
1274 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1276 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1277 type->base.qualifiers = qualifiers;
1278 type->atomic.akind = akind;
1280 return identify_new_type(type);
1284 * Creates a new complex type.
1286 * @param akind The kind of the atomic type.
1287 * @param qualifiers Type qualifiers for the new type.
1289 type_t *make_complex_type(atomic_type_kind_t akind,
1290 type_qualifiers_t qualifiers)
1292 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1293 type->base.qualifiers = qualifiers;
1294 type->atomic.akind = akind;
1296 return identify_new_type(type);
1300 * Creates a new imaginary type.
1302 * @param akind The kind of the atomic type.
1303 * @param qualifiers Type qualifiers for the new type.
1305 type_t *make_imaginary_type(atomic_type_kind_t akind,
1306 type_qualifiers_t qualifiers)
1308 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1309 type->base.qualifiers = qualifiers;
1310 type->atomic.akind = akind;
1312 return identify_new_type(type);
1316 * Creates a new pointer type.
1318 * @param points_to The points-to type for the new type.
1319 * @param qualifiers Type qualifiers for the new type.
1321 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1323 type_t *const type = allocate_type_zero(TYPE_POINTER);
1324 type->base.qualifiers = qualifiers;
1325 type->pointer.points_to = points_to;
1326 type->pointer.base_variable = NULL;
1328 return identify_new_type(type);
1332 * Creates a new reference type.
1334 * @param refers_to The referred-to type for the new type.
1336 type_t *make_reference_type(type_t *refers_to)
1338 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1339 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1340 type->reference.refers_to = refers_to;
1342 return identify_new_type(type);
1346 * Creates a new based pointer type.
1348 * @param points_to The points-to type for the new type.
1349 * @param qualifiers Type qualifiers for the new type.
1350 * @param variable The based variable
1352 type_t *make_based_pointer_type(type_t *points_to,
1353 type_qualifiers_t qualifiers, variable_t *variable)
1355 type_t *const type = allocate_type_zero(TYPE_POINTER);
1356 type->base.qualifiers = qualifiers;
1357 type->pointer.points_to = points_to;
1358 type->pointer.base_variable = variable;
1360 return identify_new_type(type);
1364 type_t *make_array_type(type_t *element_type, size_t size,
1365 type_qualifiers_t qualifiers)
1367 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1368 type->base.qualifiers = qualifiers;
1369 type->array.element_type = element_type;
1370 type->array.size = size;
1371 type->array.size_constant = true;
1373 return identify_new_type(type);
1376 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1377 il_alignment_t *struct_alignment,
1378 bool packed, entity_t *first)
1380 il_size_t offset = *struct_offset;
1381 il_alignment_t alignment = *struct_alignment;
1382 size_t bit_offset = 0;
1385 for (member = first; member != NULL; member = member->base.next) {
1386 if (member->kind != ENTITY_COMPOUND_MEMBER)
1388 if (!member->compound_member.bitfield)
1391 type_t *const base_type = skip_typeref(member->declaration.type);
1392 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1393 il_alignment_t alignment_mask = base_alignment-1;
1394 if (base_alignment > alignment)
1395 alignment = base_alignment;
1397 size_t bit_size = member->compound_member.bit_size;
1399 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1400 offset &= ~alignment_mask;
1401 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1403 if (bit_offset + bit_size > base_size || bit_size == 0) {
1404 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1405 offset = (offset + base_alignment-1) & ~alignment_mask;
1410 if (byte_order_big_endian) {
1411 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1412 member->compound_member.offset = offset & ~alignment_mask;
1413 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1415 member->compound_member.offset = offset;
1416 member->compound_member.bit_offset = bit_offset;
1419 bit_offset += bit_size;
1420 offset += bit_offset / BITS_PER_BYTE;
1421 bit_offset %= BITS_PER_BYTE;
1427 *struct_offset = offset;
1428 *struct_alignment = alignment;
1432 void layout_struct_type(compound_type_t *type)
1434 assert(type->compound != NULL);
1436 compound_t *compound = type->compound;
1437 if (!compound->complete)
1439 if (type->compound->layouted)
1441 compound->layouted = true;
1443 il_size_t offset = 0;
1444 il_alignment_t alignment = compound->alignment;
1445 bool need_pad = false;
1447 entity_t *entry = compound->members.entities;
1448 while (entry != NULL) {
1449 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1452 type_t *const m_type = skip_typeref(entry->declaration.type);
1453 if (!is_type_valid(m_type))
1456 if (entry->compound_member.bitfield) {
1457 entry = pack_bitfield_members(&offset, &alignment,
1458 compound->packed, entry);
1462 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1463 if (m_alignment > alignment)
1464 alignment = m_alignment;
1466 if (!compound->packed) {
1467 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1469 if (new_offset > offset) {
1471 offset = new_offset;
1475 entry->compound_member.offset = offset;
1476 offset += get_type_size(m_type);
1479 entry = entry->base.next;
1482 if (!compound->packed) {
1483 il_size_t new_offset = (offset + alignment-1) & -alignment;
1484 if (new_offset > offset) {
1486 offset = new_offset;
1490 position_t const *const pos = &compound->base.pos;
1492 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1493 } else if (compound->packed) {
1494 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1497 compound->size = offset;
1498 compound->alignment = alignment;
1501 void layout_union_type(compound_type_t *type)
1503 assert(type->compound != NULL);
1505 compound_t *compound = type->compound;
1506 if (! compound->complete)
1508 if (compound->layouted)
1510 compound->layouted = true;
1513 il_alignment_t alignment = compound->alignment;
1515 entity_t *entry = compound->members.entities;
1516 for (; entry != NULL; entry = entry->base.next) {
1517 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1520 type_t *m_type = skip_typeref(entry->declaration.type);
1521 if (! is_type_valid(skip_typeref(m_type)))
1524 entry->compound_member.offset = 0;
1525 il_size_t m_size = get_type_size(m_type);
1528 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1529 if (m_alignment > alignment)
1530 alignment = m_alignment;
1532 size = (size + alignment - 1) & -alignment;
1534 compound->size = size;
1535 compound->alignment = alignment;
1538 function_parameter_t *allocate_parameter(type_t *const type)
1540 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1541 memset(param, 0, sizeof(*param));
1546 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1547 type_t *argument_type2, decl_modifiers_t modifiers)
1549 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1550 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1551 parameter1->next = parameter2;
1553 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1554 type->function.return_type = return_type;
1555 type->function.parameters = parameter1;
1556 type->function.modifiers |= modifiers;
1557 type->function.linkage = LINKAGE_C;
1559 return identify_new_type(type);
1562 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1563 decl_modifiers_t modifiers)
1565 function_parameter_t *const parameter = allocate_parameter(argument_type);
1567 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1568 type->function.return_type = return_type;
1569 type->function.parameters = parameter;
1570 type->function.modifiers |= modifiers;
1571 type->function.linkage = LINKAGE_C;
1573 return identify_new_type(type);
1576 type_t *make_function_1_type_variadic(type_t *return_type,
1577 type_t *argument_type,
1578 decl_modifiers_t modifiers)
1580 function_parameter_t *const parameter = allocate_parameter(argument_type);
1582 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1583 type->function.return_type = return_type;
1584 type->function.parameters = parameter;
1585 type->function.variadic = true;
1586 type->function.modifiers |= modifiers;
1587 type->function.linkage = LINKAGE_C;
1589 return identify_new_type(type);
1592 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1594 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1595 type->function.return_type = return_type;
1596 type->function.parameters = NULL;
1597 type->function.modifiers |= modifiers;
1598 type->function.linkage = LINKAGE_C;
1600 return identify_new_type(type);
1603 type_t *make_function_type(type_t *return_type, int n_types,
1604 type_t *const *argument_types,
1605 decl_modifiers_t modifiers)
1607 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1608 type->function.return_type = return_type;
1609 type->function.modifiers |= modifiers;
1610 type->function.linkage = LINKAGE_C;
1612 function_parameter_t **anchor = &type->function.parameters;
1613 for (int i = 0; i < n_types; ++i) {
1614 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1615 *anchor = parameter;
1616 anchor = ¶meter->next;
1619 return identify_new_type(type);
1623 * Debug helper. Prints the given type to stdout.
1625 static __attribute__((unused))
1626 void dbg_type(const type_t *type)
1628 print_to_file(stderr);