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
29 #include "type_hash.h"
30 #include "adt/error.h"
32 #include "lang_features.h"
34 static struct obstack _type_obst;
36 struct obstack *type_obst = &_type_obst;
37 static int type_visited = 0;
38 static bool print_implicit_array_size = false;
40 static void intern_print_type_pre(const type_t *type);
41 static void intern_print_type_post(const type_t *type);
43 typedef struct atomic_type_properties_t atomic_type_properties_t;
44 struct atomic_type_properties_t {
45 unsigned size; /**< type size in bytes */
46 unsigned alignment; /**< type alignment in bytes */
47 unsigned flags; /**< type flags from atomic_type_flag_t */
51 * Properties of atomic types.
53 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
54 //ATOMIC_TYPE_INVALID = 0,
55 [ATOMIC_TYPE_VOID] = {
58 .flags = ATOMIC_TYPE_FLAG_NONE
60 [ATOMIC_TYPE_WCHAR_T] = {
62 .alignment = (unsigned)-1,
63 /* signed flag will be set when known */
64 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
66 [ATOMIC_TYPE_CHAR] = {
69 /* signed flag will be set when known */
70 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
72 [ATOMIC_TYPE_SCHAR] = {
75 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
76 | ATOMIC_TYPE_FLAG_SIGNED,
78 [ATOMIC_TYPE_UCHAR] = {
81 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
83 [ATOMIC_TYPE_SHORT] = {
86 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
87 | ATOMIC_TYPE_FLAG_SIGNED
89 [ATOMIC_TYPE_USHORT] = {
92 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
95 .size = (unsigned) -1,
96 .alignment = (unsigned) -1,
97 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
98 | ATOMIC_TYPE_FLAG_SIGNED,
100 [ATOMIC_TYPE_UINT] = {
101 .size = (unsigned) -1,
102 .alignment = (unsigned) -1,
103 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
105 [ATOMIC_TYPE_LONG] = {
106 .size = (unsigned) -1,
107 .alignment = (unsigned) -1,
108 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
109 | ATOMIC_TYPE_FLAG_SIGNED,
111 [ATOMIC_TYPE_ULONG] = {
112 .size = (unsigned) -1,
113 .alignment = (unsigned) -1,
114 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
116 [ATOMIC_TYPE_LONGLONG] = {
117 .size = (unsigned) -1,
118 .alignment = (unsigned) -1,
119 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
120 | ATOMIC_TYPE_FLAG_SIGNED,
122 [ATOMIC_TYPE_ULONGLONG] = {
123 .size = (unsigned) -1,
124 .alignment = (unsigned) -1,
125 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
127 [ATOMIC_TYPE_BOOL] = {
128 .size = (unsigned) -1,
129 .alignment = (unsigned) -1,
130 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
132 [ATOMIC_TYPE_FLOAT] = {
134 .alignment = (unsigned) -1,
135 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
136 | ATOMIC_TYPE_FLAG_SIGNED,
138 [ATOMIC_TYPE_DOUBLE] = {
140 .alignment = (unsigned) -1,
141 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
142 | ATOMIC_TYPE_FLAG_SIGNED,
144 [ATOMIC_TYPE_LONG_DOUBLE] = {
146 .alignment = (unsigned) -1,
147 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
148 | ATOMIC_TYPE_FLAG_SIGNED,
150 /* complex and imaginary types are set in init_types */
153 void init_types(void)
155 obstack_init(type_obst);
157 atomic_type_properties_t *props = atomic_type_properties;
159 if (char_is_signed) {
160 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
163 unsigned int_size = machine_size < 32 ? 2 : 4;
164 unsigned long_size = machine_size < 64 ? 4 : 8;
165 unsigned llong_size = machine_size < 32 ? 4 : 8;
167 props[ATOMIC_TYPE_INT].size = int_size;
168 props[ATOMIC_TYPE_INT].alignment = int_size;
169 props[ATOMIC_TYPE_UINT].size = int_size;
170 props[ATOMIC_TYPE_UINT].alignment = int_size;
171 props[ATOMIC_TYPE_LONG].size = long_size;
172 props[ATOMIC_TYPE_LONG].alignment = long_size;
173 props[ATOMIC_TYPE_ULONG].size = long_size;
174 props[ATOMIC_TYPE_ULONG].alignment = long_size;
175 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
176 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
177 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
178 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
180 /* TODO: backend specific, need a way to query the backend for this.
181 * The following are good settings for x86 */
182 props[ATOMIC_TYPE_FLOAT].alignment = 4;
183 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
184 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
185 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
186 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
188 /* TODO: make this configurable for platforms which do not use byte sized
190 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
192 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
195 void exit_types(void)
197 obstack_free(type_obst, NULL);
200 void type_set_output(FILE *stream)
205 void inc_type_visited(void)
210 void print_type_qualifiers(type_qualifiers_t qualifiers)
213 if (qualifiers & TYPE_QUALIFIER_CONST) {
214 fputs(" const" + first, out);
217 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
218 fputs(" volatile" + first, out);
221 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
222 fputs(" restrict" + first, out);
227 const char *get_atomic_kind_name(atomic_type_kind_t kind)
230 case ATOMIC_TYPE_INVALID: break;
231 case ATOMIC_TYPE_VOID: return "void";
232 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
233 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
234 case ATOMIC_TYPE_CHAR: return "char";
235 case ATOMIC_TYPE_SCHAR: return "signed char";
236 case ATOMIC_TYPE_UCHAR: return "unsigned char";
237 case ATOMIC_TYPE_INT: return "int";
238 case ATOMIC_TYPE_UINT: return "unsigned int";
239 case ATOMIC_TYPE_SHORT: return "short";
240 case ATOMIC_TYPE_USHORT: return "unsigned short";
241 case ATOMIC_TYPE_LONG: return "long";
242 case ATOMIC_TYPE_ULONG: return "unsigned long";
243 case ATOMIC_TYPE_LONGLONG: return "long long";
244 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
245 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
246 case ATOMIC_TYPE_FLOAT: return "float";
247 case ATOMIC_TYPE_DOUBLE: return "double";
249 return "INVALIDATOMIC";
253 * Prints the name of an atomic type kinds.
255 * @param kind The type kind.
257 static void print_atomic_kinds(atomic_type_kind_t kind)
259 const char *s = get_atomic_kind_name(kind);
264 * Prints the name of an atomic type.
266 * @param type The type.
268 static void print_atomic_type(const atomic_type_t *type)
270 print_type_qualifiers(type->base.qualifiers);
271 if (type->base.qualifiers != 0)
273 print_atomic_kinds(type->akind);
277 * Prints the name of a complex type.
279 * @param type The type.
282 void print_complex_type(const complex_type_t *type)
284 int empty = type->base.qualifiers == 0;
285 print_type_qualifiers(type->base.qualifiers);
286 fputs(" _Complex " + empty, out);
287 print_atomic_kinds(type->akind);
291 * Prints the name of an imaginary type.
293 * @param type The type.
296 void print_imaginary_type(const imaginary_type_t *type)
298 int empty = type->base.qualifiers == 0;
299 print_type_qualifiers(type->base.qualifiers);
300 fputs(" _Imaginary " + empty, out);
301 print_atomic_kinds(type->akind);
305 * Print the first part (the prefix) of a type.
307 * @param type The type to print.
309 static void print_function_type_pre(const function_type_t *type)
311 switch (type->linkage) {
312 case LINKAGE_INVALID:
317 fputs("extern \"C\" ", out);
321 if (!(c_mode & _CXX))
322 fputs("extern \"C++\" ", out);
326 print_type_qualifiers(type->base.qualifiers);
327 if (type->base.qualifiers != 0)
330 intern_print_type_pre(type->return_type);
332 switch (type->calling_convention) {
333 case CC_CDECL: fputs("__cdecl ", out); break;
334 case CC_STDCALL: fputs("__stdcall ", out); break;
335 case CC_FASTCALL: fputs("__fastcall ", out); break;
336 case CC_THISCALL: fputs("__thiscall ", out); break;
337 case CC_DEFAULT: break;
342 * Print the second part (the postfix) of a type.
344 * @param type The type to print.
346 static void print_function_type_post(const function_type_t *type,
347 const scope_t *parameters)
351 if (parameters == NULL) {
352 function_parameter_t *parameter = type->parameters;
353 for( ; parameter != NULL; parameter = parameter->next) {
359 print_type(parameter->type);
362 entity_t *parameter = parameters->entities;
363 for (; parameter != NULL; parameter = parameter->base.next) {
364 if (parameter->kind != ENTITY_PARAMETER)
372 const type_t *const type = parameter->declaration.type;
374 fputs(parameter->base.symbol->string, out);
376 print_type_ext(type, parameter->base.symbol, NULL);
380 if (type->variadic) {
388 if (first && !type->unspecified_parameters) {
393 intern_print_type_post(type->return_type);
397 * Prints the prefix part of a pointer type.
399 * @param type The pointer type.
401 static void print_pointer_type_pre(const pointer_type_t *type)
403 type_t const *const points_to = type->points_to;
404 intern_print_type_pre(points_to);
405 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
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 postfix part of a pointer type.
423 * @param type The pointer type.
425 static void print_pointer_type_post(const pointer_type_t *type)
427 type_t const *const points_to = type->points_to;
428 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
430 intern_print_type_post(points_to);
434 * Prints the prefix part of a reference type.
436 * @param type The reference type.
438 static void print_reference_type_pre(const reference_type_t *type)
440 type_t const *const refers_to = type->refers_to;
441 intern_print_type_pre(refers_to);
442 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
448 * Prints the postfix part of a reference type.
450 * @param type The reference type.
452 static void print_reference_type_post(const reference_type_t *type)
454 type_t const *const refers_to = type->refers_to;
455 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
457 intern_print_type_post(refers_to);
461 * Prints the prefix part of an array type.
463 * @param type The array type.
465 static void print_array_type_pre(const array_type_t *type)
467 intern_print_type_pre(type->element_type);
471 * Prints the postfix part of an array type.
473 * @param type The array type.
475 static void print_array_type_post(const array_type_t *type)
478 if (type->is_static) {
479 fputs("static ", out);
481 print_type_qualifiers(type->base.qualifiers);
482 if (type->base.qualifiers != 0)
484 if (type->size_expression != NULL
485 && (print_implicit_array_size || !type->has_implicit_size)) {
486 print_expression(type->size_expression);
489 intern_print_type_post(type->element_type);
493 * Prints the postfix part of a bitfield type.
495 * @param type The array type.
497 static void print_bitfield_type_post(const bitfield_type_t *type)
500 print_expression(type->size_expression);
501 intern_print_type_post(type->base_type);
505 * Prints an enum definition.
507 * @param declaration The enum's type declaration.
509 void print_enum_definition(const enum_t *enume)
515 entity_t *entry = enume->base.next;
516 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
517 entry = entry->base.next) {
520 fputs(entry->base.symbol->string, out);
521 if (entry->enum_value.value != NULL) {
524 /* skip the implicit cast */
525 expression_t *expression = entry->enum_value.value;
526 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
527 expression = expression->unary.value;
529 print_expression(expression);
540 * Prints an enum type.
542 * @param type The enum type.
544 static void print_type_enum(const enum_type_t *type)
546 int empty = type->base.qualifiers == 0;
547 print_type_qualifiers(type->base.qualifiers);
548 fputs(" enum " + empty, out);
550 enum_t *enume = type->enume;
551 symbol_t *symbol = enume->base.symbol;
552 if (symbol != NULL) {
553 fputs(symbol->string, out);
555 print_enum_definition(enume);
560 * Print the compound part of a compound type.
562 void print_compound_definition(const compound_t *compound)
567 entity_t *entity = compound->members.entities;
568 for( ; entity != NULL; entity = entity->base.next) {
569 if (entity->kind != ENTITY_COMPOUND_MEMBER)
573 print_entity(entity);
580 if (compound->modifiers & DM_TRANSPARENT_UNION) {
581 fputs("__attribute__((__transparent_union__))", out);
586 * Prints a compound type.
588 * @param type The compound type.
590 static void print_compound_type(const compound_type_t *type)
592 int empty = type->base.qualifiers == 0;
593 print_type_qualifiers(type->base.qualifiers);
595 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
596 fputs(" struct " + empty, out);
598 assert(type->base.kind == TYPE_COMPOUND_UNION);
599 fputs(" union " + empty, out);
602 compound_t *compound = type->compound;
603 symbol_t *symbol = compound->base.symbol;
604 if (symbol != NULL) {
605 fputs(symbol->string, out);
607 print_compound_definition(compound);
612 * Prints the prefix part of a typedef type.
614 * @param type The typedef type.
616 static void print_typedef_type_pre(const typedef_type_t *const type)
618 print_type_qualifiers(type->base.qualifiers);
619 if (type->base.qualifiers != 0)
621 fputs(type->typedefe->base.symbol->string, out);
625 * Prints the prefix part of a typeof type.
627 * @param type The typeof type.
629 static void print_typeof_type_pre(const typeof_type_t *const type)
631 fputs("typeof(", out);
632 if (type->expression != NULL) {
633 print_expression(type->expression);
635 print_type(type->typeof_type);
641 * Prints the prefix part of a type.
643 * @param type The type.
645 static void intern_print_type_pre(const type_t *const type)
649 fputs("<error>", out);
652 fputs("<invalid>", out);
655 print_type_enum(&type->enumt);
658 print_atomic_type(&type->atomic);
661 print_complex_type(&type->complex);
664 print_imaginary_type(&type->imaginary);
666 case TYPE_COMPOUND_STRUCT:
667 case TYPE_COMPOUND_UNION:
668 print_compound_type(&type->compound);
671 fputs(type->builtin.symbol->string, out);
674 print_function_type_pre(&type->function);
677 print_pointer_type_pre(&type->pointer);
680 print_reference_type_pre(&type->reference);
683 intern_print_type_pre(type->bitfield.base_type);
686 print_array_type_pre(&type->array);
689 print_typedef_type_pre(&type->typedeft);
692 print_typeof_type_pre(&type->typeoft);
695 fputs("unknown", out);
699 * Prints the postfix part of a type.
701 * @param type The type.
703 static void intern_print_type_post(const type_t *const type)
707 print_function_type_post(&type->function, NULL);
710 print_pointer_type_post(&type->pointer);
713 print_reference_type_post(&type->reference);
716 print_array_type_post(&type->array);
719 print_bitfield_type_post(&type->bitfield);
727 case TYPE_COMPOUND_STRUCT:
728 case TYPE_COMPOUND_UNION:
735 if (type->base.modifiers & DM_TRANSPARENT_UNION) {
736 fputs("__attribute__((__transparent_union__))", out);
743 * @param type The type.
745 void print_type(const type_t *const type)
747 print_type_ext(type, NULL, NULL);
750 void print_type_ext(const type_t *const type, const symbol_t *symbol,
751 const scope_t *parameters)
754 fputs("nil type", out);
758 intern_print_type_pre(type);
759 if (symbol != NULL) {
761 fputs(symbol->string, out);
763 if (type->kind == TYPE_FUNCTION) {
764 print_function_type_post(&type->function, parameters);
766 intern_print_type_post(type);
771 * Return the size of a type AST node.
773 * @param type The type.
775 static size_t get_type_size(const type_t *type)
778 case TYPE_ATOMIC: return sizeof(atomic_type_t);
779 case TYPE_COMPLEX: return sizeof(complex_type_t);
780 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
781 case TYPE_COMPOUND_STRUCT:
782 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
783 case TYPE_ENUM: return sizeof(enum_type_t);
784 case TYPE_FUNCTION: return sizeof(function_type_t);
785 case TYPE_POINTER: return sizeof(pointer_type_t);
786 case TYPE_REFERENCE: return sizeof(reference_type_t);
787 case TYPE_ARRAY: return sizeof(array_type_t);
788 case TYPE_BUILTIN: return sizeof(builtin_type_t);
789 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
790 case TYPE_TYPEOF: return sizeof(typeof_type_t);
791 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
792 case TYPE_ERROR: panic("error type found");
793 case TYPE_INVALID: panic("invalid type found");
795 panic("unknown type found");
801 * @param type The type to copy.
802 * @return A copy of the type.
804 * @note This does not produce a deep copy!
806 type_t *duplicate_type(const type_t *type)
808 size_t size = get_type_size(type);
810 type_t *copy = obstack_alloc(type_obst, size);
811 memcpy(copy, type, size);
812 copy->base.firm_type = NULL;
818 * Returns the unqualified type of a given type.
820 * @param type The type.
821 * @returns The unqualified type.
823 type_t *get_unqualified_type(type_t *type)
825 assert(!is_typeref(type));
827 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
830 type_t *unqualified_type = duplicate_type(type);
831 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
833 return identify_new_type(unqualified_type);
836 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
838 type_t *type = skip_typeref(orig_type);
841 if (is_type_array(type)) {
842 /* For array types the element type has to be adjusted */
843 type_t *element_type = type->array.element_type;
844 type_t *qual_element_type = get_qualified_type(element_type, qual);
846 if (qual_element_type == element_type)
849 copy = duplicate_type(type);
850 copy->array.element_type = qual_element_type;
851 } else if (is_type_valid(type)) {
852 if ((type->base.qualifiers & qual) == qual)
855 copy = duplicate_type(type);
856 copy->base.qualifiers |= qual;
861 return identify_new_type(copy);
865 * Check if a type is valid.
867 * @param type The type to check.
868 * @return true if type represents a valid type.
870 bool type_valid(const type_t *type)
872 return type->kind != TYPE_INVALID;
875 static bool test_atomic_type_flag(atomic_type_kind_t kind,
876 atomic_type_flag_t flag)
878 assert(kind <= ATOMIC_TYPE_LAST);
879 return (atomic_type_properties[kind].flags & flag) != 0;
883 * Returns true if the given type is an integer type.
885 * @param type The type to check.
886 * @return True if type is an integer type.
888 bool is_type_integer(const type_t *type)
890 assert(!is_typeref(type));
892 if (type->kind == TYPE_ENUM)
894 if (type->kind == TYPE_BITFIELD)
897 if (type->kind != TYPE_ATOMIC)
900 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
904 * Returns true if the given type is an enum type.
906 * @param type The type to check.
907 * @return True if type is an enum type.
909 bool is_type_enum(const type_t *type)
911 assert(!is_typeref(type));
912 return type->kind == TYPE_ENUM;
916 * Returns true if the given type is an floating point type.
918 * @param type The type to check.
919 * @return True if type is a floating point type.
921 bool is_type_float(const type_t *type)
923 assert(!is_typeref(type));
925 if (type->kind != TYPE_ATOMIC)
928 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
932 * Returns true if the given type is an complex type.
934 * @param type The type to check.
935 * @return True if type is a complex type.
937 bool is_type_complex(const type_t *type)
939 assert(!is_typeref(type));
941 if (type->kind != TYPE_ATOMIC)
944 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
948 * Returns true if the given type is a signed type.
950 * @param type The type to check.
951 * @return True if type is a signed type.
953 bool is_type_signed(const type_t *type)
955 assert(!is_typeref(type));
957 /* enum types are int for now */
958 if (type->kind == TYPE_ENUM)
960 if (type->kind == TYPE_BITFIELD)
961 return is_type_signed(type->bitfield.base_type);
963 if (type->kind != TYPE_ATOMIC)
966 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
970 * Returns true if the given type represents an arithmetic type.
972 * @param type The type to check.
973 * @return True if type represents an arithmetic type.
975 bool is_type_arithmetic(const type_t *type)
977 assert(!is_typeref(type));
984 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
986 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
988 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
995 * Returns true if the given type is an integer or float type.
997 * @param type The type to check.
998 * @return True if type is an integer or float type.
1000 bool is_type_real(const type_t *type)
1003 return is_type_integer(type) || is_type_float(type);
1007 * Returns true if the given type represents a scalar type.
1009 * @param type The type to check.
1010 * @return True if type represents a scalar type.
1012 bool is_type_scalar(const type_t *type)
1014 assert(!is_typeref(type));
1016 switch (type->kind) {
1017 case TYPE_POINTER: return true;
1018 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1022 return is_type_arithmetic(type);
1026 * Check if a given type is incomplete.
1028 * @param type The type to check.
1029 * @return True if the given type is incomplete (ie. just forward).
1031 bool is_type_incomplete(const type_t *type)
1033 assert(!is_typeref(type));
1035 switch(type->kind) {
1036 case TYPE_COMPOUND_STRUCT:
1037 case TYPE_COMPOUND_UNION: {
1038 const compound_type_t *compound_type = &type->compound;
1039 return !compound_type->compound->complete;
1045 return type->array.size_expression == NULL
1046 && !type->array.size_constant;
1049 return type->atomic.akind == ATOMIC_TYPE_VOID;
1052 return type->complex.akind == ATOMIC_TYPE_VOID;
1054 case TYPE_IMAGINARY:
1055 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1060 case TYPE_REFERENCE:
1067 panic("is_type_incomplete called without typerefs skipped");
1072 panic("invalid type found");
1075 bool is_type_object(const type_t *type)
1077 return !is_type_function(type) && !is_type_incomplete(type);
1080 bool is_builtin_va_list(type_t *type)
1082 type_t *tp = skip_typeref(type);
1084 return tp->kind == type_valist->kind &&
1085 tp->builtin.symbol == type_valist->builtin.symbol;
1089 * Check if two function types are compatible.
1091 static bool function_types_compatible(const function_type_t *func1,
1092 const function_type_t *func2)
1094 const type_t* const ret1 = skip_typeref(func1->return_type);
1095 const type_t* const ret2 = skip_typeref(func2->return_type);
1096 if (!types_compatible(ret1, ret2))
1099 if (func1->linkage != func2->linkage)
1102 if (func1->calling_convention != func2->calling_convention)
1105 /* can parameters be compared? */
1106 if (func1->unspecified_parameters || func2->unspecified_parameters)
1109 if (func1->variadic != func2->variadic)
1112 /* TODO: handling of unspecified parameters not correct yet */
1114 /* all argument types must be compatible */
1115 function_parameter_t *parameter1 = func1->parameters;
1116 function_parameter_t *parameter2 = func2->parameters;
1117 for ( ; parameter1 != NULL && parameter2 != NULL;
1118 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1119 type_t *parameter1_type = skip_typeref(parameter1->type);
1120 type_t *parameter2_type = skip_typeref(parameter2->type);
1122 parameter1_type = get_unqualified_type(parameter1_type);
1123 parameter2_type = get_unqualified_type(parameter2_type);
1125 if (!types_compatible(parameter1_type, parameter2_type))
1128 /* same number of arguments? */
1129 if (parameter1 != NULL || parameter2 != NULL)
1136 * Check if two array types are compatible.
1138 static bool array_types_compatible(const array_type_t *array1,
1139 const array_type_t *array2)
1141 type_t *element_type1 = skip_typeref(array1->element_type);
1142 type_t *element_type2 = skip_typeref(array2->element_type);
1143 if (!types_compatible(element_type1, element_type2))
1146 if (!array1->size_constant || !array2->size_constant)
1149 return array1->size == array2->size;
1153 * Check if two types are compatible.
1155 bool types_compatible(const type_t *type1, const type_t *type2)
1157 assert(!is_typeref(type1));
1158 assert(!is_typeref(type2));
1160 /* shortcut: the same type is always compatible */
1164 if (!is_type_valid(type1) || !is_type_valid(type2))
1167 if (type1->base.qualifiers != type2->base.qualifiers)
1169 if (type1->kind != type2->kind)
1172 switch (type1->kind) {
1174 return function_types_compatible(&type1->function, &type2->function);
1176 return type1->atomic.akind == type2->atomic.akind;
1178 return type1->complex.akind == type2->complex.akind;
1179 case TYPE_IMAGINARY:
1180 return type1->imaginary.akind == type2->imaginary.akind;
1182 return array_types_compatible(&type1->array, &type2->array);
1184 case TYPE_POINTER: {
1185 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1186 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1187 return types_compatible(to1, to2);
1190 case TYPE_REFERENCE: {
1191 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1192 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1193 return types_compatible(to1, to2);
1196 case TYPE_COMPOUND_STRUCT:
1197 case TYPE_COMPOUND_UNION:
1200 /* TODO: not implemented */
1204 /* not sure if this makes sense or is even needed, implement it if you
1205 * really need it! */
1206 panic("type compatibility check for bitfield type");
1209 /* Hmm, the error type should be compatible to all other types */
1212 panic("invalid type found in compatible types");
1215 panic("typerefs not skipped in compatible types?!?");
1218 /* TODO: incomplete */
1223 * Skip all typerefs and return the underlying type.
1225 type_t *skip_typeref(type_t *type)
1227 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1228 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1229 il_alignment_t alignment = 0;
1232 if (alignment < type->base.alignment)
1233 alignment = type->base.alignment;
1235 switch (type->kind) {
1238 case TYPE_TYPEDEF: {
1239 qualifiers |= type->base.qualifiers;
1240 modifiers |= type->base.modifiers;
1242 const typedef_type_t *typedef_type = &type->typedeft;
1243 if (typedef_type->resolved_type != NULL) {
1244 type = typedef_type->resolved_type;
1247 type = typedef_type->typedefe->type;
1251 qualifiers |= type->base.qualifiers;
1252 modifiers |= type->base.modifiers;
1253 type = type->typeoft.typeof_type;
1261 if (qualifiers != TYPE_QUALIFIER_NONE ||
1262 modifiers != TYPE_MODIFIER_NONE ||
1263 alignment > type->base.alignment) {
1264 type_t *const copy = duplicate_type(type);
1266 /* for const with typedefed array type the element type has to be
1268 if (is_type_array(copy)) {
1269 type_t *element_type = copy->array.element_type;
1270 element_type = duplicate_type(element_type);
1271 element_type->base.qualifiers |= qualifiers;
1272 element_type->base.modifiers |= modifiers;
1273 element_type->base.alignment = alignment;
1274 copy->array.element_type = element_type;
1276 copy->base.qualifiers |= qualifiers;
1277 copy->base.modifiers |= modifiers;
1278 copy->base.alignment = alignment;
1281 type = identify_new_type(copy);
1287 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1289 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1292 switch (type->base.kind) {
1294 return TYPE_QUALIFIER_NONE;
1296 qualifiers |= type->base.qualifiers;
1297 const typedef_type_t *typedef_type = &type->typedeft;
1298 if (typedef_type->resolved_type != NULL)
1299 type = typedef_type->resolved_type;
1301 type = typedef_type->typedefe->type;
1304 type = type->typeoft.typeof_type;
1307 if (skip_array_type) {
1308 type = type->array.element_type;
1317 return type->base.qualifiers | qualifiers;
1320 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1322 assert(kind <= ATOMIC_TYPE_LAST);
1323 return atomic_type_properties[kind].size;
1326 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1328 assert(kind <= ATOMIC_TYPE_LAST);
1329 return atomic_type_properties[kind].alignment;
1332 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1334 assert(kind <= ATOMIC_TYPE_LAST);
1335 return atomic_type_properties[kind].flags;
1338 atomic_type_kind_t get_intptr_kind(void)
1340 if (machine_size <= 32)
1341 return ATOMIC_TYPE_INT;
1342 else if (machine_size <= 64)
1343 return ATOMIC_TYPE_LONG;
1345 return ATOMIC_TYPE_LONGLONG;
1348 atomic_type_kind_t get_uintptr_kind(void)
1350 if (machine_size <= 32)
1351 return ATOMIC_TYPE_UINT;
1352 else if (machine_size <= 64)
1353 return ATOMIC_TYPE_ULONG;
1355 return ATOMIC_TYPE_ULONGLONG;
1359 * Find the atomic type kind representing a given size (signed).
1361 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1363 static atomic_type_kind_t kinds[32];
1366 atomic_type_kind_t kind = kinds[size];
1367 if (kind == ATOMIC_TYPE_INVALID) {
1368 static const atomic_type_kind_t possible_kinds[] = {
1373 ATOMIC_TYPE_LONGLONG
1375 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1376 if (get_atomic_type_size(possible_kinds[i]) == size) {
1377 kind = possible_kinds[i];
1387 * Find the atomic type kind representing a given size (signed).
1389 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1391 static atomic_type_kind_t kinds[32];
1394 atomic_type_kind_t kind = kinds[size];
1395 if (kind == ATOMIC_TYPE_INVALID) {
1396 static const atomic_type_kind_t possible_kinds[] = {
1401 ATOMIC_TYPE_ULONGLONG
1403 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1404 if (get_atomic_type_size(possible_kinds[i]) == size) {
1405 kind = possible_kinds[i];
1415 * Hash the given type and return the "singleton" version
1418 type_t *identify_new_type(type_t *type)
1420 type_t *result = typehash_insert(type);
1421 if (result != type) {
1422 obstack_free(type_obst, type);
1428 * Creates a new atomic type.
1430 * @param akind The kind of the atomic type.
1431 * @param qualifiers Type qualifiers for the new type.
1433 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1435 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1436 memset(type, 0, sizeof(atomic_type_t));
1438 type->kind = TYPE_ATOMIC;
1439 type->base.size = get_atomic_type_size(akind);
1440 type->base.alignment = get_atomic_type_alignment(akind);
1441 type->base.qualifiers = qualifiers;
1442 type->atomic.akind = akind;
1444 return identify_new_type(type);
1448 * Creates a new complex type.
1450 * @param akind The kind of the atomic type.
1451 * @param qualifiers Type qualifiers for the new type.
1453 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1455 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1456 memset(type, 0, sizeof(complex_type_t));
1458 type->kind = TYPE_COMPLEX;
1459 type->base.qualifiers = qualifiers;
1460 type->base.alignment = get_atomic_type_alignment(akind);
1461 type->complex.akind = akind;
1463 return identify_new_type(type);
1467 * Creates a new imaginary type.
1469 * @param akind The kind of the atomic type.
1470 * @param qualifiers Type qualifiers for the new type.
1472 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1474 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1475 memset(type, 0, sizeof(imaginary_type_t));
1477 type->kind = TYPE_IMAGINARY;
1478 type->base.qualifiers = qualifiers;
1479 type->base.alignment = get_atomic_type_alignment(akind);
1480 type->imaginary.akind = akind;
1482 return identify_new_type(type);
1486 * Creates a new pointer type.
1488 * @param points_to The points-to type for the new type.
1489 * @param qualifiers Type qualifiers for the new type.
1491 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1493 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1494 memset(type, 0, sizeof(pointer_type_t));
1496 type->kind = TYPE_POINTER;
1497 type->base.qualifiers = qualifiers;
1498 type->base.alignment = 0;
1499 type->pointer.points_to = points_to;
1500 type->pointer.base_variable = NULL;
1502 return identify_new_type(type);
1506 * Creates a new reference type.
1508 * @param refers_to The referred-to type for the new type.
1510 type_t *make_reference_type(type_t *refers_to)
1512 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1513 memset(type, 0, sizeof(reference_type_t));
1515 type->kind = TYPE_REFERENCE;
1516 type->base.qualifiers = 0;
1517 type->base.alignment = 0;
1518 type->reference.refers_to = refers_to;
1520 return identify_new_type(type);
1524 * Creates a new based pointer type.
1526 * @param points_to The points-to type for the new type.
1527 * @param qualifiers Type qualifiers for the new type.
1528 * @param variable The based variable
1530 type_t *make_based_pointer_type(type_t *points_to,
1531 type_qualifiers_t qualifiers, variable_t *variable)
1533 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1534 memset(type, 0, sizeof(pointer_type_t));
1536 type->kind = TYPE_POINTER;
1537 type->base.qualifiers = qualifiers;
1538 type->base.alignment = 0;
1539 type->pointer.points_to = points_to;
1540 type->pointer.base_variable = variable;
1542 return identify_new_type(type);
1546 type_t *make_array_type(type_t *element_type, size_t size,
1547 type_qualifiers_t qualifiers)
1549 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1550 memset(type, 0, sizeof(array_type_t));
1552 type->kind = TYPE_ARRAY;
1553 type->base.qualifiers = qualifiers;
1554 type->base.alignment = 0;
1555 type->array.element_type = element_type;
1556 type->array.size = size;
1557 type->array.size_constant = true;
1559 return identify_new_type(type);
1563 * Debug helper. Prints the given type to stdout.
1565 static __attribute__((unused))
1566 void dbg_type(const type_t *type)
1568 FILE *old_out = out;