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_FLOAT] = {
158 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
159 | ATOMIC_TYPE_FLAG_SIGNED,
162 [ATOMIC_TYPE_DOUBLE] = {
165 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
166 | ATOMIC_TYPE_FLAG_SIGNED,
169 [ATOMIC_TYPE_WCHAR_T] = {
170 .size = (unsigned)-1,
171 .alignment = (unsigned)-1,
172 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
173 .rank = (unsigned)-1,
176 atomic_type_properties_t pointer_properties = {
179 .flags = ATOMIC_TYPE_FLAG_NONE,
182 static inline bool is_po2(unsigned x)
184 return (x & (x-1)) == 0;
187 void init_types(unsigned machine_size)
189 obstack_init(&type_obst);
191 atomic_type_properties_t *props = atomic_type_properties;
193 /* atempt to set some sane defaults based on machine size */
195 unsigned int_size = machine_size < 32 ? 2 : 4;
196 unsigned long_size = machine_size < 64 ? 4 : 8;
198 props[ATOMIC_TYPE_INT].size = int_size;
199 props[ATOMIC_TYPE_INT].alignment = int_size;
200 props[ATOMIC_TYPE_UINT].size = int_size;
201 props[ATOMIC_TYPE_UINT].alignment = int_size;
202 props[ATOMIC_TYPE_LONG].size = long_size;
203 props[ATOMIC_TYPE_LONG].alignment = long_size;
204 props[ATOMIC_TYPE_ULONG].size = long_size;
205 props[ATOMIC_TYPE_ULONG].alignment = long_size;
207 pointer_properties.size = long_size;
208 pointer_properties.alignment = long_size;
209 pointer_properties.struct_alignment = long_size;
211 props[ATOMIC_TYPE_LONGLONG] = props[ATOMIC_TYPE_LONG];
212 props[ATOMIC_TYPE_ULONGLONG] = props[ATOMIC_TYPE_ULONG];
213 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
214 props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
216 /* set struct alignments to the same value as alignment */
218 i < sizeof(atomic_type_properties)/sizeof(atomic_type_properties[0]);
220 props[i].struct_alignment = props[i].alignment;
224 void exit_types(void)
226 obstack_free(&type_obst, NULL);
229 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
231 size_t sep = q & QUAL_SEP_START ? 0 : 1;
232 if (qualifiers & TYPE_QUALIFIER_CONST) {
233 print_string(" const" + sep);
236 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
237 print_string(" volatile" + sep);
240 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
241 print_string(" restrict" + sep);
244 if (sep == 0 && q & QUAL_SEP_END)
248 const char *get_atomic_kind_name(atomic_type_kind_t kind)
251 case ATOMIC_TYPE_INVALID: break;
252 case ATOMIC_TYPE_VOID: return "void";
253 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
254 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
255 case ATOMIC_TYPE_CHAR: return "char";
256 case ATOMIC_TYPE_SCHAR: return "signed char";
257 case ATOMIC_TYPE_UCHAR: return "unsigned char";
258 case ATOMIC_TYPE_INT: return "int";
259 case ATOMIC_TYPE_UINT: return "unsigned int";
260 case ATOMIC_TYPE_SHORT: return "short";
261 case ATOMIC_TYPE_USHORT: return "unsigned short";
262 case ATOMIC_TYPE_LONG: return "long";
263 case ATOMIC_TYPE_ULONG: return "unsigned long";
264 case ATOMIC_TYPE_LONGLONG: return "long long";
265 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
266 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
267 case ATOMIC_TYPE_FLOAT: return "float";
268 case ATOMIC_TYPE_DOUBLE: return "double";
270 return "INVALIDATOMIC";
274 * Prints the name of an atomic type kinds.
276 * @param kind The type kind.
278 static void print_atomic_kinds(atomic_type_kind_t kind)
280 const char *s = get_atomic_kind_name(kind);
285 * Prints the name of an atomic type.
287 * @param type The type.
289 static void print_atomic_type(const atomic_type_t *type)
291 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
292 print_atomic_kinds(type->akind);
296 * Prints the name of a complex type.
298 * @param type The type.
300 static void print_complex_type(const atomic_type_t *type)
302 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
303 print_string("_Complex");
304 print_atomic_kinds(type->akind);
308 * Prints the name of an imaginary type.
310 * @param type The type.
312 static void print_imaginary_type(const atomic_type_t *type)
314 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
315 print_string("_Imaginary ");
316 print_atomic_kinds(type->akind);
320 * Print the first part (the prefix) of a type.
322 * @param type The type to print.
324 static void print_function_type_pre(const function_type_t *type)
326 switch (type->linkage) {
329 print_string("extern \"C\" ");
333 if (!(c_mode & _CXX))
334 print_string("extern \"C++\" ");
338 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
340 intern_print_type_pre(type->return_type);
342 cc_kind_t cc = type->calling_convention;
345 case CC_CDECL: print_string(" __cdecl"); break;
346 case CC_STDCALL: print_string(" __stdcall"); break;
347 case CC_FASTCALL: print_string(" __fastcall"); break;
348 case CC_THISCALL: print_string(" __thiscall"); break;
350 if (default_calling_convention != CC_CDECL) {
351 /* show the default calling convention if its not cdecl */
352 cc = default_calling_convention;
360 * Print the second part (the postfix) of a type.
362 * @param type The type to print.
364 static void print_function_type_post(const function_type_t *type,
365 const scope_t *parameters)
369 if (parameters == NULL) {
370 function_parameter_t *parameter = type->parameters;
371 for( ; parameter != NULL; parameter = parameter->next) {
377 print_type(parameter->type);
380 entity_t *parameter = parameters->entities;
381 for (; parameter != NULL; parameter = parameter->base.next) {
382 if (parameter->kind != ENTITY_PARAMETER)
390 const type_t *const param_type = parameter->declaration.type;
391 if (param_type == NULL) {
392 print_string(parameter->base.symbol->string);
394 print_type_ext(param_type, parameter->base.symbol, NULL);
398 if (type->variadic) {
406 if (first && !type->unspecified_parameters) {
407 print_string("void");
411 intern_print_type_post(type->return_type);
415 * Prints the prefix part of a pointer type.
417 * @param type The pointer type.
419 static void print_pointer_type_pre(const pointer_type_t *type)
421 type_t const *const points_to = type->points_to;
422 intern_print_type_pre(points_to);
423 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
425 variable_t *const variable = type->base_variable;
426 if (variable != NULL) {
427 print_string(" __based(");
428 print_string(variable->base.base.symbol->string);
432 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
436 * Prints the postfix part of a pointer type.
438 * @param type The pointer type.
440 static void print_pointer_type_post(const pointer_type_t *type)
442 type_t const *const points_to = type->points_to;
443 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
445 intern_print_type_post(points_to);
449 * Prints the prefix part of a reference type.
451 * @param type The reference type.
453 static void print_reference_type_pre(const reference_type_t *type)
455 type_t const *const refers_to = type->refers_to;
456 intern_print_type_pre(refers_to);
457 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
463 * Prints the postfix part of a reference type.
465 * @param type The reference type.
467 static void print_reference_type_post(const reference_type_t *type)
469 type_t const *const refers_to = type->refers_to;
470 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
472 intern_print_type_post(refers_to);
476 * Prints the prefix part of an array type.
478 * @param type The array type.
480 static void print_array_type_pre(const array_type_t *type)
482 intern_print_type_pre(type->element_type);
486 * Prints the postfix part of an array type.
488 * @param type The array type.
490 static void print_array_type_post(const array_type_t *type)
493 if (type->is_static) {
494 print_string("static ");
496 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
497 if (type->size_expression != NULL
498 && (print_implicit_array_size || !type->has_implicit_size)) {
499 print_expression(type->size_expression);
502 intern_print_type_post(type->element_type);
506 * Prints an enum definition.
508 * @param declaration The enum's type declaration.
510 void print_enum_definition(const enum_t *enume)
516 entity_t *entry = enume->base.next;
517 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
518 entry = entry->base.next) {
521 print_string(entry->base.symbol->string);
522 if (entry->enum_value.value != NULL) {
525 /* skip the implicit cast */
526 expression_t *expression = entry->enum_value.value;
527 print_expression(expression);
538 * Prints an enum type.
540 * @param type The enum type.
542 static void print_type_enum(const enum_type_t *type)
544 print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
545 print_string("enum ");
547 enum_t *enume = type->enume;
548 symbol_t *symbol = enume->base.symbol;
549 if (symbol != NULL) {
550 print_string(symbol->string);
552 print_enum_definition(enume);
557 * Print the compound part of a compound type.
559 void print_compound_definition(const compound_t *compound)
564 entity_t *entity = compound->members.entities;
565 for( ; entity != NULL; entity = entity->base.next) {
566 if (entity->kind != ENTITY_COMPOUND_MEMBER)
570 print_entity(entity);
577 if (compound->modifiers & DM_TRANSPARENT_UNION) {
578 print_string("__attribute__((__transparent_union__))");
583 * Prints a compound type.
585 * @param type The compound type.
587 static void print_compound_type(const compound_type_t *type)
589 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
591 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
592 print_string("struct ");
594 assert(type->base.kind == TYPE_COMPOUND_UNION);
595 print_string("union ");
598 compound_t *compound = type->compound;
599 symbol_t *symbol = compound->base.symbol;
600 if (symbol != NULL) {
601 print_string(symbol->string);
603 print_compound_definition(compound);
608 * Prints the prefix part of a typedef type.
610 * @param type The typedef type.
612 static void print_typedef_type_pre(const typedef_type_t *const type)
614 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
615 print_string(type->typedefe->base.symbol->string);
619 * Prints the prefix part of a typeof type.
621 * @param type The typeof type.
623 static void print_typeof_type_pre(const typeof_type_t *const type)
625 print_string("typeof(");
626 if (type->expression != NULL) {
627 print_expression(type->expression);
629 print_type(type->typeof_type);
635 * Prints the prefix part of a type.
637 * @param type The type.
639 static void intern_print_type_pre(const type_t *const type)
643 print_string("<error>");
646 print_type_enum(&type->enumt);
649 print_atomic_type(&type->atomic);
652 print_complex_type(&type->atomic);
655 print_imaginary_type(&type->atomic);
657 case TYPE_COMPOUND_STRUCT:
658 case TYPE_COMPOUND_UNION:
659 print_compound_type(&type->compound);
662 print_function_type_pre(&type->function);
665 print_pointer_type_pre(&type->pointer);
668 print_reference_type_pre(&type->reference);
671 print_array_type_pre(&type->array);
674 print_typedef_type_pre(&type->typedeft);
677 print_typeof_type_pre(&type->typeoft);
680 print_string("unknown");
684 * Prints the postfix part of a type.
686 * @param type The type.
688 static void intern_print_type_post(const type_t *const type)
692 print_function_type_post(&type->function, NULL);
695 print_pointer_type_post(&type->pointer);
698 print_reference_type_post(&type->reference);
701 print_array_type_post(&type->array);
708 case TYPE_COMPOUND_STRUCT:
709 case TYPE_COMPOUND_UNION:
719 * @param type The type.
721 void print_type(const type_t *const type)
723 print_type_ext(type, NULL, NULL);
726 void print_type_ext(const type_t *const type, const symbol_t *symbol,
727 const scope_t *parameters)
729 intern_print_type_pre(type);
730 if (symbol != NULL) {
732 print_string(symbol->string);
734 if (type->kind == TYPE_FUNCTION) {
735 print_function_type_post(&type->function, parameters);
737 intern_print_type_post(type);
744 * @param type The type to copy.
745 * @return A copy of the type.
747 * @note This does not produce a deep copy!
749 type_t *duplicate_type(const type_t *type)
751 size_t size = get_type_struct_size(type->kind);
753 type_t *const copy = obstack_alloc(&type_obst, size);
754 memcpy(copy, type, size);
755 copy->base.firm_type = NULL;
761 * Returns the unqualified type of a given type.
763 * @param type The type.
764 * @returns The unqualified type.
766 type_t *get_unqualified_type(type_t *type)
768 assert(!is_typeref(type));
770 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
773 type_t *unqualified_type = duplicate_type(type);
774 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
776 return identify_new_type(unqualified_type);
779 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
781 type_t *type = skip_typeref(orig_type);
784 if (is_type_array(type)) {
785 /* For array types the element type has to be adjusted */
786 type_t *element_type = type->array.element_type;
787 type_t *qual_element_type = get_qualified_type(element_type, qual);
789 if (qual_element_type == element_type)
792 copy = duplicate_type(type);
793 copy->array.element_type = qual_element_type;
794 } else if (is_type_valid(type)) {
795 if ((type->base.qualifiers & qual) == (int)qual)
798 copy = duplicate_type(type);
799 copy->base.qualifiers |= qual;
804 return identify_new_type(copy);
807 static bool test_atomic_type_flag(atomic_type_kind_t kind,
808 atomic_type_flag_t flag)
810 assert(kind <= ATOMIC_TYPE_LAST);
811 return (atomic_type_properties[kind].flags & flag) != 0;
815 * Returns true if the given type is an integer type.
817 * @param type The type to check.
818 * @return True if type is an integer type.
820 bool is_type_integer(const type_t *type)
822 assert(!is_typeref(type));
824 if (type->kind == TYPE_ENUM)
826 if (type->kind != TYPE_ATOMIC)
829 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
833 * Returns true if the given type is an enum type.
835 * @param type The type to check.
836 * @return True if type is an enum type.
838 bool is_type_enum(const type_t *type)
840 assert(!is_typeref(type));
841 return type->kind == TYPE_ENUM;
845 * Returns true if the given type is an floating point type.
847 * @param type The type to check.
848 * @return True if type is a floating point type.
850 bool is_type_float(const type_t *type)
852 assert(!is_typeref(type));
854 if (type->kind != TYPE_ATOMIC)
857 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
861 * Returns true if the given type is an complex type.
863 * @param type The type to check.
864 * @return True if type is a complex type.
866 bool is_type_complex(const type_t *type)
868 assert(!is_typeref(type));
870 if (type->kind != TYPE_ATOMIC)
873 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
877 * Returns true if the given type is a signed type.
879 * @param type The type to check.
880 * @return True if type is a signed type.
882 bool is_type_signed(const type_t *type)
884 assert(!is_typeref(type));
886 /* enum types are int for now */
887 if (type->kind == TYPE_ENUM)
889 if (type->kind != TYPE_ATOMIC)
892 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
896 * Returns true if the given type represents an arithmetic type.
898 * @param type The type to check.
899 * @return True if type represents an arithmetic type.
901 bool is_type_arithmetic(const type_t *type)
903 assert(!is_typeref(type));
911 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
918 * Returns true if the given type is an integer or float type.
920 * @param type The type to check.
921 * @return True if type is an integer or float type.
923 bool is_type_real(const type_t *type)
926 return is_type_integer(type) || is_type_float(type);
930 * Returns true if the given type represents a scalar type.
932 * @param type The type to check.
933 * @return True if type represents a scalar type.
935 bool is_type_scalar(const type_t *type)
937 assert(!is_typeref(type));
939 if (type->kind == TYPE_POINTER)
942 return is_type_arithmetic(type);
946 * Check if a given type is incomplete.
948 * @param type The type to check.
949 * @return True if the given type is incomplete (ie. just forward).
951 bool is_type_incomplete(const type_t *type)
953 assert(!is_typeref(type));
956 case TYPE_COMPOUND_STRUCT:
957 case TYPE_COMPOUND_UNION: {
958 const compound_type_t *compound_type = &type->compound;
959 return !compound_type->compound->complete;
965 return type->array.size_expression == NULL
966 && !type->array.size_constant;
971 return type->atomic.akind == ATOMIC_TYPE_VOID;
981 panic("is_type_incomplete called without typerefs skipped");
984 panic("invalid type found");
987 bool is_type_object(const type_t *type)
989 return !is_type_function(type) && !is_type_incomplete(type);
993 * Check if two function types are compatible.
995 static bool function_types_compatible(const function_type_t *func1,
996 const function_type_t *func2)
998 const type_t* const ret1 = skip_typeref(func1->return_type);
999 const type_t* const ret2 = skip_typeref(func2->return_type);
1000 if (!types_compatible(ret1, ret2))
1003 if (func1->linkage != func2->linkage)
1006 cc_kind_t cc1 = func1->calling_convention;
1007 if (cc1 == CC_DEFAULT)
1008 cc1 = default_calling_convention;
1009 cc_kind_t cc2 = func2->calling_convention;
1010 if (cc2 == CC_DEFAULT)
1011 cc2 = default_calling_convention;
1016 if (func1->variadic != func2->variadic)
1019 /* can parameters be compared? */
1020 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1021 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1024 /* TODO: handling of unspecified parameters not correct yet */
1026 /* all argument types must be compatible */
1027 function_parameter_t *parameter1 = func1->parameters;
1028 function_parameter_t *parameter2 = func2->parameters;
1029 for ( ; parameter1 != NULL && parameter2 != NULL;
1030 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1031 type_t *parameter1_type = skip_typeref(parameter1->type);
1032 type_t *parameter2_type = skip_typeref(parameter2->type);
1034 parameter1_type = get_unqualified_type(parameter1_type);
1035 parameter2_type = get_unqualified_type(parameter2_type);
1037 if (!types_compatible(parameter1_type, parameter2_type))
1040 /* same number of arguments? */
1041 if (parameter1 != NULL || parameter2 != NULL)
1048 * Check if two array types are compatible.
1050 static bool array_types_compatible(const array_type_t *array1,
1051 const array_type_t *array2)
1053 type_t *element_type1 = skip_typeref(array1->element_type);
1054 type_t *element_type2 = skip_typeref(array2->element_type);
1055 if (!types_compatible(element_type1, element_type2))
1058 if (!array1->size_constant || !array2->size_constant)
1061 return array1->size == array2->size;
1065 * Check if two types are compatible.
1067 bool types_compatible(const type_t *type1, const type_t *type2)
1069 assert(!is_typeref(type1));
1070 assert(!is_typeref(type2));
1072 /* shortcut: the same type is always compatible */
1076 if (!is_type_valid(type1) || !is_type_valid(type2))
1079 if (type1->base.qualifiers != type2->base.qualifiers)
1081 if (type1->kind != type2->kind)
1084 switch (type1->kind) {
1086 return function_types_compatible(&type1->function, &type2->function);
1088 case TYPE_IMAGINARY:
1090 return type1->atomic.akind == type2->atomic.akind;
1092 return array_types_compatible(&type1->array, &type2->array);
1094 case TYPE_POINTER: {
1095 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1096 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1097 return types_compatible(to1, to2);
1100 case TYPE_REFERENCE: {
1101 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1102 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1103 return types_compatible(to1, to2);
1106 case TYPE_COMPOUND_STRUCT:
1107 case TYPE_COMPOUND_UNION: {
1111 /* TODO: not implemented */
1115 /* Hmm, the error type should be compatible to all other types */
1119 panic("typerefs not skipped in compatible types?!?");
1126 * Skip all typerefs and return the underlying type.
1128 type_t *skip_typeref(type_t *type)
1130 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1133 switch (type->kind) {
1136 case TYPE_TYPEDEF: {
1137 qualifiers |= type->base.qualifiers;
1139 const typedef_type_t *typedef_type = &type->typedeft;
1140 if (typedef_type->resolved_type != NULL) {
1141 type = typedef_type->resolved_type;
1144 type = typedef_type->typedefe->type;
1148 qualifiers |= type->base.qualifiers;
1149 type = type->typeoft.typeof_type;
1157 if (qualifiers != TYPE_QUALIFIER_NONE) {
1158 type_t *const copy = duplicate_type(type);
1160 /* for const with typedefed array type the element type has to be
1162 if (is_type_array(copy)) {
1163 type_t *element_type = copy->array.element_type;
1164 element_type = duplicate_type(element_type);
1165 element_type->base.qualifiers |= qualifiers;
1166 copy->array.element_type = element_type;
1168 copy->base.qualifiers |= qualifiers;
1171 type = identify_new_type(copy);
1177 unsigned get_type_size(type_t *type)
1179 switch (type->kind) {
1183 case TYPE_IMAGINARY:
1185 return get_atomic_type_size(type->atomic.akind);
1187 return get_atomic_type_size(type->atomic.akind) * 2;
1188 case TYPE_COMPOUND_UNION:
1189 layout_union_type(&type->compound);
1190 return type->compound.compound->size;
1191 case TYPE_COMPOUND_STRUCT:
1192 layout_struct_type(&type->compound);
1193 return type->compound.compound->size;
1195 return 0; /* non-const (but "address-const") */
1196 case TYPE_REFERENCE:
1198 return pointer_properties.size;
1200 /* TODO: correct if element_type is aligned? */
1201 il_size_t element_size = get_type_size(type->array.element_type);
1202 return type->array.size * element_size;
1205 return get_type_size(type->typedeft.typedefe->type);
1207 if (type->typeoft.typeof_type) {
1208 return get_type_size(type->typeoft.typeof_type);
1210 return get_type_size(type->typeoft.expression->base.type);
1213 panic("invalid type in get_type_size");
1216 unsigned get_type_alignment(type_t *type)
1218 switch (type->kind) {
1222 case TYPE_IMAGINARY:
1225 return get_atomic_type_alignment(type->atomic.akind);
1226 case TYPE_COMPOUND_UNION:
1227 layout_union_type(&type->compound);
1228 return type->compound.compound->alignment;
1229 case TYPE_COMPOUND_STRUCT:
1230 layout_struct_type(&type->compound);
1231 return type->compound.compound->alignment;
1233 /* gcc says 1 here... */
1235 case TYPE_REFERENCE:
1237 return pointer_properties.alignment;
1239 return get_type_alignment(type->array.element_type);
1240 case TYPE_TYPEDEF: {
1241 il_alignment_t alignment
1242 = get_type_alignment(type->typedeft.typedefe->type);
1243 if (type->typedeft.typedefe->alignment > alignment)
1244 alignment = type->typedeft.typedefe->alignment;
1249 if (type->typeoft.typeof_type) {
1250 return get_type_alignment(type->typeoft.typeof_type);
1252 return get_type_alignment(type->typeoft.expression->base.type);
1255 panic("invalid type in get_type_alignment");
1258 unsigned get_type_alignment_compound(type_t *type)
1260 if (type->kind == TYPE_ATOMIC)
1261 return atomic_type_properties[type->atomic.akind].struct_alignment;
1262 return get_type_alignment(type);
1265 decl_modifiers_t get_type_modifiers(const type_t *type)
1267 switch(type->kind) {
1270 case TYPE_COMPOUND_STRUCT:
1271 case TYPE_COMPOUND_UNION:
1272 return type->compound.compound->modifiers;
1274 return type->function.modifiers;
1278 case TYPE_IMAGINARY:
1279 case TYPE_REFERENCE:
1283 case TYPE_TYPEDEF: {
1284 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1285 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1289 if (type->typeoft.typeof_type) {
1290 return get_type_modifiers(type->typeoft.typeof_type);
1292 return get_type_modifiers(type->typeoft.expression->base.type);
1295 panic("invalid type found in get_type_modifiers");
1298 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1300 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1303 switch (type->base.kind) {
1305 return TYPE_QUALIFIER_NONE;
1307 qualifiers |= type->base.qualifiers;
1308 const typedef_type_t *typedef_type = &type->typedeft;
1309 if (typedef_type->resolved_type != NULL)
1310 type = typedef_type->resolved_type;
1312 type = typedef_type->typedefe->type;
1315 type = type->typeoft.typeof_type;
1318 if (skip_array_type) {
1319 type = type->array.element_type;
1328 return type->base.qualifiers | qualifiers;
1331 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1333 assert(kind <= ATOMIC_TYPE_LAST);
1334 return atomic_type_properties[kind].size;
1337 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1339 assert(kind <= ATOMIC_TYPE_LAST);
1340 return atomic_type_properties[kind].alignment;
1343 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1345 assert(kind <= ATOMIC_TYPE_LAST);
1346 return atomic_type_properties[kind].flags;
1350 * Find the atomic type kind representing a given size (signed).
1352 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1354 static atomic_type_kind_t kinds[32];
1357 atomic_type_kind_t kind = kinds[size];
1358 if (kind == ATOMIC_TYPE_INVALID) {
1359 static const atomic_type_kind_t possible_kinds[] = {
1364 ATOMIC_TYPE_LONGLONG
1366 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1367 if (get_atomic_type_size(possible_kinds[i]) == size) {
1368 kind = possible_kinds[i];
1378 * Find the atomic type kind representing a given size (signed).
1380 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1382 static atomic_type_kind_t kinds[32];
1385 atomic_type_kind_t kind = kinds[size];
1386 if (kind == ATOMIC_TYPE_INVALID) {
1387 static const atomic_type_kind_t possible_kinds[] = {
1392 ATOMIC_TYPE_ULONGLONG
1394 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1395 if (get_atomic_type_size(possible_kinds[i]) == size) {
1396 kind = possible_kinds[i];
1406 * Hash the given type and return the "singleton" version
1409 type_t *identify_new_type(type_t *type)
1411 type_t *result = typehash_insert(type);
1412 if (result != type) {
1413 obstack_free(&type_obst, type);
1419 * Creates a new atomic type.
1421 * @param akind The kind of the atomic type.
1422 * @param qualifiers Type qualifiers for the new type.
1424 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1426 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1427 type->base.qualifiers = qualifiers;
1428 type->atomic.akind = akind;
1430 return identify_new_type(type);
1434 * Creates a new complex type.
1436 * @param akind The kind of the atomic type.
1437 * @param qualifiers Type qualifiers for the new type.
1439 type_t *make_complex_type(atomic_type_kind_t akind,
1440 type_qualifiers_t qualifiers)
1442 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1443 type->base.qualifiers = qualifiers;
1444 type->atomic.akind = akind;
1446 return identify_new_type(type);
1450 * Creates a new imaginary type.
1452 * @param akind The kind of the atomic type.
1453 * @param qualifiers Type qualifiers for the new type.
1455 type_t *make_imaginary_type(atomic_type_kind_t akind,
1456 type_qualifiers_t qualifiers)
1458 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1459 type->base.qualifiers = qualifiers;
1460 type->atomic.akind = akind;
1462 return identify_new_type(type);
1466 * Creates a new pointer type.
1468 * @param points_to The points-to type for the new type.
1469 * @param qualifiers Type qualifiers for the new type.
1471 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1473 type_t *const type = allocate_type_zero(TYPE_POINTER);
1474 type->base.qualifiers = qualifiers;
1475 type->pointer.points_to = points_to;
1476 type->pointer.base_variable = NULL;
1478 return identify_new_type(type);
1482 * Creates a new reference type.
1484 * @param refers_to The referred-to type for the new type.
1486 type_t *make_reference_type(type_t *refers_to)
1488 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1489 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1490 type->reference.refers_to = refers_to;
1492 return identify_new_type(type);
1496 * Creates a new based pointer type.
1498 * @param points_to The points-to type for the new type.
1499 * @param qualifiers Type qualifiers for the new type.
1500 * @param variable The based variable
1502 type_t *make_based_pointer_type(type_t *points_to,
1503 type_qualifiers_t qualifiers, variable_t *variable)
1505 type_t *const type = allocate_type_zero(TYPE_POINTER);
1506 type->base.qualifiers = qualifiers;
1507 type->pointer.points_to = points_to;
1508 type->pointer.base_variable = variable;
1510 return identify_new_type(type);
1514 type_t *make_array_type(type_t *element_type, size_t size,
1515 type_qualifiers_t qualifiers)
1517 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1518 type->base.qualifiers = qualifiers;
1519 type->array.element_type = element_type;
1520 type->array.size = size;
1521 type->array.size_constant = true;
1523 return identify_new_type(type);
1526 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1527 il_alignment_t *struct_alignment,
1528 bool packed, entity_t *first)
1530 il_size_t offset = *struct_offset;
1531 il_alignment_t alignment = *struct_alignment;
1532 size_t bit_offset = 0;
1535 for (member = first; member != NULL; member = member->base.next) {
1536 if (member->kind != ENTITY_COMPOUND_MEMBER)
1538 if (!member->compound_member.bitfield)
1541 type_t *base_type = member->declaration.type;
1542 il_alignment_t base_alignment = get_type_alignment_compound(base_type);
1543 il_alignment_t alignment_mask = base_alignment-1;
1544 if (base_alignment > alignment)
1545 alignment = base_alignment;
1547 size_t bit_size = member->compound_member.bit_size;
1549 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1550 offset &= ~alignment_mask;
1551 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1553 if (bit_offset + bit_size > base_size || bit_size == 0) {
1554 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1555 offset = (offset + base_alignment-1) & ~alignment_mask;
1560 if (byte_order_big_endian) {
1561 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1562 member->compound_member.offset = offset & ~alignment_mask;
1563 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1565 member->compound_member.offset = offset;
1566 member->compound_member.bit_offset = bit_offset;
1569 bit_offset += bit_size;
1570 offset += bit_offset / BITS_PER_BYTE;
1571 bit_offset %= BITS_PER_BYTE;
1577 *struct_offset = offset;
1578 *struct_alignment = alignment;
1582 void layout_struct_type(compound_type_t *type)
1584 assert(type->compound != NULL);
1586 compound_t *compound = type->compound;
1587 if (!compound->complete)
1589 if (type->compound->layouted)
1592 il_size_t offset = 0;
1593 il_alignment_t alignment = compound->alignment;
1594 bool need_pad = false;
1596 entity_t *entry = compound->members.entities;
1597 while (entry != NULL) {
1598 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1599 entry = entry->base.next;
1603 type_t *m_type = entry->declaration.type;
1604 type_t *skipped = skip_typeref(m_type);
1605 if (! is_type_valid(skipped)) {
1606 entry = entry->base.next;
1610 if (entry->compound_member.bitfield) {
1611 entry = pack_bitfield_members(&offset, &alignment,
1612 compound->packed, entry);
1616 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1617 if (m_alignment > alignment)
1618 alignment = m_alignment;
1620 if (!compound->packed) {
1621 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1623 if (new_offset > offset) {
1625 offset = new_offset;
1629 entry->compound_member.offset = offset;
1630 offset += get_type_size(m_type);
1632 entry = entry->base.next;
1635 if (!compound->packed) {
1636 il_size_t new_offset = (offset + alignment-1) & -alignment;
1637 if (new_offset > offset) {
1639 offset = new_offset;
1643 source_position_t const *const pos = &compound->base.source_position;
1645 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1646 } else if (compound->packed) {
1647 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1650 compound->size = offset;
1651 compound->alignment = alignment;
1652 compound->layouted = true;
1655 void layout_union_type(compound_type_t *type)
1657 assert(type->compound != NULL);
1659 compound_t *compound = type->compound;
1660 if (! compound->complete)
1664 il_alignment_t alignment = compound->alignment;
1666 entity_t *entry = compound->members.entities;
1667 for (; entry != NULL; entry = entry->base.next) {
1668 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1671 type_t *m_type = entry->declaration.type;
1672 if (! is_type_valid(skip_typeref(m_type)))
1675 entry->compound_member.offset = 0;
1676 il_size_t m_size = get_type_size(m_type);
1679 il_alignment_t m_alignment = get_type_alignment_compound(m_type);
1680 if (m_alignment > alignment)
1681 alignment = m_alignment;
1683 size = (size + alignment - 1) & -alignment;
1685 compound->size = size;
1686 compound->alignment = alignment;
1689 function_parameter_t *allocate_parameter(type_t *const type)
1691 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1692 memset(param, 0, sizeof(*param));
1697 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1698 type_t *argument_type2)
1700 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1701 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1702 parameter1->next = parameter2;
1704 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1705 type->function.return_type = return_type;
1706 type->function.parameters = parameter1;
1707 type->function.linkage = LINKAGE_C;
1709 return identify_new_type(type);
1712 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1714 function_parameter_t *const parameter = allocate_parameter(argument_type);
1716 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1717 type->function.return_type = return_type;
1718 type->function.parameters = parameter;
1719 type->function.linkage = LINKAGE_C;
1721 return identify_new_type(type);
1724 type_t *make_function_1_type_variadic(type_t *return_type,
1725 type_t *argument_type)
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.variadic = true;
1733 type->function.linkage = LINKAGE_C;
1735 return identify_new_type(type);
1738 type_t *make_function_0_type(type_t *return_type)
1740 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1741 type->function.return_type = return_type;
1742 type->function.parameters = NULL;
1743 type->function.linkage = LINKAGE_C;
1745 return identify_new_type(type);
1748 type_t *make_function_type(type_t *return_type, int n_types,
1749 type_t *const *argument_types,
1750 decl_modifiers_t modifiers)
1752 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1753 type->function.return_type = return_type;
1754 type->function.modifiers |= modifiers;
1755 type->function.linkage = LINKAGE_C;
1757 function_parameter_t **anchor = &type->function.parameters;
1758 for (int i = 0; i < n_types; ++i) {
1759 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1760 *anchor = parameter;
1761 anchor = ¶meter->next;
1764 return identify_new_type(type);
1768 * Debug helper. Prints the given type to stdout.
1770 static __attribute__((unused))
1771 void dbg_type(const type_t *type)
1773 print_to_file(stderr);