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
28 #include "type_hash.h"
29 #include "adt/error.h"
30 #include "lang_features.h"
32 static struct obstack _type_obst;
34 struct obstack *type_obst = &_type_obst;
35 static int type_visited = 0;
36 static bool print_implicit_array_size = false;
38 static void intern_print_type_pre(const type_t *type, bool top);
39 static void intern_print_type_post(const type_t *type, bool top);
41 typedef struct atomic_type_properties_t atomic_type_properties_t;
42 struct atomic_type_properties_t {
43 unsigned size; /**< type size in bytes */
44 unsigned alignment; /**< type alignment in bytes */
45 unsigned flags; /**< type flags from atomic_type_flag_t */
48 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
49 //ATOMIC_TYPE_INVALID = 0,
50 [ATOMIC_TYPE_VOID] = {
53 .flags = ATOMIC_TYPE_FLAG_NONE
55 [ATOMIC_TYPE_CHAR] = {
58 /* signed flag will be set when known */
59 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
61 [ATOMIC_TYPE_SCHAR] = {
64 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
65 | ATOMIC_TYPE_FLAG_SIGNED,
67 [ATOMIC_TYPE_UCHAR] = {
70 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
72 [ATOMIC_TYPE_SHORT] = {
75 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
76 | ATOMIC_TYPE_FLAG_SIGNED
78 [ATOMIC_TYPE_USHORT] = {
81 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
84 .size = (unsigned) -1,
85 .alignment = (unsigned) -1,
86 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
87 | ATOMIC_TYPE_FLAG_SIGNED,
89 [ATOMIC_TYPE_UINT] = {
90 .size = (unsigned) -1,
91 .alignment = (unsigned) -1,
92 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
94 [ATOMIC_TYPE_LONG] = {
95 .size = (unsigned) -1,
96 .alignment = (unsigned) -1,
97 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
98 | ATOMIC_TYPE_FLAG_SIGNED,
100 [ATOMIC_TYPE_ULONG] = {
101 .size = (unsigned) -1,
102 .alignment = (unsigned) -1,
103 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
105 [ATOMIC_TYPE_LONGLONG] = {
106 .size = (unsigned) -1,
107 .alignment = (unsigned) -1,
108 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
109 | ATOMIC_TYPE_FLAG_SIGNED,
111 [ATOMIC_TYPE_ULONGLONG] = {
112 .size = (unsigned) -1,
113 .alignment = (unsigned) -1,
114 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
116 [ATOMIC_TYPE_BOOL] = {
117 .size = (unsigned) -1,
118 .alignment = (unsigned) -1,
119 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
121 [ATOMIC_TYPE_FLOAT] = {
123 .alignment = (unsigned) -1,
124 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
125 | ATOMIC_TYPE_FLAG_SIGNED,
127 [ATOMIC_TYPE_DOUBLE] = {
129 .alignment = (unsigned) -1,
130 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
131 | ATOMIC_TYPE_FLAG_SIGNED,
133 [ATOMIC_TYPE_LONG_DOUBLE] = {
135 .alignment = (unsigned) -1,
136 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
137 | ATOMIC_TYPE_FLAG_SIGNED,
139 /* complex and imaginary types are set in init_types */
142 void init_types(void)
144 obstack_init(type_obst);
146 atomic_type_properties_t *props = atomic_type_properties;
148 if (char_is_signed) {
149 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
152 unsigned int_size = machine_size < 32 ? 2 : 4;
153 unsigned long_size = machine_size < 64 ? 4 : 8;
154 unsigned llong_size = machine_size < 32 ? 4 : 8;
156 props[ATOMIC_TYPE_INT].size = int_size;
157 props[ATOMIC_TYPE_INT].alignment = int_size;
158 props[ATOMIC_TYPE_UINT].size = int_size;
159 props[ATOMIC_TYPE_UINT].alignment = int_size;
160 props[ATOMIC_TYPE_LONG].size = long_size;
161 props[ATOMIC_TYPE_LONG].alignment = long_size;
162 props[ATOMIC_TYPE_ULONG].size = long_size;
163 props[ATOMIC_TYPE_ULONG].alignment = long_size;
164 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
165 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
166 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
167 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
169 /* TODO: backend specific, need a way to query the backend for this.
170 * The following are good settings for x86 */
171 props[ATOMIC_TYPE_FLOAT].alignment = 4;
172 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
173 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
174 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
175 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
177 /* TODO: make this configurable for platforms which do not use byte sized
179 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
182 void exit_types(void)
184 obstack_free(type_obst, NULL);
187 void type_set_output(FILE *stream)
192 void inc_type_visited(void)
197 void print_type_qualifiers(type_qualifiers_t qualifiers)
200 if (qualifiers & TYPE_QUALIFIER_CONST) {
201 fputs(" const" + first, out);
204 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
205 fputs(" volatile" + first, out);
208 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
209 fputs(" restrict" + first, out);
214 const char *get_atomic_kind_name(atomic_type_kind_t kind)
217 case ATOMIC_TYPE_INVALID: break;
218 case ATOMIC_TYPE_VOID: return "void";
219 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
220 case ATOMIC_TYPE_CHAR: return "char";
221 case ATOMIC_TYPE_SCHAR: return "signed char";
222 case ATOMIC_TYPE_UCHAR: return "unsigned char";
223 case ATOMIC_TYPE_INT: return "int";
224 case ATOMIC_TYPE_UINT: return "unsigned int";
225 case ATOMIC_TYPE_SHORT: return "short";
226 case ATOMIC_TYPE_USHORT: return "unsigned short";
227 case ATOMIC_TYPE_LONG: return "long";
228 case ATOMIC_TYPE_ULONG: return "unsigned long";
229 case ATOMIC_TYPE_LONGLONG: return "long long";
230 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
231 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
232 case ATOMIC_TYPE_FLOAT: return "float";
233 case ATOMIC_TYPE_DOUBLE: return "double";
235 return "INVALIDATOMIC";
239 * Prints the name of an atomic type kinds.
241 * @param kind The type kind.
243 static void print_atomic_kinds(atomic_type_kind_t kind)
245 const char *s = get_atomic_kind_name(kind);
250 * Prints the name of an atomic type.
252 * @param type The type.
254 static void print_atomic_type(const atomic_type_t *type)
256 print_type_qualifiers(type->base.qualifiers);
257 if (type->base.qualifiers != 0)
259 print_atomic_kinds(type->akind);
263 * Prints the name of a complex type.
265 * @param type The type.
268 void print_complex_type(const complex_type_t *type)
270 int empty = type->base.qualifiers == 0;
271 print_type_qualifiers(type->base.qualifiers);
272 fputs(" _Complex " + empty, out);
273 print_atomic_kinds(type->akind);
277 * Prints the name of an imaginary type.
279 * @param type The type.
282 void print_imaginary_type(const imaginary_type_t *type)
284 int empty = type->base.qualifiers == 0;
285 print_type_qualifiers(type->base.qualifiers);
286 fputs(" _Imaginary " + empty, out);
287 print_atomic_kinds(type->akind);
291 * Print the first part (the prefix) of a type.
293 * @param type The type to print.
294 * @param top true, if this is the top type, false if it's an embedded type.
296 static void print_function_type_pre(const function_type_t *type, bool top)
298 switch (type->linkage) {
299 case LINKAGE_INVALID:
304 fputs("extern \"C\" ", out);
308 if (!(c_mode & _CXX))
309 fputs("extern \"C++\" ", out);
313 print_type_qualifiers(type->base.qualifiers);
314 if (type->base.qualifiers != 0)
317 intern_print_type_pre(type->return_type, false);
319 switch (type->calling_convention) {
320 case CC_CDECL: fputs("__cdecl ", out); break;
321 case CC_STDCALL: fputs("__stdcall ", out); break;
322 case CC_FASTCALL: fputs("__fastcall ", out); break;
323 case CC_THISCALL: fputs("__thiscall ", out); break;
324 case CC_DEFAULT: break;
327 /* don't emit parenthesis if we're the toplevel type... */
333 * Print the second part (the postfix) of a type.
335 * @param type The type to print.
336 * @param top true, if this is the top type, false if it's an embedded type.
338 static void print_function_type_post(const function_type_t *type,
339 const scope_t *parameters, bool top)
341 /* don't emit parenthesis if we're the toplevel type... */
347 if (parameters == NULL) {
348 function_parameter_t *parameter = type->parameters;
349 for( ; parameter != NULL; parameter = parameter->next) {
355 print_type(parameter->type);
358 entity_t *parameter = parameters->entities;
359 for( ; parameter != NULL; parameter = parameter->base.next) {
365 assert(is_declaration(parameter));
366 print_type_ext(parameter->declaration.type, parameter->base.symbol,
370 if (type->variadic) {
378 if (first && !type->unspecified_parameters) {
383 intern_print_type_post(type->return_type, false);
387 * Prints the prefix part of a pointer type.
389 * @param type The pointer type.
391 static void print_pointer_type_pre(const pointer_type_t *type)
393 intern_print_type_pre(type->points_to, false);
394 variable_t *const variable = type->base_variable;
395 if (variable != NULL) {
396 fputs(" __based(", out);
397 fputs(variable->base.base.symbol->string, out);
401 print_type_qualifiers(type->base.qualifiers);
402 if (type->base.qualifiers != 0)
407 * Prints the postfix part of a pointer type.
409 * @param type The pointer type.
411 static void print_pointer_type_post(const pointer_type_t *type)
413 intern_print_type_post(type->points_to, false);
417 * Prints the prefix part of an array type.
419 * @param type The array type.
421 static void print_array_type_pre(const array_type_t *type)
423 intern_print_type_pre(type->element_type, false);
427 * Prints the postfix part of an array type.
429 * @param type The array type.
431 static void print_array_type_post(const array_type_t *type)
434 if (type->is_static) {
435 fputs("static ", out);
437 print_type_qualifiers(type->base.qualifiers);
438 if (type->base.qualifiers != 0)
440 if (type->size_expression != NULL
441 && (print_implicit_array_size || !type->has_implicit_size)) {
442 print_expression(type->size_expression);
445 intern_print_type_post(type->element_type, false);
449 * Prints the postfix part of a bitfield type.
451 * @param type The array type.
453 static void print_bitfield_type_post(const bitfield_type_t *type)
456 print_expression(type->size_expression);
457 intern_print_type_post(type->base_type, false);
461 * Prints an enum definition.
463 * @param declaration The enum's type declaration.
465 void print_enum_definition(const enum_t *enume)
471 entity_t *entry = enume->base.next;
472 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
473 entry = entry->base.next) {
476 fprintf(out, "%s", entry->base.symbol->string);
477 if (entry->enum_value.value != NULL) {
480 /* skip the implicit cast */
481 expression_t *expression = entry->enum_value.value;
482 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
483 expression = expression->unary.value;
485 print_expression(expression);
496 * Prints an enum type.
498 * @param type The enum type.
500 static void print_type_enum(const enum_type_t *type)
502 int empty = type->base.qualifiers == 0;
503 print_type_qualifiers(type->base.qualifiers);
504 fputs(" enum " + empty, out);
506 enum_t *enume = type->enume;
507 symbol_t *symbol = enume->base.symbol;
508 if (symbol != NULL) {
509 fputs(symbol->string, out);
511 print_enum_definition(enume);
516 * Print the compound part of a compound type.
518 void print_compound_definition(const compound_t *compound)
523 entity_t *entity = compound->members.entities;
524 for( ; entity != NULL; entity = entity->base.next) {
525 if (entity->kind != ENTITY_COMPOUND_MEMBER)
529 print_entity(entity);
539 * Prints a compound type.
541 * @param type The compound type.
543 static void print_compound_type(const compound_type_t *type)
545 int empty = type->base.qualifiers == 0;
546 print_type_qualifiers(type->base.qualifiers);
548 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
549 fputs(" struct " + empty, out);
551 assert(type->base.kind == TYPE_COMPOUND_UNION);
552 fputs(" union " + empty, out);
555 compound_t *compound = type->compound;
556 symbol_t *symbol = compound->base.symbol;
557 if (symbol != NULL) {
558 fputs(symbol->string, out);
560 print_compound_definition(compound);
565 * Prints the prefix part of a typedef type.
567 * @param type The typedef type.
569 static void print_typedef_type_pre(const typedef_type_t *const type)
571 print_type_qualifiers(type->base.qualifiers);
572 if (type->base.qualifiers != 0)
574 fputs(type->typedefe->base.symbol->string, out);
578 * Prints the prefix part of a typeof type.
580 * @param type The typeof type.
582 static void print_typeof_type_pre(const typeof_type_t *const type)
584 fputs("typeof(", out);
585 if (type->expression != NULL) {
586 assert(type->typeof_type == NULL);
587 print_expression(type->expression);
589 print_type(type->typeof_type);
595 * Prints the prefix part of a type.
597 * @param type The type.
598 * @param top true if we print the toplevel type, false else.
600 static void intern_print_type_pre(const type_t *const type, const bool top)
604 fputs("<error>", out);
607 fputs("<invalid>", out);
610 print_type_enum(&type->enumt);
613 print_atomic_type(&type->atomic);
616 print_complex_type(&type->complex);
619 print_imaginary_type(&type->imaginary);
621 case TYPE_COMPOUND_STRUCT:
622 case TYPE_COMPOUND_UNION:
623 print_compound_type(&type->compound);
626 fputs(type->builtin.symbol->string, out);
629 print_function_type_pre(&type->function, top);
632 print_pointer_type_pre(&type->pointer);
635 intern_print_type_pre(type->bitfield.base_type, top);
638 print_array_type_pre(&type->array);
641 print_typedef_type_pre(&type->typedeft);
644 print_typeof_type_pre(&type->typeoft);
647 fputs("unknown", out);
651 * Prints the postfix part of a type.
653 * @param type The type.
654 * @param top true if we print the toplevel type, false else.
656 static void intern_print_type_post(const type_t *const type, const bool top)
660 print_function_type_post(&type->function, NULL, top);
663 print_pointer_type_post(&type->pointer);
666 print_array_type_post(&type->array);
669 print_bitfield_type_post(&type->bitfield);
677 case TYPE_COMPOUND_STRUCT:
678 case TYPE_COMPOUND_UNION:
689 * @param type The type.
691 void print_type(const type_t *const type)
693 print_type_ext(type, NULL, NULL);
696 void print_type_ext(const type_t *const type, const symbol_t *symbol,
697 const scope_t *parameters)
700 fputs("nil type", out);
704 intern_print_type_pre(type, true);
705 if (symbol != NULL) {
707 fputs(symbol->string, out);
709 if (type->kind == TYPE_FUNCTION) {
710 print_function_type_post(&type->function, parameters, true);
712 intern_print_type_post(type, true);
717 * Return the size of a type AST node.
719 * @param type The type.
721 static size_t get_type_size(const type_t *type)
724 case TYPE_ATOMIC: return sizeof(atomic_type_t);
725 case TYPE_COMPLEX: return sizeof(complex_type_t);
726 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
727 case TYPE_COMPOUND_STRUCT:
728 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
729 case TYPE_ENUM: return sizeof(enum_type_t);
730 case TYPE_FUNCTION: return sizeof(function_type_t);
731 case TYPE_POINTER: return sizeof(pointer_type_t);
732 case TYPE_ARRAY: return sizeof(array_type_t);
733 case TYPE_BUILTIN: return sizeof(builtin_type_t);
734 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
735 case TYPE_TYPEOF: return sizeof(typeof_type_t);
736 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
737 case TYPE_ERROR: panic("error type found");
738 case TYPE_INVALID: panic("invalid type found");
740 panic("unknown type found");
746 * @param type The type to copy.
747 * @return A copy of the type.
749 * @note This does not produce a deep copy!
751 type_t *duplicate_type(const type_t *type)
753 size_t size = get_type_size(type);
755 type_t *copy = obstack_alloc(type_obst, size);
756 memcpy(copy, type, size);
762 * Returns the unqualified type of a given type.
764 * @param type The type.
765 * @returns The unqualified type.
767 type_t *get_unqualified_type(type_t *type)
769 assert(!is_typeref(type));
771 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
774 type_t *unqualified_type = duplicate_type(type);
775 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
777 type_t *result = typehash_insert(unqualified_type);
778 if (result != unqualified_type) {
779 obstack_free(type_obst, unqualified_type);
785 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
787 type_t *type = skip_typeref(orig_type);
790 if (is_type_array(type)) {
791 /* For array types the element type has to be adjusted */
792 type_t *element_type = type->array.element_type;
793 type_t *qual_element_type = get_qualified_type(element_type, qual);
795 if (qual_element_type == element_type)
798 copy = duplicate_type(type);
799 copy->array.element_type = qual_element_type;
800 } else if (is_type_valid(type)) {
801 if ((type->base.qualifiers & qual) == qual)
804 copy = duplicate_type(type);
805 copy->base.qualifiers |= qual;
810 type = typehash_insert(copy);
812 obstack_free(type_obst, copy);
818 * Check if a type is valid.
820 * @param type The type to check.
821 * @return true if type represents a valid type.
823 bool type_valid(const type_t *type)
825 return type->kind != TYPE_INVALID;
828 static bool test_atomic_type_flag(atomic_type_kind_t kind,
829 atomic_type_flag_t flag)
831 assert(kind <= ATOMIC_TYPE_LAST);
832 return (atomic_type_properties[kind].flags & flag) != 0;
836 * Returns true if the given type is an integer type.
838 * @param type The type to check.
839 * @return True if type is an integer type.
841 bool is_type_integer(const type_t *type)
843 assert(!is_typeref(type));
845 if (type->kind == TYPE_ENUM)
847 if (type->kind == TYPE_BITFIELD)
850 if (type->kind != TYPE_ATOMIC)
853 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
857 * Returns true if the given type is an enum type.
859 * @param type The type to check.
860 * @return True if type is an enum type.
862 bool is_type_enum(const type_t *type)
864 assert(!is_typeref(type));
865 return type->kind == TYPE_ENUM;
869 * Returns true if the given type is an floating point type.
871 * @param type The type to check.
872 * @return True if type is a floating point type.
874 bool is_type_float(const type_t *type)
876 assert(!is_typeref(type));
878 if (type->kind != TYPE_ATOMIC)
881 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
885 * Returns true if the given type is an complex type.
887 * @param type The type to check.
888 * @return True if type is a complex type.
890 bool is_type_complex(const type_t *type)
892 assert(!is_typeref(type));
894 if (type->kind != TYPE_ATOMIC)
897 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
901 * Returns true if the given type is a signed type.
903 * @param type The type to check.
904 * @return True if type is a signed type.
906 bool is_type_signed(const type_t *type)
908 assert(!is_typeref(type));
910 /* enum types are int for now */
911 if (type->kind == TYPE_ENUM)
913 if (type->kind == TYPE_BITFIELD)
914 return is_type_signed(type->bitfield.base_type);
916 if (type->kind != TYPE_ATOMIC)
919 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
923 * Returns true if the given type represents an arithmetic type.
925 * @param type The type to check.
926 * @return True if type represents an arithmetic type.
928 bool is_type_arithmetic(const type_t *type)
930 assert(!is_typeref(type));
937 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
939 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
941 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
948 * Returns true if the given type is an integer or float type.
950 * @param type The type to check.
951 * @return True if type is an integer or float type.
953 bool is_type_real(const type_t *type)
956 return is_type_integer(type) || is_type_float(type);
960 * Returns true if the given type represents a scalar type.
962 * @param type The type to check.
963 * @return True if type represents a scalar type.
965 bool is_type_scalar(const type_t *type)
967 assert(!is_typeref(type));
969 switch (type->kind) {
970 case TYPE_POINTER: return true;
971 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
975 return is_type_arithmetic(type);
979 * Check if a given type is incomplete.
981 * @param type The type to check.
982 * @return True if the given type is incomplete (ie. just forward).
984 bool is_type_incomplete(const type_t *type)
986 assert(!is_typeref(type));
989 case TYPE_COMPOUND_STRUCT:
990 case TYPE_COMPOUND_UNION: {
991 const compound_type_t *compound_type = &type->compound;
992 return !compound_type->compound->complete;
998 return type->array.size_expression == NULL
999 && !type->array.size_constant;
1002 return type->atomic.akind == ATOMIC_TYPE_VOID;
1005 return type->complex.akind == ATOMIC_TYPE_VOID;
1007 case TYPE_IMAGINARY:
1008 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1019 panic("is_type_incomplete called without typerefs skipped");
1024 panic("invalid type found");
1027 bool is_type_object(const type_t *type)
1029 return !is_type_function(type) && !is_type_incomplete(type);
1033 * Check if two function types are compatible.
1035 static bool function_types_compatible(const function_type_t *func1,
1036 const function_type_t *func2)
1038 const type_t* const ret1 = skip_typeref(func1->return_type);
1039 const type_t* const ret2 = skip_typeref(func2->return_type);
1040 if (!types_compatible(ret1, ret2))
1043 if (func1->linkage != func2->linkage)
1046 if (func1->calling_convention != func2->calling_convention)
1049 /* can parameters be compared? */
1050 if (func1->unspecified_parameters || func2->unspecified_parameters)
1053 if (func1->variadic != func2->variadic)
1056 /* TODO: handling of unspecified parameters not correct yet */
1058 /* all argument types must be compatible */
1059 function_parameter_t *parameter1 = func1->parameters;
1060 function_parameter_t *parameter2 = func2->parameters;
1061 for ( ; parameter1 != NULL && parameter2 != NULL;
1062 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1063 type_t *parameter1_type = skip_typeref(parameter1->type);
1064 type_t *parameter2_type = skip_typeref(parameter2->type);
1066 parameter1_type = get_unqualified_type(parameter1_type);
1067 parameter2_type = get_unqualified_type(parameter2_type);
1069 if (!types_compatible(parameter1_type, parameter2_type))
1072 /* same number of arguments? */
1073 if (parameter1 != NULL || parameter2 != NULL)
1080 * Check if two array types are compatible.
1082 static bool array_types_compatible(const array_type_t *array1,
1083 const array_type_t *array2)
1085 type_t *element_type1 = skip_typeref(array1->element_type);
1086 type_t *element_type2 = skip_typeref(array2->element_type);
1087 if (!types_compatible(element_type1, element_type2))
1090 if (!array1->size_constant || !array2->size_constant)
1093 return array1->size == array2->size;
1097 * Check if two types are compatible.
1099 bool types_compatible(const type_t *type1, const type_t *type2)
1101 assert(!is_typeref(type1));
1102 assert(!is_typeref(type2));
1104 /* shortcut: the same type is always compatible */
1108 if (!is_type_valid(type1) || !is_type_valid(type2))
1111 if (type1->base.qualifiers != type2->base.qualifiers)
1113 if (type1->kind != type2->kind)
1116 switch (type1->kind) {
1118 return function_types_compatible(&type1->function, &type2->function);
1120 return type1->atomic.akind == type2->atomic.akind;
1122 return type1->complex.akind == type2->complex.akind;
1123 case TYPE_IMAGINARY:
1124 return type1->imaginary.akind == type2->imaginary.akind;
1126 return array_types_compatible(&type1->array, &type2->array);
1128 case TYPE_POINTER: {
1129 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1130 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1131 return types_compatible(to1, to2);
1134 case TYPE_COMPOUND_STRUCT:
1135 case TYPE_COMPOUND_UNION:
1138 /* TODO: not implemented */
1142 /* not sure if this makes sense or is even needed, implement it if you
1143 * really need it! */
1144 panic("type compatibility check for bitfield type");
1147 /* Hmm, the error type should be compatible to all other types */
1150 panic("invalid type found in compatible types");
1153 panic("typerefs not skipped in compatible types?!?");
1156 /* TODO: incomplete */
1161 * Skip all typerefs and return the underlying type.
1163 type_t *skip_typeref(type_t *type)
1165 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1166 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1169 switch (type->kind) {
1172 case TYPE_TYPEDEF: {
1173 qualifiers |= type->base.qualifiers;
1174 modifiers |= type->base.modifiers;
1175 const typedef_type_t *typedef_type = &type->typedeft;
1176 if (typedef_type->resolved_type != NULL) {
1177 type = typedef_type->resolved_type;
1180 type = typedef_type->typedefe->type;
1184 const typeof_type_t *typeof_type = &type->typeoft;
1185 if (typeof_type->typeof_type != NULL) {
1186 type = typeof_type->typeof_type;
1188 type = typeof_type->expression->base.type;
1198 if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1199 type_t *const copy = duplicate_type(type);
1201 /* for const with typedefed array type the element type has to be
1203 if (is_type_array(copy)) {
1204 type_t *element_type = copy->array.element_type;
1205 element_type = duplicate_type(element_type);
1206 element_type->base.qualifiers |= qualifiers;
1207 element_type->base.modifiers |= modifiers;
1208 copy->array.element_type = element_type;
1210 copy->base.qualifiers |= qualifiers;
1211 copy->base.modifiers |= modifiers;
1214 type = typehash_insert(copy);
1216 obstack_free(type_obst, copy);
1223 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type) {
1224 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1227 switch (type->base.kind) {
1229 return TYPE_QUALIFIER_NONE;
1231 qualifiers |= type->base.qualifiers;
1232 const typedef_type_t *typedef_type = &type->typedeft;
1233 if (typedef_type->resolved_type != NULL)
1234 type = typedef_type->resolved_type;
1236 type = typedef_type->typedefe->type;
1239 const typeof_type_t *typeof_type = &type->typeoft;
1240 if (typeof_type->typeof_type != NULL) {
1241 type = typeof_type->typeof_type;
1243 type = typeof_type->expression->base.type;
1248 if (skip_array_type) {
1249 type = type->array.element_type;
1258 return type->base.qualifiers | qualifiers;
1261 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1263 assert(kind <= ATOMIC_TYPE_LAST);
1264 return atomic_type_properties[kind].size;
1267 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1269 assert(kind <= ATOMIC_TYPE_LAST);
1270 return atomic_type_properties[kind].alignment;
1273 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1275 assert(kind <= ATOMIC_TYPE_LAST);
1276 return atomic_type_properties[kind].flags;
1279 atomic_type_kind_t get_intptr_kind(void)
1281 if (machine_size <= 32)
1282 return ATOMIC_TYPE_INT;
1283 else if (machine_size <= 64)
1284 return ATOMIC_TYPE_LONG;
1286 return ATOMIC_TYPE_LONGLONG;
1289 atomic_type_kind_t get_uintptr_kind(void)
1291 if (machine_size <= 32)
1292 return ATOMIC_TYPE_UINT;
1293 else if (machine_size <= 64)
1294 return ATOMIC_TYPE_ULONG;
1296 return ATOMIC_TYPE_ULONGLONG;
1300 * Find the atomic type kind representing a given size (signed).
1302 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1303 static atomic_type_kind_t kinds[32];
1306 atomic_type_kind_t kind = kinds[size];
1307 if (kind == ATOMIC_TYPE_INVALID) {
1308 static const atomic_type_kind_t possible_kinds[] = {
1313 ATOMIC_TYPE_LONGLONG
1315 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1316 if (get_atomic_type_size(possible_kinds[i]) == size) {
1317 kind = possible_kinds[i];
1327 * Find the atomic type kind representing a given size (signed).
1329 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1330 static atomic_type_kind_t kinds[32];
1333 atomic_type_kind_t kind = kinds[size];
1334 if (kind == ATOMIC_TYPE_INVALID) {
1335 static const atomic_type_kind_t possible_kinds[] = {
1340 ATOMIC_TYPE_ULONGLONG
1342 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1343 if (get_atomic_type_size(possible_kinds[i]) == size) {
1344 kind = possible_kinds[i];
1354 * Hash the given type and return the "singleton" version
1357 static type_t *identify_new_type(type_t *type)
1359 type_t *result = typehash_insert(type);
1360 if (result != type) {
1361 obstack_free(type_obst, type);
1367 * Creates a new atomic type.
1369 * @param akind The kind of the atomic type.
1370 * @param qualifiers Type qualifiers for the new type.
1372 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1374 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1375 memset(type, 0, sizeof(atomic_type_t));
1377 type->kind = TYPE_ATOMIC;
1378 type->base.size = get_atomic_type_size(akind);
1379 type->base.alignment = get_atomic_type_alignment(akind);
1380 type->base.qualifiers = qualifiers;
1381 type->atomic.akind = akind;
1383 return identify_new_type(type);
1387 * Creates a new complex type.
1389 * @param akind The kind of the atomic type.
1390 * @param qualifiers Type qualifiers for the new type.
1392 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1394 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1395 memset(type, 0, sizeof(complex_type_t));
1397 type->kind = TYPE_COMPLEX;
1398 type->base.qualifiers = qualifiers;
1399 type->base.alignment = get_atomic_type_alignment(akind);
1400 type->complex.akind = akind;
1402 return identify_new_type(type);
1406 * Creates a new imaginary type.
1408 * @param akind The kind of the atomic type.
1409 * @param qualifiers Type qualifiers for the new type.
1411 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1413 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1414 memset(type, 0, sizeof(imaginary_type_t));
1416 type->kind = TYPE_IMAGINARY;
1417 type->base.qualifiers = qualifiers;
1418 type->base.alignment = get_atomic_type_alignment(akind);
1419 type->imaginary.akind = akind;
1421 return identify_new_type(type);
1425 * Creates a new pointer type.
1427 * @param points_to The points-to type for the new type.
1428 * @param qualifiers Type qualifiers for the new type.
1430 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1432 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1433 memset(type, 0, sizeof(pointer_type_t));
1435 type->kind = TYPE_POINTER;
1436 type->base.qualifiers = qualifiers;
1437 type->base.alignment = 0;
1438 type->pointer.points_to = points_to;
1439 type->pointer.base_variable = NULL;
1441 return identify_new_type(type);
1445 * Creates a new based pointer type.
1447 * @param points_to The points-to type for the new type.
1448 * @param qualifiers Type qualifiers for the new type.
1449 * @param variable The based variable
1451 type_t *make_based_pointer_type(type_t *points_to,
1452 type_qualifiers_t qualifiers, variable_t *variable)
1454 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1455 memset(type, 0, sizeof(pointer_type_t));
1457 type->kind = TYPE_POINTER;
1458 type->base.qualifiers = qualifiers;
1459 type->base.alignment = 0;
1460 type->pointer.points_to = points_to;
1461 type->pointer.base_variable = variable;
1463 return identify_new_type(type);
1467 type_t *make_array_type(type_t *element_type, size_t size,
1468 type_qualifiers_t qualifiers)
1470 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1471 memset(type, 0, sizeof(array_type_t));
1473 type->kind = TYPE_ARRAY;
1474 type->base.qualifiers = qualifiers;
1475 type->base.alignment = 0;
1476 type->array.element_type = element_type;
1477 type->array.size = size;
1478 type->array.size_constant = true;
1480 return identify_new_type(type);
1484 * Debug helper. Prints the given type to stdout.
1486 static __attribute__((unused))
1487 void dbg_type(const type_t *type)
1489 FILE *old_out = out;