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_COMPLEX] = sizeof(complex_type_t),
56 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
57 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
58 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
59 [TYPE_ENUM] = sizeof(enum_type_t),
60 [TYPE_FUNCTION] = sizeof(function_type_t),
61 [TYPE_POINTER] = sizeof(pointer_type_t),
62 [TYPE_REFERENCE] = sizeof(reference_type_t),
63 [TYPE_ARRAY] = sizeof(array_type_t),
64 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
65 [TYPE_TYPEOF] = sizeof(typeof_type_t),
67 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
68 assert(kind <= TYPE_TYPEOF);
69 assert(sizes[kind] != 0);
73 type_t *allocate_type_zero(type_kind_t kind)
75 size_t const size = get_type_struct_size(kind);
76 type_t *const res = obstack_alloc(&type_obst, size);
78 res->base.kind = kind;
84 * Properties of atomic types.
86 atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
87 [ATOMIC_TYPE_VOID] = {
90 .flags = ATOMIC_TYPE_FLAG_NONE
92 [ATOMIC_TYPE_WCHAR_T] = {
94 .alignment = (unsigned)-1,
95 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
97 [ATOMIC_TYPE_CHAR] = {
100 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
102 [ATOMIC_TYPE_SCHAR] = {
105 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
106 | ATOMIC_TYPE_FLAG_SIGNED,
108 [ATOMIC_TYPE_UCHAR] = {
111 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
113 [ATOMIC_TYPE_SHORT] = {
116 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
117 | ATOMIC_TYPE_FLAG_SIGNED
119 [ATOMIC_TYPE_USHORT] = {
122 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
124 [ATOMIC_TYPE_INT] = {
125 .size = (unsigned) -1,
126 .alignment = (unsigned) -1,
127 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
128 | ATOMIC_TYPE_FLAG_SIGNED,
130 [ATOMIC_TYPE_UINT] = {
131 .size = (unsigned) -1,
132 .alignment = (unsigned) -1,
133 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
135 [ATOMIC_TYPE_LONG] = {
136 .size = (unsigned) -1,
137 .alignment = (unsigned) -1,
138 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
139 | ATOMIC_TYPE_FLAG_SIGNED,
141 [ATOMIC_TYPE_ULONG] = {
142 .size = (unsigned) -1,
143 .alignment = (unsigned) -1,
144 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
146 [ATOMIC_TYPE_BOOL] = {
149 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
151 [ATOMIC_TYPE_FLOAT] = {
154 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
155 | ATOMIC_TYPE_FLAG_SIGNED,
157 [ATOMIC_TYPE_DOUBLE] = {
160 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
161 | ATOMIC_TYPE_FLAG_SIGNED,
164 atomic_type_properties_t pointer_properties = {
167 .flags = ATOMIC_TYPE_FLAG_NONE,
170 static inline bool is_po2(unsigned x)
172 return (x & (x-1)) == 0;
175 void init_types(unsigned machine_size)
177 obstack_init(&type_obst);
179 atomic_type_properties_t *props = atomic_type_properties;
181 /* atempt to set some sane defaults based on machine size */
183 unsigned int_size = machine_size < 32 ? 2 : 4;
184 unsigned long_size = machine_size < 64 ? 4 : 8;
186 props[ATOMIC_TYPE_INT].size = int_size;
187 props[ATOMIC_TYPE_INT].alignment = int_size;
188 props[ATOMIC_TYPE_UINT].size = int_size;
189 props[ATOMIC_TYPE_UINT].alignment = int_size;
190 props[ATOMIC_TYPE_LONG].size = long_size;
191 props[ATOMIC_TYPE_LONG].alignment = long_size;
192 props[ATOMIC_TYPE_ULONG].size = long_size;
193 props[ATOMIC_TYPE_ULONG].alignment = long_size;
195 pointer_properties.size = long_size;
196 pointer_properties.alignment = long_size;
197 pointer_properties.struct_alignment = long_size;
199 props[ATOMIC_TYPE_LONGLONG] = props[ATOMIC_TYPE_LONG];
200 props[ATOMIC_TYPE_ULONGLONG] = props[ATOMIC_TYPE_ULONG];
201 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
202 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
204 /* set struct alignments to the same value as alignment */
206 i < sizeof(atomic_type_properties)/sizeof(atomic_type_properties[0]);
208 props[i].struct_alignment = props[i].alignment;
212 void exit_types(void)
214 obstack_free(&type_obst, NULL);
217 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
219 size_t sep = q & QUAL_SEP_START ? 0 : 1;
220 if (qualifiers & TYPE_QUALIFIER_CONST) {
221 print_string(" const" + sep);
224 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
225 print_string(" volatile" + sep);
228 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
229 print_string(" restrict" + sep);
232 if (sep == 0 && q & QUAL_SEP_END)
236 const char *get_atomic_kind_name(atomic_type_kind_t kind)
239 case ATOMIC_TYPE_INVALID: break;
240 case ATOMIC_TYPE_VOID: return "void";
241 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
242 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
243 case ATOMIC_TYPE_CHAR: return "char";
244 case ATOMIC_TYPE_SCHAR: return "signed char";
245 case ATOMIC_TYPE_UCHAR: return "unsigned char";
246 case ATOMIC_TYPE_INT: return "int";
247 case ATOMIC_TYPE_UINT: return "unsigned int";
248 case ATOMIC_TYPE_SHORT: return "short";
249 case ATOMIC_TYPE_USHORT: return "unsigned short";
250 case ATOMIC_TYPE_LONG: return "long";
251 case ATOMIC_TYPE_ULONG: return "unsigned long";
252 case ATOMIC_TYPE_LONGLONG: return "long long";
253 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
254 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
255 case ATOMIC_TYPE_FLOAT: return "float";
256 case ATOMIC_TYPE_DOUBLE: return "double";
258 return "INVALIDATOMIC";
262 * Prints the name of an atomic type kinds.
264 * @param kind The type kind.
266 static void print_atomic_kinds(atomic_type_kind_t kind)
268 const char *s = get_atomic_kind_name(kind);
273 * Prints the name of an atomic type.
275 * @param type The type.
277 static void print_atomic_type(const atomic_type_t *type)
279 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
280 print_atomic_kinds(type->akind);
284 * Prints the name of a complex type.
286 * @param type The type.
288 static void print_complex_type(const complex_type_t *type)
290 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
291 print_string("_Complex");
292 print_atomic_kinds(type->akind);
296 * Prints the name of an imaginary type.
298 * @param type The type.
300 static void print_imaginary_type(const imaginary_type_t *type)
302 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
303 print_string("_Imaginary ");
304 print_atomic_kinds(type->akind);
308 * Print the first part (the prefix) of a type.
310 * @param type The type to print.
312 static void print_function_type_pre(const function_type_t *type)
314 switch (type->linkage) {
315 case LINKAGE_INVALID:
320 print_string("extern \"C\" ");
324 if (!(c_mode & _CXX))
325 print_string("extern \"C++\" ");
329 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
331 intern_print_type_pre(type->return_type);
333 cc_kind_t cc = type->calling_convention;
336 case CC_CDECL: print_string(" __cdecl"); break;
337 case CC_STDCALL: print_string(" __stdcall"); break;
338 case CC_FASTCALL: print_string(" __fastcall"); break;
339 case CC_THISCALL: print_string(" __thiscall"); break;
341 if (default_calling_convention != CC_CDECL) {
342 /* show the default calling convention if its not cdecl */
343 cc = default_calling_convention;
351 * Print the second part (the postfix) of a type.
353 * @param type The type to print.
355 static void print_function_type_post(const function_type_t *type,
356 const scope_t *parameters)
360 if (parameters == NULL) {
361 function_parameter_t *parameter = type->parameters;
362 for( ; parameter != NULL; parameter = parameter->next) {
368 print_type(parameter->type);
371 entity_t *parameter = parameters->entities;
372 for (; parameter != NULL; parameter = parameter->base.next) {
373 if (parameter->kind != ENTITY_PARAMETER)
381 const type_t *const param_type = parameter->declaration.type;
382 if (param_type == NULL) {
383 print_string(parameter->base.symbol->string);
385 print_type_ext(param_type, parameter->base.symbol, NULL);
389 if (type->variadic) {
397 if (first && !type->unspecified_parameters) {
398 print_string("void");
402 intern_print_type_post(type->return_type);
406 * Prints the prefix part of a pointer type.
408 * @param type The pointer type.
410 static void print_pointer_type_pre(const pointer_type_t *type)
412 type_t const *const points_to = type->points_to;
413 intern_print_type_pre(points_to);
414 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
416 variable_t *const variable = type->base_variable;
417 if (variable != NULL) {
418 print_string(" __based(");
419 print_string(variable->base.base.symbol->string);
423 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
427 * Prints the postfix part of a pointer type.
429 * @param type The pointer type.
431 static void print_pointer_type_post(const pointer_type_t *type)
433 type_t const *const points_to = type->points_to;
434 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
436 intern_print_type_post(points_to);
440 * Prints the prefix part of a reference type.
442 * @param type The reference type.
444 static void print_reference_type_pre(const reference_type_t *type)
446 type_t const *const refers_to = type->refers_to;
447 intern_print_type_pre(refers_to);
448 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
454 * Prints the postfix part of a reference type.
456 * @param type The reference type.
458 static void print_reference_type_post(const reference_type_t *type)
460 type_t const *const refers_to = type->refers_to;
461 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
463 intern_print_type_post(refers_to);
467 * Prints the prefix part of an array type.
469 * @param type The array type.
471 static void print_array_type_pre(const array_type_t *type)
473 intern_print_type_pre(type->element_type);
477 * Prints the postfix part of an array type.
479 * @param type The array type.
481 static void print_array_type_post(const array_type_t *type)
484 if (type->is_static) {
485 print_string("static ");
487 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
488 if (type->size_expression != NULL
489 && (print_implicit_array_size || !type->has_implicit_size)) {
490 print_expression(type->size_expression);
493 intern_print_type_post(type->element_type);
497 * Prints an enum definition.
499 * @param declaration The enum's type declaration.
501 void print_enum_definition(const enum_t *enume)
507 entity_t *entry = enume->base.next;
508 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
509 entry = entry->base.next) {
512 print_string(entry->base.symbol->string);
513 if (entry->enum_value.value != NULL) {
516 /* skip the implicit cast */
517 expression_t *expression = entry->enum_value.value;
518 print_expression(expression);
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.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);
548 * Print the compound part of a compound type.
550 void print_compound_definition(const compound_t *compound)
555 entity_t *entity = compound->members.entities;
556 for( ; entity != NULL; entity = entity->base.next) {
557 if (entity->kind != ENTITY_COMPOUND_MEMBER)
561 print_entity(entity);
568 if (compound->modifiers & DM_TRANSPARENT_UNION) {
569 print_string("__attribute__((__transparent_union__))");
574 * Prints a compound type.
576 * @param type The compound type.
578 static void print_compound_type(const compound_type_t *type)
580 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
582 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
583 print_string("struct ");
585 assert(type->base.kind == TYPE_COMPOUND_UNION);
586 print_string("union ");
589 compound_t *compound = type->compound;
590 symbol_t *symbol = compound->base.symbol;
591 if (symbol != NULL) {
592 print_string(symbol->string);
594 print_compound_definition(compound);
599 * Prints the prefix part of a typedef type.
601 * @param type The typedef type.
603 static void print_typedef_type_pre(const typedef_type_t *const type)
605 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
606 print_string(type->typedefe->base.symbol->string);
610 * Prints the prefix part of a typeof type.
612 * @param type The typeof type.
614 static void print_typeof_type_pre(const typeof_type_t *const type)
616 print_string("typeof(");
617 if (type->expression != NULL) {
618 print_expression(type->expression);
620 print_type(type->typeof_type);
626 * Prints the prefix part of a type.
628 * @param type The type.
630 static void intern_print_type_pre(const type_t *const type)
634 print_string("<error>");
637 print_type_enum(&type->enumt);
640 print_atomic_type(&type->atomic);
643 print_complex_type(&type->complex);
646 print_imaginary_type(&type->imaginary);
648 case TYPE_COMPOUND_STRUCT:
649 case TYPE_COMPOUND_UNION:
650 print_compound_type(&type->compound);
653 print_function_type_pre(&type->function);
656 print_pointer_type_pre(&type->pointer);
659 print_reference_type_pre(&type->reference);
662 print_array_type_pre(&type->array);
665 print_typedef_type_pre(&type->typedeft);
668 print_typeof_type_pre(&type->typeoft);
671 print_string("unknown");
675 * Prints the postfix part of a type.
677 * @param type The type.
679 static void intern_print_type_post(const type_t *const type)
683 print_function_type_post(&type->function, NULL);
686 print_pointer_type_post(&type->pointer);
689 print_reference_type_post(&type->reference);
692 print_array_type_post(&type->array);
699 case TYPE_COMPOUND_STRUCT:
700 case TYPE_COMPOUND_UNION:
710 * @param type The type.
712 void print_type(const type_t *const type)
714 print_type_ext(type, NULL, NULL);
717 void print_type_ext(const type_t *const type, const symbol_t *symbol,
718 const scope_t *parameters)
720 intern_print_type_pre(type);
721 if (symbol != NULL) {
723 print_string(symbol->string);
725 if (type->kind == TYPE_FUNCTION) {
726 print_function_type_post(&type->function, parameters);
728 intern_print_type_post(type);
735 * @param type The type to copy.
736 * @return A copy of the type.
738 * @note This does not produce a deep copy!
740 type_t *duplicate_type(const type_t *type)
742 size_t size = get_type_struct_size(type->kind);
744 type_t *const copy = obstack_alloc(&type_obst, size);
745 memcpy(copy, type, size);
746 copy->base.firm_type = NULL;
752 * Returns the unqualified type of a given type.
754 * @param type The type.
755 * @returns The unqualified type.
757 type_t *get_unqualified_type(type_t *type)
759 assert(!is_typeref(type));
761 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
764 type_t *unqualified_type = duplicate_type(type);
765 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
767 return identify_new_type(unqualified_type);
770 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
772 type_t *type = skip_typeref(orig_type);
775 if (is_type_array(type)) {
776 /* For array types the element type has to be adjusted */
777 type_t *element_type = type->array.element_type;
778 type_t *qual_element_type = get_qualified_type(element_type, qual);
780 if (qual_element_type == element_type)
783 copy = duplicate_type(type);
784 copy->array.element_type = qual_element_type;
785 } else if (is_type_valid(type)) {
786 if ((type->base.qualifiers & qual) == (int)qual)
789 copy = duplicate_type(type);
790 copy->base.qualifiers |= qual;
795 return identify_new_type(copy);
798 static bool test_atomic_type_flag(atomic_type_kind_t kind,
799 atomic_type_flag_t flag)
801 assert(kind <= ATOMIC_TYPE_LAST);
802 return (atomic_type_properties[kind].flags & flag) != 0;
806 * Returns true if the given type is an integer type.
808 * @param type The type to check.
809 * @return True if type is an integer type.
811 bool is_type_integer(const type_t *type)
813 assert(!is_typeref(type));
815 if (type->kind == TYPE_ENUM)
817 if (type->kind != TYPE_ATOMIC)
820 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
824 * Returns true if the given type is an enum type.
826 * @param type The type to check.
827 * @return True if type is an enum type.
829 bool is_type_enum(const type_t *type)
831 assert(!is_typeref(type));
832 return type->kind == TYPE_ENUM;
836 * Returns true if the given type is an floating point type.
838 * @param type The type to check.
839 * @return True if type is a floating point type.
841 bool is_type_float(const type_t *type)
843 assert(!is_typeref(type));
845 if (type->kind != TYPE_ATOMIC)
848 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
852 * Returns true if the given type is an complex type.
854 * @param type The type to check.
855 * @return True if type is a complex type.
857 bool is_type_complex(const type_t *type)
859 assert(!is_typeref(type));
861 if (type->kind != TYPE_ATOMIC)
864 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
868 * Returns true if the given type is a signed type.
870 * @param type The type to check.
871 * @return True if type is a signed type.
873 bool is_type_signed(const type_t *type)
875 assert(!is_typeref(type));
877 /* enum types are int for now */
878 if (type->kind == TYPE_ENUM)
880 if (type->kind != TYPE_ATOMIC)
883 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
887 * Returns true if the given type represents an arithmetic type.
889 * @param type The type to check.
890 * @return True if type represents an arithmetic type.
892 bool is_type_arithmetic(const type_t *type)
894 assert(!is_typeref(type));
900 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
902 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
904 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
911 * Returns true if the given type is an integer or float type.
913 * @param type The type to check.
914 * @return True if type is an integer or float type.
916 bool is_type_real(const type_t *type)
919 return is_type_integer(type) || is_type_float(type);
923 * Returns true if the given type represents a scalar type.
925 * @param type The type to check.
926 * @return True if type represents a scalar type.
928 bool is_type_scalar(const type_t *type)
930 assert(!is_typeref(type));
932 if (type->kind == TYPE_POINTER)
935 return is_type_arithmetic(type);
939 * Check if a given type is incomplete.
941 * @param type The type to check.
942 * @return True if the given type is incomplete (ie. just forward).
944 bool is_type_incomplete(const type_t *type)
946 assert(!is_typeref(type));
949 case TYPE_COMPOUND_STRUCT:
950 case TYPE_COMPOUND_UNION: {
951 const compound_type_t *compound_type = &type->compound;
952 return !compound_type->compound->complete;
958 return type->array.size_expression == NULL
959 && !type->array.size_constant;
962 return type->atomic.akind == ATOMIC_TYPE_VOID;
965 return type->complex.akind == ATOMIC_TYPE_VOID;
968 return type->imaginary.akind == ATOMIC_TYPE_VOID;
978 panic("is_type_incomplete called without typerefs skipped");
981 panic("invalid type found");
984 bool is_type_object(const type_t *type)
986 return !is_type_function(type) && !is_type_incomplete(type);
990 * Check if two function types are compatible.
992 static bool function_types_compatible(const function_type_t *func1,
993 const function_type_t *func2)
995 const type_t* const ret1 = skip_typeref(func1->return_type);
996 const type_t* const ret2 = skip_typeref(func2->return_type);
997 if (!types_compatible(ret1, ret2))
1000 if (func1->linkage != func2->linkage)
1003 cc_kind_t cc1 = func1->calling_convention;
1004 if (cc1 == CC_DEFAULT)
1005 cc1 = default_calling_convention;
1006 cc_kind_t cc2 = func2->calling_convention;
1007 if (cc2 == CC_DEFAULT)
1008 cc2 = default_calling_convention;
1013 if (func1->variadic != func2->variadic)
1016 /* can parameters be compared? */
1017 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1018 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1021 /* TODO: handling of unspecified parameters not correct yet */
1023 /* all argument types must be compatible */
1024 function_parameter_t *parameter1 = func1->parameters;
1025 function_parameter_t *parameter2 = func2->parameters;
1026 for ( ; parameter1 != NULL && parameter2 != NULL;
1027 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1028 type_t *parameter1_type = skip_typeref(parameter1->type);
1029 type_t *parameter2_type = skip_typeref(parameter2->type);
1031 parameter1_type = get_unqualified_type(parameter1_type);
1032 parameter2_type = get_unqualified_type(parameter2_type);
1034 if (!types_compatible(parameter1_type, parameter2_type))
1037 /* same number of arguments? */
1038 if (parameter1 != NULL || parameter2 != NULL)
1045 * Check if two array types are compatible.
1047 static bool array_types_compatible(const array_type_t *array1,
1048 const array_type_t *array2)
1050 type_t *element_type1 = skip_typeref(array1->element_type);
1051 type_t *element_type2 = skip_typeref(array2->element_type);
1052 if (!types_compatible(element_type1, element_type2))
1055 if (!array1->size_constant || !array2->size_constant)
1058 return array1->size == array2->size;
1062 * Check if two types are compatible.
1064 bool types_compatible(const type_t *type1, const type_t *type2)
1066 assert(!is_typeref(type1));
1067 assert(!is_typeref(type2));
1069 /* shortcut: the same type is always compatible */
1073 if (!is_type_valid(type1) || !is_type_valid(type2))
1076 if (type1->base.qualifiers != type2->base.qualifiers)
1078 if (type1->kind != type2->kind)
1081 switch (type1->kind) {
1083 return function_types_compatible(&type1->function, &type2->function);
1085 return type1->atomic.akind == type2->atomic.akind;
1087 return type1->complex.akind == type2->complex.akind;
1088 case TYPE_IMAGINARY:
1089 return type1->imaginary.akind == type2->imaginary.akind;
1091 return array_types_compatible(&type1->array, &type2->array);
1093 case TYPE_POINTER: {
1094 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1095 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1096 return types_compatible(to1, to2);
1099 case TYPE_REFERENCE: {
1100 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1101 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1102 return types_compatible(to1, to2);
1105 case TYPE_COMPOUND_STRUCT:
1106 case TYPE_COMPOUND_UNION: {
1110 /* TODO: not implemented */
1114 /* Hmm, the error type should be compatible to all other types */
1118 panic("typerefs not skipped in compatible types?!?");
1125 * Skip all typerefs and return the underlying type.
1127 type_t *skip_typeref(type_t *type)
1129 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1132 switch (type->kind) {
1135 case TYPE_TYPEDEF: {
1136 qualifiers |= type->base.qualifiers;
1138 const typedef_type_t *typedef_type = &type->typedeft;
1139 if (typedef_type->resolved_type != NULL) {
1140 type = typedef_type->resolved_type;
1143 type = typedef_type->typedefe->type;
1147 qualifiers |= type->base.qualifiers;
1148 type = type->typeoft.typeof_type;
1156 if (qualifiers != TYPE_QUALIFIER_NONE) {
1157 type_t *const copy = duplicate_type(type);
1159 /* for const with typedefed array type the element type has to be
1161 if (is_type_array(copy)) {
1162 type_t *element_type = copy->array.element_type;
1163 element_type = duplicate_type(element_type);
1164 element_type->base.qualifiers |= qualifiers;
1165 copy->array.element_type = element_type;
1167 copy->base.qualifiers |= qualifiers;
1170 type = identify_new_type(copy);
1176 unsigned get_type_size(type_t *type)
1178 switch (type->kind) {
1182 return get_atomic_type_size(type->atomic.akind);
1184 return get_atomic_type_size(type->complex.akind) * 2;
1185 case TYPE_IMAGINARY:
1186 return get_atomic_type_size(type->imaginary.akind);
1187 case TYPE_COMPOUND_UNION:
1188 layout_union_type(&type->compound);
1189 return type->compound.compound->size;
1190 case TYPE_COMPOUND_STRUCT:
1191 layout_struct_type(&type->compound);
1192 return type->compound.compound->size;
1194 return get_atomic_type_size(type->enumt.akind);
1196 return 0; /* non-const (but "address-const") */
1197 case TYPE_REFERENCE:
1199 return pointer_properties.size;
1201 /* TODO: correct if element_type is aligned? */
1202 il_size_t element_size = get_type_size(type->array.element_type);
1203 return type->array.size * element_size;
1206 return get_type_size(type->typedeft.typedefe->type);
1208 if (type->typeoft.typeof_type) {
1209 return get_type_size(type->typeoft.typeof_type);
1211 return get_type_size(type->typeoft.expression->base.type);
1214 panic("invalid type in get_type_size");
1217 unsigned get_type_alignment(type_t *type)
1219 switch (type->kind) {
1223 return get_atomic_type_alignment(type->atomic.akind);
1225 return get_atomic_type_alignment(type->complex.akind);
1226 case TYPE_IMAGINARY:
1227 return get_atomic_type_alignment(type->imaginary.akind);
1228 case TYPE_COMPOUND_UNION:
1229 layout_union_type(&type->compound);
1230 return type->compound.compound->alignment;
1231 case TYPE_COMPOUND_STRUCT:
1232 layout_struct_type(&type->compound);
1233 return type->compound.compound->alignment;
1235 return get_atomic_type_alignment(type->enumt.akind);
1237 /* gcc says 1 here... */
1239 case TYPE_REFERENCE:
1241 return pointer_properties.alignment;
1243 return get_type_alignment(type->array.element_type);
1244 case TYPE_TYPEDEF: {
1245 il_alignment_t alignment
1246 = get_type_alignment(type->typedeft.typedefe->type);
1247 if (type->typedeft.typedefe->alignment > alignment)
1248 alignment = type->typedeft.typedefe->alignment;
1253 if (type->typeoft.typeof_type) {
1254 return get_type_alignment(type->typeoft.typeof_type);
1256 return get_type_alignment(type->typeoft.expression->base.type);
1259 panic("invalid type in get_type_alignment");
1262 unsigned get_type_alignment_compound(type_t *type)
1264 if (type->kind == TYPE_ATOMIC)
1265 return atomic_type_properties[type->atomic.akind].struct_alignment;
1266 return get_type_alignment(type);
1269 decl_modifiers_t get_type_modifiers(const type_t *type)
1271 switch(type->kind) {
1274 case TYPE_COMPOUND_STRUCT:
1275 case TYPE_COMPOUND_UNION:
1276 return type->compound.compound->modifiers;
1278 return type->function.modifiers;
1282 case TYPE_IMAGINARY:
1283 case TYPE_REFERENCE:
1287 case TYPE_TYPEDEF: {
1288 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1289 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1293 if (type->typeoft.typeof_type) {
1294 return get_type_modifiers(type->typeoft.typeof_type);
1296 return get_type_modifiers(type->typeoft.expression->base.type);
1299 panic("invalid type found in get_type_modifiers");
1302 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1304 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1307 switch (type->base.kind) {
1309 return TYPE_QUALIFIER_NONE;
1311 qualifiers |= type->base.qualifiers;
1312 const typedef_type_t *typedef_type = &type->typedeft;
1313 if (typedef_type->resolved_type != NULL)
1314 type = typedef_type->resolved_type;
1316 type = typedef_type->typedefe->type;
1319 type = type->typeoft.typeof_type;
1322 if (skip_array_type) {
1323 type = type->array.element_type;
1332 return type->base.qualifiers | qualifiers;
1335 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1337 assert(kind <= ATOMIC_TYPE_LAST);
1338 return atomic_type_properties[kind].size;
1341 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1343 assert(kind <= ATOMIC_TYPE_LAST);
1344 return atomic_type_properties[kind].alignment;
1347 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1349 assert(kind <= ATOMIC_TYPE_LAST);
1350 return atomic_type_properties[kind].flags;
1354 * Find the atomic type kind representing a given size (signed).
1356 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1358 static atomic_type_kind_t kinds[32];
1361 atomic_type_kind_t kind = kinds[size];
1362 if (kind == ATOMIC_TYPE_INVALID) {
1363 static const atomic_type_kind_t possible_kinds[] = {
1368 ATOMIC_TYPE_LONGLONG
1370 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1371 if (get_atomic_type_size(possible_kinds[i]) == size) {
1372 kind = possible_kinds[i];
1382 * Find the atomic type kind representing a given size (signed).
1384 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1386 static atomic_type_kind_t kinds[32];
1389 atomic_type_kind_t kind = kinds[size];
1390 if (kind == ATOMIC_TYPE_INVALID) {
1391 static const atomic_type_kind_t possible_kinds[] = {
1396 ATOMIC_TYPE_ULONGLONG
1398 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1399 if (get_atomic_type_size(possible_kinds[i]) == size) {
1400 kind = possible_kinds[i];
1410 * Hash the given type and return the "singleton" version
1413 type_t *identify_new_type(type_t *type)
1415 type_t *result = typehash_insert(type);
1416 if (result != type) {
1417 obstack_free(&type_obst, type);
1423 * Creates a new atomic type.
1425 * @param akind The kind of the atomic type.
1426 * @param qualifiers Type qualifiers for the new type.
1428 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1430 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1431 type->base.qualifiers = qualifiers;
1432 type->atomic.akind = akind;
1434 return identify_new_type(type);
1438 * Creates a new complex type.
1440 * @param akind The kind of the atomic type.
1441 * @param qualifiers Type qualifiers for the new type.
1443 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1445 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1446 type->base.qualifiers = qualifiers;
1447 type->complex.akind = akind;
1449 return identify_new_type(type);
1453 * Creates a new imaginary type.
1455 * @param akind The kind of the atomic type.
1456 * @param qualifiers Type qualifiers for the new type.
1458 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1460 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1461 type->base.qualifiers = qualifiers;
1462 type->imaginary.akind = akind;
1464 return identify_new_type(type);
1468 * Creates a new pointer type.
1470 * @param points_to The points-to type for the new type.
1471 * @param qualifiers Type qualifiers for the new type.
1473 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1475 type_t *const type = allocate_type_zero(TYPE_POINTER);
1476 type->base.qualifiers = qualifiers;
1477 type->pointer.points_to = points_to;
1478 type->pointer.base_variable = NULL;
1480 return identify_new_type(type);
1484 * Creates a new reference type.
1486 * @param refers_to The referred-to type for the new type.
1488 type_t *make_reference_type(type_t *refers_to)
1490 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1491 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1492 type->reference.refers_to = refers_to;
1494 return identify_new_type(type);
1498 * Creates a new based pointer type.
1500 * @param points_to The points-to type for the new type.
1501 * @param qualifiers Type qualifiers for the new type.
1502 * @param variable The based variable
1504 type_t *make_based_pointer_type(type_t *points_to,
1505 type_qualifiers_t qualifiers, variable_t *variable)
1507 type_t *const type = allocate_type_zero(TYPE_POINTER);
1508 type->base.qualifiers = qualifiers;
1509 type->pointer.points_to = points_to;
1510 type->pointer.base_variable = variable;
1512 return identify_new_type(type);
1516 type_t *make_array_type(type_t *element_type, size_t size,
1517 type_qualifiers_t qualifiers)
1519 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1520 type->base.qualifiers = qualifiers;
1521 type->array.element_type = element_type;
1522 type->array.size = size;
1523 type->array.size_constant = true;
1525 return identify_new_type(type);
1528 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1529 il_alignment_t *struct_alignment,
1530 bool packed, entity_t *first)
1532 il_size_t offset = *struct_offset;
1533 il_alignment_t alignment = *struct_alignment;
1534 size_t bit_offset = 0;
1537 for (member = first; member != NULL; member = member->base.next) {
1538 if (member->kind != ENTITY_COMPOUND_MEMBER)
1540 if (!member->compound_member.bitfield)
1543 type_t *base_type = member->declaration.type;
1544 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1545 il_alignment_t alignment_mask = base_alignment-1;
1546 if (base_alignment > alignment)
1547 alignment = base_alignment;
1549 size_t bit_size = member->compound_member.bit_size;
1551 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1552 offset &= ~alignment_mask;
1553 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1555 if (bit_offset + bit_size > base_size || bit_size == 0) {
1556 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1557 offset = (offset + base_alignment-1) & ~alignment_mask;
1562 if (byte_order_big_endian) {
1563 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1564 member->compound_member.offset = offset & ~alignment_mask;
1565 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1567 member->compound_member.offset = offset;
1568 member->compound_member.bit_offset = bit_offset;
1571 bit_offset += bit_size;
1572 offset += bit_offset / BITS_PER_BYTE;
1573 bit_offset %= BITS_PER_BYTE;
1579 *struct_offset = offset;
1580 *struct_alignment = alignment;
1584 void layout_struct_type(compound_type_t *type)
1586 assert(type->compound != NULL);
1588 compound_t *compound = type->compound;
1589 if (!compound->complete)
1591 if (type->compound->layouted)
1594 il_size_t offset = 0;
1595 il_alignment_t alignment = compound->alignment;
1596 bool need_pad = false;
1598 entity_t *entry = compound->members.entities;
1599 while (entry != NULL) {
1600 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1601 entry = entry->base.next;
1605 type_t *m_type = entry->declaration.type;
1606 type_t *skipped = skip_typeref(m_type);
1607 if (! is_type_valid(skipped)) {
1608 entry = entry->base.next;
1612 if (entry->compound_member.bitfield) {
1613 entry = pack_bitfield_members(&offset, &alignment,
1614 compound->packed, entry);
1618 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1619 if (m_alignment > alignment)
1620 alignment = m_alignment;
1622 if (!compound->packed) {
1623 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1625 if (new_offset > offset) {
1627 offset = new_offset;
1631 entry->compound_member.offset = offset;
1632 offset += get_type_size(m_type);
1634 entry = entry->base.next;
1637 if (!compound->packed) {
1638 il_size_t new_offset = (offset + alignment-1) & -alignment;
1639 if (new_offset > offset) {
1641 offset = new_offset;
1645 source_position_t const *const pos = &compound->base.source_position;
1647 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1648 } else if (compound->packed) {
1649 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1652 compound->size = offset;
1653 compound->alignment = alignment;
1654 compound->layouted = true;
1657 void layout_union_type(compound_type_t *type)
1659 assert(type->compound != NULL);
1661 compound_t *compound = type->compound;
1662 if (! compound->complete)
1666 il_alignment_t alignment = compound->alignment;
1668 entity_t *entry = compound->members.entities;
1669 for (; entry != NULL; entry = entry->base.next) {
1670 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1673 type_t *m_type = entry->declaration.type;
1674 if (! is_type_valid(skip_typeref(m_type)))
1677 entry->compound_member.offset = 0;
1678 il_size_t m_size = get_type_size(m_type);
1681 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1682 if (m_alignment > alignment)
1683 alignment = m_alignment;
1685 size = (size + alignment - 1) & -alignment;
1687 compound->size = size;
1688 compound->alignment = alignment;
1691 function_parameter_t *allocate_parameter(type_t *const type)
1693 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1694 memset(param, 0, sizeof(*param));
1699 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1700 type_t *argument_type2)
1702 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1703 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1704 parameter1->next = parameter2;
1706 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1707 type->function.return_type = return_type;
1708 type->function.parameters = parameter1;
1709 type->function.linkage = LINKAGE_C;
1711 return identify_new_type(type);
1714 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1716 function_parameter_t *const parameter = allocate_parameter(argument_type);
1718 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1719 type->function.return_type = return_type;
1720 type->function.parameters = parameter;
1721 type->function.linkage = LINKAGE_C;
1723 return identify_new_type(type);
1726 type_t *make_function_1_type_variadic(type_t *return_type,
1727 type_t *argument_type)
1729 function_parameter_t *const parameter = allocate_parameter(argument_type);
1731 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1732 type->function.return_type = return_type;
1733 type->function.parameters = parameter;
1734 type->function.variadic = true;
1735 type->function.linkage = LINKAGE_C;
1737 return identify_new_type(type);
1740 type_t *make_function_0_type(type_t *return_type)
1742 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1743 type->function.return_type = return_type;
1744 type->function.parameters = NULL;
1745 type->function.linkage = LINKAGE_C;
1747 return identify_new_type(type);
1750 type_t *make_function_type(type_t *return_type, int n_types,
1751 type_t *const *argument_types,
1752 decl_modifiers_t modifiers)
1754 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1755 type->function.return_type = return_type;
1756 type->function.modifiers |= modifiers;
1757 type->function.linkage = LINKAGE_C;
1759 function_parameter_t **anchor = &type->function.parameters;
1760 for (int i = 0; i < n_types; ++i) {
1761 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1762 *anchor = parameter;
1763 anchor = ¶meter->next;
1766 return identify_new_type(type);
1770 * Debug helper. Prints the given type to stdout.
1772 static __attribute__((unused))
1773 void dbg_type(const type_t *type)
1775 print_to_file(stderr);