2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "type_hash.h"
29 #include "adt/error.h"
30 #include "lang_features.h"
32 static struct obstack _type_obst;
34 struct obstack *type_obst = &_type_obst;
35 static int type_visited = 0;
36 static bool print_implicit_array_size = false;
38 static void intern_print_type_pre(const type_t *type, bool top);
39 static void intern_print_type_post(const type_t *type, bool top);
41 typedef struct atomic_type_properties_t atomic_type_properties_t;
42 struct atomic_type_properties_t {
43 unsigned size; /**< type size in bytes */
44 unsigned alignment; /**< type alignment in bytes */
45 unsigned flags; /**< type flags from atomic_type_flag_t */
48 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
49 //ATOMIC_TYPE_INVALID = 0,
50 [ATOMIC_TYPE_VOID] = {
53 .flags = ATOMIC_TYPE_FLAG_NONE
55 [ATOMIC_TYPE_WCHAR_T] = {
57 .alignment = (unsigned)-1,
58 /* signed flag will be set when known */
59 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
61 [ATOMIC_TYPE_CHAR] = {
64 /* signed flag will be set when known */
65 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
67 [ATOMIC_TYPE_SCHAR] = {
70 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
71 | ATOMIC_TYPE_FLAG_SIGNED,
73 [ATOMIC_TYPE_UCHAR] = {
76 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
78 [ATOMIC_TYPE_SHORT] = {
81 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
82 | ATOMIC_TYPE_FLAG_SIGNED
84 [ATOMIC_TYPE_USHORT] = {
87 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
90 .size = (unsigned) -1,
91 .alignment = (unsigned) -1,
92 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
93 | ATOMIC_TYPE_FLAG_SIGNED,
95 [ATOMIC_TYPE_UINT] = {
96 .size = (unsigned) -1,
97 .alignment = (unsigned) -1,
98 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
100 [ATOMIC_TYPE_LONG] = {
101 .size = (unsigned) -1,
102 .alignment = (unsigned) -1,
103 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
104 | ATOMIC_TYPE_FLAG_SIGNED,
106 [ATOMIC_TYPE_ULONG] = {
107 .size = (unsigned) -1,
108 .alignment = (unsigned) -1,
109 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
111 [ATOMIC_TYPE_LONGLONG] = {
112 .size = (unsigned) -1,
113 .alignment = (unsigned) -1,
114 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
115 | ATOMIC_TYPE_FLAG_SIGNED,
117 [ATOMIC_TYPE_ULONGLONG] = {
118 .size = (unsigned) -1,
119 .alignment = (unsigned) -1,
120 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
122 [ATOMIC_TYPE_BOOL] = {
123 .size = (unsigned) -1,
124 .alignment = (unsigned) -1,
125 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
127 [ATOMIC_TYPE_FLOAT] = {
129 .alignment = (unsigned) -1,
130 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
131 | ATOMIC_TYPE_FLAG_SIGNED,
133 [ATOMIC_TYPE_DOUBLE] = {
135 .alignment = (unsigned) -1,
136 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
137 | ATOMIC_TYPE_FLAG_SIGNED,
139 [ATOMIC_TYPE_LONG_DOUBLE] = {
141 .alignment = (unsigned) -1,
142 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
143 | ATOMIC_TYPE_FLAG_SIGNED,
145 /* complex and imaginary types are set in init_types */
148 void init_types(void)
150 obstack_init(type_obst);
152 atomic_type_properties_t *props = atomic_type_properties;
154 if (char_is_signed) {
155 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
158 unsigned int_size = machine_size < 32 ? 2 : 4;
159 unsigned long_size = machine_size < 64 ? 4 : 8;
160 unsigned llong_size = machine_size < 32 ? 4 : 8;
162 props[ATOMIC_TYPE_INT].size = int_size;
163 props[ATOMIC_TYPE_INT].alignment = int_size;
164 props[ATOMIC_TYPE_UINT].size = int_size;
165 props[ATOMIC_TYPE_UINT].alignment = int_size;
166 props[ATOMIC_TYPE_LONG].size = long_size;
167 props[ATOMIC_TYPE_LONG].alignment = long_size;
168 props[ATOMIC_TYPE_ULONG].size = long_size;
169 props[ATOMIC_TYPE_ULONG].alignment = long_size;
170 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
171 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
172 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
173 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
175 /* TODO: backend specific, need a way to query the backend for this.
176 * The following are good settings for x86 */
177 props[ATOMIC_TYPE_FLOAT].alignment = 4;
178 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
179 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
180 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
181 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
183 /* TODO: make this configurable for platforms which do not use byte sized
185 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
187 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
190 void exit_types(void)
192 obstack_free(type_obst, NULL);
195 void type_set_output(FILE *stream)
200 void inc_type_visited(void)
205 void print_type_qualifiers(type_qualifiers_t qualifiers)
208 if (qualifiers & TYPE_QUALIFIER_CONST) {
209 fputs(" const" + first, out);
212 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
213 fputs(" volatile" + first, out);
216 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
217 fputs(" restrict" + first, out);
222 const char *get_atomic_kind_name(atomic_type_kind_t kind)
225 case ATOMIC_TYPE_INVALID: break;
226 case ATOMIC_TYPE_VOID: return "void";
227 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
228 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
229 case ATOMIC_TYPE_CHAR: return "char";
230 case ATOMIC_TYPE_SCHAR: return "signed char";
231 case ATOMIC_TYPE_UCHAR: return "unsigned char";
232 case ATOMIC_TYPE_INT: return "int";
233 case ATOMIC_TYPE_UINT: return "unsigned int";
234 case ATOMIC_TYPE_SHORT: return "short";
235 case ATOMIC_TYPE_USHORT: return "unsigned short";
236 case ATOMIC_TYPE_LONG: return "long";
237 case ATOMIC_TYPE_ULONG: return "unsigned long";
238 case ATOMIC_TYPE_LONGLONG: return "long long";
239 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
240 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
241 case ATOMIC_TYPE_FLOAT: return "float";
242 case ATOMIC_TYPE_DOUBLE: return "double";
244 return "INVALIDATOMIC";
248 * Prints the name of an atomic type kinds.
250 * @param kind The type kind.
252 static void print_atomic_kinds(atomic_type_kind_t kind)
254 const char *s = get_atomic_kind_name(kind);
259 * Prints the name of an atomic type.
261 * @param type The type.
263 static void print_atomic_type(const atomic_type_t *type)
265 print_type_qualifiers(type->base.qualifiers);
266 if (type->base.qualifiers != 0)
268 print_atomic_kinds(type->akind);
272 * Prints the name of a complex type.
274 * @param type The type.
277 void print_complex_type(const complex_type_t *type)
279 int empty = type->base.qualifiers == 0;
280 print_type_qualifiers(type->base.qualifiers);
281 fputs(" _Complex " + empty, out);
282 print_atomic_kinds(type->akind);
286 * Prints the name of an imaginary type.
288 * @param type The type.
291 void print_imaginary_type(const imaginary_type_t *type)
293 int empty = type->base.qualifiers == 0;
294 print_type_qualifiers(type->base.qualifiers);
295 fputs(" _Imaginary " + empty, out);
296 print_atomic_kinds(type->akind);
300 * Print the first part (the prefix) of a type.
302 * @param type The type to print.
303 * @param top true, if this is the top type, false if it's an embedded type.
305 static void print_function_type_pre(const function_type_t *type, bool top)
307 switch (type->linkage) {
308 case LINKAGE_INVALID:
313 fputs("extern \"C\" ", out);
317 if (!(c_mode & _CXX))
318 fputs("extern \"C++\" ", out);
322 print_type_qualifiers(type->base.qualifiers);
323 if (type->base.qualifiers != 0)
326 intern_print_type_pre(type->return_type, false);
328 switch (type->calling_convention) {
329 case CC_CDECL: fputs("__cdecl ", out); break;
330 case CC_STDCALL: fputs("__stdcall ", out); break;
331 case CC_FASTCALL: fputs("__fastcall ", out); break;
332 case CC_THISCALL: fputs("__thiscall ", out); break;
333 case CC_DEFAULT: break;
336 /* don't emit parenthesis if we're the toplevel type... */
342 * Print the second part (the postfix) of a type.
344 * @param type The type to print.
345 * @param top true, if this is the top type, false if it's an embedded type.
347 static void print_function_type_post(const function_type_t *type,
348 const scope_t *parameters, bool top)
350 /* don't emit parenthesis if we're the toplevel type... */
356 if (parameters == NULL) {
357 function_parameter_t *parameter = type->parameters;
358 for( ; parameter != NULL; parameter = parameter->next) {
364 print_type(parameter->type);
367 entity_t *parameter = parameters->entities;
368 for (; parameter != NULL; parameter = parameter->base.next) {
374 assert(is_declaration(parameter));
375 const type_t *const type = parameter->declaration.type;
377 fputs(parameter->base.symbol->string, out);
379 print_type_ext(type, parameter->base.symbol, NULL);
383 if (type->variadic) {
391 if (first && !type->unspecified_parameters) {
396 intern_print_type_post(type->return_type, false);
400 * Prints the prefix part of a pointer type.
402 * @param type The pointer type.
404 static void print_pointer_type_pre(const pointer_type_t *type)
406 intern_print_type_pre(type->points_to, false);
407 variable_t *const variable = type->base_variable;
408 if (variable != NULL) {
409 fputs(" __based(", out);
410 fputs(variable->base.base.symbol->string, out);
414 print_type_qualifiers(type->base.qualifiers);
415 if (type->base.qualifiers != 0)
420 * Prints the prefix part of a reference type.
422 * @param type The reference type.
424 static void print_reference_type_pre(const reference_type_t *type)
426 intern_print_type_pre(type->refers_to, false);
431 * Prints the postfix part of a pointer type.
433 * @param type The pointer type.
435 static void print_pointer_type_post(const pointer_type_t *type)
437 intern_print_type_post(type->points_to, false);
441 * Prints the postfix part of a reference type.
443 * @param type The reference type.
445 static void print_reference_type_post(const reference_type_t *type)
447 intern_print_type_post(type->refers_to, false);
451 * Prints the prefix part of an array type.
453 * @param type The array type.
455 static void print_array_type_pre(const array_type_t *type)
457 intern_print_type_pre(type->element_type, false);
461 * Prints the postfix part of an array type.
463 * @param type The array type.
465 static void print_array_type_post(const array_type_t *type)
468 if (type->is_static) {
469 fputs("static ", out);
471 print_type_qualifiers(type->base.qualifiers);
472 if (type->base.qualifiers != 0)
474 if (type->size_expression != NULL
475 && (print_implicit_array_size || !type->has_implicit_size)) {
476 print_expression(type->size_expression);
479 intern_print_type_post(type->element_type, false);
483 * Prints the postfix part of a bitfield type.
485 * @param type The array type.
487 static void print_bitfield_type_post(const bitfield_type_t *type)
490 print_expression(type->size_expression);
491 intern_print_type_post(type->base_type, false);
495 * Prints an enum definition.
497 * @param declaration The enum's type declaration.
499 void print_enum_definition(const enum_t *enume)
505 entity_t *entry = enume->base.next;
506 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
507 entry = entry->base.next) {
510 fputs(entry->base.symbol->string, out);
511 if (entry->enum_value.value != NULL) {
514 /* skip the implicit cast */
515 expression_t *expression = entry->enum_value.value;
516 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
517 expression = expression->unary.value;
519 print_expression(expression);
530 * Prints an enum type.
532 * @param type The enum type.
534 static void print_type_enum(const enum_type_t *type)
536 int empty = type->base.qualifiers == 0;
537 print_type_qualifiers(type->base.qualifiers);
538 fputs(" enum " + empty, out);
540 enum_t *enume = type->enume;
541 symbol_t *symbol = enume->base.symbol;
542 if (symbol != NULL) {
543 fputs(symbol->string, out);
545 print_enum_definition(enume);
550 * Print the compound part of a compound type.
552 void print_compound_definition(const compound_t *compound)
557 entity_t *entity = compound->members.entities;
558 for( ; entity != NULL; entity = entity->base.next) {
559 if (entity->kind != ENTITY_COMPOUND_MEMBER)
563 print_entity(entity);
570 if (compound->modifiers & DM_TRANSPARENT_UNION) {
571 fputs("__attribute__((__transparent_union__))", out);
576 * Prints a compound type.
578 * @param type The compound type.
580 static void print_compound_type(const compound_type_t *type)
582 int empty = type->base.qualifiers == 0;
583 print_type_qualifiers(type->base.qualifiers);
585 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
586 fputs(" struct " + empty, out);
588 assert(type->base.kind == TYPE_COMPOUND_UNION);
589 fputs(" union " + empty, out);
592 compound_t *compound = type->compound;
593 symbol_t *symbol = compound->base.symbol;
594 if (symbol != NULL) {
595 fputs(symbol->string, out);
597 print_compound_definition(compound);
602 * Prints the prefix part of a typedef type.
604 * @param type The typedef type.
606 static void print_typedef_type_pre(const typedef_type_t *const type)
608 print_type_qualifiers(type->base.qualifiers);
609 if (type->base.qualifiers != 0)
611 fputs(type->typedefe->base.symbol->string, out);
615 * Prints the prefix part of a typeof type.
617 * @param type The typeof type.
619 static void print_typeof_type_pre(const typeof_type_t *const type)
621 fputs("typeof(", out);
622 if (type->expression != NULL) {
623 assert(type->typeof_type == NULL);
624 print_expression(type->expression);
626 print_type(type->typeof_type);
632 * Prints the prefix part of a type.
634 * @param type The type.
635 * @param top true if we print the toplevel type, false else.
637 static void intern_print_type_pre(const type_t *const type, const bool top)
641 fputs("<error>", out);
644 fputs("<invalid>", out);
647 print_type_enum(&type->enumt);
650 print_atomic_type(&type->atomic);
653 print_complex_type(&type->complex);
656 print_imaginary_type(&type->imaginary);
658 case TYPE_COMPOUND_STRUCT:
659 case TYPE_COMPOUND_UNION:
660 print_compound_type(&type->compound);
663 fputs(type->builtin.symbol->string, out);
666 print_function_type_pre(&type->function, top);
669 print_pointer_type_pre(&type->pointer);
672 print_reference_type_pre(&type->reference);
675 intern_print_type_pre(type->bitfield.base_type, top);
678 print_array_type_pre(&type->array);
681 print_typedef_type_pre(&type->typedeft);
684 print_typeof_type_pre(&type->typeoft);
687 fputs("unknown", out);
691 * Prints the postfix part of a type.
693 * @param type The type.
694 * @param top true if we print the toplevel type, false else.
696 static void intern_print_type_post(const type_t *const type, const bool top)
700 print_function_type_post(&type->function, NULL, top);
703 print_pointer_type_post(&type->pointer);
706 print_reference_type_post(&type->reference);
709 print_array_type_post(&type->array);
712 print_bitfield_type_post(&type->bitfield);
720 case TYPE_COMPOUND_STRUCT:
721 case TYPE_COMPOUND_UNION:
728 if (type->base.modifiers & DM_TRANSPARENT_UNION) {
729 fputs("__attribute__((__transparent_union__))", out);
736 * @param type The type.
738 void print_type(const type_t *const type)
740 print_type_ext(type, NULL, NULL);
743 void print_type_ext(const type_t *const type, const symbol_t *symbol,
744 const scope_t *parameters)
747 fputs("nil type", out);
751 intern_print_type_pre(type, true);
752 if (symbol != NULL) {
754 fputs(symbol->string, out);
756 if (type->kind == TYPE_FUNCTION) {
757 print_function_type_post(&type->function, parameters, true);
759 intern_print_type_post(type, true);
764 * Return the size of a type AST node.
766 * @param type The type.
768 static size_t get_type_size(const type_t *type)
771 case TYPE_ATOMIC: return sizeof(atomic_type_t);
772 case TYPE_COMPLEX: return sizeof(complex_type_t);
773 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
774 case TYPE_COMPOUND_STRUCT:
775 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
776 case TYPE_ENUM: return sizeof(enum_type_t);
777 case TYPE_FUNCTION: return sizeof(function_type_t);
778 case TYPE_POINTER: return sizeof(pointer_type_t);
779 case TYPE_REFERENCE: return sizeof(reference_type_t);
780 case TYPE_ARRAY: return sizeof(array_type_t);
781 case TYPE_BUILTIN: return sizeof(builtin_type_t);
782 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
783 case TYPE_TYPEOF: return sizeof(typeof_type_t);
784 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
785 case TYPE_ERROR: panic("error type found");
786 case TYPE_INVALID: panic("invalid type found");
788 panic("unknown type found");
794 * @param type The type to copy.
795 * @return A copy of the type.
797 * @note This does not produce a deep copy!
799 type_t *duplicate_type(const type_t *type)
801 size_t size = get_type_size(type);
803 type_t *copy = obstack_alloc(type_obst, size);
804 memcpy(copy, type, size);
810 * Returns the unqualified type of a given type.
812 * @param type The type.
813 * @returns The unqualified type.
815 type_t *get_unqualified_type(type_t *type)
817 assert(!is_typeref(type));
819 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
822 type_t *unqualified_type = duplicate_type(type);
823 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
825 type_t *result = typehash_insert(unqualified_type);
826 if (result != unqualified_type) {
827 obstack_free(type_obst, unqualified_type);
833 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
835 type_t *type = skip_typeref(orig_type);
838 if (is_type_array(type)) {
839 /* For array types the element type has to be adjusted */
840 type_t *element_type = type->array.element_type;
841 type_t *qual_element_type = get_qualified_type(element_type, qual);
843 if (qual_element_type == element_type)
846 copy = duplicate_type(type);
847 copy->array.element_type = qual_element_type;
848 } else if (is_type_valid(type)) {
849 if ((type->base.qualifiers & qual) == qual)
852 copy = duplicate_type(type);
853 copy->base.qualifiers |= qual;
858 type = typehash_insert(copy);
860 obstack_free(type_obst, copy);
866 * Check if a type is valid.
868 * @param type The type to check.
869 * @return true if type represents a valid type.
871 bool type_valid(const type_t *type)
873 return type->kind != TYPE_INVALID;
876 static bool test_atomic_type_flag(atomic_type_kind_t kind,
877 atomic_type_flag_t flag)
879 assert(kind <= ATOMIC_TYPE_LAST);
880 return (atomic_type_properties[kind].flags & flag) != 0;
884 * Returns true if the given type is an integer type.
886 * @param type The type to check.
887 * @return True if type is an integer type.
889 bool is_type_integer(const type_t *type)
891 assert(!is_typeref(type));
893 if (type->kind == TYPE_ENUM)
895 if (type->kind == TYPE_BITFIELD)
898 if (type->kind != TYPE_ATOMIC)
901 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
905 * Returns true if the given type is an enum type.
907 * @param type The type to check.
908 * @return True if type is an enum type.
910 bool is_type_enum(const type_t *type)
912 assert(!is_typeref(type));
913 return type->kind == TYPE_ENUM;
917 * Returns true if the given type is an floating point type.
919 * @param type The type to check.
920 * @return True if type is a floating point type.
922 bool is_type_float(const type_t *type)
924 assert(!is_typeref(type));
926 if (type->kind != TYPE_ATOMIC)
929 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
933 * Returns true if the given type is an complex type.
935 * @param type The type to check.
936 * @return True if type is a complex type.
938 bool is_type_complex(const type_t *type)
940 assert(!is_typeref(type));
942 if (type->kind != TYPE_ATOMIC)
945 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
949 * Returns true if the given type is a signed type.
951 * @param type The type to check.
952 * @return True if type is a signed type.
954 bool is_type_signed(const type_t *type)
956 assert(!is_typeref(type));
958 /* enum types are int for now */
959 if (type->kind == TYPE_ENUM)
961 if (type->kind == TYPE_BITFIELD)
962 return is_type_signed(type->bitfield.base_type);
964 if (type->kind != TYPE_ATOMIC)
967 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
971 * Returns true if the given type represents an arithmetic type.
973 * @param type The type to check.
974 * @return True if type represents an arithmetic type.
976 bool is_type_arithmetic(const type_t *type)
978 assert(!is_typeref(type));
985 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
987 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
989 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
996 * Returns true if the given type is an integer or float type.
998 * @param type The type to check.
999 * @return True if type is an integer or float type.
1001 bool is_type_real(const type_t *type)
1004 return is_type_integer(type) || is_type_float(type);
1008 * Returns true if the given type represents a scalar type.
1010 * @param type The type to check.
1011 * @return True if type represents a scalar type.
1013 bool is_type_scalar(const type_t *type)
1015 assert(!is_typeref(type));
1017 switch (type->kind) {
1018 case TYPE_POINTER: return true;
1019 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1023 return is_type_arithmetic(type);
1027 * Check if a given type is incomplete.
1029 * @param type The type to check.
1030 * @return True if the given type is incomplete (ie. just forward).
1032 bool is_type_incomplete(const type_t *type)
1034 assert(!is_typeref(type));
1036 switch(type->kind) {
1037 case TYPE_COMPOUND_STRUCT:
1038 case TYPE_COMPOUND_UNION: {
1039 const compound_type_t *compound_type = &type->compound;
1040 return !compound_type->compound->complete;
1046 return type->array.size_expression == NULL
1047 && !type->array.size_constant;
1050 return type->atomic.akind == ATOMIC_TYPE_VOID;
1053 return type->complex.akind == ATOMIC_TYPE_VOID;
1055 case TYPE_IMAGINARY:
1056 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1061 case TYPE_REFERENCE:
1068 panic("is_type_incomplete called without typerefs skipped");
1073 panic("invalid type found");
1076 bool is_type_object(const type_t *type)
1078 return !is_type_function(type) && !is_type_incomplete(type);
1082 * Check if two function types are compatible.
1084 static bool function_types_compatible(const function_type_t *func1,
1085 const function_type_t *func2)
1087 const type_t* const ret1 = skip_typeref(func1->return_type);
1088 const type_t* const ret2 = skip_typeref(func2->return_type);
1089 if (!types_compatible(ret1, ret2))
1092 if (func1->linkage != func2->linkage)
1095 if (func1->calling_convention != func2->calling_convention)
1098 /* can parameters be compared? */
1099 if (func1->unspecified_parameters || func2->unspecified_parameters)
1102 if (func1->variadic != func2->variadic)
1105 /* TODO: handling of unspecified parameters not correct yet */
1107 /* all argument types must be compatible */
1108 function_parameter_t *parameter1 = func1->parameters;
1109 function_parameter_t *parameter2 = func2->parameters;
1110 for ( ; parameter1 != NULL && parameter2 != NULL;
1111 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1112 type_t *parameter1_type = skip_typeref(parameter1->type);
1113 type_t *parameter2_type = skip_typeref(parameter2->type);
1115 parameter1_type = get_unqualified_type(parameter1_type);
1116 parameter2_type = get_unqualified_type(parameter2_type);
1118 if (!types_compatible(parameter1_type, parameter2_type))
1121 /* same number of arguments? */
1122 if (parameter1 != NULL || parameter2 != NULL)
1129 * Check if two array types are compatible.
1131 static bool array_types_compatible(const array_type_t *array1,
1132 const array_type_t *array2)
1134 type_t *element_type1 = skip_typeref(array1->element_type);
1135 type_t *element_type2 = skip_typeref(array2->element_type);
1136 if (!types_compatible(element_type1, element_type2))
1139 if (!array1->size_constant || !array2->size_constant)
1142 return array1->size == array2->size;
1146 * Check if two types are compatible.
1148 bool types_compatible(const type_t *type1, const type_t *type2)
1150 assert(!is_typeref(type1));
1151 assert(!is_typeref(type2));
1153 /* shortcut: the same type is always compatible */
1157 if (!is_type_valid(type1) || !is_type_valid(type2))
1160 if (type1->base.qualifiers != type2->base.qualifiers)
1162 if (type1->kind != type2->kind)
1165 switch (type1->kind) {
1167 return function_types_compatible(&type1->function, &type2->function);
1169 return type1->atomic.akind == type2->atomic.akind;
1171 return type1->complex.akind == type2->complex.akind;
1172 case TYPE_IMAGINARY:
1173 return type1->imaginary.akind == type2->imaginary.akind;
1175 return array_types_compatible(&type1->array, &type2->array);
1177 case TYPE_POINTER: {
1178 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1179 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1180 return types_compatible(to1, to2);
1183 case TYPE_REFERENCE: {
1184 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1185 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1186 return types_compatible(to1, to2);
1189 case TYPE_COMPOUND_STRUCT:
1190 case TYPE_COMPOUND_UNION:
1193 /* TODO: not implemented */
1197 /* not sure if this makes sense or is even needed, implement it if you
1198 * really need it! */
1199 panic("type compatibility check for bitfield type");
1202 /* Hmm, the error type should be compatible to all other types */
1205 panic("invalid type found in compatible types");
1208 panic("typerefs not skipped in compatible types?!?");
1211 /* TODO: incomplete */
1216 * Skip all typerefs and return the underlying type.
1218 type_t *skip_typeref(type_t *type)
1220 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1221 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
1224 switch (type->kind) {
1227 case TYPE_TYPEDEF: {
1228 qualifiers |= type->base.qualifiers;
1229 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 const typeof_type_t *typeof_type = &type->typeoft;
1240 if (typeof_type->typeof_type != NULL) {
1241 type = typeof_type->typeof_type;
1243 type = typeof_type->expression->base.type;
1253 if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
1254 type_t *const copy = duplicate_type(type);
1256 /* for const with typedefed array type the element type has to be
1258 if (is_type_array(copy)) {
1259 type_t *element_type = copy->array.element_type;
1260 element_type = duplicate_type(element_type);
1261 element_type->base.qualifiers |= qualifiers;
1262 element_type->base.modifiers |= modifiers;
1263 copy->array.element_type = element_type;
1265 copy->base.qualifiers |= qualifiers;
1266 copy->base.modifiers |= modifiers;
1269 type = typehash_insert(copy);
1271 obstack_free(type_obst, copy);
1278 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type) {
1279 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1282 switch (type->base.kind) {
1284 return TYPE_QUALIFIER_NONE;
1286 qualifiers |= type->base.qualifiers;
1287 const typedef_type_t *typedef_type = &type->typedeft;
1288 if (typedef_type->resolved_type != NULL)
1289 type = typedef_type->resolved_type;
1291 type = typedef_type->typedefe->type;
1294 const typeof_type_t *typeof_type = &type->typeoft;
1295 if (typeof_type->typeof_type != NULL) {
1296 type = typeof_type->typeof_type;
1298 type = typeof_type->expression->base.type;
1303 if (skip_array_type) {
1304 type = type->array.element_type;
1313 return type->base.qualifiers | qualifiers;
1316 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1318 assert(kind <= ATOMIC_TYPE_LAST);
1319 return atomic_type_properties[kind].size;
1322 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1324 assert(kind <= ATOMIC_TYPE_LAST);
1325 return atomic_type_properties[kind].alignment;
1328 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1330 assert(kind <= ATOMIC_TYPE_LAST);
1331 return atomic_type_properties[kind].flags;
1334 atomic_type_kind_t get_intptr_kind(void)
1336 if (machine_size <= 32)
1337 return ATOMIC_TYPE_INT;
1338 else if (machine_size <= 64)
1339 return ATOMIC_TYPE_LONG;
1341 return ATOMIC_TYPE_LONGLONG;
1344 atomic_type_kind_t get_uintptr_kind(void)
1346 if (machine_size <= 32)
1347 return ATOMIC_TYPE_UINT;
1348 else if (machine_size <= 64)
1349 return ATOMIC_TYPE_ULONG;
1351 return ATOMIC_TYPE_ULONGLONG;
1355 * Find the atomic type kind representing a given size (signed).
1357 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1358 static atomic_type_kind_t kinds[32];
1361 atomic_type_kind_t kind = kinds[size];
1362 if (kind == ATOMIC_TYPE_INVALID) {
1363 static const atomic_type_kind_t possible_kinds[] = {
1368 ATOMIC_TYPE_LONGLONG
1370 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1371 if (get_atomic_type_size(possible_kinds[i]) == size) {
1372 kind = possible_kinds[i];
1382 * Find the atomic type kind representing a given size (signed).
1384 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1385 static atomic_type_kind_t kinds[32];
1388 atomic_type_kind_t kind = kinds[size];
1389 if (kind == ATOMIC_TYPE_INVALID) {
1390 static const atomic_type_kind_t possible_kinds[] = {
1395 ATOMIC_TYPE_ULONGLONG
1397 for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1398 if (get_atomic_type_size(possible_kinds[i]) == size) {
1399 kind = possible_kinds[i];
1409 * Hash the given type and return the "singleton" version
1412 static type_t *identify_new_type(type_t *type)
1414 type_t *result = typehash_insert(type);
1415 if (result != type) {
1416 obstack_free(type_obst, type);
1422 * Creates a new atomic type.
1424 * @param akind The kind of the atomic type.
1425 * @param qualifiers Type qualifiers for the new type.
1427 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1429 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1430 memset(type, 0, sizeof(atomic_type_t));
1432 type->kind = TYPE_ATOMIC;
1433 type->base.size = get_atomic_type_size(akind);
1434 type->base.alignment = get_atomic_type_alignment(akind);
1435 type->base.qualifiers = qualifiers;
1436 type->atomic.akind = akind;
1438 return identify_new_type(type);
1442 * Creates a new complex type.
1444 * @param akind The kind of the atomic type.
1445 * @param qualifiers Type qualifiers for the new type.
1447 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1449 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1450 memset(type, 0, sizeof(complex_type_t));
1452 type->kind = TYPE_COMPLEX;
1453 type->base.qualifiers = qualifiers;
1454 type->base.alignment = get_atomic_type_alignment(akind);
1455 type->complex.akind = akind;
1457 return identify_new_type(type);
1461 * Creates a new imaginary type.
1463 * @param akind The kind of the atomic type.
1464 * @param qualifiers Type qualifiers for the new type.
1466 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1468 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1469 memset(type, 0, sizeof(imaginary_type_t));
1471 type->kind = TYPE_IMAGINARY;
1472 type->base.qualifiers = qualifiers;
1473 type->base.alignment = get_atomic_type_alignment(akind);
1474 type->imaginary.akind = akind;
1476 return identify_new_type(type);
1480 * Creates a new pointer type.
1482 * @param points_to The points-to type for the new type.
1483 * @param qualifiers Type qualifiers for the new type.
1485 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1487 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1488 memset(type, 0, sizeof(pointer_type_t));
1490 type->kind = TYPE_POINTER;
1491 type->base.qualifiers = qualifiers;
1492 type->base.alignment = 0;
1493 type->pointer.points_to = points_to;
1494 type->pointer.base_variable = NULL;
1496 return identify_new_type(type);
1500 * Creates a new reference type.
1502 * @param refers_to The referred-to type for the new type.
1504 type_t *make_reference_type(type_t *refers_to)
1506 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1507 memset(type, 0, sizeof(reference_type_t));
1509 type->kind = TYPE_REFERENCE;
1510 type->base.qualifiers = 0;
1511 type->base.alignment = 0;
1512 type->reference.refers_to = refers_to;
1514 return identify_new_type(type);
1518 * Creates a new based pointer type.
1520 * @param points_to The points-to type for the new type.
1521 * @param qualifiers Type qualifiers for the new type.
1522 * @param variable The based variable
1524 type_t *make_based_pointer_type(type_t *points_to,
1525 type_qualifiers_t qualifiers, variable_t *variable)
1527 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1528 memset(type, 0, sizeof(pointer_type_t));
1530 type->kind = TYPE_POINTER;
1531 type->base.qualifiers = qualifiers;
1532 type->base.alignment = 0;
1533 type->pointer.points_to = points_to;
1534 type->pointer.base_variable = variable;
1536 return identify_new_type(type);
1540 type_t *make_array_type(type_t *element_type, size_t size,
1541 type_qualifiers_t qualifiers)
1543 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1544 memset(type, 0, sizeof(array_type_t));
1546 type->kind = TYPE_ARRAY;
1547 type->base.qualifiers = qualifiers;
1548 type->base.alignment = 0;
1549 type->array.element_type = element_type;
1550 type->array.size = size;
1551 type->array.size_constant = true;
1553 return identify_new_type(type);
1557 * Debug helper. Prints the given type to stdout.
1559 static __attribute__((unused))
1560 void dbg_type(const type_t *type)
1562 FILE *old_out = out;