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));
756 if (!is_type_arithmetic(type))
758 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
761 bool is_type_enum(const type_t *type)
763 assert(!is_typeref(type));
764 return type->kind == TYPE_ENUM;
767 bool is_type_float(const type_t *type)
769 assert(!is_typeref(type));
771 if (type->kind != TYPE_ATOMIC)
774 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
777 bool is_type_complex(const type_t *type)
779 assert(!is_typeref(type));
780 return type->kind == TYPE_COMPLEX;
783 bool is_type_signed(const type_t *type)
785 assert(!is_typeref(type));
786 if (!is_type_arithmetic(type))
788 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
791 bool is_type_arithmetic(const type_t *type)
793 assert(!is_typeref(type));
795 switch (type->kind) {
801 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
807 bool is_type_real(const type_t *type)
810 return is_type_integer(type) || is_type_float(type);
813 bool is_type_scalar(const type_t *type)
815 assert(!is_typeref(type));
817 switch (type->kind) {
824 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
830 bool is_type_incomplete(const type_t *type)
832 assert(!is_typeref(type));
834 switch (type->kind) {
835 case TYPE_COMPOUND_STRUCT:
836 case TYPE_COMPOUND_UNION: {
837 const compound_type_t *compound_type = &type->compound;
838 return !compound_type->compound->complete;
844 return type->array.size_expression == NULL
845 && !type->array.size_constant;
850 return type->atomic.akind == ATOMIC_TYPE_VOID;
860 panic("typedef not skipped");
863 panic("invalid type");
866 bool is_type_object(const type_t *type)
868 return !is_type_function(type) && !is_type_incomplete(type);
872 * Check if two function types are compatible.
874 static bool function_types_compatible(const function_type_t *func1,
875 const function_type_t *func2)
877 const type_t* const ret1 = skip_typeref(func1->return_type);
878 const type_t* const ret2 = skip_typeref(func2->return_type);
879 if (!types_compatible(ret1, ret2))
882 if (func1->linkage != func2->linkage)
885 cc_kind_t cc1 = func1->calling_convention;
886 if (cc1 == CC_DEFAULT)
887 cc1 = default_calling_convention;
888 cc_kind_t cc2 = func2->calling_convention;
889 if (cc2 == CC_DEFAULT)
890 cc2 = default_calling_convention;
895 if (func1->variadic != func2->variadic)
898 /* can parameters be compared? */
899 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
900 || (func2->unspecified_parameters && !func2->kr_style_parameters))
903 /* TODO: handling of unspecified parameters not correct yet */
905 /* all argument types must be compatible */
906 function_parameter_t *parameter1 = func1->parameters;
907 function_parameter_t *parameter2 = func2->parameters;
908 for ( ; parameter1 != NULL && parameter2 != NULL;
909 parameter1 = parameter1->next, parameter2 = parameter2->next) {
910 type_t *parameter1_type = skip_typeref(parameter1->type);
911 type_t *parameter2_type = skip_typeref(parameter2->type);
913 parameter1_type = get_unqualified_type(parameter1_type);
914 parameter2_type = get_unqualified_type(parameter2_type);
916 if (!types_compatible(parameter1_type, parameter2_type))
919 /* same number of arguments? */
920 if (parameter1 != NULL || parameter2 != NULL)
927 * Check if two array types are compatible.
929 static bool array_types_compatible(const array_type_t *array1,
930 const array_type_t *array2)
932 type_t *element_type1 = skip_typeref(array1->element_type);
933 type_t *element_type2 = skip_typeref(array2->element_type);
934 if (!types_compatible(element_type1, element_type2))
937 if (!array1->size_constant || !array2->size_constant)
940 return array1->size == array2->size;
943 bool types_compatible(const type_t *type1, const type_t *type2)
945 assert(!is_typeref(type1));
946 assert(!is_typeref(type2));
948 /* shortcut: the same type is always compatible */
952 if (type1->base.qualifiers == type2->base.qualifiers &&
953 type1->kind == type2->kind) {
954 switch (type1->kind) {
956 return function_types_compatible(&type1->function, &type2->function);
960 return type1->atomic.akind == type2->atomic.akind;
962 return array_types_compatible(&type1->array, &type2->array);
965 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
966 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
967 return types_compatible(to1, to2);
970 case TYPE_REFERENCE: {
971 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
972 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
973 return types_compatible(to1, to2);
976 case TYPE_COMPOUND_STRUCT:
977 case TYPE_COMPOUND_UNION:
981 /* TODO: not implemented */
985 /* Hmm, the error type should be compatible to all other types */
989 panic("typeref not skipped");
993 return !is_type_valid(type1) || !is_type_valid(type2);
997 * Skip all typerefs and return the underlying type.
999 type_t *skip_typeref(type_t *type)
1001 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1004 switch (type->kind) {
1007 case TYPE_TYPEDEF: {
1008 qualifiers |= type->base.qualifiers;
1010 const typedef_type_t *typedef_type = &type->typedeft;
1011 if (typedef_type->resolved_type != NULL) {
1012 type = typedef_type->resolved_type;
1015 type = typedef_type->typedefe->type;
1019 qualifiers |= type->base.qualifiers;
1020 type = type->typeoft.typeof_type;
1028 if (qualifiers != TYPE_QUALIFIER_NONE) {
1029 type_t *const copy = duplicate_type(type);
1031 /* for const with typedefed array type the element type has to be
1033 if (is_type_array(copy)) {
1034 type_t *element_type = copy->array.element_type;
1035 element_type = duplicate_type(element_type);
1036 element_type->base.qualifiers |= qualifiers;
1037 copy->array.element_type = element_type;
1039 copy->base.qualifiers |= qualifiers;
1042 type = identify_new_type(copy);
1048 unsigned get_type_size(type_t *type)
1050 switch (type->kind) {
1054 case TYPE_IMAGINARY:
1056 return get_atomic_type_size(type->atomic.akind);
1058 return get_atomic_type_size(type->atomic.akind) * 2;
1059 case TYPE_COMPOUND_UNION:
1060 layout_union_type(&type->compound);
1061 return type->compound.compound->size;
1062 case TYPE_COMPOUND_STRUCT:
1063 layout_struct_type(&type->compound);
1064 return type->compound.compound->size;
1066 return 1; /* strange GNU extensions: sizeof(function) == 1 */
1067 case TYPE_REFERENCE:
1069 return pointer_properties.size;
1071 /* TODO: correct if element_type is aligned? */
1072 il_size_t element_size = get_type_size(type->array.element_type);
1073 return type->array.size * element_size;
1076 return get_type_size(type->typedeft.typedefe->type);
1078 return get_type_size(type->typeoft.typeof_type);
1080 panic("invalid type");
1083 unsigned get_type_alignment(type_t *type)
1085 switch (type->kind) {
1089 case TYPE_IMAGINARY:
1092 return get_atomic_type_alignment(type->atomic.akind);
1093 case TYPE_COMPOUND_UNION:
1094 layout_union_type(&type->compound);
1095 return type->compound.compound->alignment;
1096 case TYPE_COMPOUND_STRUCT:
1097 layout_struct_type(&type->compound);
1098 return type->compound.compound->alignment;
1100 /* gcc says 1 here... */
1102 case TYPE_REFERENCE:
1104 return pointer_properties.alignment;
1106 return get_type_alignment(type->array.element_type);
1107 case TYPE_TYPEDEF: {
1108 il_alignment_t alignment
1109 = get_type_alignment(type->typedeft.typedefe->type);
1110 if (type->typedeft.typedefe->alignment > alignment)
1111 alignment = type->typedeft.typedefe->alignment;
1116 return get_type_alignment(type->typeoft.typeof_type);
1118 panic("invalid type");
1122 * get alignment of a type when used inside a compound.
1123 * Some ABIs are broken and alignment inside a compound is different from
1124 * recommended alignment of a type
1126 static unsigned get_type_alignment_compound(type_t *const type)
1128 assert(!is_typeref(type));
1129 if (type->kind == TYPE_ATOMIC)
1130 return atomic_type_properties[type->atomic.akind].struct_alignment;
1131 return get_type_alignment(type);
1134 decl_modifiers_t get_type_modifiers(const type_t *type)
1136 switch (type->kind) {
1139 case TYPE_COMPOUND_STRUCT:
1140 case TYPE_COMPOUND_UNION:
1141 return type->compound.compound->modifiers;
1143 return type->function.modifiers;
1147 case TYPE_IMAGINARY:
1148 case TYPE_REFERENCE:
1152 case TYPE_TYPEDEF: {
1153 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1154 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1158 return get_type_modifiers(type->typeoft.typeof_type);
1160 panic("invalid type");
1163 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1165 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1168 switch (type->base.kind) {
1170 return TYPE_QUALIFIER_NONE;
1172 qualifiers |= type->base.qualifiers;
1173 const typedef_type_t *typedef_type = &type->typedeft;
1174 if (typedef_type->resolved_type != NULL)
1175 type = typedef_type->resolved_type;
1177 type = typedef_type->typedefe->type;
1180 type = type->typeoft.typeof_type;
1183 if (skip_array_type) {
1184 type = type->array.element_type;
1193 return type->base.qualifiers | qualifiers;
1196 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1198 assert(kind <= ATOMIC_TYPE_LAST);
1199 return atomic_type_properties[kind].size;
1202 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1204 assert(kind <= ATOMIC_TYPE_LAST);
1205 return atomic_type_properties[kind].alignment;
1208 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1210 assert(kind <= ATOMIC_TYPE_LAST);
1211 return atomic_type_properties[kind].flags;
1215 * Find the atomic type kind representing a given size (signed).
1217 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1219 static atomic_type_kind_t kinds[32];
1222 atomic_type_kind_t kind = kinds[size];
1223 if (kind == (atomic_type_kind_t)0) {
1224 static const atomic_type_kind_t possible_kinds[] = {
1229 ATOMIC_TYPE_LONGLONG
1231 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1232 if (get_atomic_type_size(possible_kinds[i]) == size) {
1233 kind = possible_kinds[i];
1243 * Find the atomic type kind representing a given size (signed).
1245 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1247 static atomic_type_kind_t kinds[32];
1250 atomic_type_kind_t kind = kinds[size];
1251 if (kind == (atomic_type_kind_t)0) {
1252 static const atomic_type_kind_t possible_kinds[] = {
1257 ATOMIC_TYPE_ULONGLONG
1259 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1260 if (get_atomic_type_size(possible_kinds[i]) == size) {
1261 kind = possible_kinds[i];
1271 * Hash the given type and return the "singleton" version
1274 type_t *identify_new_type(type_t *type)
1276 type_t *result = typehash_insert(type);
1277 if (result != type) {
1278 obstack_free(&type_obst, type);
1284 * Creates a new atomic type.
1286 * @param akind The kind of the atomic type.
1287 * @param qualifiers Type qualifiers for the new type.
1289 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1291 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1292 type->base.qualifiers = qualifiers;
1293 type->atomic.akind = akind;
1295 return identify_new_type(type);
1299 * Creates a new complex type.
1301 * @param akind The kind of the atomic type.
1302 * @param qualifiers Type qualifiers for the new type.
1304 type_t *make_complex_type(atomic_type_kind_t akind,
1305 type_qualifiers_t qualifiers)
1307 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1308 type->base.qualifiers = qualifiers;
1309 type->atomic.akind = akind;
1311 return identify_new_type(type);
1315 * Creates a new imaginary type.
1317 * @param akind The kind of the atomic type.
1318 * @param qualifiers Type qualifiers for the new type.
1320 type_t *make_imaginary_type(atomic_type_kind_t akind,
1321 type_qualifiers_t qualifiers)
1323 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1324 type->base.qualifiers = qualifiers;
1325 type->atomic.akind = akind;
1327 return identify_new_type(type);
1331 * Creates a new pointer type.
1333 * @param points_to The points-to type for the new type.
1334 * @param qualifiers Type qualifiers for the new type.
1336 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1338 type_t *const type = allocate_type_zero(TYPE_POINTER);
1339 type->base.qualifiers = qualifiers;
1340 type->pointer.points_to = points_to;
1341 type->pointer.base_variable = NULL;
1343 return identify_new_type(type);
1347 * Creates a new reference type.
1349 * @param refers_to The referred-to type for the new type.
1351 type_t *make_reference_type(type_t *refers_to)
1353 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1354 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1355 type->reference.refers_to = refers_to;
1357 return identify_new_type(type);
1361 * Creates a new based pointer type.
1363 * @param points_to The points-to type for the new type.
1364 * @param qualifiers Type qualifiers for the new type.
1365 * @param variable The based variable
1367 type_t *make_based_pointer_type(type_t *points_to,
1368 type_qualifiers_t qualifiers, variable_t *variable)
1370 type_t *const type = allocate_type_zero(TYPE_POINTER);
1371 type->base.qualifiers = qualifiers;
1372 type->pointer.points_to = points_to;
1373 type->pointer.base_variable = variable;
1375 return identify_new_type(type);
1379 type_t *make_array_type(type_t *element_type, size_t size,
1380 type_qualifiers_t qualifiers)
1382 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1383 type->base.qualifiers = qualifiers;
1384 type->array.element_type = element_type;
1385 type->array.size = size;
1386 type->array.size_constant = true;
1388 return identify_new_type(type);
1391 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1392 il_alignment_t *struct_alignment,
1393 bool packed, entity_t *first)
1395 il_size_t offset = *struct_offset;
1396 il_alignment_t alignment = *struct_alignment;
1397 size_t bit_offset = 0;
1400 for (member = first; member != NULL; member = member->base.next) {
1401 if (member->kind != ENTITY_COMPOUND_MEMBER)
1403 if (!member->compound_member.bitfield)
1406 type_t *const base_type = skip_typeref(member->declaration.type);
1407 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1408 il_alignment_t alignment_mask = base_alignment-1;
1409 if (base_alignment > alignment)
1410 alignment = base_alignment;
1412 size_t bit_size = member->compound_member.bit_size;
1414 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1415 offset &= ~alignment_mask;
1416 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1418 if (bit_offset + bit_size > base_size || bit_size == 0) {
1419 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1420 offset = (offset + base_alignment-1) & ~alignment_mask;
1425 if (byte_order_big_endian) {
1426 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1427 member->compound_member.offset = offset & ~alignment_mask;
1428 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1430 member->compound_member.offset = offset;
1431 member->compound_member.bit_offset = bit_offset;
1434 bit_offset += bit_size;
1435 offset += bit_offset / BITS_PER_BYTE;
1436 bit_offset %= BITS_PER_BYTE;
1442 *struct_offset = offset;
1443 *struct_alignment = alignment;
1447 void layout_struct_type(compound_type_t *type)
1449 assert(type->compound != NULL);
1451 compound_t *compound = type->compound;
1452 if (!compound->complete)
1454 if (type->compound->layouted)
1456 compound->layouted = true;
1458 il_size_t offset = 0;
1459 il_alignment_t alignment = compound->alignment;
1460 bool need_pad = false;
1462 entity_t *entry = compound->members.entities;
1463 while (entry != NULL) {
1464 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1467 type_t *const m_type = skip_typeref(entry->declaration.type);
1468 if (!is_type_valid(m_type))
1471 if (entry->compound_member.bitfield) {
1472 entry = pack_bitfield_members(&offset, &alignment,
1473 compound->packed, entry);
1477 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1478 if (m_alignment > alignment)
1479 alignment = m_alignment;
1481 if (!compound->packed) {
1482 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1484 if (new_offset > offset) {
1486 offset = new_offset;
1490 entry->compound_member.offset = offset;
1491 offset += get_type_size(m_type);
1494 entry = entry->base.next;
1497 if (!compound->packed) {
1498 il_size_t new_offset = (offset + alignment-1) & -alignment;
1499 if (new_offset > offset) {
1501 offset = new_offset;
1505 position_t const *const pos = &compound->base.pos;
1507 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1508 } else if (compound->packed) {
1509 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1512 compound->size = offset;
1513 compound->alignment = alignment;
1516 void layout_union_type(compound_type_t *type)
1518 assert(type->compound != NULL);
1520 compound_t *compound = type->compound;
1521 if (! compound->complete)
1523 if (compound->layouted)
1525 compound->layouted = true;
1528 il_alignment_t alignment = compound->alignment;
1530 entity_t *entry = compound->members.entities;
1531 for (; entry != NULL; entry = entry->base.next) {
1532 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1535 type_t *m_type = skip_typeref(entry->declaration.type);
1536 if (! is_type_valid(skip_typeref(m_type)))
1539 entry->compound_member.offset = 0;
1540 il_size_t m_size = get_type_size(m_type);
1543 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1544 if (m_alignment > alignment)
1545 alignment = m_alignment;
1547 size = (size + alignment - 1) & -alignment;
1549 compound->size = size;
1550 compound->alignment = alignment;
1553 function_parameter_t *allocate_parameter(type_t *const type)
1555 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1556 memset(param, 0, sizeof(*param));
1561 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1562 type_t *argument_type2, decl_modifiers_t modifiers)
1564 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1565 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1566 parameter1->next = parameter2;
1568 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1569 type->function.return_type = return_type;
1570 type->function.parameters = parameter1;
1571 type->function.modifiers |= modifiers;
1572 type->function.linkage = LINKAGE_C;
1574 return identify_new_type(type);
1577 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1578 decl_modifiers_t modifiers)
1580 function_parameter_t *const parameter = allocate_parameter(argument_type);
1582 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1583 type->function.return_type = return_type;
1584 type->function.parameters = parameter;
1585 type->function.modifiers |= modifiers;
1586 type->function.linkage = LINKAGE_C;
1588 return identify_new_type(type);
1591 type_t *make_function_1_type_variadic(type_t *return_type,
1592 type_t *argument_type,
1593 decl_modifiers_t modifiers)
1595 function_parameter_t *const parameter = allocate_parameter(argument_type);
1597 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1598 type->function.return_type = return_type;
1599 type->function.parameters = parameter;
1600 type->function.variadic = true;
1601 type->function.modifiers |= modifiers;
1602 type->function.linkage = LINKAGE_C;
1604 return identify_new_type(type);
1607 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1609 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1610 type->function.return_type = return_type;
1611 type->function.parameters = NULL;
1612 type->function.modifiers |= modifiers;
1613 type->function.linkage = LINKAGE_C;
1615 return identify_new_type(type);
1618 type_t *make_function_type(type_t *return_type, int n_types,
1619 type_t *const *argument_types,
1620 decl_modifiers_t modifiers)
1622 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1623 type->function.return_type = return_type;
1624 type->function.modifiers |= modifiers;
1625 type->function.linkage = LINKAGE_C;
1627 function_parameter_t **anchor = &type->function.parameters;
1628 for (int i = 0; i < n_types; ++i) {
1629 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1630 *anchor = parameter;
1631 anchor = ¶meter->next;
1634 return identify_new_type(type);
1638 * Debug helper. Prints the given type to stdout.
1640 static __attribute__((unused))
1641 void dbg_type(const type_t *type)
1643 print_to_file(stderr);