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_WCHAR_T] = {
57 .alignment = (unsigned)-1,
58 /* signed flag will be set when known */
59 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
61 [ATOMIC_TYPE_CHAR] = {
64 /* signed flag will be set when known */
65 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
67 [ATOMIC_TYPE_SCHAR] = {
70 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
71 | ATOMIC_TYPE_FLAG_SIGNED,
73 [ATOMIC_TYPE_UCHAR] = {
76 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
78 [ATOMIC_TYPE_SHORT] = {
81 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
82 | ATOMIC_TYPE_FLAG_SIGNED
84 [ATOMIC_TYPE_USHORT] = {
87 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
90 .size = (unsigned) -1,
91 .alignment = (unsigned) -1,
92 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
93 | ATOMIC_TYPE_FLAG_SIGNED,
95 [ATOMIC_TYPE_UINT] = {
96 .size = (unsigned) -1,
97 .alignment = (unsigned) -1,
98 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
100 [ATOMIC_TYPE_LONG] = {
101 .size = (unsigned) -1,
102 .alignment = (unsigned) -1,
103 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
104 | ATOMIC_TYPE_FLAG_SIGNED,
106 [ATOMIC_TYPE_ULONG] = {
107 .size = (unsigned) -1,
108 .alignment = (unsigned) -1,
109 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
111 [ATOMIC_TYPE_LONGLONG] = {
112 .size = (unsigned) -1,
113 .alignment = (unsigned) -1,
114 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
115 | ATOMIC_TYPE_FLAG_SIGNED,
117 [ATOMIC_TYPE_ULONGLONG] = {
118 .size = (unsigned) -1,
119 .alignment = (unsigned) -1,
120 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
122 [ATOMIC_TYPE_BOOL] = {
123 .size = (unsigned) -1,
124 .alignment = (unsigned) -1,
125 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
127 [ATOMIC_TYPE_FLOAT] = {
129 .alignment = (unsigned) -1,
130 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
131 | ATOMIC_TYPE_FLAG_SIGNED,
133 [ATOMIC_TYPE_DOUBLE] = {
135 .alignment = (unsigned) -1,
136 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
137 | ATOMIC_TYPE_FLAG_SIGNED,
139 [ATOMIC_TYPE_LONG_DOUBLE] = {
141 .alignment = (unsigned) -1,
142 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
143 | ATOMIC_TYPE_FLAG_SIGNED,
145 /* complex and imaginary types are set in init_types */
148 void init_types(void)
150 obstack_init(type_obst);
152 atomic_type_properties_t *props = atomic_type_properties;
154 if (char_is_signed) {
155 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
158 unsigned int_size = machine_size < 32 ? 2 : 4;
159 unsigned long_size = machine_size < 64 ? 4 : 8;
160 unsigned llong_size = machine_size < 32 ? 4 : 8;
162 props[ATOMIC_TYPE_INT].size = int_size;
163 props[ATOMIC_TYPE_INT].alignment = int_size;
164 props[ATOMIC_TYPE_UINT].size = int_size;
165 props[ATOMIC_TYPE_UINT].alignment = int_size;
166 props[ATOMIC_TYPE_LONG].size = long_size;
167 props[ATOMIC_TYPE_LONG].alignment = long_size;
168 props[ATOMIC_TYPE_ULONG].size = long_size;
169 props[ATOMIC_TYPE_ULONG].alignment = long_size;
170 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
171 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
172 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
173 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
175 /* TODO: backend specific, need a way to query the backend for this.
176 * The following are good settings for x86 */
177 props[ATOMIC_TYPE_FLOAT].alignment = 4;
178 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
179 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
180 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
181 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
183 /* TODO: make this configurable for platforms which do not use byte sized
185 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
187 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
190 void exit_types(void)
192 obstack_free(type_obst, NULL);
195 void type_set_output(FILE *stream)
200 void inc_type_visited(void)
205 void print_type_qualifiers(type_qualifiers_t qualifiers)
208 if (qualifiers & TYPE_QUALIFIER_CONST) {
209 fputs(" const" + first, out);
212 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
213 fputs(" volatile" + first, out);
216 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
217 fputs(" restrict" + first, out);
222 const char *get_atomic_kind_name(atomic_type_kind_t kind)
225 case ATOMIC_TYPE_INVALID: break;
226 case ATOMIC_TYPE_VOID: return "void";
227 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
228 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
229 case ATOMIC_TYPE_CHAR: return "char";
230 case ATOMIC_TYPE_SCHAR: return "signed char";
231 case ATOMIC_TYPE_UCHAR: return "unsigned char";
232 case ATOMIC_TYPE_INT: return "int";
233 case ATOMIC_TYPE_UINT: return "unsigned int";
234 case ATOMIC_TYPE_SHORT: return "short";
235 case ATOMIC_TYPE_USHORT: return "unsigned short";
236 case ATOMIC_TYPE_LONG: return "long";
237 case ATOMIC_TYPE_ULONG: return "unsigned long";
238 case ATOMIC_TYPE_LONGLONG: return "long long";
239 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
240 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
241 case ATOMIC_TYPE_FLOAT: return "float";
242 case ATOMIC_TYPE_DOUBLE: return "double";
244 return "INVALIDATOMIC";
248 * Prints the name of an atomic type kinds.
250 * @param kind The type kind.
252 static void print_atomic_kinds(atomic_type_kind_t kind)
254 const char *s = get_atomic_kind_name(kind);
259 * Prints the name of an atomic type.
261 * @param type The type.
263 static void print_atomic_type(const atomic_type_t *type)
265 print_type_qualifiers(type->base.qualifiers);
266 if (type->base.qualifiers != 0)
268 print_atomic_kinds(type->akind);
272 * Prints the name of a complex type.
274 * @param type The type.
277 void print_complex_type(const complex_type_t *type)
279 int empty = type->base.qualifiers == 0;
280 print_type_qualifiers(type->base.qualifiers);
281 fputs(" _Complex " + empty, out);
282 print_atomic_kinds(type->akind);
286 * Prints the name of an imaginary type.
288 * @param type The type.
291 void print_imaginary_type(const imaginary_type_t *type)
293 int empty = type->base.qualifiers == 0;
294 print_type_qualifiers(type->base.qualifiers);
295 fputs(" _Imaginary " + empty, out);
296 print_atomic_kinds(type->akind);
300 * Print the first part (the prefix) of a type.
302 * @param type The type to print.
303 * @param top true, if this is the top type, false if it's an embedded type.
305 static void print_function_type_pre(const function_type_t *type, bool top)
307 switch (type->linkage) {
308 case LINKAGE_INVALID:
313 fputs("extern \"C\" ", out);
317 if (!(c_mode & _CXX))
318 fputs("extern \"C++\" ", out);
322 print_type_qualifiers(type->base.qualifiers);
323 if (type->base.qualifiers != 0)
326 intern_print_type_pre(type->return_type, false);
328 switch (type->calling_convention) {
329 case CC_CDECL: fputs("__cdecl ", out); break;
330 case CC_STDCALL: fputs("__stdcall ", out); break;
331 case CC_FASTCALL: fputs("__fastcall ", out); break;
332 case CC_THISCALL: fputs("__thiscall ", out); break;
333 case CC_DEFAULT: break;
336 /* don't emit parenthesis if we're the toplevel type... */
342 * Print the second part (the postfix) of a type.
344 * @param type The type to print.
345 * @param top true, if this is the top type, false if it's an embedded type.
347 static void print_function_type_post(const function_type_t *type,
348 const scope_t *parameters, bool top)
350 /* don't emit parenthesis if we're the toplevel type... */
356 if (parameters == NULL) {
357 function_parameter_t *parameter = type->parameters;
358 for( ; parameter != NULL; parameter = parameter->next) {
364 print_type(parameter->type);
367 entity_t *parameter = parameters->entities;
368 for (; parameter != NULL; parameter = parameter->base.next) {
374 assert(is_declaration(parameter));
375 const type_t *const type = parameter->declaration.type;
377 fputs(parameter->base.symbol->string, out);
379 print_type_ext(type, parameter->base.symbol, NULL);
383 if (type->variadic) {
391 if (first && !type->unspecified_parameters) {
396 intern_print_type_post(type->return_type, false);
400 * Prints the prefix part of a pointer type.
402 * @param type The pointer type.
404 static void print_pointer_type_pre(const pointer_type_t *type)
406 intern_print_type_pre(type->points_to, false);
407 variable_t *const variable = type->base_variable;
408 if (variable != NULL) {
409 fputs(" __based(", out);
410 fputs(variable->base.base.symbol->string, out);
414 type_qualifiers_t const qual = type->base.qualifiers;
417 print_type_qualifiers(qual);
421 * Prints the prefix part of a reference type.
423 * @param type The reference type.
425 static void print_reference_type_pre(const reference_type_t *type)
427 intern_print_type_pre(type->refers_to, false);
432 * Prints the postfix part of a pointer type.
434 * @param type The pointer type.
436 static void print_pointer_type_post(const pointer_type_t *type)
438 intern_print_type_post(type->points_to, false);
442 * Prints the postfix part of a reference type.
444 * @param type The reference type.
446 static void print_reference_type_post(const reference_type_t *type)
448 intern_print_type_post(type->refers_to, false);
452 * Prints the prefix part of an array type.
454 * @param type The array type.
456 static void print_array_type_pre(const array_type_t *type)
458 intern_print_type_pre(type->element_type, false);
462 * Prints the postfix part of an array type.
464 * @param type The array type.
466 static void print_array_type_post(const array_type_t *type)
469 if (type->is_static) {
470 fputs("static ", out);
472 print_type_qualifiers(type->base.qualifiers);
473 if (type->base.qualifiers != 0)
475 if (type->size_expression != NULL
476 && (print_implicit_array_size || !type->has_implicit_size)) {
477 print_expression(type->size_expression);
480 intern_print_type_post(type->element_type, false);
484 * Prints the postfix part of a bitfield type.
486 * @param type The array type.
488 static void print_bitfield_type_post(const bitfield_type_t *type)
491 print_expression(type->size_expression);
492 intern_print_type_post(type->base_type, false);
496 * Prints an enum definition.
498 * @param declaration The enum's type declaration.
500 void print_enum_definition(const enum_t *enume)
506 entity_t *entry = enume->base.next;
507 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
508 entry = entry->base.next) {
511 fputs(entry->base.symbol->string, out);
512 if (entry->enum_value.value != NULL) {
515 /* skip the implicit cast */
516 expression_t *expression = entry->enum_value.value;
517 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
518 expression = expression->unary.value;
520 print_expression(expression);
531 * Prints an enum type.
533 * @param type The enum type.
535 static void print_type_enum(const enum_type_t *type)
537 int empty = type->base.qualifiers == 0;
538 print_type_qualifiers(type->base.qualifiers);
539 fputs(" enum " + empty, out);
541 enum_t *enume = type->enume;
542 symbol_t *symbol = enume->base.symbol;
543 if (symbol != NULL) {
544 fputs(symbol->string, out);
546 print_enum_definition(enume);
551 * Print the compound part of a compound type.
553 void print_compound_definition(const compound_t *compound)
558 entity_t *entity = compound->members.entities;
559 for( ; entity != NULL; entity = entity->base.next) {
560 if (entity->kind != ENTITY_COMPOUND_MEMBER)
564 print_entity(entity);
571 if (compound->modifiers & DM_TRANSPARENT_UNION) {
572 fputs("__attribute__((__transparent_union__))", out);
577 * Prints a compound type.
579 * @param type The compound type.
581 static void print_compound_type(const compound_type_t *type)
583 int empty = type->base.qualifiers == 0;
584 print_type_qualifiers(type->base.qualifiers);
586 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
587 fputs(" struct " + empty, out);
589 assert(type->base.kind == TYPE_COMPOUND_UNION);
590 fputs(" union " + empty, out);
593 compound_t *compound = type->compound;
594 symbol_t *symbol = compound->base.symbol;
595 if (symbol != NULL) {
596 fputs(symbol->string, out);
598 print_compound_definition(compound);
603 * Prints the prefix part of a typedef type.
605 * @param type The typedef type.
607 static void print_typedef_type_pre(const typedef_type_t *const type)
609 print_type_qualifiers(type->base.qualifiers);
610 if (type->base.qualifiers != 0)
612 fputs(type->typedefe->base.symbol->string, out);
616 * Prints the prefix part of a typeof type.
618 * @param type The typeof type.
620 static void print_typeof_type_pre(const typeof_type_t *const type)
622 fputs("typeof(", out);
623 if (type->expression != NULL) {
624 assert(type->typeof_type == NULL);
625 print_expression(type->expression);
627 print_type(type->typeof_type);
633 * Prints the prefix part of a type.
635 * @param type The type.
636 * @param top true if we print the toplevel type, false else.
638 static void intern_print_type_pre(const type_t *const type, const bool top)
642 fputs("<error>", out);
645 fputs("<invalid>", out);
648 print_type_enum(&type->enumt);
651 print_atomic_type(&type->atomic);
654 print_complex_type(&type->complex);
657 print_imaginary_type(&type->imaginary);
659 case TYPE_COMPOUND_STRUCT:
660 case TYPE_COMPOUND_UNION:
661 print_compound_type(&type->compound);
664 fputs(type->builtin.symbol->string, out);
667 print_function_type_pre(&type->function, top);
670 print_pointer_type_pre(&type->pointer);
673 print_reference_type_pre(&type->reference);
676 intern_print_type_pre(type->bitfield.base_type, top);
679 print_array_type_pre(&type->array);
682 print_typedef_type_pre(&type->typedeft);
685 print_typeof_type_pre(&type->typeoft);
688 fputs("unknown", out);
692 * Prints the postfix part of a type.
694 * @param type The type.
695 * @param top true if we print the toplevel type, false else.
697 static void intern_print_type_post(const type_t *const type, const bool top)
701 print_function_type_post(&type->function, NULL, top);
704 print_pointer_type_post(&type->pointer);
707 print_reference_type_post(&type->reference);
710 print_array_type_post(&type->array);
713 print_bitfield_type_post(&type->bitfield);
721 case TYPE_COMPOUND_STRUCT:
722 case TYPE_COMPOUND_UNION:
729 if (type->base.modifiers & DM_TRANSPARENT_UNION) {
730 fputs("__attribute__((__transparent_union__))", out);
737 * @param type The type.
739 void print_type(const type_t *const type)
741 print_type_ext(type, NULL, NULL);
744 void print_type_ext(const type_t *const type, const symbol_t *symbol,
745 const scope_t *parameters)
748 fputs("nil type", out);
752 intern_print_type_pre(type, true);
753 if (symbol != NULL) {
755 fputs(symbol->string, out);
757 if (type->kind == TYPE_FUNCTION) {
758 print_function_type_post(&type->function, parameters, true);
760 intern_print_type_post(type, true);
765 * Return the size of a type AST node.
767 * @param type The type.
769 static size_t get_type_size(const type_t *type)
772 case TYPE_ATOMIC: return sizeof(atomic_type_t);
773 case TYPE_COMPLEX: return sizeof(complex_type_t);
774 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
775 case TYPE_COMPOUND_STRUCT:
776 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
777 case TYPE_ENUM: return sizeof(enum_type_t);
778 case TYPE_FUNCTION: return sizeof(function_type_t);
779 case TYPE_POINTER: return sizeof(pointer_type_t);
780 case TYPE_REFERENCE: return sizeof(reference_type_t);
781 case TYPE_ARRAY: return sizeof(array_type_t);
782 case TYPE_BUILTIN: return sizeof(builtin_type_t);
783 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
784 case TYPE_TYPEOF: return sizeof(typeof_type_t);
785 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
786 case TYPE_ERROR: panic("error type found");
787 case TYPE_INVALID: panic("invalid type found");
789 panic("unknown type found");
795 * @param type The type to copy.
796 * @return A copy of the type.
798 * @note This does not produce a deep copy!
800 type_t *duplicate_type(const type_t *type)
802 size_t size = get_type_size(type);
804 type_t *copy = obstack_alloc(type_obst, size);
805 memcpy(copy, type, size);
811 * Returns the unqualified type of a given type.
813 * @param type The type.
814 * @returns The unqualified type.
816 type_t *get_unqualified_type(type_t *type)
818 assert(!is_typeref(type));
820 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
823 type_t *unqualified_type = duplicate_type(type);
824 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
826 type_t *result = typehash_insert(unqualified_type);
827 if (result != unqualified_type) {
828 obstack_free(type_obst, unqualified_type);
834 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
836 type_t *type = skip_typeref(orig_type);
839 if (is_type_array(type)) {
840 /* For array types the element type has to be adjusted */
841 type_t *element_type = type->array.element_type;
842 type_t *qual_element_type = get_qualified_type(element_type, qual);
844 if (qual_element_type == element_type)
847 copy = duplicate_type(type);
848 copy->array.element_type = qual_element_type;
849 } else if (is_type_valid(type)) {
850 if ((type->base.qualifiers & qual) == qual)
853 copy = duplicate_type(type);
854 copy->base.qualifiers |= qual;
859 type = typehash_insert(copy);
861 obstack_free(type_obst, copy);
867 * Check if a type is valid.
869 * @param type The type to check.
870 * @return true if type represents a valid type.
872 bool type_valid(const type_t *type)
874 return type->kind != TYPE_INVALID;
877 static bool test_atomic_type_flag(atomic_type_kind_t kind,
878 atomic_type_flag_t flag)
880 assert(kind <= ATOMIC_TYPE_LAST);
881 return (atomic_type_properties[kind].flags & flag) != 0;
885 * Returns true if the given type is an integer type.
887 * @param type The type to check.
888 * @return True if type is an integer type.
890 bool is_type_integer(const type_t *type)
892 assert(!is_typeref(type));
894 if (type->kind == TYPE_ENUM)
896 if (type->kind == TYPE_BITFIELD)
899 if (type->kind != TYPE_ATOMIC)
902 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
906 * Returns true if the given type is an enum type.
908 * @param type The type to check.
909 * @return True if type is an enum type.
911 bool is_type_enum(const type_t *type)
913 assert(!is_typeref(type));
914 return type->kind == TYPE_ENUM;
918 * Returns true if the given type is an floating point type.
920 * @param type The type to check.
921 * @return True if type is a floating point type.
923 bool is_type_float(const type_t *type)
925 assert(!is_typeref(type));
927 if (type->kind != TYPE_ATOMIC)
930 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
934 * Returns true if the given type is an complex type.
936 * @param type The type to check.
937 * @return True if type is a complex type.
939 bool is_type_complex(const type_t *type)
941 assert(!is_typeref(type));
943 if (type->kind != TYPE_ATOMIC)
946 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
950 * Returns true if the given type is a signed type.
952 * @param type The type to check.
953 * @return True if type is a signed type.
955 bool is_type_signed(const type_t *type)
957 assert(!is_typeref(type));
959 /* enum types are int for now */
960 if (type->kind == TYPE_ENUM)
962 if (type->kind == TYPE_BITFIELD)
963 return is_type_signed(type->bitfield.base_type);
965 if (type->kind != TYPE_ATOMIC)
968 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
972 * Returns true if the given type represents an arithmetic type.
974 * @param type The type to check.
975 * @return True if type represents an arithmetic type.
977 bool is_type_arithmetic(const type_t *type)
979 assert(!is_typeref(type));
986 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
988 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
990 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
997 * Returns true if the given type is an integer or float type.
999 * @param type The type to check.
1000 * @return True if type is an integer or float type.
1002 bool is_type_real(const type_t *type)
1005 return is_type_integer(type) || is_type_float(type);
1009 * Returns true if the given type represents a scalar type.
1011 * @param type The type to check.
1012 * @return True if type represents a scalar type.
1014 bool is_type_scalar(const type_t *type)
1016 assert(!is_typeref(type));
1018 switch (type->kind) {
1019 case TYPE_POINTER: return true;
1020 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1024 return is_type_arithmetic(type);
1028 * Check if a given type is incomplete.
1030 * @param type The type to check.
1031 * @return True if the given type is incomplete (ie. just forward).
1033 bool is_type_incomplete(const type_t *type)
1035 assert(!is_typeref(type));
1037 switch(type->kind) {
1038 case TYPE_COMPOUND_STRUCT:
1039 case TYPE_COMPOUND_UNION: {
1040 const compound_type_t *compound_type = &type->compound;
1041 return !compound_type->compound->complete;
1047 return type->array.size_expression == NULL
1048 && !type->array.size_constant;
1051 return type->atomic.akind == ATOMIC_TYPE_VOID;
1054 return type->complex.akind == ATOMIC_TYPE_VOID;
1056 case TYPE_IMAGINARY:
1057 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1062 case TYPE_REFERENCE:
1069 panic("is_type_incomplete called without typerefs skipped");
1074 panic("invalid type found");
1077 bool is_type_object(const type_t *type)
1079 return !is_type_function(type) && !is_type_incomplete(type);
1083 * Check if two function types are compatible.
1085 static bool function_types_compatible(const function_type_t *func1,
1086 const function_type_t *func2)
1088 const type_t* const ret1 = skip_typeref(func1->return_type);
1089 const type_t* const ret2 = skip_typeref(func2->return_type);
1090 if (!types_compatible(ret1, ret2))
1093 if (func1->linkage != func2->linkage)
1096 if (func1->calling_convention != func2->calling_convention)
1099 /* can parameters be compared? */
1100 if (func1->unspecified_parameters || func2->unspecified_parameters)
1103 if (func1->variadic != func2->variadic)
1106 /* TODO: handling of unspecified parameters not correct yet */
1108 /* all argument types must be compatible */
1109 function_parameter_t *parameter1 = func1->parameters;
1110 function_parameter_t *parameter2 = func2->parameters;
1111 for ( ; parameter1 != NULL && parameter2 != NULL;
1112 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1113 type_t *parameter1_type = skip_typeref(parameter1->type);
1114 type_t *parameter2_type = skip_typeref(parameter2->type);
1116 parameter1_type = get_unqualified_type(parameter1_type);
1117 parameter2_type = get_unqualified_type(parameter2_type);
1119 if (!types_compatible(parameter1_type, parameter2_type))
1122 /* same number of arguments? */
1123 if (parameter1 != NULL || parameter2 != NULL)
1130 * Check if two array types are compatible.
1132 static bool array_types_compatible(const array_type_t *array1,
1133 const array_type_t *array2)
1135 type_t *element_type1 = skip_typeref(array1->element_type);
1136 type_t *element_type2 = skip_typeref(array2->element_type);
1137 if (!types_compatible(element_type1, element_type2))
1140 if (!array1->size_constant || !array2->size_constant)
1143 return array1->size == array2->size;
1147 * Check if two types are compatible.
1149 bool types_compatible(const type_t *type1, const type_t *type2)
1151 assert(!is_typeref(type1));
1152 assert(!is_typeref(type2));
1154 /* shortcut: the same type is always compatible */
1158 if (!is_type_valid(type1) || !is_type_valid(type2))
1161 if (type1->base.qualifiers != type2->base.qualifiers)
1163 if (type1->kind != type2->kind)
1166 switch (type1->kind) {
1168 return function_types_compatible(&type1->function, &type2->function);
1170 return type1->atomic.akind == type2->atomic.akind;
1172 return type1->complex.akind == type2->complex.akind;
1173 case TYPE_IMAGINARY:
1174 return type1->imaginary.akind == type2->imaginary.akind;
1176 return array_types_compatible(&type1->array, &type2->array);
1178 case TYPE_POINTER: {
1179 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1180 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1181 return types_compatible(to1, to2);
1184 case TYPE_REFERENCE: {
1185 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1186 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1187 return types_compatible(to1, to2);
1190 case TYPE_COMPOUND_STRUCT:
1191 case TYPE_COMPOUND_UNION:
1194 /* TODO: not implemented */
1198 /* not sure if this makes sense or is even needed, implement it if you
1199 * really need it! */
1200 panic("type compatibility check for bitfield type");
1203 /* Hmm, the error type should be compatible to all other types */
1206 panic("invalid type found in compatible types");
1209 panic("typerefs not skipped in compatible types?!?");
1212 /* TODO: incomplete */
1217 * Skip all typerefs and return the underlying type.
1219 type_t *skip_typeref(type_t *type)
1221 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1222 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1225 switch (type->kind) {
1228 case TYPE_TYPEDEF: {
1229 qualifiers |= type->base.qualifiers;
1230 modifiers |= type->base.modifiers;
1231 const typedef_type_t *typedef_type = &type->typedeft;
1232 if (typedef_type->resolved_type != NULL) {
1233 type = typedef_type->resolved_type;
1236 type = typedef_type->typedefe->type;
1240 const typeof_type_t *typeof_type = &type->typeoft;
1241 if (typeof_type->typeof_type != NULL) {
1242 type = typeof_type->typeof_type;
1244 type = typeof_type->expression->base.type;
1254 if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1255 type_t *const copy = duplicate_type(type);
1257 /* for const with typedefed array type the element type has to be
1259 if (is_type_array(copy)) {
1260 type_t *element_type = copy->array.element_type;
1261 element_type = duplicate_type(element_type);
1262 element_type->base.qualifiers |= qualifiers;
1263 element_type->base.modifiers |= modifiers;
1264 copy->array.element_type = element_type;
1266 copy->base.qualifiers |= qualifiers;
1267 copy->base.modifiers |= modifiers;
1270 type = typehash_insert(copy);
1272 obstack_free(type_obst, copy);
1279 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type) {
1280 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1283 switch (type->base.kind) {
1285 return TYPE_QUALIFIER_NONE;
1287 qualifiers |= type->base.qualifiers;
1288 const typedef_type_t *typedef_type = &type->typedeft;
1289 if (typedef_type->resolved_type != NULL)
1290 type = typedef_type->resolved_type;
1292 type = typedef_type->typedefe->type;
1295 const typeof_type_t *typeof_type = &type->typeoft;
1296 if (typeof_type->typeof_type != NULL) {
1297 type = typeof_type->typeof_type;
1299 type = typeof_type->expression->base.type;
1304 if (skip_array_type) {
1305 type = type->array.element_type;
1314 return type->base.qualifiers | qualifiers;
1317 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1319 assert(kind <= ATOMIC_TYPE_LAST);
1320 return atomic_type_properties[kind].size;
1323 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1325 assert(kind <= ATOMIC_TYPE_LAST);
1326 return atomic_type_properties[kind].alignment;
1329 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1331 assert(kind <= ATOMIC_TYPE_LAST);
1332 return atomic_type_properties[kind].flags;
1335 atomic_type_kind_t get_intptr_kind(void)
1337 if (machine_size <= 32)
1338 return ATOMIC_TYPE_INT;
1339 else if (machine_size <= 64)
1340 return ATOMIC_TYPE_LONG;
1342 return ATOMIC_TYPE_LONGLONG;
1345 atomic_type_kind_t get_uintptr_kind(void)
1347 if (machine_size <= 32)
1348 return ATOMIC_TYPE_UINT;
1349 else if (machine_size <= 64)
1350 return ATOMIC_TYPE_ULONG;
1352 return ATOMIC_TYPE_ULONGLONG;
1356 * Find the atomic type kind representing a given size (signed).
1358 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1359 static atomic_type_kind_t kinds[32];
1362 atomic_type_kind_t kind = kinds[size];
1363 if (kind == ATOMIC_TYPE_INVALID) {
1364 static const atomic_type_kind_t possible_kinds[] = {
1369 ATOMIC_TYPE_LONGLONG
1371 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1372 if (get_atomic_type_size(possible_kinds[i]) == size) {
1373 kind = possible_kinds[i];
1383 * Find the atomic type kind representing a given size (signed).
1385 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1386 static atomic_type_kind_t kinds[32];
1389 atomic_type_kind_t kind = kinds[size];
1390 if (kind == ATOMIC_TYPE_INVALID) {
1391 static const atomic_type_kind_t possible_kinds[] = {
1396 ATOMIC_TYPE_ULONGLONG
1398 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1399 if (get_atomic_type_size(possible_kinds[i]) == size) {
1400 kind = possible_kinds[i];
1410 * Hash the given type and return the "singleton" version
1413 static type_t *identify_new_type(type_t *type)
1415 type_t *result = typehash_insert(type);
1416 if (result != type) {
1417 obstack_free(type_obst, type);
1423 * Creates a new atomic type.
1425 * @param akind The kind of the atomic type.
1426 * @param qualifiers Type qualifiers for the new type.
1428 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1430 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1431 memset(type, 0, sizeof(atomic_type_t));
1433 type->kind = TYPE_ATOMIC;
1434 type->base.size = get_atomic_type_size(akind);
1435 type->base.alignment = get_atomic_type_alignment(akind);
1436 type->base.qualifiers = qualifiers;
1437 type->atomic.akind = akind;
1439 return identify_new_type(type);
1443 * Creates a new complex type.
1445 * @param akind The kind of the atomic type.
1446 * @param qualifiers Type qualifiers for the new type.
1448 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1450 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1451 memset(type, 0, sizeof(complex_type_t));
1453 type->kind = TYPE_COMPLEX;
1454 type->base.qualifiers = qualifiers;
1455 type->base.alignment = get_atomic_type_alignment(akind);
1456 type->complex.akind = akind;
1458 return identify_new_type(type);
1462 * Creates a new imaginary type.
1464 * @param akind The kind of the atomic type.
1465 * @param qualifiers Type qualifiers for the new type.
1467 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1469 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1470 memset(type, 0, sizeof(imaginary_type_t));
1472 type->kind = TYPE_IMAGINARY;
1473 type->base.qualifiers = qualifiers;
1474 type->base.alignment = get_atomic_type_alignment(akind);
1475 type->imaginary.akind = akind;
1477 return identify_new_type(type);
1481 * Creates a new pointer type.
1483 * @param points_to The points-to type for the new type.
1484 * @param qualifiers Type qualifiers for the new type.
1486 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1488 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1489 memset(type, 0, sizeof(pointer_type_t));
1491 type->kind = TYPE_POINTER;
1492 type->base.qualifiers = qualifiers;
1493 type->base.alignment = 0;
1494 type->pointer.points_to = points_to;
1495 type->pointer.base_variable = NULL;
1497 return identify_new_type(type);
1501 * Creates a new reference type.
1503 * @param refers_to The referred-to type for the new type.
1505 type_t *make_reference_type(type_t *refers_to)
1507 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1508 memset(type, 0, sizeof(reference_type_t));
1510 type->kind = TYPE_REFERENCE;
1511 type->base.qualifiers = 0;
1512 type->base.alignment = 0;
1513 type->reference.refers_to = refers_to;
1515 return identify_new_type(type);
1519 * Creates a new based pointer type.
1521 * @param points_to The points-to type for the new type.
1522 * @param qualifiers Type qualifiers for the new type.
1523 * @param variable The based variable
1525 type_t *make_based_pointer_type(type_t *points_to,
1526 type_qualifiers_t qualifiers, variable_t *variable)
1528 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1529 memset(type, 0, sizeof(pointer_type_t));
1531 type->kind = TYPE_POINTER;
1532 type->base.qualifiers = qualifiers;
1533 type->base.alignment = 0;
1534 type->pointer.points_to = points_to;
1535 type->pointer.base_variable = variable;
1537 return identify_new_type(type);
1541 type_t *make_array_type(type_t *element_type, size_t size,
1542 type_qualifiers_t qualifiers)
1544 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1545 memset(type, 0, sizeof(array_type_t));
1547 type->kind = TYPE_ARRAY;
1548 type->base.qualifiers = qualifiers;
1549 type->base.alignment = 0;
1550 type->array.element_type = element_type;
1551 type->array.size = size;
1552 type->array.size_constant = true;
1554 return identify_new_type(type);
1558 * Debug helper. Prints the given type to stdout.
1560 static __attribute__((unused))
1561 void dbg_type(const type_t *type)
1563 FILE *old_out = out;