2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "type_hash.h"
30 #include "adt/error.h"
32 #include "lang_features.h"
34 #include "diagnostic.h"
36 #include "separator_t.h"
38 /** The default calling convention. */
39 cc_kind_t default_calling_convention = CC_CDECL;
41 static struct obstack type_obst;
42 static bool print_implicit_array_size = false;
44 static void intern_print_type_pre(const type_t *type);
45 static void intern_print_type_post(const type_t *type);
48 * Returns the size of a type node.
50 * @param kind the type kind
52 static size_t get_type_struct_size(type_kind_t kind)
54 static const size_t sizes[] = {
55 [TYPE_ATOMIC] = sizeof(atomic_type_t),
56 [TYPE_IMAGINARY] = sizeof(atomic_type_t),
57 [TYPE_COMPLEX] = sizeof(atomic_type_t),
58 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
59 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
60 [TYPE_ENUM] = sizeof(enum_type_t),
61 [TYPE_FUNCTION] = sizeof(function_type_t),
62 [TYPE_POINTER] = sizeof(pointer_type_t),
63 [TYPE_REFERENCE] = sizeof(reference_type_t),
64 [TYPE_ARRAY] = sizeof(array_type_t),
65 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
66 [TYPE_TYPEOF] = sizeof(typeof_type_t),
68 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
69 assert(kind <= TYPE_TYPEOF);
70 assert(sizes[kind] != 0);
74 type_t *allocate_type_zero(type_kind_t kind)
76 size_t const size = get_type_struct_size(kind);
77 type_t *const res = obstack_alloc(&type_obst, size);
79 res->base.kind = kind;
85 * Properties of atomic types.
87 atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
88 [ATOMIC_TYPE_VOID] = {
91 .flags = ATOMIC_TYPE_FLAG_NONE,
94 [ATOMIC_TYPE_BOOL] = {
97 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
100 [ATOMIC_TYPE_CHAR] = {
103 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
106 [ATOMIC_TYPE_SCHAR] = {
109 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
110 | ATOMIC_TYPE_FLAG_SIGNED,
113 [ATOMIC_TYPE_UCHAR] = {
116 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
119 [ATOMIC_TYPE_SHORT] = {
122 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
123 | ATOMIC_TYPE_FLAG_SIGNED,
126 [ATOMIC_TYPE_USHORT] = {
129 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
132 [ATOMIC_TYPE_INT] = {
133 .size = (unsigned) -1,
134 .alignment = (unsigned) -1,
135 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
136 | ATOMIC_TYPE_FLAG_SIGNED,
139 [ATOMIC_TYPE_UINT] = {
140 .size = (unsigned) -1,
141 .alignment = (unsigned) -1,
142 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
145 [ATOMIC_TYPE_LONG] = {
146 .size = (unsigned) -1,
147 .alignment = (unsigned) -1,
148 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
149 | ATOMIC_TYPE_FLAG_SIGNED,
152 [ATOMIC_TYPE_ULONG] = {
153 .size = (unsigned) -1,
154 .alignment = (unsigned) -1,
155 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
158 [ATOMIC_TYPE_LONGLONG] = {
161 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
162 | ATOMIC_TYPE_FLAG_SIGNED,
165 [ATOMIC_TYPE_ULONGLONG] = {
168 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
171 [ATOMIC_TYPE_FLOAT] = {
174 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
175 | ATOMIC_TYPE_FLAG_SIGNED,
178 [ATOMIC_TYPE_DOUBLE] = {
181 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
182 | ATOMIC_TYPE_FLAG_SIGNED,
185 [ATOMIC_TYPE_WCHAR_T] = {
186 .size = (unsigned)-1,
187 .alignment = (unsigned)-1,
188 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
189 .rank = (unsigned)-1,
192 atomic_type_properties_t pointer_properties = {
195 .flags = ATOMIC_TYPE_FLAG_NONE,
198 static inline bool is_po2(unsigned x)
200 return (x & (x-1)) == 0;
203 void init_types(unsigned machine_size)
205 obstack_init(&type_obst);
207 atomic_type_properties_t *props = atomic_type_properties;
209 /* atempt to set some sane defaults based on machine size */
211 unsigned int_size = machine_size < 32 ? 2 : 4;
212 unsigned long_size = machine_size < 64 ? 4 : 8;
214 props[ATOMIC_TYPE_INT].size = int_size;
215 props[ATOMIC_TYPE_INT].alignment = int_size;
216 props[ATOMIC_TYPE_UINT].size = int_size;
217 props[ATOMIC_TYPE_UINT].alignment = int_size;
218 props[ATOMIC_TYPE_LONG].size = long_size;
219 props[ATOMIC_TYPE_LONG].alignment = long_size;
220 props[ATOMIC_TYPE_ULONG].size = long_size;
221 props[ATOMIC_TYPE_ULONG].alignment = long_size;
223 pointer_properties.size = long_size;
224 pointer_properties.alignment = long_size;
225 pointer_properties.struct_alignment = long_size;
227 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
228 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
230 /* set struct alignments to the same value as alignment */
231 for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
232 props[i].struct_alignment = props[i].alignment;
236 void exit_types(void)
238 obstack_free(&type_obst, NULL);
241 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
243 size_t sep = q & QUAL_SEP_START ? 0 : 1;
244 if (qualifiers & TYPE_QUALIFIER_CONST) {
245 print_string(&" const"[sep]);
248 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
249 print_string(&" volatile"[sep]);
252 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
253 print_string(&" restrict"[sep]);
256 if (sep == 0 && q & QUAL_SEP_END)
260 const char *get_atomic_kind_name(atomic_type_kind_t kind)
263 case ATOMIC_TYPE_VOID: return "void";
264 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
265 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
266 case ATOMIC_TYPE_CHAR: return "char";
267 case ATOMIC_TYPE_SCHAR: return "signed char";
268 case ATOMIC_TYPE_UCHAR: return "unsigned char";
269 case ATOMIC_TYPE_INT: return "int";
270 case ATOMIC_TYPE_UINT: return "unsigned int";
271 case ATOMIC_TYPE_SHORT: return "short";
272 case ATOMIC_TYPE_USHORT: return "unsigned short";
273 case ATOMIC_TYPE_LONG: return "long";
274 case ATOMIC_TYPE_ULONG: return "unsigned long";
275 case ATOMIC_TYPE_LONGLONG: return "long long";
276 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
277 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
278 case ATOMIC_TYPE_FLOAT: return "float";
279 case ATOMIC_TYPE_DOUBLE: return "double";
281 return "INVALIDATOMIC";
285 * Prints the name of an atomic type kinds.
287 * @param kind The type kind.
289 static void print_atomic_kinds(atomic_type_kind_t kind)
291 const char *s = get_atomic_kind_name(kind);
296 * Prints the name of an atomic type.
298 * @param type The type.
300 static void print_atomic_type(const atomic_type_t *type)
302 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
303 print_atomic_kinds(type->akind);
307 * Prints the name of a complex type.
309 * @param type The type.
311 static void print_complex_type(const atomic_type_t *type)
313 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
314 print_string("_Complex ");
315 print_atomic_kinds(type->akind);
319 * Prints the name of an imaginary type.
321 * @param type The type.
323 static void print_imaginary_type(const atomic_type_t *type)
325 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
326 print_string("_Imaginary ");
327 print_atomic_kinds(type->akind);
331 * Print the first part (the prefix) of a type.
333 * @param type The type to print.
335 static void print_function_type_pre(const function_type_t *type)
337 switch (type->linkage) {
340 print_string("extern \"C\" ");
344 if (!(c_mode & _CXX))
345 print_string("extern \"C++\" ");
349 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
351 intern_print_type_pre(type->return_type);
353 cc_kind_t cc = type->calling_convention;
356 case CC_CDECL: print_string(" __cdecl"); break;
357 case CC_STDCALL: print_string(" __stdcall"); break;
358 case CC_FASTCALL: print_string(" __fastcall"); break;
359 case CC_THISCALL: print_string(" __thiscall"); break;
361 if (default_calling_convention != CC_CDECL) {
362 /* show the default calling convention if its not cdecl */
363 cc = default_calling_convention;
371 * Print the second part (the postfix) of a type.
373 * @param type The type to print.
375 static void print_function_type_post(const function_type_t *type,
376 const scope_t *parameters)
379 separator_t sep = { "", ", " };
380 if (parameters == NULL) {
381 function_parameter_t *parameter = type->parameters;
382 for ( ; parameter != NULL; parameter = parameter->next) {
383 print_string(sep_next(&sep));
384 print_type(parameter->type);
387 entity_t *parameter = parameters->entities;
388 for (; parameter != NULL; parameter = parameter->base.next) {
389 if (parameter->kind != ENTITY_PARAMETER)
392 print_string(sep_next(&sep));
393 const type_t *const param_type = parameter->declaration.type;
394 if (param_type == NULL) {
395 print_string(parameter->base.symbol->string);
397 print_type_ext(param_type, parameter->base.symbol, NULL);
401 if (type->variadic) {
402 print_string(sep_next(&sep));
405 if (sep_at_first(&sep) && !type->unspecified_parameters) {
406 print_string("void");
410 intern_print_type_post(type->return_type);
414 * Prints the prefix part of a pointer type.
416 * @param type The pointer type.
418 static void print_pointer_type_pre(const pointer_type_t *type)
420 type_t const *const points_to = type->points_to;
421 intern_print_type_pre(points_to);
422 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
424 variable_t *const variable = type->base_variable;
425 if (variable != NULL) {
426 print_string(" __based(");
427 print_string(variable->base.base.symbol->string);
431 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
435 * Prints the postfix part of a pointer type.
437 * @param type The pointer type.
439 static void print_pointer_type_post(const pointer_type_t *type)
441 type_t const *const points_to = type->points_to;
442 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
444 intern_print_type_post(points_to);
448 * Prints the prefix part of a reference type.
450 * @param type The reference type.
452 static void print_reference_type_pre(const reference_type_t *type)
454 type_t const *const refers_to = type->refers_to;
455 intern_print_type_pre(refers_to);
456 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
462 * Prints the postfix part of a reference type.
464 * @param type The reference type.
466 static void print_reference_type_post(const reference_type_t *type)
468 type_t const *const refers_to = type->refers_to;
469 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
471 intern_print_type_post(refers_to);
475 * Prints the prefix part of an array type.
477 * @param type The array type.
479 static void print_array_type_pre(const array_type_t *type)
481 intern_print_type_pre(type->element_type);
485 * Prints the postfix part of an array type.
487 * @param type The array type.
489 static void print_array_type_post(const array_type_t *type)
492 if (type->is_static) {
493 print_string("static ");
495 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
496 if (type->size_expression != NULL
497 && (print_implicit_array_size || !type->has_implicit_size)) {
498 print_expression(type->size_expression);
501 intern_print_type_post(type->element_type);
504 void print_enum_definition(const enum_t *enume)
510 entity_t *entry = enume->base.next;
511 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
512 entry = entry->base.next) {
515 print_string(entry->base.symbol->string);
516 if (entry->enum_value.value != NULL) {
518 print_expression(entry->enum_value.value);
529 * Prints an enum type.
531 * @param type The enum type.
533 static void print_type_enum(const enum_type_t *type)
535 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
536 print_string("enum ");
538 enum_t *enume = type->enume;
539 symbol_t *symbol = enume->base.symbol;
540 if (symbol != NULL) {
541 print_string(symbol->string);
543 print_enum_definition(enume);
547 void print_compound_definition(const compound_t *compound)
552 entity_t *entity = compound->members.entities;
553 for ( ; entity != NULL; entity = entity->base.next) {
554 if (entity->kind != ENTITY_COMPOUND_MEMBER)
558 print_entity(entity);
565 if (compound->modifiers & DM_TRANSPARENT_UNION) {
566 print_string("__attribute__((__transparent_union__))");
571 * Prints a compound type.
573 * @param kind The name of the compound kind.
574 * @param type The compound type.
576 static void print_compound_type(char const *const kind, compound_type_t const *const type)
578 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
581 compound_t *compound = type->compound;
582 symbol_t *symbol = compound->base.symbol;
583 if (symbol != NULL) {
584 print_string(symbol->string);
586 print_compound_definition(compound);
591 * Prints the prefix part of a typedef type.
593 * @param type The typedef type.
595 static void print_typedef_type_pre(const typedef_type_t *const type)
597 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
598 print_string(type->typedefe->base.symbol->string);
602 * Prints the prefix part of a typeof type.
604 * @param type The typeof type.
606 static void print_typeof_type_pre(const typeof_type_t *const type)
608 print_string("typeof(");
609 if (type->expression != NULL) {
610 print_expression(type->expression);
612 print_type(type->typeof_type);
618 * Prints the prefix part of a type.
620 * @param type The type.
622 static void intern_print_type_pre(const type_t *const type)
624 switch (type->kind) {
625 case TYPE_ARRAY: print_array_type_pre( &type->array); return;
626 case TYPE_ATOMIC: print_atomic_type( &type->atomic); return;
627 case TYPE_COMPLEX: print_complex_type( &type->atomic); return;
628 case TYPE_COMPOUND_STRUCT: print_compound_type("struct ", &type->compound); return;
629 case TYPE_COMPOUND_UNION: print_compound_type("union ", &type->compound); return;
630 case TYPE_ENUM: print_type_enum( &type->enumt); return;
631 case TYPE_ERROR: print_string("<error>"); return;
632 case TYPE_FUNCTION: print_function_type_pre( &type->function); return;
633 case TYPE_IMAGINARY: print_imaginary_type( &type->atomic); return;
634 case TYPE_POINTER: print_pointer_type_pre( &type->pointer); return;
635 case TYPE_REFERENCE: print_reference_type_pre( &type->reference); return;
636 case TYPE_TYPEDEF: print_typedef_type_pre( &type->typedeft); return;
637 case TYPE_TYPEOF: print_typeof_type_pre( &type->typeoft); return;
639 print_string("unknown");
643 * Prints the postfix part of a type.
645 * @param type The type.
647 static void intern_print_type_post(const type_t *const type)
649 switch (type->kind) {
651 print_function_type_post(&type->function, NULL);
654 print_pointer_type_post(&type->pointer);
657 print_reference_type_post(&type->reference);
660 print_array_type_post(&type->array);
667 case TYPE_COMPOUND_STRUCT:
668 case TYPE_COMPOUND_UNION:
675 void print_type(const type_t *const type)
677 print_type_ext(type, NULL, NULL);
680 void print_type_ext(const type_t *const type, const symbol_t *symbol,
681 const scope_t *parameters)
683 intern_print_type_pre(type);
684 if (symbol != NULL) {
686 print_string(symbol->string);
688 if (type->kind == TYPE_FUNCTION) {
689 print_function_type_post(&type->function, parameters);
691 intern_print_type_post(type);
695 type_t *duplicate_type(const type_t *type)
697 size_t size = get_type_struct_size(type->kind);
699 type_t *const copy = obstack_copy(&type_obst, type, size);
700 copy->base.firm_type = NULL;
705 type_t *get_unqualified_type(type_t *type)
707 assert(!is_typeref(type));
709 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
712 type_t *unqualified_type = duplicate_type(type);
713 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
715 return identify_new_type(unqualified_type);
718 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
720 type_t *type = skip_typeref(orig_type);
723 if (is_type_array(type)) {
724 /* For array types the element type has to be adjusted */
725 type_t *element_type = type->array.element_type;
726 type_t *qual_element_type = get_qualified_type(element_type, qual);
728 if (qual_element_type == element_type)
731 copy = duplicate_type(type);
732 copy->array.element_type = qual_element_type;
733 } else if (is_type_valid(type)) {
734 if ((type->base.qualifiers & qual) == (int)qual)
737 copy = duplicate_type(type);
738 copy->base.qualifiers |= qual;
743 return identify_new_type(copy);
746 static bool test_atomic_type_flag(atomic_type_kind_t kind,
747 atomic_type_flag_t flag)
749 assert(kind <= ATOMIC_TYPE_LAST);
750 return (atomic_type_properties[kind].flags & flag) != 0;
753 bool is_type_integer(const type_t *type)
755 assert(!is_typeref(type));
757 if (type->kind == TYPE_ENUM)
759 if (type->kind != TYPE_ATOMIC)
762 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
765 bool is_type_enum(const type_t *type)
767 assert(!is_typeref(type));
768 return type->kind == TYPE_ENUM;
771 bool is_type_float(const type_t *type)
773 assert(!is_typeref(type));
775 if (type->kind != TYPE_ATOMIC)
778 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
781 bool is_type_signed(const type_t *type)
783 assert(!is_typeref(type));
785 /* enum types are int for now */
786 if (type->kind == TYPE_ENUM)
788 if (type->kind != TYPE_ATOMIC)
791 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
794 bool is_type_arithmetic(const type_t *type)
796 assert(!is_typeref(type));
798 switch (type->kind) {
804 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
810 bool is_type_real(const type_t *type)
813 return is_type_integer(type) || is_type_float(type);
816 bool is_type_scalar(const type_t *type)
818 assert(!is_typeref(type));
820 switch (type->kind) {
826 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
832 bool is_type_incomplete(const type_t *type)
834 assert(!is_typeref(type));
836 switch (type->kind) {
837 case TYPE_COMPOUND_STRUCT:
838 case TYPE_COMPOUND_UNION: {
839 const compound_type_t *compound_type = &type->compound;
840 return !compound_type->compound->complete;
846 return type->array.size_expression == NULL
847 && !type->array.size_constant;
852 return type->atomic.akind == ATOMIC_TYPE_VOID;
862 panic("typedef not skipped");
865 panic("invalid type");
868 bool is_type_object(const type_t *type)
870 return !is_type_function(type) && !is_type_incomplete(type);
874 * Check if two function types are compatible.
876 static bool function_types_compatible(const function_type_t *func1,
877 const function_type_t *func2)
879 const type_t* const ret1 = skip_typeref(func1->return_type);
880 const type_t* const ret2 = skip_typeref(func2->return_type);
881 if (!types_compatible(ret1, ret2))
884 if (func1->linkage != func2->linkage)
887 cc_kind_t cc1 = func1->calling_convention;
888 if (cc1 == CC_DEFAULT)
889 cc1 = default_calling_convention;
890 cc_kind_t cc2 = func2->calling_convention;
891 if (cc2 == CC_DEFAULT)
892 cc2 = default_calling_convention;
897 if (func1->variadic != func2->variadic)
900 /* can parameters be compared? */
901 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
902 || (func2->unspecified_parameters && !func2->kr_style_parameters))
905 /* TODO: handling of unspecified parameters not correct yet */
907 /* all argument types must be compatible */
908 function_parameter_t *parameter1 = func1->parameters;
909 function_parameter_t *parameter2 = func2->parameters;
910 for ( ; parameter1 != NULL && parameter2 != NULL;
911 parameter1 = parameter1->next, parameter2 = parameter2->next) {
912 type_t *parameter1_type = skip_typeref(parameter1->type);
913 type_t *parameter2_type = skip_typeref(parameter2->type);
915 parameter1_type = get_unqualified_type(parameter1_type);
916 parameter2_type = get_unqualified_type(parameter2_type);
918 if (!types_compatible(parameter1_type, parameter2_type))
921 /* same number of arguments? */
922 if (parameter1 != NULL || parameter2 != NULL)
929 * Check if two array types are compatible.
931 static bool array_types_compatible(const array_type_t *array1,
932 const array_type_t *array2)
934 type_t *element_type1 = skip_typeref(array1->element_type);
935 type_t *element_type2 = skip_typeref(array2->element_type);
936 if (!types_compatible(element_type1, element_type2))
939 if (!array1->size_constant || !array2->size_constant)
942 return array1->size == array2->size;
945 bool types_compatible(const type_t *type1, const type_t *type2)
947 assert(!is_typeref(type1));
948 assert(!is_typeref(type2));
950 /* shortcut: the same type is always compatible */
954 if (type1->base.qualifiers == type2->base.qualifiers &&
955 type1->kind == type2->kind) {
956 switch (type1->kind) {
958 return function_types_compatible(&type1->function, &type2->function);
962 return type1->atomic.akind == type2->atomic.akind;
964 return array_types_compatible(&type1->array, &type2->array);
967 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
968 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
969 return types_compatible(to1, to2);
972 case TYPE_REFERENCE: {
973 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
974 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
975 return types_compatible(to1, to2);
978 case TYPE_COMPOUND_STRUCT:
979 case TYPE_COMPOUND_UNION:
983 /* TODO: not implemented */
987 /* Hmm, the error type should be compatible to all other types */
991 panic("typeref not skipped");
995 return !is_type_valid(type1) || !is_type_valid(type2);
999 * Skip all typerefs and return the underlying type.
1001 type_t *skip_typeref(type_t *type)
1003 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1006 switch (type->kind) {
1009 case TYPE_TYPEDEF: {
1010 qualifiers |= type->base.qualifiers;
1012 const typedef_type_t *typedef_type = &type->typedeft;
1013 if (typedef_type->resolved_type != NULL) {
1014 type = typedef_type->resolved_type;
1017 type = typedef_type->typedefe->type;
1021 qualifiers |= type->base.qualifiers;
1022 type = type->typeoft.typeof_type;
1030 if (qualifiers != TYPE_QUALIFIER_NONE) {
1031 type_t *const copy = duplicate_type(type);
1033 /* for const with typedefed array type the element type has to be
1035 if (is_type_array(copy)) {
1036 type_t *element_type = copy->array.element_type;
1037 element_type = duplicate_type(element_type);
1038 element_type->base.qualifiers |= qualifiers;
1039 copy->array.element_type = element_type;
1041 copy->base.qualifiers |= qualifiers;
1044 type = identify_new_type(copy);
1050 unsigned get_type_size(type_t *type)
1052 switch (type->kind) {
1056 case TYPE_IMAGINARY:
1058 return get_atomic_type_size(type->atomic.akind);
1060 return get_atomic_type_size(type->atomic.akind) * 2;
1061 case TYPE_COMPOUND_UNION:
1062 layout_union_type(&type->compound);
1063 return type->compound.compound->size;
1064 case TYPE_COMPOUND_STRUCT:
1065 layout_struct_type(&type->compound);
1066 return type->compound.compound->size;
1068 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1069 case TYPE_REFERENCE:
1071 return pointer_properties.size;
1073 /* TODO: correct if element_type is aligned? */
1074 il_size_t element_size = get_type_size(type->array.element_type);
1075 return type->array.size * element_size;
1078 return get_type_size(type->typedeft.typedefe->type);
1080 return get_type_size(type->typeoft.typeof_type);
1082 panic("invalid type");
1085 unsigned get_type_alignment(type_t *type)
1087 switch (type->kind) {
1091 case TYPE_IMAGINARY:
1094 return get_atomic_type_alignment(type->atomic.akind);
1095 case TYPE_COMPOUND_UNION:
1096 layout_union_type(&type->compound);
1097 return type->compound.compound->alignment;
1098 case TYPE_COMPOUND_STRUCT:
1099 layout_struct_type(&type->compound);
1100 return type->compound.compound->alignment;
1102 /* gcc says 1 here... */
1104 case TYPE_REFERENCE:
1106 return pointer_properties.alignment;
1108 return get_type_alignment(type->array.element_type);
1109 case TYPE_TYPEDEF: {
1110 il_alignment_t alignment
1111 = get_type_alignment(type->typedeft.typedefe->type);
1112 if (type->typedeft.typedefe->alignment > alignment)
1113 alignment = type->typedeft.typedefe->alignment;
1118 return get_type_alignment(type->typeoft.typeof_type);
1120 panic("invalid type");
1124 * get alignment of a type when used inside a compound.
1125 * Some ABIs are broken and alignment inside a compound is different from
1126 * recommended alignment of a type
1128 static unsigned get_type_alignment_compound(type_t *const type)
1130 assert(!is_typeref(type));
1131 if (type->kind == TYPE_ATOMIC)
1132 return atomic_type_properties[type->atomic.akind].struct_alignment;
1133 return get_type_alignment(type);
1136 decl_modifiers_t get_type_modifiers(const type_t *type)
1138 switch (type->kind) {
1141 case TYPE_COMPOUND_STRUCT:
1142 case TYPE_COMPOUND_UNION:
1143 return type->compound.compound->modifiers;
1145 return type->function.modifiers;
1149 case TYPE_IMAGINARY:
1150 case TYPE_REFERENCE:
1154 case TYPE_TYPEDEF: {
1155 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1156 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1160 return get_type_modifiers(type->typeoft.typeof_type);
1162 panic("invalid type");
1165 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1167 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1170 switch (type->base.kind) {
1172 return TYPE_QUALIFIER_NONE;
1174 qualifiers |= type->base.qualifiers;
1175 const typedef_type_t *typedef_type = &type->typedeft;
1176 if (typedef_type->resolved_type != NULL)
1177 type = typedef_type->resolved_type;
1179 type = typedef_type->typedefe->type;
1182 type = type->typeoft.typeof_type;
1185 if (skip_array_type) {
1186 type = type->array.element_type;
1195 return type->base.qualifiers | qualifiers;
1198 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1200 assert(kind <= ATOMIC_TYPE_LAST);
1201 return atomic_type_properties[kind].size;
1204 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1206 assert(kind <= ATOMIC_TYPE_LAST);
1207 return atomic_type_properties[kind].alignment;
1210 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1212 assert(kind <= ATOMIC_TYPE_LAST);
1213 return atomic_type_properties[kind].flags;
1217 * Find the atomic type kind representing a given size (signed).
1219 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1221 static atomic_type_kind_t kinds[32];
1224 atomic_type_kind_t kind = kinds[size];
1225 if (kind == (atomic_type_kind_t)0) {
1226 static const atomic_type_kind_t possible_kinds[] = {
1231 ATOMIC_TYPE_LONGLONG
1233 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1234 if (get_atomic_type_size(possible_kinds[i]) == size) {
1235 kind = possible_kinds[i];
1245 * Find the atomic type kind representing a given size (signed).
1247 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1249 static atomic_type_kind_t kinds[32];
1252 atomic_type_kind_t kind = kinds[size];
1253 if (kind == (atomic_type_kind_t)0) {
1254 static const atomic_type_kind_t possible_kinds[] = {
1259 ATOMIC_TYPE_ULONGLONG
1261 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1262 if (get_atomic_type_size(possible_kinds[i]) == size) {
1263 kind = possible_kinds[i];
1273 * Hash the given type and return the "singleton" version
1276 type_t *identify_new_type(type_t *type)
1278 type_t *result = typehash_insert(type);
1279 if (result != type) {
1280 obstack_free(&type_obst, type);
1286 * Creates a new atomic type.
1288 * @param akind The kind of the atomic type.
1289 * @param qualifiers Type qualifiers for the new type.
1291 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1293 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1294 type->base.qualifiers = qualifiers;
1295 type->atomic.akind = akind;
1297 return identify_new_type(type);
1301 * Creates a new complex type.
1303 * @param akind The kind of the atomic type.
1304 * @param qualifiers Type qualifiers for the new type.
1306 type_t *make_complex_type(atomic_type_kind_t akind,
1307 type_qualifiers_t qualifiers)
1309 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1310 type->base.qualifiers = qualifiers;
1311 type->atomic.akind = akind;
1313 return identify_new_type(type);
1317 * Creates a new imaginary type.
1319 * @param akind The kind of the atomic type.
1320 * @param qualifiers Type qualifiers for the new type.
1322 type_t *make_imaginary_type(atomic_type_kind_t akind,
1323 type_qualifiers_t qualifiers)
1325 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1326 type->base.qualifiers = qualifiers;
1327 type->atomic.akind = akind;
1329 return identify_new_type(type);
1333 * Creates a new pointer type.
1335 * @param points_to The points-to type for the new type.
1336 * @param qualifiers Type qualifiers for the new type.
1338 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1340 type_t *const type = allocate_type_zero(TYPE_POINTER);
1341 type->base.qualifiers = qualifiers;
1342 type->pointer.points_to = points_to;
1343 type->pointer.base_variable = NULL;
1345 return identify_new_type(type);
1349 * Creates a new reference type.
1351 * @param refers_to The referred-to type for the new type.
1353 type_t *make_reference_type(type_t *refers_to)
1355 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1356 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1357 type->reference.refers_to = refers_to;
1359 return identify_new_type(type);
1363 * Creates a new based pointer type.
1365 * @param points_to The points-to type for the new type.
1366 * @param qualifiers Type qualifiers for the new type.
1367 * @param variable The based variable
1369 type_t *make_based_pointer_type(type_t *points_to,
1370 type_qualifiers_t qualifiers, variable_t *variable)
1372 type_t *const type = allocate_type_zero(TYPE_POINTER);
1373 type->base.qualifiers = qualifiers;
1374 type->pointer.points_to = points_to;
1375 type->pointer.base_variable = variable;
1377 return identify_new_type(type);
1381 type_t *make_array_type(type_t *element_type, size_t size,
1382 type_qualifiers_t qualifiers)
1384 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1385 type->base.qualifiers = qualifiers;
1386 type->array.element_type = element_type;
1387 type->array.size = size;
1388 type->array.size_constant = true;
1390 return identify_new_type(type);
1393 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1394 il_alignment_t *struct_alignment,
1395 bool packed, entity_t *first)
1397 il_size_t offset = *struct_offset;
1398 il_alignment_t alignment = *struct_alignment;
1399 size_t bit_offset = 0;
1402 for (member = first; member != NULL; member = member->base.next) {
1403 if (member->kind != ENTITY_COMPOUND_MEMBER)
1405 if (!member->compound_member.bitfield)
1408 type_t *const base_type = skip_typeref(member->declaration.type);
1409 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1410 il_alignment_t alignment_mask = base_alignment-1;
1411 if (base_alignment > alignment)
1412 alignment = base_alignment;
1414 size_t bit_size = member->compound_member.bit_size;
1416 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1417 offset &= ~alignment_mask;
1418 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1420 if (bit_offset + bit_size > base_size || bit_size == 0) {
1421 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1422 offset = (offset + base_alignment-1) & ~alignment_mask;
1427 if (byte_order_big_endian) {
1428 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1429 member->compound_member.offset = offset & ~alignment_mask;
1430 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1432 member->compound_member.offset = offset;
1433 member->compound_member.bit_offset = bit_offset;
1436 bit_offset += bit_size;
1437 offset += bit_offset / BITS_PER_BYTE;
1438 bit_offset %= BITS_PER_BYTE;
1444 *struct_offset = offset;
1445 *struct_alignment = alignment;
1449 void layout_struct_type(compound_type_t *type)
1451 assert(type->compound != NULL);
1453 compound_t *compound = type->compound;
1454 if (!compound->complete)
1456 if (type->compound->layouted)
1458 compound->layouted = true;
1460 il_size_t offset = 0;
1461 il_alignment_t alignment = compound->alignment;
1462 bool need_pad = false;
1464 entity_t *entry = compound->members.entities;
1465 while (entry != NULL) {
1466 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1469 type_t *const m_type = skip_typeref(entry->declaration.type);
1470 if (!is_type_valid(m_type))
1473 if (entry->compound_member.bitfield) {
1474 entry = pack_bitfield_members(&offset, &alignment,
1475 compound->packed, entry);
1479 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1480 if (m_alignment > alignment)
1481 alignment = m_alignment;
1483 if (!compound->packed) {
1484 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1486 if (new_offset > offset) {
1488 offset = new_offset;
1492 entry->compound_member.offset = offset;
1493 offset += get_type_size(m_type);
1496 entry = entry->base.next;
1499 if (!compound->packed) {
1500 il_size_t new_offset = (offset + alignment-1) & -alignment;
1501 if (new_offset > offset) {
1503 offset = new_offset;
1507 position_t const *const pos = &compound->base.pos;
1509 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1510 } else if (compound->packed) {
1511 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1514 compound->size = offset;
1515 compound->alignment = alignment;
1518 void layout_union_type(compound_type_t *type)
1520 assert(type->compound != NULL);
1522 compound_t *compound = type->compound;
1523 if (! compound->complete)
1525 if (compound->layouted)
1527 compound->layouted = true;
1530 il_alignment_t alignment = compound->alignment;
1532 entity_t *entry = compound->members.entities;
1533 for (; entry != NULL; entry = entry->base.next) {
1534 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1537 type_t *m_type = skip_typeref(entry->declaration.type);
1538 if (! is_type_valid(skip_typeref(m_type)))
1541 entry->compound_member.offset = 0;
1542 il_size_t m_size = get_type_size(m_type);
1545 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1546 if (m_alignment > alignment)
1547 alignment = m_alignment;
1549 size = (size + alignment - 1) & -alignment;
1551 compound->size = size;
1552 compound->alignment = alignment;
1555 function_parameter_t *allocate_parameter(type_t *const type)
1557 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1558 memset(param, 0, sizeof(*param));
1563 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1564 type_t *argument_type2, decl_modifiers_t modifiers)
1566 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1567 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1568 parameter1->next = parameter2;
1570 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1571 type->function.return_type = return_type;
1572 type->function.parameters = parameter1;
1573 type->function.modifiers |= modifiers;
1574 type->function.linkage = LINKAGE_C;
1576 return identify_new_type(type);
1579 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1580 decl_modifiers_t modifiers)
1582 function_parameter_t *const parameter = allocate_parameter(argument_type);
1584 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1585 type->function.return_type = return_type;
1586 type->function.parameters = parameter;
1587 type->function.modifiers |= modifiers;
1588 type->function.linkage = LINKAGE_C;
1590 return identify_new_type(type);
1593 type_t *make_function_1_type_variadic(type_t *return_type,
1594 type_t *argument_type,
1595 decl_modifiers_t modifiers)
1597 function_parameter_t *const parameter = allocate_parameter(argument_type);
1599 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1600 type->function.return_type = return_type;
1601 type->function.parameters = parameter;
1602 type->function.variadic = true;
1603 type->function.modifiers |= modifiers;
1604 type->function.linkage = LINKAGE_C;
1606 return identify_new_type(type);
1609 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1611 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1612 type->function.return_type = return_type;
1613 type->function.parameters = NULL;
1614 type->function.modifiers |= modifiers;
1615 type->function.linkage = LINKAGE_C;
1617 return identify_new_type(type);
1620 type_t *make_function_type(type_t *return_type, int n_types,
1621 type_t *const *argument_types,
1622 decl_modifiers_t modifiers)
1624 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1625 type->function.return_type = return_type;
1626 type->function.modifiers |= modifiers;
1627 type->function.linkage = LINKAGE_C;
1629 function_parameter_t **anchor = &type->function.parameters;
1630 for (int i = 0; i < n_types; ++i) {
1631 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1632 *anchor = parameter;
1633 anchor = ¶meter->next;
1636 return identify_new_type(type);
1640 * Debug helper. Prints the given type to stdout.
1642 static __attribute__((unused))
1643 void dbg_type(const type_t *type)
1645 print_to_file(stderr);