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] = {
187 .alignment = (unsigned) -1,
188 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
189 | ATOMIC_TYPE_FLAG_SIGNED,
191 /* complex and imaginary types are set in init_types */
194 void init_types(void)
196 obstack_init(type_obst);
198 atomic_type_properties_t *props = atomic_type_properties;
200 if (char_is_signed) {
201 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
204 unsigned int_size = machine_size < 32 ? 2 : 4;
205 /* long is always 32bit on windows */
206 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
207 unsigned llong_size = machine_size < 32 ? 4 : 8;
209 props[ATOMIC_TYPE_INT].size = int_size;
210 props[ATOMIC_TYPE_INT].alignment = int_size;
211 props[ATOMIC_TYPE_UINT].size = int_size;
212 props[ATOMIC_TYPE_UINT].alignment = int_size;
213 props[ATOMIC_TYPE_LONG].size = long_size;
214 props[ATOMIC_TYPE_LONG].alignment = long_size;
215 props[ATOMIC_TYPE_ULONG].size = long_size;
216 props[ATOMIC_TYPE_ULONG].alignment = long_size;
217 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
218 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
219 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
220 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
222 /* TODO: backend specific, need a way to query the backend for this.
223 * The following are good settings for x86 */
224 if (machine_size <= 32) {
225 props[ATOMIC_TYPE_FLOAT].alignment = 4;
226 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
227 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
228 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
229 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
231 props[ATOMIC_TYPE_FLOAT].alignment = 4;
232 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
233 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
234 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
235 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
237 if (force_long_double_size > 0) {
238 props[ATOMIC_TYPE_LONG_DOUBLE].size = force_long_double_size;
239 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = force_long_double_size;
242 /* TODO: make this configurable for platforms which do not use byte sized
244 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
246 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
249 void exit_types(void)
251 obstack_free(type_obst, NULL);
254 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
256 size_t sep = q & QUAL_SEP_START ? 0 : 1;
257 if (qualifiers & TYPE_QUALIFIER_CONST) {
258 print_string(" const" + sep);
261 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
262 print_string(" volatile" + sep);
265 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
266 print_string(" restrict" + sep);
269 if (sep == 0 && q & QUAL_SEP_END)
273 const char *get_atomic_kind_name(atomic_type_kind_t kind)
276 case ATOMIC_TYPE_INVALID: break;
277 case ATOMIC_TYPE_VOID: return "void";
278 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
279 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
280 case ATOMIC_TYPE_CHAR: return "char";
281 case ATOMIC_TYPE_SCHAR: return "signed char";
282 case ATOMIC_TYPE_UCHAR: return "unsigned char";
283 case ATOMIC_TYPE_INT: return "int";
284 case ATOMIC_TYPE_UINT: return "unsigned int";
285 case ATOMIC_TYPE_SHORT: return "short";
286 case ATOMIC_TYPE_USHORT: return "unsigned short";
287 case ATOMIC_TYPE_LONG: return "long";
288 case ATOMIC_TYPE_ULONG: return "unsigned long";
289 case ATOMIC_TYPE_LONGLONG: return "long long";
290 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
291 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
292 case ATOMIC_TYPE_FLOAT: return "float";
293 case ATOMIC_TYPE_DOUBLE: return "double";
295 return "INVALIDATOMIC";
299 * Prints the name of an atomic type kinds.
301 * @param kind The type kind.
303 static void print_atomic_kinds(atomic_type_kind_t kind)
305 const char *s = get_atomic_kind_name(kind);
310 * Prints the name of an atomic type.
312 * @param type The type.
314 static void print_atomic_type(const atomic_type_t *type)
316 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
317 print_atomic_kinds(type->akind);
321 * Prints the name of a complex type.
323 * @param type The type.
325 static void print_complex_type(const complex_type_t *type)
327 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
328 print_string("_Complex");
329 print_atomic_kinds(type->akind);
333 * Prints the name of an imaginary type.
335 * @param type The type.
337 static void print_imaginary_type(const imaginary_type_t *type)
339 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
340 print_string("_Imaginary ");
341 print_atomic_kinds(type->akind);
345 * Print the first part (the prefix) of a type.
347 * @param type The type to print.
349 static void print_function_type_pre(const function_type_t *type)
351 switch (type->linkage) {
352 case LINKAGE_INVALID:
357 print_string("extern \"C\" ");
361 if (!(c_mode & _CXX))
362 print_string("extern \"C++\" ");
366 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
368 intern_print_type_pre(type->return_type);
370 cc_kind_t cc = type->calling_convention;
373 case CC_CDECL: print_string(" __cdecl"); break;
374 case CC_STDCALL: print_string(" __stdcall"); break;
375 case CC_FASTCALL: print_string(" __fastcall"); break;
376 case CC_THISCALL: print_string(" __thiscall"); break;
378 if (default_calling_convention != CC_CDECL) {
379 /* show the default calling convention if its not cdecl */
380 cc = default_calling_convention;
388 * Print the second part (the postfix) of a type.
390 * @param type The type to print.
392 static void print_function_type_post(const function_type_t *type,
393 const scope_t *parameters)
397 if (parameters == NULL) {
398 function_parameter_t *parameter = type->parameters;
399 for( ; parameter != NULL; parameter = parameter->next) {
405 print_type(parameter->type);
408 entity_t *parameter = parameters->entities;
409 for (; parameter != NULL; parameter = parameter->base.next) {
410 if (parameter->kind != ENTITY_PARAMETER)
418 const type_t *const type = parameter->declaration.type;
420 print_string(parameter->base.symbol->string);
422 print_type_ext(type, parameter->base.symbol, NULL);
426 if (type->variadic) {
434 if (first && !type->unspecified_parameters) {
435 print_string("void");
439 intern_print_type_post(type->return_type);
443 * Prints the prefix part of a pointer type.
445 * @param type The pointer type.
447 static void print_pointer_type_pre(const pointer_type_t *type)
449 type_t const *const points_to = type->points_to;
450 intern_print_type_pre(points_to);
451 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
453 variable_t *const variable = type->base_variable;
454 if (variable != NULL) {
455 print_string(" __based(");
456 print_string(variable->base.base.symbol->string);
460 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
464 * Prints the postfix part of a pointer type.
466 * @param type The pointer type.
468 static void print_pointer_type_post(const pointer_type_t *type)
470 type_t const *const points_to = type->points_to;
471 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
473 intern_print_type_post(points_to);
477 * Prints the prefix part of a reference type.
479 * @param type The reference type.
481 static void print_reference_type_pre(const reference_type_t *type)
483 type_t const *const refers_to = type->refers_to;
484 intern_print_type_pre(refers_to);
485 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
491 * Prints the postfix part of a reference type.
493 * @param type The reference type.
495 static void print_reference_type_post(const reference_type_t *type)
497 type_t const *const refers_to = type->refers_to;
498 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
500 intern_print_type_post(refers_to);
504 * Prints the prefix part of an array type.
506 * @param type The array type.
508 static void print_array_type_pre(const array_type_t *type)
510 intern_print_type_pre(type->element_type);
514 * Prints the postfix part of an array type.
516 * @param type The array type.
518 static void print_array_type_post(const array_type_t *type)
521 if (type->is_static) {
522 print_string("static ");
524 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
525 if (type->size_expression != NULL
526 && (print_implicit_array_size || !type->has_implicit_size)) {
527 print_expression(type->size_expression);
530 intern_print_type_post(type->element_type);
534 * Prints the postfix part of a bitfield type.
536 * @param type The array type.
538 static void print_bitfield_type_post(const bitfield_type_t *type)
541 print_expression(type->size_expression);
542 intern_print_type_post(type->base_type);
546 * Prints an enum definition.
548 * @param declaration The enum's type declaration.
550 void print_enum_definition(const enum_t *enume)
556 entity_t *entry = enume->base.next;
557 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
558 entry = entry->base.next) {
561 print_string(entry->base.symbol->string);
562 if (entry->enum_value.value != NULL) {
565 /* skip the implicit cast */
566 expression_t *expression = entry->enum_value.value;
567 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
568 expression = expression->unary.value;
570 print_expression(expression);
581 * Prints an enum type.
583 * @param type The enum type.
585 static void print_type_enum(const enum_type_t *type)
587 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
588 print_string("enum ");
590 enum_t *enume = type->enume;
591 symbol_t *symbol = enume->base.symbol;
592 if (symbol != NULL) {
593 print_string(symbol->string);
595 print_enum_definition(enume);
600 * Print the compound part of a compound type.
602 void print_compound_definition(const compound_t *compound)
607 entity_t *entity = compound->members.entities;
608 for( ; entity != NULL; entity = entity->base.next) {
609 if (entity->kind != ENTITY_COMPOUND_MEMBER)
613 print_entity(entity);
620 if (compound->modifiers & DM_TRANSPARENT_UNION) {
621 print_string("__attribute__((__transparent_union__))");
626 * Prints a compound type.
628 * @param type The compound type.
630 static void print_compound_type(const compound_type_t *type)
632 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
634 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
635 print_string("struct ");
637 assert(type->base.kind == TYPE_COMPOUND_UNION);
638 print_string("union ");
641 compound_t *compound = type->compound;
642 symbol_t *symbol = compound->base.symbol;
643 if (symbol != NULL) {
644 print_string(symbol->string);
646 print_compound_definition(compound);
651 * Prints the prefix part of a typedef type.
653 * @param type The typedef type.
655 static void print_typedef_type_pre(const typedef_type_t *const type)
657 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
658 print_string(type->typedefe->base.symbol->string);
662 * Prints the prefix part of a typeof type.
664 * @param type The typeof type.
666 static void print_typeof_type_pre(const typeof_type_t *const type)
668 print_string("typeof(");
669 if (type->expression != NULL) {
670 print_expression(type->expression);
672 print_type(type->typeof_type);
678 * Prints the prefix part of a type.
680 * @param type The type.
682 static void intern_print_type_pre(const type_t *const type)
686 print_string("<error>");
689 print_string("<invalid>");
692 print_type_enum(&type->enumt);
695 print_atomic_type(&type->atomic);
698 print_complex_type(&type->complex);
701 print_imaginary_type(&type->imaginary);
703 case TYPE_COMPOUND_STRUCT:
704 case TYPE_COMPOUND_UNION:
705 print_compound_type(&type->compound);
708 print_function_type_pre(&type->function);
711 print_pointer_type_pre(&type->pointer);
714 print_reference_type_pre(&type->reference);
717 intern_print_type_pre(type->bitfield.base_type);
720 print_array_type_pre(&type->array);
723 print_typedef_type_pre(&type->typedeft);
726 print_typeof_type_pre(&type->typeoft);
729 print_string("unknown");
733 * Prints the postfix part of a type.
735 * @param type The type.
737 static void intern_print_type_post(const type_t *const type)
741 print_function_type_post(&type->function, NULL);
744 print_pointer_type_post(&type->pointer);
747 print_reference_type_post(&type->reference);
750 print_array_type_post(&type->array);
753 print_bitfield_type_post(&type->bitfield);
761 case TYPE_COMPOUND_STRUCT:
762 case TYPE_COMPOUND_UNION:
772 * @param type The type.
774 void print_type(const type_t *const type)
776 print_type_ext(type, NULL, NULL);
779 void print_type_ext(const type_t *const type, const symbol_t *symbol,
780 const scope_t *parameters)
782 intern_print_type_pre(type);
783 if (symbol != NULL) {
785 print_string(symbol->string);
787 if (type->kind == TYPE_FUNCTION) {
788 print_function_type_post(&type->function, parameters);
790 intern_print_type_post(type);
797 * @param type The type to copy.
798 * @return A copy of the type.
800 * @note This does not produce a deep copy!
802 type_t *duplicate_type(const type_t *type)
804 size_t size = get_type_struct_size(type->kind);
806 type_t *copy = obstack_alloc(type_obst, size);
807 memcpy(copy, type, size);
808 copy->base.firm_type = NULL;
814 * Returns the unqualified type of a given type.
816 * @param type The type.
817 * @returns The unqualified type.
819 type_t *get_unqualified_type(type_t *type)
821 assert(!is_typeref(type));
823 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
826 type_t *unqualified_type = duplicate_type(type);
827 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
829 return identify_new_type(unqualified_type);
832 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
834 type_t *type = skip_typeref(orig_type);
837 if (is_type_array(type)) {
838 /* For array types the element type has to be adjusted */
839 type_t *element_type = type->array.element_type;
840 type_t *qual_element_type = get_qualified_type(element_type, qual);
842 if (qual_element_type == element_type)
845 copy = duplicate_type(type);
846 copy->array.element_type = qual_element_type;
847 } else if (is_type_valid(type)) {
848 if ((type->base.qualifiers & qual) == qual)
851 copy = duplicate_type(type);
852 copy->base.qualifiers |= qual;
857 return identify_new_type(copy);
861 * Check if a type is valid.
863 * @param type The type to check.
864 * @return true if type represents a valid type.
866 bool type_valid(const type_t *type)
868 return type->kind != TYPE_INVALID;
871 static bool test_atomic_type_flag(atomic_type_kind_t kind,
872 atomic_type_flag_t flag)
874 assert(kind <= ATOMIC_TYPE_LAST);
875 return (atomic_type_properties[kind].flags & flag) != 0;
879 * Returns true if the given type is an integer type.
881 * @param type The type to check.
882 * @return True if type is an integer type.
884 bool is_type_integer(const type_t *type)
886 assert(!is_typeref(type));
888 if (type->kind == TYPE_ENUM)
890 if (type->kind == TYPE_BITFIELD)
893 if (type->kind != TYPE_ATOMIC)
896 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
900 * Returns true if the given type is an enum type.
902 * @param type The type to check.
903 * @return True if type is an enum type.
905 bool is_type_enum(const type_t *type)
907 assert(!is_typeref(type));
908 return type->kind == TYPE_ENUM;
912 * Returns true if the given type is an floating point type.
914 * @param type The type to check.
915 * @return True if type is a floating point type.
917 bool is_type_float(const type_t *type)
919 assert(!is_typeref(type));
921 if (type->kind != TYPE_ATOMIC)
924 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
928 * Returns true if the given type is an complex type.
930 * @param type The type to check.
931 * @return True if type is a complex type.
933 bool is_type_complex(const type_t *type)
935 assert(!is_typeref(type));
937 if (type->kind != TYPE_ATOMIC)
940 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
944 * Returns true if the given type is a signed type.
946 * @param type The type to check.
947 * @return True if type is a signed type.
949 bool is_type_signed(const type_t *type)
951 assert(!is_typeref(type));
953 /* enum types are int for now */
954 if (type->kind == TYPE_ENUM)
956 if (type->kind == TYPE_BITFIELD)
957 return is_type_signed(type->bitfield.base_type);
959 if (type->kind != TYPE_ATOMIC)
962 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
966 * Returns true if the given type represents an arithmetic type.
968 * @param type The type to check.
969 * @return True if type represents an arithmetic type.
971 bool is_type_arithmetic(const type_t *type)
973 assert(!is_typeref(type));
980 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
982 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
984 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
991 * Returns true if the given type is an integer or float type.
993 * @param type The type to check.
994 * @return True if type is an integer or float type.
996 bool is_type_real(const type_t *type)
999 return is_type_integer(type) || is_type_float(type);
1003 * Returns true if the given type represents a scalar type.
1005 * @param type The type to check.
1006 * @return True if type represents a scalar type.
1008 bool is_type_scalar(const type_t *type)
1010 assert(!is_typeref(type));
1012 if (type->kind == TYPE_POINTER)
1015 return is_type_arithmetic(type);
1019 * Check if a given type is incomplete.
1021 * @param type The type to check.
1022 * @return True if the given type is incomplete (ie. just forward).
1024 bool is_type_incomplete(const type_t *type)
1026 assert(!is_typeref(type));
1028 switch(type->kind) {
1029 case TYPE_COMPOUND_STRUCT:
1030 case TYPE_COMPOUND_UNION: {
1031 const compound_type_t *compound_type = &type->compound;
1032 return !compound_type->compound->complete;
1038 return type->array.size_expression == NULL
1039 && !type->array.size_constant;
1042 return type->atomic.akind == ATOMIC_TYPE_VOID;
1045 return type->complex.akind == ATOMIC_TYPE_VOID;
1047 case TYPE_IMAGINARY:
1048 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1053 case TYPE_REFERENCE:
1059 panic("is_type_incomplete called without typerefs skipped");
1064 panic("invalid type found");
1067 bool is_type_object(const type_t *type)
1069 return !is_type_function(type) && !is_type_incomplete(type);
1073 * Check if two function types are compatible.
1075 static bool function_types_compatible(const function_type_t *func1,
1076 const function_type_t *func2)
1078 const type_t* const ret1 = skip_typeref(func1->return_type);
1079 const type_t* const ret2 = skip_typeref(func2->return_type);
1080 if (!types_compatible(ret1, ret2))
1083 if (func1->linkage != func2->linkage)
1086 cc_kind_t cc1 = func1->calling_convention;
1087 if (cc1 == CC_DEFAULT)
1088 cc1 = default_calling_convention;
1089 cc_kind_t cc2 = func2->calling_convention;
1090 if (cc2 == CC_DEFAULT)
1091 cc2 = default_calling_convention;
1096 if (func1->variadic != func2->variadic)
1099 /* can parameters be compared? */
1100 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1101 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1104 /* TODO: handling of unspecified parameters not correct yet */
1106 /* all argument types must be compatible */
1107 function_parameter_t *parameter1 = func1->parameters;
1108 function_parameter_t *parameter2 = func2->parameters;
1109 for ( ; parameter1 != NULL && parameter2 != NULL;
1110 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1111 type_t *parameter1_type = skip_typeref(parameter1->type);
1112 type_t *parameter2_type = skip_typeref(parameter2->type);
1114 parameter1_type = get_unqualified_type(parameter1_type);
1115 parameter2_type = get_unqualified_type(parameter2_type);
1117 if (!types_compatible(parameter1_type, parameter2_type))
1120 /* same number of arguments? */
1121 if (parameter1 != NULL || parameter2 != NULL)
1128 * Check if two array types are compatible.
1130 static bool array_types_compatible(const array_type_t *array1,
1131 const array_type_t *array2)
1133 type_t *element_type1 = skip_typeref(array1->element_type);
1134 type_t *element_type2 = skip_typeref(array2->element_type);
1135 if (!types_compatible(element_type1, element_type2))
1138 if (!array1->size_constant || !array2->size_constant)
1141 return array1->size == array2->size;
1145 * Check if two types are compatible.
1147 bool types_compatible(const type_t *type1, const type_t *type2)
1149 assert(!is_typeref(type1));
1150 assert(!is_typeref(type2));
1152 /* shortcut: the same type is always compatible */
1156 if (!is_type_valid(type1) || !is_type_valid(type2))
1159 if (type1->base.qualifiers != type2->base.qualifiers)
1161 if (type1->kind != type2->kind)
1164 switch (type1->kind) {
1166 return function_types_compatible(&type1->function, &type2->function);
1168 return type1->atomic.akind == type2->atomic.akind;
1170 return type1->complex.akind == type2->complex.akind;
1171 case TYPE_IMAGINARY:
1172 return type1->imaginary.akind == type2->imaginary.akind;
1174 return array_types_compatible(&type1->array, &type2->array);
1176 case TYPE_POINTER: {
1177 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1178 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1179 return types_compatible(to1, to2);
1182 case TYPE_REFERENCE: {
1183 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1184 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1185 return types_compatible(to1, to2);
1188 case TYPE_COMPOUND_STRUCT:
1189 case TYPE_COMPOUND_UNION: {
1195 /* TODO: not implemented */
1199 /* not sure if this makes sense or is even needed, implement it if you
1200 * really need it! */
1201 panic("type compatibility check for bitfield type");
1204 /* Hmm, the error type should be compatible to all other types */
1207 panic("invalid type found in compatible types");
1210 panic("typerefs not skipped in compatible types?!?");
1213 /* TODO: incomplete */
1218 * Skip all typerefs and return the underlying type.
1220 type_t *skip_typeref(type_t *type)
1222 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1225 switch (type->kind) {
1228 case TYPE_TYPEDEF: {
1229 qualifiers |= type->base.qualifiers;
1231 const typedef_type_t *typedef_type = &type->typedeft;
1232 if (typedef_type->resolved_type != NULL) {
1233 type = typedef_type->resolved_type;
1236 type = typedef_type->typedefe->type;
1240 qualifiers |= type->base.qualifiers;
1241 type = type->typeoft.typeof_type;
1249 if (qualifiers != TYPE_QUALIFIER_NONE) {
1250 type_t *const copy = duplicate_type(type);
1252 /* for const with typedefed array type the element type has to be
1254 if (is_type_array(copy)) {
1255 type_t *element_type = copy->array.element_type;
1256 element_type = duplicate_type(element_type);
1257 element_type->base.qualifiers |= qualifiers;
1258 copy->array.element_type = element_type;
1260 copy->base.qualifiers |= qualifiers;
1263 type = identify_new_type(copy);
1269 unsigned get_type_size(type_t *type)
1271 switch (type->kind) {
1277 return get_atomic_type_size(type->atomic.akind);
1279 return get_atomic_type_size(type->complex.akind) * 2;
1280 case TYPE_IMAGINARY:
1281 return get_atomic_type_size(type->imaginary.akind);
1282 case TYPE_COMPOUND_UNION:
1283 layout_union_type(&type->compound);
1284 return type->compound.compound->size;
1285 case TYPE_COMPOUND_STRUCT:
1286 layout_struct_type(&type->compound);
1287 return type->compound.compound->size;
1289 return get_atomic_type_size(type->enumt.akind);
1291 return 0; /* non-const (but "address-const") */
1292 case TYPE_REFERENCE:
1294 /* TODO: make configurable by backend */
1297 /* TODO: correct if element_type is aligned? */
1298 il_size_t element_size = get_type_size(type->array.element_type);
1299 return type->array.size * element_size;
1304 return get_type_size(type->typedeft.typedefe->type);
1306 if (type->typeoft.typeof_type) {
1307 return get_type_size(type->typeoft.typeof_type);
1309 return get_type_size(type->typeoft.expression->base.type);
1312 panic("invalid type in get_type_size");
1315 unsigned get_type_alignment(type_t *type)
1317 switch (type->kind) {
1323 return get_atomic_type_alignment(type->atomic.akind);
1325 return get_atomic_type_alignment(type->complex.akind);
1326 case TYPE_IMAGINARY:
1327 return get_atomic_type_alignment(type->imaginary.akind);
1328 case TYPE_COMPOUND_UNION:
1329 layout_union_type(&type->compound);
1330 return type->compound.compound->alignment;
1331 case TYPE_COMPOUND_STRUCT:
1332 layout_struct_type(&type->compound);
1333 return type->compound.compound->alignment;
1335 return get_atomic_type_alignment(type->enumt.akind);
1337 /* what is correct here? */
1339 case TYPE_REFERENCE:
1341 /* TODO: make configurable by backend */
1344 return get_type_alignment(type->array.element_type);
1347 case TYPE_TYPEDEF: {
1348 il_alignment_t alignment
1349 = get_type_alignment(type->typedeft.typedefe->type);
1350 if (type->typedeft.typedefe->alignment > alignment)
1351 alignment = type->typedeft.typedefe->alignment;
1356 if (type->typeoft.typeof_type) {
1357 return get_type_alignment(type->typeoft.typeof_type);
1359 return get_type_alignment(type->typeoft.expression->base.type);
1362 panic("invalid type in get_type_alignment");
1365 decl_modifiers_t get_type_modifiers(const type_t *type)
1367 switch(type->kind) {
1371 case TYPE_COMPOUND_STRUCT:
1372 case TYPE_COMPOUND_UNION:
1373 return type->compound.compound->modifiers;
1375 return type->function.modifiers;
1379 case TYPE_IMAGINARY:
1380 case TYPE_REFERENCE:
1385 case TYPE_TYPEDEF: {
1386 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1387 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1391 if (type->typeoft.typeof_type) {
1392 return get_type_modifiers(type->typeoft.typeof_type);
1394 return get_type_modifiers(type->typeoft.expression->base.type);
1397 panic("invalid type found in get_type_modifiers");
1400 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1402 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1405 switch (type->base.kind) {
1407 return TYPE_QUALIFIER_NONE;
1409 qualifiers |= type->base.qualifiers;
1410 const typedef_type_t *typedef_type = &type->typedeft;
1411 if (typedef_type->resolved_type != NULL)
1412 type = typedef_type->resolved_type;
1414 type = typedef_type->typedefe->type;
1417 type = type->typeoft.typeof_type;
1420 if (skip_array_type) {
1421 type = type->array.element_type;
1430 return type->base.qualifiers | qualifiers;
1433 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1435 assert(kind <= ATOMIC_TYPE_LAST);
1436 return atomic_type_properties[kind].size;
1439 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1441 assert(kind <= ATOMIC_TYPE_LAST);
1442 return atomic_type_properties[kind].alignment;
1445 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1447 assert(kind <= ATOMIC_TYPE_LAST);
1448 return atomic_type_properties[kind].flags;
1451 atomic_type_kind_t get_intptr_kind(void)
1453 if (machine_size <= 32)
1454 return ATOMIC_TYPE_INT;
1455 else if (machine_size <= 64)
1456 return ATOMIC_TYPE_LONG;
1458 return ATOMIC_TYPE_LONGLONG;
1461 atomic_type_kind_t get_uintptr_kind(void)
1463 if (machine_size <= 32)
1464 return ATOMIC_TYPE_UINT;
1465 else if (machine_size <= 64)
1466 return ATOMIC_TYPE_ULONG;
1468 return ATOMIC_TYPE_ULONGLONG;
1472 * Find the atomic type kind representing a given size (signed).
1474 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1476 static atomic_type_kind_t kinds[32];
1479 atomic_type_kind_t kind = kinds[size];
1480 if (kind == ATOMIC_TYPE_INVALID) {
1481 static const atomic_type_kind_t possible_kinds[] = {
1486 ATOMIC_TYPE_LONGLONG
1488 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1489 if (get_atomic_type_size(possible_kinds[i]) == size) {
1490 kind = possible_kinds[i];
1500 * Find the atomic type kind representing a given size (signed).
1502 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1504 static atomic_type_kind_t kinds[32];
1507 atomic_type_kind_t kind = kinds[size];
1508 if (kind == ATOMIC_TYPE_INVALID) {
1509 static const atomic_type_kind_t possible_kinds[] = {
1514 ATOMIC_TYPE_ULONGLONG
1516 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1517 if (get_atomic_type_size(possible_kinds[i]) == size) {
1518 kind = possible_kinds[i];
1528 * Hash the given type and return the "singleton" version
1531 type_t *identify_new_type(type_t *type)
1533 type_t *result = typehash_insert(type);
1534 if (result != type) {
1535 obstack_free(type_obst, type);
1541 * Creates a new atomic type.
1543 * @param akind The kind of the atomic type.
1544 * @param qualifiers Type qualifiers for the new type.
1546 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1548 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1549 memset(type, 0, sizeof(atomic_type_t));
1551 type->kind = TYPE_ATOMIC;
1552 type->base.qualifiers = qualifiers;
1553 type->atomic.akind = akind;
1555 return identify_new_type(type);
1559 * Creates a new complex type.
1561 * @param akind The kind of the atomic type.
1562 * @param qualifiers Type qualifiers for the new type.
1564 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1566 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1567 memset(type, 0, sizeof(complex_type_t));
1569 type->kind = TYPE_COMPLEX;
1570 type->base.qualifiers = qualifiers;
1571 type->complex.akind = akind;
1573 return identify_new_type(type);
1577 * Creates a new imaginary type.
1579 * @param akind The kind of the atomic type.
1580 * @param qualifiers Type qualifiers for the new type.
1582 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1584 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1585 memset(type, 0, sizeof(imaginary_type_t));
1587 type->kind = TYPE_IMAGINARY;
1588 type->base.qualifiers = qualifiers;
1589 type->imaginary.akind = akind;
1591 return identify_new_type(type);
1595 * Creates a new pointer type.
1597 * @param points_to The points-to type for the new type.
1598 * @param qualifiers Type qualifiers for the new type.
1600 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1602 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1603 memset(type, 0, sizeof(pointer_type_t));
1605 type->kind = TYPE_POINTER;
1606 type->base.qualifiers = qualifiers;
1607 type->pointer.points_to = points_to;
1608 type->pointer.base_variable = NULL;
1610 return identify_new_type(type);
1614 * Creates a new reference type.
1616 * @param refers_to The referred-to type for the new type.
1618 type_t *make_reference_type(type_t *refers_to)
1620 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1621 memset(type, 0, sizeof(reference_type_t));
1623 type->kind = TYPE_REFERENCE;
1624 type->base.qualifiers = 0;
1625 type->reference.refers_to = refers_to;
1627 return identify_new_type(type);
1631 * Creates a new based pointer type.
1633 * @param points_to The points-to type for the new type.
1634 * @param qualifiers Type qualifiers for the new type.
1635 * @param variable The based variable
1637 type_t *make_based_pointer_type(type_t *points_to,
1638 type_qualifiers_t qualifiers, variable_t *variable)
1640 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1641 memset(type, 0, sizeof(pointer_type_t));
1643 type->kind = TYPE_POINTER;
1644 type->base.qualifiers = qualifiers;
1645 type->pointer.points_to = points_to;
1646 type->pointer.base_variable = variable;
1648 return identify_new_type(type);
1652 type_t *make_array_type(type_t *element_type, size_t size,
1653 type_qualifiers_t qualifiers)
1655 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1656 memset(type, 0, sizeof(array_type_t));
1658 type->kind = TYPE_ARRAY;
1659 type->base.qualifiers = qualifiers;
1660 type->array.element_type = element_type;
1661 type->array.size = size;
1662 type->array.size_constant = true;
1664 return identify_new_type(type);
1667 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1668 il_alignment_t *struct_alignment,
1669 bool packed, entity_t *first)
1671 il_size_t offset = *struct_offset;
1672 il_alignment_t alignment = *struct_alignment;
1673 size_t bit_offset = 0;
1676 for (member = first; member != NULL; member = member->base.next) {
1677 if (member->kind != ENTITY_COMPOUND_MEMBER)
1680 type_t *type = member->declaration.type;
1681 if (type->kind != TYPE_BITFIELD)
1684 type_t *base_type = skip_typeref(type->bitfield.base_type);
1685 il_alignment_t base_alignment = get_type_alignment(base_type);
1686 il_alignment_t alignment_mask = base_alignment-1;
1687 if (base_alignment > alignment)
1688 alignment = base_alignment;
1690 size_t bit_size = type->bitfield.bit_size;
1692 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1693 offset &= ~alignment_mask;
1694 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1696 if (bit_offset + bit_size > base_size || bit_size == 0) {
1697 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1698 offset = (offset + base_alignment-1) & ~alignment_mask;
1703 if (byte_order_big_endian) {
1704 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1705 member->compound_member.offset = offset & ~alignment_mask;
1706 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1708 member->compound_member.offset = offset;
1709 member->compound_member.bit_offset = bit_offset;
1712 bit_offset += bit_size;
1713 offset += bit_offset / BITS_PER_BYTE;
1714 bit_offset %= BITS_PER_BYTE;
1720 *struct_offset = offset;
1721 *struct_alignment = alignment;
1725 void layout_struct_type(compound_type_t *type)
1727 assert(type->compound != NULL);
1729 compound_t *compound = type->compound;
1730 if (!compound->complete)
1732 if (type->compound->layouted)
1735 il_size_t offset = 0;
1736 il_alignment_t alignment = compound->alignment;
1737 bool need_pad = false;
1739 entity_t *entry = compound->members.entities;
1740 while (entry != NULL) {
1741 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1742 entry = entry->base.next;
1746 type_t *m_type = entry->declaration.type;
1747 type_t *skipped = skip_typeref(m_type);
1748 if (! is_type_valid(skipped)) {
1749 entry = entry->base.next;
1753 if (skipped->kind == TYPE_BITFIELD) {
1754 entry = pack_bitfield_members(&offset, &alignment,
1755 compound->packed, entry);
1759 il_alignment_t m_alignment = get_type_alignment(m_type);
1760 if (m_alignment > alignment)
1761 alignment = m_alignment;
1763 if (!compound->packed) {
1764 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1766 if (new_offset > offset) {
1768 offset = new_offset;
1772 entry->compound_member.offset = offset;
1773 offset += get_type_size(m_type);
1775 entry = entry->base.next;
1778 if (!compound->packed) {
1779 il_size_t new_offset = (offset + alignment-1) & -alignment;
1780 if (new_offset > offset) {
1782 offset = new_offset;
1787 if (warning.padded) {
1788 warningf(&compound->base.source_position, "'%T' needs padding",
1791 } else if (compound->packed && warning.packed) {
1792 warningf(&compound->base.source_position,
1793 "superfluous packed attribute on '%T'", type);
1796 compound->size = offset;
1797 compound->alignment = alignment;
1798 compound->layouted = true;
1801 void layout_union_type(compound_type_t *type)
1803 assert(type->compound != NULL);
1805 compound_t *compound = type->compound;
1806 if (! compound->complete)
1810 il_alignment_t alignment = compound->alignment;
1812 entity_t *entry = compound->members.entities;
1813 for (; entry != NULL; entry = entry->base.next) {
1814 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1817 type_t *m_type = entry->declaration.type;
1818 if (! is_type_valid(skip_typeref(m_type)))
1821 entry->compound_member.offset = 0;
1822 il_size_t m_size = get_type_size(m_type);
1825 il_alignment_t m_alignment = get_type_alignment(m_type);
1826 if (m_alignment > alignment)
1827 alignment = m_alignment;
1829 size = (size + alignment - 1) & -alignment;
1831 compound->size = size;
1832 compound->alignment = alignment;
1835 static function_parameter_t *allocate_parameter(type_t *const type)
1837 function_parameter_t *const param
1838 = obstack_alloc(type_obst, sizeof(*param));
1839 memset(param, 0, sizeof(*param));
1844 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1845 type_t *argument_type2)
1847 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1848 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1849 parameter1->next = parameter2;
1851 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1852 type->function.return_type = return_type;
1853 type->function.parameters = parameter1;
1854 type->function.linkage = LINKAGE_C;
1856 return identify_new_type(type);
1859 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1861 function_parameter_t *const parameter = allocate_parameter(argument_type);
1863 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1864 type->function.return_type = return_type;
1865 type->function.parameters = parameter;
1866 type->function.linkage = LINKAGE_C;
1868 return identify_new_type(type);
1871 type_t *make_function_1_type_variadic(type_t *return_type,
1872 type_t *argument_type)
1874 function_parameter_t *const parameter = allocate_parameter(argument_type);
1876 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1877 type->function.return_type = return_type;
1878 type->function.parameters = parameter;
1879 type->function.variadic = true;
1880 type->function.linkage = LINKAGE_C;
1882 return identify_new_type(type);
1885 type_t *make_function_0_type(type_t *return_type)
1887 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1888 type->function.return_type = return_type;
1889 type->function.parameters = NULL;
1890 type->function.linkage = LINKAGE_C;
1892 return identify_new_type(type);
1895 type_t *make_function_type(type_t *return_type, int n_types,
1896 type_t *const *argument_types,
1897 decl_modifiers_t modifiers)
1899 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1900 type->function.return_type = return_type;
1901 type->function.modifiers |= modifiers;
1902 type->function.linkage = LINKAGE_C;
1904 function_parameter_t *last = NULL;
1905 for (int i = 0; i < n_types; ++i) {
1906 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1908 type->function.parameters = parameter;
1910 last->next = parameter;
1915 return identify_new_type(type);
1919 * Debug helper. Prints the given type to stdout.
1921 static __attribute__((unused))
1922 void dbg_type(const type_t *type)
1924 print_to_file(stderr);