2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
13 #include "type_hash.h"
14 #include "adt/error.h"
16 #include "lang_features.h"
18 #include "diagnostic.h"
20 #include "separator_t.h"
22 /** The default calling convention. */
23 cc_kind_t default_calling_convention = CC_CDECL;
25 static struct obstack type_obst;
26 static bool print_implicit_array_size = false;
28 static void intern_print_type_pre(const type_t *type);
29 static void intern_print_type_post(const type_t *type);
32 * Returns the size of a type node.
34 * @param kind the type kind
36 static size_t get_type_struct_size(type_kind_t kind)
38 static const size_t sizes[] = {
39 [TYPE_ATOMIC] = sizeof(atomic_type_t),
40 [TYPE_IMAGINARY] = sizeof(atomic_type_t),
41 [TYPE_COMPLEX] = sizeof(atomic_type_t),
42 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
43 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
44 [TYPE_ENUM] = sizeof(enum_type_t),
45 [TYPE_FUNCTION] = sizeof(function_type_t),
46 [TYPE_POINTER] = sizeof(pointer_type_t),
47 [TYPE_REFERENCE] = sizeof(reference_type_t),
48 [TYPE_ARRAY] = sizeof(array_type_t),
49 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
50 [TYPE_TYPEOF] = sizeof(typeof_type_t),
52 assert((size_t)kind < lengthof(sizes));
53 assert(sizes[kind] != 0);
57 type_t *allocate_type_zero(type_kind_t kind)
59 size_t const size = get_type_struct_size(kind);
60 type_t *const res = obstack_alloc(&type_obst, size);
62 res->base.kind = kind;
68 * Properties of atomic types.
70 atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
71 [ATOMIC_TYPE_VOID] = {
74 .flags = ATOMIC_TYPE_FLAG_NONE,
77 [ATOMIC_TYPE_BOOL] = {
80 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
83 [ATOMIC_TYPE_CHAR] = {
86 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
89 [ATOMIC_TYPE_SCHAR] = {
92 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
93 | ATOMIC_TYPE_FLAG_SIGNED,
96 [ATOMIC_TYPE_UCHAR] = {
99 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
102 [ATOMIC_TYPE_SHORT] = {
105 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
106 | ATOMIC_TYPE_FLAG_SIGNED,
109 [ATOMIC_TYPE_USHORT] = {
112 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
115 [ATOMIC_TYPE_INT] = {
116 .size = (unsigned) -1,
117 .alignment = (unsigned) -1,
118 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
119 | ATOMIC_TYPE_FLAG_SIGNED,
122 [ATOMIC_TYPE_UINT] = {
123 .size = (unsigned) -1,
124 .alignment = (unsigned) -1,
125 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
128 [ATOMIC_TYPE_LONG] = {
129 .size = (unsigned) -1,
130 .alignment = (unsigned) -1,
131 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
132 | ATOMIC_TYPE_FLAG_SIGNED,
135 [ATOMIC_TYPE_ULONG] = {
136 .size = (unsigned) -1,
137 .alignment = (unsigned) -1,
138 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
141 [ATOMIC_TYPE_LONGLONG] = {
144 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
145 | ATOMIC_TYPE_FLAG_SIGNED,
148 [ATOMIC_TYPE_ULONGLONG] = {
151 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
154 [ATOMIC_TYPE_FLOAT] = {
157 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
158 | ATOMIC_TYPE_FLAG_SIGNED,
161 [ATOMIC_TYPE_DOUBLE] = {
164 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
165 | ATOMIC_TYPE_FLAG_SIGNED,
168 [ATOMIC_TYPE_WCHAR_T] = {
169 .size = (unsigned)-1,
170 .alignment = (unsigned)-1,
171 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
172 .rank = (unsigned)-1,
175 atomic_type_properties_t pointer_properties = {
178 .flags = ATOMIC_TYPE_FLAG_NONE,
181 void init_types(unsigned machine_size)
183 obstack_init(&type_obst);
185 atomic_type_properties_t *props = atomic_type_properties;
187 /* atempt to set some sane defaults based on machine size */
189 unsigned int_size = machine_size < 32 ? 2 : 4;
190 unsigned long_size = machine_size < 64 ? 4 : 8;
192 props[ATOMIC_TYPE_INT].size = int_size;
193 props[ATOMIC_TYPE_INT].alignment = int_size;
194 props[ATOMIC_TYPE_UINT].size = int_size;
195 props[ATOMIC_TYPE_UINT].alignment = int_size;
196 props[ATOMIC_TYPE_LONG].size = long_size;
197 props[ATOMIC_TYPE_LONG].alignment = long_size;
198 props[ATOMIC_TYPE_ULONG].size = long_size;
199 props[ATOMIC_TYPE_ULONG].alignment = long_size;
201 pointer_properties.size = long_size;
202 pointer_properties.alignment = long_size;
203 pointer_properties.struct_alignment = long_size;
205 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
206 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
208 /* set struct alignments to the same value as alignment */
209 for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
210 props[i].struct_alignment = props[i].alignment;
214 void exit_types(void)
216 obstack_free(&type_obst, NULL);
219 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
221 size_t sep = q & QUAL_SEP_START ? 0 : 1;
222 if (qualifiers & TYPE_QUALIFIER_CONST) {
223 print_string(&" const"[sep]);
226 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
227 print_string(&" volatile"[sep]);
230 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
231 print_string(&" restrict"[sep]);
234 if (sep == 0 && q & QUAL_SEP_END)
238 const char *get_atomic_kind_name(atomic_type_kind_t kind)
241 case ATOMIC_TYPE_VOID: return "void";
242 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
243 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
244 case ATOMIC_TYPE_CHAR: return "char";
245 case ATOMIC_TYPE_SCHAR: return "signed char";
246 case ATOMIC_TYPE_UCHAR: return "unsigned char";
247 case ATOMIC_TYPE_INT: return "int";
248 case ATOMIC_TYPE_UINT: return "unsigned int";
249 case ATOMIC_TYPE_SHORT: return "short";
250 case ATOMIC_TYPE_USHORT: return "unsigned short";
251 case ATOMIC_TYPE_LONG: return "long";
252 case ATOMIC_TYPE_ULONG: return "unsigned long";
253 case ATOMIC_TYPE_LONGLONG: return "long long";
254 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
255 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
256 case ATOMIC_TYPE_FLOAT: return "float";
257 case ATOMIC_TYPE_DOUBLE: return "double";
259 return "INVALIDATOMIC";
263 * Prints the name of an atomic type kinds.
265 * @param kind The type kind.
267 static void print_atomic_kinds(atomic_type_kind_t kind)
269 const char *s = get_atomic_kind_name(kind);
274 * Prints the name of an atomic type.
276 * @param type The type.
278 static void print_atomic_type(const atomic_type_t *type)
280 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
281 print_atomic_kinds(type->akind);
285 * Prints the name of a complex type.
287 * @param type The type.
289 static void print_complex_type(const atomic_type_t *type)
291 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
292 print_string("_Complex ");
293 print_atomic_kinds(type->akind);
297 * Prints the name of an imaginary type.
299 * @param type The type.
301 static void print_imaginary_type(const atomic_type_t *type)
303 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
304 print_string("_Imaginary ");
305 print_atomic_kinds(type->akind);
309 * Print the first part (the prefix) of a type.
311 * @param type The type to print.
313 static void print_function_type_pre(const function_type_t *type)
315 switch (type->linkage) {
318 print_string("extern \"C\" ");
322 if (!(c_mode & _CXX))
323 print_string("extern \"C++\" ");
327 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
329 intern_print_type_pre(type->return_type);
331 cc_kind_t cc = type->calling_convention;
334 case CC_CDECL: print_string(" __cdecl"); break;
335 case CC_STDCALL: print_string(" __stdcall"); break;
336 case CC_FASTCALL: print_string(" __fastcall"); break;
337 case CC_THISCALL: print_string(" __thiscall"); break;
339 if (default_calling_convention != CC_CDECL) {
340 /* show the default calling convention if its not cdecl */
341 cc = default_calling_convention;
349 * Print the second part (the postfix) of a type.
351 * @param type The type to print.
353 static void print_function_type_post(const function_type_t *type,
354 const scope_t *parameters)
357 separator_t sep = { "", ", " };
358 if (parameters == NULL) {
359 function_parameter_t *parameter = type->parameters;
360 for ( ; parameter != NULL; parameter = parameter->next) {
361 print_string(sep_next(&sep));
362 print_type(parameter->type);
365 entity_t *parameter = parameters->entities;
366 for (; parameter != NULL; parameter = parameter->base.next) {
367 if (parameter->kind != ENTITY_PARAMETER)
370 print_string(sep_next(&sep));
371 const type_t *const param_type = parameter->declaration.type;
372 if (param_type == NULL) {
373 print_string(parameter->base.symbol->string);
375 print_type_ext(param_type, parameter->base.symbol, NULL);
379 if (type->variadic) {
380 print_string(sep_next(&sep));
383 if (sep_at_first(&sep) && !type->unspecified_parameters) {
384 print_string("void");
388 intern_print_type_post(type->return_type);
392 * Prints the prefix part of a pointer type.
394 * @param type The pointer type.
396 static void print_pointer_type_pre(const pointer_type_t *type)
398 type_t const *const points_to = type->points_to;
399 intern_print_type_pre(points_to);
400 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
402 variable_t *const variable = type->base_variable;
403 if (variable != NULL) {
404 print_string(" __based(");
405 print_string(variable->base.base.symbol->string);
409 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
413 * Prints the postfix part of a pointer type.
415 * @param type The pointer type.
417 static void print_pointer_type_post(const pointer_type_t *type)
419 type_t const *const points_to = type->points_to;
420 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
422 intern_print_type_post(points_to);
426 * Prints the prefix part of a reference type.
428 * @param type The reference type.
430 static void print_reference_type_pre(const reference_type_t *type)
432 type_t const *const refers_to = type->refers_to;
433 intern_print_type_pre(refers_to);
434 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
440 * Prints the postfix part of a reference type.
442 * @param type The reference type.
444 static void print_reference_type_post(const reference_type_t *type)
446 type_t const *const refers_to = type->refers_to;
447 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
449 intern_print_type_post(refers_to);
453 * Prints the prefix part of an array type.
455 * @param type The array type.
457 static void print_array_type_pre(const array_type_t *type)
459 intern_print_type_pre(type->element_type);
463 * Prints the postfix part of an array type.
465 * @param type The array type.
467 static void print_array_type_post(const array_type_t *type)
470 if (type->is_static) {
471 print_string("static ");
473 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
474 if (type->size_expression != NULL
475 && (print_implicit_array_size || !type->has_implicit_size)) {
476 print_expression(type->size_expression);
479 intern_print_type_post(type->element_type);
482 void print_enum_definition(const enum_t *enume)
488 entity_t *entry = enume->base.next;
489 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
490 entry = entry->base.next) {
493 print_string(entry->base.symbol->string);
494 if (entry->enum_value.value != NULL) {
496 print_expression(entry->enum_value.value);
507 * Prints an enum type.
509 * @param type The enum type.
511 static void print_type_enum(const enum_type_t *type)
513 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
514 print_string("enum ");
516 enum_t *enume = type->enume;
517 symbol_t *symbol = enume->base.symbol;
518 if (symbol != NULL) {
519 print_string(symbol->string);
521 print_enum_definition(enume);
525 void print_compound_definition(const compound_t *compound)
530 entity_t *entity = compound->members.entities;
531 for ( ; entity != NULL; entity = entity->base.next) {
532 if (entity->kind != ENTITY_COMPOUND_MEMBER)
536 print_entity(entity);
543 if (compound->modifiers & DM_TRANSPARENT_UNION) {
544 print_string("__attribute__((__transparent_union__))");
549 * Prints a compound type.
551 * @param kind The name of the compound kind.
552 * @param type The compound type.
554 static void print_compound_type(char const *const kind, compound_type_t const *const type)
556 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
559 compound_t *compound = type->compound;
560 symbol_t *symbol = compound->base.symbol;
561 if (symbol != NULL) {
562 print_string(symbol->string);
564 print_compound_definition(compound);
569 * Prints the prefix part of a typedef type.
571 * @param type The typedef type.
573 static void print_typedef_type_pre(const typedef_type_t *const type)
575 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
576 print_string(type->typedefe->base.symbol->string);
580 * Prints the prefix part of a typeof type.
582 * @param type The typeof type.
584 static void print_typeof_type_pre(const typeof_type_t *const type)
586 print_string("typeof(");
587 if (type->expression != NULL) {
588 print_expression(type->expression);
590 print_type(type->typeof_type);
596 * Prints the prefix part of a type.
598 * @param type The type.
600 static void intern_print_type_pre(const type_t *const type)
602 switch (type->kind) {
603 case TYPE_ARRAY: print_array_type_pre( &type->array); return;
604 case TYPE_ATOMIC: print_atomic_type( &type->atomic); return;
605 case TYPE_COMPLEX: print_complex_type( &type->atomic); return;
606 case TYPE_COMPOUND_STRUCT: print_compound_type("struct ", &type->compound); return;
607 case TYPE_COMPOUND_UNION: print_compound_type("union ", &type->compound); return;
608 case TYPE_ENUM: print_type_enum( &type->enumt); return;
609 case TYPE_ERROR: print_string("<error>"); return;
610 case TYPE_FUNCTION: print_function_type_pre( &type->function); return;
611 case TYPE_IMAGINARY: print_imaginary_type( &type->atomic); return;
612 case TYPE_POINTER: print_pointer_type_pre( &type->pointer); return;
613 case TYPE_REFERENCE: print_reference_type_pre( &type->reference); return;
614 case TYPE_TYPEDEF: print_typedef_type_pre( &type->typedeft); return;
615 case TYPE_TYPEOF: print_typeof_type_pre( &type->typeoft); return;
617 print_string("unknown");
621 * Prints the postfix part of a type.
623 * @param type The type.
625 static void intern_print_type_post(const type_t *const type)
627 switch (type->kind) {
629 print_function_type_post(&type->function, NULL);
632 print_pointer_type_post(&type->pointer);
635 print_reference_type_post(&type->reference);
638 print_array_type_post(&type->array);
645 case TYPE_COMPOUND_STRUCT:
646 case TYPE_COMPOUND_UNION:
653 void print_type(const type_t *const type)
655 print_type_ext(type, NULL, NULL);
658 void print_type_ext(const type_t *const type, const symbol_t *symbol,
659 const scope_t *parameters)
661 intern_print_type_pre(type);
662 if (symbol != NULL) {
664 print_string(symbol->string);
666 if (type->kind == TYPE_FUNCTION) {
667 print_function_type_post(&type->function, parameters);
669 intern_print_type_post(type);
673 type_t *duplicate_type(const type_t *type)
675 size_t size = get_type_struct_size(type->kind);
677 type_t *const copy = obstack_copy(&type_obst, type, size);
678 copy->base.firm_type = NULL;
683 type_t *get_unqualified_type(type_t *type)
685 assert(!is_typeref(type));
687 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
690 type_t *unqualified_type = duplicate_type(type);
691 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
693 return identify_new_type(unqualified_type);
696 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
698 type_t *type = skip_typeref(orig_type);
701 if (is_type_array(type)) {
702 /* For array types the element type has to be adjusted */
703 type_t *element_type = type->array.element_type;
704 type_t *qual_element_type = get_qualified_type(element_type, qual);
706 if (qual_element_type == element_type)
709 copy = duplicate_type(type);
710 copy->array.element_type = qual_element_type;
711 } else if (is_type_valid(type)) {
712 if ((type->base.qualifiers & qual) == (int)qual)
715 copy = duplicate_type(type);
716 copy->base.qualifiers |= qual;
721 return identify_new_type(copy);
724 static bool test_atomic_type_flag(atomic_type_kind_t kind,
725 atomic_type_flag_t flag)
727 assert(kind <= ATOMIC_TYPE_LAST);
728 return (atomic_type_properties[kind].flags & flag) != 0;
731 bool is_type_integer(const type_t *type)
733 assert(!is_typeref(type));
734 if (!is_type_arithmetic(type))
736 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
739 bool is_type_enum(const type_t *type)
741 assert(!is_typeref(type));
742 return type->kind == TYPE_ENUM;
745 bool is_type_float(const type_t *type)
747 assert(!is_typeref(type));
749 if (type->kind != TYPE_ATOMIC)
752 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
755 bool is_type_complex(const type_t *type)
757 assert(!is_typeref(type));
758 return type->kind == TYPE_COMPLEX;
761 bool is_type_signed(const type_t *type)
763 assert(!is_typeref(type));
764 if (!is_type_arithmetic(type))
766 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
769 bool is_type_arithmetic(const type_t *type)
771 assert(!is_typeref(type));
773 switch (type->kind) {
779 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
785 bool is_type_real(const type_t *type)
788 return is_type_integer(type) || is_type_float(type);
791 bool is_type_scalar(const type_t *type)
793 assert(!is_typeref(type));
795 switch (type->kind) {
802 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
808 bool is_type_incomplete(const type_t *type)
810 assert(!is_typeref(type));
812 switch (type->kind) {
813 case TYPE_COMPOUND_STRUCT:
814 case TYPE_COMPOUND_UNION: {
815 const compound_type_t *compound_type = &type->compound;
816 return !compound_type->compound->complete;
822 return type->array.size_expression == NULL
823 && !type->array.size_constant;
828 return type->atomic.akind == ATOMIC_TYPE_VOID;
838 panic("typedef not skipped");
841 panic("invalid type");
844 bool is_type_object(const type_t *type)
846 return !is_type_function(type) && !is_type_incomplete(type);
850 * Check if two function types are compatible.
852 static bool function_types_compatible(const function_type_t *func1,
853 const function_type_t *func2)
855 const type_t* const ret1 = skip_typeref(func1->return_type);
856 const type_t* const ret2 = skip_typeref(func2->return_type);
857 if (!types_compatible(ret1, ret2))
860 if (func1->linkage != func2->linkage)
863 cc_kind_t cc1 = func1->calling_convention;
864 if (cc1 == CC_DEFAULT)
865 cc1 = default_calling_convention;
866 cc_kind_t cc2 = func2->calling_convention;
867 if (cc2 == CC_DEFAULT)
868 cc2 = default_calling_convention;
873 if (func1->variadic != func2->variadic)
876 /* can parameters be compared? */
877 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
878 || (func2->unspecified_parameters && !func2->kr_style_parameters))
881 /* TODO: handling of unspecified parameters not correct yet */
883 /* all argument types must be compatible */
884 function_parameter_t *parameter1 = func1->parameters;
885 function_parameter_t *parameter2 = func2->parameters;
886 for ( ; parameter1 != NULL && parameter2 != NULL;
887 parameter1 = parameter1->next, parameter2 = parameter2->next) {
888 type_t *parameter1_type = skip_typeref(parameter1->type);
889 type_t *parameter2_type = skip_typeref(parameter2->type);
891 parameter1_type = get_unqualified_type(parameter1_type);
892 parameter2_type = get_unqualified_type(parameter2_type);
894 if (!types_compatible(parameter1_type, parameter2_type))
897 /* same number of arguments? */
898 if (parameter1 != NULL || parameter2 != NULL)
905 * Check if two array types are compatible.
907 static bool array_types_compatible(const array_type_t *array1,
908 const array_type_t *array2)
910 type_t *element_type1 = skip_typeref(array1->element_type);
911 type_t *element_type2 = skip_typeref(array2->element_type);
912 if (!types_compatible(element_type1, element_type2))
915 if (!array1->size_constant || !array2->size_constant)
918 return array1->size == array2->size;
921 bool types_compatible(const type_t *type1, const type_t *type2)
923 assert(!is_typeref(type1));
924 assert(!is_typeref(type2));
926 /* shortcut: the same type is always compatible */
930 if (type1->base.qualifiers == type2->base.qualifiers &&
931 type1->kind == type2->kind) {
932 switch (type1->kind) {
934 return function_types_compatible(&type1->function, &type2->function);
938 return type1->atomic.akind == type2->atomic.akind;
940 return array_types_compatible(&type1->array, &type2->array);
943 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
944 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
945 return types_compatible(to1, to2);
948 case TYPE_REFERENCE: {
949 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
950 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
951 return types_compatible(to1, to2);
954 case TYPE_COMPOUND_STRUCT:
955 case TYPE_COMPOUND_UNION:
959 /* TODO: not implemented */
963 /* Hmm, the error type should be compatible to all other types */
967 panic("typeref not skipped");
971 return !is_type_valid(type1) || !is_type_valid(type2);
975 * Skip all typerefs and return the underlying type.
977 type_t *skip_typeref(type_t *type)
979 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
982 switch (type->kind) {
986 qualifiers |= type->base.qualifiers;
988 const typedef_type_t *typedef_type = &type->typedeft;
989 if (typedef_type->resolved_type != NULL) {
990 type = typedef_type->resolved_type;
993 type = typedef_type->typedefe->type;
997 qualifiers |= type->base.qualifiers;
998 type = type->typeoft.typeof_type;
1006 if (qualifiers != TYPE_QUALIFIER_NONE) {
1007 type_t *const copy = duplicate_type(type);
1009 /* for const with typedefed array type the element type has to be
1011 if (is_type_array(copy)) {
1012 type_t *element_type = copy->array.element_type;
1013 element_type = duplicate_type(element_type);
1014 element_type->base.qualifiers |= qualifiers;
1015 copy->array.element_type = element_type;
1017 copy->base.qualifiers |= qualifiers;
1020 type = identify_new_type(copy);
1026 unsigned get_type_size(type_t *type)
1028 switch (type->kind) {
1032 case TYPE_IMAGINARY:
1034 return get_atomic_type_size(type->atomic.akind);
1036 return get_atomic_type_size(type->atomic.akind) * 2;
1037 case TYPE_COMPOUND_UNION:
1038 layout_union_type(&type->compound);
1039 return type->compound.compound->size;
1040 case TYPE_COMPOUND_STRUCT:
1041 layout_struct_type(&type->compound);
1042 return type->compound.compound->size;
1044 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1045 case TYPE_REFERENCE:
1047 return pointer_properties.size;
1049 /* TODO: correct if element_type is aligned? */
1050 il_size_t element_size = get_type_size(type->array.element_type);
1051 return type->array.size * element_size;
1054 return get_type_size(type->typedeft.typedefe->type);
1056 return get_type_size(type->typeoft.typeof_type);
1058 panic("invalid type");
1061 unsigned get_type_alignment(type_t *type)
1063 switch (type->kind) {
1067 case TYPE_IMAGINARY:
1070 return get_atomic_type_alignment(type->atomic.akind);
1071 case TYPE_COMPOUND_UNION:
1072 layout_union_type(&type->compound);
1073 return type->compound.compound->alignment;
1074 case TYPE_COMPOUND_STRUCT:
1075 layout_struct_type(&type->compound);
1076 return type->compound.compound->alignment;
1078 /* gcc says 1 here... */
1080 case TYPE_REFERENCE:
1082 return pointer_properties.alignment;
1084 return get_type_alignment(type->array.element_type);
1085 case TYPE_TYPEDEF: {
1086 il_alignment_t alignment
1087 = get_type_alignment(type->typedeft.typedefe->type);
1088 if (type->typedeft.typedefe->alignment > alignment)
1089 alignment = type->typedeft.typedefe->alignment;
1094 return get_type_alignment(type->typeoft.typeof_type);
1096 panic("invalid type");
1100 * get alignment of a type when used inside a compound.
1101 * Some ABIs are broken and alignment inside a compound is different from
1102 * recommended alignment of a type
1104 static unsigned get_type_alignment_compound(type_t *const type)
1106 assert(!is_typeref(type));
1107 if (type->kind == TYPE_ATOMIC)
1108 return atomic_type_properties[type->atomic.akind].struct_alignment;
1109 return get_type_alignment(type);
1112 decl_modifiers_t get_type_modifiers(const type_t *type)
1114 switch (type->kind) {
1117 case TYPE_COMPOUND_STRUCT:
1118 case TYPE_COMPOUND_UNION:
1119 return type->compound.compound->modifiers;
1121 return type->function.modifiers;
1125 case TYPE_IMAGINARY:
1126 case TYPE_REFERENCE:
1130 case TYPE_TYPEDEF: {
1131 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1132 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1136 return get_type_modifiers(type->typeoft.typeof_type);
1138 panic("invalid type");
1141 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1143 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1146 switch (type->base.kind) {
1148 return TYPE_QUALIFIER_NONE;
1150 qualifiers |= type->base.qualifiers;
1151 const typedef_type_t *typedef_type = &type->typedeft;
1152 if (typedef_type->resolved_type != NULL)
1153 type = typedef_type->resolved_type;
1155 type = typedef_type->typedefe->type;
1158 type = type->typeoft.typeof_type;
1161 if (skip_array_type) {
1162 type = type->array.element_type;
1171 return type->base.qualifiers | qualifiers;
1174 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1176 assert(kind <= ATOMIC_TYPE_LAST);
1177 return atomic_type_properties[kind].size;
1180 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1182 assert(kind <= ATOMIC_TYPE_LAST);
1183 return atomic_type_properties[kind].alignment;
1186 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1188 assert(kind <= ATOMIC_TYPE_LAST);
1189 return atomic_type_properties[kind].flags;
1193 * Find the atomic type kind representing a given size (signed).
1195 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1197 static atomic_type_kind_t kinds[32];
1200 atomic_type_kind_t kind = kinds[size];
1201 if (kind == (atomic_type_kind_t)0) {
1202 static const atomic_type_kind_t possible_kinds[] = {
1207 ATOMIC_TYPE_LONGLONG
1209 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1210 if (get_atomic_type_size(possible_kinds[i]) == size) {
1211 kind = possible_kinds[i];
1221 * Find the atomic type kind representing a given size (signed).
1223 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1225 static atomic_type_kind_t kinds[32];
1228 atomic_type_kind_t kind = kinds[size];
1229 if (kind == (atomic_type_kind_t)0) {
1230 static const atomic_type_kind_t possible_kinds[] = {
1235 ATOMIC_TYPE_ULONGLONG
1237 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1238 if (get_atomic_type_size(possible_kinds[i]) == size) {
1239 kind = possible_kinds[i];
1249 * Hash the given type and return the "singleton" version
1252 type_t *identify_new_type(type_t *type)
1254 type_t *result = typehash_insert(type);
1255 if (result != type) {
1256 obstack_free(&type_obst, type);
1262 * Creates a new atomic type.
1264 * @param akind The kind of the atomic type.
1265 * @param qualifiers Type qualifiers for the new type.
1267 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1269 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1270 type->base.qualifiers = qualifiers;
1271 type->atomic.akind = akind;
1273 return identify_new_type(type);
1277 * Creates a new complex type.
1279 * @param akind The kind of the atomic type.
1280 * @param qualifiers Type qualifiers for the new type.
1282 type_t *make_complex_type(atomic_type_kind_t akind,
1283 type_qualifiers_t qualifiers)
1285 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1286 type->base.qualifiers = qualifiers;
1287 type->atomic.akind = akind;
1289 return identify_new_type(type);
1293 * Creates a new imaginary type.
1295 * @param akind The kind of the atomic type.
1296 * @param qualifiers Type qualifiers for the new type.
1298 type_t *make_imaginary_type(atomic_type_kind_t akind,
1299 type_qualifiers_t qualifiers)
1301 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1302 type->base.qualifiers = qualifiers;
1303 type->atomic.akind = akind;
1305 return identify_new_type(type);
1309 * Creates a new pointer type.
1311 * @param points_to The points-to type for the new type.
1312 * @param qualifiers Type qualifiers for the new type.
1314 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1316 type_t *const type = allocate_type_zero(TYPE_POINTER);
1317 type->base.qualifiers = qualifiers;
1318 type->pointer.points_to = points_to;
1319 type->pointer.base_variable = NULL;
1321 return identify_new_type(type);
1325 * Creates a new reference type.
1327 * @param refers_to The referred-to type for the new type.
1329 type_t *make_reference_type(type_t *refers_to)
1331 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1332 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1333 type->reference.refers_to = refers_to;
1335 return identify_new_type(type);
1339 * Creates a new based pointer type.
1341 * @param points_to The points-to type for the new type.
1342 * @param qualifiers Type qualifiers for the new type.
1343 * @param variable The based variable
1345 type_t *make_based_pointer_type(type_t *points_to,
1346 type_qualifiers_t qualifiers, variable_t *variable)
1348 type_t *const type = allocate_type_zero(TYPE_POINTER);
1349 type->base.qualifiers = qualifiers;
1350 type->pointer.points_to = points_to;
1351 type->pointer.base_variable = variable;
1353 return identify_new_type(type);
1357 type_t *make_array_type(type_t *element_type, size_t size,
1358 type_qualifiers_t qualifiers)
1360 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1361 type->base.qualifiers = qualifiers;
1362 type->array.element_type = element_type;
1363 type->array.size = size;
1364 type->array.size_constant = true;
1366 return identify_new_type(type);
1369 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1370 il_alignment_t *struct_alignment,
1371 bool packed, entity_t *first)
1373 il_size_t offset = *struct_offset;
1374 il_alignment_t alignment = *struct_alignment;
1375 size_t bit_offset = 0;
1378 for (member = first; member != NULL; member = member->base.next) {
1379 if (member->kind != ENTITY_COMPOUND_MEMBER)
1381 if (!member->compound_member.bitfield)
1384 type_t *const base_type = skip_typeref(member->declaration.type);
1385 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1386 il_alignment_t alignment_mask = base_alignment-1;
1387 if (base_alignment > alignment)
1388 alignment = base_alignment;
1390 size_t bit_size = member->compound_member.bit_size;
1392 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1393 offset &= ~alignment_mask;
1394 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1396 if (bit_offset + bit_size > base_size || bit_size == 0) {
1397 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1398 offset = (offset + base_alignment-1) & ~alignment_mask;
1403 if (byte_order_big_endian) {
1404 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1405 member->compound_member.offset = offset & ~alignment_mask;
1406 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1408 member->compound_member.offset = offset;
1409 member->compound_member.bit_offset = bit_offset;
1412 bit_offset += bit_size;
1413 offset += bit_offset / BITS_PER_BYTE;
1414 bit_offset %= BITS_PER_BYTE;
1420 *struct_offset = offset;
1421 *struct_alignment = alignment;
1425 void layout_struct_type(compound_type_t *type)
1427 assert(type->compound != NULL);
1429 compound_t *compound = type->compound;
1430 if (!compound->complete)
1432 if (type->compound->layouted)
1434 compound->layouted = true;
1436 il_size_t offset = 0;
1437 il_alignment_t alignment = compound->alignment;
1438 bool need_pad = false;
1440 entity_t *entry = compound->members.entities;
1441 while (entry != NULL) {
1442 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1445 type_t *const m_type = skip_typeref(entry->declaration.type);
1446 if (!is_type_valid(m_type))
1449 if (entry->compound_member.bitfield) {
1450 entry = pack_bitfield_members(&offset, &alignment,
1451 compound->packed, entry);
1455 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1456 if (m_alignment > alignment)
1457 alignment = m_alignment;
1459 if (!compound->packed) {
1460 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1462 if (new_offset > offset) {
1464 offset = new_offset;
1468 entry->compound_member.offset = offset;
1469 offset += get_type_size(m_type);
1472 entry = entry->base.next;
1475 if (!compound->packed) {
1476 il_size_t new_offset = (offset + alignment-1) & -alignment;
1477 if (new_offset > offset) {
1479 offset = new_offset;
1483 position_t const *const pos = &compound->base.pos;
1485 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1486 } else if (compound->packed) {
1487 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1490 compound->size = offset;
1491 compound->alignment = alignment;
1494 void layout_union_type(compound_type_t *type)
1496 assert(type->compound != NULL);
1498 compound_t *compound = type->compound;
1499 if (! compound->complete)
1501 if (compound->layouted)
1503 compound->layouted = true;
1506 il_alignment_t alignment = compound->alignment;
1508 entity_t *entry = compound->members.entities;
1509 for (; entry != NULL; entry = entry->base.next) {
1510 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1513 type_t *m_type = skip_typeref(entry->declaration.type);
1514 if (! is_type_valid(skip_typeref(m_type)))
1517 entry->compound_member.offset = 0;
1518 il_size_t m_size = get_type_size(m_type);
1521 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1522 if (m_alignment > alignment)
1523 alignment = m_alignment;
1525 size = (size + alignment - 1) & -alignment;
1527 compound->size = size;
1528 compound->alignment = alignment;
1531 function_parameter_t *allocate_parameter(type_t *const type)
1533 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1534 memset(param, 0, sizeof(*param));
1539 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1540 type_t *argument_type2, decl_modifiers_t modifiers)
1542 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1543 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1544 parameter1->next = parameter2;
1546 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1547 type->function.return_type = return_type;
1548 type->function.parameters = parameter1;
1549 type->function.modifiers |= modifiers;
1550 type->function.linkage = LINKAGE_C;
1552 return identify_new_type(type);
1555 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1556 decl_modifiers_t modifiers)
1558 function_parameter_t *const parameter = allocate_parameter(argument_type);
1560 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1561 type->function.return_type = return_type;
1562 type->function.parameters = parameter;
1563 type->function.modifiers |= modifiers;
1564 type->function.linkage = LINKAGE_C;
1566 return identify_new_type(type);
1569 type_t *make_function_1_type_variadic(type_t *return_type,
1570 type_t *argument_type,
1571 decl_modifiers_t modifiers)
1573 function_parameter_t *const parameter = allocate_parameter(argument_type);
1575 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1576 type->function.return_type = return_type;
1577 type->function.parameters = parameter;
1578 type->function.variadic = true;
1579 type->function.modifiers |= modifiers;
1580 type->function.linkage = LINKAGE_C;
1582 return identify_new_type(type);
1585 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1587 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1588 type->function.return_type = return_type;
1589 type->function.parameters = NULL;
1590 type->function.modifiers |= modifiers;
1591 type->function.linkage = LINKAGE_C;
1593 return identify_new_type(type);
1596 type_t *make_function_type(type_t *return_type, int n_types,
1597 type_t *const *argument_types,
1598 decl_modifiers_t modifiers)
1600 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1601 type->function.return_type = return_type;
1602 type->function.modifiers |= modifiers;
1603 type->function.linkage = LINKAGE_C;
1605 function_parameter_t **anchor = &type->function.parameters;
1606 for (int i = 0; i < n_types; ++i) {
1607 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1608 *anchor = parameter;
1609 anchor = ¶meter->next;
1612 return identify_new_type(type);
1616 * Debug helper. Prints the given type to stdout.
1618 static __attribute__((unused))
1619 void dbg_type(const type_t *type)
1621 print_to_file(stderr);