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"
31 #include "lang_features.h"
33 static struct obstack _type_obst;
35 struct obstack *type_obst = &_type_obst;
36 static int type_visited = 0;
37 static bool print_implicit_array_size = false;
39 static void intern_print_type_pre(const type_t *type);
40 static void intern_print_type_post(const type_t *type);
42 typedef struct atomic_type_properties_t atomic_type_properties_t;
43 struct atomic_type_properties_t {
44 unsigned size; /**< type size in bytes */
45 unsigned alignment; /**< type alignment in bytes */
46 unsigned flags; /**< type flags from atomic_type_flag_t */
50 * Properties of atomic types.
52 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
53 //ATOMIC_TYPE_INVALID = 0,
54 [ATOMIC_TYPE_VOID] = {
57 .flags = ATOMIC_TYPE_FLAG_NONE
59 [ATOMIC_TYPE_WCHAR_T] = {
61 .alignment = (unsigned)-1,
62 /* signed flag will be set when known */
63 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
65 [ATOMIC_TYPE_CHAR] = {
68 /* signed flag will be set when known */
69 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
71 [ATOMIC_TYPE_SCHAR] = {
74 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
75 | ATOMIC_TYPE_FLAG_SIGNED,
77 [ATOMIC_TYPE_UCHAR] = {
80 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
82 [ATOMIC_TYPE_SHORT] = {
85 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
86 | ATOMIC_TYPE_FLAG_SIGNED
88 [ATOMIC_TYPE_USHORT] = {
91 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
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_UINT] = {
100 .size = (unsigned) -1,
101 .alignment = (unsigned) -1,
102 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
104 [ATOMIC_TYPE_LONG] = {
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_ULONG] = {
111 .size = (unsigned) -1,
112 .alignment = (unsigned) -1,
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
115 [ATOMIC_TYPE_LONGLONG] = {
116 .size = (unsigned) -1,
117 .alignment = (unsigned) -1,
118 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
119 | ATOMIC_TYPE_FLAG_SIGNED,
121 [ATOMIC_TYPE_ULONGLONG] = {
122 .size = (unsigned) -1,
123 .alignment = (unsigned) -1,
124 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
126 [ATOMIC_TYPE_BOOL] = {
127 .size = (unsigned) -1,
128 .alignment = (unsigned) -1,
129 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
131 [ATOMIC_TYPE_FLOAT] = {
133 .alignment = (unsigned) -1,
134 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
135 | ATOMIC_TYPE_FLAG_SIGNED,
137 [ATOMIC_TYPE_DOUBLE] = {
139 .alignment = (unsigned) -1,
140 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
141 | ATOMIC_TYPE_FLAG_SIGNED,
143 [ATOMIC_TYPE_LONG_DOUBLE] = {
145 .alignment = (unsigned) -1,
146 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
147 | ATOMIC_TYPE_FLAG_SIGNED,
149 /* complex and imaginary types are set in init_types */
152 void init_types(void)
154 obstack_init(type_obst);
156 atomic_type_properties_t *props = atomic_type_properties;
158 if (char_is_signed) {
159 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
162 unsigned int_size = machine_size < 32 ? 2 : 4;
163 unsigned long_size = machine_size < 64 ? 4 : 8;
164 unsigned llong_size = machine_size < 32 ? 4 : 8;
166 props[ATOMIC_TYPE_INT].size = int_size;
167 props[ATOMIC_TYPE_INT].alignment = int_size;
168 props[ATOMIC_TYPE_UINT].size = int_size;
169 props[ATOMIC_TYPE_UINT].alignment = int_size;
170 props[ATOMIC_TYPE_LONG].size = long_size;
171 props[ATOMIC_TYPE_LONG].alignment = long_size;
172 props[ATOMIC_TYPE_ULONG].size = long_size;
173 props[ATOMIC_TYPE_ULONG].alignment = long_size;
174 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
175 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
176 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
177 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
179 /* TODO: backend specific, need a way to query the backend for this.
180 * The following are good settings for x86 */
181 props[ATOMIC_TYPE_FLOAT].alignment = 4;
182 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
183 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
184 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
185 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
187 /* TODO: make this configurable for platforms which do not use byte sized
189 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
191 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
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)
212 if (qualifiers & TYPE_QUALIFIER_CONST) {
213 fputs(" const" + first, out);
216 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
217 fputs(" volatile" + first, out);
220 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
221 fputs(" restrict" + first, out);
226 const char *get_atomic_kind_name(atomic_type_kind_t kind)
229 case ATOMIC_TYPE_INVALID: break;
230 case ATOMIC_TYPE_VOID: return "void";
231 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
232 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
233 case ATOMIC_TYPE_CHAR: return "char";
234 case ATOMIC_TYPE_SCHAR: return "signed char";
235 case ATOMIC_TYPE_UCHAR: return "unsigned char";
236 case ATOMIC_TYPE_INT: return "int";
237 case ATOMIC_TYPE_UINT: return "unsigned int";
238 case ATOMIC_TYPE_SHORT: return "short";
239 case ATOMIC_TYPE_USHORT: return "unsigned short";
240 case ATOMIC_TYPE_LONG: return "long";
241 case ATOMIC_TYPE_ULONG: return "unsigned long";
242 case ATOMIC_TYPE_LONGLONG: return "long long";
243 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
244 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
245 case ATOMIC_TYPE_FLOAT: return "float";
246 case ATOMIC_TYPE_DOUBLE: return "double";
248 return "INVALIDATOMIC";
252 * Prints the name of an atomic type kinds.
254 * @param kind The type kind.
256 static void print_atomic_kinds(atomic_type_kind_t kind)
258 const char *s = get_atomic_kind_name(kind);
263 * Prints the name of an atomic type.
265 * @param type The type.
267 static void print_atomic_type(const atomic_type_t *type)
269 print_type_qualifiers(type->base.qualifiers);
270 if (type->base.qualifiers != 0)
272 print_atomic_kinds(type->akind);
276 * Prints the name of a complex type.
278 * @param type The type.
281 void print_complex_type(const complex_type_t *type)
283 int empty = type->base.qualifiers == 0;
284 print_type_qualifiers(type->base.qualifiers);
285 fputs(" _Complex " + empty, out);
286 print_atomic_kinds(type->akind);
290 * Prints the name of an imaginary type.
292 * @param type The type.
295 void print_imaginary_type(const imaginary_type_t *type)
297 int empty = type->base.qualifiers == 0;
298 print_type_qualifiers(type->base.qualifiers);
299 fputs(" _Imaginary " + empty, out);
300 print_atomic_kinds(type->akind);
304 * Print the first part (the prefix) of a type.
306 * @param type The type to print.
308 static void print_function_type_pre(const function_type_t *type)
310 switch (type->linkage) {
311 case LINKAGE_INVALID:
316 fputs("extern \"C\" ", out);
320 if (!(c_mode & _CXX))
321 fputs("extern \"C++\" ", out);
325 print_type_qualifiers(type->base.qualifiers);
326 if (type->base.qualifiers != 0)
329 intern_print_type_pre(type->return_type);
331 switch (type->calling_convention) {
332 case CC_CDECL: fputs("__cdecl ", out); break;
333 case CC_STDCALL: fputs("__stdcall ", out); break;
334 case CC_FASTCALL: fputs("__fastcall ", out); break;
335 case CC_THISCALL: fputs("__thiscall ", out); break;
336 case CC_DEFAULT: break;
341 * Print the second part (the postfix) of a type.
343 * @param type The type to print.
345 static void print_function_type_post(const function_type_t *type,
346 const scope_t *parameters)
350 if (parameters == NULL) {
351 function_parameter_t *parameter = type->parameters;
352 for( ; parameter != NULL; parameter = parameter->next) {
358 print_type(parameter->type);
361 entity_t *parameter = parameters->entities;
362 for (; parameter != NULL; parameter = parameter->base.next) {
363 if (parameter->kind != ENTITY_PARAMETER)
371 const type_t *const type = parameter->declaration.type;
373 fputs(parameter->base.symbol->string, out);
375 print_type_ext(type, parameter->base.symbol, NULL);
379 if (type->variadic) {
387 if (first && !type->unspecified_parameters) {
392 intern_print_type_post(type->return_type);
396 * Prints the prefix part of a pointer type.
398 * @param type The pointer type.
400 static void print_pointer_type_pre(const pointer_type_t *type)
402 type_t const *const points_to = type->points_to;
403 intern_print_type_pre(points_to);
404 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
406 variable_t *const variable = type->base_variable;
407 if (variable != NULL) {
408 fputs(" __based(", out);
409 fputs(variable->base.base.symbol->string, out);
413 type_qualifiers_t const qual = type->base.qualifiers;
416 print_type_qualifiers(qual);
420 * Prints the postfix part of a pointer type.
422 * @param type The pointer type.
424 static void print_pointer_type_post(const pointer_type_t *type)
426 type_t const *const points_to = type->points_to;
427 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
429 intern_print_type_post(points_to);
433 * Prints the prefix part of a reference type.
435 * @param type The reference type.
437 static void print_reference_type_pre(const reference_type_t *type)
439 type_t const *const refers_to = type->refers_to;
440 intern_print_type_pre(refers_to);
441 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
447 * Prints the postfix part of a reference type.
449 * @param type The reference type.
451 static void print_reference_type_post(const reference_type_t *type)
453 type_t const *const refers_to = type->refers_to;
454 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
456 intern_print_type_post(refers_to);
460 * Prints the prefix part of an array type.
462 * @param type The array type.
464 static void print_array_type_pre(const array_type_t *type)
466 intern_print_type_pre(type->element_type);
470 * Prints the postfix part of an array type.
472 * @param type The array type.
474 static void print_array_type_post(const array_type_t *type)
477 if (type->is_static) {
478 fputs("static ", out);
480 print_type_qualifiers(type->base.qualifiers);
481 if (type->base.qualifiers != 0)
483 if (type->size_expression != NULL
484 && (print_implicit_array_size || !type->has_implicit_size)) {
485 print_expression(type->size_expression);
488 intern_print_type_post(type->element_type);
492 * Prints the postfix part of a bitfield type.
494 * @param type The array type.
496 static void print_bitfield_type_post(const bitfield_type_t *type)
499 print_expression(type->size_expression);
500 intern_print_type_post(type->base_type);
504 * Prints an enum definition.
506 * @param declaration The enum's type declaration.
508 void print_enum_definition(const enum_t *enume)
514 entity_t *entry = enume->base.next;
515 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
516 entry = entry->base.next) {
519 fputs(entry->base.symbol->string, out);
520 if (entry->enum_value.value != NULL) {
523 /* skip the implicit cast */
524 expression_t *expression = entry->enum_value.value;
525 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
526 expression = expression->unary.value;
528 print_expression(expression);
539 * Prints an enum type.
541 * @param type The enum type.
543 static void print_type_enum(const enum_type_t *type)
545 int empty = type->base.qualifiers == 0;
546 print_type_qualifiers(type->base.qualifiers);
547 fputs(" enum " + empty, out);
549 enum_t *enume = type->enume;
550 symbol_t *symbol = enume->base.symbol;
551 if (symbol != NULL) {
552 fputs(symbol->string, out);
554 print_enum_definition(enume);
559 * Print the compound part of a compound type.
561 void print_compound_definition(const compound_t *compound)
566 entity_t *entity = compound->members.entities;
567 for( ; entity != NULL; entity = entity->base.next) {
568 if (entity->kind != ENTITY_COMPOUND_MEMBER)
572 print_entity(entity);
579 if (compound->modifiers & DM_TRANSPARENT_UNION) {
580 fputs("__attribute__((__transparent_union__))", out);
585 * Prints a compound type.
587 * @param type The compound type.
589 static void print_compound_type(const compound_type_t *type)
591 int empty = type->base.qualifiers == 0;
592 print_type_qualifiers(type->base.qualifiers);
594 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
595 fputs(" struct " + empty, out);
597 assert(type->base.kind == TYPE_COMPOUND_UNION);
598 fputs(" union " + empty, out);
601 compound_t *compound = type->compound;
602 symbol_t *symbol = compound->base.symbol;
603 if (symbol != NULL) {
604 fputs(symbol->string, out);
606 print_compound_definition(compound);
611 * Prints the prefix part of a typedef type.
613 * @param type The typedef type.
615 static void print_typedef_type_pre(const typedef_type_t *const type)
617 print_type_qualifiers(type->base.qualifiers);
618 if (type->base.qualifiers != 0)
620 fputs(type->typedefe->base.symbol->string, out);
624 * Prints the prefix part of a typeof type.
626 * @param type The typeof type.
628 static void print_typeof_type_pre(const typeof_type_t *const type)
630 fputs("typeof(", out);
631 if (type->expression != NULL) {
632 print_expression(type->expression);
634 print_type(type->typeof_type);
640 * Prints the prefix part of a type.
642 * @param type The type.
644 static void intern_print_type_pre(const type_t *const type)
648 fputs("<error>", out);
651 fputs("<invalid>", out);
654 print_type_enum(&type->enumt);
657 print_atomic_type(&type->atomic);
660 print_complex_type(&type->complex);
663 print_imaginary_type(&type->imaginary);
665 case TYPE_COMPOUND_STRUCT:
666 case TYPE_COMPOUND_UNION:
667 print_compound_type(&type->compound);
670 fputs(type->builtin.symbol->string, out);
673 print_function_type_pre(&type->function);
676 print_pointer_type_pre(&type->pointer);
679 print_reference_type_pre(&type->reference);
682 intern_print_type_pre(type->bitfield.base_type);
685 print_array_type_pre(&type->array);
688 print_typedef_type_pre(&type->typedeft);
691 print_typeof_type_pre(&type->typeoft);
694 fputs("unknown", out);
698 * Prints the postfix part of a type.
700 * @param type The type.
702 static void intern_print_type_post(const type_t *const type)
706 print_function_type_post(&type->function, NULL);
709 print_pointer_type_post(&type->pointer);
712 print_reference_type_post(&type->reference);
715 print_array_type_post(&type->array);
718 print_bitfield_type_post(&type->bitfield);
726 case TYPE_COMPOUND_STRUCT:
727 case TYPE_COMPOUND_UNION:
734 if (type->base.modifiers & DM_TRANSPARENT_UNION) {
735 fputs("__attribute__((__transparent_union__))", out);
742 * @param type The type.
744 void print_type(const type_t *const type)
746 print_type_ext(type, NULL, NULL);
749 void print_type_ext(const type_t *const type, const symbol_t *symbol,
750 const scope_t *parameters)
753 fputs("nil type", out);
757 intern_print_type_pre(type);
758 if (symbol != NULL) {
760 fputs(symbol->string, out);
762 if (type->kind == TYPE_FUNCTION) {
763 print_function_type_post(&type->function, parameters);
765 intern_print_type_post(type);
770 * Return the size of a type AST node.
772 * @param type The type.
774 static size_t get_type_size(const type_t *type)
777 case TYPE_ATOMIC: return sizeof(atomic_type_t);
778 case TYPE_COMPLEX: return sizeof(complex_type_t);
779 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
780 case TYPE_COMPOUND_STRUCT:
781 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
782 case TYPE_ENUM: return sizeof(enum_type_t);
783 case TYPE_FUNCTION: return sizeof(function_type_t);
784 case TYPE_POINTER: return sizeof(pointer_type_t);
785 case TYPE_REFERENCE: return sizeof(reference_type_t);
786 case TYPE_ARRAY: return sizeof(array_type_t);
787 case TYPE_BUILTIN: return sizeof(builtin_type_t);
788 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
789 case TYPE_TYPEOF: return sizeof(typeof_type_t);
790 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
791 case TYPE_ERROR: panic("error type found");
792 case TYPE_INVALID: panic("invalid type found");
794 panic("unknown type found");
800 * @param type The type to copy.
801 * @return A copy of the type.
803 * @note This does not produce a deep copy!
805 type_t *duplicate_type(const type_t *type)
807 size_t size = get_type_size(type);
809 type_t *copy = obstack_alloc(type_obst, size);
810 memcpy(copy, type, size);
811 copy->base.firm_type = NULL;
817 * Returns the unqualified type of a given type.
819 * @param type The type.
820 * @returns The unqualified type.
822 type_t *get_unqualified_type(type_t *type)
824 assert(!is_typeref(type));
826 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
829 type_t *unqualified_type = duplicate_type(type);
830 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
832 return identify_new_type(unqualified_type);
835 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
837 type_t *type = skip_typeref(orig_type);
840 if (is_type_array(type)) {
841 /* For array types the element type has to be adjusted */
842 type_t *element_type = type->array.element_type;
843 type_t *qual_element_type = get_qualified_type(element_type, qual);
845 if (qual_element_type == element_type)
848 copy = duplicate_type(type);
849 copy->array.element_type = qual_element_type;
850 } else if (is_type_valid(type)) {
851 if ((type->base.qualifiers & qual) == qual)
854 copy = duplicate_type(type);
855 copy->base.qualifiers |= qual;
860 return identify_new_type(copy);
864 * Check if a type is valid.
866 * @param type The type to check.
867 * @return true if type represents a valid type.
869 bool type_valid(const type_t *type)
871 return type->kind != TYPE_INVALID;
874 static bool test_atomic_type_flag(atomic_type_kind_t kind,
875 atomic_type_flag_t flag)
877 assert(kind <= ATOMIC_TYPE_LAST);
878 return (atomic_type_properties[kind].flags & flag) != 0;
882 * Returns true if the given type is an integer type.
884 * @param type The type to check.
885 * @return True if type is an integer type.
887 bool is_type_integer(const type_t *type)
889 assert(!is_typeref(type));
891 if (type->kind == TYPE_ENUM)
893 if (type->kind == TYPE_BITFIELD)
896 if (type->kind != TYPE_ATOMIC)
899 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
903 * Returns true if the given type is an enum type.
905 * @param type The type to check.
906 * @return True if type is an enum type.
908 bool is_type_enum(const type_t *type)
910 assert(!is_typeref(type));
911 return type->kind == TYPE_ENUM;
915 * Returns true if the given type is an floating point type.
917 * @param type The type to check.
918 * @return True if type is a floating point type.
920 bool is_type_float(const type_t *type)
922 assert(!is_typeref(type));
924 if (type->kind != TYPE_ATOMIC)
927 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
931 * Returns true if the given type is an complex type.
933 * @param type The type to check.
934 * @return True if type is a complex type.
936 bool is_type_complex(const type_t *type)
938 assert(!is_typeref(type));
940 if (type->kind != TYPE_ATOMIC)
943 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
947 * Returns true if the given type is a signed type.
949 * @param type The type to check.
950 * @return True if type is a signed type.
952 bool is_type_signed(const type_t *type)
954 assert(!is_typeref(type));
956 /* enum types are int for now */
957 if (type->kind == TYPE_ENUM)
959 if (type->kind == TYPE_BITFIELD)
960 return is_type_signed(type->bitfield.base_type);
962 if (type->kind != TYPE_ATOMIC)
965 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
969 * Returns true if the given type represents an arithmetic type.
971 * @param type The type to check.
972 * @return True if type represents an arithmetic type.
974 bool is_type_arithmetic(const type_t *type)
976 assert(!is_typeref(type));
983 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
985 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
987 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
994 * Returns true if the given type is an integer or float type.
996 * @param type The type to check.
997 * @return True if type is an integer or float type.
999 bool is_type_real(const type_t *type)
1002 return is_type_integer(type) || is_type_float(type);
1006 * Returns true if the given type represents a scalar type.
1008 * @param type The type to check.
1009 * @return True if type represents a scalar type.
1011 bool is_type_scalar(const type_t *type)
1013 assert(!is_typeref(type));
1015 switch (type->kind) {
1016 case TYPE_POINTER: return true;
1017 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1021 return is_type_arithmetic(type);
1025 * Check if a given type is incomplete.
1027 * @param type The type to check.
1028 * @return True if the given type is incomplete (ie. just forward).
1030 bool is_type_incomplete(const type_t *type)
1032 assert(!is_typeref(type));
1034 switch(type->kind) {
1035 case TYPE_COMPOUND_STRUCT:
1036 case TYPE_COMPOUND_UNION: {
1037 const compound_type_t *compound_type = &type->compound;
1038 return !compound_type->compound->complete;
1044 return type->array.size_expression == NULL
1045 && !type->array.size_constant;
1048 return type->atomic.akind == ATOMIC_TYPE_VOID;
1051 return type->complex.akind == ATOMIC_TYPE_VOID;
1053 case TYPE_IMAGINARY:
1054 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1059 case TYPE_REFERENCE:
1066 panic("is_type_incomplete called without typerefs skipped");
1071 panic("invalid type found");
1074 bool is_type_object(const type_t *type)
1076 return !is_type_function(type) && !is_type_incomplete(type);
1080 * Check if two function types are compatible.
1082 static bool function_types_compatible(const function_type_t *func1,
1083 const function_type_t *func2)
1085 const type_t* const ret1 = skip_typeref(func1->return_type);
1086 const type_t* const ret2 = skip_typeref(func2->return_type);
1087 if (!types_compatible(ret1, ret2))
1090 if (func1->linkage != func2->linkage)
1093 if (func1->calling_convention != func2->calling_convention)
1096 /* can parameters be compared? */
1097 if (func1->unspecified_parameters || func2->unspecified_parameters)
1100 if (func1->variadic != func2->variadic)
1103 /* TODO: handling of unspecified parameters not correct yet */
1105 /* all argument types must be compatible */
1106 function_parameter_t *parameter1 = func1->parameters;
1107 function_parameter_t *parameter2 = func2->parameters;
1108 for ( ; parameter1 != NULL && parameter2 != NULL;
1109 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1110 type_t *parameter1_type = skip_typeref(parameter1->type);
1111 type_t *parameter2_type = skip_typeref(parameter2->type);
1113 parameter1_type = get_unqualified_type(parameter1_type);
1114 parameter2_type = get_unqualified_type(parameter2_type);
1116 if (!types_compatible(parameter1_type, parameter2_type))
1119 /* same number of arguments? */
1120 if (parameter1 != NULL || parameter2 != NULL)
1127 * Check if two array types are compatible.
1129 static bool array_types_compatible(const array_type_t *array1,
1130 const array_type_t *array2)
1132 type_t *element_type1 = skip_typeref(array1->element_type);
1133 type_t *element_type2 = skip_typeref(array2->element_type);
1134 if (!types_compatible(element_type1, element_type2))
1137 if (!array1->size_constant || !array2->size_constant)
1140 return array1->size == array2->size;
1144 * Check if two types are compatible.
1146 bool types_compatible(const type_t *type1, const type_t *type2)
1148 assert(!is_typeref(type1));
1149 assert(!is_typeref(type2));
1151 /* shortcut: the same type is always compatible */
1155 if (!is_type_valid(type1) || !is_type_valid(type2))
1158 if (type1->base.qualifiers != type2->base.qualifiers)
1160 if (type1->kind != type2->kind)
1163 switch (type1->kind) {
1165 return function_types_compatible(&type1->function, &type2->function);
1167 return type1->atomic.akind == type2->atomic.akind;
1169 return type1->complex.akind == type2->complex.akind;
1170 case TYPE_IMAGINARY:
1171 return type1->imaginary.akind == type2->imaginary.akind;
1173 return array_types_compatible(&type1->array, &type2->array);
1175 case TYPE_POINTER: {
1176 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1177 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1178 return types_compatible(to1, to2);
1181 case TYPE_REFERENCE: {
1182 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1183 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1184 return types_compatible(to1, to2);
1187 case TYPE_COMPOUND_STRUCT:
1188 case TYPE_COMPOUND_UNION:
1191 /* TODO: not implemented */
1195 /* not sure if this makes sense or is even needed, implement it if you
1196 * really need it! */
1197 panic("type compatibility check for bitfield type");
1200 /* Hmm, the error type should be compatible to all other types */
1203 panic("invalid type found in compatible types");
1206 panic("typerefs not skipped in compatible types?!?");
1209 /* TODO: incomplete */
1214 * Skip all typerefs and return the underlying type.
1216 type_t *skip_typeref(type_t *type)
1218 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1219 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1220 il_alignment_t alignment = 0;
1223 if (alignment < type->base.alignment)
1224 alignment = type->base.alignment;
1226 switch (type->kind) {
1229 case TYPE_TYPEDEF: {
1230 qualifiers |= type->base.qualifiers;
1231 modifiers |= type->base.modifiers;
1233 const typedef_type_t *typedef_type = &type->typedeft;
1234 if (typedef_type->resolved_type != NULL) {
1235 type = typedef_type->resolved_type;
1238 type = typedef_type->typedefe->type;
1242 qualifiers |= type->base.qualifiers;
1243 modifiers |= type->base.modifiers;
1244 type = type->typeoft.typeof_type;
1252 if (qualifiers != TYPE_QUALIFIER_NONE ||
1253 modifiers != TYPE_MODIFIER_NONE ||
1254 alignment > type->base.alignment) {
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 element_type->base.alignment = alignment;
1265 copy->array.element_type = element_type;
1267 copy->base.qualifiers |= qualifiers;
1268 copy->base.modifiers |= modifiers;
1269 copy->base.alignment = alignment;
1272 type = identify_new_type(copy);
1278 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 type = type->typeoft.typeof_type;
1298 if (skip_array_type) {
1299 type = type->array.element_type;
1308 return type->base.qualifiers | qualifiers;
1311 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1313 assert(kind <= ATOMIC_TYPE_LAST);
1314 return atomic_type_properties[kind].size;
1317 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1319 assert(kind <= ATOMIC_TYPE_LAST);
1320 return atomic_type_properties[kind].alignment;
1323 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1325 assert(kind <= ATOMIC_TYPE_LAST);
1326 return atomic_type_properties[kind].flags;
1329 atomic_type_kind_t get_intptr_kind(void)
1331 if (machine_size <= 32)
1332 return ATOMIC_TYPE_INT;
1333 else if (machine_size <= 64)
1334 return ATOMIC_TYPE_LONG;
1336 return ATOMIC_TYPE_LONGLONG;
1339 atomic_type_kind_t get_uintptr_kind(void)
1341 if (machine_size <= 32)
1342 return ATOMIC_TYPE_UINT;
1343 else if (machine_size <= 64)
1344 return ATOMIC_TYPE_ULONG;
1346 return ATOMIC_TYPE_ULONGLONG;
1350 * Find the atomic type kind representing a given size (signed).
1352 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1354 static atomic_type_kind_t kinds[32];
1357 atomic_type_kind_t kind = kinds[size];
1358 if (kind == ATOMIC_TYPE_INVALID) {
1359 static const atomic_type_kind_t possible_kinds[] = {
1364 ATOMIC_TYPE_LONGLONG
1366 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1367 if (get_atomic_type_size(possible_kinds[i]) == size) {
1368 kind = possible_kinds[i];
1378 * Find the atomic type kind representing a given size (signed).
1380 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1382 static atomic_type_kind_t kinds[32];
1385 atomic_type_kind_t kind = kinds[size];
1386 if (kind == ATOMIC_TYPE_INVALID) {
1387 static const atomic_type_kind_t possible_kinds[] = {
1392 ATOMIC_TYPE_ULONGLONG
1394 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1395 if (get_atomic_type_size(possible_kinds[i]) == size) {
1396 kind = possible_kinds[i];
1406 * Hash the given type and return the "singleton" version
1409 type_t *identify_new_type(type_t *type)
1411 type_t *result = typehash_insert(type);
1412 if (result != type) {
1413 obstack_free(type_obst, type);
1419 * Creates a new atomic type.
1421 * @param akind The kind of the atomic type.
1422 * @param qualifiers Type qualifiers for the new type.
1424 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1426 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1427 memset(type, 0, sizeof(atomic_type_t));
1429 type->kind = TYPE_ATOMIC;
1430 type->base.size = get_atomic_type_size(akind);
1431 type->base.alignment = get_atomic_type_alignment(akind);
1432 type->base.qualifiers = qualifiers;
1433 type->atomic.akind = akind;
1435 return identify_new_type(type);
1439 * Creates a new complex type.
1441 * @param akind The kind of the atomic type.
1442 * @param qualifiers Type qualifiers for the new type.
1444 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1446 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1447 memset(type, 0, sizeof(complex_type_t));
1449 type->kind = TYPE_COMPLEX;
1450 type->base.qualifiers = qualifiers;
1451 type->base.alignment = get_atomic_type_alignment(akind);
1452 type->complex.akind = akind;
1454 return identify_new_type(type);
1458 * Creates a new imaginary type.
1460 * @param akind The kind of the atomic type.
1461 * @param qualifiers Type qualifiers for the new type.
1463 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1465 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1466 memset(type, 0, sizeof(imaginary_type_t));
1468 type->kind = TYPE_IMAGINARY;
1469 type->base.qualifiers = qualifiers;
1470 type->base.alignment = get_atomic_type_alignment(akind);
1471 type->imaginary.akind = akind;
1473 return identify_new_type(type);
1477 * Creates a new pointer type.
1479 * @param points_to The points-to type for the new type.
1480 * @param qualifiers Type qualifiers for the new type.
1482 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1484 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1485 memset(type, 0, sizeof(pointer_type_t));
1487 type->kind = TYPE_POINTER;
1488 type->base.qualifiers = qualifiers;
1489 type->base.alignment = 0;
1490 type->pointer.points_to = points_to;
1491 type->pointer.base_variable = NULL;
1493 return identify_new_type(type);
1497 * Creates a new reference type.
1499 * @param refers_to The referred-to type for the new type.
1501 type_t *make_reference_type(type_t *refers_to)
1503 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1504 memset(type, 0, sizeof(reference_type_t));
1506 type->kind = TYPE_REFERENCE;
1507 type->base.qualifiers = 0;
1508 type->base.alignment = 0;
1509 type->reference.refers_to = refers_to;
1511 return identify_new_type(type);
1515 * Creates a new based pointer type.
1517 * @param points_to The points-to type for the new type.
1518 * @param qualifiers Type qualifiers for the new type.
1519 * @param variable The based variable
1521 type_t *make_based_pointer_type(type_t *points_to,
1522 type_qualifiers_t qualifiers, variable_t *variable)
1524 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1525 memset(type, 0, sizeof(pointer_type_t));
1527 type->kind = TYPE_POINTER;
1528 type->base.qualifiers = qualifiers;
1529 type->base.alignment = 0;
1530 type->pointer.points_to = points_to;
1531 type->pointer.base_variable = variable;
1533 return identify_new_type(type);
1537 type_t *make_array_type(type_t *element_type, size_t size,
1538 type_qualifiers_t qualifiers)
1540 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1541 memset(type, 0, sizeof(array_type_t));
1543 type->kind = TYPE_ARRAY;
1544 type->base.qualifiers = qualifiers;
1545 type->base.alignment = 0;
1546 type->array.element_type = element_type;
1547 type->array.size = size;
1548 type->array.size_constant = true;
1550 return identify_new_type(type);
1554 * Debug helper. Prints the given type to stdout.
1556 static __attribute__((unused))
1557 void dbg_type(const type_t *type)
1559 FILE *old_out = out;