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 struct obstack *type_obst = &_type_obst;
42 static bool print_implicit_array_size = false;
44 static void intern_print_type_pre(const type_t *type);
45 static void intern_print_type_post(const type_t *type);
47 typedef struct atomic_type_properties_t atomic_type_properties_t;
48 struct atomic_type_properties_t {
49 unsigned size; /**< type size in bytes */
50 unsigned alignment; /**< type alignment in bytes */
51 unsigned flags; /**< type flags from atomic_type_flag_t */
55 * Returns the size of a type node.
57 * @param kind the type kind
59 static size_t get_type_struct_size(type_kind_t kind)
61 static const size_t sizes[] = {
62 [TYPE_ATOMIC] = sizeof(atomic_type_t),
63 [TYPE_COMPLEX] = sizeof(complex_type_t),
64 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
65 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
66 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
67 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
68 [TYPE_ENUM] = sizeof(enum_type_t),
69 [TYPE_FUNCTION] = sizeof(function_type_t),
70 [TYPE_POINTER] = sizeof(pointer_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 size = get_type_struct_size(kind);
84 type_t *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 void init_types(void)
196 obstack_init(type_obst);
198 atomic_type_properties_t *props = atomic_type_properties;
200 if (char_is_signed) {
201 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
204 unsigned int_size = machine_size < 32 ? 2 : 4;
205 /* long is always 32bit on windows */
206 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
207 unsigned llong_size = machine_size < 32 ? 4 : 8;
209 props[ATOMIC_TYPE_INT].size = int_size;
210 props[ATOMIC_TYPE_INT].alignment = int_size;
211 props[ATOMIC_TYPE_UINT].size = int_size;
212 props[ATOMIC_TYPE_UINT].alignment = int_size;
213 props[ATOMIC_TYPE_LONG].size = long_size;
214 props[ATOMIC_TYPE_LONG].alignment = long_size;
215 props[ATOMIC_TYPE_ULONG].size = long_size;
216 props[ATOMIC_TYPE_ULONG].alignment = long_size;
217 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
218 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
219 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
220 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
222 /* TODO: backend specific, need a way to query the backend for this.
223 * The following are good settings for x86 */
224 if (machine_size <= 32) {
225 props[ATOMIC_TYPE_FLOAT].alignment = 4;
226 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
227 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
228 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
229 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
231 props[ATOMIC_TYPE_FLOAT].alignment = 4;
232 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
233 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
234 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
235 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
238 if (long_double_size > 0) {
239 props[ATOMIC_TYPE_LONG_DOUBLE].size = long_double_size;
240 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = long_double_size;
242 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
245 /* TODO: make this configurable for platforms which do not use byte sized
247 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
249 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
252 void exit_types(void)
254 obstack_free(type_obst, NULL);
257 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
259 size_t sep = q & QUAL_SEP_START ? 0 : 1;
260 if (qualifiers & TYPE_QUALIFIER_CONST) {
261 print_string(" const" + sep);
264 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
265 print_string(" volatile" + sep);
268 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
269 print_string(" restrict" + sep);
272 if (sep == 0 && q & QUAL_SEP_END)
276 const char *get_atomic_kind_name(atomic_type_kind_t kind)
279 case ATOMIC_TYPE_INVALID: break;
280 case ATOMIC_TYPE_VOID: return "void";
281 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
282 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
283 case ATOMIC_TYPE_CHAR: return "char";
284 case ATOMIC_TYPE_SCHAR: return "signed char";
285 case ATOMIC_TYPE_UCHAR: return "unsigned char";
286 case ATOMIC_TYPE_INT: return "int";
287 case ATOMIC_TYPE_UINT: return "unsigned int";
288 case ATOMIC_TYPE_SHORT: return "short";
289 case ATOMIC_TYPE_USHORT: return "unsigned short";
290 case ATOMIC_TYPE_LONG: return "long";
291 case ATOMIC_TYPE_ULONG: return "unsigned long";
292 case ATOMIC_TYPE_LONGLONG: return "long long";
293 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
294 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
295 case ATOMIC_TYPE_FLOAT: return "float";
296 case ATOMIC_TYPE_DOUBLE: return "double";
298 return "INVALIDATOMIC";
302 * Prints the name of an atomic type kinds.
304 * @param kind The type kind.
306 static void print_atomic_kinds(atomic_type_kind_t kind)
308 const char *s = get_atomic_kind_name(kind);
313 * Prints the name of an atomic type.
315 * @param type The type.
317 static void print_atomic_type(const atomic_type_t *type)
319 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
320 print_atomic_kinds(type->akind);
324 * Prints the name of a complex type.
326 * @param type The type.
328 static void print_complex_type(const complex_type_t *type)
330 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
331 print_string("_Complex");
332 print_atomic_kinds(type->akind);
336 * Prints the name of an imaginary type.
338 * @param type The type.
340 static void print_imaginary_type(const imaginary_type_t *type)
342 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
343 print_string("_Imaginary ");
344 print_atomic_kinds(type->akind);
348 * Print the first part (the prefix) of a type.
350 * @param type The type to print.
352 static void print_function_type_pre(const function_type_t *type)
354 switch (type->linkage) {
355 case LINKAGE_INVALID:
360 print_string("extern \"C\" ");
364 if (!(c_mode & _CXX))
365 print_string("extern \"C++\" ");
369 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
371 intern_print_type_pre(type->return_type);
373 cc_kind_t cc = type->calling_convention;
376 case CC_CDECL: print_string(" __cdecl"); break;
377 case CC_STDCALL: print_string(" __stdcall"); break;
378 case CC_FASTCALL: print_string(" __fastcall"); break;
379 case CC_THISCALL: print_string(" __thiscall"); break;
381 if (default_calling_convention != CC_CDECL) {
382 /* show the default calling convention if its not cdecl */
383 cc = default_calling_convention;
391 * Print the second part (the postfix) of a type.
393 * @param type The type to print.
395 static void print_function_type_post(const function_type_t *type,
396 const scope_t *parameters)
400 if (parameters == NULL) {
401 function_parameter_t *parameter = type->parameters;
402 for( ; parameter != NULL; parameter = parameter->next) {
408 print_type(parameter->type);
411 entity_t *parameter = parameters->entities;
412 for (; parameter != NULL; parameter = parameter->base.next) {
413 if (parameter->kind != ENTITY_PARAMETER)
421 const type_t *const type = parameter->declaration.type;
423 print_string(parameter->base.symbol->string);
425 print_type_ext(type, parameter->base.symbol, NULL);
429 if (type->variadic) {
437 if (first && !type->unspecified_parameters) {
438 print_string("void");
442 intern_print_type_post(type->return_type);
446 * Prints the prefix part of a pointer type.
448 * @param type The pointer type.
450 static void print_pointer_type_pre(const pointer_type_t *type)
452 type_t const *const points_to = type->points_to;
453 intern_print_type_pre(points_to);
454 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
456 variable_t *const variable = type->base_variable;
457 if (variable != NULL) {
458 print_string(" __based(");
459 print_string(variable->base.base.symbol->string);
463 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
467 * Prints the postfix part of a pointer type.
469 * @param type The pointer type.
471 static void print_pointer_type_post(const pointer_type_t *type)
473 type_t const *const points_to = type->points_to;
474 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
476 intern_print_type_post(points_to);
480 * Prints the prefix part of a reference type.
482 * @param type The reference type.
484 static void print_reference_type_pre(const reference_type_t *type)
486 type_t const *const refers_to = type->refers_to;
487 intern_print_type_pre(refers_to);
488 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
494 * Prints the postfix part of a reference type.
496 * @param type The reference type.
498 static void print_reference_type_post(const reference_type_t *type)
500 type_t const *const refers_to = type->refers_to;
501 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
503 intern_print_type_post(refers_to);
507 * Prints the prefix part of an array type.
509 * @param type The array type.
511 static void print_array_type_pre(const array_type_t *type)
513 intern_print_type_pre(type->element_type);
517 * Prints the postfix part of an array type.
519 * @param type The array type.
521 static void print_array_type_post(const array_type_t *type)
524 if (type->is_static) {
525 print_string("static ");
527 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
528 if (type->size_expression != NULL
529 && (print_implicit_array_size || !type->has_implicit_size)) {
530 print_expression(type->size_expression);
533 intern_print_type_post(type->element_type);
537 * Prints the postfix part of a bitfield type.
539 * @param type The array type.
541 static void print_bitfield_type_post(const bitfield_type_t *type)
544 print_expression(type->size_expression);
545 intern_print_type_post(type->base_type);
549 * Prints an enum definition.
551 * @param declaration The enum's type declaration.
553 void print_enum_definition(const enum_t *enume)
559 entity_t *entry = enume->base.next;
560 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
561 entry = entry->base.next) {
564 print_string(entry->base.symbol->string);
565 if (entry->enum_value.value != NULL) {
568 /* skip the implicit cast */
569 expression_t *expression = entry->enum_value.value;
570 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
571 expression = expression->unary.value;
573 print_expression(expression);
584 * Prints an enum type.
586 * @param type The enum type.
588 static void print_type_enum(const enum_type_t *type)
590 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
591 print_string("enum ");
593 enum_t *enume = type->enume;
594 symbol_t *symbol = enume->base.symbol;
595 if (symbol != NULL) {
596 print_string(symbol->string);
598 print_enum_definition(enume);
603 * Print the compound part of a compound type.
605 void print_compound_definition(const compound_t *compound)
610 entity_t *entity = compound->members.entities;
611 for( ; entity != NULL; entity = entity->base.next) {
612 if (entity->kind != ENTITY_COMPOUND_MEMBER)
616 print_entity(entity);
623 if (compound->modifiers & DM_TRANSPARENT_UNION) {
624 print_string("__attribute__((__transparent_union__))");
629 * Prints a compound type.
631 * @param type The compound type.
633 static void print_compound_type(const compound_type_t *type)
635 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
637 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
638 print_string("struct ");
640 assert(type->base.kind == TYPE_COMPOUND_UNION);
641 print_string("union ");
644 compound_t *compound = type->compound;
645 symbol_t *symbol = compound->base.symbol;
646 if (symbol != NULL) {
647 print_string(symbol->string);
649 print_compound_definition(compound);
654 * Prints the prefix part of a typedef type.
656 * @param type The typedef type.
658 static void print_typedef_type_pre(const typedef_type_t *const type)
660 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
661 print_string(type->typedefe->base.symbol->string);
665 * Prints the prefix part of a typeof type.
667 * @param type The typeof type.
669 static void print_typeof_type_pre(const typeof_type_t *const type)
671 print_string("typeof(");
672 if (type->expression != NULL) {
673 print_expression(type->expression);
675 print_type(type->typeof_type);
681 * Prints the prefix part of a type.
683 * @param type The type.
685 static void intern_print_type_pre(const type_t *const type)
689 print_string("<error>");
692 print_string("<invalid>");
695 print_type_enum(&type->enumt);
698 print_atomic_type(&type->atomic);
701 print_complex_type(&type->complex);
704 print_imaginary_type(&type->imaginary);
706 case TYPE_COMPOUND_STRUCT:
707 case TYPE_COMPOUND_UNION:
708 print_compound_type(&type->compound);
711 print_function_type_pre(&type->function);
714 print_pointer_type_pre(&type->pointer);
717 print_reference_type_pre(&type->reference);
720 intern_print_type_pre(type->bitfield.base_type);
723 print_array_type_pre(&type->array);
726 print_typedef_type_pre(&type->typedeft);
729 print_typeof_type_pre(&type->typeoft);
732 print_string("unknown");
736 * Prints the postfix part of a type.
738 * @param type The type.
740 static void intern_print_type_post(const type_t *const type)
744 print_function_type_post(&type->function, NULL);
747 print_pointer_type_post(&type->pointer);
750 print_reference_type_post(&type->reference);
753 print_array_type_post(&type->array);
756 print_bitfield_type_post(&type->bitfield);
764 case TYPE_COMPOUND_STRUCT:
765 case TYPE_COMPOUND_UNION:
775 * @param type The type.
777 void print_type(const type_t *const type)
779 print_type_ext(type, NULL, NULL);
782 void print_type_ext(const type_t *const type, const symbol_t *symbol,
783 const scope_t *parameters)
785 intern_print_type_pre(type);
786 if (symbol != NULL) {
788 print_string(symbol->string);
790 if (type->kind == TYPE_FUNCTION) {
791 print_function_type_post(&type->function, parameters);
793 intern_print_type_post(type);
800 * @param type The type to copy.
801 * @return A copy of the type.
803 * @note This does not produce a deep copy!
805 type_t *duplicate_type(const type_t *type)
807 size_t size = get_type_struct_size(type->kind);
809 type_t *copy = obstack_alloc(type_obst, size);
810 memcpy(copy, type, size);
811 copy->base.firm_type = NULL;
817 * Returns the unqualified type of a given type.
819 * @param type The type.
820 * @returns The unqualified type.
822 type_t *get_unqualified_type(type_t *type)
824 assert(!is_typeref(type));
826 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
829 type_t *unqualified_type = duplicate_type(type);
830 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
832 return identify_new_type(unqualified_type);
835 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
837 type_t *type = skip_typeref(orig_type);
840 if (is_type_array(type)) {
841 /* For array types the element type has to be adjusted */
842 type_t *element_type = type->array.element_type;
843 type_t *qual_element_type = get_qualified_type(element_type, qual);
845 if (qual_element_type == element_type)
848 copy = duplicate_type(type);
849 copy->array.element_type = qual_element_type;
850 } else if (is_type_valid(type)) {
851 if ((type->base.qualifiers & qual) == qual)
854 copy = duplicate_type(type);
855 copy->base.qualifiers |= qual;
860 return identify_new_type(copy);
864 * Check if a type is valid.
866 * @param type The type to check.
867 * @return true if type represents a valid type.
869 bool type_valid(const type_t *type)
871 return type->kind != TYPE_INVALID;
874 static bool test_atomic_type_flag(atomic_type_kind_t kind,
875 atomic_type_flag_t flag)
877 assert(kind <= ATOMIC_TYPE_LAST);
878 return (atomic_type_properties[kind].flags & flag) != 0;
882 * Returns true if the given type is an integer type.
884 * @param type The type to check.
885 * @return True if type is an integer type.
887 bool is_type_integer(const type_t *type)
889 assert(!is_typeref(type));
891 if (type->kind == TYPE_ENUM)
893 if (type->kind == TYPE_BITFIELD)
896 if (type->kind != TYPE_ATOMIC)
899 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
903 * Returns true if the given type is an enum type.
905 * @param type The type to check.
906 * @return True if type is an enum type.
908 bool is_type_enum(const type_t *type)
910 assert(!is_typeref(type));
911 return type->kind == TYPE_ENUM;
915 * Returns true if the given type is an floating point type.
917 * @param type The type to check.
918 * @return True if type is a floating point type.
920 bool is_type_float(const type_t *type)
922 assert(!is_typeref(type));
924 if (type->kind != TYPE_ATOMIC)
927 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
931 * Returns true if the given type is an complex type.
933 * @param type The type to check.
934 * @return True if type is a complex type.
936 bool is_type_complex(const type_t *type)
938 assert(!is_typeref(type));
940 if (type->kind != TYPE_ATOMIC)
943 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
947 * Returns true if the given type is a signed type.
949 * @param type The type to check.
950 * @return True if type is a signed type.
952 bool is_type_signed(const type_t *type)
954 assert(!is_typeref(type));
956 /* enum types are int for now */
957 if (type->kind == TYPE_ENUM)
959 if (type->kind == TYPE_BITFIELD)
960 return is_type_signed(type->bitfield.base_type);
962 if (type->kind != TYPE_ATOMIC)
965 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
969 * Returns true if the given type represents an arithmetic type.
971 * @param type The type to check.
972 * @return True if type represents an arithmetic type.
974 bool is_type_arithmetic(const type_t *type)
976 assert(!is_typeref(type));
983 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
985 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
987 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
994 * Returns true if the given type is an integer or float type.
996 * @param type The type to check.
997 * @return True if type is an integer or float type.
999 bool is_type_real(const type_t *type)
1002 return is_type_integer(type) || is_type_float(type);
1006 * Returns true if the given type represents a scalar type.
1008 * @param type The type to check.
1009 * @return True if type represents a scalar type.
1011 bool is_type_scalar(const type_t *type)
1013 assert(!is_typeref(type));
1015 if (type->kind == TYPE_POINTER)
1018 return is_type_arithmetic(type);
1022 * Check if a given type is incomplete.
1024 * @param type The type to check.
1025 * @return True if the given type is incomplete (ie. just forward).
1027 bool is_type_incomplete(const type_t *type)
1029 assert(!is_typeref(type));
1031 switch(type->kind) {
1032 case TYPE_COMPOUND_STRUCT:
1033 case TYPE_COMPOUND_UNION: {
1034 const compound_type_t *compound_type = &type->compound;
1035 return !compound_type->compound->complete;
1041 return type->array.size_expression == NULL
1042 && !type->array.size_constant;
1045 return type->atomic.akind == ATOMIC_TYPE_VOID;
1048 return type->complex.akind == ATOMIC_TYPE_VOID;
1050 case TYPE_IMAGINARY:
1051 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1056 case TYPE_REFERENCE:
1062 panic("is_type_incomplete called without typerefs skipped");
1067 panic("invalid type found");
1070 bool is_type_object(const type_t *type)
1072 return !is_type_function(type) && !is_type_incomplete(type);
1076 * Check if two function types are compatible.
1078 static bool function_types_compatible(const function_type_t *func1,
1079 const function_type_t *func2)
1081 const type_t* const ret1 = skip_typeref(func1->return_type);
1082 const type_t* const ret2 = skip_typeref(func2->return_type);
1083 if (!types_compatible(ret1, ret2))
1086 if (func1->linkage != func2->linkage)
1089 cc_kind_t cc1 = func1->calling_convention;
1090 if (cc1 == CC_DEFAULT)
1091 cc1 = default_calling_convention;
1092 cc_kind_t cc2 = func2->calling_convention;
1093 if (cc2 == CC_DEFAULT)
1094 cc2 = default_calling_convention;
1099 if (func1->variadic != func2->variadic)
1102 /* can parameters be compared? */
1103 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1104 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1107 /* TODO: handling of unspecified parameters not correct yet */
1109 /* all argument types must be compatible */
1110 function_parameter_t *parameter1 = func1->parameters;
1111 function_parameter_t *parameter2 = func2->parameters;
1112 for ( ; parameter1 != NULL && parameter2 != NULL;
1113 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1114 type_t *parameter1_type = skip_typeref(parameter1->type);
1115 type_t *parameter2_type = skip_typeref(parameter2->type);
1117 parameter1_type = get_unqualified_type(parameter1_type);
1118 parameter2_type = get_unqualified_type(parameter2_type);
1120 if (!types_compatible(parameter1_type, parameter2_type))
1123 /* same number of arguments? */
1124 if (parameter1 != NULL || parameter2 != NULL)
1131 * Check if two array types are compatible.
1133 static bool array_types_compatible(const array_type_t *array1,
1134 const array_type_t *array2)
1136 type_t *element_type1 = skip_typeref(array1->element_type);
1137 type_t *element_type2 = skip_typeref(array2->element_type);
1138 if (!types_compatible(element_type1, element_type2))
1141 if (!array1->size_constant || !array2->size_constant)
1144 return array1->size == array2->size;
1148 * Check if two types are compatible.
1150 bool types_compatible(const type_t *type1, const type_t *type2)
1152 assert(!is_typeref(type1));
1153 assert(!is_typeref(type2));
1155 /* shortcut: the same type is always compatible */
1159 if (!is_type_valid(type1) || !is_type_valid(type2))
1162 if (type1->base.qualifiers != type2->base.qualifiers)
1164 if (type1->kind != type2->kind)
1167 switch (type1->kind) {
1169 return function_types_compatible(&type1->function, &type2->function);
1171 return type1->atomic.akind == type2->atomic.akind;
1173 return type1->complex.akind == type2->complex.akind;
1174 case TYPE_IMAGINARY:
1175 return type1->imaginary.akind == type2->imaginary.akind;
1177 return array_types_compatible(&type1->array, &type2->array);
1179 case TYPE_POINTER: {
1180 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1181 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1182 return types_compatible(to1, to2);
1185 case TYPE_REFERENCE: {
1186 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1187 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1188 return types_compatible(to1, to2);
1191 case TYPE_COMPOUND_STRUCT:
1192 case TYPE_COMPOUND_UNION: {
1198 /* TODO: not implemented */
1202 /* not sure if this makes sense or is even needed, implement it if you
1203 * really need it! */
1204 panic("type compatibility check for bitfield type");
1207 /* Hmm, the error type should be compatible to all other types */
1210 panic("invalid type found in compatible types");
1213 panic("typerefs not skipped in compatible types?!?");
1216 /* TODO: incomplete */
1221 * Skip all typerefs and return the underlying type.
1223 type_t *skip_typeref(type_t *type)
1225 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1228 switch (type->kind) {
1231 case TYPE_TYPEDEF: {
1232 qualifiers |= type->base.qualifiers;
1234 const typedef_type_t *typedef_type = &type->typedeft;
1235 if (typedef_type->resolved_type != NULL) {
1236 type = typedef_type->resolved_type;
1239 type = typedef_type->typedefe->type;
1243 qualifiers |= type->base.qualifiers;
1244 type = type->typeoft.typeof_type;
1252 if (qualifiers != TYPE_QUALIFIER_NONE) {
1253 type_t *const copy = duplicate_type(type);
1255 /* for const with typedefed array type the element type has to be
1257 if (is_type_array(copy)) {
1258 type_t *element_type = copy->array.element_type;
1259 element_type = duplicate_type(element_type);
1260 element_type->base.qualifiers |= qualifiers;
1261 copy->array.element_type = element_type;
1263 copy->base.qualifiers |= qualifiers;
1266 type = identify_new_type(copy);
1272 unsigned get_type_size(type_t *type)
1274 switch (type->kind) {
1280 return get_atomic_type_size(type->atomic.akind);
1282 return get_atomic_type_size(type->complex.akind) * 2;
1283 case TYPE_IMAGINARY:
1284 return get_atomic_type_size(type->imaginary.akind);
1285 case TYPE_COMPOUND_UNION:
1286 layout_union_type(&type->compound);
1287 return type->compound.compound->size;
1288 case TYPE_COMPOUND_STRUCT:
1289 layout_struct_type(&type->compound);
1290 return type->compound.compound->size;
1292 return get_atomic_type_size(type->enumt.akind);
1294 return 0; /* non-const (but "address-const") */
1295 case TYPE_REFERENCE:
1297 /* TODO: make configurable by backend */
1300 /* TODO: correct if element_type is aligned? */
1301 il_size_t element_size = get_type_size(type->array.element_type);
1302 return type->array.size * element_size;
1307 return get_type_size(type->typedeft.typedefe->type);
1309 if (type->typeoft.typeof_type) {
1310 return get_type_size(type->typeoft.typeof_type);
1312 return get_type_size(type->typeoft.expression->base.type);
1315 panic("invalid type in get_type_size");
1318 unsigned get_type_alignment(type_t *type)
1320 switch (type->kind) {
1326 return get_atomic_type_alignment(type->atomic.akind);
1328 return get_atomic_type_alignment(type->complex.akind);
1329 case TYPE_IMAGINARY:
1330 return get_atomic_type_alignment(type->imaginary.akind);
1331 case TYPE_COMPOUND_UNION:
1332 layout_union_type(&type->compound);
1333 return type->compound.compound->alignment;
1334 case TYPE_COMPOUND_STRUCT:
1335 layout_struct_type(&type->compound);
1336 return type->compound.compound->alignment;
1338 return get_atomic_type_alignment(type->enumt.akind);
1340 /* what is correct here? */
1342 case TYPE_REFERENCE:
1344 /* TODO: make configurable by backend */
1347 return get_type_alignment(type->array.element_type);
1350 case TYPE_TYPEDEF: {
1351 il_alignment_t alignment
1352 = get_type_alignment(type->typedeft.typedefe->type);
1353 if (type->typedeft.typedefe->alignment > alignment)
1354 alignment = type->typedeft.typedefe->alignment;
1359 if (type->typeoft.typeof_type) {
1360 return get_type_alignment(type->typeoft.typeof_type);
1362 return get_type_alignment(type->typeoft.expression->base.type);
1365 panic("invalid type in get_type_alignment");
1368 decl_modifiers_t get_type_modifiers(const type_t *type)
1370 switch(type->kind) {
1374 case TYPE_COMPOUND_STRUCT:
1375 case TYPE_COMPOUND_UNION:
1376 return type->compound.compound->modifiers;
1378 return type->function.modifiers;
1382 case TYPE_IMAGINARY:
1383 case TYPE_REFERENCE:
1388 case TYPE_TYPEDEF: {
1389 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1390 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1394 if (type->typeoft.typeof_type) {
1395 return get_type_modifiers(type->typeoft.typeof_type);
1397 return get_type_modifiers(type->typeoft.expression->base.type);
1400 panic("invalid type found in get_type_modifiers");
1403 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1405 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1408 switch (type->base.kind) {
1410 return TYPE_QUALIFIER_NONE;
1412 qualifiers |= type->base.qualifiers;
1413 const typedef_type_t *typedef_type = &type->typedeft;
1414 if (typedef_type->resolved_type != NULL)
1415 type = typedef_type->resolved_type;
1417 type = typedef_type->typedefe->type;
1420 type = type->typeoft.typeof_type;
1423 if (skip_array_type) {
1424 type = type->array.element_type;
1433 return type->base.qualifiers | qualifiers;
1436 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1438 assert(kind <= ATOMIC_TYPE_LAST);
1439 return atomic_type_properties[kind].size;
1442 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1444 assert(kind <= ATOMIC_TYPE_LAST);
1445 return atomic_type_properties[kind].alignment;
1448 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1450 assert(kind <= ATOMIC_TYPE_LAST);
1451 return atomic_type_properties[kind].flags;
1454 atomic_type_kind_t get_intptr_kind(void)
1456 if (machine_size <= 32)
1457 return ATOMIC_TYPE_INT;
1458 else if (machine_size <= 64)
1459 return ATOMIC_TYPE_LONG;
1461 return ATOMIC_TYPE_LONGLONG;
1464 atomic_type_kind_t get_uintptr_kind(void)
1466 if (machine_size <= 32)
1467 return ATOMIC_TYPE_UINT;
1468 else if (machine_size <= 64)
1469 return ATOMIC_TYPE_ULONG;
1471 return ATOMIC_TYPE_ULONGLONG;
1475 * Find the atomic type kind representing a given size (signed).
1477 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1479 static atomic_type_kind_t kinds[32];
1482 atomic_type_kind_t kind = kinds[size];
1483 if (kind == ATOMIC_TYPE_INVALID) {
1484 static const atomic_type_kind_t possible_kinds[] = {
1489 ATOMIC_TYPE_LONGLONG
1491 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1492 if (get_atomic_type_size(possible_kinds[i]) == size) {
1493 kind = possible_kinds[i];
1503 * Find the atomic type kind representing a given size (signed).
1505 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1507 static atomic_type_kind_t kinds[32];
1510 atomic_type_kind_t kind = kinds[size];
1511 if (kind == ATOMIC_TYPE_INVALID) {
1512 static const atomic_type_kind_t possible_kinds[] = {
1517 ATOMIC_TYPE_ULONGLONG
1519 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1520 if (get_atomic_type_size(possible_kinds[i]) == size) {
1521 kind = possible_kinds[i];
1531 * Hash the given type and return the "singleton" version
1534 type_t *identify_new_type(type_t *type)
1536 type_t *result = typehash_insert(type);
1537 if (result != type) {
1538 obstack_free(type_obst, type);
1544 * Creates a new atomic type.
1546 * @param akind The kind of the atomic type.
1547 * @param qualifiers Type qualifiers for the new type.
1549 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1551 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1552 memset(type, 0, sizeof(atomic_type_t));
1554 type->kind = TYPE_ATOMIC;
1555 type->base.qualifiers = qualifiers;
1556 type->atomic.akind = akind;
1558 return identify_new_type(type);
1562 * Creates a new complex type.
1564 * @param akind The kind of the atomic type.
1565 * @param qualifiers Type qualifiers for the new type.
1567 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1569 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1570 memset(type, 0, sizeof(complex_type_t));
1572 type->kind = 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 *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1588 memset(type, 0, sizeof(imaginary_type_t));
1590 type->kind = TYPE_IMAGINARY;
1591 type->base.qualifiers = qualifiers;
1592 type->imaginary.akind = akind;
1594 return identify_new_type(type);
1598 * Creates a new pointer type.
1600 * @param points_to The points-to type for the new type.
1601 * @param qualifiers Type qualifiers for the new type.
1603 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1605 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1606 memset(type, 0, sizeof(pointer_type_t));
1608 type->kind = TYPE_POINTER;
1609 type->base.qualifiers = qualifiers;
1610 type->pointer.points_to = points_to;
1611 type->pointer.base_variable = NULL;
1613 return identify_new_type(type);
1617 * Creates a new reference type.
1619 * @param refers_to The referred-to type for the new type.
1621 type_t *make_reference_type(type_t *refers_to)
1623 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1624 memset(type, 0, sizeof(reference_type_t));
1626 type->kind = TYPE_REFERENCE;
1627 type->base.qualifiers = 0;
1628 type->reference.refers_to = refers_to;
1630 return identify_new_type(type);
1634 * Creates a new based pointer type.
1636 * @param points_to The points-to type for the new type.
1637 * @param qualifiers Type qualifiers for the new type.
1638 * @param variable The based variable
1640 type_t *make_based_pointer_type(type_t *points_to,
1641 type_qualifiers_t qualifiers, variable_t *variable)
1643 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1644 memset(type, 0, sizeof(pointer_type_t));
1646 type->kind = TYPE_POINTER;
1647 type->base.qualifiers = qualifiers;
1648 type->pointer.points_to = points_to;
1649 type->pointer.base_variable = variable;
1651 return identify_new_type(type);
1655 type_t *make_array_type(type_t *element_type, size_t size,
1656 type_qualifiers_t qualifiers)
1658 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1659 memset(type, 0, sizeof(array_type_t));
1661 type->kind = TYPE_ARRAY;
1662 type->base.qualifiers = qualifiers;
1663 type->array.element_type = element_type;
1664 type->array.size = size;
1665 type->array.size_constant = true;
1667 return identify_new_type(type);
1670 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1671 il_alignment_t *struct_alignment,
1672 bool packed, entity_t *first)
1674 il_size_t offset = *struct_offset;
1675 il_alignment_t alignment = *struct_alignment;
1676 size_t bit_offset = 0;
1679 for (member = first; member != NULL; member = member->base.next) {
1680 if (member->kind != ENTITY_COMPOUND_MEMBER)
1683 type_t *type = member->declaration.type;
1684 if (type->kind != TYPE_BITFIELD)
1687 type_t *base_type = skip_typeref(type->bitfield.base_type);
1688 il_alignment_t base_alignment = get_type_alignment(base_type);
1689 il_alignment_t alignment_mask = base_alignment-1;
1690 if (base_alignment > alignment)
1691 alignment = base_alignment;
1693 size_t bit_size = type->bitfield.bit_size;
1695 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1696 offset &= ~alignment_mask;
1697 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1699 if (bit_offset + bit_size > base_size || bit_size == 0) {
1700 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1701 offset = (offset + base_alignment-1) & ~alignment_mask;
1706 if (byte_order_big_endian) {
1707 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1708 member->compound_member.offset = offset & ~alignment_mask;
1709 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1711 member->compound_member.offset = offset;
1712 member->compound_member.bit_offset = bit_offset;
1715 bit_offset += bit_size;
1716 offset += bit_offset / BITS_PER_BYTE;
1717 bit_offset %= BITS_PER_BYTE;
1723 *struct_offset = offset;
1724 *struct_alignment = alignment;
1728 void layout_struct_type(compound_type_t *type)
1730 assert(type->compound != NULL);
1732 compound_t *compound = type->compound;
1733 if (!compound->complete)
1735 if (type->compound->layouted)
1738 il_size_t offset = 0;
1739 il_alignment_t alignment = compound->alignment;
1740 bool need_pad = false;
1742 entity_t *entry = compound->members.entities;
1743 while (entry != NULL) {
1744 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1745 entry = entry->base.next;
1749 type_t *m_type = entry->declaration.type;
1750 type_t *skipped = skip_typeref(m_type);
1751 if (! is_type_valid(skipped)) {
1752 entry = entry->base.next;
1756 if (skipped->kind == TYPE_BITFIELD) {
1757 entry = pack_bitfield_members(&offset, &alignment,
1758 compound->packed, entry);
1762 il_alignment_t m_alignment = get_type_alignment(m_type);
1763 if (m_alignment > alignment)
1764 alignment = m_alignment;
1766 if (!compound->packed) {
1767 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1769 if (new_offset > offset) {
1771 offset = new_offset;
1775 entry->compound_member.offset = offset;
1776 offset += get_type_size(m_type);
1778 entry = entry->base.next;
1781 if (!compound->packed) {
1782 il_size_t new_offset = (offset + alignment-1) & -alignment;
1783 if (new_offset > offset) {
1785 offset = new_offset;
1790 if (warning.padded) {
1791 warningf(&compound->base.source_position, "'%T' needs padding",
1794 } else if (compound->packed && warning.packed) {
1795 warningf(&compound->base.source_position,
1796 "superfluous packed attribute on '%T'", type);
1799 compound->size = offset;
1800 compound->alignment = alignment;
1801 compound->layouted = true;
1804 void layout_union_type(compound_type_t *type)
1806 assert(type->compound != NULL);
1808 compound_t *compound = type->compound;
1809 if (! compound->complete)
1813 il_alignment_t alignment = compound->alignment;
1815 entity_t *entry = compound->members.entities;
1816 for (; entry != NULL; entry = entry->base.next) {
1817 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1820 type_t *m_type = entry->declaration.type;
1821 if (! is_type_valid(skip_typeref(m_type)))
1824 entry->compound_member.offset = 0;
1825 il_size_t m_size = get_type_size(m_type);
1828 il_alignment_t m_alignment = get_type_alignment(m_type);
1829 if (m_alignment > alignment)
1830 alignment = m_alignment;
1832 size = (size + alignment - 1) & -alignment;
1834 compound->size = size;
1835 compound->alignment = alignment;
1838 static function_parameter_t *allocate_parameter(type_t *const type)
1840 function_parameter_t *const param
1841 = obstack_alloc(type_obst, sizeof(*param));
1842 memset(param, 0, sizeof(*param));
1847 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1848 type_t *argument_type2)
1850 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1851 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1852 parameter1->next = parameter2;
1854 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1855 type->function.return_type = return_type;
1856 type->function.parameters = parameter1;
1857 type->function.linkage = LINKAGE_C;
1859 return identify_new_type(type);
1862 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1864 function_parameter_t *const parameter = allocate_parameter(argument_type);
1866 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1867 type->function.return_type = return_type;
1868 type->function.parameters = parameter;
1869 type->function.linkage = LINKAGE_C;
1871 return identify_new_type(type);
1874 type_t *make_function_1_type_variadic(type_t *return_type,
1875 type_t *argument_type)
1877 function_parameter_t *const parameter = allocate_parameter(argument_type);
1879 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1880 type->function.return_type = return_type;
1881 type->function.parameters = parameter;
1882 type->function.variadic = true;
1883 type->function.linkage = LINKAGE_C;
1885 return identify_new_type(type);
1888 type_t *make_function_0_type(type_t *return_type)
1890 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1891 type->function.return_type = return_type;
1892 type->function.parameters = NULL;
1893 type->function.linkage = LINKAGE_C;
1895 return identify_new_type(type);
1898 type_t *make_function_type(type_t *return_type, int n_types,
1899 type_t *const *argument_types,
1900 decl_modifiers_t modifiers)
1902 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1903 type->function.return_type = return_type;
1904 type->function.modifiers |= modifiers;
1905 type->function.linkage = LINKAGE_C;
1907 function_parameter_t *last = NULL;
1908 for (int i = 0; i < n_types; ++i) {
1909 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1911 type->function.parameters = parameter;
1913 last->next = parameter;
1918 return identify_new_type(type);
1922 * Debug helper. Prints the given type to stdout.
1924 static __attribute__((unused))
1925 void dbg_type(const type_t *type)
1927 print_to_file(stderr);