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"
37 /** The default calling convention. */
38 cc_kind_t default_calling_convention = CC_CDECL;
40 static struct obstack type_obst;
41 static bool print_implicit_array_size = false;
43 static void intern_print_type_pre(const type_t *type);
44 static void intern_print_type_post(const type_t *type);
46 typedef struct atomic_type_properties_t atomic_type_properties_t;
47 struct atomic_type_properties_t {
48 unsigned size; /**< type size in bytes */
49 unsigned alignment; /**< type alignment in bytes */
50 unsigned flags; /**< type flags from atomic_type_flag_t */
54 * Returns the size of a type node.
56 * @param kind the type kind
58 static size_t get_type_struct_size(type_kind_t kind)
60 static const size_t sizes[] = {
61 [TYPE_ATOMIC] = sizeof(atomic_type_t),
62 [TYPE_COMPLEX] = sizeof(complex_type_t),
63 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
64 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
65 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
66 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
67 [TYPE_ENUM] = sizeof(enum_type_t),
68 [TYPE_FUNCTION] = sizeof(function_type_t),
69 [TYPE_POINTER] = sizeof(pointer_type_t),
70 [TYPE_REFERENCE] = sizeof(reference_type_t),
71 [TYPE_ARRAY] = sizeof(array_type_t),
72 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
73 [TYPE_TYPEOF] = sizeof(typeof_type_t),
75 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
76 assert(kind <= TYPE_TYPEOF);
77 assert(sizes[kind] != 0);
81 type_t *allocate_type_zero(type_kind_t kind)
83 size_t const size = get_type_struct_size(kind);
84 type_t *const res = obstack_alloc(&type_obst, size);
86 res->base.kind = kind;
92 * Properties of atomic types.
94 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
95 //ATOMIC_TYPE_INVALID = 0,
96 [ATOMIC_TYPE_VOID] = {
99 .flags = ATOMIC_TYPE_FLAG_NONE
101 [ATOMIC_TYPE_WCHAR_T] = {
102 .size = (unsigned)-1,
103 .alignment = (unsigned)-1,
104 /* signed flag will be set when known */
105 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
107 [ATOMIC_TYPE_CHAR] = {
110 /* signed flag will be set when known */
111 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
113 [ATOMIC_TYPE_SCHAR] = {
116 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
117 | ATOMIC_TYPE_FLAG_SIGNED,
119 [ATOMIC_TYPE_UCHAR] = {
122 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
124 [ATOMIC_TYPE_SHORT] = {
127 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
128 | ATOMIC_TYPE_FLAG_SIGNED
130 [ATOMIC_TYPE_USHORT] = {
133 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
135 [ATOMIC_TYPE_INT] = {
136 .size = (unsigned) -1,
137 .alignment = (unsigned) -1,
138 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
139 | ATOMIC_TYPE_FLAG_SIGNED,
141 [ATOMIC_TYPE_UINT] = {
142 .size = (unsigned) -1,
143 .alignment = (unsigned) -1,
144 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
146 [ATOMIC_TYPE_LONG] = {
147 .size = (unsigned) -1,
148 .alignment = (unsigned) -1,
149 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
150 | ATOMIC_TYPE_FLAG_SIGNED,
152 [ATOMIC_TYPE_ULONG] = {
153 .size = (unsigned) -1,
154 .alignment = (unsigned) -1,
155 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
157 [ATOMIC_TYPE_LONGLONG] = {
158 .size = (unsigned) -1,
159 .alignment = (unsigned) -1,
160 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
161 | ATOMIC_TYPE_FLAG_SIGNED,
163 [ATOMIC_TYPE_ULONGLONG] = {
164 .size = (unsigned) -1,
165 .alignment = (unsigned) -1,
166 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
168 [ATOMIC_TYPE_BOOL] = {
169 .size = (unsigned) -1,
170 .alignment = (unsigned) -1,
171 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
173 [ATOMIC_TYPE_FLOAT] = {
175 .alignment = (unsigned) -1,
176 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
177 | ATOMIC_TYPE_FLAG_SIGNED,
179 [ATOMIC_TYPE_DOUBLE] = {
181 .alignment = (unsigned) -1,
182 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
183 | ATOMIC_TYPE_FLAG_SIGNED,
185 [ATOMIC_TYPE_LONG_DOUBLE] = {
186 .size = (unsigned) -1, /* will be filled in later */
187 .alignment = (unsigned) -1,
188 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
189 | ATOMIC_TYPE_FLAG_SIGNED,
191 /* complex and imaginary types are set in init_types */
194 static inline bool is_po2(unsigned x)
196 return (x & (x-1)) == 0;
199 void init_types(void)
201 obstack_init(&type_obst);
203 atomic_type_properties_t *props = atomic_type_properties;
205 if (char_is_signed) {
206 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
209 unsigned int_size = machine_size < 32 ? 2 : 4;
210 /* long is always 32bit on windows */
211 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
212 unsigned llong_size = machine_size < 32 ? 4 : 8;
214 props[ATOMIC_TYPE_INT].size = int_size;
215 props[ATOMIC_TYPE_INT].alignment = int_size;
216 props[ATOMIC_TYPE_UINT].size = int_size;
217 props[ATOMIC_TYPE_UINT].alignment = int_size;
218 props[ATOMIC_TYPE_LONG].size = long_size;
219 props[ATOMIC_TYPE_LONG].alignment = long_size;
220 props[ATOMIC_TYPE_ULONG].size = long_size;
221 props[ATOMIC_TYPE_ULONG].alignment = long_size;
222 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
223 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
224 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
225 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
227 /* TODO: backend specific, need a way to query the backend for this.
228 * The following are good settings for x86 */
229 if (machine_size <= 32) {
230 props[ATOMIC_TYPE_FLOAT].alignment = 4;
231 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
232 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
233 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
234 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
236 props[ATOMIC_TYPE_FLOAT].alignment = 4;
237 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
238 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
239 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
240 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
243 if (long_double_size > 0) {
244 props[ATOMIC_TYPE_LONG_DOUBLE].size = long_double_size;
245 if (is_po2(long_double_size)) {
246 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = long_double_size;
249 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
252 /* TODO: make this configurable for platforms which do not use byte sized
254 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
256 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
259 void exit_types(void)
261 obstack_free(&type_obst, NULL);
264 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
266 size_t sep = q & QUAL_SEP_START ? 0 : 1;
267 if (qualifiers & TYPE_QUALIFIER_CONST) {
268 print_string(" const" + sep);
271 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
272 print_string(" volatile" + sep);
275 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
276 print_string(" restrict" + sep);
279 if (sep == 0 && q & QUAL_SEP_END)
283 const char *get_atomic_kind_name(atomic_type_kind_t kind)
286 case ATOMIC_TYPE_INVALID: break;
287 case ATOMIC_TYPE_VOID: return "void";
288 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
289 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
290 case ATOMIC_TYPE_CHAR: return "char";
291 case ATOMIC_TYPE_SCHAR: return "signed char";
292 case ATOMIC_TYPE_UCHAR: return "unsigned char";
293 case ATOMIC_TYPE_INT: return "int";
294 case ATOMIC_TYPE_UINT: return "unsigned int";
295 case ATOMIC_TYPE_SHORT: return "short";
296 case ATOMIC_TYPE_USHORT: return "unsigned short";
297 case ATOMIC_TYPE_LONG: return "long";
298 case ATOMIC_TYPE_ULONG: return "unsigned long";
299 case ATOMIC_TYPE_LONGLONG: return "long long";
300 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
301 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
302 case ATOMIC_TYPE_FLOAT: return "float";
303 case ATOMIC_TYPE_DOUBLE: return "double";
305 return "INVALIDATOMIC";
309 * Prints the name of an atomic type kinds.
311 * @param kind The type kind.
313 static void print_atomic_kinds(atomic_type_kind_t kind)
315 const char *s = get_atomic_kind_name(kind);
320 * Prints the name of an atomic type.
322 * @param type The type.
324 static void print_atomic_type(const atomic_type_t *type)
326 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
327 print_atomic_kinds(type->akind);
331 * Prints the name of a complex type.
333 * @param type The type.
335 static void print_complex_type(const complex_type_t *type)
337 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
338 print_string("_Complex");
339 print_atomic_kinds(type->akind);
343 * Prints the name of an imaginary type.
345 * @param type The type.
347 static void print_imaginary_type(const imaginary_type_t *type)
349 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
350 print_string("_Imaginary ");
351 print_atomic_kinds(type->akind);
355 * Print the first part (the prefix) of a type.
357 * @param type The type to print.
359 static void print_function_type_pre(const function_type_t *type)
361 switch (type->linkage) {
362 case LINKAGE_INVALID:
367 print_string("extern \"C\" ");
371 if (!(c_mode & _CXX))
372 print_string("extern \"C++\" ");
376 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
378 intern_print_type_pre(type->return_type);
380 cc_kind_t cc = type->calling_convention;
383 case CC_CDECL: print_string(" __cdecl"); break;
384 case CC_STDCALL: print_string(" __stdcall"); break;
385 case CC_FASTCALL: print_string(" __fastcall"); break;
386 case CC_THISCALL: print_string(" __thiscall"); break;
388 if (default_calling_convention != CC_CDECL) {
389 /* show the default calling convention if its not cdecl */
390 cc = default_calling_convention;
398 * Print the second part (the postfix) of a type.
400 * @param type The type to print.
402 static void print_function_type_post(const function_type_t *type,
403 const scope_t *parameters)
407 if (parameters == NULL) {
408 function_parameter_t *parameter = type->parameters;
409 for( ; parameter != NULL; parameter = parameter->next) {
415 print_type(parameter->type);
418 entity_t *parameter = parameters->entities;
419 for (; parameter != NULL; parameter = parameter->base.next) {
420 if (parameter->kind != ENTITY_PARAMETER)
428 const type_t *const param_type = parameter->declaration.type;
429 if (param_type == NULL) {
430 print_string(parameter->base.symbol->string);
432 print_type_ext(param_type, parameter->base.symbol, NULL);
436 if (type->variadic) {
444 if (first && !type->unspecified_parameters) {
445 print_string("void");
449 intern_print_type_post(type->return_type);
453 * Prints the prefix part of a pointer type.
455 * @param type The pointer type.
457 static void print_pointer_type_pre(const pointer_type_t *type)
459 type_t const *const points_to = type->points_to;
460 intern_print_type_pre(points_to);
461 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
463 variable_t *const variable = type->base_variable;
464 if (variable != NULL) {
465 print_string(" __based(");
466 print_string(variable->base.base.symbol->string);
470 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
474 * Prints the postfix part of a pointer type.
476 * @param type The pointer type.
478 static void print_pointer_type_post(const pointer_type_t *type)
480 type_t const *const points_to = type->points_to;
481 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
483 intern_print_type_post(points_to);
487 * Prints the prefix part of a reference type.
489 * @param type The reference type.
491 static void print_reference_type_pre(const reference_type_t *type)
493 type_t const *const refers_to = type->refers_to;
494 intern_print_type_pre(refers_to);
495 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
501 * Prints the postfix part of a reference type.
503 * @param type The reference type.
505 static void print_reference_type_post(const reference_type_t *type)
507 type_t const *const refers_to = type->refers_to;
508 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
510 intern_print_type_post(refers_to);
514 * Prints the prefix part of an array type.
516 * @param type The array type.
518 static void print_array_type_pre(const array_type_t *type)
520 intern_print_type_pre(type->element_type);
524 * Prints the postfix part of an array type.
526 * @param type The array type.
528 static void print_array_type_post(const array_type_t *type)
531 if (type->is_static) {
532 print_string("static ");
534 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
535 if (type->size_expression != NULL
536 && (print_implicit_array_size || !type->has_implicit_size)) {
537 print_expression(type->size_expression);
540 intern_print_type_post(type->element_type);
544 * Prints the postfix part of a bitfield type.
546 * @param type The array type.
548 static void print_bitfield_type_post(const bitfield_type_t *type)
551 print_expression(type->size_expression);
552 intern_print_type_post(type->base_type);
556 * Prints an enum definition.
558 * @param declaration The enum's type declaration.
560 void print_enum_definition(const enum_t *enume)
566 entity_t *entry = enume->base.next;
567 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
568 entry = entry->base.next) {
571 print_string(entry->base.symbol->string);
572 if (entry->enum_value.value != NULL) {
575 /* skip the implicit cast */
576 expression_t *expression = entry->enum_value.value;
577 print_expression(expression);
588 * Prints an enum type.
590 * @param type The enum type.
592 static void print_type_enum(const enum_type_t *type)
594 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
595 print_string("enum ");
597 enum_t *enume = type->enume;
598 symbol_t *symbol = enume->base.symbol;
599 if (symbol != NULL) {
600 print_string(symbol->string);
602 print_enum_definition(enume);
607 * Print the compound part of a compound type.
609 void print_compound_definition(const compound_t *compound)
614 entity_t *entity = compound->members.entities;
615 for( ; entity != NULL; entity = entity->base.next) {
616 if (entity->kind != ENTITY_COMPOUND_MEMBER)
620 print_entity(entity);
627 if (compound->modifiers & DM_TRANSPARENT_UNION) {
628 print_string("__attribute__((__transparent_union__))");
633 * Prints a compound type.
635 * @param type The compound type.
637 static void print_compound_type(const compound_type_t *type)
639 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
641 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
642 print_string("struct ");
644 assert(type->base.kind == TYPE_COMPOUND_UNION);
645 print_string("union ");
648 compound_t *compound = type->compound;
649 symbol_t *symbol = compound->base.symbol;
650 if (symbol != NULL) {
651 print_string(symbol->string);
653 print_compound_definition(compound);
658 * Prints the prefix part of a typedef type.
660 * @param type The typedef type.
662 static void print_typedef_type_pre(const typedef_type_t *const type)
664 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
665 print_string(type->typedefe->base.symbol->string);
669 * Prints the prefix part of a typeof type.
671 * @param type The typeof type.
673 static void print_typeof_type_pre(const typeof_type_t *const type)
675 print_string("typeof(");
676 if (type->expression != NULL) {
677 print_expression(type->expression);
679 print_type(type->typeof_type);
685 * Prints the prefix part of a type.
687 * @param type The type.
689 static void intern_print_type_pre(const type_t *const type)
693 print_string("<error>");
696 print_string("<invalid>");
699 print_type_enum(&type->enumt);
702 print_atomic_type(&type->atomic);
705 print_complex_type(&type->complex);
708 print_imaginary_type(&type->imaginary);
710 case TYPE_COMPOUND_STRUCT:
711 case TYPE_COMPOUND_UNION:
712 print_compound_type(&type->compound);
715 print_function_type_pre(&type->function);
718 print_pointer_type_pre(&type->pointer);
721 print_reference_type_pre(&type->reference);
724 intern_print_type_pre(type->bitfield.base_type);
727 print_array_type_pre(&type->array);
730 print_typedef_type_pre(&type->typedeft);
733 print_typeof_type_pre(&type->typeoft);
736 print_string("unknown");
740 * Prints the postfix part of a type.
742 * @param type The type.
744 static void intern_print_type_post(const type_t *const type)
748 print_function_type_post(&type->function, NULL);
751 print_pointer_type_post(&type->pointer);
754 print_reference_type_post(&type->reference);
757 print_array_type_post(&type->array);
760 print_bitfield_type_post(&type->bitfield);
768 case TYPE_COMPOUND_STRUCT:
769 case TYPE_COMPOUND_UNION:
779 * @param type The type.
781 void print_type(const type_t *const type)
783 print_type_ext(type, NULL, NULL);
786 void print_type_ext(const type_t *const type, const symbol_t *symbol,
787 const scope_t *parameters)
789 intern_print_type_pre(type);
790 if (symbol != NULL) {
792 print_string(symbol->string);
794 if (type->kind == TYPE_FUNCTION) {
795 print_function_type_post(&type->function, parameters);
797 intern_print_type_post(type);
804 * @param type The type to copy.
805 * @return A copy of the type.
807 * @note This does not produce a deep copy!
809 type_t *duplicate_type(const type_t *type)
811 size_t size = get_type_struct_size(type->kind);
813 type_t *const copy = obstack_alloc(&type_obst, size);
814 memcpy(copy, type, size);
815 copy->base.firm_type = NULL;
821 * Returns the unqualified type of a given type.
823 * @param type The type.
824 * @returns The unqualified type.
826 type_t *get_unqualified_type(type_t *type)
828 assert(!is_typeref(type));
830 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
833 type_t *unqualified_type = duplicate_type(type);
834 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
836 return identify_new_type(unqualified_type);
839 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
841 type_t *type = skip_typeref(orig_type);
844 if (is_type_array(type)) {
845 /* For array types the element type has to be adjusted */
846 type_t *element_type = type->array.element_type;
847 type_t *qual_element_type = get_qualified_type(element_type, qual);
849 if (qual_element_type == element_type)
852 copy = duplicate_type(type);
853 copy->array.element_type = qual_element_type;
854 } else if (is_type_valid(type)) {
855 if ((type->base.qualifiers & qual) == (int)qual)
858 copy = duplicate_type(type);
859 copy->base.qualifiers |= qual;
864 return identify_new_type(copy);
868 * Check if a type is valid.
870 * @param type The type to check.
871 * @return true if type represents a valid type.
873 bool type_valid(const type_t *type)
875 return type->kind != TYPE_INVALID;
878 static bool test_atomic_type_flag(atomic_type_kind_t kind,
879 atomic_type_flag_t flag)
881 assert(kind <= ATOMIC_TYPE_LAST);
882 return (atomic_type_properties[kind].flags & flag) != 0;
886 * Returns true if the given type is an integer type.
888 * @param type The type to check.
889 * @return True if type is an integer type.
891 bool is_type_integer(const type_t *type)
893 assert(!is_typeref(type));
895 if (type->kind == TYPE_ENUM)
897 if (type->kind == TYPE_BITFIELD)
900 if (type->kind != TYPE_ATOMIC)
903 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
907 * Returns true if the given type is an enum type.
909 * @param type The type to check.
910 * @return True if type is an enum type.
912 bool is_type_enum(const type_t *type)
914 assert(!is_typeref(type));
915 return type->kind == TYPE_ENUM;
919 * Returns true if the given type is an floating point type.
921 * @param type The type to check.
922 * @return True if type is a floating point type.
924 bool is_type_float(const type_t *type)
926 assert(!is_typeref(type));
928 if (type->kind != TYPE_ATOMIC)
931 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
935 * Returns true if the given type is an complex type.
937 * @param type The type to check.
938 * @return True if type is a complex type.
940 bool is_type_complex(const type_t *type)
942 assert(!is_typeref(type));
944 if (type->kind != TYPE_ATOMIC)
947 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
951 * Returns true if the given type is a signed type.
953 * @param type The type to check.
954 * @return True if type is a signed type.
956 bool is_type_signed(const type_t *type)
958 assert(!is_typeref(type));
960 /* enum types are int for now */
961 if (type->kind == TYPE_ENUM)
963 if (type->kind == TYPE_BITFIELD)
964 return is_type_signed(type->bitfield.base_type);
966 if (type->kind != TYPE_ATOMIC)
969 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
973 * Returns true if the given type represents an arithmetic type.
975 * @param type The type to check.
976 * @return True if type represents an arithmetic type.
978 bool is_type_arithmetic(const type_t *type)
980 assert(!is_typeref(type));
987 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
989 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
991 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
998 * Returns true if the given type is an integer or float type.
1000 * @param type The type to check.
1001 * @return True if type is an integer or float type.
1003 bool is_type_real(const type_t *type)
1006 return is_type_integer(type) || is_type_float(type);
1010 * Returns true if the given type represents a scalar type.
1012 * @param type The type to check.
1013 * @return True if type represents a scalar type.
1015 bool is_type_scalar(const type_t *type)
1017 assert(!is_typeref(type));
1019 if (type->kind == TYPE_POINTER)
1022 return is_type_arithmetic(type);
1026 * Check if a given type is incomplete.
1028 * @param type The type to check.
1029 * @return True if the given type is incomplete (ie. just forward).
1031 bool is_type_incomplete(const type_t *type)
1033 assert(!is_typeref(type));
1035 switch(type->kind) {
1036 case TYPE_COMPOUND_STRUCT:
1037 case TYPE_COMPOUND_UNION: {
1038 const compound_type_t *compound_type = &type->compound;
1039 return !compound_type->compound->complete;
1045 return type->array.size_expression == NULL
1046 && !type->array.size_constant;
1049 return type->atomic.akind == ATOMIC_TYPE_VOID;
1052 return type->complex.akind == ATOMIC_TYPE_VOID;
1054 case TYPE_IMAGINARY:
1055 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1060 case TYPE_REFERENCE:
1066 panic("is_type_incomplete called without typerefs skipped");
1071 panic("invalid type found");
1074 bool is_type_object(const type_t *type)
1076 return !is_type_function(type) && !is_type_incomplete(type);
1080 * Check if two function types are compatible.
1082 static bool function_types_compatible(const function_type_t *func1,
1083 const function_type_t *func2)
1085 const type_t* const ret1 = skip_typeref(func1->return_type);
1086 const type_t* const ret2 = skip_typeref(func2->return_type);
1087 if (!types_compatible(ret1, ret2))
1090 if (func1->linkage != func2->linkage)
1093 cc_kind_t cc1 = func1->calling_convention;
1094 if (cc1 == CC_DEFAULT)
1095 cc1 = default_calling_convention;
1096 cc_kind_t cc2 = func2->calling_convention;
1097 if (cc2 == CC_DEFAULT)
1098 cc2 = default_calling_convention;
1103 if (func1->variadic != func2->variadic)
1106 /* can parameters be compared? */
1107 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1108 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1111 /* TODO: handling of unspecified parameters not correct yet */
1113 /* all argument types must be compatible */
1114 function_parameter_t *parameter1 = func1->parameters;
1115 function_parameter_t *parameter2 = func2->parameters;
1116 for ( ; parameter1 != NULL && parameter2 != NULL;
1117 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1118 type_t *parameter1_type = skip_typeref(parameter1->type);
1119 type_t *parameter2_type = skip_typeref(parameter2->type);
1121 parameter1_type = get_unqualified_type(parameter1_type);
1122 parameter2_type = get_unqualified_type(parameter2_type);
1124 if (!types_compatible(parameter1_type, parameter2_type))
1127 /* same number of arguments? */
1128 if (parameter1 != NULL || parameter2 != NULL)
1135 * Check if two array types are compatible.
1137 static bool array_types_compatible(const array_type_t *array1,
1138 const array_type_t *array2)
1140 type_t *element_type1 = skip_typeref(array1->element_type);
1141 type_t *element_type2 = skip_typeref(array2->element_type);
1142 if (!types_compatible(element_type1, element_type2))
1145 if (!array1->size_constant || !array2->size_constant)
1148 return array1->size == array2->size;
1152 * Check if two types are compatible.
1154 bool types_compatible(const type_t *type1, const type_t *type2)
1156 assert(!is_typeref(type1));
1157 assert(!is_typeref(type2));
1159 /* shortcut: the same type is always compatible */
1163 if (!is_type_valid(type1) || !is_type_valid(type2))
1166 if (type1->base.qualifiers != type2->base.qualifiers)
1168 if (type1->kind != type2->kind)
1171 switch (type1->kind) {
1173 return function_types_compatible(&type1->function, &type2->function);
1175 return type1->atomic.akind == type2->atomic.akind;
1177 return type1->complex.akind == type2->complex.akind;
1178 case TYPE_IMAGINARY:
1179 return type1->imaginary.akind == type2->imaginary.akind;
1181 return array_types_compatible(&type1->array, &type2->array);
1183 case TYPE_POINTER: {
1184 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1185 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1186 return types_compatible(to1, to2);
1189 case TYPE_REFERENCE: {
1190 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1191 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1192 return types_compatible(to1, to2);
1195 case TYPE_COMPOUND_STRUCT:
1196 case TYPE_COMPOUND_UNION: {
1202 /* TODO: not implemented */
1206 /* not sure if this makes sense or is even needed, implement it if you
1207 * really need it! */
1208 panic("type compatibility check for bitfield type");
1211 /* Hmm, the error type should be compatible to all other types */
1214 panic("invalid type found in compatible types");
1217 panic("typerefs not skipped in compatible types?!?");
1220 /* TODO: incomplete */
1225 * Skip all typerefs and return the underlying type.
1227 type_t *skip_typeref(type_t *type)
1229 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1232 switch (type->kind) {
1235 case TYPE_TYPEDEF: {
1236 qualifiers |= type->base.qualifiers;
1238 const typedef_type_t *typedef_type = &type->typedeft;
1239 if (typedef_type->resolved_type != NULL) {
1240 type = typedef_type->resolved_type;
1243 type = typedef_type->typedefe->type;
1247 qualifiers |= type->base.qualifiers;
1248 type = type->typeoft.typeof_type;
1256 if (qualifiers != TYPE_QUALIFIER_NONE) {
1257 type_t *const copy = duplicate_type(type);
1259 /* for const with typedefed array type the element type has to be
1261 if (is_type_array(copy)) {
1262 type_t *element_type = copy->array.element_type;
1263 element_type = duplicate_type(element_type);
1264 element_type->base.qualifiers |= qualifiers;
1265 copy->array.element_type = element_type;
1267 copy->base.qualifiers |= qualifiers;
1270 type = identify_new_type(copy);
1276 unsigned get_type_size(type_t *type)
1278 switch (type->kind) {
1284 return get_atomic_type_size(type->atomic.akind);
1286 return get_atomic_type_size(type->complex.akind) * 2;
1287 case TYPE_IMAGINARY:
1288 return get_atomic_type_size(type->imaginary.akind);
1289 case TYPE_COMPOUND_UNION:
1290 layout_union_type(&type->compound);
1291 return type->compound.compound->size;
1292 case TYPE_COMPOUND_STRUCT:
1293 layout_struct_type(&type->compound);
1294 return type->compound.compound->size;
1296 return get_atomic_type_size(type->enumt.akind);
1298 return 0; /* non-const (but "address-const") */
1299 case TYPE_REFERENCE:
1301 /* TODO: make configurable by backend */
1304 /* TODO: correct if element_type is aligned? */
1305 il_size_t element_size = get_type_size(type->array.element_type);
1306 return type->array.size * element_size;
1311 return get_type_size(type->typedeft.typedefe->type);
1313 if (type->typeoft.typeof_type) {
1314 return get_type_size(type->typeoft.typeof_type);
1316 return get_type_size(type->typeoft.expression->base.type);
1319 panic("invalid type in get_type_size");
1322 unsigned get_type_alignment(type_t *type)
1324 switch (type->kind) {
1330 return get_atomic_type_alignment(type->atomic.akind);
1332 return get_atomic_type_alignment(type->complex.akind);
1333 case TYPE_IMAGINARY:
1334 return get_atomic_type_alignment(type->imaginary.akind);
1335 case TYPE_COMPOUND_UNION:
1336 layout_union_type(&type->compound);
1337 return type->compound.compound->alignment;
1338 case TYPE_COMPOUND_STRUCT:
1339 layout_struct_type(&type->compound);
1340 return type->compound.compound->alignment;
1342 return get_atomic_type_alignment(type->enumt.akind);
1344 /* what is correct here? */
1346 case TYPE_REFERENCE:
1348 /* TODO: make configurable by backend */
1351 return get_type_alignment(type->array.element_type);
1354 case TYPE_TYPEDEF: {
1355 il_alignment_t alignment
1356 = get_type_alignment(type->typedeft.typedefe->type);
1357 if (type->typedeft.typedefe->alignment > alignment)
1358 alignment = type->typedeft.typedefe->alignment;
1363 if (type->typeoft.typeof_type) {
1364 return get_type_alignment(type->typeoft.typeof_type);
1366 return get_type_alignment(type->typeoft.expression->base.type);
1369 panic("invalid type in get_type_alignment");
1372 decl_modifiers_t get_type_modifiers(const type_t *type)
1374 switch(type->kind) {
1378 case TYPE_COMPOUND_STRUCT:
1379 case TYPE_COMPOUND_UNION:
1380 return type->compound.compound->modifiers;
1382 return type->function.modifiers;
1386 case TYPE_IMAGINARY:
1387 case TYPE_REFERENCE:
1392 case TYPE_TYPEDEF: {
1393 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1394 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1398 if (type->typeoft.typeof_type) {
1399 return get_type_modifiers(type->typeoft.typeof_type);
1401 return get_type_modifiers(type->typeoft.expression->base.type);
1404 panic("invalid type found in get_type_modifiers");
1407 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1409 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1412 switch (type->base.kind) {
1414 return TYPE_QUALIFIER_NONE;
1416 qualifiers |= type->base.qualifiers;
1417 const typedef_type_t *typedef_type = &type->typedeft;
1418 if (typedef_type->resolved_type != NULL)
1419 type = typedef_type->resolved_type;
1421 type = typedef_type->typedefe->type;
1424 type = type->typeoft.typeof_type;
1427 if (skip_array_type) {
1428 type = type->array.element_type;
1437 return type->base.qualifiers | qualifiers;
1440 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1442 assert(kind <= ATOMIC_TYPE_LAST);
1443 return atomic_type_properties[kind].size;
1446 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1448 assert(kind <= ATOMIC_TYPE_LAST);
1449 return atomic_type_properties[kind].alignment;
1452 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1454 assert(kind <= ATOMIC_TYPE_LAST);
1455 return atomic_type_properties[kind].flags;
1458 atomic_type_kind_t get_intptr_kind(void)
1460 if (machine_size <= 32)
1461 return ATOMIC_TYPE_INT;
1462 else if (machine_size <= 64)
1463 return ATOMIC_TYPE_LONG;
1465 return ATOMIC_TYPE_LONGLONG;
1468 atomic_type_kind_t get_uintptr_kind(void)
1470 if (machine_size <= 32)
1471 return ATOMIC_TYPE_UINT;
1472 else if (machine_size <= 64)
1473 return ATOMIC_TYPE_ULONG;
1475 return ATOMIC_TYPE_ULONGLONG;
1479 * Find the atomic type kind representing a given size (signed).
1481 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1483 static atomic_type_kind_t kinds[32];
1486 atomic_type_kind_t kind = kinds[size];
1487 if (kind == ATOMIC_TYPE_INVALID) {
1488 static const atomic_type_kind_t possible_kinds[] = {
1493 ATOMIC_TYPE_LONGLONG
1495 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1496 if (get_atomic_type_size(possible_kinds[i]) == size) {
1497 kind = possible_kinds[i];
1507 * Find the atomic type kind representing a given size (signed).
1509 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1511 static atomic_type_kind_t kinds[32];
1514 atomic_type_kind_t kind = kinds[size];
1515 if (kind == ATOMIC_TYPE_INVALID) {
1516 static const atomic_type_kind_t possible_kinds[] = {
1521 ATOMIC_TYPE_ULONGLONG
1523 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1524 if (get_atomic_type_size(possible_kinds[i]) == size) {
1525 kind = possible_kinds[i];
1535 * Hash the given type and return the "singleton" version
1538 type_t *identify_new_type(type_t *type)
1540 type_t *result = typehash_insert(type);
1541 if (result != type) {
1542 obstack_free(&type_obst, type);
1548 * Creates a new atomic type.
1550 * @param akind The kind of the atomic type.
1551 * @param qualifiers Type qualifiers for the new type.
1553 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1555 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1556 type->base.qualifiers = qualifiers;
1557 type->atomic.akind = akind;
1559 return identify_new_type(type);
1563 * Creates a new complex type.
1565 * @param akind The kind of the atomic type.
1566 * @param qualifiers Type qualifiers for the new type.
1568 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1570 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1571 type->base.qualifiers = qualifiers;
1572 type->complex.akind = akind;
1574 return identify_new_type(type);
1578 * Creates a new imaginary type.
1580 * @param akind The kind of the atomic type.
1581 * @param qualifiers Type qualifiers for the new type.
1583 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1585 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1586 type->base.qualifiers = qualifiers;
1587 type->imaginary.akind = akind;
1589 return identify_new_type(type);
1593 * Creates a new pointer type.
1595 * @param points_to The points-to type for the new type.
1596 * @param qualifiers Type qualifiers for the new type.
1598 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1600 type_t *const type = allocate_type_zero(TYPE_POINTER);
1601 type->base.qualifiers = qualifiers;
1602 type->pointer.points_to = points_to;
1603 type->pointer.base_variable = NULL;
1605 return identify_new_type(type);
1609 * Creates a new reference type.
1611 * @param refers_to The referred-to type for the new type.
1613 type_t *make_reference_type(type_t *refers_to)
1615 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1616 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1617 type->reference.refers_to = refers_to;
1619 return identify_new_type(type);
1623 * Creates a new based pointer type.
1625 * @param points_to The points-to type for the new type.
1626 * @param qualifiers Type qualifiers for the new type.
1627 * @param variable The based variable
1629 type_t *make_based_pointer_type(type_t *points_to,
1630 type_qualifiers_t qualifiers, variable_t *variable)
1632 type_t *const type = allocate_type_zero(TYPE_POINTER);
1633 type->base.qualifiers = qualifiers;
1634 type->pointer.points_to = points_to;
1635 type->pointer.base_variable = variable;
1637 return identify_new_type(type);
1641 type_t *make_array_type(type_t *element_type, size_t size,
1642 type_qualifiers_t qualifiers)
1644 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1645 type->base.qualifiers = qualifiers;
1646 type->array.element_type = element_type;
1647 type->array.size = size;
1648 type->array.size_constant = true;
1650 return identify_new_type(type);
1653 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1654 il_alignment_t *struct_alignment,
1655 bool packed, entity_t *first)
1657 il_size_t offset = *struct_offset;
1658 il_alignment_t alignment = *struct_alignment;
1659 size_t bit_offset = 0;
1662 for (member = first; member != NULL; member = member->base.next) {
1663 if (member->kind != ENTITY_COMPOUND_MEMBER)
1666 type_t *type = member->declaration.type;
1667 if (type->kind != TYPE_BITFIELD)
1670 type_t *base_type = skip_typeref(type->bitfield.base_type);
1671 il_alignment_t base_alignment = get_type_alignment(base_type);
1672 il_alignment_t alignment_mask = base_alignment-1;
1673 if (base_alignment > alignment)
1674 alignment = base_alignment;
1676 size_t bit_size = type->bitfield.bit_size;
1678 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1679 offset &= ~alignment_mask;
1680 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1682 if (bit_offset + bit_size > base_size || bit_size == 0) {
1683 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1684 offset = (offset + base_alignment-1) & ~alignment_mask;
1689 if (byte_order_big_endian) {
1690 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1691 member->compound_member.offset = offset & ~alignment_mask;
1692 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1694 member->compound_member.offset = offset;
1695 member->compound_member.bit_offset = bit_offset;
1698 bit_offset += bit_size;
1699 offset += bit_offset / BITS_PER_BYTE;
1700 bit_offset %= BITS_PER_BYTE;
1706 *struct_offset = offset;
1707 *struct_alignment = alignment;
1711 void layout_struct_type(compound_type_t *type)
1713 assert(type->compound != NULL);
1715 compound_t *compound = type->compound;
1716 if (!compound->complete)
1718 if (type->compound->layouted)
1721 il_size_t offset = 0;
1722 il_alignment_t alignment = compound->alignment;
1723 bool need_pad = false;
1725 entity_t *entry = compound->members.entities;
1726 while (entry != NULL) {
1727 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1728 entry = entry->base.next;
1732 type_t *m_type = entry->declaration.type;
1733 type_t *skipped = skip_typeref(m_type);
1734 if (! is_type_valid(skipped)) {
1735 entry = entry->base.next;
1739 if (skipped->kind == TYPE_BITFIELD) {
1740 entry = pack_bitfield_members(&offset, &alignment,
1741 compound->packed, entry);
1745 il_alignment_t m_alignment = get_type_alignment(m_type);
1746 if (m_alignment > alignment)
1747 alignment = m_alignment;
1749 if (!compound->packed) {
1750 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1752 if (new_offset > offset) {
1754 offset = new_offset;
1758 entry->compound_member.offset = offset;
1759 offset += get_type_size(m_type);
1761 entry = entry->base.next;
1764 if (!compound->packed) {
1765 il_size_t new_offset = (offset + alignment-1) & -alignment;
1766 if (new_offset > offset) {
1768 offset = new_offset;
1772 source_position_t const *const pos = &compound->base.source_position;
1774 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1775 } else if (compound->packed) {
1776 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1779 compound->size = offset;
1780 compound->alignment = alignment;
1781 compound->layouted = true;
1784 void layout_union_type(compound_type_t *type)
1786 assert(type->compound != NULL);
1788 compound_t *compound = type->compound;
1789 if (! compound->complete)
1793 il_alignment_t alignment = compound->alignment;
1795 entity_t *entry = compound->members.entities;
1796 for (; entry != NULL; entry = entry->base.next) {
1797 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1800 type_t *m_type = entry->declaration.type;
1801 if (! is_type_valid(skip_typeref(m_type)))
1804 entry->compound_member.offset = 0;
1805 il_size_t m_size = get_type_size(m_type);
1808 il_alignment_t m_alignment = get_type_alignment(m_type);
1809 if (m_alignment > alignment)
1810 alignment = m_alignment;
1812 size = (size + alignment - 1) & -alignment;
1814 compound->size = size;
1815 compound->alignment = alignment;
1818 function_parameter_t *allocate_parameter(type_t *const type)
1820 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1821 memset(param, 0, sizeof(*param));
1826 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1827 type_t *argument_type2)
1829 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1830 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1831 parameter1->next = parameter2;
1833 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1834 type->function.return_type = return_type;
1835 type->function.parameters = parameter1;
1836 type->function.linkage = LINKAGE_C;
1838 return identify_new_type(type);
1841 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1843 function_parameter_t *const parameter = allocate_parameter(argument_type);
1845 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1846 type->function.return_type = return_type;
1847 type->function.parameters = parameter;
1848 type->function.linkage = LINKAGE_C;
1850 return identify_new_type(type);
1853 type_t *make_function_1_type_variadic(type_t *return_type,
1854 type_t *argument_type)
1856 function_parameter_t *const parameter = allocate_parameter(argument_type);
1858 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1859 type->function.return_type = return_type;
1860 type->function.parameters = parameter;
1861 type->function.variadic = true;
1862 type->function.linkage = LINKAGE_C;
1864 return identify_new_type(type);
1867 type_t *make_function_0_type(type_t *return_type)
1869 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1870 type->function.return_type = return_type;
1871 type->function.parameters = NULL;
1872 type->function.linkage = LINKAGE_C;
1874 return identify_new_type(type);
1877 type_t *make_function_type(type_t *return_type, int n_types,
1878 type_t *const *argument_types,
1879 decl_modifiers_t modifiers)
1881 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1882 type->function.return_type = return_type;
1883 type->function.modifiers |= modifiers;
1884 type->function.linkage = LINKAGE_C;
1886 function_parameter_t **anchor = &type->function.parameters;
1887 for (int i = 0; i < n_types; ++i) {
1888 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1889 *anchor = parameter;
1890 anchor = ¶meter->next;
1893 return identify_new_type(type);
1897 * Debug helper. Prints the given type to stdout.
1899 static __attribute__((unused))
1900 void dbg_type(const type_t *type)
1902 print_to_file(stderr);