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 static inline bool is_po2(unsigned x)
196 return (x & (x-1)) == 0;
199 void init_types(void)
201 obstack_init(type_obst);
203 atomic_type_properties_t *props = atomic_type_properties;
205 if (char_is_signed) {
206 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
209 unsigned int_size = machine_size < 32 ? 2 : 4;
210 /* long is always 32bit on windows */
211 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
212 unsigned llong_size = machine_size < 32 ? 4 : 8;
214 props[ATOMIC_TYPE_INT].size = int_size;
215 props[ATOMIC_TYPE_INT].alignment = int_size;
216 props[ATOMIC_TYPE_UINT].size = int_size;
217 props[ATOMIC_TYPE_UINT].alignment = int_size;
218 props[ATOMIC_TYPE_LONG].size = long_size;
219 props[ATOMIC_TYPE_LONG].alignment = long_size;
220 props[ATOMIC_TYPE_ULONG].size = long_size;
221 props[ATOMIC_TYPE_ULONG].alignment = long_size;
222 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
223 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
224 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
225 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
227 /* TODO: backend specific, need a way to query the backend for this.
228 * The following are good settings for x86 */
229 if (machine_size <= 32) {
230 props[ATOMIC_TYPE_FLOAT].alignment = 4;
231 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
232 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
233 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
234 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
236 props[ATOMIC_TYPE_FLOAT].alignment = 4;
237 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
238 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
239 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
240 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
243 if (long_double_size > 0) {
244 props[ATOMIC_TYPE_LONG_DOUBLE].size = long_double_size;
245 if (is_po2(long_double_size)) {
246 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = long_double_size;
249 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
252 /* TODO: make this configurable for platforms which do not use byte sized
254 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
256 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
259 void exit_types(void)
261 obstack_free(type_obst, NULL);
264 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
266 size_t sep = q & QUAL_SEP_START ? 0 : 1;
267 if (qualifiers & TYPE_QUALIFIER_CONST) {
268 print_string(" const" + sep);
271 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
272 print_string(" volatile" + sep);
275 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
276 print_string(" restrict" + sep);
279 if (sep == 0 && q & QUAL_SEP_END)
283 const char *get_atomic_kind_name(atomic_type_kind_t kind)
286 case ATOMIC_TYPE_INVALID: break;
287 case ATOMIC_TYPE_VOID: return "void";
288 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
289 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
290 case ATOMIC_TYPE_CHAR: return "char";
291 case ATOMIC_TYPE_SCHAR: return "signed char";
292 case ATOMIC_TYPE_UCHAR: return "unsigned char";
293 case ATOMIC_TYPE_INT: return "int";
294 case ATOMIC_TYPE_UINT: return "unsigned int";
295 case ATOMIC_TYPE_SHORT: return "short";
296 case ATOMIC_TYPE_USHORT: return "unsigned short";
297 case ATOMIC_TYPE_LONG: return "long";
298 case ATOMIC_TYPE_ULONG: return "unsigned long";
299 case ATOMIC_TYPE_LONGLONG: return "long long";
300 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
301 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
302 case ATOMIC_TYPE_FLOAT: return "float";
303 case ATOMIC_TYPE_DOUBLE: return "double";
305 return "INVALIDATOMIC";
309 * Prints the name of an atomic type kinds.
311 * @param kind The type kind.
313 static void print_atomic_kinds(atomic_type_kind_t kind)
315 const char *s = get_atomic_kind_name(kind);
320 * Prints the name of an atomic type.
322 * @param type The type.
324 static void print_atomic_type(const atomic_type_t *type)
326 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
327 print_atomic_kinds(type->akind);
331 * Prints the name of a complex type.
333 * @param type The type.
335 static void print_complex_type(const complex_type_t *type)
337 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
338 print_string("_Complex");
339 print_atomic_kinds(type->akind);
343 * Prints the name of an imaginary type.
345 * @param type The type.
347 static void print_imaginary_type(const imaginary_type_t *type)
349 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
350 print_string("_Imaginary ");
351 print_atomic_kinds(type->akind);
355 * Print the first part (the prefix) of a type.
357 * @param type The type to print.
359 static void print_function_type_pre(const function_type_t *type)
361 switch (type->linkage) {
362 case LINKAGE_INVALID:
367 print_string("extern \"C\" ");
371 if (!(c_mode & _CXX))
372 print_string("extern \"C++\" ");
376 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
378 intern_print_type_pre(type->return_type);
380 cc_kind_t cc = type->calling_convention;
383 case CC_CDECL: print_string(" __cdecl"); break;
384 case CC_STDCALL: print_string(" __stdcall"); break;
385 case CC_FASTCALL: print_string(" __fastcall"); break;
386 case CC_THISCALL: print_string(" __thiscall"); break;
388 if (default_calling_convention != CC_CDECL) {
389 /* show the default calling convention if its not cdecl */
390 cc = default_calling_convention;
398 * Print the second part (the postfix) of a type.
400 * @param type The type to print.
402 static void print_function_type_post(const function_type_t *type,
403 const scope_t *parameters)
407 if (parameters == NULL) {
408 function_parameter_t *parameter = type->parameters;
409 for( ; parameter != NULL; parameter = parameter->next) {
415 print_type(parameter->type);
418 entity_t *parameter = parameters->entities;
419 for (; parameter != NULL; parameter = parameter->base.next) {
420 if (parameter->kind != ENTITY_PARAMETER)
428 const type_t *const param_type = parameter->declaration.type;
429 if (param_type == NULL) {
430 print_string(parameter->base.symbol->string);
432 print_type_ext(param_type, parameter->base.symbol, NULL);
436 if (type->variadic) {
444 if (first && !type->unspecified_parameters) {
445 print_string("void");
449 intern_print_type_post(type->return_type);
453 * Prints the prefix part of a pointer type.
455 * @param type The pointer type.
457 static void print_pointer_type_pre(const pointer_type_t *type)
459 type_t const *const points_to = type->points_to;
460 intern_print_type_pre(points_to);
461 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
463 variable_t *const variable = type->base_variable;
464 if (variable != NULL) {
465 print_string(" __based(");
466 print_string(variable->base.base.symbol->string);
470 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
474 * Prints the postfix part of a pointer type.
476 * @param type The pointer type.
478 static void print_pointer_type_post(const pointer_type_t *type)
480 type_t const *const points_to = type->points_to;
481 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
483 intern_print_type_post(points_to);
487 * Prints the prefix part of a reference type.
489 * @param type The reference type.
491 static void print_reference_type_pre(const reference_type_t *type)
493 type_t const *const refers_to = type->refers_to;
494 intern_print_type_pre(refers_to);
495 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
501 * Prints the postfix part of a reference type.
503 * @param type The reference type.
505 static void print_reference_type_post(const reference_type_t *type)
507 type_t const *const refers_to = type->refers_to;
508 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
510 intern_print_type_post(refers_to);
514 * Prints the prefix part of an array type.
516 * @param type The array type.
518 static void print_array_type_pre(const array_type_t *type)
520 intern_print_type_pre(type->element_type);
524 * Prints the postfix part of an array type.
526 * @param type The array type.
528 static void print_array_type_post(const array_type_t *type)
531 if (type->is_static) {
532 print_string("static ");
534 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
535 if (type->size_expression != NULL
536 && (print_implicit_array_size || !type->has_implicit_size)) {
537 print_expression(type->size_expression);
540 intern_print_type_post(type->element_type);
544 * Prints the postfix part of a bitfield type.
546 * @param type The array type.
548 static void print_bitfield_type_post(const bitfield_type_t *type)
551 print_expression(type->size_expression);
552 intern_print_type_post(type->base_type);
556 * Prints an enum definition.
558 * @param declaration The enum's type declaration.
560 void print_enum_definition(const enum_t *enume)
566 entity_t *entry = enume->base.next;
567 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
568 entry = entry->base.next) {
571 print_string(entry->base.symbol->string);
572 if (entry->enum_value.value != NULL) {
575 /* skip the implicit cast */
576 expression_t *expression = entry->enum_value.value;
577 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
578 expression = expression->unary.value;
580 print_expression(expression);
591 * Prints an enum type.
593 * @param type The enum type.
595 static void print_type_enum(const enum_type_t *type)
597 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
598 print_string("enum ");
600 enum_t *enume = type->enume;
601 symbol_t *symbol = enume->base.symbol;
602 if (symbol != NULL) {
603 print_string(symbol->string);
605 print_enum_definition(enume);
610 * Print the compound part of a compound type.
612 void print_compound_definition(const compound_t *compound)
617 entity_t *entity = compound->members.entities;
618 for( ; entity != NULL; entity = entity->base.next) {
619 if (entity->kind != ENTITY_COMPOUND_MEMBER)
623 print_entity(entity);
630 if (compound->modifiers & DM_TRANSPARENT_UNION) {
631 print_string("__attribute__((__transparent_union__))");
636 * Prints a compound type.
638 * @param type The compound type.
640 static void print_compound_type(const compound_type_t *type)
642 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
644 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
645 print_string("struct ");
647 assert(type->base.kind == TYPE_COMPOUND_UNION);
648 print_string("union ");
651 compound_t *compound = type->compound;
652 symbol_t *symbol = compound->base.symbol;
653 if (symbol != NULL) {
654 print_string(symbol->string);
656 print_compound_definition(compound);
661 * Prints the prefix part of a typedef type.
663 * @param type The typedef type.
665 static void print_typedef_type_pre(const typedef_type_t *const type)
667 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
668 print_string(type->typedefe->base.symbol->string);
672 * Prints the prefix part of a typeof type.
674 * @param type The typeof type.
676 static void print_typeof_type_pre(const typeof_type_t *const type)
678 print_string("typeof(");
679 if (type->expression != NULL) {
680 print_expression(type->expression);
682 print_type(type->typeof_type);
688 * Prints the prefix part of a type.
690 * @param type The type.
692 static void intern_print_type_pre(const type_t *const type)
696 print_string("<error>");
699 print_string("<invalid>");
702 print_type_enum(&type->enumt);
705 print_atomic_type(&type->atomic);
708 print_complex_type(&type->complex);
711 print_imaginary_type(&type->imaginary);
713 case TYPE_COMPOUND_STRUCT:
714 case TYPE_COMPOUND_UNION:
715 print_compound_type(&type->compound);
718 print_function_type_pre(&type->function);
721 print_pointer_type_pre(&type->pointer);
724 print_reference_type_pre(&type->reference);
727 intern_print_type_pre(type->bitfield.base_type);
730 print_array_type_pre(&type->array);
733 print_typedef_type_pre(&type->typedeft);
736 print_typeof_type_pre(&type->typeoft);
739 print_string("unknown");
743 * Prints the postfix part of a type.
745 * @param type The type.
747 static void intern_print_type_post(const type_t *const type)
751 print_function_type_post(&type->function, NULL);
754 print_pointer_type_post(&type->pointer);
757 print_reference_type_post(&type->reference);
760 print_array_type_post(&type->array);
763 print_bitfield_type_post(&type->bitfield);
771 case TYPE_COMPOUND_STRUCT:
772 case TYPE_COMPOUND_UNION:
782 * @param type The type.
784 void print_type(const type_t *const type)
786 print_type_ext(type, NULL, NULL);
789 void print_type_ext(const type_t *const type, const symbol_t *symbol,
790 const scope_t *parameters)
792 intern_print_type_pre(type);
793 if (symbol != NULL) {
795 print_string(symbol->string);
797 if (type->kind == TYPE_FUNCTION) {
798 print_function_type_post(&type->function, parameters);
800 intern_print_type_post(type);
807 * @param type The type to copy.
808 * @return A copy of the type.
810 * @note This does not produce a deep copy!
812 type_t *duplicate_type(const type_t *type)
814 size_t size = get_type_struct_size(type->kind);
816 type_t *copy = obstack_alloc(type_obst, size);
817 memcpy(copy, type, size);
818 copy->base.firm_type = NULL;
824 * Returns the unqualified type of a given type.
826 * @param type The type.
827 * @returns The unqualified type.
829 type_t *get_unqualified_type(type_t *type)
831 assert(!is_typeref(type));
833 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
836 type_t *unqualified_type = duplicate_type(type);
837 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
839 return identify_new_type(unqualified_type);
842 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
844 type_t *type = skip_typeref(orig_type);
847 if (is_type_array(type)) {
848 /* For array types the element type has to be adjusted */
849 type_t *element_type = type->array.element_type;
850 type_t *qual_element_type = get_qualified_type(element_type, qual);
852 if (qual_element_type == element_type)
855 copy = duplicate_type(type);
856 copy->array.element_type = qual_element_type;
857 } else if (is_type_valid(type)) {
858 if ((type->base.qualifiers & qual) == (int)qual)
861 copy = duplicate_type(type);
862 copy->base.qualifiers |= qual;
867 return identify_new_type(copy);
871 * Check if a type is valid.
873 * @param type The type to check.
874 * @return true if type represents a valid type.
876 bool type_valid(const type_t *type)
878 return type->kind != TYPE_INVALID;
881 static bool test_atomic_type_flag(atomic_type_kind_t kind,
882 atomic_type_flag_t flag)
884 assert(kind <= ATOMIC_TYPE_LAST);
885 return (atomic_type_properties[kind].flags & flag) != 0;
889 * Returns true if the given type is an integer type.
891 * @param type The type to check.
892 * @return True if type is an integer type.
894 bool is_type_integer(const type_t *type)
896 assert(!is_typeref(type));
898 if (type->kind == TYPE_ENUM)
900 if (type->kind == TYPE_BITFIELD)
903 if (type->kind != TYPE_ATOMIC)
906 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
910 * Returns true if the given type is an enum type.
912 * @param type The type to check.
913 * @return True if type is an enum type.
915 bool is_type_enum(const type_t *type)
917 assert(!is_typeref(type));
918 return type->kind == TYPE_ENUM;
922 * Returns true if the given type is an floating point type.
924 * @param type The type to check.
925 * @return True if type is a floating point type.
927 bool is_type_float(const type_t *type)
929 assert(!is_typeref(type));
931 if (type->kind != TYPE_ATOMIC)
934 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
938 * Returns true if the given type is an complex type.
940 * @param type The type to check.
941 * @return True if type is a complex type.
943 bool is_type_complex(const type_t *type)
945 assert(!is_typeref(type));
947 if (type->kind != TYPE_ATOMIC)
950 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
954 * Returns true if the given type is a signed type.
956 * @param type The type to check.
957 * @return True if type is a signed type.
959 bool is_type_signed(const type_t *type)
961 assert(!is_typeref(type));
963 /* enum types are int for now */
964 if (type->kind == TYPE_ENUM)
966 if (type->kind == TYPE_BITFIELD)
967 return is_type_signed(type->bitfield.base_type);
969 if (type->kind != TYPE_ATOMIC)
972 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
976 * Returns true if the given type represents an arithmetic type.
978 * @param type The type to check.
979 * @return True if type represents an arithmetic type.
981 bool is_type_arithmetic(const type_t *type)
983 assert(!is_typeref(type));
990 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
992 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
994 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
1001 * Returns true if the given type is an integer or float type.
1003 * @param type The type to check.
1004 * @return True if type is an integer or float type.
1006 bool is_type_real(const type_t *type)
1009 return is_type_integer(type) || is_type_float(type);
1013 * Returns true if the given type represents a scalar type.
1015 * @param type The type to check.
1016 * @return True if type represents a scalar type.
1018 bool is_type_scalar(const type_t *type)
1020 assert(!is_typeref(type));
1022 if (type->kind == TYPE_POINTER)
1025 return is_type_arithmetic(type);
1029 * Check if a given type is incomplete.
1031 * @param type The type to check.
1032 * @return True if the given type is incomplete (ie. just forward).
1034 bool is_type_incomplete(const type_t *type)
1036 assert(!is_typeref(type));
1038 switch(type->kind) {
1039 case TYPE_COMPOUND_STRUCT:
1040 case TYPE_COMPOUND_UNION: {
1041 const compound_type_t *compound_type = &type->compound;
1042 return !compound_type->compound->complete;
1048 return type->array.size_expression == NULL
1049 && !type->array.size_constant;
1052 return type->atomic.akind == ATOMIC_TYPE_VOID;
1055 return type->complex.akind == ATOMIC_TYPE_VOID;
1057 case TYPE_IMAGINARY:
1058 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1063 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);
1083 * Check if two function types are compatible.
1085 static bool function_types_compatible(const function_type_t *func1,
1086 const function_type_t *func2)
1088 const type_t* const ret1 = skip_typeref(func1->return_type);
1089 const type_t* const ret2 = skip_typeref(func2->return_type);
1090 if (!types_compatible(ret1, ret2))
1093 if (func1->linkage != func2->linkage)
1096 cc_kind_t cc1 = func1->calling_convention;
1097 if (cc1 == CC_DEFAULT)
1098 cc1 = default_calling_convention;
1099 cc_kind_t cc2 = func2->calling_convention;
1100 if (cc2 == CC_DEFAULT)
1101 cc2 = default_calling_convention;
1106 if (func1->variadic != func2->variadic)
1109 /* can parameters be compared? */
1110 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1111 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1114 /* TODO: handling of unspecified parameters not correct yet */
1116 /* all argument types must be compatible */
1117 function_parameter_t *parameter1 = func1->parameters;
1118 function_parameter_t *parameter2 = func2->parameters;
1119 for ( ; parameter1 != NULL && parameter2 != NULL;
1120 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1121 type_t *parameter1_type = skip_typeref(parameter1->type);
1122 type_t *parameter2_type = skip_typeref(parameter2->type);
1124 parameter1_type = get_unqualified_type(parameter1_type);
1125 parameter2_type = get_unqualified_type(parameter2_type);
1127 if (!types_compatible(parameter1_type, parameter2_type))
1130 /* same number of arguments? */
1131 if (parameter1 != NULL || parameter2 != NULL)
1138 * Check if two array types are compatible.
1140 static bool array_types_compatible(const array_type_t *array1,
1141 const array_type_t *array2)
1143 type_t *element_type1 = skip_typeref(array1->element_type);
1144 type_t *element_type2 = skip_typeref(array2->element_type);
1145 if (!types_compatible(element_type1, element_type2))
1148 if (!array1->size_constant || !array2->size_constant)
1151 return array1->size == array2->size;
1155 * Check if two types are compatible.
1157 bool types_compatible(const type_t *type1, const type_t *type2)
1159 assert(!is_typeref(type1));
1160 assert(!is_typeref(type2));
1162 /* shortcut: the same type is always compatible */
1166 if (!is_type_valid(type1) || !is_type_valid(type2))
1169 if (type1->base.qualifiers != type2->base.qualifiers)
1171 if (type1->kind != type2->kind)
1174 switch (type1->kind) {
1176 return function_types_compatible(&type1->function, &type2->function);
1178 return type1->atomic.akind == type2->atomic.akind;
1180 return type1->complex.akind == type2->complex.akind;
1181 case TYPE_IMAGINARY:
1182 return type1->imaginary.akind == type2->imaginary.akind;
1184 return array_types_compatible(&type1->array, &type2->array);
1186 case TYPE_POINTER: {
1187 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1188 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1189 return types_compatible(to1, to2);
1192 case TYPE_REFERENCE: {
1193 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1194 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1195 return types_compatible(to1, to2);
1198 case TYPE_COMPOUND_STRUCT:
1199 case TYPE_COMPOUND_UNION: {
1205 /* TODO: not implemented */
1209 /* not sure if this makes sense or is even needed, implement it if you
1210 * really need it! */
1211 panic("type compatibility check for bitfield type");
1214 /* Hmm, the error type should be compatible to all other types */
1217 panic("invalid type found in compatible types");
1220 panic("typerefs not skipped in compatible types?!?");
1223 /* TODO: incomplete */
1228 * Skip all typerefs and return the underlying type.
1230 type_t *skip_typeref(type_t *type)
1232 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1235 switch (type->kind) {
1238 case TYPE_TYPEDEF: {
1239 qualifiers |= type->base.qualifiers;
1241 const typedef_type_t *typedef_type = &type->typedeft;
1242 if (typedef_type->resolved_type != NULL) {
1243 type = typedef_type->resolved_type;
1246 type = typedef_type->typedefe->type;
1250 qualifiers |= type->base.qualifiers;
1251 type = type->typeoft.typeof_type;
1259 if (qualifiers != TYPE_QUALIFIER_NONE) {
1260 type_t *const copy = duplicate_type(type);
1262 /* for const with typedefed array type the element type has to be
1264 if (is_type_array(copy)) {
1265 type_t *element_type = copy->array.element_type;
1266 element_type = duplicate_type(element_type);
1267 element_type->base.qualifiers |= qualifiers;
1268 copy->array.element_type = element_type;
1270 copy->base.qualifiers |= qualifiers;
1273 type = identify_new_type(copy);
1279 unsigned get_type_size(type_t *type)
1281 switch (type->kind) {
1287 return get_atomic_type_size(type->atomic.akind);
1289 return get_atomic_type_size(type->complex.akind) * 2;
1290 case TYPE_IMAGINARY:
1291 return get_atomic_type_size(type->imaginary.akind);
1292 case TYPE_COMPOUND_UNION:
1293 layout_union_type(&type->compound);
1294 return type->compound.compound->size;
1295 case TYPE_COMPOUND_STRUCT:
1296 layout_struct_type(&type->compound);
1297 return type->compound.compound->size;
1299 return get_atomic_type_size(type->enumt.akind);
1301 return 0; /* non-const (but "address-const") */
1302 case TYPE_REFERENCE:
1304 /* TODO: make configurable by backend */
1307 /* TODO: correct if element_type is aligned? */
1308 il_size_t element_size = get_type_size(type->array.element_type);
1309 return type->array.size * element_size;
1314 return get_type_size(type->typedeft.typedefe->type);
1316 if (type->typeoft.typeof_type) {
1317 return get_type_size(type->typeoft.typeof_type);
1319 return get_type_size(type->typeoft.expression->base.type);
1322 panic("invalid type in get_type_size");
1325 unsigned get_type_alignment(type_t *type)
1327 switch (type->kind) {
1333 return get_atomic_type_alignment(type->atomic.akind);
1335 return get_atomic_type_alignment(type->complex.akind);
1336 case TYPE_IMAGINARY:
1337 return get_atomic_type_alignment(type->imaginary.akind);
1338 case TYPE_COMPOUND_UNION:
1339 layout_union_type(&type->compound);
1340 return type->compound.compound->alignment;
1341 case TYPE_COMPOUND_STRUCT:
1342 layout_struct_type(&type->compound);
1343 return type->compound.compound->alignment;
1345 return get_atomic_type_alignment(type->enumt.akind);
1347 /* what is correct here? */
1349 case TYPE_REFERENCE:
1351 /* TODO: make configurable by backend */
1354 return get_type_alignment(type->array.element_type);
1357 case TYPE_TYPEDEF: {
1358 il_alignment_t alignment
1359 = get_type_alignment(type->typedeft.typedefe->type);
1360 if (type->typedeft.typedefe->alignment > alignment)
1361 alignment = type->typedeft.typedefe->alignment;
1366 if (type->typeoft.typeof_type) {
1367 return get_type_alignment(type->typeoft.typeof_type);
1369 return get_type_alignment(type->typeoft.expression->base.type);
1372 panic("invalid type in get_type_alignment");
1375 decl_modifiers_t get_type_modifiers(const type_t *type)
1377 switch(type->kind) {
1381 case TYPE_COMPOUND_STRUCT:
1382 case TYPE_COMPOUND_UNION:
1383 return type->compound.compound->modifiers;
1385 return type->function.modifiers;
1389 case TYPE_IMAGINARY:
1390 case TYPE_REFERENCE:
1395 case TYPE_TYPEDEF: {
1396 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1397 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1401 if (type->typeoft.typeof_type) {
1402 return get_type_modifiers(type->typeoft.typeof_type);
1404 return get_type_modifiers(type->typeoft.expression->base.type);
1407 panic("invalid type found in get_type_modifiers");
1410 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1412 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1415 switch (type->base.kind) {
1417 return TYPE_QUALIFIER_NONE;
1419 qualifiers |= type->base.qualifiers;
1420 const typedef_type_t *typedef_type = &type->typedeft;
1421 if (typedef_type->resolved_type != NULL)
1422 type = typedef_type->resolved_type;
1424 type = typedef_type->typedefe->type;
1427 type = type->typeoft.typeof_type;
1430 if (skip_array_type) {
1431 type = type->array.element_type;
1440 return type->base.qualifiers | qualifiers;
1443 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1445 assert(kind <= ATOMIC_TYPE_LAST);
1446 return atomic_type_properties[kind].size;
1449 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1451 assert(kind <= ATOMIC_TYPE_LAST);
1452 return atomic_type_properties[kind].alignment;
1455 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1457 assert(kind <= ATOMIC_TYPE_LAST);
1458 return atomic_type_properties[kind].flags;
1461 atomic_type_kind_t get_intptr_kind(void)
1463 if (machine_size <= 32)
1464 return ATOMIC_TYPE_INT;
1465 else if (machine_size <= 64)
1466 return ATOMIC_TYPE_LONG;
1468 return ATOMIC_TYPE_LONGLONG;
1471 atomic_type_kind_t get_uintptr_kind(void)
1473 if (machine_size <= 32)
1474 return ATOMIC_TYPE_UINT;
1475 else if (machine_size <= 64)
1476 return ATOMIC_TYPE_ULONG;
1478 return ATOMIC_TYPE_ULONGLONG;
1482 * Find the atomic type kind representing a given size (signed).
1484 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1486 static atomic_type_kind_t kinds[32];
1489 atomic_type_kind_t kind = kinds[size];
1490 if (kind == ATOMIC_TYPE_INVALID) {
1491 static const atomic_type_kind_t possible_kinds[] = {
1496 ATOMIC_TYPE_LONGLONG
1498 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1499 if (get_atomic_type_size(possible_kinds[i]) == size) {
1500 kind = possible_kinds[i];
1510 * Find the atomic type kind representing a given size (signed).
1512 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1514 static atomic_type_kind_t kinds[32];
1517 atomic_type_kind_t kind = kinds[size];
1518 if (kind == ATOMIC_TYPE_INVALID) {
1519 static const atomic_type_kind_t possible_kinds[] = {
1524 ATOMIC_TYPE_ULONGLONG
1526 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1527 if (get_atomic_type_size(possible_kinds[i]) == size) {
1528 kind = possible_kinds[i];
1538 * Hash the given type and return the "singleton" version
1541 type_t *identify_new_type(type_t *type)
1543 type_t *result = typehash_insert(type);
1544 if (result != type) {
1545 obstack_free(type_obst, type);
1551 * Creates a new atomic type.
1553 * @param akind The kind of the atomic type.
1554 * @param qualifiers Type qualifiers for the new type.
1556 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1558 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1559 memset(type, 0, sizeof(atomic_type_t));
1561 type->kind = TYPE_ATOMIC;
1562 type->base.qualifiers = qualifiers;
1563 type->atomic.akind = akind;
1565 return identify_new_type(type);
1569 * Creates a new complex type.
1571 * @param akind The kind of the atomic type.
1572 * @param qualifiers Type qualifiers for the new type.
1574 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1576 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1577 memset(type, 0, sizeof(complex_type_t));
1579 type->kind = TYPE_COMPLEX;
1580 type->base.qualifiers = qualifiers;
1581 type->complex.akind = akind;
1583 return identify_new_type(type);
1587 * Creates a new imaginary type.
1589 * @param akind The kind of the atomic type.
1590 * @param qualifiers Type qualifiers for the new type.
1592 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1594 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1595 memset(type, 0, sizeof(imaginary_type_t));
1597 type->kind = TYPE_IMAGINARY;
1598 type->base.qualifiers = qualifiers;
1599 type->imaginary.akind = akind;
1601 return identify_new_type(type);
1605 * Creates a new pointer type.
1607 * @param points_to The points-to type for the new type.
1608 * @param qualifiers Type qualifiers for the new type.
1610 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1612 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1613 memset(type, 0, sizeof(pointer_type_t));
1615 type->kind = TYPE_POINTER;
1616 type->base.qualifiers = qualifiers;
1617 type->pointer.points_to = points_to;
1618 type->pointer.base_variable = NULL;
1620 return identify_new_type(type);
1624 * Creates a new reference type.
1626 * @param refers_to The referred-to type for the new type.
1628 type_t *make_reference_type(type_t *refers_to)
1630 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1631 memset(type, 0, sizeof(reference_type_t));
1633 type->kind = TYPE_REFERENCE;
1634 type->base.qualifiers = 0;
1635 type->reference.refers_to = refers_to;
1637 return identify_new_type(type);
1641 * Creates a new based pointer type.
1643 * @param points_to The points-to type for the new type.
1644 * @param qualifiers Type qualifiers for the new type.
1645 * @param variable The based variable
1647 type_t *make_based_pointer_type(type_t *points_to,
1648 type_qualifiers_t qualifiers, variable_t *variable)
1650 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1651 memset(type, 0, sizeof(pointer_type_t));
1653 type->kind = TYPE_POINTER;
1654 type->base.qualifiers = qualifiers;
1655 type->pointer.points_to = points_to;
1656 type->pointer.base_variable = variable;
1658 return identify_new_type(type);
1662 type_t *make_array_type(type_t *element_type, size_t size,
1663 type_qualifiers_t qualifiers)
1665 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1666 memset(type, 0, sizeof(array_type_t));
1668 type->kind = TYPE_ARRAY;
1669 type->base.qualifiers = qualifiers;
1670 type->array.element_type = element_type;
1671 type->array.size = size;
1672 type->array.size_constant = true;
1674 return identify_new_type(type);
1677 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1678 il_alignment_t *struct_alignment,
1679 bool packed, entity_t *first)
1681 il_size_t offset = *struct_offset;
1682 il_alignment_t alignment = *struct_alignment;
1683 size_t bit_offset = 0;
1686 for (member = first; member != NULL; member = member->base.next) {
1687 if (member->kind != ENTITY_COMPOUND_MEMBER)
1690 type_t *type = member->declaration.type;
1691 if (type->kind != TYPE_BITFIELD)
1694 type_t *base_type = skip_typeref(type->bitfield.base_type);
1695 il_alignment_t base_alignment = get_type_alignment(base_type);
1696 il_alignment_t alignment_mask = base_alignment-1;
1697 if (base_alignment > alignment)
1698 alignment = base_alignment;
1700 size_t bit_size = type->bitfield.bit_size;
1702 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1703 offset &= ~alignment_mask;
1704 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1706 if (bit_offset + bit_size > base_size || bit_size == 0) {
1707 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1708 offset = (offset + base_alignment-1) & ~alignment_mask;
1713 if (byte_order_big_endian) {
1714 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1715 member->compound_member.offset = offset & ~alignment_mask;
1716 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1718 member->compound_member.offset = offset;
1719 member->compound_member.bit_offset = bit_offset;
1722 bit_offset += bit_size;
1723 offset += bit_offset / BITS_PER_BYTE;
1724 bit_offset %= BITS_PER_BYTE;
1730 *struct_offset = offset;
1731 *struct_alignment = alignment;
1735 void layout_struct_type(compound_type_t *type)
1737 assert(type->compound != NULL);
1739 compound_t *compound = type->compound;
1740 if (!compound->complete)
1742 if (type->compound->layouted)
1745 il_size_t offset = 0;
1746 il_alignment_t alignment = compound->alignment;
1747 bool need_pad = false;
1749 entity_t *entry = compound->members.entities;
1750 while (entry != NULL) {
1751 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1752 entry = entry->base.next;
1756 type_t *m_type = entry->declaration.type;
1757 type_t *skipped = skip_typeref(m_type);
1758 if (! is_type_valid(skipped)) {
1759 entry = entry->base.next;
1763 if (skipped->kind == TYPE_BITFIELD) {
1764 entry = pack_bitfield_members(&offset, &alignment,
1765 compound->packed, entry);
1769 il_alignment_t m_alignment = get_type_alignment(m_type);
1770 if (m_alignment > alignment)
1771 alignment = m_alignment;
1773 if (!compound->packed) {
1774 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1776 if (new_offset > offset) {
1778 offset = new_offset;
1782 entry->compound_member.offset = offset;
1783 offset += get_type_size(m_type);
1785 entry = entry->base.next;
1788 if (!compound->packed) {
1789 il_size_t new_offset = (offset + alignment-1) & -alignment;
1790 if (new_offset > offset) {
1792 offset = new_offset;
1796 source_position_t const *const pos = &compound->base.source_position;
1798 warningf(WARN_PADDED, pos, "'%T' needs padding", type);
1799 } else if (compound->packed) {
1800 warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
1803 compound->size = offset;
1804 compound->alignment = alignment;
1805 compound->layouted = true;
1808 void layout_union_type(compound_type_t *type)
1810 assert(type->compound != NULL);
1812 compound_t *compound = type->compound;
1813 if (! compound->complete)
1817 il_alignment_t alignment = compound->alignment;
1819 entity_t *entry = compound->members.entities;
1820 for (; entry != NULL; entry = entry->base.next) {
1821 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1824 type_t *m_type = entry->declaration.type;
1825 if (! is_type_valid(skip_typeref(m_type)))
1828 entry->compound_member.offset = 0;
1829 il_size_t m_size = get_type_size(m_type);
1832 il_alignment_t m_alignment = get_type_alignment(m_type);
1833 if (m_alignment > alignment)
1834 alignment = m_alignment;
1836 size = (size + alignment - 1) & -alignment;
1838 compound->size = size;
1839 compound->alignment = alignment;
1842 static function_parameter_t *allocate_parameter(type_t *const type)
1844 function_parameter_t *const param
1845 = obstack_alloc(type_obst, sizeof(*param));
1846 memset(param, 0, sizeof(*param));
1851 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1852 type_t *argument_type2)
1854 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1855 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1856 parameter1->next = parameter2;
1858 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1859 type->function.return_type = return_type;
1860 type->function.parameters = parameter1;
1861 type->function.linkage = LINKAGE_C;
1863 return identify_new_type(type);
1866 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1868 function_parameter_t *const parameter = allocate_parameter(argument_type);
1870 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1871 type->function.return_type = return_type;
1872 type->function.parameters = parameter;
1873 type->function.linkage = LINKAGE_C;
1875 return identify_new_type(type);
1878 type_t *make_function_1_type_variadic(type_t *return_type,
1879 type_t *argument_type)
1881 function_parameter_t *const parameter = allocate_parameter(argument_type);
1883 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1884 type->function.return_type = return_type;
1885 type->function.parameters = parameter;
1886 type->function.variadic = true;
1887 type->function.linkage = LINKAGE_C;
1889 return identify_new_type(type);
1892 type_t *make_function_0_type(type_t *return_type)
1894 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1895 type->function.return_type = return_type;
1896 type->function.parameters = NULL;
1897 type->function.linkage = LINKAGE_C;
1899 return identify_new_type(type);
1902 type_t *make_function_type(type_t *return_type, int n_types,
1903 type_t *const *argument_types,
1904 decl_modifiers_t modifiers)
1906 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1907 type->function.return_type = return_type;
1908 type->function.modifiers |= modifiers;
1909 type->function.linkage = LINKAGE_C;
1911 function_parameter_t *last = NULL;
1912 for (int i = 0; i < n_types; ++i) {
1913 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1915 type->function.parameters = parameter;
1917 last->next = parameter;
1922 return identify_new_type(type);
1926 * Debug helper. Prints the given type to stdout.
1928 static __attribute__((unused))
1929 void dbg_type(const type_t *type)
1931 print_to_file(stderr);