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"
36 #include "driver/firm_cmdline.h"
38 /** The default calling convention. */
39 cc_kind_t default_calling_convention = CC_CDECL;
41 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 print_type_qualifiers(type_qualifiers_t qualifiers)
211 if (qualifiers & TYPE_QUALIFIER_CONST) {
212 print_string("const ");
214 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
215 print_string("volatile ");
217 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
218 print_string("restrict ");
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 print_atomic_kinds(type->akind);
270 * Prints the name of a complex type.
272 * @param type The type.
274 static void print_complex_type(const complex_type_t *type)
276 print_type_qualifiers(type->base.qualifiers);
277 print_string("_Complex");
278 print_atomic_kinds(type->akind);
282 * Prints the name of an imaginary type.
284 * @param type The type.
286 static void print_imaginary_type(const imaginary_type_t *type)
288 print_type_qualifiers(type->base.qualifiers);
289 print_string("_Imaginary ");
290 print_atomic_kinds(type->akind);
294 * Print the first part (the prefix) of a type.
296 * @param type The type to print.
298 static void print_function_type_pre(const function_type_t *type)
300 switch (type->linkage) {
301 case LINKAGE_INVALID:
306 print_string("extern \"C\" ");
310 if (!(c_mode & _CXX))
311 print_string("extern \"C++\" ");
315 print_type_qualifiers(type->base.qualifiers);
317 intern_print_type_pre(type->return_type);
319 cc_kind_t cc = type->calling_convention;
322 case CC_CDECL: print_string(" __cdecl"); break;
323 case CC_STDCALL: print_string(" __stdcall"); break;
324 case CC_FASTCALL: print_string(" __fastcall"); break;
325 case CC_THISCALL: print_string(" __thiscall"); break;
327 if (default_calling_convention != CC_CDECL) {
328 /* show the default calling convention if its not cdecl */
329 cc = default_calling_convention;
337 * Print the second part (the postfix) of a type.
339 * @param type The type to print.
341 static void print_function_type_post(const function_type_t *type,
342 const scope_t *parameters)
346 if (parameters == NULL) {
347 function_parameter_t *parameter = type->parameters;
348 for( ; parameter != NULL; parameter = parameter->next) {
354 print_type(parameter->type);
357 entity_t *parameter = parameters->entities;
358 for (; parameter != NULL; parameter = parameter->base.next) {
359 if (parameter->kind != ENTITY_PARAMETER)
367 const type_t *const type = parameter->declaration.type;
369 print_string(parameter->base.symbol->string);
371 print_type_ext(type, parameter->base.symbol, NULL);
375 if (type->variadic) {
383 if (first && !type->unspecified_parameters) {
384 print_string("void");
388 intern_print_type_post(type->return_type);
392 * Prints the prefix part of a pointer type.
394 * @param type The pointer type.
396 static void print_pointer_type_pre(const pointer_type_t *type)
398 type_t const *const points_to = type->points_to;
399 intern_print_type_pre(points_to);
400 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
402 variable_t *const variable = type->base_variable;
403 if (variable != NULL) {
404 print_string(" __based(");
405 print_string(variable->base.base.symbol->string);
409 type_qualifiers_t const qual = type->base.qualifiers;
412 print_type_qualifiers(qual);
416 * Prints the postfix part of a pointer type.
418 * @param type The pointer type.
420 static void print_pointer_type_post(const pointer_type_t *type)
422 type_t const *const points_to = type->points_to;
423 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
425 intern_print_type_post(points_to);
429 * Prints the prefix part of a reference type.
431 * @param type The reference type.
433 static void print_reference_type_pre(const reference_type_t *type)
435 type_t const *const refers_to = type->refers_to;
436 intern_print_type_pre(refers_to);
437 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
443 * Prints the postfix part of a reference type.
445 * @param type The reference type.
447 static void print_reference_type_post(const reference_type_t *type)
449 type_t const *const refers_to = type->refers_to;
450 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
452 intern_print_type_post(refers_to);
456 * Prints the prefix part of an array type.
458 * @param type The array type.
460 static void print_array_type_pre(const array_type_t *type)
462 intern_print_type_pre(type->element_type);
466 * Prints the postfix part of an array type.
468 * @param type The array type.
470 static void print_array_type_post(const array_type_t *type)
473 if (type->is_static) {
474 print_string("static ");
476 print_type_qualifiers(type->base.qualifiers);
477 if (type->size_expression != NULL
478 && (print_implicit_array_size || !type->has_implicit_size)) {
479 print_expression(type->size_expression);
482 intern_print_type_post(type->element_type);
486 * Prints the postfix part of a bitfield type.
488 * @param type The array type.
490 static void print_bitfield_type_post(const bitfield_type_t *type)
493 print_expression(type->size_expression);
494 intern_print_type_post(type->base_type);
498 * Prints an enum definition.
500 * @param declaration The enum's type declaration.
502 void print_enum_definition(const enum_t *enume)
508 entity_t *entry = enume->base.next;
509 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
510 entry = entry->base.next) {
513 print_string(entry->base.symbol->string);
514 if (entry->enum_value.value != NULL) {
517 /* skip the implicit cast */
518 expression_t *expression = entry->enum_value.value;
519 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
520 expression = expression->unary.value;
522 print_expression(expression);
533 * Prints an enum type.
535 * @param type The enum type.
537 static void print_type_enum(const enum_type_t *type)
539 print_type_qualifiers(type->base.qualifiers);
540 print_string("enum ");
542 enum_t *enume = type->enume;
543 symbol_t *symbol = enume->base.symbol;
544 if (symbol != NULL) {
545 print_string(symbol->string);
547 print_enum_definition(enume);
552 * Print the compound part of a compound type.
554 void print_compound_definition(const compound_t *compound)
559 entity_t *entity = compound->members.entities;
560 for( ; entity != NULL; entity = entity->base.next) {
561 if (entity->kind != ENTITY_COMPOUND_MEMBER)
565 print_entity(entity);
572 if (compound->modifiers & DM_TRANSPARENT_UNION) {
573 print_string("__attribute__((__transparent_union__))");
578 * Prints a compound type.
580 * @param type The compound type.
582 static void print_compound_type(const compound_type_t *type)
584 print_type_qualifiers(type->base.qualifiers);
586 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
587 print_string("struct ");
589 assert(type->base.kind == TYPE_COMPOUND_UNION);
590 print_string("union ");
593 compound_t *compound = type->compound;
594 symbol_t *symbol = compound->base.symbol;
595 if (symbol != NULL) {
596 print_string(symbol->string);
598 print_compound_definition(compound);
603 * Prints the prefix part of a typedef type.
605 * @param type The typedef type.
607 static void print_typedef_type_pre(const typedef_type_t *const type)
609 print_type_qualifiers(type->base.qualifiers);
610 print_string(type->typedefe->base.symbol->string);
614 * Prints the prefix part of a typeof type.
616 * @param type The typeof type.
618 static void print_typeof_type_pre(const typeof_type_t *const type)
620 print_string("typeof(");
621 if (type->expression != NULL) {
622 print_expression(type->expression);
624 print_type(type->typeof_type);
630 * Prints the prefix part of a type.
632 * @param type The type.
634 static void intern_print_type_pre(const type_t *const type)
638 print_string("<error>");
641 print_string("<invalid>");
644 print_type_enum(&type->enumt);
647 print_atomic_type(&type->atomic);
650 print_complex_type(&type->complex);
653 print_imaginary_type(&type->imaginary);
655 case TYPE_COMPOUND_STRUCT:
656 case TYPE_COMPOUND_UNION:
657 print_compound_type(&type->compound);
660 print_string(type->builtin.symbol->string);
663 print_function_type_pre(&type->function);
666 print_pointer_type_pre(&type->pointer);
669 print_reference_type_pre(&type->reference);
672 intern_print_type_pre(type->bitfield.base_type);
675 print_array_type_pre(&type->array);
678 print_typedef_type_pre(&type->typedeft);
681 print_typeof_type_pre(&type->typeoft);
684 print_string("unknown");
688 * Prints the postfix part of a type.
690 * @param type The type.
692 static void intern_print_type_post(const type_t *const type)
696 print_function_type_post(&type->function, NULL);
699 print_pointer_type_post(&type->pointer);
702 print_reference_type_post(&type->reference);
705 print_array_type_post(&type->array);
708 print_bitfield_type_post(&type->bitfield);
716 case TYPE_COMPOUND_STRUCT:
717 case TYPE_COMPOUND_UNION:
728 * @param type The type.
730 void print_type(const type_t *const type)
732 print_type_ext(type, NULL, NULL);
735 void print_type_ext(const type_t *const type, const symbol_t *symbol,
736 const scope_t *parameters)
739 print_string("nil type");
743 intern_print_type_pre(type);
744 if (symbol != NULL) {
746 print_string(symbol->string);
748 if (type->kind == TYPE_FUNCTION) {
749 print_function_type_post(&type->function, parameters);
751 intern_print_type_post(type);
756 * Return the size of a type AST node.
758 * @param type The type.
760 static size_t get_type_struct_size(const type_t *type)
763 case TYPE_ATOMIC: return sizeof(atomic_type_t);
764 case TYPE_COMPLEX: return sizeof(complex_type_t);
765 case TYPE_IMAGINARY: return sizeof(imaginary_type_t);
766 case TYPE_COMPOUND_STRUCT:
767 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
768 case TYPE_ENUM: return sizeof(enum_type_t);
769 case TYPE_FUNCTION: return sizeof(function_type_t);
770 case TYPE_POINTER: return sizeof(pointer_type_t);
771 case TYPE_REFERENCE: return sizeof(reference_type_t);
772 case TYPE_ARRAY: return sizeof(array_type_t);
773 case TYPE_BUILTIN: return sizeof(builtin_type_t);
774 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
775 case TYPE_TYPEOF: return sizeof(typeof_type_t);
776 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
777 case TYPE_ERROR: panic("error type found");
778 case TYPE_INVALID: panic("invalid type found");
780 panic("unknown type found");
786 * @param type The type to copy.
787 * @return A copy of the type.
789 * @note This does not produce a deep copy!
791 type_t *duplicate_type(const type_t *type)
793 size_t size = get_type_struct_size(type);
795 type_t *copy = obstack_alloc(type_obst, size);
796 memcpy(copy, type, size);
797 copy->base.firm_type = NULL;
803 * Returns the unqualified type of a given type.
805 * @param type The type.
806 * @returns The unqualified type.
808 type_t *get_unqualified_type(type_t *type)
810 assert(!is_typeref(type));
812 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
815 type_t *unqualified_type = duplicate_type(type);
816 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
818 return identify_new_type(unqualified_type);
821 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
823 type_t *type = skip_typeref(orig_type);
826 if (is_type_array(type)) {
827 /* For array types the element type has to be adjusted */
828 type_t *element_type = type->array.element_type;
829 type_t *qual_element_type = get_qualified_type(element_type, qual);
831 if (qual_element_type == element_type)
834 copy = duplicate_type(type);
835 copy->array.element_type = qual_element_type;
836 } else if (is_type_valid(type)) {
837 if ((type->base.qualifiers & qual) == qual)
840 copy = duplicate_type(type);
841 copy->base.qualifiers |= qual;
846 return identify_new_type(copy);
850 * Check if a type is valid.
852 * @param type The type to check.
853 * @return true if type represents a valid type.
855 bool type_valid(const type_t *type)
857 return type->kind != TYPE_INVALID;
860 static bool test_atomic_type_flag(atomic_type_kind_t kind,
861 atomic_type_flag_t flag)
863 assert(kind <= ATOMIC_TYPE_LAST);
864 return (atomic_type_properties[kind].flags & flag) != 0;
868 * Returns true if the given type is an integer type.
870 * @param type The type to check.
871 * @return True if type is an integer type.
873 bool is_type_integer(const type_t *type)
875 assert(!is_typeref(type));
877 if (type->kind == TYPE_ENUM)
879 if (type->kind == TYPE_BITFIELD)
882 if (type->kind != TYPE_ATOMIC)
885 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
889 * Returns true if the given type is an enum type.
891 * @param type The type to check.
892 * @return True if type is an enum type.
894 bool is_type_enum(const type_t *type)
896 assert(!is_typeref(type));
897 return type->kind == TYPE_ENUM;
901 * Returns true if the given type is an floating point type.
903 * @param type The type to check.
904 * @return True if type is a floating point type.
906 bool is_type_float(const type_t *type)
908 assert(!is_typeref(type));
910 if (type->kind != TYPE_ATOMIC)
913 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
917 * Returns true if the given type is an complex type.
919 * @param type The type to check.
920 * @return True if type is a complex type.
922 bool is_type_complex(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_COMPLEX);
933 * Returns true if the given type is a signed type.
935 * @param type The type to check.
936 * @return True if type is a signed type.
938 bool is_type_signed(const type_t *type)
940 assert(!is_typeref(type));
942 /* enum types are int for now */
943 if (type->kind == TYPE_ENUM)
945 if (type->kind == TYPE_BITFIELD)
946 return is_type_signed(type->bitfield.base_type);
948 if (type->kind != TYPE_ATOMIC)
951 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
955 * Returns true if the given type represents an arithmetic type.
957 * @param type The type to check.
958 * @return True if type represents an arithmetic type.
960 bool is_type_arithmetic(const type_t *type)
962 assert(!is_typeref(type));
969 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
971 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
973 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
980 * Returns true if the given type is an integer or float type.
982 * @param type The type to check.
983 * @return True if type is an integer or float type.
985 bool is_type_real(const type_t *type)
988 return is_type_integer(type) || is_type_float(type);
992 * Returns true if the given type represents a scalar type.
994 * @param type The type to check.
995 * @return True if type represents a scalar type.
997 bool is_type_scalar(const type_t *type)
999 assert(!is_typeref(type));
1001 switch (type->kind) {
1002 case TYPE_POINTER: return true;
1003 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1007 return is_type_arithmetic(type);
1011 * Check if a given type is incomplete.
1013 * @param type The type to check.
1014 * @return True if the given type is incomplete (ie. just forward).
1016 bool is_type_incomplete(const type_t *type)
1018 assert(!is_typeref(type));
1020 switch(type->kind) {
1021 case TYPE_COMPOUND_STRUCT:
1022 case TYPE_COMPOUND_UNION: {
1023 const compound_type_t *compound_type = &type->compound;
1024 return !compound_type->compound->complete;
1030 return type->array.size_expression == NULL
1031 && !type->array.size_constant;
1034 return type->atomic.akind == ATOMIC_TYPE_VOID;
1037 return type->complex.akind == ATOMIC_TYPE_VOID;
1039 case TYPE_IMAGINARY:
1040 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1045 case TYPE_REFERENCE:
1052 panic("is_type_incomplete called without typerefs skipped");
1057 panic("invalid type found");
1060 bool is_type_object(const type_t *type)
1062 return !is_type_function(type) && !is_type_incomplete(type);
1065 bool is_builtin_va_list(type_t *type)
1067 type_t *tp = skip_typeref(type);
1069 return tp->kind == type_valist->kind &&
1070 tp->builtin.symbol == type_valist->builtin.symbol;
1074 * Check if two function types are compatible.
1076 static bool function_types_compatible(const function_type_t *func1,
1077 const function_type_t *func2)
1079 const type_t* const ret1 = skip_typeref(func1->return_type);
1080 const type_t* const ret2 = skip_typeref(func2->return_type);
1081 if (!types_compatible(ret1, ret2))
1084 if (func1->linkage != func2->linkage)
1087 cc_kind_t cc1 = func1->calling_convention;
1088 if (cc1 == CC_DEFAULT)
1089 cc1 = default_calling_convention;
1090 cc_kind_t cc2 = func2->calling_convention;
1091 if (cc2 == CC_DEFAULT)
1092 cc2 = default_calling_convention;
1097 if (func1->variadic != func2->variadic)
1100 /* can parameters be compared? */
1101 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1102 || (func2->unspecified_parameters && !func2->kr_style_parameters))
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: {
1197 /* TODO: not implemented */
1201 /* not sure if this makes sense or is even needed, implement it if you
1202 * really need it! */
1203 panic("type compatibility check for bitfield type");
1206 /* Hmm, the error type should be compatible to all other types */
1209 panic("invalid type found in compatible types");
1212 panic("typerefs not skipped in compatible types?!?");
1215 /* TODO: incomplete */
1220 * Skip all typerefs and return the underlying type.
1222 type_t *skip_typeref(type_t *type)
1224 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1227 switch (type->kind) {
1230 case TYPE_TYPEDEF: {
1231 qualifiers |= type->base.qualifiers;
1233 const typedef_type_t *typedef_type = &type->typedeft;
1234 if (typedef_type->resolved_type != NULL) {
1235 type = typedef_type->resolved_type;
1238 type = typedef_type->typedefe->type;
1242 qualifiers |= type->base.qualifiers;
1243 type = type->typeoft.typeof_type;
1251 if (qualifiers != TYPE_QUALIFIER_NONE) {
1252 type_t *const copy = duplicate_type(type);
1254 /* for const with typedefed array type the element type has to be
1256 if (is_type_array(copy)) {
1257 type_t *element_type = copy->array.element_type;
1258 element_type = duplicate_type(element_type);
1259 element_type->base.qualifiers |= qualifiers;
1260 copy->array.element_type = element_type;
1262 copy->base.qualifiers |= qualifiers;
1265 type = identify_new_type(copy);
1271 unsigned get_type_size(type_t *type)
1273 switch (type->kind) {
1279 return get_atomic_type_size(type->atomic.akind);
1281 return get_atomic_type_size(type->complex.akind) * 2;
1282 case TYPE_IMAGINARY:
1283 return get_atomic_type_size(type->imaginary.akind);
1284 case TYPE_COMPOUND_UNION:
1285 layout_union_type(&type->compound);
1286 return type->compound.compound->size;
1287 case TYPE_COMPOUND_STRUCT:
1288 layout_struct_type(&type->compound);
1289 return type->compound.compound->size;
1291 return get_atomic_type_size(type->enumt.akind);
1293 return 0; /* non-const (but "address-const") */
1294 case TYPE_REFERENCE:
1296 /* TODO: make configurable by backend */
1299 /* TODO: correct if element_type is aligned? */
1300 il_size_t element_size = get_type_size(type->array.element_type);
1301 return type->array.size * element_size;
1306 return get_type_size(type->builtin.real_type);
1308 return get_type_size(type->typedeft.typedefe->type);
1310 if (type->typeoft.typeof_type) {
1311 return get_type_size(type->typeoft.typeof_type);
1313 return get_type_size(type->typeoft.expression->base.type);
1316 panic("invalid type in get_type_size");
1319 unsigned get_type_alignment(type_t *type)
1321 switch (type->kind) {
1327 return get_atomic_type_alignment(type->atomic.akind);
1329 return get_atomic_type_alignment(type->complex.akind);
1330 case TYPE_IMAGINARY:
1331 return get_atomic_type_alignment(type->imaginary.akind);
1332 case TYPE_COMPOUND_UNION:
1333 layout_union_type(&type->compound);
1334 return type->compound.compound->alignment;
1335 case TYPE_COMPOUND_STRUCT:
1336 layout_struct_type(&type->compound);
1337 return type->compound.compound->alignment;
1339 return get_atomic_type_alignment(type->enumt.akind);
1341 /* what is correct here? */
1343 case TYPE_REFERENCE:
1345 /* TODO: make configurable by backend */
1348 return get_type_alignment(type->array.element_type);
1352 return get_type_alignment(type->builtin.real_type);
1353 case TYPE_TYPEDEF: {
1354 il_alignment_t alignment
1355 = get_type_alignment(type->typedeft.typedefe->type);
1356 if (type->typedeft.typedefe->alignment > alignment)
1357 alignment = type->typedeft.typedefe->alignment;
1362 if (type->typeoft.typeof_type) {
1363 return get_type_alignment(type->typeoft.typeof_type);
1365 return get_type_alignment(type->typeoft.expression->base.type);
1368 panic("invalid type in get_type_alignment");
1371 decl_modifiers_t get_type_modifiers(const type_t *type)
1373 switch(type->kind) {
1377 case TYPE_COMPOUND_STRUCT:
1378 case TYPE_COMPOUND_UNION:
1379 return type->compound.compound->modifiers;
1381 return type->function.modifiers;
1385 case TYPE_IMAGINARY:
1386 case TYPE_REFERENCE:
1392 return get_type_modifiers(type->builtin.real_type);
1393 case TYPE_TYPEDEF: {
1394 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1395 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1399 if (type->typeoft.typeof_type) {
1400 return get_type_modifiers(type->typeoft.typeof_type);
1402 return get_type_modifiers(type->typeoft.expression->base.type);
1405 panic("invalid type found in get_type_modifiers");
1408 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1410 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1413 switch (type->base.kind) {
1415 return TYPE_QUALIFIER_NONE;
1417 qualifiers |= type->base.qualifiers;
1418 const typedef_type_t *typedef_type = &type->typedeft;
1419 if (typedef_type->resolved_type != NULL)
1420 type = typedef_type->resolved_type;
1422 type = typedef_type->typedefe->type;
1425 type = type->typeoft.typeof_type;
1428 if (skip_array_type) {
1429 type = type->array.element_type;
1438 return type->base.qualifiers | qualifiers;
1441 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1443 assert(kind <= ATOMIC_TYPE_LAST);
1444 return atomic_type_properties[kind].size;
1447 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1449 assert(kind <= ATOMIC_TYPE_LAST);
1450 return atomic_type_properties[kind].alignment;
1453 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1455 assert(kind <= ATOMIC_TYPE_LAST);
1456 return atomic_type_properties[kind].flags;
1459 atomic_type_kind_t get_intptr_kind(void)
1461 if (machine_size <= 32)
1462 return ATOMIC_TYPE_INT;
1463 else if (machine_size <= 64)
1464 return ATOMIC_TYPE_LONG;
1466 return ATOMIC_TYPE_LONGLONG;
1469 atomic_type_kind_t get_uintptr_kind(void)
1471 if (machine_size <= 32)
1472 return ATOMIC_TYPE_UINT;
1473 else if (machine_size <= 64)
1474 return ATOMIC_TYPE_ULONG;
1476 return ATOMIC_TYPE_ULONGLONG;
1480 * Find the atomic type kind representing a given size (signed).
1482 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1484 static atomic_type_kind_t kinds[32];
1487 atomic_type_kind_t kind = kinds[size];
1488 if (kind == ATOMIC_TYPE_INVALID) {
1489 static const atomic_type_kind_t possible_kinds[] = {
1494 ATOMIC_TYPE_LONGLONG
1496 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1497 if (get_atomic_type_size(possible_kinds[i]) == size) {
1498 kind = possible_kinds[i];
1508 * Find the atomic type kind representing a given size (signed).
1510 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1512 static atomic_type_kind_t kinds[32];
1515 atomic_type_kind_t kind = kinds[size];
1516 if (kind == ATOMIC_TYPE_INVALID) {
1517 static const atomic_type_kind_t possible_kinds[] = {
1522 ATOMIC_TYPE_ULONGLONG
1524 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1525 if (get_atomic_type_size(possible_kinds[i]) == size) {
1526 kind = possible_kinds[i];
1536 * Hash the given type and return the "singleton" version
1539 type_t *identify_new_type(type_t *type)
1541 type_t *result = typehash_insert(type);
1542 if (result != type) {
1543 obstack_free(type_obst, type);
1549 * Creates a new atomic type.
1551 * @param akind The kind of the atomic type.
1552 * @param qualifiers Type qualifiers for the new type.
1554 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1556 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1557 memset(type, 0, sizeof(atomic_type_t));
1559 type->kind = TYPE_ATOMIC;
1560 type->base.qualifiers = qualifiers;
1561 type->atomic.akind = akind;
1563 return identify_new_type(type);
1567 * Creates a new complex type.
1569 * @param akind The kind of the atomic type.
1570 * @param qualifiers Type qualifiers for the new type.
1572 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1574 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1575 memset(type, 0, sizeof(complex_type_t));
1577 type->kind = TYPE_COMPLEX;
1578 type->base.qualifiers = qualifiers;
1579 type->complex.akind = akind;
1581 return identify_new_type(type);
1585 * Creates a new imaginary type.
1587 * @param akind The kind of the atomic type.
1588 * @param qualifiers Type qualifiers for the new type.
1590 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1592 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1593 memset(type, 0, sizeof(imaginary_type_t));
1595 type->kind = TYPE_IMAGINARY;
1596 type->base.qualifiers = qualifiers;
1597 type->imaginary.akind = akind;
1599 return identify_new_type(type);
1603 * Creates a new pointer type.
1605 * @param points_to The points-to type for the new type.
1606 * @param qualifiers Type qualifiers for the new type.
1608 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1610 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1611 memset(type, 0, sizeof(pointer_type_t));
1613 type->kind = TYPE_POINTER;
1614 type->base.qualifiers = qualifiers;
1615 type->pointer.points_to = points_to;
1616 type->pointer.base_variable = NULL;
1618 return identify_new_type(type);
1622 * Creates a new reference type.
1624 * @param refers_to The referred-to type for the new type.
1626 type_t *make_reference_type(type_t *refers_to)
1628 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1629 memset(type, 0, sizeof(reference_type_t));
1631 type->kind = TYPE_REFERENCE;
1632 type->base.qualifiers = 0;
1633 type->reference.refers_to = refers_to;
1635 return identify_new_type(type);
1639 * Creates a new based pointer type.
1641 * @param points_to The points-to type for the new type.
1642 * @param qualifiers Type qualifiers for the new type.
1643 * @param variable The based variable
1645 type_t *make_based_pointer_type(type_t *points_to,
1646 type_qualifiers_t qualifiers, variable_t *variable)
1648 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1649 memset(type, 0, sizeof(pointer_type_t));
1651 type->kind = TYPE_POINTER;
1652 type->base.qualifiers = qualifiers;
1653 type->pointer.points_to = points_to;
1654 type->pointer.base_variable = variable;
1656 return identify_new_type(type);
1660 type_t *make_array_type(type_t *element_type, size_t size,
1661 type_qualifiers_t qualifiers)
1663 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1664 memset(type, 0, sizeof(array_type_t));
1666 type->kind = TYPE_ARRAY;
1667 type->base.qualifiers = qualifiers;
1668 type->array.element_type = element_type;
1669 type->array.size = size;
1670 type->array.size_constant = true;
1672 return identify_new_type(type);
1675 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1676 il_alignment_t *struct_alignment,
1677 bool packed, entity_t *first)
1679 il_size_t offset = *struct_offset;
1680 il_alignment_t alignment = *struct_alignment;
1681 size_t bit_offset = 0;
1684 for (member = first; member != NULL; member = member->base.next) {
1685 if (member->kind != ENTITY_COMPOUND_MEMBER)
1688 type_t *type = member->declaration.type;
1689 if (type->kind != TYPE_BITFIELD)
1692 type_t *base_type = skip_typeref(type->bitfield.base_type);
1693 il_alignment_t base_alignment = get_type_alignment(base_type);
1694 il_alignment_t alignment_mask = base_alignment-1;
1695 if (base_alignment > alignment)
1696 alignment = base_alignment;
1698 size_t bit_size = type->bitfield.bit_size;
1700 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1701 offset &= ~alignment_mask;
1702 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1704 if (bit_offset + bit_size > base_size || bit_size == 0) {
1705 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1706 offset = (offset + base_alignment-1) & ~alignment_mask;
1711 member->compound_member.offset = offset;
1712 member->compound_member.bit_offset = bit_offset;
1714 bit_offset += bit_size;
1715 offset += bit_offset / BITS_PER_BYTE;
1716 bit_offset %= BITS_PER_BYTE;
1722 *struct_offset = offset;
1723 *struct_alignment = alignment;
1729 * Finish the construction of a struct type by calculating its size, offsets,
1732 void layout_struct_type(compound_type_t *type)
1734 assert(type->compound != NULL);
1736 compound_t *compound = type->compound;
1737 if (!compound->complete)
1739 if (type->compound->layouted)
1742 il_size_t offset = 0;
1743 il_alignment_t alignment = compound->alignment;
1744 bool need_pad = false;
1746 entity_t *entry = compound->members.entities;
1747 while (entry != NULL) {
1748 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1749 entry = entry->base.next;
1753 type_t *m_type = entry->declaration.type;
1754 type_t *skipped = skip_typeref(m_type);
1755 if (! is_type_valid(skipped)) {
1756 entry = entry->base.next;
1760 if (skipped->kind == TYPE_BITFIELD) {
1761 entry = pack_bitfield_members(&offset, &alignment,
1762 compound->packed, entry);
1766 il_alignment_t m_alignment = get_type_alignment(m_type);
1767 if (m_alignment > alignment)
1768 alignment = m_alignment;
1770 if (!compound->packed) {
1771 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1773 if (new_offset > offset) {
1775 offset = new_offset;
1779 entry->compound_member.offset = offset;
1780 offset += get_type_size(m_type);
1782 entry = entry->base.next;
1785 if (!compound->packed) {
1786 il_size_t new_offset = (offset + alignment-1) & -alignment;
1787 if (new_offset > offset) {
1789 offset = new_offset;
1794 if (warning.padded) {
1795 warningf(&compound->base.source_position, "'%T' needs padding",
1798 } else if (compound->packed && warning.packed) {
1799 warningf(&compound->base.source_position,
1800 "superfluous packed attribute on '%T'", type);
1803 compound->size = offset;
1804 compound->alignment = alignment;
1805 compound->layouted = true;
1809 * Finish the construction of an union type by calculating
1810 * its size and alignment.
1812 void layout_union_type(compound_type_t *type)
1814 assert(type->compound != NULL);
1816 compound_t *compound = type->compound;
1817 if (! compound->complete)
1821 il_alignment_t alignment = compound->alignment;
1823 entity_t *entry = compound->members.entities;
1824 for (; entry != NULL; entry = entry->base.next) {
1825 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1828 type_t *m_type = entry->declaration.type;
1829 if (! is_type_valid(skip_typeref(m_type)))
1832 entry->compound_member.offset = 0;
1833 il_size_t m_size = get_type_size(m_type);
1836 il_alignment_t m_alignment = get_type_alignment(m_type);
1837 if (m_alignment > alignment)
1838 alignment = m_alignment;
1840 size = (size + alignment - 1) & -alignment;
1842 compound->size = size;
1843 compound->alignment = alignment;
1847 * Debug helper. Prints the given type to stdout.
1849 static __attribute__((unused))
1850 void dbg_type(const type_t *type)
1852 print_to_file(stderr);