2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "type_hash.h"
30 #include "adt/error.h"
32 #include "lang_features.h"
34 #include "diagnostic.h"
36 #include "driver/firm_cmdline.h"
38 /** The default calling convention. */
39 cc_kind_t default_calling_convention = CC_CDECL;
41 static struct obstack _type_obst;
42 struct obstack *type_obst = &_type_obst;
43 static bool print_implicit_array_size = false;
45 static void intern_print_type_pre(const type_t *type);
46 static void intern_print_type_post(const type_t *type);
48 typedef struct atomic_type_properties_t atomic_type_properties_t;
49 struct atomic_type_properties_t {
50 unsigned size; /**< type size in bytes */
51 unsigned alignment; /**< type alignment in bytes */
52 unsigned flags; /**< type flags from atomic_type_flag_t */
56 * Returns the size of a type node.
58 * @param kind the type kind
60 static size_t get_type_struct_size(type_kind_t kind)
62 static const size_t sizes[] = {
63 [TYPE_ATOMIC] = sizeof(atomic_type_t),
64 [TYPE_COMPLEX] = sizeof(complex_type_t),
65 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
66 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
67 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
68 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
69 [TYPE_ENUM] = sizeof(enum_type_t),
70 [TYPE_FUNCTION] = sizeof(function_type_t),
71 [TYPE_POINTER] = sizeof(pointer_type_t),
72 [TYPE_ARRAY] = sizeof(array_type_t),
73 [TYPE_BUILTIN] = sizeof(builtin_type_t),
74 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
75 [TYPE_TYPEOF] = sizeof(typeof_type_t),
77 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
78 assert(kind <= TYPE_TYPEOF);
79 assert(sizes[kind] != 0);
83 type_t *allocate_type_zero(type_kind_t kind)
85 size_t size = get_type_struct_size(kind);
86 type_t *res = obstack_alloc(type_obst, size);
88 res->base.kind = kind;
94 * Properties of atomic types.
96 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
97 //ATOMIC_TYPE_INVALID = 0,
98 [ATOMIC_TYPE_VOID] = {
101 .flags = ATOMIC_TYPE_FLAG_NONE
103 [ATOMIC_TYPE_WCHAR_T] = {
104 .size = (unsigned)-1,
105 .alignment = (unsigned)-1,
106 /* signed flag will be set when known */
107 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
109 [ATOMIC_TYPE_CHAR] = {
112 /* signed flag will be set when known */
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
115 [ATOMIC_TYPE_SCHAR] = {
118 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
119 | ATOMIC_TYPE_FLAG_SIGNED,
121 [ATOMIC_TYPE_UCHAR] = {
124 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
126 [ATOMIC_TYPE_SHORT] = {
129 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
130 | ATOMIC_TYPE_FLAG_SIGNED
132 [ATOMIC_TYPE_USHORT] = {
135 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
137 [ATOMIC_TYPE_INT] = {
138 .size = (unsigned) -1,
139 .alignment = (unsigned) -1,
140 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
141 | ATOMIC_TYPE_FLAG_SIGNED,
143 [ATOMIC_TYPE_UINT] = {
144 .size = (unsigned) -1,
145 .alignment = (unsigned) -1,
146 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
148 [ATOMIC_TYPE_LONG] = {
149 .size = (unsigned) -1,
150 .alignment = (unsigned) -1,
151 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
152 | ATOMIC_TYPE_FLAG_SIGNED,
154 [ATOMIC_TYPE_ULONG] = {
155 .size = (unsigned) -1,
156 .alignment = (unsigned) -1,
157 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
159 [ATOMIC_TYPE_LONGLONG] = {
160 .size = (unsigned) -1,
161 .alignment = (unsigned) -1,
162 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
163 | ATOMIC_TYPE_FLAG_SIGNED,
165 [ATOMIC_TYPE_ULONGLONG] = {
166 .size = (unsigned) -1,
167 .alignment = (unsigned) -1,
168 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
170 [ATOMIC_TYPE_BOOL] = {
171 .size = (unsigned) -1,
172 .alignment = (unsigned) -1,
173 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
175 [ATOMIC_TYPE_FLOAT] = {
177 .alignment = (unsigned) -1,
178 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
179 | ATOMIC_TYPE_FLAG_SIGNED,
181 [ATOMIC_TYPE_DOUBLE] = {
183 .alignment = (unsigned) -1,
184 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
185 | ATOMIC_TYPE_FLAG_SIGNED,
187 [ATOMIC_TYPE_LONG_DOUBLE] = {
189 .alignment = (unsigned) -1,
190 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
191 | ATOMIC_TYPE_FLAG_SIGNED,
193 /* complex and imaginary types are set in init_types */
196 void init_types(void)
198 obstack_init(type_obst);
200 atomic_type_properties_t *props = atomic_type_properties;
202 if (char_is_signed) {
203 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
206 unsigned int_size = machine_size < 32 ? 2 : 4;
207 /* long is always 32bit on windows */
208 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
209 unsigned llong_size = machine_size < 32 ? 4 : 8;
211 props[ATOMIC_TYPE_INT].size = int_size;
212 props[ATOMIC_TYPE_INT].alignment = int_size;
213 props[ATOMIC_TYPE_UINT].size = int_size;
214 props[ATOMIC_TYPE_UINT].alignment = int_size;
215 props[ATOMIC_TYPE_LONG].size = long_size;
216 props[ATOMIC_TYPE_LONG].alignment = long_size;
217 props[ATOMIC_TYPE_ULONG].size = long_size;
218 props[ATOMIC_TYPE_ULONG].alignment = long_size;
219 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
220 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
221 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
222 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
224 /* TODO: backend specific, need a way to query the backend for this.
225 * The following are good settings for x86 */
226 if (machine_size <= 32) {
227 props[ATOMIC_TYPE_FLOAT].alignment = 4;
228 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
229 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
230 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
231 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
233 props[ATOMIC_TYPE_FLOAT].alignment = 4;
234 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
235 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
236 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
237 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
239 if (force_long_double_size > 0) {
240 props[ATOMIC_TYPE_LONG_DOUBLE].size = force_long_double_size;
241 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = force_long_double_size;
244 /* TODO: make this configurable for platforms which do not use byte sized
246 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
248 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
251 void exit_types(void)
253 obstack_free(type_obst, NULL);
256 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
258 size_t sep = q & QUAL_SEP_START ? 0 : 1;
259 if (qualifiers & TYPE_QUALIFIER_CONST) {
260 print_string(" const" + sep);
263 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
264 print_string(" volatile" + sep);
267 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
268 print_string(" restrict" + sep);
271 if (sep == 0 && q & QUAL_SEP_END)
275 const char *get_atomic_kind_name(atomic_type_kind_t kind)
278 case ATOMIC_TYPE_INVALID: break;
279 case ATOMIC_TYPE_VOID: return "void";
280 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
281 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
282 case ATOMIC_TYPE_CHAR: return "char";
283 case ATOMIC_TYPE_SCHAR: return "signed char";
284 case ATOMIC_TYPE_UCHAR: return "unsigned char";
285 case ATOMIC_TYPE_INT: return "int";
286 case ATOMIC_TYPE_UINT: return "unsigned int";
287 case ATOMIC_TYPE_SHORT: return "short";
288 case ATOMIC_TYPE_USHORT: return "unsigned short";
289 case ATOMIC_TYPE_LONG: return "long";
290 case ATOMIC_TYPE_ULONG: return "unsigned long";
291 case ATOMIC_TYPE_LONGLONG: return "long long";
292 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
293 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
294 case ATOMIC_TYPE_FLOAT: return "float";
295 case ATOMIC_TYPE_DOUBLE: return "double";
297 return "INVALIDATOMIC";
301 * Prints the name of an atomic type kinds.
303 * @param kind The type kind.
305 static void print_atomic_kinds(atomic_type_kind_t kind)
307 const char *s = get_atomic_kind_name(kind);
312 * Prints the name of an atomic type.
314 * @param type The type.
316 static void print_atomic_type(const atomic_type_t *type)
318 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
319 print_atomic_kinds(type->akind);
323 * Prints the name of a complex type.
325 * @param type The type.
327 static void print_complex_type(const complex_type_t *type)
329 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
330 print_string("_Complex");
331 print_atomic_kinds(type->akind);
335 * Prints the name of an imaginary type.
337 * @param type The type.
339 static void print_imaginary_type(const imaginary_type_t *type)
341 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
342 print_string("_Imaginary ");
343 print_atomic_kinds(type->akind);
347 * Print the first part (the prefix) of a type.
349 * @param type The type to print.
351 static void print_function_type_pre(const function_type_t *type)
353 switch (type->linkage) {
354 case LINKAGE_INVALID:
359 print_string("extern \"C\" ");
363 if (!(c_mode & _CXX))
364 print_string("extern \"C++\" ");
368 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
370 intern_print_type_pre(type->return_type);
372 cc_kind_t cc = type->calling_convention;
375 case CC_CDECL: print_string(" __cdecl"); break;
376 case CC_STDCALL: print_string(" __stdcall"); break;
377 case CC_FASTCALL: print_string(" __fastcall"); break;
378 case CC_THISCALL: print_string(" __thiscall"); break;
380 if (default_calling_convention != CC_CDECL) {
381 /* show the default calling convention if its not cdecl */
382 cc = default_calling_convention;
390 * Print the second part (the postfix) of a type.
392 * @param type The type to print.
394 static void print_function_type_post(const function_type_t *type,
395 const scope_t *parameters)
399 if (parameters == NULL) {
400 function_parameter_t *parameter = type->parameters;
401 for( ; parameter != NULL; parameter = parameter->next) {
407 print_type(parameter->type);
410 entity_t *parameter = parameters->entities;
411 for (; parameter != NULL; parameter = parameter->base.next) {
412 if (parameter->kind != ENTITY_PARAMETER)
420 const type_t *const type = parameter->declaration.type;
422 print_string(parameter->base.symbol->string);
424 print_type_ext(type, parameter->base.symbol, NULL);
428 if (type->variadic) {
436 if (first && !type->unspecified_parameters) {
437 print_string("void");
441 intern_print_type_post(type->return_type);
445 * Prints the prefix part of a pointer type.
447 * @param type The pointer type.
449 static void print_pointer_type_pre(const pointer_type_t *type)
451 type_t const *const points_to = type->points_to;
452 intern_print_type_pre(points_to);
453 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
455 variable_t *const variable = type->base_variable;
456 if (variable != NULL) {
457 print_string(" __based(");
458 print_string(variable->base.base.symbol->string);
462 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
466 * Prints the postfix part of a pointer type.
468 * @param type The pointer type.
470 static void print_pointer_type_post(const pointer_type_t *type)
472 type_t const *const points_to = type->points_to;
473 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
475 intern_print_type_post(points_to);
479 * Prints the prefix part of a reference type.
481 * @param type The reference type.
483 static void print_reference_type_pre(const reference_type_t *type)
485 type_t const *const refers_to = type->refers_to;
486 intern_print_type_pre(refers_to);
487 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
493 * Prints the postfix part of a reference type.
495 * @param type The reference type.
497 static void print_reference_type_post(const reference_type_t *type)
499 type_t const *const refers_to = type->refers_to;
500 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
502 intern_print_type_post(refers_to);
506 * Prints the prefix part of an array type.
508 * @param type The array type.
510 static void print_array_type_pre(const array_type_t *type)
512 intern_print_type_pre(type->element_type);
516 * Prints the postfix part of an array type.
518 * @param type The array type.
520 static void print_array_type_post(const array_type_t *type)
523 if (type->is_static) {
524 print_string("static ");
526 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
527 if (type->size_expression != NULL
528 && (print_implicit_array_size || !type->has_implicit_size)) {
529 print_expression(type->size_expression);
532 intern_print_type_post(type->element_type);
536 * Prints the postfix part of a bitfield type.
538 * @param type The array type.
540 static void print_bitfield_type_post(const bitfield_type_t *type)
543 print_expression(type->size_expression);
544 intern_print_type_post(type->base_type);
548 * Prints an enum definition.
550 * @param declaration The enum's type declaration.
552 void print_enum_definition(const enum_t *enume)
558 entity_t *entry = enume->base.next;
559 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
560 entry = entry->base.next) {
563 print_string(entry->base.symbol->string);
564 if (entry->enum_value.value != NULL) {
567 /* skip the implicit cast */
568 expression_t *expression = entry->enum_value.value;
569 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
570 expression = expression->unary.value;
572 print_expression(expression);
583 * Prints an enum type.
585 * @param type The enum type.
587 static void print_type_enum(const enum_type_t *type)
589 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
590 print_string("enum ");
592 enum_t *enume = type->enume;
593 symbol_t *symbol = enume->base.symbol;
594 if (symbol != NULL) {
595 print_string(symbol->string);
597 print_enum_definition(enume);
602 * Print the compound part of a compound type.
604 void print_compound_definition(const compound_t *compound)
609 entity_t *entity = compound->members.entities;
610 for( ; entity != NULL; entity = entity->base.next) {
611 if (entity->kind != ENTITY_COMPOUND_MEMBER)
615 print_entity(entity);
622 if (compound->modifiers & DM_TRANSPARENT_UNION) {
623 print_string("__attribute__((__transparent_union__))");
628 * Prints a compound type.
630 * @param type The compound type.
632 static void print_compound_type(const compound_type_t *type)
634 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
636 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
637 print_string("struct ");
639 assert(type->base.kind == TYPE_COMPOUND_UNION);
640 print_string("union ");
643 compound_t *compound = type->compound;
644 symbol_t *symbol = compound->base.symbol;
645 if (symbol != NULL) {
646 print_string(symbol->string);
648 print_compound_definition(compound);
653 * Prints the prefix part of a typedef type.
655 * @param type The typedef type.
657 static void print_typedef_type_pre(const typedef_type_t *const type)
659 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
660 print_string(type->typedefe->base.symbol->string);
664 * Prints the prefix part of a typeof type.
666 * @param type The typeof type.
668 static void print_typeof_type_pre(const typeof_type_t *const type)
670 print_string("typeof(");
671 if (type->expression != NULL) {
672 print_expression(type->expression);
674 print_type(type->typeof_type);
680 * Prints the prefix part of a type.
682 * @param type The type.
684 static void intern_print_type_pre(const type_t *const type)
688 print_string("<error>");
691 print_string("<invalid>");
694 print_type_enum(&type->enumt);
697 print_atomic_type(&type->atomic);
700 print_complex_type(&type->complex);
703 print_imaginary_type(&type->imaginary);
705 case TYPE_COMPOUND_STRUCT:
706 case TYPE_COMPOUND_UNION:
707 print_compound_type(&type->compound);
710 print_string(type->builtin.symbol->string);
713 print_function_type_pre(&type->function);
716 print_pointer_type_pre(&type->pointer);
719 print_reference_type_pre(&type->reference);
722 intern_print_type_pre(type->bitfield.base_type);
725 print_array_type_pre(&type->array);
728 print_typedef_type_pre(&type->typedeft);
731 print_typeof_type_pre(&type->typeoft);
734 print_string("unknown");
738 * Prints the postfix part of a type.
740 * @param type The type.
742 static void intern_print_type_post(const type_t *const type)
746 print_function_type_post(&type->function, NULL);
749 print_pointer_type_post(&type->pointer);
752 print_reference_type_post(&type->reference);
755 print_array_type_post(&type->array);
758 print_bitfield_type_post(&type->bitfield);
766 case TYPE_COMPOUND_STRUCT:
767 case TYPE_COMPOUND_UNION:
778 * @param type The type.
780 void print_type(const type_t *const type)
782 print_type_ext(type, NULL, NULL);
785 void print_type_ext(const type_t *const type, const symbol_t *symbol,
786 const scope_t *parameters)
788 intern_print_type_pre(type);
789 if (symbol != NULL) {
791 print_string(symbol->string);
793 if (type->kind == TYPE_FUNCTION) {
794 print_function_type_post(&type->function, parameters);
796 intern_print_type_post(type);
803 * @param type The type to copy.
804 * @return A copy of the type.
806 * @note This does not produce a deep copy!
808 type_t *duplicate_type(const type_t *type)
810 size_t size = get_type_struct_size(type->kind);
812 type_t *copy = obstack_alloc(type_obst, size);
813 memcpy(copy, type, size);
814 copy->base.firm_type = NULL;
820 * Returns the unqualified type of a given type.
822 * @param type The type.
823 * @returns The unqualified type.
825 type_t *get_unqualified_type(type_t *type)
827 assert(!is_typeref(type));
829 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
832 type_t *unqualified_type = duplicate_type(type);
833 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
835 return identify_new_type(unqualified_type);
838 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
840 type_t *type = skip_typeref(orig_type);
843 if (is_type_array(type)) {
844 /* For array types the element type has to be adjusted */
845 type_t *element_type = type->array.element_type;
846 type_t *qual_element_type = get_qualified_type(element_type, qual);
848 if (qual_element_type == element_type)
851 copy = duplicate_type(type);
852 copy->array.element_type = qual_element_type;
853 } else if (is_type_valid(type)) {
854 if ((type->base.qualifiers & qual) == qual)
857 copy = duplicate_type(type);
858 copy->base.qualifiers |= qual;
863 return identify_new_type(copy);
867 * Check if a type is valid.
869 * @param type The type to check.
870 * @return true if type represents a valid type.
872 bool type_valid(const type_t *type)
874 return type->kind != TYPE_INVALID;
877 static bool test_atomic_type_flag(atomic_type_kind_t kind,
878 atomic_type_flag_t flag)
880 assert(kind <= ATOMIC_TYPE_LAST);
881 return (atomic_type_properties[kind].flags & flag) != 0;
885 * Returns true if the given type is an integer type.
887 * @param type The type to check.
888 * @return True if type is an integer type.
890 bool is_type_integer(const type_t *type)
892 assert(!is_typeref(type));
894 if (type->kind == TYPE_ENUM)
896 if (type->kind == TYPE_BITFIELD)
899 if (type->kind != TYPE_ATOMIC)
902 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
906 * Returns true if the given type is an enum type.
908 * @param type The type to check.
909 * @return True if type is an enum type.
911 bool is_type_enum(const type_t *type)
913 assert(!is_typeref(type));
914 return type->kind == TYPE_ENUM;
918 * Returns true if the given type is an floating point type.
920 * @param type The type to check.
921 * @return True if type is a floating point type.
923 bool is_type_float(const type_t *type)
925 assert(!is_typeref(type));
927 if (type->kind != TYPE_ATOMIC)
930 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
934 * Returns true if the given type is an complex type.
936 * @param type The type to check.
937 * @return True if type is a complex type.
939 bool is_type_complex(const type_t *type)
941 assert(!is_typeref(type));
943 if (type->kind != TYPE_ATOMIC)
946 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
950 * Returns true if the given type is a signed type.
952 * @param type The type to check.
953 * @return True if type is a signed type.
955 bool is_type_signed(const type_t *type)
957 assert(!is_typeref(type));
959 /* enum types are int for now */
960 if (type->kind == TYPE_ENUM)
962 if (type->kind == TYPE_BITFIELD)
963 return is_type_signed(type->bitfield.base_type);
965 if (type->kind != TYPE_ATOMIC)
968 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
972 * Returns true if the given type represents an arithmetic type.
974 * @param type The type to check.
975 * @return True if type represents an arithmetic type.
977 bool is_type_arithmetic(const type_t *type)
979 assert(!is_typeref(type));
986 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
988 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
990 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
997 * Returns true if the given type is an integer or float type.
999 * @param type The type to check.
1000 * @return True if type is an integer or float type.
1002 bool is_type_real(const type_t *type)
1005 return is_type_integer(type) || is_type_float(type);
1009 * Returns true if the given type represents a scalar type.
1011 * @param type The type to check.
1012 * @return True if type represents a scalar type.
1014 bool is_type_scalar(const type_t *type)
1016 assert(!is_typeref(type));
1018 switch (type->kind) {
1019 case TYPE_POINTER: return true;
1020 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
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:
1069 panic("is_type_incomplete called without typerefs skipped");
1074 panic("invalid type found");
1077 bool is_type_object(const type_t *type)
1079 return !is_type_function(type) && !is_type_incomplete(type);
1082 bool is_builtin_va_list(type_t *type)
1084 type_t *tp = skip_typeref(type);
1086 return tp->kind == type_valist->kind &&
1087 tp->builtin.symbol == type_valist->builtin.symbol;
1091 * Check if two function types are compatible.
1093 static bool function_types_compatible(const function_type_t *func1,
1094 const function_type_t *func2)
1096 const type_t* const ret1 = skip_typeref(func1->return_type);
1097 const type_t* const ret2 = skip_typeref(func2->return_type);
1098 if (!types_compatible(ret1, ret2))
1101 if (func1->linkage != func2->linkage)
1104 cc_kind_t cc1 = func1->calling_convention;
1105 if (cc1 == CC_DEFAULT)
1106 cc1 = default_calling_convention;
1107 cc_kind_t cc2 = func2->calling_convention;
1108 if (cc2 == CC_DEFAULT)
1109 cc2 = default_calling_convention;
1114 if (func1->variadic != func2->variadic)
1117 /* can parameters be compared? */
1118 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1119 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1122 /* TODO: handling of unspecified parameters not correct yet */
1124 /* all argument types must be compatible */
1125 function_parameter_t *parameter1 = func1->parameters;
1126 function_parameter_t *parameter2 = func2->parameters;
1127 for ( ; parameter1 != NULL && parameter2 != NULL;
1128 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1129 type_t *parameter1_type = skip_typeref(parameter1->type);
1130 type_t *parameter2_type = skip_typeref(parameter2->type);
1132 parameter1_type = get_unqualified_type(parameter1_type);
1133 parameter2_type = get_unqualified_type(parameter2_type);
1135 if (!types_compatible(parameter1_type, parameter2_type))
1138 /* same number of arguments? */
1139 if (parameter1 != NULL || parameter2 != NULL)
1146 * Check if two array types are compatible.
1148 static bool array_types_compatible(const array_type_t *array1,
1149 const array_type_t *array2)
1151 type_t *element_type1 = skip_typeref(array1->element_type);
1152 type_t *element_type2 = skip_typeref(array2->element_type);
1153 if (!types_compatible(element_type1, element_type2))
1156 if (!array1->size_constant || !array2->size_constant)
1159 return array1->size == array2->size;
1163 * Check if two types are compatible.
1165 bool types_compatible(const type_t *type1, const type_t *type2)
1167 assert(!is_typeref(type1));
1168 assert(!is_typeref(type2));
1170 /* shortcut: the same type is always compatible */
1174 if (!is_type_valid(type1) || !is_type_valid(type2))
1177 if (type1->base.qualifiers != type2->base.qualifiers)
1179 if (type1->kind != type2->kind)
1182 switch (type1->kind) {
1184 return function_types_compatible(&type1->function, &type2->function);
1186 return type1->atomic.akind == type2->atomic.akind;
1188 return type1->complex.akind == type2->complex.akind;
1189 case TYPE_IMAGINARY:
1190 return type1->imaginary.akind == type2->imaginary.akind;
1192 return array_types_compatible(&type1->array, &type2->array);
1194 case TYPE_POINTER: {
1195 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1196 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1197 return types_compatible(to1, to2);
1200 case TYPE_REFERENCE: {
1201 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1202 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1203 return types_compatible(to1, to2);
1206 case TYPE_COMPOUND_STRUCT:
1207 case TYPE_COMPOUND_UNION: {
1214 /* TODO: not implemented */
1218 /* not sure if this makes sense or is even needed, implement it if you
1219 * really need it! */
1220 panic("type compatibility check for bitfield type");
1223 /* Hmm, the error type should be compatible to all other types */
1226 panic("invalid type found in compatible types");
1229 panic("typerefs not skipped in compatible types?!?");
1232 /* TODO: incomplete */
1237 * Skip all typerefs and return the underlying type.
1239 type_t *skip_typeref(type_t *type)
1241 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1244 switch (type->kind) {
1247 case TYPE_TYPEDEF: {
1248 qualifiers |= type->base.qualifiers;
1250 const typedef_type_t *typedef_type = &type->typedeft;
1251 if (typedef_type->resolved_type != NULL) {
1252 type = typedef_type->resolved_type;
1255 type = typedef_type->typedefe->type;
1259 qualifiers |= type->base.qualifiers;
1260 type = type->typeoft.typeof_type;
1268 if (qualifiers != TYPE_QUALIFIER_NONE) {
1269 type_t *const copy = duplicate_type(type);
1271 /* for const with typedefed array type the element type has to be
1273 if (is_type_array(copy)) {
1274 type_t *element_type = copy->array.element_type;
1275 element_type = duplicate_type(element_type);
1276 element_type->base.qualifiers |= qualifiers;
1277 copy->array.element_type = element_type;
1279 copy->base.qualifiers |= qualifiers;
1282 type = identify_new_type(copy);
1288 unsigned get_type_size(type_t *type)
1290 switch (type->kind) {
1296 return get_atomic_type_size(type->atomic.akind);
1298 return get_atomic_type_size(type->complex.akind) * 2;
1299 case TYPE_IMAGINARY:
1300 return get_atomic_type_size(type->imaginary.akind);
1301 case TYPE_COMPOUND_UNION:
1302 layout_union_type(&type->compound);
1303 return type->compound.compound->size;
1304 case TYPE_COMPOUND_STRUCT:
1305 layout_struct_type(&type->compound);
1306 return type->compound.compound->size;
1308 return get_atomic_type_size(type->enumt.akind);
1310 return 0; /* non-const (but "address-const") */
1311 case TYPE_REFERENCE:
1313 /* TODO: make configurable by backend */
1316 /* TODO: correct if element_type is aligned? */
1317 il_size_t element_size = get_type_size(type->array.element_type);
1318 return type->array.size * element_size;
1323 return get_type_size(type->builtin.real_type);
1325 return get_type_size(type->typedeft.typedefe->type);
1327 if (type->typeoft.typeof_type) {
1328 return get_type_size(type->typeoft.typeof_type);
1330 return get_type_size(type->typeoft.expression->base.type);
1333 panic("invalid type in get_type_size");
1336 unsigned get_type_alignment(type_t *type)
1338 switch (type->kind) {
1344 return get_atomic_type_alignment(type->atomic.akind);
1346 return get_atomic_type_alignment(type->complex.akind);
1347 case TYPE_IMAGINARY:
1348 return get_atomic_type_alignment(type->imaginary.akind);
1349 case TYPE_COMPOUND_UNION:
1350 layout_union_type(&type->compound);
1351 return type->compound.compound->alignment;
1352 case TYPE_COMPOUND_STRUCT:
1353 layout_struct_type(&type->compound);
1354 return type->compound.compound->alignment;
1356 return get_atomic_type_alignment(type->enumt.akind);
1358 /* what is correct here? */
1360 case TYPE_REFERENCE:
1362 /* TODO: make configurable by backend */
1365 return get_type_alignment(type->array.element_type);
1369 return get_type_alignment(type->builtin.real_type);
1370 case TYPE_TYPEDEF: {
1371 il_alignment_t alignment
1372 = get_type_alignment(type->typedeft.typedefe->type);
1373 if (type->typedeft.typedefe->alignment > alignment)
1374 alignment = type->typedeft.typedefe->alignment;
1379 if (type->typeoft.typeof_type) {
1380 return get_type_alignment(type->typeoft.typeof_type);
1382 return get_type_alignment(type->typeoft.expression->base.type);
1385 panic("invalid type in get_type_alignment");
1388 decl_modifiers_t get_type_modifiers(const type_t *type)
1390 switch(type->kind) {
1394 case TYPE_COMPOUND_STRUCT:
1395 case TYPE_COMPOUND_UNION:
1396 return type->compound.compound->modifiers;
1398 return type->function.modifiers;
1402 case TYPE_IMAGINARY:
1403 case TYPE_REFERENCE:
1409 return get_type_modifiers(type->builtin.real_type);
1410 case TYPE_TYPEDEF: {
1411 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1412 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1416 if (type->typeoft.typeof_type) {
1417 return get_type_modifiers(type->typeoft.typeof_type);
1419 return get_type_modifiers(type->typeoft.expression->base.type);
1422 panic("invalid type found in get_type_modifiers");
1425 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1427 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1430 switch (type->base.kind) {
1432 return TYPE_QUALIFIER_NONE;
1434 qualifiers |= type->base.qualifiers;
1435 const typedef_type_t *typedef_type = &type->typedeft;
1436 if (typedef_type->resolved_type != NULL)
1437 type = typedef_type->resolved_type;
1439 type = typedef_type->typedefe->type;
1442 type = type->typeoft.typeof_type;
1445 if (skip_array_type) {
1446 type = type->array.element_type;
1455 return type->base.qualifiers | qualifiers;
1458 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1460 assert(kind <= ATOMIC_TYPE_LAST);
1461 return atomic_type_properties[kind].size;
1464 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1466 assert(kind <= ATOMIC_TYPE_LAST);
1467 return atomic_type_properties[kind].alignment;
1470 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1472 assert(kind <= ATOMIC_TYPE_LAST);
1473 return atomic_type_properties[kind].flags;
1476 atomic_type_kind_t get_intptr_kind(void)
1478 if (machine_size <= 32)
1479 return ATOMIC_TYPE_INT;
1480 else if (machine_size <= 64)
1481 return ATOMIC_TYPE_LONG;
1483 return ATOMIC_TYPE_LONGLONG;
1486 atomic_type_kind_t get_uintptr_kind(void)
1488 if (machine_size <= 32)
1489 return ATOMIC_TYPE_UINT;
1490 else if (machine_size <= 64)
1491 return ATOMIC_TYPE_ULONG;
1493 return ATOMIC_TYPE_ULONGLONG;
1497 * Find the atomic type kind representing a given size (signed).
1499 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1501 static atomic_type_kind_t kinds[32];
1504 atomic_type_kind_t kind = kinds[size];
1505 if (kind == ATOMIC_TYPE_INVALID) {
1506 static const atomic_type_kind_t possible_kinds[] = {
1511 ATOMIC_TYPE_LONGLONG
1513 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1514 if (get_atomic_type_size(possible_kinds[i]) == size) {
1515 kind = possible_kinds[i];
1525 * Find the atomic type kind representing a given size (signed).
1527 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1529 static atomic_type_kind_t kinds[32];
1532 atomic_type_kind_t kind = kinds[size];
1533 if (kind == ATOMIC_TYPE_INVALID) {
1534 static const atomic_type_kind_t possible_kinds[] = {
1539 ATOMIC_TYPE_ULONGLONG
1541 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1542 if (get_atomic_type_size(possible_kinds[i]) == size) {
1543 kind = possible_kinds[i];
1553 * Hash the given type and return the "singleton" version
1556 type_t *identify_new_type(type_t *type)
1558 type_t *result = typehash_insert(type);
1559 if (result != type) {
1560 obstack_free(type_obst, type);
1566 * Creates a new atomic type.
1568 * @param akind The kind of the atomic type.
1569 * @param qualifiers Type qualifiers for the new type.
1571 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1573 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1574 memset(type, 0, sizeof(atomic_type_t));
1576 type->kind = TYPE_ATOMIC;
1577 type->base.qualifiers = qualifiers;
1578 type->atomic.akind = akind;
1580 return identify_new_type(type);
1584 * Creates a new complex type.
1586 * @param akind The kind of the atomic type.
1587 * @param qualifiers Type qualifiers for the new type.
1589 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1591 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1592 memset(type, 0, sizeof(complex_type_t));
1594 type->kind = TYPE_COMPLEX;
1595 type->base.qualifiers = qualifiers;
1596 type->complex.akind = akind;
1598 return identify_new_type(type);
1602 * Creates a new imaginary type.
1604 * @param akind The kind of the atomic type.
1605 * @param qualifiers Type qualifiers for the new type.
1607 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1609 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1610 memset(type, 0, sizeof(imaginary_type_t));
1612 type->kind = TYPE_IMAGINARY;
1613 type->base.qualifiers = qualifiers;
1614 type->imaginary.akind = akind;
1616 return identify_new_type(type);
1620 * Creates a new pointer type.
1622 * @param points_to The points-to type for the new type.
1623 * @param qualifiers Type qualifiers for the new type.
1625 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1627 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1628 memset(type, 0, sizeof(pointer_type_t));
1630 type->kind = TYPE_POINTER;
1631 type->base.qualifiers = qualifiers;
1632 type->pointer.points_to = points_to;
1633 type->pointer.base_variable = NULL;
1635 return identify_new_type(type);
1639 * Creates a new reference type.
1641 * @param refers_to The referred-to type for the new type.
1643 type_t *make_reference_type(type_t *refers_to)
1645 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1646 memset(type, 0, sizeof(reference_type_t));
1648 type->kind = TYPE_REFERENCE;
1649 type->base.qualifiers = 0;
1650 type->reference.refers_to = refers_to;
1652 return identify_new_type(type);
1656 * Creates a new based pointer type.
1658 * @param points_to The points-to type for the new type.
1659 * @param qualifiers Type qualifiers for the new type.
1660 * @param variable The based variable
1662 type_t *make_based_pointer_type(type_t *points_to,
1663 type_qualifiers_t qualifiers, variable_t *variable)
1665 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1666 memset(type, 0, sizeof(pointer_type_t));
1668 type->kind = TYPE_POINTER;
1669 type->base.qualifiers = qualifiers;
1670 type->pointer.points_to = points_to;
1671 type->pointer.base_variable = variable;
1673 return identify_new_type(type);
1677 type_t *make_array_type(type_t *element_type, size_t size,
1678 type_qualifiers_t qualifiers)
1680 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1681 memset(type, 0, sizeof(array_type_t));
1683 type->kind = TYPE_ARRAY;
1684 type->base.qualifiers = qualifiers;
1685 type->array.element_type = element_type;
1686 type->array.size = size;
1687 type->array.size_constant = true;
1689 return identify_new_type(type);
1692 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1693 il_alignment_t *struct_alignment,
1694 bool packed, entity_t *first)
1696 il_size_t offset = *struct_offset;
1697 il_alignment_t alignment = *struct_alignment;
1698 size_t bit_offset = 0;
1701 for (member = first; member != NULL; member = member->base.next) {
1702 if (member->kind != ENTITY_COMPOUND_MEMBER)
1705 type_t *type = member->declaration.type;
1706 if (type->kind != TYPE_BITFIELD)
1709 type_t *base_type = skip_typeref(type->bitfield.base_type);
1710 il_alignment_t base_alignment = get_type_alignment(base_type);
1711 il_alignment_t alignment_mask = base_alignment-1;
1712 if (base_alignment > alignment)
1713 alignment = base_alignment;
1715 size_t bit_size = type->bitfield.bit_size;
1717 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1718 offset &= ~alignment_mask;
1719 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1721 if (bit_offset + bit_size > base_size || bit_size == 0) {
1722 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1723 offset = (offset + base_alignment-1) & ~alignment_mask;
1728 if (byte_order_big_endian) {
1729 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1730 member->compound_member.offset = offset & ~alignment_mask;
1731 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1733 member->compound_member.offset = offset;
1734 member->compound_member.bit_offset = bit_offset;
1737 bit_offset += bit_size;
1738 offset += bit_offset / BITS_PER_BYTE;
1739 bit_offset %= BITS_PER_BYTE;
1745 *struct_offset = offset;
1746 *struct_alignment = alignment;
1750 void layout_struct_type(compound_type_t *type)
1752 assert(type->compound != NULL);
1754 compound_t *compound = type->compound;
1755 if (!compound->complete)
1757 if (type->compound->layouted)
1760 il_size_t offset = 0;
1761 il_alignment_t alignment = compound->alignment;
1762 bool need_pad = false;
1764 entity_t *entry = compound->members.entities;
1765 while (entry != NULL) {
1766 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1767 entry = entry->base.next;
1771 type_t *m_type = entry->declaration.type;
1772 type_t *skipped = skip_typeref(m_type);
1773 if (! is_type_valid(skipped)) {
1774 entry = entry->base.next;
1778 if (skipped->kind == TYPE_BITFIELD) {
1779 entry = pack_bitfield_members(&offset, &alignment,
1780 compound->packed, entry);
1784 il_alignment_t m_alignment = get_type_alignment(m_type);
1785 if (m_alignment > alignment)
1786 alignment = m_alignment;
1788 if (!compound->packed) {
1789 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1791 if (new_offset > offset) {
1793 offset = new_offset;
1797 entry->compound_member.offset = offset;
1798 offset += get_type_size(m_type);
1800 entry = entry->base.next;
1803 if (!compound->packed) {
1804 il_size_t new_offset = (offset + alignment-1) & -alignment;
1805 if (new_offset > offset) {
1807 offset = new_offset;
1812 if (warning.padded) {
1813 warningf(&compound->base.source_position, "'%T' needs padding",
1816 } else if (compound->packed && warning.packed) {
1817 warningf(&compound->base.source_position,
1818 "superfluous packed attribute on '%T'", type);
1821 compound->size = offset;
1822 compound->alignment = alignment;
1823 compound->layouted = true;
1826 void layout_union_type(compound_type_t *type)
1828 assert(type->compound != NULL);
1830 compound_t *compound = type->compound;
1831 if (! compound->complete)
1835 il_alignment_t alignment = compound->alignment;
1837 entity_t *entry = compound->members.entities;
1838 for (; entry != NULL; entry = entry->base.next) {
1839 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1842 type_t *m_type = entry->declaration.type;
1843 if (! is_type_valid(skip_typeref(m_type)))
1846 entry->compound_member.offset = 0;
1847 il_size_t m_size = get_type_size(m_type);
1850 il_alignment_t m_alignment = get_type_alignment(m_type);
1851 if (m_alignment > alignment)
1852 alignment = m_alignment;
1854 size = (size + alignment - 1) & -alignment;
1856 compound->size = size;
1857 compound->alignment = alignment;
1860 static function_parameter_t *allocate_parameter(type_t *const type)
1862 function_parameter_t *const param
1863 = obstack_alloc(type_obst, sizeof(*param));
1864 memset(param, 0, sizeof(*param));
1869 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1870 type_t *argument_type2)
1872 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1873 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1874 parameter1->next = parameter2;
1876 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1877 type->function.return_type = return_type;
1878 type->function.parameters = parameter1;
1879 type->function.linkage = LINKAGE_C;
1881 return identify_new_type(type);
1884 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1886 function_parameter_t *const parameter = allocate_parameter(argument_type);
1888 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1889 type->function.return_type = return_type;
1890 type->function.parameters = parameter;
1891 type->function.linkage = LINKAGE_C;
1893 return identify_new_type(type);
1896 type_t *make_function_1_type_variadic(type_t *return_type,
1897 type_t *argument_type)
1899 function_parameter_t *const parameter = allocate_parameter(argument_type);
1901 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1902 type->function.return_type = return_type;
1903 type->function.parameters = parameter;
1904 type->function.variadic = true;
1905 type->function.linkage = LINKAGE_C;
1907 return identify_new_type(type);
1910 type_t *make_function_0_type(type_t *return_type)
1912 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1913 type->function.return_type = return_type;
1914 type->function.parameters = NULL;
1915 type->function.linkage = LINKAGE_C;
1917 return identify_new_type(type);
1920 type_t *make_function_type(type_t *return_type, int n_types,
1921 type_t *const *argument_types,
1922 decl_modifiers_t modifiers)
1924 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1925 type->function.return_type = return_type;
1926 type->function.modifiers |= modifiers;
1927 type->function.linkage = LINKAGE_C;
1929 function_parameter_t *last = NULL;
1930 for (int i = 0; i < n_types; ++i) {
1931 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1933 type->function.parameters = parameter;
1935 last->next = parameter;
1940 return identify_new_type(type);
1944 * Debug helper. Prints the given type to stdout.
1946 static __attribute__((unused))
1947 void dbg_type(const type_t *type)
1949 print_to_file(stderr);