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, bool top);
40 static void intern_print_type_post(const type_t *type, bool top);
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 */
49 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
50 //ATOMIC_TYPE_INVALID = 0,
51 [ATOMIC_TYPE_VOID] = {
54 .flags = ATOMIC_TYPE_FLAG_NONE
56 [ATOMIC_TYPE_WCHAR_T] = {
58 .alignment = (unsigned)-1,
59 /* signed flag will be set when known */
60 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
62 [ATOMIC_TYPE_CHAR] = {
65 /* signed flag will be set when known */
66 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
68 [ATOMIC_TYPE_SCHAR] = {
71 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
72 | ATOMIC_TYPE_FLAG_SIGNED,
74 [ATOMIC_TYPE_UCHAR] = {
77 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
79 [ATOMIC_TYPE_SHORT] = {
82 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
83 | ATOMIC_TYPE_FLAG_SIGNED
85 [ATOMIC_TYPE_USHORT] = {
88 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
91 .size = (unsigned) -1,
92 .alignment = (unsigned) -1,
93 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
94 | ATOMIC_TYPE_FLAG_SIGNED,
96 [ATOMIC_TYPE_UINT] = {
97 .size = (unsigned) -1,
98 .alignment = (unsigned) -1,
99 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
101 [ATOMIC_TYPE_LONG] = {
102 .size = (unsigned) -1,
103 .alignment = (unsigned) -1,
104 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
105 | ATOMIC_TYPE_FLAG_SIGNED,
107 [ATOMIC_TYPE_ULONG] = {
108 .size = (unsigned) -1,
109 .alignment = (unsigned) -1,
110 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
112 [ATOMIC_TYPE_LONGLONG] = {
113 .size = (unsigned) -1,
114 .alignment = (unsigned) -1,
115 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
116 | ATOMIC_TYPE_FLAG_SIGNED,
118 [ATOMIC_TYPE_ULONGLONG] = {
119 .size = (unsigned) -1,
120 .alignment = (unsigned) -1,
121 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
123 [ATOMIC_TYPE_BOOL] = {
124 .size = (unsigned) -1,
125 .alignment = (unsigned) -1,
126 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
128 [ATOMIC_TYPE_FLOAT] = {
130 .alignment = (unsigned) -1,
131 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
132 | ATOMIC_TYPE_FLAG_SIGNED,
134 [ATOMIC_TYPE_DOUBLE] = {
136 .alignment = (unsigned) -1,
137 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
138 | ATOMIC_TYPE_FLAG_SIGNED,
140 [ATOMIC_TYPE_LONG_DOUBLE] = {
142 .alignment = (unsigned) -1,
143 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
144 | ATOMIC_TYPE_FLAG_SIGNED,
146 /* complex and imaginary types are set in init_types */
149 void init_types(void)
151 obstack_init(type_obst);
153 atomic_type_properties_t *props = atomic_type_properties;
155 if (char_is_signed) {
156 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
159 unsigned int_size = machine_size < 32 ? 2 : 4;
160 unsigned long_size = machine_size < 64 ? 4 : 8;
161 unsigned llong_size = machine_size < 32 ? 4 : 8;
163 props[ATOMIC_TYPE_INT].size = int_size;
164 props[ATOMIC_TYPE_INT].alignment = int_size;
165 props[ATOMIC_TYPE_UINT].size = int_size;
166 props[ATOMIC_TYPE_UINT].alignment = int_size;
167 props[ATOMIC_TYPE_LONG].size = long_size;
168 props[ATOMIC_TYPE_LONG].alignment = long_size;
169 props[ATOMIC_TYPE_ULONG].size = long_size;
170 props[ATOMIC_TYPE_ULONG].alignment = long_size;
171 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
172 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
173 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
174 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
176 /* TODO: backend specific, need a way to query the backend for this.
177 * The following are good settings for x86 */
178 props[ATOMIC_TYPE_FLOAT].alignment = 4;
179 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
180 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
181 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
182 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
184 /* TODO: make this configurable for platforms which do not use byte sized
186 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
188 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
191 void exit_types(void)
193 obstack_free(type_obst, NULL);
196 void type_set_output(FILE *stream)
201 void inc_type_visited(void)
206 void print_type_qualifiers(type_qualifiers_t qualifiers)
209 if (qualifiers & TYPE_QUALIFIER_CONST) {
210 fputs(" const" + first, out);
213 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
214 fputs(" volatile" + first, out);
217 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
218 fputs(" restrict" + first, out);
223 const char *get_atomic_kind_name(atomic_type_kind_t kind)
226 case ATOMIC_TYPE_INVALID: break;
227 case ATOMIC_TYPE_VOID: return "void";
228 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
229 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
230 case ATOMIC_TYPE_CHAR: return "char";
231 case ATOMIC_TYPE_SCHAR: return "signed char";
232 case ATOMIC_TYPE_UCHAR: return "unsigned char";
233 case ATOMIC_TYPE_INT: return "int";
234 case ATOMIC_TYPE_UINT: return "unsigned int";
235 case ATOMIC_TYPE_SHORT: return "short";
236 case ATOMIC_TYPE_USHORT: return "unsigned short";
237 case ATOMIC_TYPE_LONG: return "long";
238 case ATOMIC_TYPE_ULONG: return "unsigned long";
239 case ATOMIC_TYPE_LONGLONG: return "long long";
240 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
241 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
242 case ATOMIC_TYPE_FLOAT: return "float";
243 case ATOMIC_TYPE_DOUBLE: return "double";
245 return "INVALIDATOMIC";
249 * Prints the name of an atomic type kinds.
251 * @param kind The type kind.
253 static void print_atomic_kinds(atomic_type_kind_t kind)
255 const char *s = get_atomic_kind_name(kind);
260 * Prints the name of an atomic type.
262 * @param type The type.
264 static void print_atomic_type(const atomic_type_t *type)
266 print_type_qualifiers(type->base.qualifiers);
267 if (type->base.qualifiers != 0)
269 print_atomic_kinds(type->akind);
273 * Prints the name of a complex type.
275 * @param type The type.
278 void print_complex_type(const complex_type_t *type)
280 int empty = type->base.qualifiers == 0;
281 print_type_qualifiers(type->base.qualifiers);
282 fputs(" _Complex " + empty, out);
283 print_atomic_kinds(type->akind);
287 * Prints the name of an imaginary type.
289 * @param type The type.
292 void print_imaginary_type(const imaginary_type_t *type)
294 int empty = type->base.qualifiers == 0;
295 print_type_qualifiers(type->base.qualifiers);
296 fputs(" _Imaginary " + empty, out);
297 print_atomic_kinds(type->akind);
301 * Print the first part (the prefix) of a type.
303 * @param type The type to print.
304 * @param top true, if this is the top type, false if it's an embedded type.
306 static void print_function_type_pre(const function_type_t *type, bool top)
308 switch (type->linkage) {
309 case LINKAGE_INVALID:
314 fputs("extern \"C\" ", out);
318 if (!(c_mode & _CXX))
319 fputs("extern \"C++\" ", out);
323 print_type_qualifiers(type->base.qualifiers);
324 if (type->base.qualifiers != 0)
327 intern_print_type_pre(type->return_type, false);
329 switch (type->calling_convention) {
330 case CC_CDECL: fputs("__cdecl ", out); break;
331 case CC_STDCALL: fputs("__stdcall ", out); break;
332 case CC_FASTCALL: fputs("__fastcall ", out); break;
333 case CC_THISCALL: fputs("__thiscall ", out); break;
334 case CC_DEFAULT: break;
337 /* don't emit parenthesis if we're the toplevel type... */
343 * Print the second part (the postfix) of a type.
345 * @param type The type to print.
346 * @param top true, if this is the top type, false if it's an embedded type.
348 static void print_function_type_post(const function_type_t *type,
349 const scope_t *parameters, bool top)
351 /* don't emit parenthesis if we're the toplevel type... */
357 if (parameters == NULL) {
358 function_parameter_t *parameter = type->parameters;
359 for( ; parameter != NULL; parameter = parameter->next) {
365 print_type(parameter->type);
368 entity_t *parameter = parameters->entities;
369 for (; parameter != NULL; parameter = parameter->base.next) {
370 if (parameter->kind != ENTITY_PARAMETER)
378 const type_t *const type = parameter->declaration.type;
380 fputs(parameter->base.symbol->string, out);
382 print_type_ext(type, parameter->base.symbol, NULL);
386 if (type->variadic) {
394 if (first && !type->unspecified_parameters) {
399 intern_print_type_post(type->return_type, false);
403 * Prints the prefix part of a pointer type.
405 * @param type The pointer type.
407 static void print_pointer_type_pre(const pointer_type_t *type)
409 intern_print_type_pre(type->points_to, false);
410 variable_t *const variable = type->base_variable;
411 if (variable != NULL) {
412 fputs(" __based(", out);
413 fputs(variable->base.base.symbol->string, out);
417 type_qualifiers_t const qual = type->base.qualifiers;
420 print_type_qualifiers(qual);
424 * Prints the prefix part of a reference type.
426 * @param type The reference type.
428 static void print_reference_type_pre(const reference_type_t *type)
430 intern_print_type_pre(type->refers_to, false);
435 * Prints the postfix part of a pointer type.
437 * @param type The pointer type.
439 static void print_pointer_type_post(const pointer_type_t *type)
441 intern_print_type_post(type->points_to, false);
445 * Prints the postfix part of a reference type.
447 * @param type The reference type.
449 static void print_reference_type_post(const reference_type_t *type)
451 intern_print_type_post(type->refers_to, false);
455 * Prints the prefix part of an array type.
457 * @param type The array type.
459 static void print_array_type_pre(const array_type_t *type)
461 intern_print_type_pre(type->element_type, false);
465 * Prints the postfix part of an array type.
467 * @param type The array type.
469 static void print_array_type_post(const array_type_t *type)
472 if (type->is_static) {
473 fputs("static ", out);
475 print_type_qualifiers(type->base.qualifiers);
476 if (type->base.qualifiers != 0)
478 if (type->size_expression != NULL
479 && (print_implicit_array_size || !type->has_implicit_size)) {
480 print_expression(type->size_expression);
483 intern_print_type_post(type->element_type, false);
487 * Prints the postfix part of a bitfield type.
489 * @param type The array type.
491 static void print_bitfield_type_post(const bitfield_type_t *type)
494 print_expression(type->size_expression);
495 intern_print_type_post(type->base_type, false);
499 * Prints an enum definition.
501 * @param declaration The enum's type declaration.
503 void print_enum_definition(const enum_t *enume)
509 entity_t *entry = enume->base.next;
510 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
511 entry = entry->base.next) {
514 fputs(entry->base.symbol->string, out);
515 if (entry->enum_value.value != NULL) {
518 /* skip the implicit cast */
519 expression_t *expression = entry->enum_value.value;
520 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
521 expression = expression->unary.value;
523 print_expression(expression);
534 * Prints an enum type.
536 * @param type The enum type.
538 static void print_type_enum(const enum_type_t *type)
540 int empty = type->base.qualifiers == 0;
541 print_type_qualifiers(type->base.qualifiers);
542 fputs(" enum " + empty, out);
544 enum_t *enume = type->enume;
545 symbol_t *symbol = enume->base.symbol;
546 if (symbol != NULL) {
547 fputs(symbol->string, out);
549 print_enum_definition(enume);
554 * Print the compound part of a compound type.
556 void print_compound_definition(const compound_t *compound)
561 entity_t *entity = compound->members.entities;
562 for( ; entity != NULL; entity = entity->base.next) {
563 if (entity->kind != ENTITY_COMPOUND_MEMBER)
567 print_entity(entity);
574 if (compound->modifiers & DM_TRANSPARENT_UNION) {
575 fputs("__attribute__((__transparent_union__))", out);
580 * Prints a compound type.
582 * @param type The compound type.
584 static void print_compound_type(const compound_type_t *type)
586 int empty = type->base.qualifiers == 0;
587 print_type_qualifiers(type->base.qualifiers);
589 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
590 fputs(" struct " + empty, out);
592 assert(type->base.kind == TYPE_COMPOUND_UNION);
593 fputs(" union " + empty, out);
596 compound_t *compound = type->compound;
597 symbol_t *symbol = compound->base.symbol;
598 if (symbol != NULL) {
599 fputs(symbol->string, out);
601 print_compound_definition(compound);
606 * Prints the prefix part of a typedef type.
608 * @param type The typedef type.
610 static void print_typedef_type_pre(const typedef_type_t *const type)
612 print_type_qualifiers(type->base.qualifiers);
613 if (type->base.qualifiers != 0)
615 fputs(type->typedefe->base.symbol->string, out);
619 * Prints the prefix part of a typeof type.
621 * @param type The typeof type.
623 static void print_typeof_type_pre(const typeof_type_t *const type)
625 fputs("typeof(", out);
626 if (type->expression != NULL) {
627 print_expression(type->expression);
629 print_type(type->typeof_type);
635 * Prints the prefix part of a type.
637 * @param type The type.
638 * @param top true if we print the toplevel type, false else.
640 static void intern_print_type_pre(const type_t *const type, const bool top)
644 fputs("<error>", out);
647 fputs("<invalid>", out);
650 print_type_enum(&type->enumt);
653 print_atomic_type(&type->atomic);
656 print_complex_type(&type->complex);
659 print_imaginary_type(&type->imaginary);
661 case TYPE_COMPOUND_STRUCT:
662 case TYPE_COMPOUND_UNION:
663 print_compound_type(&type->compound);
666 fputs(type->builtin.symbol->string, out);
669 print_function_type_pre(&type->function, top);
672 print_pointer_type_pre(&type->pointer);
675 print_reference_type_pre(&type->reference);
678 intern_print_type_pre(type->bitfield.base_type, top);
681 print_array_type_pre(&type->array);
684 print_typedef_type_pre(&type->typedeft);
687 print_typeof_type_pre(&type->typeoft);
690 fputs("unknown", out);
694 * Prints the postfix part of a type.
696 * @param type The type.
697 * @param top true if we print the toplevel type, false else.
699 static void intern_print_type_post(const type_t *const type, const bool top)
703 print_function_type_post(&type->function, NULL, top);
706 print_pointer_type_post(&type->pointer);
709 print_reference_type_post(&type->reference);
712 print_array_type_post(&type->array);
715 print_bitfield_type_post(&type->bitfield);
723 case TYPE_COMPOUND_STRUCT:
724 case TYPE_COMPOUND_UNION:
731 if (type->base.modifiers & DM_TRANSPARENT_UNION) {
732 fputs("__attribute__((__transparent_union__))", out);
739 * @param type The type.
741 void print_type(const type_t *const type)
743 print_type_ext(type, NULL, NULL);
746 void print_type_ext(const type_t *const type, const symbol_t *symbol,
747 const scope_t *parameters)
750 fputs("nil type", out);
754 intern_print_type_pre(type, true);
755 if (symbol != NULL) {
757 fputs(symbol->string, out);
759 if (type->kind == TYPE_FUNCTION) {
760 print_function_type_post(&type->function, parameters, true);
762 intern_print_type_post(type, true);
767 * Return the size of a type AST node.
769 * @param type The type.
771 static size_t get_type_size(const type_t *type)
774 case TYPE_ATOMIC: return sizeof(atomic_type_t);
775 case TYPE_COMPLEX: return sizeof(complex_type_t);
776 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
777 case TYPE_COMPOUND_STRUCT:
778 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
779 case TYPE_ENUM: return sizeof(enum_type_t);
780 case TYPE_FUNCTION: return sizeof(function_type_t);
781 case TYPE_POINTER: return sizeof(pointer_type_t);
782 case TYPE_REFERENCE: return sizeof(reference_type_t);
783 case TYPE_ARRAY: return sizeof(array_type_t);
784 case TYPE_BUILTIN: return sizeof(builtin_type_t);
785 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
786 case TYPE_TYPEOF: return sizeof(typeof_type_t);
787 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
788 case TYPE_ERROR: panic("error type found");
789 case TYPE_INVALID: panic("invalid type found");
791 panic("unknown type found");
797 * @param type The type to copy.
798 * @return A copy of the type.
800 * @note This does not produce a deep copy!
802 type_t *duplicate_type(const type_t *type)
804 size_t size = get_type_size(type);
806 type_t *copy = obstack_alloc(type_obst, size);
807 memcpy(copy, type, size);
808 copy->base.firm_type = NULL;
814 * Returns the unqualified type of a given type.
816 * @param type The type.
817 * @returns The unqualified type.
819 type_t *get_unqualified_type(type_t *type)
821 assert(!is_typeref(type));
823 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
826 type_t *unqualified_type = duplicate_type(type);
827 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
829 return identify_new_type(unqualified_type);
832 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
834 type_t *type = skip_typeref(orig_type);
837 if (is_type_array(type)) {
838 /* For array types the element type has to be adjusted */
839 type_t *element_type = type->array.element_type;
840 type_t *qual_element_type = get_qualified_type(element_type, qual);
842 if (qual_element_type == element_type)
845 copy = duplicate_type(type);
846 copy->array.element_type = qual_element_type;
847 } else if (is_type_valid(type)) {
848 if ((type->base.qualifiers & qual) == qual)
851 copy = duplicate_type(type);
852 copy->base.qualifiers |= qual;
857 return identify_new_type(copy);
861 * Check if a type is valid.
863 * @param type The type to check.
864 * @return true if type represents a valid type.
866 bool type_valid(const type_t *type)
868 return type->kind != TYPE_INVALID;
871 static bool test_atomic_type_flag(atomic_type_kind_t kind,
872 atomic_type_flag_t flag)
874 assert(kind <= ATOMIC_TYPE_LAST);
875 return (atomic_type_properties[kind].flags & flag) != 0;
879 * Returns true if the given type is an integer type.
881 * @param type The type to check.
882 * @return True if type is an integer type.
884 bool is_type_integer(const type_t *type)
886 assert(!is_typeref(type));
888 if (type->kind == TYPE_ENUM)
890 if (type->kind == TYPE_BITFIELD)
893 if (type->kind != TYPE_ATOMIC)
896 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
900 * Returns true if the given type is an enum type.
902 * @param type The type to check.
903 * @return True if type is an enum type.
905 bool is_type_enum(const type_t *type)
907 assert(!is_typeref(type));
908 return type->kind == TYPE_ENUM;
912 * Returns true if the given type is an floating point type.
914 * @param type The type to check.
915 * @return True if type is a floating point type.
917 bool is_type_float(const type_t *type)
919 assert(!is_typeref(type));
921 if (type->kind != TYPE_ATOMIC)
924 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
928 * Returns true if the given type is an complex type.
930 * @param type The type to check.
931 * @return True if type is a complex type.
933 bool is_type_complex(const type_t *type)
935 assert(!is_typeref(type));
937 if (type->kind != TYPE_ATOMIC)
940 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
944 * Returns true if the given type is a signed type.
946 * @param type The type to check.
947 * @return True if type is a signed type.
949 bool is_type_signed(const type_t *type)
951 assert(!is_typeref(type));
953 /* enum types are int for now */
954 if (type->kind == TYPE_ENUM)
956 if (type->kind == TYPE_BITFIELD)
957 return is_type_signed(type->bitfield.base_type);
959 if (type->kind != TYPE_ATOMIC)
962 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
966 * Returns true if the given type represents an arithmetic type.
968 * @param type The type to check.
969 * @return True if type represents an arithmetic type.
971 bool is_type_arithmetic(const type_t *type)
973 assert(!is_typeref(type));
980 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
982 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
984 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
991 * Returns true if the given type is an integer or float type.
993 * @param type The type to check.
994 * @return True if type is an integer or float type.
996 bool is_type_real(const type_t *type)
999 return is_type_integer(type) || is_type_float(type);
1003 * Returns true if the given type represents a scalar type.
1005 * @param type The type to check.
1006 * @return True if type represents a scalar type.
1008 bool is_type_scalar(const type_t *type)
1010 assert(!is_typeref(type));
1012 switch (type->kind) {
1013 case TYPE_POINTER: return true;
1014 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1018 return is_type_arithmetic(type);
1022 * Check if a given type is incomplete.
1024 * @param type The type to check.
1025 * @return True if the given type is incomplete (ie. just forward).
1027 bool is_type_incomplete(const type_t *type)
1029 assert(!is_typeref(type));
1031 switch(type->kind) {
1032 case TYPE_COMPOUND_STRUCT:
1033 case TYPE_COMPOUND_UNION: {
1034 const compound_type_t *compound_type = &type->compound;
1035 return !compound_type->compound->complete;
1041 return type->array.size_expression == NULL
1042 && !type->array.size_constant;
1045 return type->atomic.akind == ATOMIC_TYPE_VOID;
1048 return type->complex.akind == ATOMIC_TYPE_VOID;
1050 case TYPE_IMAGINARY:
1051 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1056 case TYPE_REFERENCE:
1063 panic("is_type_incomplete called without typerefs skipped");
1068 panic("invalid type found");
1071 bool is_type_object(const type_t *type)
1073 return !is_type_function(type) && !is_type_incomplete(type);
1077 * Check if two function types are compatible.
1079 static bool function_types_compatible(const function_type_t *func1,
1080 const function_type_t *func2)
1082 const type_t* const ret1 = skip_typeref(func1->return_type);
1083 const type_t* const ret2 = skip_typeref(func2->return_type);
1084 if (!types_compatible(ret1, ret2))
1087 if (func1->linkage != func2->linkage)
1090 if (func1->calling_convention != func2->calling_convention)
1093 /* can parameters be compared? */
1094 if (func1->unspecified_parameters || func2->unspecified_parameters)
1097 if (func1->variadic != func2->variadic)
1100 /* TODO: handling of unspecified parameters not correct yet */
1102 /* all argument types must be compatible */
1103 function_parameter_t *parameter1 = func1->parameters;
1104 function_parameter_t *parameter2 = func2->parameters;
1105 for ( ; parameter1 != NULL && parameter2 != NULL;
1106 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1107 type_t *parameter1_type = skip_typeref(parameter1->type);
1108 type_t *parameter2_type = skip_typeref(parameter2->type);
1110 parameter1_type = get_unqualified_type(parameter1_type);
1111 parameter2_type = get_unqualified_type(parameter2_type);
1113 if (!types_compatible(parameter1_type, parameter2_type))
1116 /* same number of arguments? */
1117 if (parameter1 != NULL || parameter2 != NULL)
1124 * Check if two array types are compatible.
1126 static bool array_types_compatible(const array_type_t *array1,
1127 const array_type_t *array2)
1129 type_t *element_type1 = skip_typeref(array1->element_type);
1130 type_t *element_type2 = skip_typeref(array2->element_type);
1131 if (!types_compatible(element_type1, element_type2))
1134 if (!array1->size_constant || !array2->size_constant)
1137 return array1->size == array2->size;
1141 * Check if two types are compatible.
1143 bool types_compatible(const type_t *type1, const type_t *type2)
1145 assert(!is_typeref(type1));
1146 assert(!is_typeref(type2));
1148 /* shortcut: the same type is always compatible */
1152 if (!is_type_valid(type1) || !is_type_valid(type2))
1155 if (type1->base.qualifiers != type2->base.qualifiers)
1157 if (type1->kind != type2->kind)
1160 switch (type1->kind) {
1162 return function_types_compatible(&type1->function, &type2->function);
1164 return type1->atomic.akind == type2->atomic.akind;
1166 return type1->complex.akind == type2->complex.akind;
1167 case TYPE_IMAGINARY:
1168 return type1->imaginary.akind == type2->imaginary.akind;
1170 return array_types_compatible(&type1->array, &type2->array);
1172 case TYPE_POINTER: {
1173 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1174 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1175 return types_compatible(to1, to2);
1178 case TYPE_REFERENCE: {
1179 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1180 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1181 return types_compatible(to1, to2);
1184 case TYPE_COMPOUND_STRUCT:
1185 case TYPE_COMPOUND_UNION:
1188 /* TODO: not implemented */
1192 /* not sure if this makes sense or is even needed, implement it if you
1193 * really need it! */
1194 panic("type compatibility check for bitfield type");
1197 /* Hmm, the error type should be compatible to all other types */
1200 panic("invalid type found in compatible types");
1203 panic("typerefs not skipped in compatible types?!?");
1206 /* TODO: incomplete */
1211 * Skip all typerefs and return the underlying type.
1213 type_t *skip_typeref(type_t *type)
1215 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1216 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1217 il_alignment_t alignment = 0;
1220 if (alignment < type->base.alignment)
1221 alignment = type->base.alignment;
1223 switch (type->kind) {
1226 case TYPE_TYPEDEF: {
1227 qualifiers |= type->base.qualifiers;
1228 modifiers |= type->base.modifiers;
1230 const typedef_type_t *typedef_type = &type->typedeft;
1231 if (typedef_type->resolved_type != NULL) {
1232 type = typedef_type->resolved_type;
1235 type = typedef_type->typedefe->type;
1239 qualifiers |= type->base.qualifiers;
1240 modifiers |= type->base.modifiers;
1241 type = type->typeoft.typeof_type;
1249 if (qualifiers != TYPE_QUALIFIER_NONE ||
1250 modifiers != TYPE_MODIFIER_NONE ||
1251 alignment > type->base.alignment) {
1252 type_t *const copy = duplicate_type(type);
1254 /* for const with typedefed array type the element type has to be
1256 if (is_type_array(copy)) {
1257 type_t *element_type = copy->array.element_type;
1258 element_type = duplicate_type(element_type);
1259 element_type->base.qualifiers |= qualifiers;
1260 element_type->base.modifiers |= modifiers;
1261 element_type->base.alignment = alignment;
1262 copy->array.element_type = element_type;
1264 copy->base.qualifiers |= qualifiers;
1265 copy->base.modifiers |= modifiers;
1266 copy->base.alignment = alignment;
1269 type = identify_new_type(copy);
1275 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1277 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1280 switch (type->base.kind) {
1282 return TYPE_QUALIFIER_NONE;
1284 qualifiers |= type->base.qualifiers;
1285 const typedef_type_t *typedef_type = &type->typedeft;
1286 if (typedef_type->resolved_type != NULL)
1287 type = typedef_type->resolved_type;
1289 type = typedef_type->typedefe->type;
1292 type = type->typeoft.typeof_type;
1295 if (skip_array_type) {
1296 type = type->array.element_type;
1305 return type->base.qualifiers | qualifiers;
1308 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1310 assert(kind <= ATOMIC_TYPE_LAST);
1311 return atomic_type_properties[kind].size;
1314 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1316 assert(kind <= ATOMIC_TYPE_LAST);
1317 return atomic_type_properties[kind].alignment;
1320 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1322 assert(kind <= ATOMIC_TYPE_LAST);
1323 return atomic_type_properties[kind].flags;
1326 atomic_type_kind_t get_intptr_kind(void)
1328 if (machine_size <= 32)
1329 return ATOMIC_TYPE_INT;
1330 else if (machine_size <= 64)
1331 return ATOMIC_TYPE_LONG;
1333 return ATOMIC_TYPE_LONGLONG;
1336 atomic_type_kind_t get_uintptr_kind(void)
1338 if (machine_size <= 32)
1339 return ATOMIC_TYPE_UINT;
1340 else if (machine_size <= 64)
1341 return ATOMIC_TYPE_ULONG;
1343 return ATOMIC_TYPE_ULONGLONG;
1347 * Find the atomic type kind representing a given size (signed).
1349 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1351 static atomic_type_kind_t kinds[32];
1354 atomic_type_kind_t kind = kinds[size];
1355 if (kind == ATOMIC_TYPE_INVALID) {
1356 static const atomic_type_kind_t possible_kinds[] = {
1361 ATOMIC_TYPE_LONGLONG
1363 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1364 if (get_atomic_type_size(possible_kinds[i]) == size) {
1365 kind = possible_kinds[i];
1375 * Find the atomic type kind representing a given size (signed).
1377 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1379 static atomic_type_kind_t kinds[32];
1382 atomic_type_kind_t kind = kinds[size];
1383 if (kind == ATOMIC_TYPE_INVALID) {
1384 static const atomic_type_kind_t possible_kinds[] = {
1389 ATOMIC_TYPE_ULONGLONG
1391 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1392 if (get_atomic_type_size(possible_kinds[i]) == size) {
1393 kind = possible_kinds[i];
1403 * Hash the given type and return the "singleton" version
1406 type_t *identify_new_type(type_t *type)
1408 type_t *result = typehash_insert(type);
1409 if (result != type) {
1410 obstack_free(type_obst, type);
1416 * Creates a new atomic type.
1418 * @param akind The kind of the atomic type.
1419 * @param qualifiers Type qualifiers for the new type.
1421 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1423 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1424 memset(type, 0, sizeof(atomic_type_t));
1426 type->kind = TYPE_ATOMIC;
1427 type->base.size = get_atomic_type_size(akind);
1428 type->base.alignment = get_atomic_type_alignment(akind);
1429 type->base.qualifiers = qualifiers;
1430 type->atomic.akind = akind;
1432 return identify_new_type(type);
1436 * Creates a new complex type.
1438 * @param akind The kind of the atomic type.
1439 * @param qualifiers Type qualifiers for the new type.
1441 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1443 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1444 memset(type, 0, sizeof(complex_type_t));
1446 type->kind = TYPE_COMPLEX;
1447 type->base.qualifiers = qualifiers;
1448 type->base.alignment = get_atomic_type_alignment(akind);
1449 type->complex.akind = akind;
1451 return identify_new_type(type);
1455 * Creates a new imaginary type.
1457 * @param akind The kind of the atomic type.
1458 * @param qualifiers Type qualifiers for the new type.
1460 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1462 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1463 memset(type, 0, sizeof(imaginary_type_t));
1465 type->kind = TYPE_IMAGINARY;
1466 type->base.qualifiers = qualifiers;
1467 type->base.alignment = get_atomic_type_alignment(akind);
1468 type->imaginary.akind = akind;
1470 return identify_new_type(type);
1474 * Creates a new pointer type.
1476 * @param points_to The points-to type for the new type.
1477 * @param qualifiers Type qualifiers for the new type.
1479 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1481 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1482 memset(type, 0, sizeof(pointer_type_t));
1484 type->kind = TYPE_POINTER;
1485 type->base.qualifiers = qualifiers;
1486 type->base.alignment = 0;
1487 type->pointer.points_to = points_to;
1488 type->pointer.base_variable = NULL;
1490 return identify_new_type(type);
1494 * Creates a new reference type.
1496 * @param refers_to The referred-to type for the new type.
1498 type_t *make_reference_type(type_t *refers_to)
1500 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1501 memset(type, 0, sizeof(reference_type_t));
1503 type->kind = TYPE_REFERENCE;
1504 type->base.qualifiers = 0;
1505 type->base.alignment = 0;
1506 type->reference.refers_to = refers_to;
1508 return identify_new_type(type);
1512 * Creates a new based pointer type.
1514 * @param points_to The points-to type for the new type.
1515 * @param qualifiers Type qualifiers for the new type.
1516 * @param variable The based variable
1518 type_t *make_based_pointer_type(type_t *points_to,
1519 type_qualifiers_t qualifiers, variable_t *variable)
1521 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1522 memset(type, 0, sizeof(pointer_type_t));
1524 type->kind = TYPE_POINTER;
1525 type->base.qualifiers = qualifiers;
1526 type->base.alignment = 0;
1527 type->pointer.points_to = points_to;
1528 type->pointer.base_variable = variable;
1530 return identify_new_type(type);
1534 type_t *make_array_type(type_t *element_type, size_t size,
1535 type_qualifiers_t qualifiers)
1537 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1538 memset(type, 0, sizeof(array_type_t));
1540 type->kind = TYPE_ARRAY;
1541 type->base.qualifiers = qualifiers;
1542 type->base.alignment = 0;
1543 type->array.element_type = element_type;
1544 type->array.size = size;
1545 type->array.size_constant = true;
1547 return identify_new_type(type);
1551 * Debug helper. Prints the given type to stdout.
1553 static __attribute__((unused))
1554 void dbg_type(const type_t *type)
1556 FILE *old_out = out;