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 */
228 for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
229 props[i].struct_alignment = props[i].alignment;
233 void exit_types(void)
235 obstack_free(&type_obst, NULL);
238 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
240 size_t sep = q & QUAL_SEP_START ? 0 : 1;
241 if (qualifiers & TYPE_QUALIFIER_CONST) {
242 print_string(" const" + sep);
245 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
246 print_string(" volatile" + sep);
249 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
250 print_string(" restrict" + sep);
253 if (sep == 0 && q & QUAL_SEP_END)
257 const char *get_atomic_kind_name(atomic_type_kind_t kind)
260 case ATOMIC_TYPE_INVALID: break;
261 case ATOMIC_TYPE_VOID: return "void";
262 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
263 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
264 case ATOMIC_TYPE_CHAR: return "char";
265 case ATOMIC_TYPE_SCHAR: return "signed char";
266 case ATOMIC_TYPE_UCHAR: return "unsigned char";
267 case ATOMIC_TYPE_INT: return "int";
268 case ATOMIC_TYPE_UINT: return "unsigned int";
269 case ATOMIC_TYPE_SHORT: return "short";
270 case ATOMIC_TYPE_USHORT: return "unsigned short";
271 case ATOMIC_TYPE_LONG: return "long";
272 case ATOMIC_TYPE_ULONG: return "unsigned long";
273 case ATOMIC_TYPE_LONGLONG: return "long long";
274 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
275 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
276 case ATOMIC_TYPE_FLOAT: return "float";
277 case ATOMIC_TYPE_DOUBLE: return "double";
279 return "INVALIDATOMIC";
283 * Prints the name of an atomic type kinds.
285 * @param kind The type kind.
287 static void print_atomic_kinds(atomic_type_kind_t kind)
289 const char *s = get_atomic_kind_name(kind);
294 * Prints the name of an atomic type.
296 * @param type The type.
298 static void print_atomic_type(const atomic_type_t *type)
300 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
301 print_atomic_kinds(type->akind);
305 * Prints the name of a complex type.
307 * @param type The type.
309 static void print_complex_type(const atomic_type_t *type)
311 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
312 print_string("_Complex");
313 print_atomic_kinds(type->akind);
317 * Prints the name of an imaginary type.
319 * @param type The type.
321 static void print_imaginary_type(const atomic_type_t *type)
323 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
324 print_string("_Imaginary ");
325 print_atomic_kinds(type->akind);
329 * Print the first part (the prefix) of a type.
331 * @param type The type to print.
333 static void print_function_type_pre(const function_type_t *type)
335 switch (type->linkage) {
338 print_string("extern \"C\" ");
342 if (!(c_mode & _CXX))
343 print_string("extern \"C++\" ");
347 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
349 intern_print_type_pre(type->return_type);
351 cc_kind_t cc = type->calling_convention;
354 case CC_CDECL: print_string(" __cdecl"); break;
355 case CC_STDCALL: print_string(" __stdcall"); break;
356 case CC_FASTCALL: print_string(" __fastcall"); break;
357 case CC_THISCALL: print_string(" __thiscall"); break;
359 if (default_calling_convention != CC_CDECL) {
360 /* show the default calling convention if its not cdecl */
361 cc = default_calling_convention;
369 * Print the second part (the postfix) of a type.
371 * @param type The type to print.
373 static void print_function_type_post(const function_type_t *type,
374 const scope_t *parameters)
378 if (parameters == NULL) {
379 function_parameter_t *parameter = type->parameters;
380 for( ; parameter != NULL; parameter = parameter->next) {
386 print_type(parameter->type);
389 entity_t *parameter = parameters->entities;
390 for (; parameter != NULL; parameter = parameter->base.next) {
391 if (parameter->kind != ENTITY_PARAMETER)
399 const type_t *const param_type = parameter->declaration.type;
400 if (param_type == NULL) {
401 print_string(parameter->base.symbol->string);
403 print_type_ext(param_type, parameter->base.symbol, NULL);
407 if (type->variadic) {
415 if (first && !type->unspecified_parameters) {
416 print_string("void");
420 intern_print_type_post(type->return_type);
424 * Prints the prefix part of a pointer type.
426 * @param type The pointer type.
428 static void print_pointer_type_pre(const pointer_type_t *type)
430 type_t const *const points_to = type->points_to;
431 intern_print_type_pre(points_to);
432 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
434 variable_t *const variable = type->base_variable;
435 if (variable != NULL) {
436 print_string(" __based(");
437 print_string(variable->base.base.symbol->string);
441 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
445 * Prints the postfix part of a pointer type.
447 * @param type The pointer type.
449 static void print_pointer_type_post(const pointer_type_t *type)
451 type_t const *const points_to = type->points_to;
452 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
454 intern_print_type_post(points_to);
458 * Prints the prefix part of a reference type.
460 * @param type The reference type.
462 static void print_reference_type_pre(const reference_type_t *type)
464 type_t const *const refers_to = type->refers_to;
465 intern_print_type_pre(refers_to);
466 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
472 * Prints the postfix part of a reference type.
474 * @param type The reference type.
476 static void print_reference_type_post(const reference_type_t *type)
478 type_t const *const refers_to = type->refers_to;
479 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
481 intern_print_type_post(refers_to);
485 * Prints the prefix part of an array type.
487 * @param type The array type.
489 static void print_array_type_pre(const array_type_t *type)
491 intern_print_type_pre(type->element_type);
495 * Prints the postfix part of an array type.
497 * @param type The array type.
499 static void print_array_type_post(const array_type_t *type)
502 if (type->is_static) {
503 print_string("static ");
505 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
506 if (type->size_expression != NULL
507 && (print_implicit_array_size || !type->has_implicit_size)) {
508 print_expression(type->size_expression);
511 intern_print_type_post(type->element_type);
515 * Prints an enum definition.
517 * @param declaration The enum's type declaration.
519 void print_enum_definition(const enum_t *enume)
525 entity_t *entry = enume->base.next;
526 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
527 entry = entry->base.next) {
530 print_string(entry->base.symbol->string);
531 if (entry->enum_value.value != NULL) {
534 /* skip the implicit cast */
535 expression_t *expression = entry->enum_value.value;
536 print_expression(expression);
547 * Prints an enum type.
549 * @param type The enum type.
551 static void print_type_enum(const enum_type_t *type)
553 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
554 print_string("enum ");
556 enum_t *enume = type->enume;
557 symbol_t *symbol = enume->base.symbol;
558 if (symbol != NULL) {
559 print_string(symbol->string);
561 print_enum_definition(enume);
566 * Print the compound part of a compound type.
568 void print_compound_definition(const compound_t *compound)
573 entity_t *entity = compound->members.entities;
574 for( ; entity != NULL; entity = entity->base.next) {
575 if (entity->kind != ENTITY_COMPOUND_MEMBER)
579 print_entity(entity);
586 if (compound->modifiers & DM_TRANSPARENT_UNION) {
587 print_string("__attribute__((__transparent_union__))");
592 * Prints a compound type.
594 * @param type The compound type.
596 static void print_compound_type(const compound_type_t *type)
598 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
600 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
601 print_string("struct ");
603 assert(type->base.kind == TYPE_COMPOUND_UNION);
604 print_string("union ");
607 compound_t *compound = type->compound;
608 symbol_t *symbol = compound->base.symbol;
609 if (symbol != NULL) {
610 print_string(symbol->string);
612 print_compound_definition(compound);
617 * Prints the prefix part of a typedef type.
619 * @param type The typedef type.
621 static void print_typedef_type_pre(const typedef_type_t *const type)
623 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
624 print_string(type->typedefe->base.symbol->string);
628 * Prints the prefix part of a typeof type.
630 * @param type The typeof type.
632 static void print_typeof_type_pre(const typeof_type_t *const type)
634 print_string("typeof(");
635 if (type->expression != NULL) {
636 print_expression(type->expression);
638 print_type(type->typeof_type);
644 * Prints the prefix part of a type.
646 * @param type The type.
648 static void intern_print_type_pre(const type_t *const type)
652 print_string("<error>");
655 print_type_enum(&type->enumt);
658 print_atomic_type(&type->atomic);
661 print_complex_type(&type->atomic);
664 print_imaginary_type(&type->atomic);
666 case TYPE_COMPOUND_STRUCT:
667 case TYPE_COMPOUND_UNION:
668 print_compound_type(&type->compound);
671 print_function_type_pre(&type->function);
674 print_pointer_type_pre(&type->pointer);
677 print_reference_type_pre(&type->reference);
680 print_array_type_pre(&type->array);
683 print_typedef_type_pre(&type->typedeft);
686 print_typeof_type_pre(&type->typeoft);
689 print_string("unknown");
693 * Prints the postfix part of a type.
695 * @param type The type.
697 static void intern_print_type_post(const type_t *const type)
701 print_function_type_post(&type->function, NULL);
704 print_pointer_type_post(&type->pointer);
707 print_reference_type_post(&type->reference);
710 print_array_type_post(&type->array);
717 case TYPE_COMPOUND_STRUCT:
718 case TYPE_COMPOUND_UNION:
728 * @param type The type.
730 void print_type(const type_t *const type)
732 print_type_ext(type, NULL, NULL);
735 void print_type_ext(const type_t *const type, const symbol_t *symbol,
736 const scope_t *parameters)
738 intern_print_type_pre(type);
739 if (symbol != NULL) {
741 print_string(symbol->string);
743 if (type->kind == TYPE_FUNCTION) {
744 print_function_type_post(&type->function, parameters);
746 intern_print_type_post(type);
753 * @param type The type to copy.
754 * @return A copy of the type.
756 * @note This does not produce a deep copy!
758 type_t *duplicate_type(const type_t *type)
760 size_t size = get_type_struct_size(type->kind);
762 type_t *const copy = obstack_alloc(&type_obst, size);
763 memcpy(copy, type, size);
764 copy->base.firm_type = NULL;
770 * Returns the unqualified type of a given type.
772 * @param type The type.
773 * @returns The unqualified type.
775 type_t *get_unqualified_type(type_t *type)
777 assert(!is_typeref(type));
779 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
782 type_t *unqualified_type = duplicate_type(type);
783 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
785 return identify_new_type(unqualified_type);
788 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
790 type_t *type = skip_typeref(orig_type);
793 if (is_type_array(type)) {
794 /* For array types the element type has to be adjusted */
795 type_t *element_type = type->array.element_type;
796 type_t *qual_element_type = get_qualified_type(element_type, qual);
798 if (qual_element_type == element_type)
801 copy = duplicate_type(type);
802 copy->array.element_type = qual_element_type;
803 } else if (is_type_valid(type)) {
804 if ((type->base.qualifiers & qual) == (int)qual)
807 copy = duplicate_type(type);
808 copy->base.qualifiers |= qual;
813 return identify_new_type(copy);
816 static bool test_atomic_type_flag(atomic_type_kind_t kind,
817 atomic_type_flag_t flag)
819 assert(kind <= ATOMIC_TYPE_LAST);
820 return (atomic_type_properties[kind].flags & flag) != 0;
824 * Returns true if the given type is an integer type.
826 * @param type The type to check.
827 * @return True if type is an integer type.
829 bool is_type_integer(const type_t *type)
831 assert(!is_typeref(type));
833 if (type->kind == TYPE_ENUM)
835 if (type->kind != TYPE_ATOMIC)
838 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
842 * Returns true if the given type is an enum type.
844 * @param type The type to check.
845 * @return True if type is an enum type.
847 bool is_type_enum(const type_t *type)
849 assert(!is_typeref(type));
850 return type->kind == TYPE_ENUM;
854 * Returns true if the given type is an floating point type.
856 * @param type The type to check.
857 * @return True if type is a floating point type.
859 bool is_type_float(const type_t *type)
861 assert(!is_typeref(type));
863 if (type->kind != TYPE_ATOMIC)
866 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
870 * Returns true if the given type is an complex type.
872 * @param type The type to check.
873 * @return True if type is a complex type.
875 bool is_type_complex(const type_t *type)
877 assert(!is_typeref(type));
879 if (type->kind != TYPE_ATOMIC)
882 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
886 * Returns true if the given type is a signed type.
888 * @param type The type to check.
889 * @return True if type is a signed type.
891 bool is_type_signed(const type_t *type)
893 assert(!is_typeref(type));
895 /* enum types are int for now */
896 if (type->kind == TYPE_ENUM)
898 if (type->kind != TYPE_ATOMIC)
901 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
905 * Returns true if the given type represents an arithmetic type.
907 * @param type The type to check.
908 * @return True if type represents an arithmetic type.
910 bool is_type_arithmetic(const type_t *type)
912 assert(!is_typeref(type));
920 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
927 * Returns true if the given type is an integer or float type.
929 * @param type The type to check.
930 * @return True if type is an integer or float type.
932 bool is_type_real(const type_t *type)
935 return is_type_integer(type) || is_type_float(type);
939 * Returns true if the given type represents a scalar type.
941 * @param type The type to check.
942 * @return True if type represents a scalar type.
944 bool is_type_scalar(const type_t *type)
946 assert(!is_typeref(type));
948 if (type->kind == TYPE_POINTER)
951 return is_type_arithmetic(type);
955 * Check if a given type is incomplete.
957 * @param type The type to check.
958 * @return True if the given type is incomplete (ie. just forward).
960 bool is_type_incomplete(const type_t *type)
962 assert(!is_typeref(type));
965 case TYPE_COMPOUND_STRUCT:
966 case TYPE_COMPOUND_UNION: {
967 const compound_type_t *compound_type = &type->compound;
968 return !compound_type->compound->complete;
974 return type->array.size_expression == NULL
975 && !type->array.size_constant;
980 return type->atomic.akind == ATOMIC_TYPE_VOID;
990 panic("is_type_incomplete called without typerefs skipped");
993 panic("invalid type found");
996 bool is_type_object(const type_t *type)
998 return !is_type_function(type) && !is_type_incomplete(type);
1002 * Check if two function types are compatible.
1004 static bool function_types_compatible(const function_type_t *func1,
1005 const function_type_t *func2)
1007 const type_t* const ret1 = skip_typeref(func1->return_type);
1008 const type_t* const ret2 = skip_typeref(func2->return_type);
1009 if (!types_compatible(ret1, ret2))
1012 if (func1->linkage != func2->linkage)
1015 cc_kind_t cc1 = func1->calling_convention;
1016 if (cc1 == CC_DEFAULT)
1017 cc1 = default_calling_convention;
1018 cc_kind_t cc2 = func2->calling_convention;
1019 if (cc2 == CC_DEFAULT)
1020 cc2 = default_calling_convention;
1025 if (func1->variadic != func2->variadic)
1028 /* can parameters be compared? */
1029 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1030 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1033 /* TODO: handling of unspecified parameters not correct yet */
1035 /* all argument types must be compatible */
1036 function_parameter_t *parameter1 = func1->parameters;
1037 function_parameter_t *parameter2 = func2->parameters;
1038 for ( ; parameter1 != NULL && parameter2 != NULL;
1039 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1040 type_t *parameter1_type = skip_typeref(parameter1->type);
1041 type_t *parameter2_type = skip_typeref(parameter2->type);
1043 parameter1_type = get_unqualified_type(parameter1_type);
1044 parameter2_type = get_unqualified_type(parameter2_type);
1046 if (!types_compatible(parameter1_type, parameter2_type))
1049 /* same number of arguments? */
1050 if (parameter1 != NULL || parameter2 != NULL)
1057 * Check if two array types are compatible.
1059 static bool array_types_compatible(const array_type_t *array1,
1060 const array_type_t *array2)
1062 type_t *element_type1 = skip_typeref(array1->element_type);
1063 type_t *element_type2 = skip_typeref(array2->element_type);
1064 if (!types_compatible(element_type1, element_type2))
1067 if (!array1->size_constant || !array2->size_constant)
1070 return array1->size == array2->size;
1074 * Check if two types are compatible.
1076 bool types_compatible(const type_t *type1, const type_t *type2)
1078 assert(!is_typeref(type1));
1079 assert(!is_typeref(type2));
1081 /* shortcut: the same type is always compatible */
1085 if (!is_type_valid(type1) || !is_type_valid(type2))
1088 if (type1->base.qualifiers != type2->base.qualifiers)
1090 if (type1->kind != type2->kind)
1093 switch (type1->kind) {
1095 return function_types_compatible(&type1->function, &type2->function);
1097 case TYPE_IMAGINARY:
1099 return type1->atomic.akind == type2->atomic.akind;
1101 return array_types_compatible(&type1->array, &type2->array);
1103 case TYPE_POINTER: {
1104 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1105 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1106 return types_compatible(to1, to2);
1109 case TYPE_REFERENCE: {
1110 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1111 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1112 return types_compatible(to1, to2);
1115 case TYPE_COMPOUND_STRUCT:
1116 case TYPE_COMPOUND_UNION: {
1120 /* TODO: not implemented */
1124 /* Hmm, the error type should be compatible to all other types */
1128 panic("typerefs not skipped in compatible types?!?");
1135 * Skip all typerefs and return the underlying type.
1137 type_t *skip_typeref(type_t *type)
1139 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1142 switch (type->kind) {
1145 case TYPE_TYPEDEF: {
1146 qualifiers |= type->base.qualifiers;
1148 const typedef_type_t *typedef_type = &type->typedeft;
1149 if (typedef_type->resolved_type != NULL) {
1150 type = typedef_type->resolved_type;
1153 type = typedef_type->typedefe->type;
1157 qualifiers |= type->base.qualifiers;
1158 type = type->typeoft.typeof_type;
1166 if (qualifiers != TYPE_QUALIFIER_NONE) {
1167 type_t *const copy = duplicate_type(type);
1169 /* for const with typedefed array type the element type has to be
1171 if (is_type_array(copy)) {
1172 type_t *element_type = copy->array.element_type;
1173 element_type = duplicate_type(element_type);
1174 element_type->base.qualifiers |= qualifiers;
1175 copy->array.element_type = element_type;
1177 copy->base.qualifiers |= qualifiers;
1180 type = identify_new_type(copy);
1186 unsigned get_type_size(type_t *type)
1188 switch (type->kind) {
1192 case TYPE_IMAGINARY:
1194 return get_atomic_type_size(type->atomic.akind);
1196 return get_atomic_type_size(type->atomic.akind) * 2;
1197 case TYPE_COMPOUND_UNION:
1198 layout_union_type(&type->compound);
1199 return type->compound.compound->size;
1200 case TYPE_COMPOUND_STRUCT:
1201 layout_struct_type(&type->compound);
1202 return type->compound.compound->size;
1204 return 0; /* non-const (but "address-const") */
1205 case TYPE_REFERENCE:
1207 return pointer_properties.size;
1209 /* TODO: correct if element_type is aligned? */
1210 il_size_t element_size = get_type_size(type->array.element_type);
1211 return type->array.size * element_size;
1214 return get_type_size(type->typedeft.typedefe->type);
1216 if (type->typeoft.typeof_type) {
1217 return get_type_size(type->typeoft.typeof_type);
1219 return get_type_size(type->typeoft.expression->base.type);
1222 panic("invalid type in get_type_size");
1225 unsigned get_type_alignment(type_t *type)
1227 switch (type->kind) {
1231 case TYPE_IMAGINARY:
1234 return get_atomic_type_alignment(type->atomic.akind);
1235 case TYPE_COMPOUND_UNION:
1236 layout_union_type(&type->compound);
1237 return type->compound.compound->alignment;
1238 case TYPE_COMPOUND_STRUCT:
1239 layout_struct_type(&type->compound);
1240 return type->compound.compound->alignment;
1242 /* gcc says 1 here... */
1244 case TYPE_REFERENCE:
1246 return pointer_properties.alignment;
1248 return get_type_alignment(type->array.element_type);
1249 case TYPE_TYPEDEF: {
1250 il_alignment_t alignment
1251 = get_type_alignment(type->typedeft.typedefe->type);
1252 if (type->typedeft.typedefe->alignment > alignment)
1253 alignment = type->typedeft.typedefe->alignment;
1258 if (type->typeoft.typeof_type) {
1259 return get_type_alignment(type->typeoft.typeof_type);
1261 return get_type_alignment(type->typeoft.expression->base.type);
1264 panic("invalid type in get_type_alignment");
1268 * get alignment of a type when used inside a compound.
1269 * Some ABIs are broken and alignment inside a compound is different from
1270 * recommended alignment of a type
1272 static unsigned get_type_alignment_compound(type_t *const type)
1274 assert(!is_typeref(type));
1275 if (type->kind == TYPE_ATOMIC)
1276 return atomic_type_properties[type->atomic.akind].struct_alignment;
1277 return get_type_alignment(type);
1280 decl_modifiers_t get_type_modifiers(const type_t *type)
1282 switch(type->kind) {
1285 case TYPE_COMPOUND_STRUCT:
1286 case TYPE_COMPOUND_UNION:
1287 return type->compound.compound->modifiers;
1289 return type->function.modifiers;
1293 case TYPE_IMAGINARY:
1294 case TYPE_REFERENCE:
1298 case TYPE_TYPEDEF: {
1299 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1300 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1304 if (type->typeoft.typeof_type) {
1305 return get_type_modifiers(type->typeoft.typeof_type);
1307 return get_type_modifiers(type->typeoft.expression->base.type);
1310 panic("invalid type found in get_type_modifiers");
1313 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1315 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1318 switch (type->base.kind) {
1320 return TYPE_QUALIFIER_NONE;
1322 qualifiers |= type->base.qualifiers;
1323 const typedef_type_t *typedef_type = &type->typedeft;
1324 if (typedef_type->resolved_type != NULL)
1325 type = typedef_type->resolved_type;
1327 type = typedef_type->typedefe->type;
1330 type = type->typeoft.typeof_type;
1333 if (skip_array_type) {
1334 type = type->array.element_type;
1343 return type->base.qualifiers | qualifiers;
1346 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1348 assert(kind <= ATOMIC_TYPE_LAST);
1349 return atomic_type_properties[kind].size;
1352 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1354 assert(kind <= ATOMIC_TYPE_LAST);
1355 return atomic_type_properties[kind].alignment;
1358 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1360 assert(kind <= ATOMIC_TYPE_LAST);
1361 return atomic_type_properties[kind].flags;
1365 * Find the atomic type kind representing a given size (signed).
1367 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1369 static atomic_type_kind_t kinds[32];
1372 atomic_type_kind_t kind = kinds[size];
1373 if (kind == ATOMIC_TYPE_INVALID) {
1374 static const atomic_type_kind_t possible_kinds[] = {
1379 ATOMIC_TYPE_LONGLONG
1381 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1382 if (get_atomic_type_size(possible_kinds[i]) == size) {
1383 kind = possible_kinds[i];
1393 * Find the atomic type kind representing a given size (signed).
1395 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1397 static atomic_type_kind_t kinds[32];
1400 atomic_type_kind_t kind = kinds[size];
1401 if (kind == ATOMIC_TYPE_INVALID) {
1402 static const atomic_type_kind_t possible_kinds[] = {
1407 ATOMIC_TYPE_ULONGLONG
1409 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1410 if (get_atomic_type_size(possible_kinds[i]) == size) {
1411 kind = possible_kinds[i];
1421 * Hash the given type and return the "singleton" version
1424 type_t *identify_new_type(type_t *type)
1426 type_t *result = typehash_insert(type);
1427 if (result != type) {
1428 obstack_free(&type_obst, type);
1434 * Creates a new atomic type.
1436 * @param akind The kind of the atomic type.
1437 * @param qualifiers Type qualifiers for the new type.
1439 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1441 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1442 type->base.qualifiers = qualifiers;
1443 type->atomic.akind = akind;
1445 return identify_new_type(type);
1449 * Creates a new complex type.
1451 * @param akind The kind of the atomic type.
1452 * @param qualifiers Type qualifiers for the new type.
1454 type_t *make_complex_type(atomic_type_kind_t akind,
1455 type_qualifiers_t qualifiers)
1457 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1458 type->base.qualifiers = qualifiers;
1459 type->atomic.akind = akind;
1461 return identify_new_type(type);
1465 * Creates a new imaginary type.
1467 * @param akind The kind of the atomic type.
1468 * @param qualifiers Type qualifiers for the new type.
1470 type_t *make_imaginary_type(atomic_type_kind_t akind,
1471 type_qualifiers_t qualifiers)
1473 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1474 type->base.qualifiers = qualifiers;
1475 type->atomic.akind = akind;
1477 return identify_new_type(type);
1481 * Creates a new pointer type.
1483 * @param points_to The points-to type for the new type.
1484 * @param qualifiers Type qualifiers for the new type.
1486 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1488 type_t *const type = allocate_type_zero(TYPE_POINTER);
1489 type->base.qualifiers = qualifiers;
1490 type->pointer.points_to = points_to;
1491 type->pointer.base_variable = NULL;
1493 return identify_new_type(type);
1497 * Creates a new reference type.
1499 * @param refers_to The referred-to type for the new type.
1501 type_t *make_reference_type(type_t *refers_to)
1503 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1504 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1505 type->reference.refers_to = refers_to;
1507 return identify_new_type(type);
1511 * Creates a new based pointer type.
1513 * @param points_to The points-to type for the new type.
1514 * @param qualifiers Type qualifiers for the new type.
1515 * @param variable The based variable
1517 type_t *make_based_pointer_type(type_t *points_to,
1518 type_qualifiers_t qualifiers, variable_t *variable)
1520 type_t *const type = allocate_type_zero(TYPE_POINTER);
1521 type->base.qualifiers = qualifiers;
1522 type->pointer.points_to = points_to;
1523 type->pointer.base_variable = variable;
1525 return identify_new_type(type);
1529 type_t *make_array_type(type_t *element_type, size_t size,
1530 type_qualifiers_t qualifiers)
1532 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1533 type->base.qualifiers = qualifiers;
1534 type->array.element_type = element_type;
1535 type->array.size = size;
1536 type->array.size_constant = true;
1538 return identify_new_type(type);
1541 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1542 il_alignment_t *struct_alignment,
1543 bool packed, entity_t *first)
1545 il_size_t offset = *struct_offset;
1546 il_alignment_t alignment = *struct_alignment;
1547 size_t bit_offset = 0;
1550 for (member = first; member != NULL; member = member->base.next) {
1551 if (member->kind != ENTITY_COMPOUND_MEMBER)
1553 if (!member->compound_member.bitfield)
1556 type_t *const base_type = skip_typeref(member->declaration.type);
1557 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1558 il_alignment_t alignment_mask = base_alignment-1;
1559 if (base_alignment > alignment)
1560 alignment = base_alignment;
1562 size_t bit_size = member->compound_member.bit_size;
1564 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1565 offset &= ~alignment_mask;
1566 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1568 if (bit_offset + bit_size > base_size || bit_size == 0) {
1569 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1570 offset = (offset + base_alignment-1) & ~alignment_mask;
1575 if (byte_order_big_endian) {
1576 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1577 member->compound_member.offset = offset & ~alignment_mask;
1578 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1580 member->compound_member.offset = offset;
1581 member->compound_member.bit_offset = bit_offset;
1584 bit_offset += bit_size;
1585 offset += bit_offset / BITS_PER_BYTE;
1586 bit_offset %= BITS_PER_BYTE;
1592 *struct_offset = offset;
1593 *struct_alignment = alignment;
1597 void layout_struct_type(compound_type_t *type)
1599 assert(type->compound != NULL);
1601 compound_t *compound = type->compound;
1602 if (!compound->complete)
1604 if (type->compound->layouted)
1607 il_size_t offset = 0;
1608 il_alignment_t alignment = compound->alignment;
1609 bool need_pad = false;
1611 entity_t *entry = compound->members.entities;
1612 while (entry != NULL) {
1613 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1614 entry = entry->base.next;
1618 type_t *const m_type = skip_typeref(entry->declaration.type);
1619 if (!is_type_valid(m_type)) {
1620 entry = entry->base.next;
1624 if (entry->compound_member.bitfield) {
1625 entry = pack_bitfield_members(&offset, &alignment,
1626 compound->packed, entry);
1630 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1631 if (m_alignment > alignment)
1632 alignment = m_alignment;
1634 if (!compound->packed) {
1635 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1637 if (new_offset > offset) {
1639 offset = new_offset;
1643 entry->compound_member.offset = offset;
1644 offset += get_type_size(m_type);
1646 entry = entry->base.next;
1649 if (!compound->packed) {
1650 il_size_t new_offset = (offset + alignment-1) & -alignment;
1651 if (new_offset > offset) {
1653 offset = new_offset;
1657 source_position_t const *const pos = &compound->base.source_position;
1659 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1660 } else if (compound->packed) {
1661 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1664 compound->size = offset;
1665 compound->alignment = alignment;
1666 compound->layouted = true;
1669 void layout_union_type(compound_type_t *type)
1671 assert(type->compound != NULL);
1673 compound_t *compound = type->compound;
1674 if (! compound->complete)
1678 il_alignment_t alignment = compound->alignment;
1680 entity_t *entry = compound->members.entities;
1681 for (; entry != NULL; entry = entry->base.next) {
1682 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1685 type_t *m_type = skip_typeref(entry->declaration.type);
1686 if (! is_type_valid(skip_typeref(m_type)))
1689 entry->compound_member.offset = 0;
1690 il_size_t m_size = get_type_size(m_type);
1693 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1694 if (m_alignment > alignment)
1695 alignment = m_alignment;
1697 size = (size + alignment - 1) & -alignment;
1699 compound->size = size;
1700 compound->alignment = alignment;
1703 function_parameter_t *allocate_parameter(type_t *const type)
1705 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1706 memset(param, 0, sizeof(*param));
1711 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1712 type_t *argument_type2, decl_modifiers_t modifiers)
1714 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1715 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1716 parameter1->next = parameter2;
1718 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1719 type->function.return_type = return_type;
1720 type->function.parameters = parameter1;
1721 type->function.modifiers |= modifiers;
1722 type->function.linkage = LINKAGE_C;
1724 return identify_new_type(type);
1727 type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
1728 decl_modifiers_t modifiers)
1730 function_parameter_t *const parameter = allocate_parameter(argument_type);
1732 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1733 type->function.return_type = return_type;
1734 type->function.parameters = parameter;
1735 type->function.modifiers |= modifiers;
1736 type->function.linkage = LINKAGE_C;
1738 return identify_new_type(type);
1741 type_t *make_function_1_type_variadic(type_t *return_type,
1742 type_t *argument_type,
1743 decl_modifiers_t modifiers)
1745 function_parameter_t *const parameter = allocate_parameter(argument_type);
1747 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1748 type->function.return_type = return_type;
1749 type->function.parameters = parameter;
1750 type->function.variadic = true;
1751 type->function.modifiers |= modifiers;
1752 type->function.linkage = LINKAGE_C;
1754 return identify_new_type(type);
1757 type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
1759 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1760 type->function.return_type = return_type;
1761 type->function.parameters = NULL;
1762 type->function.modifiers |= modifiers;
1763 type->function.linkage = LINKAGE_C;
1765 return identify_new_type(type);
1768 type_t *make_function_type(type_t *return_type, int n_types,
1769 type_t *const *argument_types,
1770 decl_modifiers_t modifiers)
1772 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1773 type->function.return_type = return_type;
1774 type->function.modifiers |= modifiers;
1775 type->function.linkage = LINKAGE_C;
1777 function_parameter_t **anchor = &type->function.parameters;
1778 for (int i = 0; i < n_types; ++i) {
1779 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1780 *anchor = parameter;
1781 anchor = ¶meter->next;
1784 return identify_new_type(type);
1788 * Debug helper. Prints the given type to stdout.
1790 static __attribute__((unused))
1791 void dbg_type(const type_t *type)
1793 print_to_file(stderr);