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] = {
123 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
124 | ATOMIC_TYPE_FLAG_SIGNED,
126 [ATOMIC_TYPE_DOUBLE] = {
129 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
130 | ATOMIC_TYPE_FLAG_SIGNED,
132 [ATOMIC_TYPE_LONG_DOUBLE] = {
135 .flags = ATOMIC_TYPE_FLAG_INTEGER | 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;
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_LONGLONG].alignment = 4;
173 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
175 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UINT];
177 /* initialize complex/imaginary types */
178 props[ATOMIC_TYPE_FLOAT_COMPLEX] = props[ATOMIC_TYPE_FLOAT];
179 props[ATOMIC_TYPE_FLOAT_COMPLEX].flags |= ATOMIC_TYPE_FLAG_COMPLEX;
180 props[ATOMIC_TYPE_FLOAT_COMPLEX].size *= 2;
181 props[ATOMIC_TYPE_DOUBLE_COMPLEX] = props[ATOMIC_TYPE_DOUBLE];
182 props[ATOMIC_TYPE_DOUBLE_COMPLEX].flags |= ATOMIC_TYPE_FLAG_COMPLEX;
183 props[ATOMIC_TYPE_DOUBLE_COMPLEX].size *= 2;
184 props[ATOMIC_TYPE_LONG_DOUBLE_COMPLEX]
185 = props[ATOMIC_TYPE_LONG_DOUBLE];
186 props[ATOMIC_TYPE_LONG_DOUBLE_COMPLEX].flags |= ATOMIC_TYPE_FLAG_COMPLEX;
187 props[ATOMIC_TYPE_LONG_DOUBLE_COMPLEX].size *= 2;
189 props[ATOMIC_TYPE_FLOAT_IMAGINARY] = props[ATOMIC_TYPE_FLOAT];
190 props[ATOMIC_TYPE_DOUBLE_IMAGINARY] = props[ATOMIC_TYPE_DOUBLE];
191 props[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = props[ATOMIC_TYPE_LONG_DOUBLE];
194 void exit_types(void)
196 obstack_free(type_obst, NULL);
199 void type_set_output(FILE *stream)
204 void inc_type_visited(void)
209 void print_type_qualifiers(type_qualifiers_t qualifiers)
211 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
212 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
213 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
217 * Prints the name of a atomic type.
219 * @param type The type.
222 void print_atomic_type(const atomic_type_t *type)
224 print_type_qualifiers(type->type.qualifiers);
226 const char *s = "INVALIDATOMIC";
227 switch((atomic_type_kind_t) type->akind) {
228 case ATOMIC_TYPE_INVALID: break;
229 case ATOMIC_TYPE_VOID: s = "void"; break;
230 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
231 case ATOMIC_TYPE_CHAR: s = "char"; break;
232 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
233 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
234 case ATOMIC_TYPE_INT: s = "int"; break;
235 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
236 case ATOMIC_TYPE_SHORT: s = "short"; break;
237 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
238 case ATOMIC_TYPE_LONG: s = "long"; break;
239 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
240 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
241 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
242 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
243 case ATOMIC_TYPE_FLOAT: s = "float"; break;
244 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
245 case ATOMIC_TYPE_FLOAT_COMPLEX: s = "_Complex float"; break;
246 case ATOMIC_TYPE_DOUBLE_COMPLEX: s = "_Complex float"; break;
247 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX: s = "_Complex float"; break;
248 case ATOMIC_TYPE_FLOAT_IMAGINARY: s = "_Imaginary float"; break;
249 case ATOMIC_TYPE_DOUBLE_IMAGINARY: s = "_Imaginary float"; break;
250 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY: s = "_Imaginary float"; break;
256 * Print the first part (the prefix) of a type.
258 * @param type The type to print.
259 * @param top true, if this is the top type, false if it's an embedded type.
261 static void print_function_type_pre(const function_type_t *type, bool top)
263 print_type_qualifiers(type->type.qualifiers);
265 intern_print_type_pre(type->return_type, false);
267 /* don't emit braces if we're the toplevel type... */
273 * Print the second part (the postfix) of a type.
275 * @param type The type to print.
276 * @param top true, if this is the top type, false if it's an embedded type.
278 static void print_function_type_post(const function_type_t *type,
279 const scope_t *scope, bool top)
281 intern_print_type_post(type->return_type, false);
282 /* don't emit braces if we're the toplevel type... */
290 function_parameter_t *parameter = type->parameters;
291 for( ; parameter != NULL; parameter = parameter->next) {
297 print_type(parameter->type);
300 declaration_t *parameter = scope->declarations;
301 for( ; parameter != NULL; parameter = parameter->next) {
307 print_type_ext(parameter->type, parameter->symbol,
319 if(first && !type->unspecified_parameters) {
326 * Prints the prefix part of a pointer type.
328 * @param type The pointer type.
330 static void print_pointer_type_pre(const pointer_type_t *type)
332 intern_print_type_pre(type->points_to, false);
334 print_type_qualifiers(type->type.qualifiers);
338 * Prints the postfix part of a pointer type.
340 * @param type The pointer type.
342 static void print_pointer_type_post(const pointer_type_t *type)
344 intern_print_type_post(type->points_to, false);
348 * Prints the prefix part of an array type.
350 * @param type The array type.
352 static void print_array_type_pre(const array_type_t *type)
354 intern_print_type_pre(type->element_type, false);
358 * Prints the postfix part of an array type.
360 * @param type The array type.
362 static void print_array_type_post(const array_type_t *type)
365 if(type->is_static) {
366 fputs("static ", out);
368 print_type_qualifiers(type->type.qualifiers);
369 if(type->size_expression != NULL
370 && (print_implicit_array_size || !type->has_implicit_size)) {
371 print_expression(type->size_expression);
374 intern_print_type_post(type->element_type, false);
378 * Prints the postfix part of a bitfield type.
380 * @param type The array type.
382 static void print_bitfield_type_post(const bitfield_type_t *type)
385 print_expression(type->size);
386 intern_print_type_post(type->base, false);
390 * Prints an enum definition.
392 * @param declaration The enum's type declaration.
394 void print_enum_definition(const declaration_t *declaration)
400 declaration_t *entry = declaration->next;
401 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
402 entry = entry->next) {
405 fprintf(out, "%s", entry->symbol->string);
406 if(entry->init.initializer != NULL) {
409 /* skip the implicit cast */
410 expression_t *expression = entry->init.enum_value;
411 if(expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
412 expression = expression->unary.value;
414 print_expression(expression);
425 * Prints an enum type.
427 * @param type The enum type.
429 static void print_type_enum(const enum_type_t *type)
431 print_type_qualifiers(type->type.qualifiers);
434 declaration_t *declaration = type->declaration;
435 symbol_t *symbol = declaration->symbol;
437 fputs(symbol->string, out);
439 print_enum_definition(declaration);
444 * Print the compound part of a compound type.
446 * @param declaration The declaration of the compound type.
448 void print_compound_definition(const declaration_t *declaration)
453 declaration_t *iter = declaration->scope.declarations;
454 for( ; iter != NULL; iter = iter->next) {
456 print_declaration(iter);
466 * Prints a compound type.
468 * @param type The compound type.
470 static void print_compound_type(const compound_type_t *type)
472 print_type_qualifiers(type->type.qualifiers);
474 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
475 fputs("struct ", out);
477 assert(type->type.kind == TYPE_COMPOUND_UNION);
478 fputs("union ", out);
481 declaration_t *declaration = type->declaration;
482 symbol_t *symbol = declaration->symbol;
484 fputs(symbol->string, out);
486 print_compound_definition(declaration);
491 * Prints the prefix part of a typedef type.
493 * @param type The typedef type.
495 static void print_typedef_type_pre(const typedef_type_t *const type)
497 print_type_qualifiers(type->type.qualifiers);
498 fputs(type->declaration->symbol->string, out);
502 * Prints the prefix part of a typeof type.
504 * @param type The typeof type.
506 static void print_typeof_type_pre(const typeof_type_t *const type)
508 fputs("typeof(", out);
509 if(type->expression != NULL) {
510 assert(type->typeof_type == NULL);
511 print_expression(type->expression);
513 print_type(type->typeof_type);
519 * Prints the prefix part of a type.
521 * @param type The type.
522 * @param top true if we print the toplevel type, false else.
524 static void intern_print_type_pre(const type_t *const type, const bool top)
528 fputs("<error>", out);
530 fputs("<invalid>", out);
533 print_type_enum(&type->enumt);
536 print_atomic_type(&type->atomic);
538 case TYPE_COMPOUND_STRUCT:
539 case TYPE_COMPOUND_UNION:
540 print_compound_type(&type->compound);
543 fputs(type->builtin.symbol->string, out);
546 print_function_type_pre(&type->function, top);
549 print_pointer_type_pre(&type->pointer);
552 intern_print_type_pre(type->bitfield.base, top);
555 print_array_type_pre(&type->array);
558 print_typedef_type_pre(&type->typedeft);
561 print_typeof_type_pre(&type->typeoft);
564 fputs("unknown", out);
568 * Prints the postfix part of a type.
570 * @param type The type.
571 * @param top true if we print the toplevel type, false else.
573 static void intern_print_type_post(const type_t *const type, const bool top)
577 print_function_type_post(&type->function, NULL, top);
580 print_pointer_type_post(&type->pointer);
583 print_array_type_post(&type->array);
586 print_bitfield_type_post(&type->bitfield);
592 case TYPE_COMPOUND_STRUCT:
593 case TYPE_COMPOUND_UNION:
604 * @param type The type.
606 void print_type(const type_t *const type)
608 print_type_ext(type, NULL, NULL);
611 void print_type_ext(const type_t *const type, const symbol_t *symbol,
612 const scope_t *scope)
615 fputs("nil type", out);
619 intern_print_type_pre(type, true);
622 fputs(symbol->string, out);
624 if(type->kind == TYPE_FUNCTION) {
625 print_function_type_post(&type->function, scope, true);
627 intern_print_type_post(type, true);
632 * Return the size of a type AST node.
634 * @param type The type.
636 static size_t get_type_size(const type_t *type)
639 case TYPE_ATOMIC: return sizeof(atomic_type_t);
640 case TYPE_COMPOUND_STRUCT:
641 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
642 case TYPE_ENUM: return sizeof(enum_type_t);
643 case TYPE_FUNCTION: return sizeof(function_type_t);
644 case TYPE_POINTER: return sizeof(pointer_type_t);
645 case TYPE_ARRAY: return sizeof(array_type_t);
646 case TYPE_BUILTIN: return sizeof(builtin_type_t);
647 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
648 case TYPE_TYPEOF: return sizeof(typeof_type_t);
649 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
650 case TYPE_ERROR: panic("error type found");
651 case TYPE_INVALID: panic("invalid type found");
653 panic("unknown type found");
659 * @param type The type to copy.
660 * @return A copy of the type.
662 * @note This does not produce a deep copy!
664 type_t *duplicate_type(const type_t *type)
666 size_t size = get_type_size(type);
668 type_t *copy = obstack_alloc(type_obst, size);
669 memcpy(copy, type, size);
675 * Returns the unqualified type of a given type.
677 * @param type The type.
678 * @returns The unqualified type.
680 type_t *get_unqualified_type(type_t *type)
682 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
685 type_t *unqualified_type = duplicate_type(type);
686 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
688 type_t *result = typehash_insert(unqualified_type);
689 if(result != unqualified_type) {
690 obstack_free(type_obst, unqualified_type);
697 * Check if a type is valid.
699 * @param type The type to check.
700 * @return true if type represents a valid type.
702 bool type_valid(const type_t *type)
704 return type->kind != TYPE_INVALID;
707 static bool test_atomic_type_flag(atomic_type_kind_t kind,
708 atomic_type_flag_t flag)
710 assert(kind <= ATOMIC_TYPE_LAST);
711 return (atomic_type_properties[kind].flags & flag) != 0;
715 * Returns true if the given type is an integer type.
717 * @param type The type to check.
718 * @return True if type is an integer type.
720 bool is_type_integer(const type_t *type)
722 assert(!is_typeref(type));
724 if(type->kind == TYPE_ENUM)
727 if(type->kind != TYPE_ATOMIC)
730 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
734 * Returns true if the given type is an floating point type.
736 * @param type The type to check.
737 * @return True if type is a floating point type.
739 bool is_type_float(const type_t *type)
741 assert(!is_typeref(type));
743 if(type->kind != TYPE_ATOMIC)
746 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
750 * Returns true if the given type is a signed type.
752 * @param type The type to check.
753 * @return True if type is a signed type.
755 bool is_type_signed(const type_t *type)
757 assert(!is_typeref(type));
759 /* enum types are int for now */
760 if(type->kind == TYPE_ENUM)
763 if(type->kind != TYPE_ATOMIC)
766 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
770 * Returns true if the given type represents an arithmetic type.
772 * @param type The type to check.
773 * @return True if type represents an arithmetic type.
775 bool is_type_arithmetic(const type_t *type)
777 assert(!is_typeref(type));
779 if(type->kind == TYPE_BITFIELD || type->kind == TYPE_ENUM)
781 if(type->kind != TYPE_ATOMIC)
784 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
788 * Returns true if the given type represents a scalar type.
790 * @param type The type to check.
791 * @return True if type represents a scalar type.
793 bool is_type_scalar(const type_t *type)
795 assert(!is_typeref(type));
797 switch (type->kind) {
798 case TYPE_POINTER: return true;
799 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
803 return is_type_arithmetic(type);
807 * Check if a given type is incomplete.
809 * @param type The type to check.
810 * @return True if the given type is incomplete (ie. just forward).
812 bool is_type_incomplete(const type_t *type)
814 assert(!is_typeref(type));
817 case TYPE_COMPOUND_STRUCT:
818 case TYPE_COMPOUND_UNION: {
819 const compound_type_t *compound_type = &type->compound;
820 declaration_t *declaration = compound_type->declaration;
821 return !declaration->init.is_defined;
824 const enum_type_t *enum_type = &type->enumt;
825 declaration_t *declaration = enum_type->declaration;
826 return !declaration->init.is_defined;
833 return type->array.size_expression == NULL;
836 return type->atomic.akind == ATOMIC_TYPE_VOID;
845 panic("is_type_incomplete called without typerefs skipped");
850 panic("invalid type found");
854 * Check if two function types are compatible.
856 static bool function_types_compatible(const function_type_t *func1,
857 const function_type_t *func2)
859 const type_t* const ret1 = skip_typeref(func1->return_type);
860 const type_t* const ret2 = skip_typeref(func2->return_type);
861 if (!types_compatible(ret1, ret2))
864 /* can parameters be compared? */
865 if(func1->unspecified_parameters || func2->unspecified_parameters)
868 if(func1->variadic != func2->variadic)
871 /* TODO: handling of unspecified parameters not correct yet */
873 /* all argument types must be compatible */
874 function_parameter_t *parameter1 = func1->parameters;
875 function_parameter_t *parameter2 = func2->parameters;
876 for( ; parameter1 != NULL && parameter2 != NULL;
877 parameter1 = parameter1->next, parameter2 = parameter2->next) {
878 type_t *parameter1_type = skip_typeref(parameter1->type);
879 type_t *parameter2_type = skip_typeref(parameter2->type);
881 parameter1_type = get_unqualified_type(parameter1_type);
882 parameter2_type = get_unqualified_type(parameter2_type);
884 if(!types_compatible(parameter1_type, parameter2_type))
887 /* same number of arguments? */
888 if(parameter1 != NULL || parameter2 != NULL)
895 * Check if two array types are compatible.
897 static bool array_types_compatible(const array_type_t *array1,
898 const array_type_t *array2)
900 type_t *element_type1 = skip_typeref(array1->element_type);
901 type_t *element_type2 = skip_typeref(array2->element_type);
902 if(!types_compatible(element_type1, element_type2))
905 if(!array1->size_constant || !array2->size_constant)
908 return array1->size == array2->size;
912 * Check if two types are compatible.
914 bool types_compatible(const type_t *type1, const type_t *type2)
916 assert(!is_typeref(type1));
917 assert(!is_typeref(type2));
919 /* shortcut: the same type is always compatible */
923 if(type1->base.qualifiers != type2->base.qualifiers)
925 if(type1->kind != type2->kind)
928 switch(type1->kind) {
930 return function_types_compatible(&type1->function, &type2->function);
932 return type1->atomic.akind == type2->atomic.akind;
934 return array_types_compatible(&type1->array, &type2->array);
937 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
938 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
939 return types_compatible(to1, to2);
942 case TYPE_COMPOUND_STRUCT:
943 case TYPE_COMPOUND_UNION:
946 /* TODO: not implemented */
950 /* not sure if this makes sense or is even needed, implement it if you
952 panic("type compatibility check for bitfield type");
955 /* Hmm, the error type should be compatible to all other types */
958 panic("invalid type found in compatible types");
961 panic("typerefs not skipped in compatible types?!?");
964 /* TODO: incomplete */
969 * Check if two pointer types are compatible.
971 bool pointers_compatible(const type_t *type1, const type_t *type2)
973 assert(!is_typeref(type1));
974 assert(!is_typeref(type2));
976 assert(type1->kind == TYPE_POINTER);
977 assert(type2->kind == TYPE_POINTER);
985 * Skip all typerefs and return the underlying type.
987 type_t *skip_typeref(type_t *type)
989 unsigned qualifiers = TYPE_QUALIFIER_NONE;
996 qualifiers |= type->base.qualifiers;
997 const typedef_type_t *typedef_type = &type->typedeft;
998 if(typedef_type->resolved_type != NULL) {
999 type = typedef_type->resolved_type;
1002 type = typedef_type->declaration->type;
1006 const typeof_type_t *typeof_type = &type->typeoft;
1007 if(typeof_type->typeof_type != NULL) {
1008 type = typeof_type->typeof_type;
1010 type = typeof_type->expression->base.type;
1020 if (qualifiers != TYPE_QUALIFIER_NONE) {
1021 type_t *const copy = duplicate_type(type);
1022 copy->base.qualifiers |= qualifiers;
1024 type = typehash_insert(copy);
1026 obstack_free(type_obst, copy);
1033 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1035 assert(kind <= ATOMIC_TYPE_LAST);
1036 return atomic_type_properties[kind].size;
1039 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1041 assert(kind <= ATOMIC_TYPE_LAST);
1042 return atomic_type_properties[kind].alignment;
1045 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1047 assert(kind <= ATOMIC_TYPE_LAST);
1048 return atomic_type_properties[kind].flags;
1052 * Find the atomic type kind representing a given size (signed).
1054 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1055 static atomic_type_kind_t kinds[32];
1058 atomic_type_kind_t kind = kinds[size];
1059 if(kind == ATOMIC_TYPE_INVALID) {
1060 static const atomic_type_kind_t possible_kinds[] = {
1065 ATOMIC_TYPE_LONGLONG
1067 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1068 if(get_atomic_type_size(possible_kinds[i]) == size) {
1069 kind = possible_kinds[i];
1079 * Find the atomic type kind representing a given size (signed).
1081 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1082 static atomic_type_kind_t kinds[32];
1085 atomic_type_kind_t kind = kinds[size];
1086 if(kind == ATOMIC_TYPE_INVALID) {
1087 static const atomic_type_kind_t possible_kinds[] = {
1092 ATOMIC_TYPE_ULONGLONG
1094 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1095 if(get_atomic_type_size(possible_kinds[i]) == size) {
1096 kind = possible_kinds[i];
1106 * Hash the given type and return the "singleton" version
1109 static type_t *identify_new_type(type_t *type)
1111 type_t *result = typehash_insert(type);
1112 if(result != type) {
1113 obstack_free(type_obst, type);
1119 * Creates a new atomic type.
1121 * @param akind The kind of the atomic type.
1122 * @param qualifiers Type qualifiers for the new type.
1124 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
1126 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1127 memset(type, 0, sizeof(atomic_type_t));
1129 type->kind = TYPE_ATOMIC;
1130 type->base.qualifiers = qualifiers;
1131 type->base.alignment = 0;
1132 type->atomic.akind = atype;
1134 /* TODO: set the alignment depending on the atype here */
1136 return identify_new_type(type);
1140 * Creates a new pointer type.
1142 * @param points_to The points-to type for the new type.
1143 * @param qualifiers Type qualifiers for the new type.
1145 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1147 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1148 memset(type, 0, sizeof(pointer_type_t));
1150 type->kind = TYPE_POINTER;
1151 type->base.qualifiers = qualifiers;
1152 type->base.alignment = 0;
1153 type->pointer.points_to = points_to;
1155 return identify_new_type(type);
1158 type_t *make_array_type(type_t *element_type, size_t size,
1159 type_qualifiers_t qualifiers)
1161 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1162 memset(type, 0, sizeof(array_type_t));
1164 type->kind = TYPE_ARRAY;
1165 type->base.qualifiers = qualifiers;
1166 type->base.alignment = 0;
1167 type->array.element_type = element_type;
1168 type->array.size = size;
1169 type->array.size_constant = true;
1171 return identify_new_type(type);
1175 * Debug helper. Prints the given type to stdout.
1177 static __attribute__((unused))
1178 void dbg_type(const type_t *type)
1180 FILE *old_out = out;