2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
9 #include "adt/bitfiddle.h"
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 void init_types(unsigned machine_size)
184 obstack_init(&type_obst);
186 atomic_type_properties_t *props = atomic_type_properties;
188 /* atempt to set some sane defaults based on machine size */
190 unsigned int_size = machine_size < 32 ? 2 : 4;
191 unsigned long_size = machine_size < 64 ? 4 : 8;
193 props[ATOMIC_TYPE_INT].size = int_size;
194 props[ATOMIC_TYPE_INT].alignment = int_size;
195 props[ATOMIC_TYPE_UINT].size = int_size;
196 props[ATOMIC_TYPE_UINT].alignment = int_size;
197 props[ATOMIC_TYPE_LONG].size = long_size;
198 props[ATOMIC_TYPE_LONG].alignment = long_size;
199 props[ATOMIC_TYPE_ULONG].size = long_size;
200 props[ATOMIC_TYPE_ULONG].alignment = long_size;
202 pointer_properties.size = long_size;
203 pointer_properties.alignment = long_size;
204 pointer_properties.struct_alignment = long_size;
206 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
207 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
209 /* set struct alignments to the same value as alignment */
210 for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
211 props[i].struct_alignment = props[i].alignment;
215 void exit_types(void)
217 obstack_free(&type_obst, NULL);
220 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
222 size_t sep = q & QUAL_SEP_START ? 0 : 1;
223 if (qualifiers & TYPE_QUALIFIER_CONST) {
224 print_string(&" const"[sep]);
227 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
228 print_string(&" volatile"[sep]);
231 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
232 print_string(&" restrict"[sep]);
235 if (sep == 0 && q & QUAL_SEP_END)
239 const char *get_atomic_kind_name(atomic_type_kind_t kind)
242 case ATOMIC_TYPE_VOID: return "void";
243 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
244 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
245 case ATOMIC_TYPE_CHAR: return "char";
246 case ATOMIC_TYPE_SCHAR: return "signed char";
247 case ATOMIC_TYPE_UCHAR: return "unsigned char";
248 case ATOMIC_TYPE_INT: return "int";
249 case ATOMIC_TYPE_UINT: return "unsigned int";
250 case ATOMIC_TYPE_SHORT: return "short";
251 case ATOMIC_TYPE_USHORT: return "unsigned short";
252 case ATOMIC_TYPE_LONG: return "long";
253 case ATOMIC_TYPE_ULONG: return "unsigned long";
254 case ATOMIC_TYPE_LONGLONG: return "long long";
255 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
256 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
257 case ATOMIC_TYPE_FLOAT: return "float";
258 case ATOMIC_TYPE_DOUBLE: return "double";
260 return "INVALIDATOMIC";
264 * Prints the name of an atomic type kinds.
266 * @param kind The type kind.
268 static void print_atomic_kinds(atomic_type_kind_t kind)
270 const char *s = get_atomic_kind_name(kind);
275 * Prints the name of an atomic type.
277 * @param type The type.
279 static void print_atomic_type(const atomic_type_t *type)
281 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
282 print_atomic_kinds(type->akind);
286 * Prints the name of a complex type.
288 * @param type The type.
290 static void print_complex_type(const atomic_type_t *type)
292 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
293 print_string("_Complex ");
294 print_atomic_kinds(type->akind);
298 * Prints the name of an imaginary type.
300 * @param type The type.
302 static void print_imaginary_type(const atomic_type_t *type)
304 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
305 print_string("_Imaginary ");
306 print_atomic_kinds(type->akind);
310 * Print the first part (the prefix) of a type.
312 * @param type The type to print.
314 static void print_function_type_pre(const function_type_t *type)
316 switch (type->linkage) {
319 print_string("extern \"C\" ");
323 if (!(c_mode & _CXX))
324 print_string("extern \"C++\" ");
328 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
330 intern_print_type_pre(type->return_type);
332 cc_kind_t cc = type->calling_convention;
335 case CC_CDECL: print_string(" __cdecl"); break;
336 case CC_STDCALL: print_string(" __stdcall"); break;
337 case CC_FASTCALL: print_string(" __fastcall"); break;
338 case CC_THISCALL: print_string(" __thiscall"); break;
340 if (default_calling_convention != CC_CDECL) {
341 /* show the default calling convention if its not cdecl */
342 cc = default_calling_convention;
350 * Print the second part (the postfix) of a type.
352 * @param type The type to print.
354 static void print_function_type_post(const function_type_t *type,
355 const scope_t *parameters)
358 separator_t sep = { "", ", " };
359 if (parameters == NULL) {
360 function_parameter_t *parameter = type->parameters;
361 for ( ; parameter != NULL; parameter = parameter->next) {
362 print_string(sep_next(&sep));
363 print_type(parameter->type);
366 entity_t *parameter = parameters->entities;
367 for (; parameter != NULL; parameter = parameter->base.next) {
368 if (parameter->kind != ENTITY_PARAMETER)
371 print_string(sep_next(&sep));
372 const type_t *const param_type = parameter->declaration.type;
373 if (param_type == NULL) {
374 print_string(parameter->base.symbol->string);
376 print_type_ext(param_type, parameter->base.symbol, NULL);
380 if (type->variadic) {
381 print_string(sep_next(&sep));
384 if (sep_at_first(&sep) && !type->unspecified_parameters) {
385 print_string("void");
389 intern_print_type_post(type->return_type);
393 * Prints the prefix part of a pointer type.
395 * @param type The pointer type.
397 static void print_pointer_type_pre(const pointer_type_t *type)
399 type_t const *const points_to = type->points_to;
400 intern_print_type_pre(points_to);
401 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
403 variable_t *const variable = type->base_variable;
404 if (variable != NULL) {
405 print_string(" __based(");
406 print_string(variable->base.base.symbol->string);
410 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
414 * Prints the postfix part of a pointer type.
416 * @param type The pointer type.
418 static void print_pointer_type_post(const pointer_type_t *type)
420 type_t const *const points_to = type->points_to;
421 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
423 intern_print_type_post(points_to);
427 * Prints the prefix part of a reference type.
429 * @param type The reference type.
431 static void print_reference_type_pre(const reference_type_t *type)
433 type_t const *const refers_to = type->refers_to;
434 intern_print_type_pre(refers_to);
435 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
441 * Prints the postfix part of a reference type.
443 * @param type The reference type.
445 static void print_reference_type_post(const reference_type_t *type)
447 type_t const *const refers_to = type->refers_to;
448 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
450 intern_print_type_post(refers_to);
454 * Prints the prefix part of an array type.
456 * @param type The array type.
458 static void print_array_type_pre(const array_type_t *type)
460 intern_print_type_pre(type->element_type);
464 * Prints the postfix part of an array type.
466 * @param type The array type.
468 static void print_array_type_post(const array_type_t *type)
471 if (type->is_static) {
472 print_string("static ");
474 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
475 if (type->size_expression != NULL
476 && (print_implicit_array_size || !type->has_implicit_size)) {
477 print_expression(type->size_expression);
480 intern_print_type_post(type->element_type);
483 void print_enum_definition(const enum_t *enume)
489 entity_t *entry = enume->base.next;
490 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
491 entry = entry->base.next) {
494 print_string(entry->base.symbol->string);
495 if (entry->enum_value.value != NULL) {
497 print_expression(entry->enum_value.value);
508 * Prints an enum type.
510 * @param type The enum type.
512 static void print_type_enum(const enum_type_t *type)
514 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
515 print_string("enum ");
517 enum_t *enume = type->enume;
518 symbol_t *symbol = enume->base.symbol;
519 if (symbol != NULL) {
520 print_string(symbol->string);
522 print_enum_definition(enume);
526 void print_compound_definition(const compound_t *compound)
531 entity_t *entity = compound->members.entities;
532 for ( ; entity != NULL; entity = entity->base.next) {
533 if (entity->kind != ENTITY_COMPOUND_MEMBER)
537 print_entity(entity);
544 if (compound->modifiers & DM_TRANSPARENT_UNION) {
545 print_string("__attribute__((__transparent_union__))");
550 * Prints a compound type.
552 * @param kind The name of the compound kind.
553 * @param type The compound type.
555 static void print_compound_type(char const *const kind, compound_type_t const *const type)
557 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
560 compound_t *compound = type->compound;
561 symbol_t *symbol = compound->base.symbol;
562 if (symbol != NULL) {
563 print_string(symbol->string);
565 print_compound_definition(compound);
570 * Prints the prefix part of a typedef type.
572 * @param type The typedef type.
574 static void print_typedef_type_pre(const typedef_type_t *const type)
576 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
577 print_string(type->typedefe->base.symbol->string);
581 * Prints the prefix part of a typeof type.
583 * @param type The typeof type.
585 static void print_typeof_type_pre(const typeof_type_t *const type)
587 print_string("typeof(");
588 if (type->expression != NULL) {
589 print_expression(type->expression);
591 print_type(type->typeof_type);
597 * Prints the prefix part of a type.
599 * @param type The type.
601 static void intern_print_type_pre(const type_t *const type)
603 switch (type->kind) {
604 case TYPE_ARRAY: print_array_type_pre( &type->array); return;
605 case TYPE_ATOMIC: print_atomic_type( &type->atomic); return;
606 case TYPE_COMPLEX: print_complex_type( &type->atomic); return;
607 case TYPE_COMPOUND_STRUCT: print_compound_type("struct ", &type->compound); return;
608 case TYPE_COMPOUND_UNION: print_compound_type("union ", &type->compound); return;
609 case TYPE_ENUM: print_type_enum( &type->enumt); return;
610 case TYPE_ERROR: print_string("<error>"); return;
611 case TYPE_FUNCTION: print_function_type_pre( &type->function); return;
612 case TYPE_IMAGINARY: print_imaginary_type( &type->atomic); return;
613 case TYPE_POINTER: print_pointer_type_pre( &type->pointer); return;
614 case TYPE_REFERENCE: print_reference_type_pre( &type->reference); return;
615 case TYPE_TYPEDEF: print_typedef_type_pre( &type->typedeft); return;
616 case TYPE_TYPEOF: print_typeof_type_pre( &type->typeoft); return;
618 print_string("unknown");
622 * Prints the postfix part of a type.
624 * @param type The type.
626 static void intern_print_type_post(const type_t *const type)
628 switch (type->kind) {
630 print_function_type_post(&type->function, NULL);
633 print_pointer_type_post(&type->pointer);
636 print_reference_type_post(&type->reference);
639 print_array_type_post(&type->array);
646 case TYPE_COMPOUND_STRUCT:
647 case TYPE_COMPOUND_UNION:
654 void print_type(const type_t *const type)
656 print_type_ext(type, NULL, NULL);
659 void print_type_ext(const type_t *const type, const symbol_t *symbol,
660 const scope_t *parameters)
662 intern_print_type_pre(type);
663 if (symbol != NULL) {
665 print_string(symbol->string);
667 if (type->kind == TYPE_FUNCTION) {
668 print_function_type_post(&type->function, parameters);
670 intern_print_type_post(type);
674 type_t *duplicate_type(const type_t *type)
676 size_t size = get_type_struct_size(type->kind);
678 type_t *const copy = obstack_copy(&type_obst, type, size);
679 copy->base.firm_type = NULL;
684 type_t *get_unqualified_type(type_t *type)
686 assert(!is_typeref(type));
688 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
691 type_t *unqualified_type = duplicate_type(type);
692 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
694 return identify_new_type(unqualified_type);
697 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
699 type_t *type = skip_typeref(orig_type);
702 if (is_type_array(type)) {
703 /* For array types the element type has to be adjusted */
704 type_t *element_type = type->array.element_type;
705 type_t *qual_element_type = get_qualified_type(element_type, qual);
707 if (qual_element_type == element_type)
710 copy = duplicate_type(type);
711 copy->array.element_type = qual_element_type;
712 } else if (is_type_valid(type)) {
713 if ((type->base.qualifiers & qual) == (int)qual)
716 copy = duplicate_type(type);
717 copy->base.qualifiers |= qual;
722 return identify_new_type(copy);
725 static bool test_atomic_type_flag(atomic_type_kind_t kind,
726 atomic_type_flag_t flag)
728 assert(kind <= ATOMIC_TYPE_LAST);
729 return (atomic_type_properties[kind].flags & flag) != 0;
732 bool is_type_integer(const type_t *type)
734 assert(!is_typeref(type));
735 if (!is_type_arithmetic(type))
737 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
740 bool is_type_enum(const type_t *type)
742 assert(!is_typeref(type));
743 return type->kind == TYPE_ENUM;
746 bool is_type_float(const type_t *type)
748 assert(!is_typeref(type));
750 if (type->kind != TYPE_ATOMIC)
753 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
756 bool is_type_complex(const type_t *type)
758 assert(!is_typeref(type));
759 return type->kind == TYPE_COMPLEX;
762 bool is_type_signed(const type_t *type)
764 assert(!is_typeref(type));
765 if (!is_type_arithmetic(type))
767 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
770 bool is_type_arithmetic(const type_t *type)
772 assert(!is_typeref(type));
774 switch (type->kind) {
780 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
786 bool is_type_real(const type_t *type)
789 return is_type_integer(type) || is_type_float(type);
792 bool is_type_scalar(const type_t *type)
794 assert(!is_typeref(type));
796 switch (type->kind) {
803 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
809 bool is_type_incomplete(const type_t *type)
811 assert(!is_typeref(type));
813 switch (type->kind) {
814 case TYPE_COMPOUND_STRUCT:
815 case TYPE_COMPOUND_UNION: {
816 const compound_type_t *compound_type = &type->compound;
817 return !compound_type->compound->complete;
823 return type->array.size_expression == NULL
824 && !type->array.size_constant;
829 return type->atomic.akind == ATOMIC_TYPE_VOID;
839 panic("typedef not skipped");
842 panic("invalid type");
845 bool is_type_object(const type_t *type)
847 return !is_type_function(type) && !is_type_incomplete(type);
851 * Check if two function types are compatible.
853 static bool function_types_compatible(const function_type_t *func1,
854 const function_type_t *func2)
856 const type_t* const ret1 = skip_typeref(func1->return_type);
857 const type_t* const ret2 = skip_typeref(func2->return_type);
858 if (!types_compatible(ret1, ret2))
861 if (func1->linkage != func2->linkage)
864 cc_kind_t cc1 = func1->calling_convention;
865 if (cc1 == CC_DEFAULT)
866 cc1 = default_calling_convention;
867 cc_kind_t cc2 = func2->calling_convention;
868 if (cc2 == CC_DEFAULT)
869 cc2 = default_calling_convention;
874 if (func1->variadic != func2->variadic)
877 /* can parameters be compared? */
878 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
879 || (func2->unspecified_parameters && !func2->kr_style_parameters))
882 /* TODO: handling of unspecified parameters not correct yet */
884 /* all argument types must be compatible */
885 function_parameter_t *parameter1 = func1->parameters;
886 function_parameter_t *parameter2 = func2->parameters;
887 for ( ; parameter1 != NULL && parameter2 != NULL;
888 parameter1 = parameter1->next, parameter2 = parameter2->next) {
889 type_t *parameter1_type = skip_typeref(parameter1->type);
890 type_t *parameter2_type = skip_typeref(parameter2->type);
892 parameter1_type = get_unqualified_type(parameter1_type);
893 parameter2_type = get_unqualified_type(parameter2_type);
895 if (!types_compatible(parameter1_type, parameter2_type))
898 /* same number of arguments? */
899 if (parameter1 != NULL || parameter2 != NULL)
906 * Check if two array types are compatible.
908 static bool array_types_compatible(const array_type_t *array1,
909 const array_type_t *array2)
911 type_t *element_type1 = skip_typeref(array1->element_type);
912 type_t *element_type2 = skip_typeref(array2->element_type);
913 if (!types_compatible(element_type1, element_type2))
916 if (!array1->size_constant || !array2->size_constant)
919 return array1->size == array2->size;
922 bool types_compatible(const type_t *type1, const type_t *type2)
924 assert(!is_typeref(type1));
925 assert(!is_typeref(type2));
927 /* shortcut: the same type is always compatible */
931 if (type1->base.qualifiers == type2->base.qualifiers &&
932 type1->kind == type2->kind) {
933 switch (type1->kind) {
935 return function_types_compatible(&type1->function, &type2->function);
939 return type1->atomic.akind == type2->atomic.akind;
941 return array_types_compatible(&type1->array, &type2->array);
944 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
945 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
946 return types_compatible(to1, to2);
949 case TYPE_REFERENCE: {
950 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
951 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
952 return types_compatible(to1, to2);
955 case TYPE_COMPOUND_STRUCT:
956 case TYPE_COMPOUND_UNION:
960 /* TODO: not implemented */
964 /* Hmm, the error type should be compatible to all other types */
968 panic("typeref not skipped");
972 return !is_type_valid(type1) || !is_type_valid(type2);
976 * Skip all typerefs and return the underlying type.
978 type_t *skip_typeref(type_t *type)
980 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
983 switch (type->kind) {
987 qualifiers |= type->base.qualifiers;
989 const typedef_type_t *typedef_type = &type->typedeft;
990 if (typedef_type->resolved_type != NULL) {
991 type = typedef_type->resolved_type;
994 type = typedef_type->typedefe->type;
998 qualifiers |= type->base.qualifiers;
999 type = type->typeoft.typeof_type;
1007 if (qualifiers != TYPE_QUALIFIER_NONE) {
1008 type_t *const copy = duplicate_type(type);
1010 /* for const with typedefed array type the element type has to be
1012 if (is_type_array(copy)) {
1013 type_t *element_type = copy->array.element_type;
1014 element_type = duplicate_type(element_type);
1015 element_type->base.qualifiers |= qualifiers;
1016 copy->array.element_type = element_type;
1018 copy->base.qualifiers |= qualifiers;
1021 type = identify_new_type(copy);
1027 unsigned get_type_size(type_t *type)
1029 switch (type->kind) {
1033 case TYPE_IMAGINARY:
1035 return get_atomic_type_size(type->atomic.akind);
1037 return get_atomic_type_size(type->atomic.akind) * 2;
1038 case TYPE_COMPOUND_UNION:
1039 layout_union_type(&type->compound);
1040 return type->compound.compound->size;
1041 case TYPE_COMPOUND_STRUCT:
1042 layout_struct_type(&type->compound);
1043 return type->compound.compound->size;
1045 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1046 case TYPE_REFERENCE:
1048 return pointer_properties.size;
1050 /* TODO: correct if element_type is aligned? */
1051 il_size_t element_size = get_type_size(type->array.element_type);
1052 return type->array.size * element_size;
1055 return get_type_size(type->typedeft.typedefe->type);
1057 return get_type_size(type->typeoft.typeof_type);
1059 panic("invalid type");
1062 unsigned get_type_alignment(type_t *type)
1064 switch (type->kind) {
1068 case TYPE_IMAGINARY:
1071 return get_atomic_type_alignment(type->atomic.akind);
1072 case TYPE_COMPOUND_UNION:
1073 layout_union_type(&type->compound);
1074 return type->compound.compound->alignment;
1075 case TYPE_COMPOUND_STRUCT:
1076 layout_struct_type(&type->compound);
1077 return type->compound.compound->alignment;
1079 /* gcc says 1 here... */
1081 case TYPE_REFERENCE:
1083 return pointer_properties.alignment;
1085 return get_type_alignment(type->array.element_type);
1086 case TYPE_TYPEDEF: {
1087 il_alignment_t alignment
1088 = get_type_alignment(type->typedeft.typedefe->type);
1089 if (type->typedeft.typedefe->alignment > alignment)
1090 alignment = type->typedeft.typedefe->alignment;
1095 return get_type_alignment(type->typeoft.typeof_type);
1097 panic("invalid type");
1101 * get alignment of a type when used inside a compound.
1102 * Some ABIs are broken and alignment inside a compound is different from
1103 * recommended alignment of a type
1105 static unsigned get_type_alignment_compound(type_t *const type)
1107 assert(!is_typeref(type));
1108 if (type->kind == TYPE_ATOMIC)
1109 return atomic_type_properties[type->atomic.akind].struct_alignment;
1110 return get_type_alignment(type);
1113 decl_modifiers_t get_type_modifiers(const type_t *type)
1115 switch (type->kind) {
1118 case TYPE_COMPOUND_STRUCT:
1119 case TYPE_COMPOUND_UNION:
1120 return type->compound.compound->modifiers;
1122 return type->function.modifiers;
1126 case TYPE_IMAGINARY:
1127 case TYPE_REFERENCE:
1131 case TYPE_TYPEDEF: {
1132 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1133 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1137 return get_type_modifiers(type->typeoft.typeof_type);
1139 panic("invalid type");
1142 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1144 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1147 switch (type->base.kind) {
1149 return TYPE_QUALIFIER_NONE;
1151 qualifiers |= type->base.qualifiers;
1152 const typedef_type_t *typedef_type = &type->typedeft;
1153 if (typedef_type->resolved_type != NULL)
1154 type = typedef_type->resolved_type;
1156 type = typedef_type->typedefe->type;
1159 type = type->typeoft.typeof_type;
1162 if (skip_array_type) {
1163 type = type->array.element_type;
1172 return type->base.qualifiers | qualifiers;
1175 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1177 assert(kind <= ATOMIC_TYPE_LAST);
1178 return atomic_type_properties[kind].size;
1181 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1183 assert(kind <= ATOMIC_TYPE_LAST);
1184 return atomic_type_properties[kind].alignment;
1187 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1189 assert(kind <= ATOMIC_TYPE_LAST);
1190 return atomic_type_properties[kind].flags;
1194 * Find the atomic type kind representing a given size (signed).
1196 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1198 static atomic_type_kind_t kinds[32];
1201 atomic_type_kind_t kind = kinds[size];
1202 if (kind == (atomic_type_kind_t)0) {
1203 static const atomic_type_kind_t possible_kinds[] = {
1208 ATOMIC_TYPE_LONGLONG
1210 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1211 if (get_atomic_type_size(possible_kinds[i]) == size) {
1212 kind = possible_kinds[i];
1222 * Find the atomic type kind representing a given size (signed).
1224 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1226 static atomic_type_kind_t kinds[32];
1229 atomic_type_kind_t kind = kinds[size];
1230 if (kind == (atomic_type_kind_t)0) {
1231 static const atomic_type_kind_t possible_kinds[] = {
1236 ATOMIC_TYPE_ULONGLONG
1238 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1239 if (get_atomic_type_size(possible_kinds[i]) == size) {
1240 kind = possible_kinds[i];
1250 * Hash the given type and return the "singleton" version
1253 type_t *identify_new_type(type_t *type)
1255 type_t *result = typehash_insert(type);
1256 if (result != type) {
1257 obstack_free(&type_obst, type);
1263 * Creates a new atomic type.
1265 * @param akind The kind of the atomic type.
1266 * @param qualifiers Type qualifiers for the new type.
1268 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1270 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1271 type->base.qualifiers = qualifiers;
1272 type->atomic.akind = akind;
1274 return identify_new_type(type);
1278 * Creates a new complex type.
1280 * @param akind The kind of the atomic type.
1281 * @param qualifiers Type qualifiers for the new type.
1283 type_t *make_complex_type(atomic_type_kind_t akind,
1284 type_qualifiers_t qualifiers)
1286 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1287 type->base.qualifiers = qualifiers;
1288 type->atomic.akind = akind;
1290 return identify_new_type(type);
1294 * Creates a new imaginary type.
1296 * @param akind The kind of the atomic type.
1297 * @param qualifiers Type qualifiers for the new type.
1299 type_t *make_imaginary_type(atomic_type_kind_t akind,
1300 type_qualifiers_t qualifiers)
1302 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1303 type->base.qualifiers = qualifiers;
1304 type->atomic.akind = akind;
1306 return identify_new_type(type);
1310 * Creates a new pointer type.
1312 * @param points_to The points-to type for the new type.
1313 * @param qualifiers Type qualifiers for the new type.
1315 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1317 type_t *const type = allocate_type_zero(TYPE_POINTER);
1318 type->base.qualifiers = qualifiers;
1319 type->pointer.points_to = points_to;
1320 type->pointer.base_variable = NULL;
1322 return identify_new_type(type);
1326 * Creates a new reference type.
1328 * @param refers_to The referred-to type for the new type.
1330 type_t *make_reference_type(type_t *refers_to)
1332 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1333 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1334 type->reference.refers_to = refers_to;
1336 return identify_new_type(type);
1340 * Creates a new based pointer type.
1342 * @param points_to The points-to type for the new type.
1343 * @param qualifiers Type qualifiers for the new type.
1344 * @param variable The based variable
1346 type_t *make_based_pointer_type(type_t *points_to,
1347 type_qualifiers_t qualifiers, variable_t *variable)
1349 type_t *const type = allocate_type_zero(TYPE_POINTER);
1350 type->base.qualifiers = qualifiers;
1351 type->pointer.points_to = points_to;
1352 type->pointer.base_variable = variable;
1354 return identify_new_type(type);
1358 type_t *make_array_type(type_t *element_type, size_t size,
1359 type_qualifiers_t qualifiers)
1361 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1362 type->base.qualifiers = qualifiers;
1363 type->array.element_type = element_type;
1364 type->array.size = size;
1365 type->array.size_constant = true;
1367 return identify_new_type(type);
1370 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1371 il_alignment_t *struct_alignment,
1372 bool packed, entity_t *first)
1374 il_size_t offset = *struct_offset;
1375 il_alignment_t alignment = *struct_alignment;
1376 size_t bit_offset = 0;
1379 for (member = first; member != NULL; member = member->base.next) {
1380 if (member->kind != ENTITY_COMPOUND_MEMBER)
1382 if (!member->compound_member.bitfield)
1385 type_t *const base_type = skip_typeref(member->declaration.type);
1386 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1387 il_alignment_t alignment_mask = base_alignment-1;
1388 if (base_alignment > alignment)
1389 alignment = base_alignment;
1391 size_t bit_size = member->compound_member.bit_size;
1393 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1394 offset &= ~alignment_mask;
1395 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1397 if (bit_offset + bit_size > base_size || bit_size == 0) {
1398 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1399 offset = (offset + base_alignment-1) & ~alignment_mask;
1404 if (byte_order_big_endian) {
1405 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1406 member->compound_member.offset = offset & ~alignment_mask;
1407 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1409 member->compound_member.offset = offset;
1410 member->compound_member.bit_offset = bit_offset;
1413 bit_offset += bit_size;
1414 offset += bit_offset / BITS_PER_BYTE;
1415 bit_offset %= BITS_PER_BYTE;
1421 *struct_offset = offset;
1422 *struct_alignment = alignment;
1426 void layout_struct_type(compound_type_t *type)
1428 assert(type->compound != NULL);
1430 compound_t *compound = type->compound;
1431 if (!compound->complete)
1433 if (type->compound->layouted)
1435 compound->layouted = true;
1437 il_size_t offset = 0;
1438 il_alignment_t alignment = compound->alignment;
1439 bool need_pad = false;
1441 entity_t *entry = compound->members.entities;
1442 while (entry != NULL) {
1443 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1446 type_t *const m_type = skip_typeref(entry->declaration.type);
1447 if (!is_type_valid(m_type))
1450 if (entry->compound_member.bitfield) {
1451 entry = pack_bitfield_members(&offset, &alignment,
1452 compound->packed, entry);
1456 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1457 if (m_alignment > alignment)
1458 alignment = m_alignment;
1460 if (!compound->packed) {
1461 il_size_t const new_offset = round_up2(offset, 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 const new_offset = round_up2(offset, 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 = round_up2(size, 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);