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_ARRAY] = sizeof(array_type_t),
71 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
72 [TYPE_TYPEOF] = sizeof(typeof_type_t),
74 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
75 assert(kind <= TYPE_TYPEOF);
76 assert(sizes[kind] != 0);
80 type_t *allocate_type_zero(type_kind_t kind)
82 size_t const size = get_type_struct_size(kind);
83 type_t *const res = obstack_alloc(&type_obst, size);
85 res->base.kind = kind;
91 * Properties of atomic types.
93 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
94 //ATOMIC_TYPE_INVALID = 0,
95 [ATOMIC_TYPE_VOID] = {
98 .flags = ATOMIC_TYPE_FLAG_NONE
100 [ATOMIC_TYPE_WCHAR_T] = {
101 .size = (unsigned)-1,
102 .alignment = (unsigned)-1,
103 /* signed flag will be set when known */
104 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
106 [ATOMIC_TYPE_CHAR] = {
109 /* signed flag will be set when known */
110 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
112 [ATOMIC_TYPE_SCHAR] = {
115 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
116 | ATOMIC_TYPE_FLAG_SIGNED,
118 [ATOMIC_TYPE_UCHAR] = {
121 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
123 [ATOMIC_TYPE_SHORT] = {
126 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
127 | ATOMIC_TYPE_FLAG_SIGNED
129 [ATOMIC_TYPE_USHORT] = {
132 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
134 [ATOMIC_TYPE_INT] = {
135 .size = (unsigned) -1,
136 .alignment = (unsigned) -1,
137 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
138 | ATOMIC_TYPE_FLAG_SIGNED,
140 [ATOMIC_TYPE_UINT] = {
141 .size = (unsigned) -1,
142 .alignment = (unsigned) -1,
143 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
145 [ATOMIC_TYPE_LONG] = {
146 .size = (unsigned) -1,
147 .alignment = (unsigned) -1,
148 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
149 | ATOMIC_TYPE_FLAG_SIGNED,
151 [ATOMIC_TYPE_ULONG] = {
152 .size = (unsigned) -1,
153 .alignment = (unsigned) -1,
154 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
156 [ATOMIC_TYPE_LONGLONG] = {
157 .size = (unsigned) -1,
158 .alignment = (unsigned) -1,
159 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
160 | ATOMIC_TYPE_FLAG_SIGNED,
162 [ATOMIC_TYPE_ULONGLONG] = {
163 .size = (unsigned) -1,
164 .alignment = (unsigned) -1,
165 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
167 [ATOMIC_TYPE_BOOL] = {
168 .size = (unsigned) -1,
169 .alignment = (unsigned) -1,
170 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
172 [ATOMIC_TYPE_FLOAT] = {
174 .alignment = (unsigned) -1,
175 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
176 | ATOMIC_TYPE_FLAG_SIGNED,
178 [ATOMIC_TYPE_DOUBLE] = {
180 .alignment = (unsigned) -1,
181 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
182 | ATOMIC_TYPE_FLAG_SIGNED,
184 [ATOMIC_TYPE_LONG_DOUBLE] = {
185 .size = (unsigned) -1, /* will be filled in later */
186 .alignment = (unsigned) -1,
187 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
188 | ATOMIC_TYPE_FLAG_SIGNED,
190 /* complex and imaginary types are set in init_types */
193 static inline bool is_po2(unsigned x)
195 return (x & (x-1)) == 0;
198 void init_types(void)
200 obstack_init(&type_obst);
202 atomic_type_properties_t *props = atomic_type_properties;
204 if (char_is_signed) {
205 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
208 unsigned int_size = machine_size < 32 ? 2 : 4;
209 /* long is always 32bit on windows */
210 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
211 unsigned llong_size = machine_size < 32 ? 4 : 8;
213 props[ATOMIC_TYPE_INT].size = int_size;
214 props[ATOMIC_TYPE_INT].alignment = int_size;
215 props[ATOMIC_TYPE_UINT].size = int_size;
216 props[ATOMIC_TYPE_UINT].alignment = int_size;
217 props[ATOMIC_TYPE_LONG].size = long_size;
218 props[ATOMIC_TYPE_LONG].alignment = long_size;
219 props[ATOMIC_TYPE_ULONG].size = long_size;
220 props[ATOMIC_TYPE_ULONG].alignment = long_size;
221 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
222 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
223 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
224 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
226 /* TODO: backend specific, need a way to query the backend for this.
227 * The following are good settings for x86 */
228 if (machine_size <= 32) {
229 props[ATOMIC_TYPE_FLOAT].alignment = 4;
230 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
231 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
232 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
233 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
235 props[ATOMIC_TYPE_FLOAT].alignment = 4;
236 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
237 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
238 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
239 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
242 if (long_double_size > 0) {
243 props[ATOMIC_TYPE_LONG_DOUBLE].size = long_double_size;
244 if (is_po2(long_double_size)) {
245 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = long_double_size;
248 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
251 /* TODO: make this configurable for platforms which do not use byte sized
253 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
255 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
258 void exit_types(void)
260 obstack_free(&type_obst, NULL);
263 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
265 size_t sep = q & QUAL_SEP_START ? 0 : 1;
266 if (qualifiers & TYPE_QUALIFIER_CONST) {
267 print_string(" const" + sep);
270 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
271 print_string(" volatile" + sep);
274 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
275 print_string(" restrict" + sep);
278 if (sep == 0 && q & QUAL_SEP_END)
282 const char *get_atomic_kind_name(atomic_type_kind_t kind)
285 case ATOMIC_TYPE_INVALID: break;
286 case ATOMIC_TYPE_VOID: return "void";
287 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
288 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
289 case ATOMIC_TYPE_CHAR: return "char";
290 case ATOMIC_TYPE_SCHAR: return "signed char";
291 case ATOMIC_TYPE_UCHAR: return "unsigned char";
292 case ATOMIC_TYPE_INT: return "int";
293 case ATOMIC_TYPE_UINT: return "unsigned int";
294 case ATOMIC_TYPE_SHORT: return "short";
295 case ATOMIC_TYPE_USHORT: return "unsigned short";
296 case ATOMIC_TYPE_LONG: return "long";
297 case ATOMIC_TYPE_ULONG: return "unsigned long";
298 case ATOMIC_TYPE_LONGLONG: return "long long";
299 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
300 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
301 case ATOMIC_TYPE_FLOAT: return "float";
302 case ATOMIC_TYPE_DOUBLE: return "double";
304 return "INVALIDATOMIC";
308 * Prints the name of an atomic type kinds.
310 * @param kind The type kind.
312 static void print_atomic_kinds(atomic_type_kind_t kind)
314 const char *s = get_atomic_kind_name(kind);
319 * Prints the name of an atomic type.
321 * @param type The type.
323 static void print_atomic_type(const atomic_type_t *type)
325 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
326 print_atomic_kinds(type->akind);
330 * Prints the name of a complex type.
332 * @param type The type.
334 static void print_complex_type(const complex_type_t *type)
336 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
337 print_string("_Complex");
338 print_atomic_kinds(type->akind);
342 * Prints the name of an imaginary type.
344 * @param type The type.
346 static void print_imaginary_type(const imaginary_type_t *type)
348 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
349 print_string("_Imaginary ");
350 print_atomic_kinds(type->akind);
354 * Print the first part (the prefix) of a type.
356 * @param type The type to print.
358 static void print_function_type_pre(const function_type_t *type)
360 switch (type->linkage) {
361 case LINKAGE_INVALID:
366 print_string("extern \"C\" ");
370 if (!(c_mode & _CXX))
371 print_string("extern \"C++\" ");
375 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
377 intern_print_type_pre(type->return_type);
379 cc_kind_t cc = type->calling_convention;
382 case CC_CDECL: print_string(" __cdecl"); break;
383 case CC_STDCALL: print_string(" __stdcall"); break;
384 case CC_FASTCALL: print_string(" __fastcall"); break;
385 case CC_THISCALL: print_string(" __thiscall"); break;
387 if (default_calling_convention != CC_CDECL) {
388 /* show the default calling convention if its not cdecl */
389 cc = default_calling_convention;
397 * Print the second part (the postfix) of a type.
399 * @param type The type to print.
401 static void print_function_type_post(const function_type_t *type,
402 const scope_t *parameters)
406 if (parameters == NULL) {
407 function_parameter_t *parameter = type->parameters;
408 for( ; parameter != NULL; parameter = parameter->next) {
414 print_type(parameter->type);
417 entity_t *parameter = parameters->entities;
418 for (; parameter != NULL; parameter = parameter->base.next) {
419 if (parameter->kind != ENTITY_PARAMETER)
427 const type_t *const param_type = parameter->declaration.type;
428 if (param_type == NULL) {
429 print_string(parameter->base.symbol->string);
431 print_type_ext(param_type, parameter->base.symbol, NULL);
435 if (type->variadic) {
443 if (first && !type->unspecified_parameters) {
444 print_string("void");
448 intern_print_type_post(type->return_type);
452 * Prints the prefix part of a pointer type.
454 * @param type The pointer type.
456 static void print_pointer_type_pre(const pointer_type_t *type)
458 type_t const *const points_to = type->points_to;
459 intern_print_type_pre(points_to);
460 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
462 variable_t *const variable = type->base_variable;
463 if (variable != NULL) {
464 print_string(" __based(");
465 print_string(variable->base.base.symbol->string);
469 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
473 * Prints the postfix part of a pointer type.
475 * @param type The pointer type.
477 static void print_pointer_type_post(const pointer_type_t *type)
479 type_t const *const points_to = type->points_to;
480 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
482 intern_print_type_post(points_to);
486 * Prints the prefix part of a reference type.
488 * @param type The reference type.
490 static void print_reference_type_pre(const reference_type_t *type)
492 type_t const *const refers_to = type->refers_to;
493 intern_print_type_pre(refers_to);
494 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
500 * Prints the postfix part of a reference type.
502 * @param type The reference type.
504 static void print_reference_type_post(const reference_type_t *type)
506 type_t const *const refers_to = type->refers_to;
507 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
509 intern_print_type_post(refers_to);
513 * Prints the prefix part of an array type.
515 * @param type The array type.
517 static void print_array_type_pre(const array_type_t *type)
519 intern_print_type_pre(type->element_type);
523 * Prints the postfix part of an array type.
525 * @param type The array type.
527 static void print_array_type_post(const array_type_t *type)
530 if (type->is_static) {
531 print_string("static ");
533 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
534 if (type->size_expression != NULL
535 && (print_implicit_array_size || !type->has_implicit_size)) {
536 print_expression(type->size_expression);
539 intern_print_type_post(type->element_type);
543 * Prints the postfix part of a bitfield type.
545 * @param type The array type.
547 static void print_bitfield_type_post(const bitfield_type_t *type)
550 print_expression(type->size_expression);
551 intern_print_type_post(type->base_type);
555 * Prints an enum definition.
557 * @param declaration The enum's type declaration.
559 void print_enum_definition(const enum_t *enume)
565 entity_t *entry = enume->base.next;
566 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
567 entry = entry->base.next) {
570 print_string(entry->base.symbol->string);
571 if (entry->enum_value.value != NULL) {
574 /* skip the implicit cast */
575 expression_t *expression = entry->enum_value.value;
576 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
577 expression = expression->unary.value;
579 print_expression(expression);
590 * Prints an enum type.
592 * @param type The enum type.
594 static void print_type_enum(const enum_type_t *type)
596 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
597 print_string("enum ");
599 enum_t *enume = type->enume;
600 symbol_t *symbol = enume->base.symbol;
601 if (symbol != NULL) {
602 print_string(symbol->string);
604 print_enum_definition(enume);
609 * Print the compound part of a compound type.
611 void print_compound_definition(const compound_t *compound)
616 entity_t *entity = compound->members.entities;
617 for( ; entity != NULL; entity = entity->base.next) {
618 if (entity->kind != ENTITY_COMPOUND_MEMBER)
622 print_entity(entity);
629 if (compound->modifiers & DM_TRANSPARENT_UNION) {
630 print_string("__attribute__((__transparent_union__))");
635 * Prints a compound type.
637 * @param type The compound type.
639 static void print_compound_type(const compound_type_t *type)
641 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
643 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
644 print_string("struct ");
646 assert(type->base.kind == TYPE_COMPOUND_UNION);
647 print_string("union ");
650 compound_t *compound = type->compound;
651 symbol_t *symbol = compound->base.symbol;
652 if (symbol != NULL) {
653 print_string(symbol->string);
655 print_compound_definition(compound);
660 * Prints the prefix part of a typedef type.
662 * @param type The typedef type.
664 static void print_typedef_type_pre(const typedef_type_t *const type)
666 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
667 print_string(type->typedefe->base.symbol->string);
671 * Prints the prefix part of a typeof type.
673 * @param type The typeof type.
675 static void print_typeof_type_pre(const typeof_type_t *const type)
677 print_string("typeof(");
678 if (type->expression != NULL) {
679 print_expression(type->expression);
681 print_type(type->typeof_type);
687 * Prints the prefix part of a type.
689 * @param type The type.
691 static void intern_print_type_pre(const type_t *const type)
695 print_string("<error>");
698 print_string("<invalid>");
701 print_type_enum(&type->enumt);
704 print_atomic_type(&type->atomic);
707 print_complex_type(&type->complex);
710 print_imaginary_type(&type->imaginary);
712 case TYPE_COMPOUND_STRUCT:
713 case TYPE_COMPOUND_UNION:
714 print_compound_type(&type->compound);
717 print_function_type_pre(&type->function);
720 print_pointer_type_pre(&type->pointer);
723 print_reference_type_pre(&type->reference);
726 intern_print_type_pre(type->bitfield.base_type);
729 print_array_type_pre(&type->array);
732 print_typedef_type_pre(&type->typedeft);
735 print_typeof_type_pre(&type->typeoft);
738 print_string("unknown");
742 * Prints the postfix part of a type.
744 * @param type The type.
746 static void intern_print_type_post(const type_t *const type)
750 print_function_type_post(&type->function, NULL);
753 print_pointer_type_post(&type->pointer);
756 print_reference_type_post(&type->reference);
759 print_array_type_post(&type->array);
762 print_bitfield_type_post(&type->bitfield);
770 case TYPE_COMPOUND_STRUCT:
771 case TYPE_COMPOUND_UNION:
781 * @param type The type.
783 void print_type(const type_t *const type)
785 print_type_ext(type, NULL, NULL);
788 void print_type_ext(const type_t *const type, const symbol_t *symbol,
789 const scope_t *parameters)
791 intern_print_type_pre(type);
792 if (symbol != NULL) {
794 print_string(symbol->string);
796 if (type->kind == TYPE_FUNCTION) {
797 print_function_type_post(&type->function, parameters);
799 intern_print_type_post(type);
806 * @param type The type to copy.
807 * @return A copy of the type.
809 * @note This does not produce a deep copy!
811 type_t *duplicate_type(const type_t *type)
813 size_t size = get_type_struct_size(type->kind);
815 type_t *const copy = obstack_alloc(&type_obst, size);
816 memcpy(copy, type, size);
817 copy->base.firm_type = NULL;
823 * Returns the unqualified type of a given type.
825 * @param type The type.
826 * @returns The unqualified type.
828 type_t *get_unqualified_type(type_t *type)
830 assert(!is_typeref(type));
832 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
835 type_t *unqualified_type = duplicate_type(type);
836 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
838 return identify_new_type(unqualified_type);
841 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
843 type_t *type = skip_typeref(orig_type);
846 if (is_type_array(type)) {
847 /* For array types the element type has to be adjusted */
848 type_t *element_type = type->array.element_type;
849 type_t *qual_element_type = get_qualified_type(element_type, qual);
851 if (qual_element_type == element_type)
854 copy = duplicate_type(type);
855 copy->array.element_type = qual_element_type;
856 } else if (is_type_valid(type)) {
857 if ((type->base.qualifiers & qual) == (int)qual)
860 copy = duplicate_type(type);
861 copy->base.qualifiers |= qual;
866 return identify_new_type(copy);
870 * Check if a type is valid.
872 * @param type The type to check.
873 * @return true if type represents a valid type.
875 bool type_valid(const type_t *type)
877 return type->kind != TYPE_INVALID;
880 static bool test_atomic_type_flag(atomic_type_kind_t kind,
881 atomic_type_flag_t flag)
883 assert(kind <= ATOMIC_TYPE_LAST);
884 return (atomic_type_properties[kind].flags & flag) != 0;
888 * Returns true if the given type is an integer type.
890 * @param type The type to check.
891 * @return True if type is an integer type.
893 bool is_type_integer(const type_t *type)
895 assert(!is_typeref(type));
897 if (type->kind == TYPE_ENUM)
899 if (type->kind == TYPE_BITFIELD)
902 if (type->kind != TYPE_ATOMIC)
905 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
909 * Returns true if the given type is an enum type.
911 * @param type The type to check.
912 * @return True if type is an enum type.
914 bool is_type_enum(const type_t *type)
916 assert(!is_typeref(type));
917 return type->kind == TYPE_ENUM;
921 * Returns true if the given type is an floating point type.
923 * @param type The type to check.
924 * @return True if type is a floating point type.
926 bool is_type_float(const type_t *type)
928 assert(!is_typeref(type));
930 if (type->kind != TYPE_ATOMIC)
933 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
937 * Returns true if the given type is an complex type.
939 * @param type The type to check.
940 * @return True if type is a complex type.
942 bool is_type_complex(const type_t *type)
944 assert(!is_typeref(type));
946 if (type->kind != TYPE_ATOMIC)
949 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
953 * Returns true if the given type is a signed type.
955 * @param type The type to check.
956 * @return True if type is a signed type.
958 bool is_type_signed(const type_t *type)
960 assert(!is_typeref(type));
962 /* enum types are int for now */
963 if (type->kind == TYPE_ENUM)
965 if (type->kind == TYPE_BITFIELD)
966 return is_type_signed(type->bitfield.base_type);
968 if (type->kind != TYPE_ATOMIC)
971 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
975 * Returns true if the given type represents an arithmetic type.
977 * @param type The type to check.
978 * @return True if type represents an arithmetic type.
980 bool is_type_arithmetic(const type_t *type)
982 assert(!is_typeref(type));
989 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
991 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
993 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
1000 * Returns true if the given type is an integer or float type.
1002 * @param type The type to check.
1003 * @return True if type is an integer or float type.
1005 bool is_type_real(const type_t *type)
1008 return is_type_integer(type) || is_type_float(type);
1012 * Returns true if the given type represents a scalar type.
1014 * @param type The type to check.
1015 * @return True if type represents a scalar type.
1017 bool is_type_scalar(const type_t *type)
1019 assert(!is_typeref(type));
1021 if (type->kind == TYPE_POINTER)
1024 return is_type_arithmetic(type);
1028 * Check if a given type is incomplete.
1030 * @param type The type to check.
1031 * @return True if the given type is incomplete (ie. just forward).
1033 bool is_type_incomplete(const type_t *type)
1035 assert(!is_typeref(type));
1037 switch(type->kind) {
1038 case TYPE_COMPOUND_STRUCT:
1039 case TYPE_COMPOUND_UNION: {
1040 const compound_type_t *compound_type = &type->compound;
1041 return !compound_type->compound->complete;
1047 return type->array.size_expression == NULL
1048 && !type->array.size_constant;
1051 return type->atomic.akind == ATOMIC_TYPE_VOID;
1054 return type->complex.akind == ATOMIC_TYPE_VOID;
1056 case TYPE_IMAGINARY:
1057 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1062 case TYPE_REFERENCE:
1068 panic("is_type_incomplete called without typerefs skipped");
1073 panic("invalid type found");
1076 bool is_type_object(const type_t *type)
1078 return !is_type_function(type) && !is_type_incomplete(type);
1082 * Check if two function types are compatible.
1084 static bool function_types_compatible(const function_type_t *func1,
1085 const function_type_t *func2)
1087 const type_t* const ret1 = skip_typeref(func1->return_type);
1088 const type_t* const ret2 = skip_typeref(func2->return_type);
1089 if (!types_compatible(ret1, ret2))
1092 if (func1->linkage != func2->linkage)
1095 cc_kind_t cc1 = func1->calling_convention;
1096 if (cc1 == CC_DEFAULT)
1097 cc1 = default_calling_convention;
1098 cc_kind_t cc2 = func2->calling_convention;
1099 if (cc2 == CC_DEFAULT)
1100 cc2 = default_calling_convention;
1105 if (func1->variadic != func2->variadic)
1108 /* can parameters be compared? */
1109 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1110 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1113 /* TODO: handling of unspecified parameters not correct yet */
1115 /* all argument types must be compatible */
1116 function_parameter_t *parameter1 = func1->parameters;
1117 function_parameter_t *parameter2 = func2->parameters;
1118 for ( ; parameter1 != NULL && parameter2 != NULL;
1119 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1120 type_t *parameter1_type = skip_typeref(parameter1->type);
1121 type_t *parameter2_type = skip_typeref(parameter2->type);
1123 parameter1_type = get_unqualified_type(parameter1_type);
1124 parameter2_type = get_unqualified_type(parameter2_type);
1126 if (!types_compatible(parameter1_type, parameter2_type))
1129 /* same number of arguments? */
1130 if (parameter1 != NULL || parameter2 != NULL)
1137 * Check if two array types are compatible.
1139 static bool array_types_compatible(const array_type_t *array1,
1140 const array_type_t *array2)
1142 type_t *element_type1 = skip_typeref(array1->element_type);
1143 type_t *element_type2 = skip_typeref(array2->element_type);
1144 if (!types_compatible(element_type1, element_type2))
1147 if (!array1->size_constant || !array2->size_constant)
1150 return array1->size == array2->size;
1154 * Check if two types are compatible.
1156 bool types_compatible(const type_t *type1, const type_t *type2)
1158 assert(!is_typeref(type1));
1159 assert(!is_typeref(type2));
1161 /* shortcut: the same type is always compatible */
1165 if (!is_type_valid(type1) || !is_type_valid(type2))
1168 if (type1->base.qualifiers != type2->base.qualifiers)
1170 if (type1->kind != type2->kind)
1173 switch (type1->kind) {
1175 return function_types_compatible(&type1->function, &type2->function);
1177 return type1->atomic.akind == type2->atomic.akind;
1179 return type1->complex.akind == type2->complex.akind;
1180 case TYPE_IMAGINARY:
1181 return type1->imaginary.akind == type2->imaginary.akind;
1183 return array_types_compatible(&type1->array, &type2->array);
1185 case TYPE_POINTER: {
1186 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1187 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1188 return types_compatible(to1, to2);
1191 case TYPE_REFERENCE: {
1192 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1193 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1194 return types_compatible(to1, to2);
1197 case TYPE_COMPOUND_STRUCT:
1198 case TYPE_COMPOUND_UNION: {
1204 /* TODO: not implemented */
1208 /* not sure if this makes sense or is even needed, implement it if you
1209 * really need it! */
1210 panic("type compatibility check for bitfield type");
1213 /* Hmm, the error type should be compatible to all other types */
1216 panic("invalid type found in compatible types");
1219 panic("typerefs not skipped in compatible types?!?");
1222 /* TODO: incomplete */
1227 * Skip all typerefs and return the underlying type.
1229 type_t *skip_typeref(type_t *type)
1231 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1234 switch (type->kind) {
1237 case TYPE_TYPEDEF: {
1238 qualifiers |= type->base.qualifiers;
1240 const typedef_type_t *typedef_type = &type->typedeft;
1241 if (typedef_type->resolved_type != NULL) {
1242 type = typedef_type->resolved_type;
1245 type = typedef_type->typedefe->type;
1249 qualifiers |= type->base.qualifiers;
1250 type = type->typeoft.typeof_type;
1258 if (qualifiers != TYPE_QUALIFIER_NONE) {
1259 type_t *const copy = duplicate_type(type);
1261 /* for const with typedefed array type the element type has to be
1263 if (is_type_array(copy)) {
1264 type_t *element_type = copy->array.element_type;
1265 element_type = duplicate_type(element_type);
1266 element_type->base.qualifiers |= qualifiers;
1267 copy->array.element_type = element_type;
1269 copy->base.qualifiers |= qualifiers;
1272 type = identify_new_type(copy);
1278 unsigned get_type_size(type_t *type)
1280 switch (type->kind) {
1286 return get_atomic_type_size(type->atomic.akind);
1288 return get_atomic_type_size(type->complex.akind) * 2;
1289 case TYPE_IMAGINARY:
1290 return get_atomic_type_size(type->imaginary.akind);
1291 case TYPE_COMPOUND_UNION:
1292 layout_union_type(&type->compound);
1293 return type->compound.compound->size;
1294 case TYPE_COMPOUND_STRUCT:
1295 layout_struct_type(&type->compound);
1296 return type->compound.compound->size;
1298 return get_atomic_type_size(type->enumt.akind);
1300 return 0; /* non-const (but "address-const") */
1301 case TYPE_REFERENCE:
1303 /* TODO: make configurable by backend */
1306 /* TODO: correct if element_type is aligned? */
1307 il_size_t element_size = get_type_size(type->array.element_type);
1308 return type->array.size * element_size;
1313 return get_type_size(type->typedeft.typedefe->type);
1315 if (type->typeoft.typeof_type) {
1316 return get_type_size(type->typeoft.typeof_type);
1318 return get_type_size(type->typeoft.expression->base.type);
1321 panic("invalid type in get_type_size");
1324 unsigned get_type_alignment(type_t *type)
1326 switch (type->kind) {
1332 return get_atomic_type_alignment(type->atomic.akind);
1334 return get_atomic_type_alignment(type->complex.akind);
1335 case TYPE_IMAGINARY:
1336 return get_atomic_type_alignment(type->imaginary.akind);
1337 case TYPE_COMPOUND_UNION:
1338 layout_union_type(&type->compound);
1339 return type->compound.compound->alignment;
1340 case TYPE_COMPOUND_STRUCT:
1341 layout_struct_type(&type->compound);
1342 return type->compound.compound->alignment;
1344 return get_atomic_type_alignment(type->enumt.akind);
1346 /* what is correct here? */
1348 case TYPE_REFERENCE:
1350 /* TODO: make configurable by backend */
1353 return get_type_alignment(type->array.element_type);
1356 case TYPE_TYPEDEF: {
1357 il_alignment_t alignment
1358 = get_type_alignment(type->typedeft.typedefe->type);
1359 if (type->typedeft.typedefe->alignment > alignment)
1360 alignment = type->typedeft.typedefe->alignment;
1365 if (type->typeoft.typeof_type) {
1366 return get_type_alignment(type->typeoft.typeof_type);
1368 return get_type_alignment(type->typeoft.expression->base.type);
1371 panic("invalid type in get_type_alignment");
1374 decl_modifiers_t get_type_modifiers(const type_t *type)
1376 switch(type->kind) {
1380 case TYPE_COMPOUND_STRUCT:
1381 case TYPE_COMPOUND_UNION:
1382 return type->compound.compound->modifiers;
1384 return type->function.modifiers;
1388 case TYPE_IMAGINARY:
1389 case TYPE_REFERENCE:
1394 case TYPE_TYPEDEF: {
1395 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1396 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1400 if (type->typeoft.typeof_type) {
1401 return get_type_modifiers(type->typeoft.typeof_type);
1403 return get_type_modifiers(type->typeoft.expression->base.type);
1406 panic("invalid type found in get_type_modifiers");
1409 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1411 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1414 switch (type->base.kind) {
1416 return TYPE_QUALIFIER_NONE;
1418 qualifiers |= type->base.qualifiers;
1419 const typedef_type_t *typedef_type = &type->typedeft;
1420 if (typedef_type->resolved_type != NULL)
1421 type = typedef_type->resolved_type;
1423 type = typedef_type->typedefe->type;
1426 type = type->typeoft.typeof_type;
1429 if (skip_array_type) {
1430 type = type->array.element_type;
1439 return type->base.qualifiers | qualifiers;
1442 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1444 assert(kind <= ATOMIC_TYPE_LAST);
1445 return atomic_type_properties[kind].size;
1448 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1450 assert(kind <= ATOMIC_TYPE_LAST);
1451 return atomic_type_properties[kind].alignment;
1454 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1456 assert(kind <= ATOMIC_TYPE_LAST);
1457 return atomic_type_properties[kind].flags;
1460 atomic_type_kind_t get_intptr_kind(void)
1462 if (machine_size <= 32)
1463 return ATOMIC_TYPE_INT;
1464 else if (machine_size <= 64)
1465 return ATOMIC_TYPE_LONG;
1467 return ATOMIC_TYPE_LONGLONG;
1470 atomic_type_kind_t get_uintptr_kind(void)
1472 if (machine_size <= 32)
1473 return ATOMIC_TYPE_UINT;
1474 else if (machine_size <= 64)
1475 return ATOMIC_TYPE_ULONG;
1477 return ATOMIC_TYPE_ULONGLONG;
1481 * Find the atomic type kind representing a given size (signed).
1483 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1485 static atomic_type_kind_t kinds[32];
1488 atomic_type_kind_t kind = kinds[size];
1489 if (kind == ATOMIC_TYPE_INVALID) {
1490 static const atomic_type_kind_t possible_kinds[] = {
1495 ATOMIC_TYPE_LONGLONG
1497 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1498 if (get_atomic_type_size(possible_kinds[i]) == size) {
1499 kind = possible_kinds[i];
1509 * Find the atomic type kind representing a given size (signed).
1511 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1513 static atomic_type_kind_t kinds[32];
1516 atomic_type_kind_t kind = kinds[size];
1517 if (kind == ATOMIC_TYPE_INVALID) {
1518 static const atomic_type_kind_t possible_kinds[] = {
1523 ATOMIC_TYPE_ULONGLONG
1525 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1526 if (get_atomic_type_size(possible_kinds[i]) == size) {
1527 kind = possible_kinds[i];
1537 * Hash the given type and return the "singleton" version
1540 type_t *identify_new_type(type_t *type)
1542 type_t *result = typehash_insert(type);
1543 if (result != type) {
1544 obstack_free(&type_obst, type);
1550 * Creates a new atomic type.
1552 * @param akind The kind of the atomic type.
1553 * @param qualifiers Type qualifiers for the new type.
1555 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1557 type_t *const type = allocate_type_zero(TYPE_ATOMIC);
1558 type->base.qualifiers = qualifiers;
1559 type->atomic.akind = akind;
1561 return identify_new_type(type);
1565 * Creates a new complex type.
1567 * @param akind The kind of the atomic type.
1568 * @param qualifiers Type qualifiers for the new type.
1570 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1572 type_t *const type = allocate_type_zero(TYPE_COMPLEX);
1573 type->base.qualifiers = qualifiers;
1574 type->complex.akind = akind;
1576 return identify_new_type(type);
1580 * Creates a new imaginary type.
1582 * @param akind The kind of the atomic type.
1583 * @param qualifiers Type qualifiers for the new type.
1585 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1587 type_t *const type = allocate_type_zero(TYPE_IMAGINARY);
1588 type->base.qualifiers = qualifiers;
1589 type->imaginary.akind = akind;
1591 return identify_new_type(type);
1595 * Creates a new pointer type.
1597 * @param points_to The points-to type for the new type.
1598 * @param qualifiers Type qualifiers for the new type.
1600 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1602 type_t *const type = allocate_type_zero(TYPE_POINTER);
1603 type->base.qualifiers = qualifiers;
1604 type->pointer.points_to = points_to;
1605 type->pointer.base_variable = NULL;
1607 return identify_new_type(type);
1611 * Creates a new reference type.
1613 * @param refers_to The referred-to type for the new type.
1615 type_t *make_reference_type(type_t *refers_to)
1617 type_t *const type = allocate_type_zero(TYPE_REFERENCE);
1618 type->base.qualifiers = TYPE_QUALIFIER_NONE;
1619 type->reference.refers_to = refers_to;
1621 return identify_new_type(type);
1625 * Creates a new based pointer type.
1627 * @param points_to The points-to type for the new type.
1628 * @param qualifiers Type qualifiers for the new type.
1629 * @param variable The based variable
1631 type_t *make_based_pointer_type(type_t *points_to,
1632 type_qualifiers_t qualifiers, variable_t *variable)
1634 type_t *const type = allocate_type_zero(TYPE_POINTER);
1635 type->base.qualifiers = qualifiers;
1636 type->pointer.points_to = points_to;
1637 type->pointer.base_variable = variable;
1639 return identify_new_type(type);
1643 type_t *make_array_type(type_t *element_type, size_t size,
1644 type_qualifiers_t qualifiers)
1646 type_t *const type = allocate_type_zero(TYPE_ARRAY);
1647 type->base.qualifiers = qualifiers;
1648 type->array.element_type = element_type;
1649 type->array.size = size;
1650 type->array.size_constant = true;
1652 return identify_new_type(type);
1655 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1656 il_alignment_t *struct_alignment,
1657 bool packed, entity_t *first)
1659 il_size_t offset = *struct_offset;
1660 il_alignment_t alignment = *struct_alignment;
1661 size_t bit_offset = 0;
1664 for (member = first; member != NULL; member = member->base.next) {
1665 if (member->kind != ENTITY_COMPOUND_MEMBER)
1668 type_t *type = member->declaration.type;
1669 if (type->kind != TYPE_BITFIELD)
1672 type_t *base_type = skip_typeref(type->bitfield.base_type);
1673 il_alignment_t base_alignment = get_type_alignment(base_type);
1674 il_alignment_t alignment_mask = base_alignment-1;
1675 if (base_alignment > alignment)
1676 alignment = base_alignment;
1678 size_t bit_size = type->bitfield.bit_size;
1680 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1681 offset &= ~alignment_mask;
1682 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1684 if (bit_offset + bit_size > base_size || bit_size == 0) {
1685 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1686 offset = (offset + base_alignment-1) & ~alignment_mask;
1691 if (byte_order_big_endian) {
1692 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1693 member->compound_member.offset = offset & ~alignment_mask;
1694 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1696 member->compound_member.offset = offset;
1697 member->compound_member.bit_offset = bit_offset;
1700 bit_offset += bit_size;
1701 offset += bit_offset / BITS_PER_BYTE;
1702 bit_offset %= BITS_PER_BYTE;
1708 *struct_offset = offset;
1709 *struct_alignment = alignment;
1713 void layout_struct_type(compound_type_t *type)
1715 assert(type->compound != NULL);
1717 compound_t *compound = type->compound;
1718 if (!compound->complete)
1720 if (type->compound->layouted)
1723 il_size_t offset = 0;
1724 il_alignment_t alignment = compound->alignment;
1725 bool need_pad = false;
1727 entity_t *entry = compound->members.entities;
1728 while (entry != NULL) {
1729 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1730 entry = entry->base.next;
1734 type_t *m_type = entry->declaration.type;
1735 type_t *skipped = skip_typeref(m_type);
1736 if (! is_type_valid(skipped)) {
1737 entry = entry->base.next;
1741 if (skipped->kind == TYPE_BITFIELD) {
1742 entry = pack_bitfield_members(&offset, &alignment,
1743 compound->packed, entry);
1747 il_alignment_t m_alignment = get_type_alignment(m_type);
1748 if (m_alignment > alignment)
1749 alignment = m_alignment;
1751 if (!compound->packed) {
1752 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1754 if (new_offset > offset) {
1756 offset = new_offset;
1760 entry->compound_member.offset = offset;
1761 offset += get_type_size(m_type);
1763 entry = entry->base.next;
1766 if (!compound->packed) {
1767 il_size_t new_offset = (offset + alignment-1) & -alignment;
1768 if (new_offset > offset) {
1770 offset = new_offset;
1774 source_position_t const *const pos = &compound->base.source_position;
1776 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1777 } else if (compound->packed) {
1778 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1781 compound->size = offset;
1782 compound->alignment = alignment;
1783 compound->layouted = true;
1786 void layout_union_type(compound_type_t *type)
1788 assert(type->compound != NULL);
1790 compound_t *compound = type->compound;
1791 if (! compound->complete)
1795 il_alignment_t alignment = compound->alignment;
1797 entity_t *entry = compound->members.entities;
1798 for (; entry != NULL; entry = entry->base.next) {
1799 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1802 type_t *m_type = entry->declaration.type;
1803 if (! is_type_valid(skip_typeref(m_type)))
1806 entry->compound_member.offset = 0;
1807 il_size_t m_size = get_type_size(m_type);
1810 il_alignment_t m_alignment = get_type_alignment(m_type);
1811 if (m_alignment > alignment)
1812 alignment = m_alignment;
1814 size = (size + alignment - 1) & -alignment;
1816 compound->size = size;
1817 compound->alignment = alignment;
1820 function_parameter_t *allocate_parameter(type_t *const type)
1822 function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
1823 memset(param, 0, sizeof(*param));
1828 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1829 type_t *argument_type2)
1831 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1832 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1833 parameter1->next = parameter2;
1835 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1836 type->function.return_type = return_type;
1837 type->function.parameters = parameter1;
1838 type->function.linkage = LINKAGE_C;
1840 return identify_new_type(type);
1843 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1845 function_parameter_t *const parameter = allocate_parameter(argument_type);
1847 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1848 type->function.return_type = return_type;
1849 type->function.parameters = parameter;
1850 type->function.linkage = LINKAGE_C;
1852 return identify_new_type(type);
1855 type_t *make_function_1_type_variadic(type_t *return_type,
1856 type_t *argument_type)
1858 function_parameter_t *const parameter = allocate_parameter(argument_type);
1860 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1861 type->function.return_type = return_type;
1862 type->function.parameters = parameter;
1863 type->function.variadic = true;
1864 type->function.linkage = LINKAGE_C;
1866 return identify_new_type(type);
1869 type_t *make_function_0_type(type_t *return_type)
1871 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1872 type->function.return_type = return_type;
1873 type->function.parameters = NULL;
1874 type->function.linkage = LINKAGE_C;
1876 return identify_new_type(type);
1879 type_t *make_function_type(type_t *return_type, int n_types,
1880 type_t *const *argument_types,
1881 decl_modifiers_t modifiers)
1883 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1884 type->function.return_type = return_type;
1885 type->function.modifiers |= modifiers;
1886 type->function.linkage = LINKAGE_C;
1888 function_parameter_t **anchor = &type->function.parameters;
1889 for (int i = 0; i < n_types; ++i) {
1890 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1891 *anchor = parameter;
1892 anchor = ¶meter->next;
1895 return identify_new_type(type);
1899 * Debug helper. Prints the given type to stdout.
1901 static __attribute__((unused))
1902 void dbg_type(const type_t *type)
1904 print_to_file(stderr);