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 /* TODO: make this configurable for platforms which do not use byte sized
178 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
181 void exit_types(void)
183 obstack_free(type_obst, NULL);
186 void type_set_output(FILE *stream)
191 void inc_type_visited(void)
196 void print_type_qualifiers(type_qualifiers_t qualifiers)
199 if (qualifiers & TYPE_QUALIFIER_CONST) {
200 fputs(" const" + first, out);
203 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
204 fputs(" volatile" + first, out);
207 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
208 fputs(" restrict" + first, out);
214 * Prints the name of an atomic type kinds.
216 * @param kind The type kind.
219 void print_atomic_kinds(atomic_type_kind_t kind)
221 const char *s = "INVALIDATOMIC";
223 case ATOMIC_TYPE_INVALID: break;
224 case ATOMIC_TYPE_VOID: s = "void"; break;
225 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
226 case ATOMIC_TYPE_CHAR: s = "char"; break;
227 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
228 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
229 case ATOMIC_TYPE_INT: s = "int"; break;
230 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
231 case ATOMIC_TYPE_SHORT: s = "short"; break;
232 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
233 case ATOMIC_TYPE_LONG: s = "long"; break;
234 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
235 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
236 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
237 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
238 case ATOMIC_TYPE_FLOAT: s = "float"; break;
239 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
245 * Prints the name of an atomic type.
247 * @param type The type.
250 void print_atomic_type(const atomic_type_t *type)
252 print_type_qualifiers(type->base.qualifiers);
253 if (type->base.qualifiers != 0)
255 print_atomic_kinds(type->akind);
259 * Prints the name of a complex type.
261 * @param type The type.
264 void print_complex_type(const complex_type_t *type)
266 int empty = type->base.qualifiers == 0;
267 print_type_qualifiers(type->base.qualifiers);
268 fputs(" _Complex " + empty, out);
269 print_atomic_kinds(type->akind);
273 * Prints the name of an imaginary type.
275 * @param type The type.
278 void print_imaginary_type(const imaginary_type_t *type)
280 int empty = type->base.qualifiers == 0;
281 print_type_qualifiers(type->base.qualifiers);
282 fputs(" _Imaginary " + empty, out);
283 print_atomic_kinds(type->akind);
287 * Print the first part (the prefix) of a type.
289 * @param type The type to print.
290 * @param top true, if this is the top type, false if it's an embedded type.
292 static void print_function_type_pre(const function_type_t *type, bool top)
294 print_type_qualifiers(type->base.qualifiers);
295 if (type->base.qualifiers != 0)
299 intern_print_type_pre(type->return_type, false);
301 switch (type->calling_convention) {
303 fputs("__cdecl ", out);
306 fputs("__stdcall ", out);
309 fputs("__fastcall ", out);
312 fputs("__thiscall ", out);
318 /* don't emit parenthesis if we're the toplevel type... */
324 * Print the second part (the postfix) of a type.
326 * @param type The type to print.
327 * @param top true, if this is the top type, false if it's an embedded type.
329 static void print_function_type_post(const function_type_t *type,
330 const scope_t *scope, bool top)
332 /* don't emit parenthesis if we're the toplevel type... */
339 function_parameter_t *parameter = type->parameters;
340 for( ; parameter != NULL; parameter = parameter->next) {
346 print_type(parameter->type);
349 declaration_t *parameter = scope->declarations;
350 for( ; parameter != NULL; parameter = parameter->next) {
356 print_type_ext(parameter->type, parameter->symbol,
360 if (type->variadic) {
368 if (first && !type->unspecified_parameters) {
373 intern_print_type_post(type->return_type, false);
377 * Prints the prefix part of a pointer type.
379 * @param type The pointer type.
381 static void print_pointer_type_pre(const pointer_type_t *type)
383 intern_print_type_pre(type->points_to, false);
385 print_type_qualifiers(type->base.qualifiers);
386 if (type->base.qualifiers != 0)
391 * Prints the postfix part of a pointer type.
393 * @param type The pointer type.
395 static void print_pointer_type_post(const pointer_type_t *type)
397 intern_print_type_post(type->points_to, false);
401 * Prints the prefix part of an array type.
403 * @param type The array type.
405 static void print_array_type_pre(const array_type_t *type)
407 intern_print_type_pre(type->element_type, false);
411 * Prints the postfix part of an array type.
413 * @param type The array type.
415 static void print_array_type_post(const array_type_t *type)
418 if (type->is_static) {
419 fputs("static ", out);
421 print_type_qualifiers(type->base.qualifiers);
422 if (type->base.qualifiers != 0)
424 if (type->size_expression != NULL
425 && (print_implicit_array_size || !type->has_implicit_size)) {
426 print_expression(type->size_expression);
429 intern_print_type_post(type->element_type, false);
433 * Prints the postfix part of a bitfield type.
435 * @param type The array type.
437 static void print_bitfield_type_post(const bitfield_type_t *type)
440 print_expression(type->size_expression);
441 intern_print_type_post(type->base_type, false);
445 * Prints an enum definition.
447 * @param declaration The enum's type declaration.
449 void print_enum_definition(const declaration_t *declaration)
455 declaration_t *entry = declaration->next;
456 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
457 entry = entry->next) {
460 fprintf(out, "%s", entry->symbol->string);
461 if (entry->init.initializer != NULL) {
464 /* skip the implicit cast */
465 expression_t *expression = entry->init.enum_value;
466 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
467 expression = expression->unary.value;
469 print_expression(expression);
480 * Prints an enum type.
482 * @param type The enum type.
484 static void print_type_enum(const enum_type_t *type)
486 int empty = type->base.qualifiers == 0;
487 print_type_qualifiers(type->base.qualifiers);
488 fputs(" enum " + empty, out);
490 declaration_t *declaration = type->declaration;
491 symbol_t *symbol = declaration->symbol;
492 if (symbol != NULL) {
493 fputs(symbol->string, out);
495 print_enum_definition(declaration);
500 * Print the compound part of a compound type.
502 * @param declaration The declaration of the compound type.
504 void print_compound_definition(const declaration_t *declaration)
509 declaration_t *iter = declaration->scope.declarations;
510 for( ; iter != NULL; iter = iter->next) {
512 print_declaration(iter);
522 * Prints a compound type.
524 * @param type The compound type.
526 static void print_compound_type(const compound_type_t *type)
528 int empty = type->base.qualifiers == 0;
529 print_type_qualifiers(type->base.qualifiers);
531 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
532 fputs(" struct " + empty, out);
534 assert(type->base.kind == TYPE_COMPOUND_UNION);
535 fputs(" union " + empty, out);
538 declaration_t *declaration = type->declaration;
539 symbol_t *symbol = declaration->symbol;
540 if (symbol != NULL) {
541 fputs(symbol->string, out);
543 print_compound_definition(declaration);
548 * Prints the prefix part of a typedef type.
550 * @param type The typedef type.
552 static void print_typedef_type_pre(const typedef_type_t *const type)
554 print_type_qualifiers(type->base.qualifiers);
555 if (type->base.qualifiers != 0)
557 fputs(type->declaration->symbol->string, out);
561 * Prints the prefix part of a typeof type.
563 * @param type The typeof type.
565 static void print_typeof_type_pre(const typeof_type_t *const type)
567 fputs("typeof(", out);
568 if (type->expression != NULL) {
569 assert(type->typeof_type == NULL);
570 print_expression(type->expression);
572 print_type(type->typeof_type);
578 * Prints the prefix part of a type.
580 * @param type The type.
581 * @param top true if we print the toplevel type, false else.
583 static void intern_print_type_pre(const type_t *const type, const bool top)
587 fputs("<error>", out);
590 fputs("<invalid>", out);
593 print_type_enum(&type->enumt);
596 print_atomic_type(&type->atomic);
599 print_complex_type(&type->complex);
602 print_imaginary_type(&type->imaginary);
604 case TYPE_COMPOUND_STRUCT:
605 case TYPE_COMPOUND_UNION:
606 print_compound_type(&type->compound);
609 fputs(type->builtin.symbol->string, out);
612 print_function_type_pre(&type->function, top);
615 print_pointer_type_pre(&type->pointer);
618 intern_print_type_pre(type->bitfield.base_type, top);
621 print_array_type_pre(&type->array);
624 print_typedef_type_pre(&type->typedeft);
627 print_typeof_type_pre(&type->typeoft);
630 fputs("unknown", out);
634 * Prints the postfix part of a type.
636 * @param type The type.
637 * @param top true if we print the toplevel type, false else.
639 static void intern_print_type_post(const type_t *const type, const bool top)
643 print_function_type_post(&type->function, NULL, top);
646 print_pointer_type_post(&type->pointer);
649 print_array_type_post(&type->array);
652 print_bitfield_type_post(&type->bitfield);
660 case TYPE_COMPOUND_STRUCT:
661 case TYPE_COMPOUND_UNION:
672 * @param type The type.
674 void print_type(const type_t *const type)
676 print_type_ext(type, NULL, NULL);
679 void print_type_ext(const type_t *const type, const symbol_t *symbol,
680 const scope_t *scope)
683 fputs("nil type", out);
687 intern_print_type_pre(type, true);
688 if (symbol != NULL) {
690 fputs(symbol->string, out);
692 if (type->kind == TYPE_FUNCTION) {
693 print_function_type_post(&type->function, scope, true);
695 intern_print_type_post(type, true);
700 * Return the size of a type AST node.
702 * @param type The type.
704 static size_t get_type_size(const type_t *type)
707 case TYPE_ATOMIC: return sizeof(atomic_type_t);
708 case TYPE_COMPLEX: return sizeof(complex_type_t);
709 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
710 case TYPE_COMPOUND_STRUCT:
711 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
712 case TYPE_ENUM: return sizeof(enum_type_t);
713 case TYPE_FUNCTION: return sizeof(function_type_t);
714 case TYPE_POINTER: return sizeof(pointer_type_t);
715 case TYPE_ARRAY: return sizeof(array_type_t);
716 case TYPE_BUILTIN: return sizeof(builtin_type_t);
717 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
718 case TYPE_TYPEOF: return sizeof(typeof_type_t);
719 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
720 case TYPE_ERROR: panic("error type found");
721 case TYPE_INVALID: panic("invalid type found");
723 panic("unknown type found");
729 * @param type The type to copy.
730 * @return A copy of the type.
732 * @note This does not produce a deep copy!
734 type_t *duplicate_type(const type_t *type)
736 size_t size = get_type_size(type);
738 type_t *copy = obstack_alloc(type_obst, size);
739 memcpy(copy, type, size);
745 * Returns the unqualified type of a given type.
747 * @param type The type.
748 * @returns The unqualified type.
750 type_t *get_unqualified_type(type_t *type)
752 assert(!is_typeref(type));
754 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
757 type_t *unqualified_type = duplicate_type(type);
758 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
760 type_t *result = typehash_insert(unqualified_type);
761 if (result != unqualified_type) {
762 obstack_free(type_obst, unqualified_type);
768 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
770 type_t *type = skip_typeref(orig_type);
773 if (is_type_array(type)) {
774 /* For array types the element type has to be adjusted */
775 type_t *element_type = type->array.element_type;
776 type_t *qual_element_type = get_qualified_type(element_type, qual);
778 if (qual_element_type == element_type)
781 copy = duplicate_type(type);
782 copy->array.element_type = qual_element_type;
783 } else if (is_type_valid(type)) {
784 if ((type->base.qualifiers & qual) == qual)
787 copy = duplicate_type(type);
788 copy->base.qualifiers |= qual;
793 type = typehash_insert(copy);
795 obstack_free(type_obst, copy);
801 * Check if a type is valid.
803 * @param type The type to check.
804 * @return true if type represents a valid type.
806 bool type_valid(const type_t *type)
808 return type->kind != TYPE_INVALID;
811 static bool test_atomic_type_flag(atomic_type_kind_t kind,
812 atomic_type_flag_t flag)
814 assert(kind <= ATOMIC_TYPE_LAST);
815 return (atomic_type_properties[kind].flags & flag) != 0;
819 * Returns true if the given type is an integer type.
821 * @param type The type to check.
822 * @return True if type is an integer type.
824 bool is_type_integer(const type_t *type)
826 assert(!is_typeref(type));
828 if (type->kind == TYPE_ENUM)
830 if (type->kind == TYPE_BITFIELD)
833 if (type->kind != TYPE_ATOMIC)
836 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
840 * Returns true if the given type is an enum type.
842 * @param type The type to check.
843 * @return True if type is an enum type.
845 bool is_type_enum(const type_t *type)
847 assert(!is_typeref(type));
848 return type->kind == TYPE_ENUM;
852 * Returns true if the given type is an floating point type.
854 * @param type The type to check.
855 * @return True if type is a floating point type.
857 bool is_type_float(const type_t *type)
859 assert(!is_typeref(type));
861 if (type->kind != TYPE_ATOMIC)
864 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
868 * Returns true if the given type is an complex type.
870 * @param type The type to check.
871 * @return True if type is a complex type.
873 bool is_type_complex(const type_t *type)
875 assert(!is_typeref(type));
877 if (type->kind != TYPE_ATOMIC)
880 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
884 * Returns true if the given type is a signed type.
886 * @param type The type to check.
887 * @return True if type is a signed type.
889 bool is_type_signed(const type_t *type)
891 assert(!is_typeref(type));
893 /* enum types are int for now */
894 if (type->kind == TYPE_ENUM)
896 if (type->kind == TYPE_BITFIELD)
897 return is_type_signed(type->bitfield.base_type);
899 if (type->kind != TYPE_ATOMIC)
902 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
906 * Returns true if the given type represents an arithmetic type.
908 * @param type The type to check.
909 * @return True if type represents an arithmetic type.
911 bool is_type_arithmetic(const type_t *type)
913 assert(!is_typeref(type));
920 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
922 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
924 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
931 * Returns true if the given type is an integer or float type.
933 * @param type The type to check.
934 * @return True if type is an integer or float type.
936 bool is_type_real(const type_t *type)
939 return is_type_integer(type) || is_type_float(type);
943 * Returns true if the given type represents a scalar type.
945 * @param type The type to check.
946 * @return True if type represents a scalar type.
948 bool is_type_scalar(const type_t *type)
950 assert(!is_typeref(type));
952 switch (type->kind) {
953 case TYPE_POINTER: return true;
954 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
958 return is_type_arithmetic(type);
962 * Check if a given type is incomplete.
964 * @param type The type to check.
965 * @return True if the given type is incomplete (ie. just forward).
967 bool is_type_incomplete(const type_t *type)
969 assert(!is_typeref(type));
972 case TYPE_COMPOUND_STRUCT:
973 case TYPE_COMPOUND_UNION: {
974 const compound_type_t *compound_type = &type->compound;
975 declaration_t *declaration = compound_type->declaration;
976 return !declaration->init.complete;
979 const enum_type_t *enum_type = &type->enumt;
980 declaration_t *declaration = enum_type->declaration;
981 return !declaration->init.complete;
985 return type->array.size_expression == NULL
986 && !type->array.size_constant;
989 return type->atomic.akind == ATOMIC_TYPE_VOID;
992 return type->complex.akind == ATOMIC_TYPE_VOID;
995 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1006 panic("is_type_incomplete called without typerefs skipped");
1011 panic("invalid type found");
1014 bool is_type_object(const type_t *type)
1016 return !is_type_function(type) && !is_type_incomplete(type);
1020 * Check if two function types are compatible.
1022 static bool function_types_compatible(const function_type_t *func1,
1023 const function_type_t *func2)
1025 const type_t* const ret1 = skip_typeref(func1->return_type);
1026 const type_t* const ret2 = skip_typeref(func2->return_type);
1027 if (!types_compatible(ret1, ret2))
1030 if (func1->calling_convention != func2->calling_convention)
1033 /* can parameters be compared? */
1034 if (func1->unspecified_parameters || func2->unspecified_parameters)
1037 if (func1->variadic != func2->variadic)
1040 /* TODO: handling of unspecified parameters not correct yet */
1042 /* all argument types must be compatible */
1043 function_parameter_t *parameter1 = func1->parameters;
1044 function_parameter_t *parameter2 = func2->parameters;
1045 for ( ; parameter1 != NULL && parameter2 != NULL;
1046 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1047 type_t *parameter1_type = skip_typeref(parameter1->type);
1048 type_t *parameter2_type = skip_typeref(parameter2->type);
1050 parameter1_type = get_unqualified_type(parameter1_type);
1051 parameter2_type = get_unqualified_type(parameter2_type);
1053 if (!types_compatible(parameter1_type, parameter2_type))
1056 /* same number of arguments? */
1057 if (parameter1 != NULL || parameter2 != NULL)
1064 * Check if two array types are compatible.
1066 static bool array_types_compatible(const array_type_t *array1,
1067 const array_type_t *array2)
1069 type_t *element_type1 = skip_typeref(array1->element_type);
1070 type_t *element_type2 = skip_typeref(array2->element_type);
1071 if (!types_compatible(element_type1, element_type2))
1074 if (!array1->size_constant || !array2->size_constant)
1077 return array1->size == array2->size;
1081 * Check if two types are compatible.
1083 bool types_compatible(const type_t *type1, const type_t *type2)
1085 assert(!is_typeref(type1));
1086 assert(!is_typeref(type2));
1088 /* shortcut: the same type is always compatible */
1092 if (!is_type_valid(type1) || !is_type_valid(type2))
1095 if (type1->base.qualifiers != type2->base.qualifiers)
1097 if (type1->kind != type2->kind)
1100 switch (type1->kind) {
1102 return function_types_compatible(&type1->function, &type2->function);
1104 return type1->atomic.akind == type2->atomic.akind;
1106 return type1->complex.akind == type2->complex.akind;
1107 case TYPE_IMAGINARY:
1108 return type1->imaginary.akind == type2->imaginary.akind;
1110 return array_types_compatible(&type1->array, &type2->array);
1112 case TYPE_POINTER: {
1113 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1114 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1115 return types_compatible(to1, to2);
1118 case TYPE_COMPOUND_STRUCT:
1119 case TYPE_COMPOUND_UNION:
1122 /* TODO: not implemented */
1126 /* not sure if this makes sense or is even needed, implement it if you
1127 * really need it! */
1128 panic("type compatibility check for bitfield type");
1131 /* Hmm, the error type should be compatible to all other types */
1134 panic("invalid type found in compatible types");
1137 panic("typerefs not skipped in compatible types?!?");
1140 /* TODO: incomplete */
1145 * Skip all typerefs and return the underlying type.
1147 type_t *skip_typeref(type_t *type)
1149 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1150 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1153 switch (type->kind) {
1156 case TYPE_TYPEDEF: {
1157 qualifiers |= type->base.qualifiers;
1158 modifiers |= type->base.modifiers;
1159 const typedef_type_t *typedef_type = &type->typedeft;
1160 if (typedef_type->resolved_type != NULL) {
1161 type = typedef_type->resolved_type;
1164 type = typedef_type->declaration->type;
1168 const typeof_type_t *typeof_type = &type->typeoft;
1169 if (typeof_type->typeof_type != NULL) {
1170 type = typeof_type->typeof_type;
1172 type = typeof_type->expression->base.type;
1182 if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1183 type_t *const copy = duplicate_type(type);
1185 /* for const with typedefed array type the element type has to be
1187 if (is_type_array(copy)) {
1188 type_t *element_type = copy->array.element_type;
1189 element_type = duplicate_type(element_type);
1190 element_type->base.qualifiers |= qualifiers;
1191 element_type->base.modifiers |= modifiers;
1192 copy->array.element_type = element_type;
1194 copy->base.qualifiers |= qualifiers;
1195 copy->base.modifiers |= modifiers;
1198 type = typehash_insert(copy);
1200 obstack_free(type_obst, copy);
1207 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type) {
1208 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1211 switch (type->base.kind) {
1213 return TYPE_QUALIFIER_NONE;
1215 qualifiers |= type->base.qualifiers;
1216 const typedef_type_t *typedef_type = &type->typedeft;
1217 if (typedef_type->resolved_type != NULL)
1218 type = typedef_type->resolved_type;
1220 type = typedef_type->declaration->type;
1223 const typeof_type_t *typeof_type = &type->typeoft;
1224 if (typeof_type->typeof_type != NULL) {
1225 type = typeof_type->typeof_type;
1227 type = typeof_type->expression->base.type;
1232 if (skip_array_type) {
1233 type = type->array.element_type;
1242 return type->base.qualifiers | qualifiers;
1245 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1247 assert(kind <= ATOMIC_TYPE_LAST);
1248 return atomic_type_properties[kind].size;
1251 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1253 assert(kind <= ATOMIC_TYPE_LAST);
1254 return atomic_type_properties[kind].alignment;
1257 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1259 assert(kind <= ATOMIC_TYPE_LAST);
1260 return atomic_type_properties[kind].flags;
1263 atomic_type_kind_t get_intptr_kind(void)
1265 if (machine_size <= 32)
1266 return ATOMIC_TYPE_INT;
1267 else if (machine_size <= 64)
1268 return ATOMIC_TYPE_LONG;
1270 return ATOMIC_TYPE_LONGLONG;
1273 atomic_type_kind_t get_uintptr_kind(void)
1275 if (machine_size <= 32)
1276 return ATOMIC_TYPE_UINT;
1277 else if (machine_size <= 64)
1278 return ATOMIC_TYPE_ULONG;
1280 return ATOMIC_TYPE_ULONGLONG;
1284 * Find the atomic type kind representing a given size (signed).
1286 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1287 static atomic_type_kind_t kinds[32];
1290 atomic_type_kind_t kind = kinds[size];
1291 if (kind == ATOMIC_TYPE_INVALID) {
1292 static const atomic_type_kind_t possible_kinds[] = {
1297 ATOMIC_TYPE_LONGLONG
1299 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1300 if (get_atomic_type_size(possible_kinds[i]) == size) {
1301 kind = possible_kinds[i];
1311 * Find the atomic type kind representing a given size (signed).
1313 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1314 static atomic_type_kind_t kinds[32];
1317 atomic_type_kind_t kind = kinds[size];
1318 if (kind == ATOMIC_TYPE_INVALID) {
1319 static const atomic_type_kind_t possible_kinds[] = {
1324 ATOMIC_TYPE_ULONGLONG
1326 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1327 if (get_atomic_type_size(possible_kinds[i]) == size) {
1328 kind = possible_kinds[i];
1338 * Hash the given type and return the "singleton" version
1341 static type_t *identify_new_type(type_t *type)
1343 type_t *result = typehash_insert(type);
1344 if (result != type) {
1345 obstack_free(type_obst, type);
1351 * Creates a new atomic type.
1353 * @param akind The kind of the atomic type.
1354 * @param qualifiers Type qualifiers for the new type.
1356 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1358 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1359 memset(type, 0, sizeof(atomic_type_t));
1361 type->kind = TYPE_ATOMIC;
1362 type->base.size = get_atomic_type_size(akind);
1363 type->base.alignment = get_atomic_type_alignment(akind);
1364 type->base.qualifiers = qualifiers;
1365 type->atomic.akind = akind;
1367 return identify_new_type(type);
1371 * Creates a new complex type.
1373 * @param akind The kind of the atomic type.
1374 * @param qualifiers Type qualifiers for the new type.
1376 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1378 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1379 memset(type, 0, sizeof(complex_type_t));
1381 type->kind = TYPE_COMPLEX;
1382 type->base.qualifiers = qualifiers;
1383 type->base.alignment = get_atomic_type_alignment(akind);
1384 type->complex.akind = akind;
1386 return identify_new_type(type);
1390 * Creates a new imaginary type.
1392 * @param akind The kind of the atomic type.
1393 * @param qualifiers Type qualifiers for the new type.
1395 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1397 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1398 memset(type, 0, sizeof(imaginary_type_t));
1400 type->kind = TYPE_IMAGINARY;
1401 type->base.qualifiers = qualifiers;
1402 type->base.alignment = get_atomic_type_alignment(akind);
1403 type->imaginary.akind = akind;
1405 return identify_new_type(type);
1409 * Creates a new pointer type.
1411 * @param points_to The points-to type for the new type.
1412 * @param qualifiers Type qualifiers for the new type.
1414 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1416 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1417 memset(type, 0, sizeof(pointer_type_t));
1419 type->kind = TYPE_POINTER;
1420 type->base.qualifiers = qualifiers;
1421 type->base.alignment = 0;
1422 type->pointer.points_to = points_to;
1424 return identify_new_type(type);
1427 type_t *make_array_type(type_t *element_type, size_t size,
1428 type_qualifiers_t qualifiers)
1430 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1431 memset(type, 0, sizeof(array_type_t));
1433 type->kind = TYPE_ARRAY;
1434 type->base.qualifiers = qualifiers;
1435 type->base.alignment = 0;
1436 type->array.element_type = element_type;
1437 type->array.size = size;
1438 type->array.size_constant = true;
1440 return identify_new_type(type);
1444 * Debug helper. Prints the given type to stdout.
1446 static __attribute__((unused))
1447 void dbg_type(const type_t *type)
1449 FILE *old_out = out;