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)
197 if (qualifiers & TYPE_QUALIFIER_CONST) {
198 fputs(" const" + first, out);
201 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
202 fputs(" volatile" + first, out);
205 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
206 fputs(" restrict" + first, out);
212 * Prints the name of an atomic type kinds.
214 * @param kind The type kind.
217 void print_atomic_kinds(atomic_type_kind_t kind)
219 const char *s = "INVALIDATOMIC";
221 case ATOMIC_TYPE_INVALID: break;
222 case ATOMIC_TYPE_VOID: s = "void"; break;
223 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
224 case ATOMIC_TYPE_CHAR: s = "char"; break;
225 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
226 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
227 case ATOMIC_TYPE_INT: s = "int"; break;
228 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
229 case ATOMIC_TYPE_SHORT: s = "short"; break;
230 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
231 case ATOMIC_TYPE_LONG: s = "long"; break;
232 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
233 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
234 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
235 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
236 case ATOMIC_TYPE_FLOAT: s = "float"; break;
237 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
243 * Prints the name of an atomic type.
245 * @param type The type.
248 void print_atomic_type(const atomic_type_t *type)
250 print_type_qualifiers(type->base.qualifiers);
251 if (type->base.qualifiers != 0)
253 print_atomic_kinds(type->akind);
257 * Prints the name of a complex type.
259 * @param type The type.
262 void print_complex_type(const complex_type_t *type)
264 int empty = type->base.qualifiers == 0;
265 print_type_qualifiers(type->base.qualifiers);
266 fputs(" _Complex " + empty, out);
267 print_atomic_kinds(type->akind);
271 * Prints the name of an imaginary type.
273 * @param type The type.
276 void print_imaginary_type(const imaginary_type_t *type)
278 int empty = type->base.qualifiers == 0;
279 print_type_qualifiers(type->base.qualifiers);
280 fputs(" _Imaginary " + empty, out);
281 print_atomic_kinds(type->akind);
285 * Print the first part (the prefix) of a type.
287 * @param type The type to print.
288 * @param top true, if this is the top type, false if it's an embedded type.
290 static void print_function_type_pre(const function_type_t *type, bool top)
292 print_type_qualifiers(type->base.qualifiers);
293 if (type->base.qualifiers != 0)
297 intern_print_type_pre(type->return_type, false);
299 switch (type->calling_convention) {
301 fputs("__cdecl ", out);
304 fputs("__stdcall ", out);
307 fputs("__fastcall ", out);
310 fputs("__thiscall ", out);
316 /* don't emit braces if we're the toplevel type... */
322 * Print the second part (the postfix) of a type.
324 * @param type The type to print.
325 * @param top true, if this is the top type, false if it's an embedded type.
327 static void print_function_type_post(const function_type_t *type,
328 const scope_t *scope, bool top)
330 intern_print_type_post(type->return_type, false);
335 function_parameter_t *parameter = type->parameters;
336 for( ; parameter != NULL; parameter = parameter->next) {
342 print_type(parameter->type);
345 declaration_t *parameter = scope->declarations;
346 for( ; parameter != NULL; parameter = parameter->next) {
352 print_type_ext(parameter->type, parameter->symbol,
356 if (type->variadic) {
364 if (first && !type->unspecified_parameters) {
369 /* don't emit braces if we're the toplevel type... */
375 * Prints the prefix part of a pointer type.
377 * @param type The pointer type.
379 static void print_pointer_type_pre(const pointer_type_t *type)
381 intern_print_type_pre(type->points_to, false);
383 print_type_qualifiers(type->base.qualifiers);
384 if (type->base.qualifiers != 0)
389 * Prints the postfix part of a pointer type.
391 * @param type The pointer type.
393 static void print_pointer_type_post(const pointer_type_t *type)
395 intern_print_type_post(type->points_to, false);
399 * Prints the prefix part of an array type.
401 * @param type The array type.
403 static void print_array_type_pre(const array_type_t *type)
405 intern_print_type_pre(type->element_type, false);
409 * Prints the postfix part of an array type.
411 * @param type The array type.
413 static void print_array_type_post(const array_type_t *type)
416 if (type->is_static) {
417 fputs("static ", out);
419 print_type_qualifiers(type->base.qualifiers);
420 if (type->base.qualifiers != 0)
422 if (type->size_expression != NULL
423 && (print_implicit_array_size || !type->has_implicit_size)) {
424 print_expression(type->size_expression);
427 intern_print_type_post(type->element_type, false);
431 * Prints the postfix part of a bitfield type.
433 * @param type The array type.
435 static void print_bitfield_type_post(const bitfield_type_t *type)
438 print_expression(type->size);
439 intern_print_type_post(type->base_type, false);
443 * Prints an enum definition.
445 * @param declaration The enum's type declaration.
447 void print_enum_definition(const declaration_t *declaration)
453 declaration_t *entry = declaration->next;
454 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
455 entry = entry->next) {
458 fprintf(out, "%s", entry->symbol->string);
459 if (entry->init.initializer != NULL) {
462 /* skip the implicit cast */
463 expression_t *expression = entry->init.enum_value;
464 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
465 expression = expression->unary.value;
467 print_expression(expression);
478 * Prints an enum type.
480 * @param type The enum type.
482 static void print_type_enum(const enum_type_t *type)
484 int empty = type->base.qualifiers == 0;
485 print_type_qualifiers(type->base.qualifiers);
486 fputs(" enum " + empty, out);
488 declaration_t *declaration = type->declaration;
489 symbol_t *symbol = declaration->symbol;
490 if (symbol != NULL) {
491 fputs(symbol->string, out);
493 print_enum_definition(declaration);
498 * Print the compound part of a compound type.
500 * @param declaration The declaration of the compound type.
502 void print_compound_definition(const declaration_t *declaration)
507 declaration_t *iter = declaration->scope.declarations;
508 for( ; iter != NULL; iter = iter->next) {
510 print_declaration(iter);
520 * Prints a compound type.
522 * @param type The compound type.
524 static void print_compound_type(const compound_type_t *type)
526 int empty = type->base.qualifiers == 0;
527 print_type_qualifiers(type->base.qualifiers);
529 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
530 fputs(" struct " + empty, out);
532 assert(type->base.kind == TYPE_COMPOUND_UNION);
533 fputs(" union " + empty, out);
536 declaration_t *declaration = type->declaration;
537 symbol_t *symbol = declaration->symbol;
538 if (symbol != NULL) {
539 fputs(symbol->string, out);
541 print_compound_definition(declaration);
546 * Prints the prefix part of a typedef type.
548 * @param type The typedef type.
550 static void print_typedef_type_pre(const typedef_type_t *const type)
552 print_type_qualifiers(type->base.qualifiers);
553 if (type->base.qualifiers != 0)
555 fputs(type->declaration->symbol->string, out);
559 * Prints the prefix part of a typeof type.
561 * @param type The typeof type.
563 static void print_typeof_type_pre(const typeof_type_t *const type)
565 fputs("typeof(", out);
566 if (type->expression != NULL) {
567 assert(type->typeof_type == NULL);
568 print_expression(type->expression);
570 print_type(type->typeof_type);
576 * Prints the prefix part of a type.
578 * @param type The type.
579 * @param top true if we print the toplevel type, false else.
581 static void intern_print_type_pre(const type_t *const type, const bool top)
585 fputs("<error>", out);
588 fputs("<invalid>", out);
591 print_type_enum(&type->enumt);
594 print_atomic_type(&type->atomic);
597 print_complex_type(&type->complex);
600 print_imaginary_type(&type->imaginary);
602 case TYPE_COMPOUND_STRUCT:
603 case TYPE_COMPOUND_UNION:
604 print_compound_type(&type->compound);
607 fputs(type->builtin.symbol->string, out);
610 print_function_type_pre(&type->function, top);
613 print_pointer_type_pre(&type->pointer);
616 intern_print_type_pre(type->bitfield.base_type, top);
619 print_array_type_pre(&type->array);
622 print_typedef_type_pre(&type->typedeft);
625 print_typeof_type_pre(&type->typeoft);
628 fputs("unknown", out);
632 * Prints the postfix part of a type.
634 * @param type The type.
635 * @param top true if we print the toplevel type, false else.
637 static void intern_print_type_post(const type_t *const type, const bool top)
641 print_function_type_post(&type->function, NULL, top);
644 print_pointer_type_post(&type->pointer);
647 print_array_type_post(&type->array);
650 print_bitfield_type_post(&type->bitfield);
658 case TYPE_COMPOUND_STRUCT:
659 case TYPE_COMPOUND_UNION:
670 * @param type The type.
672 void print_type(const type_t *const type)
674 print_type_ext(type, NULL, NULL);
677 void print_type_ext(const type_t *const type, const symbol_t *symbol,
678 const scope_t *scope)
681 fputs("nil type", out);
685 intern_print_type_pre(type, true);
686 if (symbol != NULL) {
688 fputs(symbol->string, out);
690 if (type->kind == TYPE_FUNCTION) {
691 print_function_type_post(&type->function, scope, true);
693 intern_print_type_post(type, true);
698 * Return the size of a type AST node.
700 * @param type The type.
702 static size_t get_type_size(const type_t *type)
705 case TYPE_ATOMIC: return sizeof(atomic_type_t);
706 case TYPE_COMPLEX: return sizeof(complex_type_t);
707 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
708 case TYPE_COMPOUND_STRUCT:
709 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
710 case TYPE_ENUM: return sizeof(enum_type_t);
711 case TYPE_FUNCTION: return sizeof(function_type_t);
712 case TYPE_POINTER: return sizeof(pointer_type_t);
713 case TYPE_ARRAY: return sizeof(array_type_t);
714 case TYPE_BUILTIN: return sizeof(builtin_type_t);
715 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
716 case TYPE_TYPEOF: return sizeof(typeof_type_t);
717 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
718 case TYPE_ERROR: panic("error type found");
719 case TYPE_INVALID: panic("invalid type found");
721 panic("unknown type found");
727 * @param type The type to copy.
728 * @return A copy of the type.
730 * @note This does not produce a deep copy!
732 type_t *duplicate_type(const type_t *type)
734 size_t size = get_type_size(type);
736 type_t *copy = obstack_alloc(type_obst, size);
737 memcpy(copy, type, size);
743 * Returns the unqualified type of a given type.
745 * @param type The type.
746 * @returns The unqualified type.
748 type_t *get_unqualified_type(type_t *type)
750 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
753 type_t *unqualified_type = duplicate_type(type);
754 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
756 type_t *result = typehash_insert(unqualified_type);
757 if (result != unqualified_type) {
758 obstack_free(type_obst, unqualified_type);
764 type_t *get_qualified_type(type_t *type, type_qualifiers_t const qual)
766 assert(!is_typeref(type));
769 if (is_type_array(type)) {
770 /* For array types the element type has to be adjusted */
771 type_t *element_type = type->array.element_type;
772 type_t *qual_element_type = get_qualified_type(element_type, qual);
774 if (qual_element_type == element_type)
777 copy = duplicate_type(type);
778 copy->array.element_type = qual_element_type;
780 if ((type->base.qualifiers & qual) == qual)
783 copy = duplicate_type(type);
784 copy->base.qualifiers |= qual;
787 type = typehash_insert(copy);
789 obstack_free(type_obst, copy);
795 * Check if a type is valid.
797 * @param type The type to check.
798 * @return true if type represents a valid type.
800 bool type_valid(const type_t *type)
802 return type->kind != TYPE_INVALID;
805 static bool test_atomic_type_flag(atomic_type_kind_t kind,
806 atomic_type_flag_t flag)
808 assert(kind <= ATOMIC_TYPE_LAST);
809 return (atomic_type_properties[kind].flags & flag) != 0;
813 * Returns true if the given type is an integer type.
815 * @param type The type to check.
816 * @return True if type is an integer type.
818 bool is_type_integer(const type_t *type)
820 assert(!is_typeref(type));
822 if (type->kind == TYPE_ENUM)
824 if (type->kind == TYPE_BITFIELD)
827 if (type->kind != TYPE_ATOMIC)
830 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
834 * Returns true if the given type is an enum type.
836 * @param type The type to check.
837 * @return True if type is an enum type.
839 bool is_type_enum(const type_t *type)
841 assert(!is_typeref(type));
842 return type->kind == TYPE_ENUM;
846 * Returns true if the given type is an floating point type.
848 * @param type The type to check.
849 * @return True if type is a floating point type.
851 bool is_type_float(const type_t *type)
853 assert(!is_typeref(type));
855 if (type->kind != TYPE_ATOMIC)
858 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
862 * Returns true if the given type is an complex type.
864 * @param type The type to check.
865 * @return True if type is a complex type.
867 bool is_type_complex(const type_t *type)
869 assert(!is_typeref(type));
871 if (type->kind != TYPE_ATOMIC)
874 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
878 * Returns true if the given type is a signed type.
880 * @param type The type to check.
881 * @return True if type is a signed type.
883 bool is_type_signed(const type_t *type)
885 assert(!is_typeref(type));
887 /* enum types are int for now */
888 if (type->kind == TYPE_ENUM)
890 if (type->kind == TYPE_BITFIELD)
891 return is_type_signed(type->bitfield.base_type);
893 if (type->kind != TYPE_ATOMIC)
896 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
900 * Returns true if the given type represents an arithmetic type.
902 * @param type The type to check.
903 * @return True if type represents an arithmetic type.
905 bool is_type_arithmetic(const type_t *type)
907 assert(!is_typeref(type));
914 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
916 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
918 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
925 * Returns true if the given type is an integer or float type.
927 * @param type The type to check.
928 * @return True if type is an integer or float type.
930 bool is_type_real(const type_t *type)
933 return is_type_integer(type)
934 || (type->kind == TYPE_ATOMIC && is_type_float(type));
938 * Returns true if the given type represents a scalar type.
940 * @param type The type to check.
941 * @return True if type represents a scalar type.
943 bool is_type_scalar(const type_t *type)
945 assert(!is_typeref(type));
947 switch (type->kind) {
948 case TYPE_POINTER: return true;
949 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
953 return is_type_arithmetic(type);
957 * Check if a given type is incomplete.
959 * @param type The type to check.
960 * @return True if the given type is incomplete (ie. just forward).
962 bool is_type_incomplete(const type_t *type)
964 assert(!is_typeref(type));
967 case TYPE_COMPOUND_STRUCT:
968 case TYPE_COMPOUND_UNION: {
969 const compound_type_t *compound_type = &type->compound;
970 declaration_t *declaration = compound_type->declaration;
971 return !declaration->init.complete;
974 const enum_type_t *enum_type = &type->enumt;
975 declaration_t *declaration = enum_type->declaration;
976 return !declaration->init.complete;
980 return type->array.size_expression == NULL
981 && !type->array.size_constant;
984 return type->atomic.akind == ATOMIC_TYPE_VOID;
987 return type->complex.akind == ATOMIC_TYPE_VOID;
990 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1001 panic("is_type_incomplete called without typerefs skipped");
1006 panic("invalid type found");
1009 bool is_type_object(const type_t *type)
1011 return !is_type_function(type) && !is_type_incomplete(type);
1015 * Check if two function types are compatible.
1017 static bool function_types_compatible(const function_type_t *func1,
1018 const function_type_t *func2)
1020 const type_t* const ret1 = skip_typeref(func1->return_type);
1021 const type_t* const ret2 = skip_typeref(func2->return_type);
1022 if (!types_compatible(ret1, ret2))
1025 /* can parameters be compared? */
1026 if (func1->unspecified_parameters || func2->unspecified_parameters)
1029 if (func1->variadic != func2->variadic)
1032 if (func1->calling_convention != func2->calling_convention)
1035 /* TODO: handling of unspecified parameters not correct yet */
1037 /* all argument types must be compatible */
1038 function_parameter_t *parameter1 = func1->parameters;
1039 function_parameter_t *parameter2 = func2->parameters;
1040 for ( ; parameter1 != NULL && parameter2 != NULL;
1041 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1042 type_t *parameter1_type = skip_typeref(parameter1->type);
1043 type_t *parameter2_type = skip_typeref(parameter2->type);
1045 parameter1_type = get_unqualified_type(parameter1_type);
1046 parameter2_type = get_unqualified_type(parameter2_type);
1048 if (!types_compatible(parameter1_type, parameter2_type))
1051 /* same number of arguments? */
1052 if (parameter1 != NULL || parameter2 != NULL)
1059 * Check if two array types are compatible.
1061 static bool array_types_compatible(const array_type_t *array1,
1062 const array_type_t *array2)
1064 type_t *element_type1 = skip_typeref(array1->element_type);
1065 type_t *element_type2 = skip_typeref(array2->element_type);
1066 if (!types_compatible(element_type1, element_type2))
1069 if (!array1->size_constant || !array2->size_constant)
1072 return array1->size == array2->size;
1076 * Check if two types are compatible.
1078 bool types_compatible(const type_t *type1, const type_t *type2)
1080 assert(!is_typeref(type1));
1081 assert(!is_typeref(type2));
1083 /* shortcut: the same type is always compatible */
1087 if (type1->base.qualifiers != type2->base.qualifiers)
1089 if (type1->kind != type2->kind)
1092 switch (type1->kind) {
1094 return function_types_compatible(&type1->function, &type2->function);
1096 return type1->atomic.akind == type2->atomic.akind;
1098 return type1->complex.akind == type2->complex.akind;
1099 case TYPE_IMAGINARY:
1100 return type1->imaginary.akind == type2->imaginary.akind;
1102 return array_types_compatible(&type1->array, &type2->array);
1104 case TYPE_POINTER: {
1105 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1106 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1107 return types_compatible(to1, to2);
1110 case TYPE_COMPOUND_STRUCT:
1111 case TYPE_COMPOUND_UNION:
1114 /* TODO: not implemented */
1118 /* not sure if this makes sense or is even needed, implement it if you
1119 * really need it! */
1120 panic("type compatibility check for bitfield type");
1123 /* Hmm, the error type should be compatible to all other types */
1126 panic("invalid type found in compatible types");
1129 panic("typerefs not skipped in compatible types?!?");
1132 /* TODO: incomplete */
1137 * Skip all typerefs and return the underlying type.
1139 type_t *skip_typeref(type_t *type)
1141 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1142 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1145 switch (type->kind) {
1148 case TYPE_TYPEDEF: {
1149 qualifiers |= type->base.qualifiers;
1150 modifiers |= type->base.modifiers;
1151 const typedef_type_t *typedef_type = &type->typedeft;
1152 if (typedef_type->resolved_type != NULL) {
1153 type = typedef_type->resolved_type;
1156 type = typedef_type->declaration->type;
1160 const typeof_type_t *typeof_type = &type->typeoft;
1161 if (typeof_type->typeof_type != NULL) {
1162 type = typeof_type->typeof_type;
1164 type = typeof_type->expression->base.type;
1174 if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1175 type_t *const copy = duplicate_type(type);
1177 /* for const with typedefed array type the element type has to be
1179 if (is_type_array(copy)) {
1180 type_t *element_type = copy->array.element_type;
1181 element_type = duplicate_type(element_type);
1182 element_type->base.qualifiers |= qualifiers;
1183 element_type->base.modifiers |= modifiers;
1184 copy->array.element_type = element_type;
1186 copy->base.qualifiers |= qualifiers;
1187 copy->base.modifiers |= modifiers;
1190 type = typehash_insert(copy);
1192 obstack_free(type_obst, copy);
1199 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1201 assert(kind <= ATOMIC_TYPE_LAST);
1202 return atomic_type_properties[kind].size;
1205 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1207 assert(kind <= ATOMIC_TYPE_LAST);
1208 return atomic_type_properties[kind].alignment;
1211 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1213 assert(kind <= ATOMIC_TYPE_LAST);
1214 return atomic_type_properties[kind].flags;
1217 atomic_type_kind_t get_intptr_kind(void)
1219 if (machine_size <= 32)
1220 return ATOMIC_TYPE_INT;
1221 else if (machine_size <= 64)
1222 return ATOMIC_TYPE_LONG;
1224 return ATOMIC_TYPE_LONGLONG;
1227 atomic_type_kind_t get_uintptr_kind(void)
1229 if (machine_size <= 32)
1230 return ATOMIC_TYPE_UINT;
1231 else if (machine_size <= 64)
1232 return ATOMIC_TYPE_ULONG;
1234 return ATOMIC_TYPE_ULONGLONG;
1238 * Find the atomic type kind representing a given size (signed).
1240 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1241 static atomic_type_kind_t kinds[32];
1244 atomic_type_kind_t kind = kinds[size];
1245 if (kind == ATOMIC_TYPE_INVALID) {
1246 static const atomic_type_kind_t possible_kinds[] = {
1251 ATOMIC_TYPE_LONGLONG
1253 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1254 if (get_atomic_type_size(possible_kinds[i]) == size) {
1255 kind = possible_kinds[i];
1265 * Find the atomic type kind representing a given size (signed).
1267 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1268 static atomic_type_kind_t kinds[32];
1271 atomic_type_kind_t kind = kinds[size];
1272 if (kind == ATOMIC_TYPE_INVALID) {
1273 static const atomic_type_kind_t possible_kinds[] = {
1278 ATOMIC_TYPE_ULONGLONG
1280 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1281 if (get_atomic_type_size(possible_kinds[i]) == size) {
1282 kind = possible_kinds[i];
1292 * Hash the given type and return the "singleton" version
1295 static type_t *identify_new_type(type_t *type)
1297 type_t *result = typehash_insert(type);
1298 if (result != type) {
1299 obstack_free(type_obst, type);
1305 * Creates a new atomic type.
1307 * @param akind The kind of the atomic type.
1308 * @param qualifiers Type qualifiers for the new type.
1310 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1312 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1313 memset(type, 0, sizeof(atomic_type_t));
1315 type->kind = TYPE_ATOMIC;
1316 type->base.size = get_atomic_type_size(akind);
1317 type->base.alignment = get_atomic_type_alignment(akind);
1318 type->base.qualifiers = qualifiers;
1319 type->atomic.akind = akind;
1321 return identify_new_type(type);
1325 * Creates a new complex type.
1327 * @param akind The kind of the atomic type.
1328 * @param qualifiers Type qualifiers for the new type.
1330 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1332 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1333 memset(type, 0, sizeof(complex_type_t));
1335 type->kind = TYPE_COMPLEX;
1336 type->base.qualifiers = qualifiers;
1337 type->base.alignment = get_atomic_type_alignment(akind);
1338 type->complex.akind = akind;
1340 return identify_new_type(type);
1344 * Creates a new imaginary type.
1346 * @param akind The kind of the atomic type.
1347 * @param qualifiers Type qualifiers for the new type.
1349 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1351 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1352 memset(type, 0, sizeof(imaginary_type_t));
1354 type->kind = TYPE_IMAGINARY;
1355 type->base.qualifiers = qualifiers;
1356 type->base.alignment = get_atomic_type_alignment(akind);
1357 type->imaginary.akind = akind;
1359 return identify_new_type(type);
1363 * Creates a new pointer type.
1365 * @param points_to The points-to type for the new type.
1366 * @param qualifiers Type qualifiers for the new type.
1368 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1370 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1371 memset(type, 0, sizeof(pointer_type_t));
1373 type->kind = TYPE_POINTER;
1374 type->base.qualifiers = qualifiers;
1375 type->base.alignment = 0;
1376 type->pointer.points_to = points_to;
1378 return identify_new_type(type);
1381 type_t *make_array_type(type_t *element_type, size_t size,
1382 type_qualifiers_t qualifiers)
1384 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1385 memset(type, 0, sizeof(array_type_t));
1387 type->kind = TYPE_ARRAY;
1388 type->base.qualifiers = qualifiers;
1389 type->base.alignment = 0;
1390 type->array.element_type = element_type;
1391 type->array.size = size;
1392 type->array.size_constant = true;
1394 return identify_new_type(type);
1398 * Debug helper. Prints the given type to stdout.
1400 static __attribute__((unused))
1401 void dbg_type(const type_t *type)
1403 FILE *old_out = out;