2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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
27 #include "type_hash.h"
28 #include "adt/error.h"
29 #include "lang_features.h"
31 static struct obstack _type_obst;
33 struct obstack *type_obst = &_type_obst;
34 static int type_visited = 0;
35 static bool print_implicit_array_size = false;
37 static void intern_print_type_pre(const type_t *type, bool top);
38 static void intern_print_type_post(const type_t *type, bool top);
40 typedef struct atomic_type_properties_t atomic_type_properties_t;
41 struct atomic_type_properties_t {
42 unsigned size; /**< type size in bytes */
43 unsigned alignment; /**< type alignment in bytes */
44 unsigned flags; /**< type flags from atomic_type_flag_t */
47 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
48 //ATOMIC_TYPE_INVALID = 0,
49 [ATOMIC_TYPE_VOID] = {
52 .flags = ATOMIC_TYPE_FLAG_NONE
54 [ATOMIC_TYPE_CHAR] = {
57 /* signed flag will be set when known */
58 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
60 [ATOMIC_TYPE_SCHAR] = {
63 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
64 | ATOMIC_TYPE_FLAG_SIGNED,
66 [ATOMIC_TYPE_UCHAR] = {
69 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
71 [ATOMIC_TYPE_SHORT] = {
74 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
75 | ATOMIC_TYPE_FLAG_SIGNED
77 [ATOMIC_TYPE_USHORT] = {
80 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
83 .size = (unsigned) -1,
84 .alignment = (unsigned) -1,
85 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
86 | ATOMIC_TYPE_FLAG_SIGNED,
88 [ATOMIC_TYPE_UINT] = {
89 .size = (unsigned) -1,
90 .alignment = (unsigned) -1,
91 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
93 [ATOMIC_TYPE_LONG] = {
94 .size = (unsigned) -1,
95 .alignment = (unsigned) -1,
96 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
97 | ATOMIC_TYPE_FLAG_SIGNED,
99 [ATOMIC_TYPE_ULONG] = {
100 .size = (unsigned) -1,
101 .alignment = (unsigned) -1,
102 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
104 [ATOMIC_TYPE_LONGLONG] = {
105 .size = (unsigned) -1,
106 .alignment = (unsigned) -1,
107 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
108 | ATOMIC_TYPE_FLAG_SIGNED,
110 [ATOMIC_TYPE_ULONGLONG] = {
111 .size = (unsigned) -1,
112 .alignment = (unsigned) -1,
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
115 [ATOMIC_TYPE_BOOL] = {
116 .size = (unsigned) -1,
117 .alignment = (unsigned) -1,
118 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
120 [ATOMIC_TYPE_FLOAT] = {
122 .alignment = (unsigned) -1,
123 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
124 | ATOMIC_TYPE_FLAG_SIGNED,
126 [ATOMIC_TYPE_DOUBLE] = {
128 .alignment = (unsigned) -1,
129 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
130 | ATOMIC_TYPE_FLAG_SIGNED,
132 [ATOMIC_TYPE_LONG_DOUBLE] = {
134 .alignment = (unsigned) -1,
135 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
136 | ATOMIC_TYPE_FLAG_SIGNED,
138 /* complex and imaginary types are set in init_types */
141 void init_types(void)
143 obstack_init(type_obst);
145 atomic_type_properties_t *props = atomic_type_properties;
147 if (char_is_signed) {
148 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
151 unsigned int_size = machine_size < 32 ? 2 : 4;
152 unsigned long_size = machine_size < 64 ? 4 : 8;
153 unsigned llong_size = machine_size < 32 ? 4 : 8;
155 props[ATOMIC_TYPE_INT].size = int_size;
156 props[ATOMIC_TYPE_INT].alignment = int_size;
157 props[ATOMIC_TYPE_UINT].size = int_size;
158 props[ATOMIC_TYPE_UINT].alignment = int_size;
159 props[ATOMIC_TYPE_LONG].size = long_size;
160 props[ATOMIC_TYPE_LONG].alignment = long_size;
161 props[ATOMIC_TYPE_ULONG].size = long_size;
162 props[ATOMIC_TYPE_ULONG].alignment = long_size;
163 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
164 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
165 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
166 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
168 /* TODO: backend specific, need a way to query the backend for this.
169 * The following are good settings for x86 */
170 props[ATOMIC_TYPE_FLOAT].alignment = 4;
171 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
172 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
173 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
174 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
176 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UINT];
179 void exit_types(void)
181 obstack_free(type_obst, NULL);
184 void type_set_output(FILE *stream)
189 void inc_type_visited(void)
194 void print_type_qualifiers(type_qualifiers_t qualifiers)
196 if (qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
197 if (qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
198 if (qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
202 * Prints the name of an atomic type kinds.
204 * @param kind The type kind.
207 void print_atomic_kinds(atomic_type_kind_t kind)
209 const char *s = "INVALIDATOMIC";
211 case ATOMIC_TYPE_INVALID: break;
212 case ATOMIC_TYPE_VOID: s = "void"; break;
213 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
214 case ATOMIC_TYPE_CHAR: s = "char"; break;
215 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
216 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
217 case ATOMIC_TYPE_INT: s = "int"; break;
218 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
219 case ATOMIC_TYPE_SHORT: s = "short"; break;
220 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
221 case ATOMIC_TYPE_LONG: s = "long"; break;
222 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
223 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
224 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
225 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
226 case ATOMIC_TYPE_FLOAT: s = "float"; break;
227 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
233 * Prints the name of an atomic type.
235 * @param type The type.
238 void print_atomic_type(const atomic_type_t *type)
240 print_type_qualifiers(type->base.qualifiers);
241 print_atomic_kinds(type->akind);
245 * Prints the name of a complex type.
247 * @param type The type.
250 void print_complex_type(const complex_type_t *type)
252 print_type_qualifiers(type->base.qualifiers);
253 fputs("_Complex ", out);
254 print_atomic_kinds(type->akind);
258 * Prints the name of an imaginary type.
260 * @param type The type.
263 void print_imaginary_type(const imaginary_type_t *type)
265 print_type_qualifiers(type->base.qualifiers);
266 fputs("_Imaginary ", out);
267 print_atomic_kinds(type->akind);
271 * Print the first part (the prefix) of a type.
273 * @param type The type to print.
274 * @param top true, if this is the top type, false if it's an embedded type.
276 static void print_function_type_pre(const function_type_t *type, bool top)
278 print_type_qualifiers(type->base.qualifiers);
280 intern_print_type_pre(type->return_type, false);
282 /* don't emit braces if we're the toplevel type... */
288 * Print the second part (the postfix) of a type.
290 * @param type The type to print.
291 * @param top true, if this is the top type, false if it's an embedded type.
293 static void print_function_type_post(const function_type_t *type,
294 const scope_t *scope, bool top)
296 intern_print_type_post(type->return_type, false);
297 /* don't emit braces if we're the toplevel type... */
305 function_parameter_t *parameter = type->parameters;
306 for( ; parameter != NULL; parameter = parameter->next) {
312 print_type(parameter->type);
315 declaration_t *parameter = scope->declarations;
316 for( ; parameter != NULL; parameter = parameter->next) {
322 print_type_ext(parameter->type, parameter->symbol,
326 if (type->variadic) {
334 if (first && !type->unspecified_parameters) {
341 * Prints the prefix part of a pointer type.
343 * @param type The pointer type.
345 static void print_pointer_type_pre(const pointer_type_t *type)
347 intern_print_type_pre(type->points_to, false);
349 print_type_qualifiers(type->base.qualifiers);
353 * Prints the postfix part of a pointer type.
355 * @param type The pointer type.
357 static void print_pointer_type_post(const pointer_type_t *type)
359 intern_print_type_post(type->points_to, false);
363 * Prints the prefix part of an array type.
365 * @param type The array type.
367 static void print_array_type_pre(const array_type_t *type)
369 intern_print_type_pre(type->element_type, false);
373 * Prints the postfix part of an array type.
375 * @param type The array type.
377 static void print_array_type_post(const array_type_t *type)
380 if (type->is_static) {
381 fputs("static ", out);
383 print_type_qualifiers(type->base.qualifiers);
384 if (type->size_expression != NULL
385 && (print_implicit_array_size || !type->has_implicit_size)) {
386 print_expression(type->size_expression);
389 intern_print_type_post(type->element_type, false);
393 * Prints the postfix part of a bitfield type.
395 * @param type The array type.
397 static void print_bitfield_type_post(const bitfield_type_t *type)
400 print_expression(type->size);
401 intern_print_type_post(type->base_type, false);
405 * Prints an enum definition.
407 * @param declaration The enum's type declaration.
409 void print_enum_definition(const declaration_t *declaration)
415 declaration_t *entry = declaration->next;
416 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
417 entry = entry->next) {
420 fprintf(out, "%s", entry->symbol->string);
421 if (entry->init.initializer != NULL) {
424 /* skip the implicit cast */
425 expression_t *expression = entry->init.enum_value;
426 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
427 expression = expression->unary.value;
429 print_expression(expression);
440 * Prints an enum type.
442 * @param type The enum type.
444 static void print_type_enum(const enum_type_t *type)
446 print_type_qualifiers(type->base.qualifiers);
449 declaration_t *declaration = type->declaration;
450 symbol_t *symbol = declaration->symbol;
451 if (symbol != NULL) {
452 fputs(symbol->string, out);
454 print_enum_definition(declaration);
459 * Print the compound part of a compound type.
461 * @param declaration The declaration of the compound type.
463 void print_compound_definition(const declaration_t *declaration)
468 declaration_t *iter = declaration->scope.declarations;
469 for( ; iter != NULL; iter = iter->next) {
471 print_declaration(iter);
481 * Prints a compound type.
483 * @param type The compound type.
485 static void print_compound_type(const compound_type_t *type)
487 print_type_qualifiers(type->base.qualifiers);
489 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
490 fputs("struct ", out);
492 assert(type->base.kind == TYPE_COMPOUND_UNION);
493 fputs("union ", out);
496 declaration_t *declaration = type->declaration;
497 symbol_t *symbol = declaration->symbol;
498 if (symbol != NULL) {
499 fputs(symbol->string, out);
501 print_compound_definition(declaration);
506 * Prints the prefix part of a typedef type.
508 * @param type The typedef type.
510 static void print_typedef_type_pre(const typedef_type_t *const type)
512 print_type_qualifiers(type->base.qualifiers);
513 fputs(type->declaration->symbol->string, out);
517 * Prints the prefix part of a typeof type.
519 * @param type The typeof type.
521 static void print_typeof_type_pre(const typeof_type_t *const type)
523 fputs("typeof(", out);
524 if (type->expression != NULL) {
525 assert(type->typeof_type == NULL);
526 print_expression(type->expression);
528 print_type(type->typeof_type);
534 * Prints the prefix part of a type.
536 * @param type The type.
537 * @param top true if we print the toplevel type, false else.
539 static void intern_print_type_pre(const type_t *const type, const bool top)
543 fputs("<error>", out);
545 fputs("<invalid>", out);
548 print_type_enum(&type->enumt);
551 print_atomic_type(&type->atomic);
554 print_complex_type(&type->complex);
557 print_imaginary_type(&type->imaginary);
559 case TYPE_COMPOUND_STRUCT:
560 case TYPE_COMPOUND_UNION:
561 print_compound_type(&type->compound);
564 fputs(type->builtin.symbol->string, out);
567 print_function_type_pre(&type->function, top);
570 print_pointer_type_pre(&type->pointer);
573 intern_print_type_pre(type->bitfield.base_type, top);
576 print_array_type_pre(&type->array);
579 print_typedef_type_pre(&type->typedeft);
582 print_typeof_type_pre(&type->typeoft);
585 fputs("unknown", out);
589 * Prints the postfix part of a type.
591 * @param type The type.
592 * @param top true if we print the toplevel type, false else.
594 static void intern_print_type_post(const type_t *const type, const bool top)
598 print_function_type_post(&type->function, NULL, top);
601 print_pointer_type_post(&type->pointer);
604 print_array_type_post(&type->array);
607 print_bitfield_type_post(&type->bitfield);
615 case TYPE_COMPOUND_STRUCT:
616 case TYPE_COMPOUND_UNION:
627 * @param type The type.
629 void print_type(const type_t *const type)
631 print_type_ext(type, NULL, NULL);
634 void print_type_ext(const type_t *const type, const symbol_t *symbol,
635 const scope_t *scope)
638 fputs("nil type", out);
642 intern_print_type_pre(type, true);
643 if (symbol != NULL) {
645 fputs(symbol->string, out);
647 if (type->kind == TYPE_FUNCTION) {
648 print_function_type_post(&type->function, scope, true);
650 intern_print_type_post(type, true);
655 * Return the size of a type AST node.
657 * @param type The type.
659 static size_t get_type_size(const type_t *type)
662 case TYPE_ATOMIC: return sizeof(atomic_type_t);
663 case TYPE_COMPLEX: return sizeof(complex_type_t);
664 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
665 case TYPE_COMPOUND_STRUCT:
666 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
667 case TYPE_ENUM: return sizeof(enum_type_t);
668 case TYPE_FUNCTION: return sizeof(function_type_t);
669 case TYPE_POINTER: return sizeof(pointer_type_t);
670 case TYPE_ARRAY: return sizeof(array_type_t);
671 case TYPE_BUILTIN: return sizeof(builtin_type_t);
672 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
673 case TYPE_TYPEOF: return sizeof(typeof_type_t);
674 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
675 case TYPE_ERROR: panic("error type found");
676 case TYPE_INVALID: panic("invalid type found");
678 panic("unknown type found");
684 * @param type The type to copy.
685 * @return A copy of the type.
687 * @note This does not produce a deep copy!
689 type_t *duplicate_type(const type_t *type)
691 size_t size = get_type_size(type);
693 type_t *copy = obstack_alloc(type_obst, size);
694 memcpy(copy, type, size);
700 * Returns the unqualified type of a given type.
702 * @param type The type.
703 * @returns The unqualified type.
705 type_t *get_unqualified_type(type_t *type)
707 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
710 type_t *unqualified_type = duplicate_type(type);
711 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
713 type_t *result = typehash_insert(unqualified_type);
714 if (result != unqualified_type) {
715 obstack_free(type_obst, unqualified_type);
722 * Check if a type is valid.
724 * @param type The type to check.
725 * @return true if type represents a valid type.
727 bool type_valid(const type_t *type)
729 return type->kind != TYPE_INVALID;
732 static bool test_atomic_type_flag(atomic_type_kind_t kind,
733 atomic_type_flag_t flag)
735 assert(kind <= ATOMIC_TYPE_LAST);
736 return (atomic_type_properties[kind].flags & flag) != 0;
740 * Returns true if the given type is an integer type.
742 * @param type The type to check.
743 * @return True if type is an integer type.
745 bool is_type_integer(const type_t *type)
747 assert(!is_typeref(type));
749 if (type->kind == TYPE_ENUM)
751 if (type->kind == TYPE_BITFIELD)
754 if (type->kind != TYPE_ATOMIC)
757 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
761 * Returns true if the given type is an floating point type.
763 * @param type The type to check.
764 * @return True if type is a floating point type.
766 bool is_type_float(const type_t *type)
768 assert(!is_typeref(type));
770 if (type->kind != TYPE_ATOMIC)
773 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
777 * Returns true if the given type is a signed type.
779 * @param type The type to check.
780 * @return True if type is a signed type.
782 bool is_type_signed(const type_t *type)
784 assert(!is_typeref(type));
786 /* enum types are int for now */
787 if (type->kind == TYPE_ENUM)
789 if (type->kind == TYPE_BITFIELD)
790 return is_type_signed(type->bitfield.base_type);
792 if (type->kind != TYPE_ATOMIC)
795 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
799 * Returns true if the given type represents an arithmetic type.
801 * @param type The type to check.
802 * @return True if type represents an arithmetic type.
804 bool is_type_arithmetic(const type_t *type)
806 assert(!is_typeref(type));
813 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
815 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
817 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
823 bool is_type_real(const type_t *type)
826 return is_type_integer(type)
827 || (type->kind == TYPE_ATOMIC && is_type_float(type));
831 * Returns true if the given type represents a scalar type.
833 * @param type The type to check.
834 * @return True if type represents a scalar type.
836 bool is_type_scalar(const type_t *type)
838 assert(!is_typeref(type));
840 switch (type->kind) {
841 case TYPE_POINTER: return true;
842 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
846 return is_type_arithmetic(type);
850 * Check if a given type is incomplete.
852 * @param type The type to check.
853 * @return True if the given type is incomplete (ie. just forward).
855 bool is_type_incomplete(const type_t *type)
857 assert(!is_typeref(type));
860 case TYPE_COMPOUND_STRUCT:
861 case TYPE_COMPOUND_UNION: {
862 const compound_type_t *compound_type = &type->compound;
863 declaration_t *declaration = compound_type->declaration;
864 return !declaration->init.complete;
867 const enum_type_t *enum_type = &type->enumt;
868 declaration_t *declaration = enum_type->declaration;
869 return !declaration->init.complete;
873 return type->array.size_expression == NULL
874 && !type->array.size_constant;
877 return type->atomic.akind == ATOMIC_TYPE_VOID;
880 return type->complex.akind == ATOMIC_TYPE_VOID;
883 return type->imaginary.akind == ATOMIC_TYPE_VOID;
894 panic("is_type_incomplete called without typerefs skipped");
899 panic("invalid type found");
902 bool is_type_object(const type_t *type)
904 return !is_type_function(type) && !is_type_incomplete(type);
908 * Check if two function types are compatible.
910 static bool function_types_compatible(const function_type_t *func1,
911 const function_type_t *func2)
913 const type_t* const ret1 = skip_typeref(func1->return_type);
914 const type_t* const ret2 = skip_typeref(func2->return_type);
915 if (!types_compatible(ret1, ret2))
918 /* can parameters be compared? */
919 if (func1->unspecified_parameters || func2->unspecified_parameters)
922 if (func1->variadic != func2->variadic)
925 /* TODO: handling of unspecified parameters not correct yet */
927 /* all argument types must be compatible */
928 function_parameter_t *parameter1 = func1->parameters;
929 function_parameter_t *parameter2 = func2->parameters;
930 for ( ; parameter1 != NULL && parameter2 != NULL;
931 parameter1 = parameter1->next, parameter2 = parameter2->next) {
932 type_t *parameter1_type = skip_typeref(parameter1->type);
933 type_t *parameter2_type = skip_typeref(parameter2->type);
935 parameter1_type = get_unqualified_type(parameter1_type);
936 parameter2_type = get_unqualified_type(parameter2_type);
938 if (!types_compatible(parameter1_type, parameter2_type))
941 /* same number of arguments? */
942 if (parameter1 != NULL || parameter2 != NULL)
949 * Check if two array types are compatible.
951 static bool array_types_compatible(const array_type_t *array1,
952 const array_type_t *array2)
954 type_t *element_type1 = skip_typeref(array1->element_type);
955 type_t *element_type2 = skip_typeref(array2->element_type);
956 if (!types_compatible(element_type1, element_type2))
959 if (!array1->size_constant || !array2->size_constant)
962 return array1->size == array2->size;
966 * Check if two types are compatible.
968 bool types_compatible(const type_t *type1, const type_t *type2)
970 assert(!is_typeref(type1));
971 assert(!is_typeref(type2));
973 /* shortcut: the same type is always compatible */
977 if (type1->base.qualifiers != type2->base.qualifiers)
979 if (type1->kind != type2->kind)
982 switch(type1->kind) {
984 return function_types_compatible(&type1->function, &type2->function);
986 return type1->atomic.akind == type2->atomic.akind;
988 return type1->complex.akind == type2->complex.akind;
990 return type1->imaginary.akind == type2->imaginary.akind;
992 return array_types_compatible(&type1->array, &type2->array);
995 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
996 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
997 return types_compatible(to1, to2);
1000 case TYPE_COMPOUND_STRUCT:
1001 case TYPE_COMPOUND_UNION:
1004 /* TODO: not implemented */
1008 /* not sure if this makes sense or is even needed, implement it if you
1009 * really need it! */
1010 panic("type compatibility check for bitfield type");
1013 /* Hmm, the error type should be compatible to all other types */
1016 panic("invalid type found in compatible types");
1019 panic("typerefs not skipped in compatible types?!?");
1022 /* TODO: incomplete */
1027 * Skip all typerefs and return the underlying type.
1029 type_t *skip_typeref(type_t *type)
1031 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1032 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1035 switch(type->kind) {
1038 case TYPE_TYPEDEF: {
1039 qualifiers |= type->base.qualifiers;
1040 modifiers |= type->base.modifiers;
1041 const typedef_type_t *typedef_type = &type->typedeft;
1042 if (typedef_type->resolved_type != NULL) {
1043 type = typedef_type->resolved_type;
1046 type = typedef_type->declaration->type;
1050 const typeof_type_t *typeof_type = &type->typeoft;
1051 if (typeof_type->typeof_type != NULL) {
1052 type = typeof_type->typeof_type;
1054 type = typeof_type->expression->base.type;
1064 if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1065 type_t *const copy = duplicate_type(type);
1067 /* for const with typedefed array type the element type has to be
1069 if (is_type_array(copy)) {
1070 type_t *element_type = copy->array.element_type;
1071 element_type = duplicate_type(element_type);
1072 element_type->base.qualifiers |= qualifiers;
1073 element_type->base.modifiers |= modifiers;
1074 copy->array.element_type = element_type;
1076 copy->base.qualifiers |= qualifiers;
1077 copy->base.modifiers |= modifiers;
1080 type = typehash_insert(copy);
1082 obstack_free(type_obst, copy);
1089 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1091 assert(kind <= ATOMIC_TYPE_LAST);
1092 return atomic_type_properties[kind].size;
1095 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1097 assert(kind <= ATOMIC_TYPE_LAST);
1098 return atomic_type_properties[kind].alignment;
1101 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1103 assert(kind <= ATOMIC_TYPE_LAST);
1104 return atomic_type_properties[kind].flags;
1107 atomic_type_kind_t get_intptr_kind(void)
1109 if (machine_size <= 32)
1110 return ATOMIC_TYPE_INT;
1111 else if (machine_size <= 64)
1112 return ATOMIC_TYPE_LONG;
1114 return ATOMIC_TYPE_LONGLONG;
1117 atomic_type_kind_t get_uintptr_kind(void)
1119 if (machine_size <= 32)
1120 return ATOMIC_TYPE_UINT;
1121 else if (machine_size <= 64)
1122 return ATOMIC_TYPE_ULONG;
1124 return ATOMIC_TYPE_ULONGLONG;
1128 * Find the atomic type kind representing a given size (signed).
1130 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1131 static atomic_type_kind_t kinds[32];
1134 atomic_type_kind_t kind = kinds[size];
1135 if (kind == ATOMIC_TYPE_INVALID) {
1136 static const atomic_type_kind_t possible_kinds[] = {
1141 ATOMIC_TYPE_LONGLONG
1143 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1144 if (get_atomic_type_size(possible_kinds[i]) == size) {
1145 kind = possible_kinds[i];
1155 * Find the atomic type kind representing a given size (signed).
1157 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1158 static atomic_type_kind_t kinds[32];
1161 atomic_type_kind_t kind = kinds[size];
1162 if (kind == ATOMIC_TYPE_INVALID) {
1163 static const atomic_type_kind_t possible_kinds[] = {
1168 ATOMIC_TYPE_ULONGLONG
1170 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1171 if (get_atomic_type_size(possible_kinds[i]) == size) {
1172 kind = possible_kinds[i];
1182 * Hash the given type and return the "singleton" version
1185 static type_t *identify_new_type(type_t *type)
1187 type_t *result = typehash_insert(type);
1188 if (result != type) {
1189 obstack_free(type_obst, type);
1195 * Creates a new atomic type.
1197 * @param akind The kind of the atomic type.
1198 * @param qualifiers Type qualifiers for the new type.
1200 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1202 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1203 memset(type, 0, sizeof(atomic_type_t));
1205 type->kind = TYPE_ATOMIC;
1206 type->base.qualifiers = qualifiers;
1207 type->base.alignment = get_atomic_type_alignment(akind);
1208 type->atomic.akind = akind;
1210 return identify_new_type(type);
1214 * Creates a new complex type.
1216 * @param akind The kind of the atomic type.
1217 * @param qualifiers Type qualifiers for the new type.
1219 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1221 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1222 memset(type, 0, sizeof(complex_type_t));
1224 type->kind = TYPE_COMPLEX;
1225 type->base.qualifiers = qualifiers;
1226 type->base.alignment = get_atomic_type_alignment(akind);
1227 type->complex.akind = akind;
1229 return identify_new_type(type);
1233 * Creates a new imaginary type.
1235 * @param akind The kind of the atomic type.
1236 * @param qualifiers Type qualifiers for the new type.
1238 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1240 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1241 memset(type, 0, sizeof(imaginary_type_t));
1243 type->kind = TYPE_IMAGINARY;
1244 type->base.qualifiers = qualifiers;
1245 type->base.alignment = get_atomic_type_alignment(akind);
1246 type->imaginary.akind = akind;
1248 return identify_new_type(type);
1252 * Creates a new pointer type.
1254 * @param points_to The points-to type for the new type.
1255 * @param qualifiers Type qualifiers for the new type.
1257 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1259 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1260 memset(type, 0, sizeof(pointer_type_t));
1262 type->kind = TYPE_POINTER;
1263 type->base.qualifiers = qualifiers;
1264 type->base.alignment = 0;
1265 type->pointer.points_to = points_to;
1267 return identify_new_type(type);
1270 type_t *make_array_type(type_t *element_type, size_t size,
1271 type_qualifiers_t qualifiers)
1273 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1274 memset(type, 0, sizeof(array_type_t));
1276 type->kind = TYPE_ARRAY;
1277 type->base.qualifiers = qualifiers;
1278 type->base.alignment = 0;
1279 type->array.element_type = element_type;
1280 type->array.size = size;
1281 type->array.size_constant = true;
1283 return identify_new_type(type);
1287 * Debug helper. Prints the given type to stdout.
1289 static __attribute__((unused))
1290 void dbg_type(const type_t *type)
1292 FILE *old_out = out;