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"
37 /** The default calling convention. */
38 cc_kind_t default_calling_convention = CC_CDECL;
40 static struct obstack type_obst;
41 static bool print_implicit_array_size = false;
43 static void intern_print_type_pre(const type_t *type);
44 static void intern_print_type_post(const type_t *type);
47 * Returns the size of a type node.
49 * @param kind the type kind
51 static size_t get_type_struct_size(type_kind_t kind)
53 static const size_t sizes[] = {
54 [TYPE_ATOMIC] = sizeof(atomic_type_t),
55 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
56 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
57 [TYPE_ENUM] = sizeof(enum_type_t),
58 [TYPE_FUNCTION] = sizeof(function_type_t),
59 [TYPE_POINTER] = sizeof(pointer_type_t),
60 [TYPE_REFERENCE] = sizeof(reference_type_t),
61 [TYPE_ARRAY] = sizeof(array_type_t),
62 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
63 [TYPE_TYPEOF] = sizeof(typeof_type_t),
65 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
66 assert(kind <= TYPE_TYPEOF);
67 assert(sizes[kind] != 0);
71 type_t *allocate_type_zero(type_kind_t kind)
73 size_t const size = get_type_struct_size(kind);
74 type_t *const res = obstack_alloc(&type_obst, size);
76 res->base.kind = kind;
82 * Properties of atomic types.
84 atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
85 [ATOMIC_TYPE_VOID] = {
88 .flags = ATOMIC_TYPE_FLAG_NONE,
91 [ATOMIC_TYPE_BOOL] = {
94 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
97 [ATOMIC_TYPE_CHAR] = {
100 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
103 [ATOMIC_TYPE_SCHAR] = {
106 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
107 | ATOMIC_TYPE_FLAG_SIGNED,
110 [ATOMIC_TYPE_UCHAR] = {
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
116 [ATOMIC_TYPE_SHORT] = {
119 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
120 | ATOMIC_TYPE_FLAG_SIGNED,
123 [ATOMIC_TYPE_USHORT] = {
126 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
129 [ATOMIC_TYPE_INT] = {
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_UINT] = {
137 .size = (unsigned) -1,
138 .alignment = (unsigned) -1,
139 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
142 [ATOMIC_TYPE_LONG] = {
143 .size = (unsigned) -1,
144 .alignment = (unsigned) -1,
145 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
146 | ATOMIC_TYPE_FLAG_SIGNED,
149 [ATOMIC_TYPE_ULONG] = {
150 .size = (unsigned) -1,
151 .alignment = (unsigned) -1,
152 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
155 [ATOMIC_TYPE_LONGLONG] = {
158 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
159 | ATOMIC_TYPE_FLAG_SIGNED,
162 [ATOMIC_TYPE_ULONGLONG] = {
165 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
168 [ATOMIC_TYPE_FLOAT] = {
171 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
172 | ATOMIC_TYPE_FLAG_SIGNED,
175 [ATOMIC_TYPE_DOUBLE] = {
178 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
179 | ATOMIC_TYPE_FLAG_SIGNED,
182 [ATOMIC_TYPE_WCHAR_T] = {
183 .size = (unsigned)-1,
184 .alignment = (unsigned)-1,
185 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
186 .rank = (unsigned)-1,
189 atomic_type_properties_t pointer_properties = {
192 .flags = ATOMIC_TYPE_FLAG_NONE,
195 static inline bool is_po2(unsigned x)
197 return (x & (x-1)) == 0;
200 void init_types(unsigned machine_size)
202 obstack_init(&type_obst);
204 atomic_type_properties_t *props = atomic_type_properties;
206 /* atempt to set some sane defaults based on machine size */
208 unsigned int_size = machine_size < 32 ? 2 : 4;
209 unsigned long_size = machine_size < 64 ? 4 : 8;
211 props[ATOMIC_TYPE_INT].size = int_size;
212 props[ATOMIC_TYPE_INT].alignment = int_size;
213 props[ATOMIC_TYPE_UINT].size = int_size;
214 props[ATOMIC_TYPE_UINT].alignment = int_size;
215 props[ATOMIC_TYPE_LONG].size = long_size;
216 props[ATOMIC_TYPE_LONG].alignment = long_size;
217 props[ATOMIC_TYPE_ULONG].size = long_size;
218 props[ATOMIC_TYPE_ULONG].alignment = long_size;
220 pointer_properties.size = long_size;
221 pointer_properties.alignment = long_size;
222 pointer_properties.struct_alignment = long_size;
224 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
225 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
227 /* set struct alignments to the same value as alignment */
229 i < sizeof(atomic_type_properties)/sizeof(atomic_type_properties[0]);
231 props[i].struct_alignment = props[i].alignment;
235 void exit_types(void)
237 obstack_free(&type_obst, NULL);
240 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
242 size_t sep = q & QUAL_SEP_START ? 0 : 1;
243 if (qualifiers & TYPE_QUALIFIER_CONST) {
244 print_string(" const" + sep);
247 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
248 print_string(" volatile" + sep);
251 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
252 print_string(" restrict" + sep);
255 if (sep == 0 && q & QUAL_SEP_END)
259 const char *get_atomic_kind_name(atomic_type_kind_t kind)
262 case ATOMIC_TYPE_INVALID: break;
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)
380 if (parameters == NULL) {
381 function_parameter_t *parameter = type->parameters;
382 for( ; parameter != NULL; parameter = parameter->next) {
388 print_type(parameter->type);
391 entity_t *parameter = parameters->entities;
392 for (; parameter != NULL; parameter = parameter->base.next) {
393 if (parameter->kind != ENTITY_PARAMETER)
401 const type_t *const param_type = parameter->declaration.type;
402 if (param_type == NULL) {
403 print_string(parameter->base.symbol->string);
405 print_type_ext(param_type, parameter->base.symbol, NULL);
409 if (type->variadic) {
417 if (first && !type->unspecified_parameters) {
418 print_string("void");
422 intern_print_type_post(type->return_type);
426 * Prints the prefix part of a pointer type.
428 * @param type The pointer type.
430 static void print_pointer_type_pre(const pointer_type_t *type)
432 type_t const *const points_to = type->points_to;
433 intern_print_type_pre(points_to);
434 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
436 variable_t *const variable = type->base_variable;
437 if (variable != NULL) {
438 print_string(" __based(");
439 print_string(variable->base.base.symbol->string);
443 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
447 * Prints the postfix part of a pointer type.
449 * @param type The pointer type.
451 static void print_pointer_type_post(const pointer_type_t *type)
453 type_t const *const points_to = type->points_to;
454 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
456 intern_print_type_post(points_to);
460 * Prints the prefix part of a reference type.
462 * @param type The reference type.
464 static void print_reference_type_pre(const reference_type_t *type)
466 type_t const *const refers_to = type->refers_to;
467 intern_print_type_pre(refers_to);
468 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
474 * Prints the postfix part of a reference type.
476 * @param type The reference type.
478 static void print_reference_type_post(const reference_type_t *type)
480 type_t const *const refers_to = type->refers_to;
481 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
483 intern_print_type_post(refers_to);
487 * Prints the prefix part of an array type.
489 * @param type The array type.
491 static void print_array_type_pre(const array_type_t *type)
493 intern_print_type_pre(type->element_type);
497 * Prints the postfix part of an array type.
499 * @param type The array type.
501 static void print_array_type_post(const array_type_t *type)
504 if (type->is_static) {
505 print_string("static ");
507 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
508 if (type->size_expression != NULL
509 && (print_implicit_array_size || !type->has_implicit_size)) {
510 print_expression(type->size_expression);
513 intern_print_type_post(type->element_type);
517 * Prints an enum definition.
519 * @param declaration The enum's type declaration.
521 void print_enum_definition(const enum_t *enume)
527 entity_t *entry = enume->base.next;
528 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
529 entry = entry->base.next) {
532 print_string(entry->base.symbol->string);
533 if (entry->enum_value.value != NULL) {
536 /* skip the implicit cast */
537 expression_t *expression = entry->enum_value.value;
538 print_expression(expression);
549 * Prints an enum type.
551 * @param type The enum type.
553 static void print_type_enum(const enum_type_t *type)
555 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
556 print_string("enum ");
558 enum_t *enume = type->enume;
559 symbol_t *symbol = enume->base.symbol;
560 if (symbol != NULL) {
561 print_string(symbol->string);
563 print_enum_definition(enume);
568 * Print the compound part of a compound type.
570 void print_compound_definition(const compound_t *compound)
575 entity_t *entity = compound->members.entities;
576 for( ; entity != NULL; entity = entity->base.next) {
577 if (entity->kind != ENTITY_COMPOUND_MEMBER)
581 print_entity(entity);
588 if (compound->modifiers & DM_TRANSPARENT_UNION) {
589 print_string("__attribute__((__transparent_union__))");
594 * Prints a compound type.
596 * @param type The compound type.
598 static void print_compound_type(const compound_type_t *type)
600 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
602 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
603 print_string("struct ");
605 assert(type->base.kind == TYPE_COMPOUND_UNION);
606 print_string("union ");
609 compound_t *compound = type->compound;
610 symbol_t *symbol = compound->base.symbol;
611 if (symbol != NULL) {
612 print_string(symbol->string);
614 print_compound_definition(compound);
619 * Prints the prefix part of a typedef type.
621 * @param type The typedef type.
623 static void print_typedef_type_pre(const typedef_type_t *const type)
625 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
626 print_string(type->typedefe->base.symbol->string);
630 * Prints the prefix part of a typeof type.
632 * @param type The typeof type.
634 static void print_typeof_type_pre(const typeof_type_t *const type)
636 print_string("typeof(");
637 if (type->expression != NULL) {
638 print_expression(type->expression);
640 print_type(type->typeof_type);
646 * Prints the prefix part of a type.
648 * @param type The type.
650 static void intern_print_type_pre(const type_t *const type)
654 print_string("<error>");
657 print_type_enum(&type->enumt);
660 print_atomic_type(&type->atomic);
663 print_complex_type(&type->atomic);
666 print_imaginary_type(&type->atomic);
668 case TYPE_COMPOUND_STRUCT:
669 case TYPE_COMPOUND_UNION:
670 print_compound_type(&type->compound);
673 print_function_type_pre(&type->function);
676 print_pointer_type_pre(&type->pointer);
679 print_reference_type_pre(&type->reference);
682 print_array_type_pre(&type->array);
685 print_typedef_type_pre(&type->typedeft);
688 print_typeof_type_pre(&type->typeoft);
691 print_string("unknown");
695 * Prints the postfix part of a type.
697 * @param type The type.
699 static void intern_print_type_post(const type_t *const type)
703 print_function_type_post(&type->function, NULL);
706 print_pointer_type_post(&type->pointer);
709 print_reference_type_post(&type->reference);
712 print_array_type_post(&type->array);
719 case TYPE_COMPOUND_STRUCT:
720 case TYPE_COMPOUND_UNION:
730 * @param type The type.
732 void print_type(const type_t *const type)
734 print_type_ext(type, NULL, NULL);
737 void print_type_ext(const type_t *const type, const symbol_t *symbol,
738 const scope_t *parameters)
740 intern_print_type_pre(type);
741 if (symbol != NULL) {
743 print_string(symbol->string);
745 if (type->kind == TYPE_FUNCTION) {
746 print_function_type_post(&type->function, parameters);
748 intern_print_type_post(type);
755 * @param type The type to copy.
756 * @return A copy of the type.
758 * @note This does not produce a deep copy!
760 type_t *duplicate_type(const type_t *type)
762 size_t size = get_type_struct_size(type->kind);
764 type_t *const copy = obstack_alloc(&type_obst, size);
765 memcpy(copy, type, size);
766 copy->base.firm_type = NULL;
772 * Returns the unqualified type of a given type.
774 * @param type The type.
775 * @returns The unqualified type.
777 type_t *get_unqualified_type(type_t *type)
779 assert(!is_typeref(type));
781 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
784 type_t *unqualified_type = duplicate_type(type);
785 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
787 return identify_new_type(unqualified_type);
790 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
792 type_t *type = skip_typeref(orig_type);
795 if (is_type_array(type)) {
796 /* For array types the element type has to be adjusted */
797 type_t *element_type = type->array.element_type;
798 type_t *qual_element_type = get_qualified_type(element_type, qual);
800 if (qual_element_type == element_type)
803 copy = duplicate_type(type);
804 copy->array.element_type = qual_element_type;
805 } else if (is_type_valid(type)) {
806 if ((type->base.qualifiers & qual) == (int)qual)
809 copy = duplicate_type(type);
810 copy->base.qualifiers |= qual;
815 return identify_new_type(copy);
818 static bool test_atomic_type_flag(atomic_type_kind_t kind,
819 atomic_type_flag_t flag)
821 assert(kind <= ATOMIC_TYPE_LAST);
822 return (atomic_type_properties[kind].flags & flag) != 0;
826 * Returns true if the given type is an integer type.
828 * @param type The type to check.
829 * @return True if type is an integer type.
831 bool is_type_integer(const type_t *type)
833 assert(!is_typeref(type));
835 if (type->kind == TYPE_ENUM)
837 if (type->kind != TYPE_ATOMIC)
840 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
844 * Returns true if the given type is an enum type.
846 * @param type The type to check.
847 * @return True if type is an enum type.
849 bool is_type_enum(const type_t *type)
851 assert(!is_typeref(type));
852 return type->kind == TYPE_ENUM;
856 * Returns true if the given type is an floating point type.
858 * @param type The type to check.
859 * @return True if type is a floating point type.
861 bool is_type_float(const type_t *type)
863 assert(!is_typeref(type));
865 if (type->kind != TYPE_ATOMIC)
868 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
872 * Returns true if the given type is an complex type.
874 * @param type The type to check.
875 * @return True if type is a complex type.
877 bool is_type_complex(const type_t *type)
879 assert(!is_typeref(type));
881 if (type->kind != TYPE_ATOMIC)
884 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
888 * Returns true if the given type is a signed type.
890 * @param type The type to check.
891 * @return True if type is a signed type.
893 bool is_type_signed(const type_t *type)
895 assert(!is_typeref(type));
897 /* enum types are int for now */
898 if (type->kind == TYPE_ENUM)
900 if (type->kind != TYPE_ATOMIC)
903 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
907 * Returns true if the given type represents an arithmetic type.
909 * @param type The type to check.
910 * @return True if type represents an arithmetic type.
912 bool is_type_arithmetic(const type_t *type)
914 assert(!is_typeref(type));
922 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
929 * Returns true if the given type is an integer or float type.
931 * @param type The type to check.
932 * @return True if type is an integer or float type.
934 bool is_type_real(const type_t *type)
937 return is_type_integer(type) || is_type_float(type);
941 * Returns true if the given type represents a scalar type.
943 * @param type The type to check.
944 * @return True if type represents a scalar type.
946 bool is_type_scalar(const type_t *type)
948 assert(!is_typeref(type));
950 if (type->kind == TYPE_POINTER)
953 return is_type_arithmetic(type);
957 * Check if a given type is incomplete.
959 * @param type The type to check.
960 * @return True if the given type is incomplete (ie. just forward).
962 bool is_type_incomplete(const type_t *type)
964 assert(!is_typeref(type));
967 case TYPE_COMPOUND_STRUCT:
968 case TYPE_COMPOUND_UNION: {
969 const compound_type_t *compound_type = &type->compound;
970 return !compound_type->compound->complete;
976 return type->array.size_expression == NULL
977 && !type->array.size_constant;
982 return type->atomic.akind == ATOMIC_TYPE_VOID;
992 panic("is_type_incomplete called without typerefs skipped");
995 panic("invalid type found");
998 bool is_type_object(const type_t *type)
1000 return !is_type_function(type) && !is_type_incomplete(type);
1004 * Check if two function types are compatible.
1006 static bool function_types_compatible(const function_type_t *func1,
1007 const function_type_t *func2)
1009 const type_t* const ret1 = skip_typeref(func1->return_type);
1010 const type_t* const ret2 = skip_typeref(func2->return_type);
1011 if (!types_compatible(ret1, ret2))
1014 if (func1->linkage != func2->linkage)
1017 cc_kind_t cc1 = func1->calling_convention;
1018 if (cc1 == CC_DEFAULT)
1019 cc1 = default_calling_convention;
1020 cc_kind_t cc2 = func2->calling_convention;
1021 if (cc2 == CC_DEFAULT)
1022 cc2 = default_calling_convention;
1027 if (func1->variadic != func2->variadic)
1030 /* can parameters be compared? */
1031 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1032 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1035 /* TODO: handling of unspecified parameters not correct yet */
1037 /* all argument types must be compatible */
1038 function_parameter_t *parameter1 = func1->parameters;
1039 function_parameter_t *parameter2 = func2->parameters;
1040 for ( ; parameter1 != NULL && parameter2 != NULL;
1041 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1042 type_t *parameter1_type = skip_typeref(parameter1->type);
1043 type_t *parameter2_type = skip_typeref(parameter2->type);
1045 parameter1_type = get_unqualified_type(parameter1_type);
1046 parameter2_type = get_unqualified_type(parameter2_type);
1048 if (!types_compatible(parameter1_type, parameter2_type))
1051 /* same number of arguments? */
1052 if (parameter1 != NULL || parameter2 != NULL)
1059 * Check if two array types are compatible.
1061 static bool array_types_compatible(const array_type_t *array1,
1062 const array_type_t *array2)
1064 type_t *element_type1 = skip_typeref(array1->element_type);
1065 type_t *element_type2 = skip_typeref(array2->element_type);
1066 if (!types_compatible(element_type1, element_type2))
1069 if (!array1->size_constant || !array2->size_constant)
1072 return array1->size == array2->size;
1076 * Check if two types are compatible.
1078 bool types_compatible(const type_t *type1, const type_t *type2)
1080 assert(!is_typeref(type1));
1081 assert(!is_typeref(type2));
1083 /* shortcut: the same type is always compatible */
1087 if (!is_type_valid(type1) || !is_type_valid(type2))
1090 if (type1->base.qualifiers != type2->base.qualifiers)
1092 if (type1->kind != type2->kind)
1095 switch (type1->kind) {
1097 return function_types_compatible(&type1->function, &type2->function);
1099 case TYPE_IMAGINARY:
1101 return type1->atomic.akind == type2->atomic.akind;
1103 return array_types_compatible(&type1->array, &type2->array);
1105 case TYPE_POINTER: {
1106 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1107 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1108 return types_compatible(to1, to2);
1111 case TYPE_REFERENCE: {
1112 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1113 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1114 return types_compatible(to1, to2);
1117 case TYPE_COMPOUND_STRUCT:
1118 case TYPE_COMPOUND_UNION: {
1122 /* TODO: not implemented */
1126 /* Hmm, the error type should be compatible to all other types */
1130 panic("typerefs not skipped in compatible types?!?");
1137 * Skip all typerefs and return the underlying type.
1139 type_t *skip_typeref(type_t *type)
1141 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1144 switch (type->kind) {
1147 case TYPE_TYPEDEF: {
1148 qualifiers |= type->base.qualifiers;
1150 const typedef_type_t *typedef_type = &type->typedeft;
1151 if (typedef_type->resolved_type != NULL) {
1152 type = typedef_type->resolved_type;
1155 type = typedef_type->typedefe->type;
1159 qualifiers |= type->base.qualifiers;
1160 type = type->typeoft.typeof_type;
1168 if (qualifiers != TYPE_QUALIFIER_NONE) {
1169 type_t *const copy = duplicate_type(type);
1171 /* for const with typedefed array type the element type has to be
1173 if (is_type_array(copy)) {
1174 type_t *element_type = copy->array.element_type;
1175 element_type = duplicate_type(element_type);
1176 element_type->base.qualifiers |= qualifiers;
1177 copy->array.element_type = element_type;
1179 copy->base.qualifiers |= qualifiers;
1182 type = identify_new_type(copy);
1188 unsigned get_type_size(type_t *type)
1190 switch (type->kind) {
1194 case TYPE_IMAGINARY:
1196 return get_atomic_type_size(type->atomic.akind);
1198 return get_atomic_type_size(type->atomic.akind) * 2;
1199 case TYPE_COMPOUND_UNION:
1200 layout_union_type(&type->compound);
1201 return type->compound.compound->size;
1202 case TYPE_COMPOUND_STRUCT:
1203 layout_struct_type(&type->compound);
1204 return type->compound.compound->size;
1206 return 0; /* non-const (but "address-const") */
1207 case TYPE_REFERENCE:
1209 return pointer_properties.size;
1211 /* TODO: correct if element_type is aligned? */
1212 il_size_t element_size = get_type_size(type->array.element_type);
1213 return type->array.size * element_size;
1216 return get_type_size(type->typedeft.typedefe->type);
1218 if (type->typeoft.typeof_type) {
1219 return get_type_size(type->typeoft.typeof_type);
1221 return get_type_size(type->typeoft.expression->base.type);
1224 panic("invalid type in get_type_size");
1227 unsigned get_type_alignment(type_t *type)
1229 switch (type->kind) {
1233 case TYPE_IMAGINARY:
1236 return get_atomic_type_alignment(type->atomic.akind);
1237 case TYPE_COMPOUND_UNION:
1238 layout_union_type(&type->compound);
1239 return type->compound.compound->alignment;
1240 case TYPE_COMPOUND_STRUCT:
1241 layout_struct_type(&type->compound);
1242 return type->compound.compound->alignment;
1244 /* gcc says 1 here... */
1246 case TYPE_REFERENCE:
1248 return pointer_properties.alignment;
1250 return get_type_alignment(type->array.element_type);
1251 case TYPE_TYPEDEF: {
1252 il_alignment_t alignment
1253 = get_type_alignment(type->typedeft.typedefe->type);
1254 if (type->typedeft.typedefe->alignment > alignment)
1255 alignment = type->typedeft.typedefe->alignment;
1260 if (type->typeoft.typeof_type) {
1261 return get_type_alignment(type->typeoft.typeof_type);
1263 return get_type_alignment(type->typeoft.expression->base.type);
1266 panic("invalid type in get_type_alignment");
1269 unsigned get_type_alignment_compound(type_t *type)
1271 if (type->kind == TYPE_ATOMIC)
1272 return atomic_type_properties[type->atomic.akind].struct_alignment;
1273 return get_type_alignment(type);
1276 decl_modifiers_t get_type_modifiers(const type_t *type)
1278 switch(type->kind) {
1281 case TYPE_COMPOUND_STRUCT:
1282 case TYPE_COMPOUND_UNION:
1283 return type->compound.compound->modifiers;
1285 return type->function.modifiers;
1289 case TYPE_IMAGINARY:
1290 case TYPE_REFERENCE:
1294 case TYPE_TYPEDEF: {
1295 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1296 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1300 if (type->typeoft.typeof_type) {
1301 return get_type_modifiers(type->typeoft.typeof_type);
1303 return get_type_modifiers(type->typeoft.expression->base.type);
1306 panic("invalid type found in get_type_modifiers");
1309 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1311 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1314 switch (type->base.kind) {
1316 return TYPE_QUALIFIER_NONE;
1318 qualifiers |= type->base.qualifiers;
1319 const typedef_type_t *typedef_type = &type->typedeft;
1320 if (typedef_type->resolved_type != NULL)
1321 type = typedef_type->resolved_type;
1323 type = typedef_type->typedefe->type;
1326 type = type->typeoft.typeof_type;
1329 if (skip_array_type) {
1330 type = type->array.element_type;
1339 return type->base.qualifiers | qualifiers;
1342 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1344 assert(kind <= ATOMIC_TYPE_LAST);
1345 return atomic_type_properties[kind].size;
1348 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1350 assert(kind <= ATOMIC_TYPE_LAST);
1351 return atomic_type_properties[kind].alignment;
1354 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1356 assert(kind <= ATOMIC_TYPE_LAST);
1357 return atomic_type_properties[kind].flags;
1361 * Find the atomic type kind representing a given size (signed).
1363 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1365 static atomic_type_kind_t kinds[32];
1368 atomic_type_kind_t kind = kinds[size];
1369 if (kind == ATOMIC_TYPE_INVALID) {
1370 static const atomic_type_kind_t possible_kinds[] = {
1375 ATOMIC_TYPE_LONGLONG
1377 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1378 if (get_atomic_type_size(possible_kinds[i]) == size) {
1379 kind = possible_kinds[i];
1389 * Find the atomic type kind representing a given size (signed).
1391 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1393 static atomic_type_kind_t kinds[32];
1396 atomic_type_kind_t kind = kinds[size];
1397 if (kind == ATOMIC_TYPE_INVALID) {
1398 static const atomic_type_kind_t possible_kinds[] = {
1403 ATOMIC_TYPE_ULONGLONG
1405 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1406 if (get_atomic_type_size(possible_kinds[i]) == size) {
1407 kind = possible_kinds[i];
1417 * Hash the given type and return the "singleton" version
1420 type_t *identify_new_type(type_t *type)
1422 type_t *result = typehash_insert(type);
1423 if (result != type) {
1424 obstack_free(&type_obst, type);
1430 * Creates a new atomic type.
1432 * @param akind The kind of the atomic type.
1433 * @param qualifiers Type qualifiers for the new type.
1435 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1437 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1438 type->base.qualifiers = qualifiers;
1439 type->atomic.akind = akind;
1441 return identify_new_type(type);
1445 * Creates a new complex type.
1447 * @param akind The kind of the atomic type.
1448 * @param qualifiers Type qualifiers for the new type.
1450 type_t *make_complex_type(atomic_type_kind_t akind,
1451 type_qualifiers_t qualifiers)
1453 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1454 type->base.qualifiers = qualifiers;
1455 type->atomic.akind = akind;
1457 return identify_new_type(type);
1461 * Creates a new imaginary type.
1463 * @param akind The kind of the atomic type.
1464 * @param qualifiers Type qualifiers for the new type.
1466 type_t *make_imaginary_type(atomic_type_kind_t akind,
1467 type_qualifiers_t qualifiers)
1469 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1470 type->base.qualifiers = qualifiers;
1471 type->atomic.akind = akind;
1473 return identify_new_type(type);
1477 * Creates a new pointer type.
1479 * @param points_to The points-to type for the new type.
1480 * @param qualifiers Type qualifiers for the new type.
1482 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1484 type_t *const type = allocate_type_zero(TYPE_POINTER);
1485 type->base.qualifiers = qualifiers;
1486 type->pointer.points_to = points_to;
1487 type->pointer.base_variable = NULL;
1489 return identify_new_type(type);
1493 * Creates a new reference type.
1495 * @param refers_to The referred-to type for the new type.
1497 type_t *make_reference_type(type_t *refers_to)
1499 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1500 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1501 type->reference.refers_to = refers_to;
1503 return identify_new_type(type);
1507 * Creates a new based pointer type.
1509 * @param points_to The points-to type for the new type.
1510 * @param qualifiers Type qualifiers for the new type.
1511 * @param variable The based variable
1513 type_t *make_based_pointer_type(type_t *points_to,
1514 type_qualifiers_t qualifiers, variable_t *variable)
1516 type_t *const type = allocate_type_zero(TYPE_POINTER);
1517 type->base.qualifiers = qualifiers;
1518 type->pointer.points_to = points_to;
1519 type->pointer.base_variable = variable;
1521 return identify_new_type(type);
1525 type_t *make_array_type(type_t *element_type, size_t size,
1526 type_qualifiers_t qualifiers)
1528 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1529 type->base.qualifiers = qualifiers;
1530 type->array.element_type = element_type;
1531 type->array.size = size;
1532 type->array.size_constant = true;
1534 return identify_new_type(type);
1537 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1538 il_alignment_t *struct_alignment,
1539 bool packed, entity_t *first)
1541 il_size_t offset = *struct_offset;
1542 il_alignment_t alignment = *struct_alignment;
1543 size_t bit_offset = 0;
1546 for (member = first; member != NULL; member = member->base.next) {
1547 if (member->kind != ENTITY_COMPOUND_MEMBER)
1549 if (!member->compound_member.bitfield)
1552 type_t *base_type = member->declaration.type;
1553 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1554 il_alignment_t alignment_mask = base_alignment-1;
1555 if (base_alignment > alignment)
1556 alignment = base_alignment;
1558 size_t bit_size = member->compound_member.bit_size;
1560 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1561 offset &= ~alignment_mask;
1562 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1564 if (bit_offset + bit_size > base_size || bit_size == 0) {
1565 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1566 offset = (offset + base_alignment-1) & ~alignment_mask;
1571 if (byte_order_big_endian) {
1572 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1573 member->compound_member.offset = offset & ~alignment_mask;
1574 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1576 member->compound_member.offset = offset;
1577 member->compound_member.bit_offset = bit_offset;
1580 bit_offset += bit_size;
1581 offset += bit_offset / BITS_PER_BYTE;
1582 bit_offset %= BITS_PER_BYTE;
1588 *struct_offset = offset;
1589 *struct_alignment = alignment;
1593 void layout_struct_type(compound_type_t *type)
1595 assert(type->compound != NULL);
1597 compound_t *compound = type->compound;
1598 if (!compound->complete)
1600 if (type->compound->layouted)
1603 il_size_t offset = 0;
1604 il_alignment_t alignment = compound->alignment;
1605 bool need_pad = false;
1607 entity_t *entry = compound->members.entities;
1608 while (entry != NULL) {
1609 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1610 entry = entry->base.next;
1614 type_t *m_type = entry->declaration.type;
1615 type_t *skipped = skip_typeref(m_type);
1616 if (! is_type_valid(skipped)) {
1617 entry = entry->base.next;
1621 if (entry->compound_member.bitfield) {
1622 entry = pack_bitfield_members(&offset, &alignment,
1623 compound->packed, entry);
1627 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1628 if (m_alignment > alignment)
1629 alignment = m_alignment;
1631 if (!compound->packed) {
1632 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1634 if (new_offset > offset) {
1636 offset = new_offset;
1640 entry->compound_member.offset = offset;
1641 offset += get_type_size(m_type);
1643 entry = entry->base.next;
1646 if (!compound->packed) {
1647 il_size_t new_offset = (offset + alignment-1) & -alignment;
1648 if (new_offset > offset) {
1650 offset = new_offset;
1654 source_position_t const *const pos = &compound->base.source_position;
1656 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1657 } else if (compound->packed) {
1658 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1661 compound->size = offset;
1662 compound->alignment = alignment;
1663 compound->layouted = true;
1666 void layout_union_type(compound_type_t *type)
1668 assert(type->compound != NULL);
1670 compound_t *compound = type->compound;
1671 if (! compound->complete)
1675 il_alignment_t alignment = compound->alignment;
1677 entity_t *entry = compound->members.entities;
1678 for (; entry != NULL; entry = entry->base.next) {
1679 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1682 type_t *m_type = entry->declaration.type;
1683 if (! is_type_valid(skip_typeref(m_type)))
1686 entry->compound_member.offset = 0;
1687 il_size_t m_size = get_type_size(m_type);
1690 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1691 if (m_alignment > alignment)
1692 alignment = m_alignment;
1694 size = (size + alignment - 1) & -alignment;
1696 compound->size = size;
1697 compound->alignment = alignment;
1700 function_parameter_t *allocate_parameter(type_t *const type)
1702 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1703 memset(param, 0, sizeof(*param));
1708 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1709 type_t *argument_type2, decl_modifiers_t modifiers)
1711 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1712 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1713 parameter1->next = parameter2;
1715 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1716 type->function.return_type = return_type;
1717 type->function.parameters = parameter1;
1718 type->function.modifiers |= modifiers;
1719 type->function.linkage = LINKAGE_C;
1721 return identify_new_type(type);
1724 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1725 decl_modifiers_t modifiers)
1727 function_parameter_t *const parameter = allocate_parameter(argument_type);
1729 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1730 type->function.return_type = return_type;
1731 type->function.parameters = parameter;
1732 type->function.modifiers |= modifiers;
1733 type->function.linkage = LINKAGE_C;
1735 return identify_new_type(type);
1738 type_t *make_function_1_type_variadic(type_t *return_type,
1739 type_t *argument_type,
1740 decl_modifiers_t modifiers)
1742 function_parameter_t *const parameter = allocate_parameter(argument_type);
1744 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1745 type->function.return_type = return_type;
1746 type->function.parameters = parameter;
1747 type->function.variadic = true;
1748 type->function.modifiers |= modifiers;
1749 type->function.linkage = LINKAGE_C;
1751 return identify_new_type(type);
1754 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1756 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1757 type->function.return_type = return_type;
1758 type->function.parameters = NULL;
1759 type->function.modifiers |= modifiers;
1760 type->function.linkage = LINKAGE_C;
1762 return identify_new_type(type);
1765 type_t *make_function_type(type_t *return_type, int n_types,
1766 type_t *const *argument_types,
1767 decl_modifiers_t modifiers)
1769 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1770 type->function.return_type = return_type;
1771 type->function.modifiers |= modifiers;
1772 type->function.linkage = LINKAGE_C;
1774 function_parameter_t **anchor = &type->function.parameters;
1775 for (int i = 0; i < n_types; ++i) {
1776 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1777 *anchor = parameter;
1778 anchor = ¶meter->next;
1781 return identify_new_type(type);
1785 * Debug helper. Prints the given type to stdout.
1787 static __attribute__((unused))
1788 void dbg_type(const type_t *type)
1790 print_to_file(stderr);