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 unsigned long_size = machine_size < 64 ? 4 : 8;
208 unsigned llong_size = machine_size < 32 ? 4 : 8;
210 props[ATOMIC_TYPE_INT].size = int_size;
211 props[ATOMIC_TYPE_INT].alignment = int_size;
212 props[ATOMIC_TYPE_UINT].size = int_size;
213 props[ATOMIC_TYPE_UINT].alignment = int_size;
214 props[ATOMIC_TYPE_LONG].size = long_size;
215 props[ATOMIC_TYPE_LONG].alignment = long_size;
216 props[ATOMIC_TYPE_ULONG].size = long_size;
217 props[ATOMIC_TYPE_ULONG].alignment = long_size;
218 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
219 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
220 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
221 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
223 /* TODO: backend specific, need a way to query the backend for this.
224 * The following are good settings for x86 */
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;
230 if (firm_opt.os_support == OS_SUPPORT_MACHO) {
231 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
232 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
235 /* TODO: make this configurable for platforms which do not use byte sized
237 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
239 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
242 void exit_types(void)
244 obstack_free(type_obst, NULL);
247 void print_type_qualifiers(type_qualifiers_t qualifiers)
249 if (qualifiers & TYPE_QUALIFIER_CONST) {
250 print_string("const ");
252 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
253 print_string("volatile ");
255 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
256 print_string("restrict ");
260 const char *get_atomic_kind_name(atomic_type_kind_t kind)
263 case ATOMIC_TYPE_INVALID: break;
264 case ATOMIC_TYPE_VOID: return "void";
265 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
266 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
267 case ATOMIC_TYPE_CHAR: return "char";
268 case ATOMIC_TYPE_SCHAR: return "signed char";
269 case ATOMIC_TYPE_UCHAR: return "unsigned char";
270 case ATOMIC_TYPE_INT: return "int";
271 case ATOMIC_TYPE_UINT: return "unsigned int";
272 case ATOMIC_TYPE_SHORT: return "short";
273 case ATOMIC_TYPE_USHORT: return "unsigned short";
274 case ATOMIC_TYPE_LONG: return "long";
275 case ATOMIC_TYPE_ULONG: return "unsigned long";
276 case ATOMIC_TYPE_LONGLONG: return "long long";
277 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
278 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
279 case ATOMIC_TYPE_FLOAT: return "float";
280 case ATOMIC_TYPE_DOUBLE: return "double";
282 return "INVALIDATOMIC";
286 * Prints the name of an atomic type kinds.
288 * @param kind The type kind.
290 static void print_atomic_kinds(atomic_type_kind_t kind)
292 const char *s = get_atomic_kind_name(kind);
297 * Prints the name of an atomic type.
299 * @param type The type.
301 static void print_atomic_type(const atomic_type_t *type)
303 print_type_qualifiers(type->base.qualifiers);
304 print_atomic_kinds(type->akind);
308 * Prints the name of a complex type.
310 * @param type The type.
312 static void print_complex_type(const complex_type_t *type)
314 print_type_qualifiers(type->base.qualifiers);
315 print_string("_Complex");
316 print_atomic_kinds(type->akind);
320 * Prints the name of an imaginary type.
322 * @param type The type.
324 static void print_imaginary_type(const imaginary_type_t *type)
326 print_type_qualifiers(type->base.qualifiers);
327 print_string("_Imaginary ");
328 print_atomic_kinds(type->akind);
332 * Print the first part (the prefix) of a type.
334 * @param type The type to print.
336 static void print_function_type_pre(const function_type_t *type)
338 switch (type->linkage) {
339 case LINKAGE_INVALID:
344 print_string("extern \"C\" ");
348 if (!(c_mode & _CXX))
349 print_string("extern \"C++\" ");
353 print_type_qualifiers(type->base.qualifiers);
355 intern_print_type_pre(type->return_type);
357 cc_kind_t cc = type->calling_convention;
360 case CC_CDECL: print_string(" __cdecl"); break;
361 case CC_STDCALL: print_string(" __stdcall"); break;
362 case CC_FASTCALL: print_string(" __fastcall"); break;
363 case CC_THISCALL: print_string(" __thiscall"); break;
365 if (default_calling_convention != CC_CDECL) {
366 /* show the default calling convention if its not cdecl */
367 cc = default_calling_convention;
375 * Print the second part (the postfix) of a type.
377 * @param type The type to print.
379 static void print_function_type_post(const function_type_t *type,
380 const scope_t *parameters)
384 if (parameters == NULL) {
385 function_parameter_t *parameter = type->parameters;
386 for( ; parameter != NULL; parameter = parameter->next) {
392 print_type(parameter->type);
395 entity_t *parameter = parameters->entities;
396 for (; parameter != NULL; parameter = parameter->base.next) {
397 if (parameter->kind != ENTITY_PARAMETER)
405 const type_t *const type = parameter->declaration.type;
407 print_string(parameter->base.symbol->string);
409 print_type_ext(type, parameter->base.symbol, NULL);
413 if (type->variadic) {
421 if (first && !type->unspecified_parameters) {
422 print_string("void");
426 intern_print_type_post(type->return_type);
430 * Prints the prefix part of a pointer type.
432 * @param type The pointer type.
434 static void print_pointer_type_pre(const pointer_type_t *type)
436 type_t const *const points_to = type->points_to;
437 intern_print_type_pre(points_to);
438 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
440 variable_t *const variable = type->base_variable;
441 if (variable != NULL) {
442 print_string(" __based(");
443 print_string(variable->base.base.symbol->string);
447 type_qualifiers_t const qual = type->base.qualifiers;
450 print_type_qualifiers(qual);
454 * Prints the postfix part of a pointer type.
456 * @param type The pointer type.
458 static void print_pointer_type_post(const pointer_type_t *type)
460 type_t const *const points_to = type->points_to;
461 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
463 intern_print_type_post(points_to);
467 * Prints the prefix part of a reference type.
469 * @param type The reference type.
471 static void print_reference_type_pre(const reference_type_t *type)
473 type_t const *const refers_to = type->refers_to;
474 intern_print_type_pre(refers_to);
475 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
481 * Prints the postfix part of a reference type.
483 * @param type The reference type.
485 static void print_reference_type_post(const reference_type_t *type)
487 type_t const *const refers_to = type->refers_to;
488 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
490 intern_print_type_post(refers_to);
494 * Prints the prefix part of an array type.
496 * @param type The array type.
498 static void print_array_type_pre(const array_type_t *type)
500 intern_print_type_pre(type->element_type);
504 * Prints the postfix part of an array type.
506 * @param type The array type.
508 static void print_array_type_post(const array_type_t *type)
511 if (type->is_static) {
512 print_string("static ");
514 print_type_qualifiers(type->base.qualifiers);
515 if (type->size_expression != NULL
516 && (print_implicit_array_size || !type->has_implicit_size)) {
517 print_expression(type->size_expression);
520 intern_print_type_post(type->element_type);
524 * Prints the postfix part of a bitfield type.
526 * @param type The array type.
528 static void print_bitfield_type_post(const bitfield_type_t *type)
531 print_expression(type->size_expression);
532 intern_print_type_post(type->base_type);
536 * Prints an enum definition.
538 * @param declaration The enum's type declaration.
540 void print_enum_definition(const enum_t *enume)
546 entity_t *entry = enume->base.next;
547 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
548 entry = entry->base.next) {
551 print_string(entry->base.symbol->string);
552 if (entry->enum_value.value != NULL) {
555 /* skip the implicit cast */
556 expression_t *expression = entry->enum_value.value;
557 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
558 expression = expression->unary.value;
560 print_expression(expression);
571 * Prints an enum type.
573 * @param type The enum type.
575 static void print_type_enum(const enum_type_t *type)
577 print_type_qualifiers(type->base.qualifiers);
578 print_string("enum ");
580 enum_t *enume = type->enume;
581 symbol_t *symbol = enume->base.symbol;
582 if (symbol != NULL) {
583 print_string(symbol->string);
585 print_enum_definition(enume);
590 * Print the compound part of a compound type.
592 void print_compound_definition(const compound_t *compound)
597 entity_t *entity = compound->members.entities;
598 for( ; entity != NULL; entity = entity->base.next) {
599 if (entity->kind != ENTITY_COMPOUND_MEMBER)
603 print_entity(entity);
610 if (compound->modifiers & DM_TRANSPARENT_UNION) {
611 print_string("__attribute__((__transparent_union__))");
616 * Prints a compound type.
618 * @param type The compound type.
620 static void print_compound_type(const compound_type_t *type)
622 print_type_qualifiers(type->base.qualifiers);
624 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
625 print_string("struct ");
627 assert(type->base.kind == TYPE_COMPOUND_UNION);
628 print_string("union ");
631 compound_t *compound = type->compound;
632 symbol_t *symbol = compound->base.symbol;
633 if (symbol != NULL) {
634 print_string(symbol->string);
636 print_compound_definition(compound);
641 * Prints the prefix part of a typedef type.
643 * @param type The typedef type.
645 static void print_typedef_type_pre(const typedef_type_t *const type)
647 print_type_qualifiers(type->base.qualifiers);
648 print_string(type->typedefe->base.symbol->string);
652 * Prints the prefix part of a typeof type.
654 * @param type The typeof type.
656 static void print_typeof_type_pre(const typeof_type_t *const type)
658 print_string("typeof(");
659 if (type->expression != NULL) {
660 print_expression(type->expression);
662 print_type(type->typeof_type);
668 * Prints the prefix part of a type.
670 * @param type The type.
672 static void intern_print_type_pre(const type_t *const type)
676 print_string("<error>");
679 print_string("<invalid>");
682 print_type_enum(&type->enumt);
685 print_atomic_type(&type->atomic);
688 print_complex_type(&type->complex);
691 print_imaginary_type(&type->imaginary);
693 case TYPE_COMPOUND_STRUCT:
694 case TYPE_COMPOUND_UNION:
695 print_compound_type(&type->compound);
698 print_string(type->builtin.symbol->string);
701 print_function_type_pre(&type->function);
704 print_pointer_type_pre(&type->pointer);
707 print_reference_type_pre(&type->reference);
710 intern_print_type_pre(type->bitfield.base_type);
713 print_array_type_pre(&type->array);
716 print_typedef_type_pre(&type->typedeft);
719 print_typeof_type_pre(&type->typeoft);
722 print_string("unknown");
726 * Prints the postfix part of a type.
728 * @param type The type.
730 static void intern_print_type_post(const type_t *const type)
734 print_function_type_post(&type->function, NULL);
737 print_pointer_type_post(&type->pointer);
740 print_reference_type_post(&type->reference);
743 print_array_type_post(&type->array);
746 print_bitfield_type_post(&type->bitfield);
754 case TYPE_COMPOUND_STRUCT:
755 case TYPE_COMPOUND_UNION:
766 * @param type The type.
768 void print_type(const type_t *const type)
770 print_type_ext(type, NULL, NULL);
773 void print_type_ext(const type_t *const type, const symbol_t *symbol,
774 const scope_t *parameters)
777 print_string("nil type");
781 intern_print_type_pre(type);
782 if (symbol != NULL) {
784 print_string(symbol->string);
786 if (type->kind == TYPE_FUNCTION) {
787 print_function_type_post(&type->function, parameters);
789 intern_print_type_post(type);
796 * @param type The type to copy.
797 * @return A copy of the type.
799 * @note This does not produce a deep copy!
801 type_t *duplicate_type(const type_t *type)
803 size_t size = get_type_struct_size(type->kind);
805 type_t *copy = obstack_alloc(type_obst, size);
806 memcpy(copy, type, size);
807 copy->base.firm_type = NULL;
813 * Returns the unqualified type of a given type.
815 * @param type The type.
816 * @returns The unqualified type.
818 type_t *get_unqualified_type(type_t *type)
820 assert(!is_typeref(type));
822 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
825 type_t *unqualified_type = duplicate_type(type);
826 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
828 return identify_new_type(unqualified_type);
831 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
833 type_t *type = skip_typeref(orig_type);
836 if (is_type_array(type)) {
837 /* For array types the element type has to be adjusted */
838 type_t *element_type = type->array.element_type;
839 type_t *qual_element_type = get_qualified_type(element_type, qual);
841 if (qual_element_type == element_type)
844 copy = duplicate_type(type);
845 copy->array.element_type = qual_element_type;
846 } else if (is_type_valid(type)) {
847 if ((type->base.qualifiers & qual) == qual)
850 copy = duplicate_type(type);
851 copy->base.qualifiers |= qual;
856 return identify_new_type(copy);
860 * Check if a type is valid.
862 * @param type The type to check.
863 * @return true if type represents a valid type.
865 bool type_valid(const type_t *type)
867 return type->kind != TYPE_INVALID;
870 static bool test_atomic_type_flag(atomic_type_kind_t kind,
871 atomic_type_flag_t flag)
873 assert(kind <= ATOMIC_TYPE_LAST);
874 return (atomic_type_properties[kind].flags & flag) != 0;
878 * Returns true if the given type is an integer type.
880 * @param type The type to check.
881 * @return True if type is an integer type.
883 bool is_type_integer(const type_t *type)
885 assert(!is_typeref(type));
887 if (type->kind == TYPE_ENUM)
889 if (type->kind == TYPE_BITFIELD)
892 if (type->kind != TYPE_ATOMIC)
895 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
899 * Returns true if the given type is an enum type.
901 * @param type The type to check.
902 * @return True if type is an enum type.
904 bool is_type_enum(const type_t *type)
906 assert(!is_typeref(type));
907 return type->kind == TYPE_ENUM;
911 * Returns true if the given type is an floating point type.
913 * @param type The type to check.
914 * @return True if type is a floating point type.
916 bool is_type_float(const type_t *type)
918 assert(!is_typeref(type));
920 if (type->kind != TYPE_ATOMIC)
923 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
927 * Returns true if the given type is an complex type.
929 * @param type The type to check.
930 * @return True if type is a complex type.
932 bool is_type_complex(const type_t *type)
934 assert(!is_typeref(type));
936 if (type->kind != TYPE_ATOMIC)
939 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
943 * Returns true if the given type is a signed type.
945 * @param type The type to check.
946 * @return True if type is a signed type.
948 bool is_type_signed(const type_t *type)
950 assert(!is_typeref(type));
952 /* enum types are int for now */
953 if (type->kind == TYPE_ENUM)
955 if (type->kind == TYPE_BITFIELD)
956 return is_type_signed(type->bitfield.base_type);
958 if (type->kind != TYPE_ATOMIC)
961 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
965 * Returns true if the given type represents an arithmetic type.
967 * @param type The type to check.
968 * @return True if type represents an arithmetic type.
970 bool is_type_arithmetic(const type_t *type)
972 assert(!is_typeref(type));
979 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
981 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
983 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
990 * Returns true if the given type is an integer or float type.
992 * @param type The type to check.
993 * @return True if type is an integer or float type.
995 bool is_type_real(const type_t *type)
998 return is_type_integer(type) || is_type_float(type);
1002 * Returns true if the given type represents a scalar type.
1004 * @param type The type to check.
1005 * @return True if type represents a scalar type.
1007 bool is_type_scalar(const type_t *type)
1009 assert(!is_typeref(type));
1011 switch (type->kind) {
1012 case TYPE_POINTER: return true;
1013 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1017 return is_type_arithmetic(type);
1021 * Check if a given type is incomplete.
1023 * @param type The type to check.
1024 * @return True if the given type is incomplete (ie. just forward).
1026 bool is_type_incomplete(const type_t *type)
1028 assert(!is_typeref(type));
1030 switch(type->kind) {
1031 case TYPE_COMPOUND_STRUCT:
1032 case TYPE_COMPOUND_UNION: {
1033 const compound_type_t *compound_type = &type->compound;
1034 return !compound_type->compound->complete;
1040 return type->array.size_expression == NULL
1041 && !type->array.size_constant;
1044 return type->atomic.akind == ATOMIC_TYPE_VOID;
1047 return type->complex.akind == ATOMIC_TYPE_VOID;
1049 case TYPE_IMAGINARY:
1050 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1055 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);
1075 bool is_builtin_va_list(type_t *type)
1077 type_t *tp = skip_typeref(type);
1079 return tp->kind == type_valist->kind &&
1080 tp->builtin.symbol == type_valist->builtin.symbol;
1084 * Check if two function types are compatible.
1086 static bool function_types_compatible(const function_type_t *func1,
1087 const function_type_t *func2)
1089 const type_t* const ret1 = skip_typeref(func1->return_type);
1090 const type_t* const ret2 = skip_typeref(func2->return_type);
1091 if (!types_compatible(ret1, ret2))
1094 if (func1->linkage != func2->linkage)
1097 cc_kind_t cc1 = func1->calling_convention;
1098 if (cc1 == CC_DEFAULT)
1099 cc1 = default_calling_convention;
1100 cc_kind_t cc2 = func2->calling_convention;
1101 if (cc2 == CC_DEFAULT)
1102 cc2 = default_calling_convention;
1107 if (func1->variadic != func2->variadic)
1110 /* can parameters be compared? */
1111 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1112 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1115 /* TODO: handling of unspecified parameters not correct yet */
1117 /* all argument types must be compatible */
1118 function_parameter_t *parameter1 = func1->parameters;
1119 function_parameter_t *parameter2 = func2->parameters;
1120 for ( ; parameter1 != NULL && parameter2 != NULL;
1121 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1122 type_t *parameter1_type = skip_typeref(parameter1->type);
1123 type_t *parameter2_type = skip_typeref(parameter2->type);
1125 parameter1_type = get_unqualified_type(parameter1_type);
1126 parameter2_type = get_unqualified_type(parameter2_type);
1128 if (!types_compatible(parameter1_type, parameter2_type))
1131 /* same number of arguments? */
1132 if (parameter1 != NULL || parameter2 != NULL)
1139 * Check if two array types are compatible.
1141 static bool array_types_compatible(const array_type_t *array1,
1142 const array_type_t *array2)
1144 type_t *element_type1 = skip_typeref(array1->element_type);
1145 type_t *element_type2 = skip_typeref(array2->element_type);
1146 if (!types_compatible(element_type1, element_type2))
1149 if (!array1->size_constant || !array2->size_constant)
1152 return array1->size == array2->size;
1156 * Check if two types are compatible.
1158 bool types_compatible(const type_t *type1, const type_t *type2)
1160 assert(!is_typeref(type1));
1161 assert(!is_typeref(type2));
1163 /* shortcut: the same type is always compatible */
1167 if (!is_type_valid(type1) || !is_type_valid(type2))
1170 if (type1->base.qualifiers != type2->base.qualifiers)
1172 if (type1->kind != type2->kind)
1175 switch (type1->kind) {
1177 return function_types_compatible(&type1->function, &type2->function);
1179 return type1->atomic.akind == type2->atomic.akind;
1181 return type1->complex.akind == type2->complex.akind;
1182 case TYPE_IMAGINARY:
1183 return type1->imaginary.akind == type2->imaginary.akind;
1185 return array_types_compatible(&type1->array, &type2->array);
1187 case TYPE_POINTER: {
1188 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1189 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1190 return types_compatible(to1, to2);
1193 case TYPE_REFERENCE: {
1194 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1195 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1196 return types_compatible(to1, to2);
1199 case TYPE_COMPOUND_STRUCT:
1200 case TYPE_COMPOUND_UNION: {
1207 /* TODO: not implemented */
1211 /* not sure if this makes sense or is even needed, implement it if you
1212 * really need it! */
1213 panic("type compatibility check for bitfield type");
1216 /* Hmm, the error type should be compatible to all other types */
1219 panic("invalid type found in compatible types");
1222 panic("typerefs not skipped in compatible types?!?");
1225 /* TODO: incomplete */
1230 * Skip all typerefs and return the underlying type.
1232 type_t *skip_typeref(type_t *type)
1234 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1237 switch (type->kind) {
1240 case TYPE_TYPEDEF: {
1241 qualifiers |= type->base.qualifiers;
1243 const typedef_type_t *typedef_type = &type->typedeft;
1244 if (typedef_type->resolved_type != NULL) {
1245 type = typedef_type->resolved_type;
1248 type = typedef_type->typedefe->type;
1252 qualifiers |= type->base.qualifiers;
1253 type = type->typeoft.typeof_type;
1261 if (qualifiers != TYPE_QUALIFIER_NONE) {
1262 type_t *const copy = duplicate_type(type);
1264 /* for const with typedefed array type the element type has to be
1266 if (is_type_array(copy)) {
1267 type_t *element_type = copy->array.element_type;
1268 element_type = duplicate_type(element_type);
1269 element_type->base.qualifiers |= qualifiers;
1270 copy->array.element_type = element_type;
1272 copy->base.qualifiers |= qualifiers;
1275 type = identify_new_type(copy);
1281 unsigned get_type_size(type_t *type)
1283 switch (type->kind) {
1289 return get_atomic_type_size(type->atomic.akind);
1291 return get_atomic_type_size(type->complex.akind) * 2;
1292 case TYPE_IMAGINARY:
1293 return get_atomic_type_size(type->imaginary.akind);
1294 case TYPE_COMPOUND_UNION:
1295 layout_union_type(&type->compound);
1296 return type->compound.compound->size;
1297 case TYPE_COMPOUND_STRUCT:
1298 layout_struct_type(&type->compound);
1299 return type->compound.compound->size;
1301 return get_atomic_type_size(type->enumt.akind);
1303 return 0; /* non-const (but "address-const") */
1304 case TYPE_REFERENCE:
1306 /* TODO: make configurable by backend */
1309 /* TODO: correct if element_type is aligned? */
1310 il_size_t element_size = get_type_size(type->array.element_type);
1311 return type->array.size * element_size;
1316 return get_type_size(type->builtin.real_type);
1318 return get_type_size(type->typedeft.typedefe->type);
1320 if (type->typeoft.typeof_type) {
1321 return get_type_size(type->typeoft.typeof_type);
1323 return get_type_size(type->typeoft.expression->base.type);
1326 panic("invalid type in get_type_size");
1329 unsigned get_type_alignment(type_t *type)
1331 switch (type->kind) {
1337 return get_atomic_type_alignment(type->atomic.akind);
1339 return get_atomic_type_alignment(type->complex.akind);
1340 case TYPE_IMAGINARY:
1341 return get_atomic_type_alignment(type->imaginary.akind);
1342 case TYPE_COMPOUND_UNION:
1343 layout_union_type(&type->compound);
1344 return type->compound.compound->alignment;
1345 case TYPE_COMPOUND_STRUCT:
1346 layout_struct_type(&type->compound);
1347 return type->compound.compound->alignment;
1349 return get_atomic_type_alignment(type->enumt.akind);
1351 /* what is correct here? */
1353 case TYPE_REFERENCE:
1355 /* TODO: make configurable by backend */
1358 return get_type_alignment(type->array.element_type);
1362 return get_type_alignment(type->builtin.real_type);
1363 case TYPE_TYPEDEF: {
1364 il_alignment_t alignment
1365 = get_type_alignment(type->typedeft.typedefe->type);
1366 if (type->typedeft.typedefe->alignment > alignment)
1367 alignment = type->typedeft.typedefe->alignment;
1372 if (type->typeoft.typeof_type) {
1373 return get_type_alignment(type->typeoft.typeof_type);
1375 return get_type_alignment(type->typeoft.expression->base.type);
1378 panic("invalid type in get_type_alignment");
1381 decl_modifiers_t get_type_modifiers(const type_t *type)
1383 switch(type->kind) {
1387 case TYPE_COMPOUND_STRUCT:
1388 case TYPE_COMPOUND_UNION:
1389 return type->compound.compound->modifiers;
1391 return type->function.modifiers;
1395 case TYPE_IMAGINARY:
1396 case TYPE_REFERENCE:
1402 return get_type_modifiers(type->builtin.real_type);
1403 case TYPE_TYPEDEF: {
1404 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1405 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1409 if (type->typeoft.typeof_type) {
1410 return get_type_modifiers(type->typeoft.typeof_type);
1412 return get_type_modifiers(type->typeoft.expression->base.type);
1415 panic("invalid type found in get_type_modifiers");
1418 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1420 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1423 switch (type->base.kind) {
1425 return TYPE_QUALIFIER_NONE;
1427 qualifiers |= type->base.qualifiers;
1428 const typedef_type_t *typedef_type = &type->typedeft;
1429 if (typedef_type->resolved_type != NULL)
1430 type = typedef_type->resolved_type;
1432 type = typedef_type->typedefe->type;
1435 type = type->typeoft.typeof_type;
1438 if (skip_array_type) {
1439 type = type->array.element_type;
1448 return type->base.qualifiers | qualifiers;
1451 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1453 assert(kind <= ATOMIC_TYPE_LAST);
1454 return atomic_type_properties[kind].size;
1457 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1459 assert(kind <= ATOMIC_TYPE_LAST);
1460 return atomic_type_properties[kind].alignment;
1463 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1465 assert(kind <= ATOMIC_TYPE_LAST);
1466 return atomic_type_properties[kind].flags;
1469 atomic_type_kind_t get_intptr_kind(void)
1471 if (machine_size <= 32)
1472 return ATOMIC_TYPE_INT;
1473 else if (machine_size <= 64)
1474 return ATOMIC_TYPE_LONG;
1476 return ATOMIC_TYPE_LONGLONG;
1479 atomic_type_kind_t get_uintptr_kind(void)
1481 if (machine_size <= 32)
1482 return ATOMIC_TYPE_UINT;
1483 else if (machine_size <= 64)
1484 return ATOMIC_TYPE_ULONG;
1486 return ATOMIC_TYPE_ULONGLONG;
1490 * Find the atomic type kind representing a given size (signed).
1492 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1494 static atomic_type_kind_t kinds[32];
1497 atomic_type_kind_t kind = kinds[size];
1498 if (kind == ATOMIC_TYPE_INVALID) {
1499 static const atomic_type_kind_t possible_kinds[] = {
1504 ATOMIC_TYPE_LONGLONG
1506 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1507 if (get_atomic_type_size(possible_kinds[i]) == size) {
1508 kind = possible_kinds[i];
1518 * Find the atomic type kind representing a given size (signed).
1520 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1522 static atomic_type_kind_t kinds[32];
1525 atomic_type_kind_t kind = kinds[size];
1526 if (kind == ATOMIC_TYPE_INVALID) {
1527 static const atomic_type_kind_t possible_kinds[] = {
1532 ATOMIC_TYPE_ULONGLONG
1534 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1535 if (get_atomic_type_size(possible_kinds[i]) == size) {
1536 kind = possible_kinds[i];
1546 * Hash the given type and return the "singleton" version
1549 type_t *identify_new_type(type_t *type)
1551 type_t *result = typehash_insert(type);
1552 if (result != type) {
1553 obstack_free(type_obst, type);
1559 * Creates a new atomic type.
1561 * @param akind The kind of the atomic type.
1562 * @param qualifiers Type qualifiers for the new type.
1564 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1566 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1567 memset(type, 0, sizeof(atomic_type_t));
1569 type->kind = TYPE_ATOMIC;
1570 type->base.qualifiers = qualifiers;
1571 type->atomic.akind = akind;
1573 return identify_new_type(type);
1577 * Creates a new complex type.
1579 * @param akind The kind of the atomic type.
1580 * @param qualifiers Type qualifiers for the new type.
1582 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1584 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1585 memset(type, 0, sizeof(complex_type_t));
1587 type->kind = TYPE_COMPLEX;
1588 type->base.qualifiers = qualifiers;
1589 type->complex.akind = akind;
1591 return identify_new_type(type);
1595 * Creates a new imaginary type.
1597 * @param akind The kind of the atomic type.
1598 * @param qualifiers Type qualifiers for the new type.
1600 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1602 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1603 memset(type, 0, sizeof(imaginary_type_t));
1605 type->kind = TYPE_IMAGINARY;
1606 type->base.qualifiers = qualifiers;
1607 type->imaginary.akind = akind;
1609 return identify_new_type(type);
1613 * Creates a new pointer type.
1615 * @param points_to The points-to type for the new type.
1616 * @param qualifiers Type qualifiers for the new type.
1618 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1620 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1621 memset(type, 0, sizeof(pointer_type_t));
1623 type->kind = TYPE_POINTER;
1624 type->base.qualifiers = qualifiers;
1625 type->pointer.points_to = points_to;
1626 type->pointer.base_variable = NULL;
1628 return identify_new_type(type);
1632 * Creates a new reference type.
1634 * @param refers_to The referred-to type for the new type.
1636 type_t *make_reference_type(type_t *refers_to)
1638 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1639 memset(type, 0, sizeof(reference_type_t));
1641 type->kind = TYPE_REFERENCE;
1642 type->base.qualifiers = 0;
1643 type->reference.refers_to = refers_to;
1645 return identify_new_type(type);
1649 * Creates a new based pointer type.
1651 * @param points_to The points-to type for the new type.
1652 * @param qualifiers Type qualifiers for the new type.
1653 * @param variable The based variable
1655 type_t *make_based_pointer_type(type_t *points_to,
1656 type_qualifiers_t qualifiers, variable_t *variable)
1658 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1659 memset(type, 0, sizeof(pointer_type_t));
1661 type->kind = TYPE_POINTER;
1662 type->base.qualifiers = qualifiers;
1663 type->pointer.points_to = points_to;
1664 type->pointer.base_variable = variable;
1666 return identify_new_type(type);
1670 type_t *make_array_type(type_t *element_type, size_t size,
1671 type_qualifiers_t qualifiers)
1673 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1674 memset(type, 0, sizeof(array_type_t));
1676 type->kind = TYPE_ARRAY;
1677 type->base.qualifiers = qualifiers;
1678 type->array.element_type = element_type;
1679 type->array.size = size;
1680 type->array.size_constant = true;
1682 return identify_new_type(type);
1685 static entity_t *pack_bitfield_members_big_endian(il_size_t *struct_offset,
1686 il_alignment_t *struct_alignment, bool packed, entity_t *first)
1688 type_t *current_base_type = NULL;
1689 il_size_t offset = *struct_offset;
1690 il_alignment_t alignment = *struct_alignment;
1691 size_t bit_offset = 0;
1694 panic("packed bitfields on big-endian arch not supported yet");
1697 for (member = first; member != NULL; member = member->base.next) {
1698 if (member->kind != ENTITY_COMPOUND_MEMBER)
1701 type_t *type = member->declaration.type;
1702 if (type->kind != TYPE_BITFIELD)
1705 size_t bit_size = type->bitfield.bit_size;
1706 type_t *base_type = skip_typeref(type->bitfield.base_type);
1708 /* see if we need to start a new "bucket" */
1709 if (base_type != current_base_type || bit_size > bit_offset) {
1710 if (current_base_type != NULL)
1711 offset += get_type_size(current_base_type);
1713 current_base_type = base_type;
1714 il_alignment_t base_alignment = get_type_alignment(base_type);
1715 il_alignment_t alignment_mask = base_alignment-1;
1716 if (base_alignment > alignment)
1717 alignment = base_alignment;
1718 offset = (offset + base_alignment-1) & ~alignment_mask;
1719 bit_offset = get_type_size(base_type) * BITS_PER_BYTE;
1720 assert(bit_offset >= bit_size);
1723 bit_offset -= bit_size;
1724 member->compound_member.offset = offset;
1725 member->compound_member.bit_offset = bit_offset;
1728 if (current_base_type != NULL)
1729 offset += get_type_size(current_base_type);
1731 *struct_offset = offset;
1732 *struct_alignment = alignment;
1736 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1737 il_alignment_t *struct_alignment,
1738 bool packed, entity_t *first)
1740 il_size_t offset = *struct_offset;
1741 il_alignment_t alignment = *struct_alignment;
1742 size_t bit_offset = 0;
1745 for (member = first; member != NULL; member = member->base.next) {
1746 if (member->kind != ENTITY_COMPOUND_MEMBER)
1749 type_t *type = member->declaration.type;
1750 if (type->kind != TYPE_BITFIELD)
1753 type_t *base_type = skip_typeref(type->bitfield.base_type);
1754 il_alignment_t base_alignment = get_type_alignment(base_type);
1755 il_alignment_t alignment_mask = base_alignment-1;
1756 if (base_alignment > alignment)
1757 alignment = base_alignment;
1759 size_t bit_size = type->bitfield.bit_size;
1761 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1762 offset &= ~alignment_mask;
1763 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1765 if (bit_offset + bit_size > base_size || bit_size == 0) {
1766 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1767 offset = (offset + base_alignment-1) & ~alignment_mask;
1772 member->compound_member.offset = offset;
1773 member->compound_member.bit_offset = bit_offset;
1775 bit_offset += bit_size;
1776 offset += bit_offset / BITS_PER_BYTE;
1777 bit_offset %= BITS_PER_BYTE;
1783 *struct_offset = offset;
1784 *struct_alignment = alignment;
1788 void layout_struct_type(compound_type_t *type)
1790 assert(type->compound != NULL);
1792 compound_t *compound = type->compound;
1793 if (!compound->complete)
1795 if (type->compound->layouted)
1798 il_size_t offset = 0;
1799 il_alignment_t alignment = compound->alignment;
1800 bool need_pad = false;
1802 entity_t *entry = compound->members.entities;
1803 while (entry != NULL) {
1804 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1805 entry = entry->base.next;
1809 type_t *m_type = entry->declaration.type;
1810 type_t *skipped = skip_typeref(m_type);
1811 if (! is_type_valid(skipped)) {
1812 entry = entry->base.next;
1816 if (skipped->kind == TYPE_BITFIELD) {
1817 if (byte_order_big_endian) {
1818 entry = pack_bitfield_members_big_endian(&offset, &alignment,
1822 entry = pack_bitfield_members(&offset, &alignment,
1823 compound->packed, entry);
1828 il_alignment_t m_alignment = get_type_alignment(m_type);
1829 if (m_alignment > alignment)
1830 alignment = m_alignment;
1832 if (!compound->packed) {
1833 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1835 if (new_offset > offset) {
1837 offset = new_offset;
1841 entry->compound_member.offset = offset;
1842 offset += get_type_size(m_type);
1844 entry = entry->base.next;
1847 if (!compound->packed) {
1848 il_size_t new_offset = (offset + alignment-1) & -alignment;
1849 if (new_offset > offset) {
1851 offset = new_offset;
1856 if (warning.padded) {
1857 warningf(&compound->base.source_position, "'%T' needs padding",
1860 } else if (compound->packed && warning.packed) {
1861 warningf(&compound->base.source_position,
1862 "superfluous packed attribute on '%T'", type);
1865 compound->size = offset;
1866 compound->alignment = alignment;
1867 compound->layouted = true;
1870 void layout_union_type(compound_type_t *type)
1872 assert(type->compound != NULL);
1874 compound_t *compound = type->compound;
1875 if (! compound->complete)
1879 il_alignment_t alignment = compound->alignment;
1881 entity_t *entry = compound->members.entities;
1882 for (; entry != NULL; entry = entry->base.next) {
1883 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1886 type_t *m_type = entry->declaration.type;
1887 if (! is_type_valid(skip_typeref(m_type)))
1890 entry->compound_member.offset = 0;
1891 il_size_t m_size = get_type_size(m_type);
1894 il_alignment_t m_alignment = get_type_alignment(m_type);
1895 if (m_alignment > alignment)
1896 alignment = m_alignment;
1898 size = (size + alignment - 1) & -alignment;
1900 compound->size = size;
1901 compound->alignment = alignment;
1904 static function_parameter_t *allocate_parameter(type_t *const type)
1906 function_parameter_t *const param
1907 = obstack_alloc(type_obst, sizeof(*param));
1908 memset(param, 0, sizeof(*param));
1913 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1914 type_t *argument_type2)
1916 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1917 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1918 parameter1->next = parameter2;
1920 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1921 type->function.return_type = return_type;
1922 type->function.parameters = parameter1;
1923 type->function.linkage = LINKAGE_C;
1925 return identify_new_type(type);
1928 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1930 function_parameter_t *const parameter = allocate_parameter(argument_type);
1932 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1933 type->function.return_type = return_type;
1934 type->function.parameters = parameter;
1935 type->function.linkage = LINKAGE_C;
1937 return identify_new_type(type);
1940 type_t *make_function_1_type_variadic(type_t *return_type,
1941 type_t *argument_type)
1943 function_parameter_t *const parameter = allocate_parameter(argument_type);
1945 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1946 type->function.return_type = return_type;
1947 type->function.parameters = parameter;
1948 type->function.variadic = true;
1949 type->function.linkage = LINKAGE_C;
1951 return identify_new_type(type);
1954 type_t *make_function_0_type(type_t *return_type)
1956 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1957 type->function.return_type = return_type;
1958 type->function.parameters = NULL;
1959 type->function.linkage = LINKAGE_C;
1961 return identify_new_type(type);
1964 type_t *make_function_type(type_t *return_type, int n_types,
1965 type_t *const *argument_types,
1966 decl_modifiers_t modifiers)
1968 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1969 type->function.return_type = return_type;
1970 type->function.modifiers |= modifiers;
1971 type->function.linkage = LINKAGE_C;
1973 function_parameter_t *last = NULL;
1974 for (int i = 0; i < n_types; ++i) {
1975 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1977 type->function.parameters = parameter;
1979 last->next = parameter;
1984 return identify_new_type(type);
1988 * Debug helper. Prints the given type to stdout.
1990 static __attribute__((unused))
1991 void dbg_type(const type_t *type)
1993 print_to_file(stderr);