2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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 #include "diagnostic.h"
35 #include "driver/firm_cmdline.h"
37 /** The default calling convention. */
38 cc_kind_t default_calling_convention = CC_CDECL;
40 static struct obstack _type_obst;
42 struct obstack *type_obst = &_type_obst;
43 static bool print_implicit_array_size = false;
45 static void intern_print_type_pre(const type_t *type);
46 static void intern_print_type_post(const type_t *type);
48 typedef struct atomic_type_properties_t atomic_type_properties_t;
49 struct atomic_type_properties_t {
50 unsigned size; /**< type size in bytes */
51 unsigned alignment; /**< type alignment in bytes */
52 unsigned flags; /**< type flags from atomic_type_flag_t */
56 * Properties of atomic types.
58 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
59 //ATOMIC_TYPE_INVALID = 0,
60 [ATOMIC_TYPE_VOID] = {
63 .flags = ATOMIC_TYPE_FLAG_NONE
65 [ATOMIC_TYPE_WCHAR_T] = {
67 .alignment = (unsigned)-1,
68 /* signed flag will be set when known */
69 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
71 [ATOMIC_TYPE_CHAR] = {
74 /* signed flag will be set when known */
75 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
77 [ATOMIC_TYPE_SCHAR] = {
80 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
81 | ATOMIC_TYPE_FLAG_SIGNED,
83 [ATOMIC_TYPE_UCHAR] = {
86 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
88 [ATOMIC_TYPE_SHORT] = {
91 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
92 | ATOMIC_TYPE_FLAG_SIGNED
94 [ATOMIC_TYPE_USHORT] = {
97 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
100 .size = (unsigned) -1,
101 .alignment = (unsigned) -1,
102 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
103 | ATOMIC_TYPE_FLAG_SIGNED,
105 [ATOMIC_TYPE_UINT] = {
106 .size = (unsigned) -1,
107 .alignment = (unsigned) -1,
108 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
110 [ATOMIC_TYPE_LONG] = {
111 .size = (unsigned) -1,
112 .alignment = (unsigned) -1,
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
114 | ATOMIC_TYPE_FLAG_SIGNED,
116 [ATOMIC_TYPE_ULONG] = {
117 .size = (unsigned) -1,
118 .alignment = (unsigned) -1,
119 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
121 [ATOMIC_TYPE_LONGLONG] = {
122 .size = (unsigned) -1,
123 .alignment = (unsigned) -1,
124 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
125 | ATOMIC_TYPE_FLAG_SIGNED,
127 [ATOMIC_TYPE_ULONGLONG] = {
128 .size = (unsigned) -1,
129 .alignment = (unsigned) -1,
130 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
132 [ATOMIC_TYPE_BOOL] = {
133 .size = (unsigned) -1,
134 .alignment = (unsigned) -1,
135 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
137 [ATOMIC_TYPE_FLOAT] = {
139 .alignment = (unsigned) -1,
140 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
141 | ATOMIC_TYPE_FLAG_SIGNED,
143 [ATOMIC_TYPE_DOUBLE] = {
145 .alignment = (unsigned) -1,
146 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
147 | ATOMIC_TYPE_FLAG_SIGNED,
149 [ATOMIC_TYPE_LONG_DOUBLE] = {
151 .alignment = (unsigned) -1,
152 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
153 | ATOMIC_TYPE_FLAG_SIGNED,
155 /* complex and imaginary types are set in init_types */
158 void init_types(void)
160 obstack_init(type_obst);
162 atomic_type_properties_t *props = atomic_type_properties;
164 if (char_is_signed) {
165 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
168 unsigned int_size = machine_size < 32 ? 2 : 4;
169 unsigned long_size = machine_size < 64 ? 4 : 8;
170 unsigned llong_size = machine_size < 32 ? 4 : 8;
172 props[ATOMIC_TYPE_INT].size = int_size;
173 props[ATOMIC_TYPE_INT].alignment = int_size;
174 props[ATOMIC_TYPE_UINT].size = int_size;
175 props[ATOMIC_TYPE_UINT].alignment = int_size;
176 props[ATOMIC_TYPE_LONG].size = long_size;
177 props[ATOMIC_TYPE_LONG].alignment = long_size;
178 props[ATOMIC_TYPE_ULONG].size = long_size;
179 props[ATOMIC_TYPE_ULONG].alignment = long_size;
180 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
181 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
182 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
183 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
185 /* TODO: backend specific, need a way to query the backend for this.
186 * The following are good settings for x86 */
187 props[ATOMIC_TYPE_FLOAT].alignment = 4;
188 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
189 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
190 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
191 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
192 if (firm_opt.os_support == OS_SUPPORT_MACHO) {
193 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
194 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
197 /* TODO: make this configurable for platforms which do not use byte sized
199 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
201 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
204 void exit_types(void)
206 obstack_free(type_obst, NULL);
209 void type_set_output(FILE *stream)
214 void print_type_qualifiers(type_qualifiers_t qualifiers)
217 if (qualifiers & TYPE_QUALIFIER_CONST) {
218 fputs(" const" + first, out);
221 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
222 fputs(" volatile" + first, out);
225 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
226 fputs(" restrict" + first, out);
231 const char *get_atomic_kind_name(atomic_type_kind_t kind)
234 case ATOMIC_TYPE_INVALID: break;
235 case ATOMIC_TYPE_VOID: return "void";
236 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
237 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
238 case ATOMIC_TYPE_CHAR: return "char";
239 case ATOMIC_TYPE_SCHAR: return "signed char";
240 case ATOMIC_TYPE_UCHAR: return "unsigned char";
241 case ATOMIC_TYPE_INT: return "int";
242 case ATOMIC_TYPE_UINT: return "unsigned int";
243 case ATOMIC_TYPE_SHORT: return "short";
244 case ATOMIC_TYPE_USHORT: return "unsigned short";
245 case ATOMIC_TYPE_LONG: return "long";
246 case ATOMIC_TYPE_ULONG: return "unsigned long";
247 case ATOMIC_TYPE_LONGLONG: return "long long";
248 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
249 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
250 case ATOMIC_TYPE_FLOAT: return "float";
251 case ATOMIC_TYPE_DOUBLE: return "double";
253 return "INVALIDATOMIC";
257 * Prints the name of an atomic type kinds.
259 * @param kind The type kind.
261 static void print_atomic_kinds(atomic_type_kind_t kind)
263 const char *s = get_atomic_kind_name(kind);
268 * Prints the name of an atomic type.
270 * @param type The type.
272 static void print_atomic_type(const atomic_type_t *type)
274 print_type_qualifiers(type->base.qualifiers);
275 if (type->base.qualifiers != 0)
277 print_atomic_kinds(type->akind);
281 * Prints the name of a complex type.
283 * @param type The type.
286 void print_complex_type(const complex_type_t *type)
288 int empty = type->base.qualifiers == 0;
289 print_type_qualifiers(type->base.qualifiers);
290 fputs(" _Complex " + empty, out);
291 print_atomic_kinds(type->akind);
295 * Prints the name of an imaginary type.
297 * @param type The type.
300 void print_imaginary_type(const imaginary_type_t *type)
302 int empty = type->base.qualifiers == 0;
303 print_type_qualifiers(type->base.qualifiers);
304 fputs(" _Imaginary " + empty, out);
305 print_atomic_kinds(type->akind);
309 * Print the first part (the prefix) of a type.
311 * @param type The type to print.
313 static void print_function_type_pre(const function_type_t *type)
315 switch (type->linkage) {
316 case LINKAGE_INVALID:
321 fputs("extern \"C\" ", out);
325 if (!(c_mode & _CXX))
326 fputs("extern \"C++\" ", out);
330 print_type_qualifiers(type->base.qualifiers);
331 if (type->base.qualifiers != 0)
334 intern_print_type_pre(type->return_type);
336 cc_kind_t cc = type->calling_convention;
339 case CC_CDECL: fputs(" __cdecl", out); break;
340 case CC_STDCALL: fputs(" __stdcall", out); break;
341 case CC_FASTCALL: fputs(" __fastcall", out); break;
342 case CC_THISCALL: fputs(" __thiscall", out); break;
344 if (default_calling_convention != CC_CDECL) {
345 /* show the default calling convention if its not cdecl */
346 cc = default_calling_convention;
354 * Print the second part (the postfix) of a type.
356 * @param type The type to print.
358 static void print_function_type_post(const function_type_t *type,
359 const scope_t *parameters)
363 if (parameters == NULL) {
364 function_parameter_t *parameter = type->parameters;
365 for( ; parameter != NULL; parameter = parameter->next) {
371 print_type(parameter->type);
374 entity_t *parameter = parameters->entities;
375 for (; parameter != NULL; parameter = parameter->base.next) {
376 if (parameter->kind != ENTITY_PARAMETER)
384 const type_t *const type = parameter->declaration.type;
386 fputs(parameter->base.symbol->string, out);
388 print_type_ext(type, parameter->base.symbol, NULL);
392 if (type->variadic) {
400 if (first && !type->unspecified_parameters) {
405 intern_print_type_post(type->return_type);
409 * Prints the prefix part of a pointer type.
411 * @param type The pointer type.
413 static void print_pointer_type_pre(const pointer_type_t *type)
415 type_t const *const points_to = type->points_to;
416 intern_print_type_pre(points_to);
417 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
419 variable_t *const variable = type->base_variable;
420 if (variable != NULL) {
421 fputs(" __based(", out);
422 fputs(variable->base.base.symbol->string, out);
426 type_qualifiers_t const qual = type->base.qualifiers;
429 print_type_qualifiers(qual);
433 * Prints the postfix part of a pointer type.
435 * @param type The pointer type.
437 static void print_pointer_type_post(const pointer_type_t *type)
439 type_t const *const points_to = type->points_to;
440 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
442 intern_print_type_post(points_to);
446 * Prints the prefix part of a reference type.
448 * @param type The reference type.
450 static void print_reference_type_pre(const reference_type_t *type)
452 type_t const *const refers_to = type->refers_to;
453 intern_print_type_pre(refers_to);
454 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
460 * Prints the postfix part of a reference type.
462 * @param type The reference type.
464 static void print_reference_type_post(const reference_type_t *type)
466 type_t const *const refers_to = type->refers_to;
467 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
469 intern_print_type_post(refers_to);
473 * Prints the prefix part of an array type.
475 * @param type The array type.
477 static void print_array_type_pre(const array_type_t *type)
479 intern_print_type_pre(type->element_type);
483 * Prints the postfix part of an array type.
485 * @param type The array type.
487 static void print_array_type_post(const array_type_t *type)
490 if (type->is_static) {
491 fputs("static ", out);
493 print_type_qualifiers(type->base.qualifiers);
494 if (type->base.qualifiers != 0)
496 if (type->size_expression != NULL
497 && (print_implicit_array_size || !type->has_implicit_size)) {
498 print_expression(type->size_expression);
501 intern_print_type_post(type->element_type);
505 * Prints the postfix part of a bitfield type.
507 * @param type The array type.
509 static void print_bitfield_type_post(const bitfield_type_t *type)
512 print_expression(type->size_expression);
513 intern_print_type_post(type->base_type);
517 * Prints an enum definition.
519 * @param declaration The enum's type declaration.
521 void print_enum_definition(const enum_t *enume)
527 entity_t *entry = enume->base.next;
528 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
529 entry = entry->base.next) {
532 fputs(entry->base.symbol->string, out);
533 if (entry->enum_value.value != NULL) {
536 /* skip the implicit cast */
537 expression_t *expression = entry->enum_value.value;
538 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
539 expression = expression->unary.value;
541 print_expression(expression);
552 * Prints an enum type.
554 * @param type The enum type.
556 static void print_type_enum(const enum_type_t *type)
558 int empty = type->base.qualifiers == 0;
559 print_type_qualifiers(type->base.qualifiers);
560 fputs(" enum " + empty, out);
562 enum_t *enume = type->enume;
563 symbol_t *symbol = enume->base.symbol;
564 if (symbol != NULL) {
565 fputs(symbol->string, out);
567 print_enum_definition(enume);
572 * Print the compound part of a compound type.
574 void print_compound_definition(const compound_t *compound)
579 entity_t *entity = compound->members.entities;
580 for( ; entity != NULL; entity = entity->base.next) {
581 if (entity->kind != ENTITY_COMPOUND_MEMBER)
585 print_entity(entity);
592 if (compound->modifiers & DM_TRANSPARENT_UNION) {
593 fputs("__attribute__((__transparent_union__))", out);
598 * Prints a compound type.
600 * @param type The compound type.
602 static void print_compound_type(const compound_type_t *type)
604 int empty = type->base.qualifiers == 0;
605 print_type_qualifiers(type->base.qualifiers);
607 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
608 fputs(" struct " + empty, out);
610 assert(type->base.kind == TYPE_COMPOUND_UNION);
611 fputs(" union " + empty, out);
614 compound_t *compound = type->compound;
615 symbol_t *symbol = compound->base.symbol;
616 if (symbol != NULL) {
617 fputs(symbol->string, out);
619 print_compound_definition(compound);
624 * Prints the prefix part of a typedef type.
626 * @param type The typedef type.
628 static void print_typedef_type_pre(const typedef_type_t *const type)
630 print_type_qualifiers(type->base.qualifiers);
631 if (type->base.qualifiers != 0)
633 fputs(type->typedefe->base.symbol->string, out);
637 * Prints the prefix part of a typeof type.
639 * @param type The typeof type.
641 static void print_typeof_type_pre(const typeof_type_t *const type)
643 fputs("typeof(", out);
644 if (type->expression != NULL) {
645 print_expression(type->expression);
647 print_type(type->typeof_type);
653 * Prints the prefix part of a type.
655 * @param type The type.
657 static void intern_print_type_pre(const type_t *const type)
661 fputs("<error>", out);
664 fputs("<invalid>", out);
667 print_type_enum(&type->enumt);
670 print_atomic_type(&type->atomic);
673 print_complex_type(&type->complex);
676 print_imaginary_type(&type->imaginary);
678 case TYPE_COMPOUND_STRUCT:
679 case TYPE_COMPOUND_UNION:
680 print_compound_type(&type->compound);
683 fputs(type->builtin.symbol->string, out);
686 print_function_type_pre(&type->function);
689 print_pointer_type_pre(&type->pointer);
692 print_reference_type_pre(&type->reference);
695 intern_print_type_pre(type->bitfield.base_type);
698 print_array_type_pre(&type->array);
701 print_typedef_type_pre(&type->typedeft);
704 print_typeof_type_pre(&type->typeoft);
707 fputs("unknown", out);
711 * Prints the postfix part of a type.
713 * @param type The type.
715 static void intern_print_type_post(const type_t *const type)
719 print_function_type_post(&type->function, NULL);
722 print_pointer_type_post(&type->pointer);
725 print_reference_type_post(&type->reference);
728 print_array_type_post(&type->array);
731 print_bitfield_type_post(&type->bitfield);
739 case TYPE_COMPOUND_STRUCT:
740 case TYPE_COMPOUND_UNION:
751 * @param type The type.
753 void print_type(const type_t *const type)
755 print_type_ext(type, NULL, NULL);
758 void print_type_ext(const type_t *const type, const symbol_t *symbol,
759 const scope_t *parameters)
762 fputs("nil type", out);
766 intern_print_type_pre(type);
767 if (symbol != NULL) {
769 fputs(symbol->string, out);
771 if (type->kind == TYPE_FUNCTION) {
772 print_function_type_post(&type->function, parameters);
774 intern_print_type_post(type);
779 * Return the size of a type AST node.
781 * @param type The type.
783 static size_t get_type_struct_size(const type_t *type)
786 case TYPE_ATOMIC: return sizeof(atomic_type_t);
787 case TYPE_COMPLEX: return sizeof(complex_type_t);
788 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
789 case TYPE_COMPOUND_STRUCT:
790 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
791 case TYPE_ENUM: return sizeof(enum_type_t);
792 case TYPE_FUNCTION: return sizeof(function_type_t);
793 case TYPE_POINTER: return sizeof(pointer_type_t);
794 case TYPE_REFERENCE: return sizeof(reference_type_t);
795 case TYPE_ARRAY: return sizeof(array_type_t);
796 case TYPE_BUILTIN: return sizeof(builtin_type_t);
797 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
798 case TYPE_TYPEOF: return sizeof(typeof_type_t);
799 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
800 case TYPE_ERROR: panic("error type found");
801 case TYPE_INVALID: panic("invalid type found");
803 panic("unknown type found");
809 * @param type The type to copy.
810 * @return A copy of the type.
812 * @note This does not produce a deep copy!
814 type_t *duplicate_type(const type_t *type)
816 size_t size = get_type_struct_size(type);
818 type_t *copy = obstack_alloc(type_obst, size);
819 memcpy(copy, type, size);
820 copy->base.firm_type = NULL;
826 * Returns the unqualified type of a given type.
828 * @param type The type.
829 * @returns The unqualified type.
831 type_t *get_unqualified_type(type_t *type)
833 assert(!is_typeref(type));
835 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
838 type_t *unqualified_type = duplicate_type(type);
839 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
841 return identify_new_type(unqualified_type);
844 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
846 type_t *type = skip_typeref(orig_type);
849 if (is_type_array(type)) {
850 /* For array types the element type has to be adjusted */
851 type_t *element_type = type->array.element_type;
852 type_t *qual_element_type = get_qualified_type(element_type, qual);
854 if (qual_element_type == element_type)
857 copy = duplicate_type(type);
858 copy->array.element_type = qual_element_type;
859 } else if (is_type_valid(type)) {
860 if ((type->base.qualifiers & qual) == qual)
863 copy = duplicate_type(type);
864 copy->base.qualifiers |= qual;
869 return identify_new_type(copy);
873 * Check if a type is valid.
875 * @param type The type to check.
876 * @return true if type represents a valid type.
878 bool type_valid(const type_t *type)
880 return type->kind != TYPE_INVALID;
883 static bool test_atomic_type_flag(atomic_type_kind_t kind,
884 atomic_type_flag_t flag)
886 assert(kind <= ATOMIC_TYPE_LAST);
887 return (atomic_type_properties[kind].flags & flag) != 0;
891 * Returns true if the given type is an integer type.
893 * @param type The type to check.
894 * @return True if type is an integer type.
896 bool is_type_integer(const type_t *type)
898 assert(!is_typeref(type));
900 if (type->kind == TYPE_ENUM)
902 if (type->kind == TYPE_BITFIELD)
905 if (type->kind != TYPE_ATOMIC)
908 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
912 * Returns true if the given type is an enum type.
914 * @param type The type to check.
915 * @return True if type is an enum type.
917 bool is_type_enum(const type_t *type)
919 assert(!is_typeref(type));
920 return type->kind == TYPE_ENUM;
924 * Returns true if the given type is an floating point type.
926 * @param type The type to check.
927 * @return True if type is a floating point type.
929 bool is_type_float(const type_t *type)
931 assert(!is_typeref(type));
933 if (type->kind != TYPE_ATOMIC)
936 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
940 * Returns true if the given type is an complex type.
942 * @param type The type to check.
943 * @return True if type is a complex type.
945 bool is_type_complex(const type_t *type)
947 assert(!is_typeref(type));
949 if (type->kind != TYPE_ATOMIC)
952 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
956 * Returns true if the given type is a signed type.
958 * @param type The type to check.
959 * @return True if type is a signed type.
961 bool is_type_signed(const type_t *type)
963 assert(!is_typeref(type));
965 /* enum types are int for now */
966 if (type->kind == TYPE_ENUM)
968 if (type->kind == TYPE_BITFIELD)
969 return is_type_signed(type->bitfield.base_type);
971 if (type->kind != TYPE_ATOMIC)
974 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
978 * Returns true if the given type represents an arithmetic type.
980 * @param type The type to check.
981 * @return True if type represents an arithmetic type.
983 bool is_type_arithmetic(const type_t *type)
985 assert(!is_typeref(type));
992 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
994 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
996 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
1003 * Returns true if the given type is an integer or float type.
1005 * @param type The type to check.
1006 * @return True if type is an integer or float type.
1008 bool is_type_real(const type_t *type)
1011 return is_type_integer(type) || is_type_float(type);
1015 * Returns true if the given type represents a scalar type.
1017 * @param type The type to check.
1018 * @return True if type represents a scalar type.
1020 bool is_type_scalar(const type_t *type)
1022 assert(!is_typeref(type));
1024 switch (type->kind) {
1025 case TYPE_POINTER: return true;
1026 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1030 return is_type_arithmetic(type);
1034 * Check if a given type is incomplete.
1036 * @param type The type to check.
1037 * @return True if the given type is incomplete (ie. just forward).
1039 bool is_type_incomplete(const type_t *type)
1041 assert(!is_typeref(type));
1043 switch(type->kind) {
1044 case TYPE_COMPOUND_STRUCT:
1045 case TYPE_COMPOUND_UNION: {
1046 const compound_type_t *compound_type = &type->compound;
1047 return !compound_type->compound->complete;
1053 return type->array.size_expression == NULL
1054 && !type->array.size_constant;
1057 return type->atomic.akind == ATOMIC_TYPE_VOID;
1060 return type->complex.akind == ATOMIC_TYPE_VOID;
1062 case TYPE_IMAGINARY:
1063 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1068 case TYPE_REFERENCE:
1075 panic("is_type_incomplete called without typerefs skipped");
1080 panic("invalid type found");
1083 bool is_type_object(const type_t *type)
1085 return !is_type_function(type) && !is_type_incomplete(type);
1088 bool is_builtin_va_list(type_t *type)
1090 type_t *tp = skip_typeref(type);
1092 return tp->kind == type_valist->kind &&
1093 tp->builtin.symbol == type_valist->builtin.symbol;
1097 * Check if two function types are compatible.
1099 static bool function_types_compatible(const function_type_t *func1,
1100 const function_type_t *func2)
1102 const type_t* const ret1 = skip_typeref(func1->return_type);
1103 const type_t* const ret2 = skip_typeref(func2->return_type);
1104 if (!types_compatible(ret1, ret2))
1107 if (func1->linkage != func2->linkage)
1110 cc_kind_t cc1 = func1->calling_convention;
1111 if (cc1 == CC_DEFAULT)
1112 cc1 = default_calling_convention;
1113 cc_kind_t cc2 = func2->calling_convention;
1114 if (cc2 == CC_DEFAULT)
1115 cc2 = default_calling_convention;
1120 if (func1->variadic != func2->variadic)
1123 /* can parameters be compared? */
1124 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1125 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1128 /* TODO: handling of unspecified parameters not correct yet */
1130 /* all argument types must be compatible */
1131 function_parameter_t *parameter1 = func1->parameters;
1132 function_parameter_t *parameter2 = func2->parameters;
1133 for ( ; parameter1 != NULL && parameter2 != NULL;
1134 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1135 type_t *parameter1_type = skip_typeref(parameter1->type);
1136 type_t *parameter2_type = skip_typeref(parameter2->type);
1138 parameter1_type = get_unqualified_type(parameter1_type);
1139 parameter2_type = get_unqualified_type(parameter2_type);
1141 if (!types_compatible(parameter1_type, parameter2_type))
1144 /* same number of arguments? */
1145 if (parameter1 != NULL || parameter2 != NULL)
1152 * Check if two array types are compatible.
1154 static bool array_types_compatible(const array_type_t *array1,
1155 const array_type_t *array2)
1157 type_t *element_type1 = skip_typeref(array1->element_type);
1158 type_t *element_type2 = skip_typeref(array2->element_type);
1159 if (!types_compatible(element_type1, element_type2))
1162 if (!array1->size_constant || !array2->size_constant)
1165 return array1->size == array2->size;
1169 * Check if two types are compatible.
1171 bool types_compatible(const type_t *type1, const type_t *type2)
1173 assert(!is_typeref(type1));
1174 assert(!is_typeref(type2));
1176 /* shortcut: the same type is always compatible */
1180 if (!is_type_valid(type1) || !is_type_valid(type2))
1183 if (type1->base.qualifiers != type2->base.qualifiers)
1185 if (type1->kind != type2->kind)
1188 switch (type1->kind) {
1190 return function_types_compatible(&type1->function, &type2->function);
1192 return type1->atomic.akind == type2->atomic.akind;
1194 return type1->complex.akind == type2->complex.akind;
1195 case TYPE_IMAGINARY:
1196 return type1->imaginary.akind == type2->imaginary.akind;
1198 return array_types_compatible(&type1->array, &type2->array);
1200 case TYPE_POINTER: {
1201 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1202 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1203 return types_compatible(to1, to2);
1206 case TYPE_REFERENCE: {
1207 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1208 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1209 return types_compatible(to1, to2);
1212 case TYPE_COMPOUND_STRUCT:
1213 case TYPE_COMPOUND_UNION: {
1220 /* TODO: not implemented */
1224 /* not sure if this makes sense or is even needed, implement it if you
1225 * really need it! */
1226 panic("type compatibility check for bitfield type");
1229 /* Hmm, the error type should be compatible to all other types */
1232 panic("invalid type found in compatible types");
1235 panic("typerefs not skipped in compatible types?!?");
1238 /* TODO: incomplete */
1243 * Skip all typerefs and return the underlying type.
1245 type_t *skip_typeref(type_t *type)
1247 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1250 switch (type->kind) {
1253 case TYPE_TYPEDEF: {
1254 qualifiers |= type->base.qualifiers;
1256 const typedef_type_t *typedef_type = &type->typedeft;
1257 if (typedef_type->resolved_type != NULL) {
1258 type = typedef_type->resolved_type;
1261 type = typedef_type->typedefe->type;
1265 qualifiers |= type->base.qualifiers;
1266 type = type->typeoft.typeof_type;
1274 if (qualifiers != TYPE_QUALIFIER_NONE) {
1275 type_t *const copy = duplicate_type(type);
1277 /* for const with typedefed array type the element type has to be
1279 if (is_type_array(copy)) {
1280 type_t *element_type = copy->array.element_type;
1281 element_type = duplicate_type(element_type);
1282 element_type->base.qualifiers |= qualifiers;
1283 copy->array.element_type = element_type;
1285 copy->base.qualifiers |= qualifiers;
1288 type = identify_new_type(copy);
1294 unsigned get_type_size(type_t *type)
1296 switch (type->kind) {
1302 return get_atomic_type_size(type->atomic.akind);
1304 return get_atomic_type_size(type->complex.akind) * 2;
1305 case TYPE_IMAGINARY:
1306 return get_atomic_type_size(type->imaginary.akind);
1307 case TYPE_COMPOUND_UNION:
1308 layout_union_type(&type->compound);
1309 return type->compound.compound->size;
1310 case TYPE_COMPOUND_STRUCT:
1311 layout_struct_type(&type->compound);
1312 return type->compound.compound->size;
1314 return get_atomic_type_size(type->enumt.akind);
1316 return 0; /* non-const (but "address-const") */
1317 case TYPE_REFERENCE:
1319 /* TODO: make configurable by backend */
1322 /* TODO: correct if element_type is aligned? */
1323 il_size_t element_size = get_type_size(type->array.element_type);
1324 return type->array.size * element_size;
1329 return get_type_size(type->builtin.real_type);
1331 return get_type_size(type->typedeft.typedefe->type);
1333 if (type->typeoft.typeof_type) {
1334 return get_type_size(type->typeoft.typeof_type);
1336 return get_type_size(type->typeoft.expression->base.type);
1339 panic("invalid type in get_type_size");
1342 unsigned get_type_alignment(type_t *type)
1344 switch (type->kind) {
1350 return get_atomic_type_alignment(type->atomic.akind);
1352 return get_atomic_type_alignment(type->complex.akind);
1353 case TYPE_IMAGINARY:
1354 return get_atomic_type_alignment(type->imaginary.akind);
1355 case TYPE_COMPOUND_UNION:
1356 layout_union_type(&type->compound);
1357 return type->compound.compound->alignment;
1358 case TYPE_COMPOUND_STRUCT:
1359 layout_struct_type(&type->compound);
1360 return type->compound.compound->alignment;
1362 return get_atomic_type_alignment(type->enumt.akind);
1364 /* what is correct here? */
1366 case TYPE_REFERENCE:
1368 /* TODO: make configurable by backend */
1371 return get_type_alignment(type->array.element_type);
1375 return get_type_alignment(type->builtin.real_type);
1376 case TYPE_TYPEDEF: {
1377 il_alignment_t alignment
1378 = get_type_alignment(type->typedeft.typedefe->type);
1379 if (type->typedeft.typedefe->alignment > alignment)
1380 alignment = type->typedeft.typedefe->alignment;
1385 if (type->typeoft.typeof_type) {
1386 return get_type_alignment(type->typeoft.typeof_type);
1388 return get_type_alignment(type->typeoft.expression->base.type);
1391 panic("invalid type in get_type_alignment");
1394 decl_modifiers_t get_type_modifiers(const type_t *type)
1396 switch(type->kind) {
1400 case TYPE_COMPOUND_STRUCT:
1401 case TYPE_COMPOUND_UNION:
1402 return type->compound.compound->modifiers;
1404 return type->function.modifiers;
1408 case TYPE_IMAGINARY:
1409 case TYPE_REFERENCE:
1415 return get_type_modifiers(type->builtin.real_type);
1416 case TYPE_TYPEDEF: {
1417 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1418 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1422 if (type->typeoft.typeof_type) {
1423 return get_type_modifiers(type->typeoft.typeof_type);
1425 return get_type_modifiers(type->typeoft.expression->base.type);
1428 panic("invalid type found in get_type_modifiers");
1431 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1433 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1436 switch (type->base.kind) {
1438 return TYPE_QUALIFIER_NONE;
1440 qualifiers |= type->base.qualifiers;
1441 const typedef_type_t *typedef_type = &type->typedeft;
1442 if (typedef_type->resolved_type != NULL)
1443 type = typedef_type->resolved_type;
1445 type = typedef_type->typedefe->type;
1448 type = type->typeoft.typeof_type;
1451 if (skip_array_type) {
1452 type = type->array.element_type;
1461 return type->base.qualifiers | qualifiers;
1464 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1466 assert(kind <= ATOMIC_TYPE_LAST);
1467 return atomic_type_properties[kind].size;
1470 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1472 assert(kind <= ATOMIC_TYPE_LAST);
1473 return atomic_type_properties[kind].alignment;
1476 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1478 assert(kind <= ATOMIC_TYPE_LAST);
1479 return atomic_type_properties[kind].flags;
1482 atomic_type_kind_t get_intptr_kind(void)
1484 if (machine_size <= 32)
1485 return ATOMIC_TYPE_INT;
1486 else if (machine_size <= 64)
1487 return ATOMIC_TYPE_LONG;
1489 return ATOMIC_TYPE_LONGLONG;
1492 atomic_type_kind_t get_uintptr_kind(void)
1494 if (machine_size <= 32)
1495 return ATOMIC_TYPE_UINT;
1496 else if (machine_size <= 64)
1497 return ATOMIC_TYPE_ULONG;
1499 return ATOMIC_TYPE_ULONGLONG;
1503 * Find the atomic type kind representing a given size (signed).
1505 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1507 static atomic_type_kind_t kinds[32];
1510 atomic_type_kind_t kind = kinds[size];
1511 if (kind == ATOMIC_TYPE_INVALID) {
1512 static const atomic_type_kind_t possible_kinds[] = {
1517 ATOMIC_TYPE_LONGLONG
1519 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1520 if (get_atomic_type_size(possible_kinds[i]) == size) {
1521 kind = possible_kinds[i];
1531 * Find the atomic type kind representing a given size (signed).
1533 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1535 static atomic_type_kind_t kinds[32];
1538 atomic_type_kind_t kind = kinds[size];
1539 if (kind == ATOMIC_TYPE_INVALID) {
1540 static const atomic_type_kind_t possible_kinds[] = {
1545 ATOMIC_TYPE_ULONGLONG
1547 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1548 if (get_atomic_type_size(possible_kinds[i]) == size) {
1549 kind = possible_kinds[i];
1559 * Hash the given type and return the "singleton" version
1562 type_t *identify_new_type(type_t *type)
1564 type_t *result = typehash_insert(type);
1565 if (result != type) {
1566 obstack_free(type_obst, type);
1572 * Creates a new atomic type.
1574 * @param akind The kind of the atomic type.
1575 * @param qualifiers Type qualifiers for the new type.
1577 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1579 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1580 memset(type, 0, sizeof(atomic_type_t));
1582 type->kind = TYPE_ATOMIC;
1583 type->base.qualifiers = qualifiers;
1584 type->atomic.akind = akind;
1586 return identify_new_type(type);
1590 * Creates a new complex type.
1592 * @param akind The kind of the atomic type.
1593 * @param qualifiers Type qualifiers for the new type.
1595 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1597 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1598 memset(type, 0, sizeof(complex_type_t));
1600 type->kind = TYPE_COMPLEX;
1601 type->base.qualifiers = qualifiers;
1602 type->complex.akind = akind;
1604 return identify_new_type(type);
1608 * Creates a new imaginary type.
1610 * @param akind The kind of the atomic type.
1611 * @param qualifiers Type qualifiers for the new type.
1613 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1615 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1616 memset(type, 0, sizeof(imaginary_type_t));
1618 type->kind = TYPE_IMAGINARY;
1619 type->base.qualifiers = qualifiers;
1620 type->imaginary.akind = akind;
1622 return identify_new_type(type);
1626 * Creates a new pointer type.
1628 * @param points_to The points-to type for the new type.
1629 * @param qualifiers Type qualifiers for the new type.
1631 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1633 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1634 memset(type, 0, sizeof(pointer_type_t));
1636 type->kind = TYPE_POINTER;
1637 type->base.qualifiers = qualifiers;
1638 type->pointer.points_to = points_to;
1639 type->pointer.base_variable = NULL;
1641 return identify_new_type(type);
1645 * Creates a new reference type.
1647 * @param refers_to The referred-to type for the new type.
1649 type_t *make_reference_type(type_t *refers_to)
1651 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1652 memset(type, 0, sizeof(reference_type_t));
1654 type->kind = TYPE_REFERENCE;
1655 type->base.qualifiers = 0;
1656 type->reference.refers_to = refers_to;
1658 return identify_new_type(type);
1662 * Creates a new based pointer type.
1664 * @param points_to The points-to type for the new type.
1665 * @param qualifiers Type qualifiers for the new type.
1666 * @param variable The based variable
1668 type_t *make_based_pointer_type(type_t *points_to,
1669 type_qualifiers_t qualifiers, variable_t *variable)
1671 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1672 memset(type, 0, sizeof(pointer_type_t));
1674 type->kind = TYPE_POINTER;
1675 type->base.qualifiers = qualifiers;
1676 type->pointer.points_to = points_to;
1677 type->pointer.base_variable = variable;
1679 return identify_new_type(type);
1683 type_t *make_array_type(type_t *element_type, size_t size,
1684 type_qualifiers_t qualifiers)
1686 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1687 memset(type, 0, sizeof(array_type_t));
1689 type->kind = TYPE_ARRAY;
1690 type->base.qualifiers = qualifiers;
1691 type->array.element_type = element_type;
1692 type->array.size = size;
1693 type->array.size_constant = true;
1695 return identify_new_type(type);
1698 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1699 il_alignment_t *struct_alignment,
1700 bool packed, entity_t *first)
1702 il_size_t offset = *struct_offset;
1703 il_alignment_t alignment = *struct_alignment;
1704 size_t bit_offset = 0;
1707 for (member = first; member != NULL; member = member->base.next) {
1708 if (member->kind != ENTITY_COMPOUND_MEMBER)
1711 type_t *type = member->declaration.type;
1712 if (type->kind != TYPE_BITFIELD)
1715 type_t *base_type = skip_typeref(type->bitfield.base_type);
1716 il_alignment_t base_alignment = get_type_alignment(base_type);
1717 il_alignment_t alignment_mask = base_alignment-1;
1718 if (base_alignment > alignment)
1719 alignment = base_alignment;
1721 size_t bit_size = type->bitfield.bit_size;
1723 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1724 offset &= ~alignment_mask;
1725 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1727 if (bit_offset + bit_size > base_size || bit_size == 0) {
1728 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1729 offset = (offset + base_alignment-1) & ~alignment_mask;
1734 member->compound_member.offset = offset;
1735 member->compound_member.bit_offset = bit_offset;
1737 bit_offset += bit_size;
1738 offset += bit_offset / BITS_PER_BYTE;
1739 bit_offset %= BITS_PER_BYTE;
1745 *struct_offset = offset;
1746 *struct_alignment = alignment;
1752 * Finish the construction of a struct type by calculating its size, offsets,
1755 void layout_struct_type(compound_type_t *type)
1757 assert(type->compound != NULL);
1759 compound_t *compound = type->compound;
1760 if (!compound->complete)
1762 if (type->compound->layouted)
1765 il_size_t offset = 0;
1766 il_alignment_t alignment = compound->alignment;
1767 bool need_pad = false;
1769 entity_t *entry = compound->members.entities;
1770 while (entry != NULL) {
1771 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1772 entry = entry->base.next;
1776 type_t *m_type = entry->declaration.type;
1777 type_t *skipped = skip_typeref(m_type);
1778 if (! is_type_valid(skipped)) {
1779 entry = entry->base.next;
1783 if (skipped->kind == TYPE_BITFIELD) {
1784 entry = pack_bitfield_members(&offset, &alignment,
1785 compound->packed, entry);
1789 il_alignment_t m_alignment = get_type_alignment(m_type);
1790 if (m_alignment > alignment)
1791 alignment = m_alignment;
1793 if (!compound->packed) {
1794 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1796 if (new_offset > offset) {
1798 offset = new_offset;
1802 entry->compound_member.offset = offset;
1803 offset += get_type_size(m_type);
1805 entry = entry->base.next;
1808 if (!compound->packed) {
1809 il_size_t new_offset = (offset + alignment-1) & -alignment;
1810 if (new_offset > offset) {
1812 offset = new_offset;
1817 if (warning.padded) {
1818 warningf(&compound->base.source_position, "'%T' needs padding",
1821 } else if (compound->packed && warning.packed) {
1822 warningf(&compound->base.source_position,
1823 "superfluous packed attribute on '%T'", type);
1826 compound->size = offset;
1827 compound->alignment = alignment;
1828 compound->layouted = true;
1832 * Finish the construction of an union type by calculating
1833 * its size and alignment.
1835 void layout_union_type(compound_type_t *type)
1837 assert(type->compound != NULL);
1839 compound_t *compound = type->compound;
1840 if (! compound->complete)
1844 il_alignment_t alignment = compound->alignment;
1846 entity_t *entry = compound->members.entities;
1847 for (; entry != NULL; entry = entry->base.next) {
1848 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1851 type_t *m_type = entry->declaration.type;
1852 if (! is_type_valid(skip_typeref(m_type)))
1855 entry->compound_member.offset = 0;
1856 il_size_t m_size = get_type_size(m_type);
1859 il_alignment_t m_alignment = get_type_alignment(m_type);
1860 if (m_alignment > alignment)
1861 alignment = m_alignment;
1863 size = (size + alignment - 1) & -alignment;
1865 compound->size = size;
1866 compound->alignment = alignment;
1870 * Debug helper. Prints the given type to stdout.
1872 static __attribute__((unused))
1873 void dbg_type(const type_t *type)
1875 FILE *old_out = out;