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_TYPEDEF] = sizeof(typedef_type_t),
74 [TYPE_TYPEOF] = sizeof(typeof_type_t),
76 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
77 assert(kind <= TYPE_TYPEOF);
78 assert(sizes[kind] != 0);
82 type_t *allocate_type_zero(type_kind_t kind)
84 size_t size = get_type_struct_size(kind);
85 type_t *res = obstack_alloc(type_obst, size);
87 res->base.kind = kind;
93 * Properties of atomic types.
95 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
96 //ATOMIC_TYPE_INVALID = 0,
97 [ATOMIC_TYPE_VOID] = {
100 .flags = ATOMIC_TYPE_FLAG_NONE
102 [ATOMIC_TYPE_WCHAR_T] = {
103 .size = (unsigned)-1,
104 .alignment = (unsigned)-1,
105 /* signed flag will be set when known */
106 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
108 [ATOMIC_TYPE_CHAR] = {
111 /* signed flag will be set when known */
112 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
114 [ATOMIC_TYPE_SCHAR] = {
117 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
118 | ATOMIC_TYPE_FLAG_SIGNED,
120 [ATOMIC_TYPE_UCHAR] = {
123 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
125 [ATOMIC_TYPE_SHORT] = {
128 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
129 | ATOMIC_TYPE_FLAG_SIGNED
131 [ATOMIC_TYPE_USHORT] = {
134 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
136 [ATOMIC_TYPE_INT] = {
137 .size = (unsigned) -1,
138 .alignment = (unsigned) -1,
139 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
140 | ATOMIC_TYPE_FLAG_SIGNED,
142 [ATOMIC_TYPE_UINT] = {
143 .size = (unsigned) -1,
144 .alignment = (unsigned) -1,
145 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
147 [ATOMIC_TYPE_LONG] = {
148 .size = (unsigned) -1,
149 .alignment = (unsigned) -1,
150 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
151 | ATOMIC_TYPE_FLAG_SIGNED,
153 [ATOMIC_TYPE_ULONG] = {
154 .size = (unsigned) -1,
155 .alignment = (unsigned) -1,
156 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
158 [ATOMIC_TYPE_LONGLONG] = {
159 .size = (unsigned) -1,
160 .alignment = (unsigned) -1,
161 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
162 | ATOMIC_TYPE_FLAG_SIGNED,
164 [ATOMIC_TYPE_ULONGLONG] = {
165 .size = (unsigned) -1,
166 .alignment = (unsigned) -1,
167 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
169 [ATOMIC_TYPE_BOOL] = {
170 .size = (unsigned) -1,
171 .alignment = (unsigned) -1,
172 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
174 [ATOMIC_TYPE_FLOAT] = {
176 .alignment = (unsigned) -1,
177 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
178 | ATOMIC_TYPE_FLAG_SIGNED,
180 [ATOMIC_TYPE_DOUBLE] = {
182 .alignment = (unsigned) -1,
183 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
184 | ATOMIC_TYPE_FLAG_SIGNED,
186 [ATOMIC_TYPE_LONG_DOUBLE] = {
188 .alignment = (unsigned) -1,
189 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
190 | ATOMIC_TYPE_FLAG_SIGNED,
192 /* complex and imaginary types are set in init_types */
195 void init_types(void)
197 obstack_init(type_obst);
199 atomic_type_properties_t *props = atomic_type_properties;
201 if (char_is_signed) {
202 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
205 unsigned int_size = machine_size < 32 ? 2 : 4;
206 /* long is always 32bit on windows */
207 unsigned long_size = c_mode & _MS ? 4 : (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 if (machine_size <= 32) {
226 props[ATOMIC_TYPE_FLOAT].alignment = 4;
227 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
228 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
229 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
230 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
232 props[ATOMIC_TYPE_FLOAT].alignment = 4;
233 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
234 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
235 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
236 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
238 if (force_long_double_size > 0) {
239 props[ATOMIC_TYPE_LONG_DOUBLE].size = force_long_double_size;
240 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = force_long_double_size;
243 /* TODO: make this configurable for platforms which do not use byte sized
245 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
247 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
250 void exit_types(void)
252 obstack_free(type_obst, NULL);
255 void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
257 size_t sep = q & QUAL_SEP_START ? 0 : 1;
258 if (qualifiers & TYPE_QUALIFIER_CONST) {
259 print_string(" const" + sep);
262 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
263 print_string(" volatile" + sep);
266 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
267 print_string(" restrict" + sep);
270 if (sep == 0 && q & QUAL_SEP_END)
274 const char *get_atomic_kind_name(atomic_type_kind_t kind)
277 case ATOMIC_TYPE_INVALID: break;
278 case ATOMIC_TYPE_VOID: return "void";
279 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
280 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
281 case ATOMIC_TYPE_CHAR: return "char";
282 case ATOMIC_TYPE_SCHAR: return "signed char";
283 case ATOMIC_TYPE_UCHAR: return "unsigned char";
284 case ATOMIC_TYPE_INT: return "int";
285 case ATOMIC_TYPE_UINT: return "unsigned int";
286 case ATOMIC_TYPE_SHORT: return "short";
287 case ATOMIC_TYPE_USHORT: return "unsigned short";
288 case ATOMIC_TYPE_LONG: return "long";
289 case ATOMIC_TYPE_ULONG: return "unsigned long";
290 case ATOMIC_TYPE_LONGLONG: return "long long";
291 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
292 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
293 case ATOMIC_TYPE_FLOAT: return "float";
294 case ATOMIC_TYPE_DOUBLE: return "double";
296 return "INVALIDATOMIC";
300 * Prints the name of an atomic type kinds.
302 * @param kind The type kind.
304 static void print_atomic_kinds(atomic_type_kind_t kind)
306 const char *s = get_atomic_kind_name(kind);
311 * Prints the name of an atomic type.
313 * @param type The type.
315 static void print_atomic_type(const atomic_type_t *type)
317 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
318 print_atomic_kinds(type->akind);
322 * Prints the name of a complex type.
324 * @param type The type.
326 static void print_complex_type(const complex_type_t *type)
328 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
329 print_string("_Complex");
330 print_atomic_kinds(type->akind);
334 * Prints the name of an imaginary type.
336 * @param type The type.
338 static void print_imaginary_type(const imaginary_type_t *type)
340 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
341 print_string("_Imaginary ");
342 print_atomic_kinds(type->akind);
346 * Print the first part (the prefix) of a type.
348 * @param type The type to print.
350 static void print_function_type_pre(const function_type_t *type)
352 switch (type->linkage) {
353 case LINKAGE_INVALID:
358 print_string("extern \"C\" ");
362 if (!(c_mode & _CXX))
363 print_string("extern \"C++\" ");
367 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
369 intern_print_type_pre(type->return_type);
371 cc_kind_t cc = type->calling_convention;
374 case CC_CDECL: print_string(" __cdecl"); break;
375 case CC_STDCALL: print_string(" __stdcall"); break;
376 case CC_FASTCALL: print_string(" __fastcall"); break;
377 case CC_THISCALL: print_string(" __thiscall"); break;
379 if (default_calling_convention != CC_CDECL) {
380 /* show the default calling convention if its not cdecl */
381 cc = default_calling_convention;
389 * Print the second part (the postfix) of a type.
391 * @param type The type to print.
393 static void print_function_type_post(const function_type_t *type,
394 const scope_t *parameters)
398 if (parameters == NULL) {
399 function_parameter_t *parameter = type->parameters;
400 for( ; parameter != NULL; parameter = parameter->next) {
406 print_type(parameter->type);
409 entity_t *parameter = parameters->entities;
410 for (; parameter != NULL; parameter = parameter->base.next) {
411 if (parameter->kind != ENTITY_PARAMETER)
419 const type_t *const type = parameter->declaration.type;
421 print_string(parameter->base.symbol->string);
423 print_type_ext(type, parameter->base.symbol, NULL);
427 if (type->variadic) {
435 if (first && !type->unspecified_parameters) {
436 print_string("void");
440 intern_print_type_post(type->return_type);
444 * Prints the prefix part of a pointer type.
446 * @param type The pointer type.
448 static void print_pointer_type_pre(const pointer_type_t *type)
450 type_t const *const points_to = type->points_to;
451 intern_print_type_pre(points_to);
452 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
454 variable_t *const variable = type->base_variable;
455 if (variable != NULL) {
456 print_string(" __based(");
457 print_string(variable->base.base.symbol->string);
461 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
465 * Prints the postfix part of a pointer type.
467 * @param type The pointer type.
469 static void print_pointer_type_post(const pointer_type_t *type)
471 type_t const *const points_to = type->points_to;
472 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
474 intern_print_type_post(points_to);
478 * Prints the prefix part of a reference type.
480 * @param type The reference type.
482 static void print_reference_type_pre(const reference_type_t *type)
484 type_t const *const refers_to = type->refers_to;
485 intern_print_type_pre(refers_to);
486 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
492 * Prints the postfix part of a reference type.
494 * @param type The reference type.
496 static void print_reference_type_post(const reference_type_t *type)
498 type_t const *const refers_to = type->refers_to;
499 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
501 intern_print_type_post(refers_to);
505 * Prints the prefix part of an array type.
507 * @param type The array type.
509 static void print_array_type_pre(const array_type_t *type)
511 intern_print_type_pre(type->element_type);
515 * Prints the postfix part of an array type.
517 * @param type The array type.
519 static void print_array_type_post(const array_type_t *type)
522 if (type->is_static) {
523 print_string("static ");
525 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
526 if (type->size_expression != NULL
527 && (print_implicit_array_size || !type->has_implicit_size)) {
528 print_expression(type->size_expression);
531 intern_print_type_post(type->element_type);
535 * Prints the postfix part of a bitfield type.
537 * @param type The array type.
539 static void print_bitfield_type_post(const bitfield_type_t *type)
542 print_expression(type->size_expression);
543 intern_print_type_post(type->base_type);
547 * Prints an enum definition.
549 * @param declaration The enum's type declaration.
551 void print_enum_definition(const enum_t *enume)
557 entity_t *entry = enume->base.next;
558 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
559 entry = entry->base.next) {
562 print_string(entry->base.symbol->string);
563 if (entry->enum_value.value != NULL) {
566 /* skip the implicit cast */
567 expression_t *expression = entry->enum_value.value;
568 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
569 expression = expression->unary.value;
571 print_expression(expression);
582 * Prints an enum type.
584 * @param type The enum type.
586 static void print_type_enum(const enum_type_t *type)
588 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
589 print_string("enum ");
591 enum_t *enume = type->enume;
592 symbol_t *symbol = enume->base.symbol;
593 if (symbol != NULL) {
594 print_string(symbol->string);
596 print_enum_definition(enume);
601 * Print the compound part of a compound type.
603 void print_compound_definition(const compound_t *compound)
608 entity_t *entity = compound->members.entities;
609 for( ; entity != NULL; entity = entity->base.next) {
610 if (entity->kind != ENTITY_COMPOUND_MEMBER)
614 print_entity(entity);
621 if (compound->modifiers & DM_TRANSPARENT_UNION) {
622 print_string("__attribute__((__transparent_union__))");
627 * Prints a compound type.
629 * @param type The compound type.
631 static void print_compound_type(const compound_type_t *type)
633 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
635 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
636 print_string("struct ");
638 assert(type->base.kind == TYPE_COMPOUND_UNION);
639 print_string("union ");
642 compound_t *compound = type->compound;
643 symbol_t *symbol = compound->base.symbol;
644 if (symbol != NULL) {
645 print_string(symbol->string);
647 print_compound_definition(compound);
652 * Prints the prefix part of a typedef type.
654 * @param type The typedef type.
656 static void print_typedef_type_pre(const typedef_type_t *const type)
658 print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
659 print_string(type->typedefe->base.symbol->string);
663 * Prints the prefix part of a typeof type.
665 * @param type The typeof type.
667 static void print_typeof_type_pre(const typeof_type_t *const type)
669 print_string("typeof(");
670 if (type->expression != NULL) {
671 print_expression(type->expression);
673 print_type(type->typeof_type);
679 * Prints the prefix part of a type.
681 * @param type The type.
683 static void intern_print_type_pre(const type_t *const type)
687 print_string("<error>");
690 print_string("<invalid>");
693 print_type_enum(&type->enumt);
696 print_atomic_type(&type->atomic);
699 print_complex_type(&type->complex);
702 print_imaginary_type(&type->imaginary);
704 case TYPE_COMPOUND_STRUCT:
705 case TYPE_COMPOUND_UNION:
706 print_compound_type(&type->compound);
709 print_function_type_pre(&type->function);
712 print_pointer_type_pre(&type->pointer);
715 print_reference_type_pre(&type->reference);
718 intern_print_type_pre(type->bitfield.base_type);
721 print_array_type_pre(&type->array);
724 print_typedef_type_pre(&type->typedeft);
727 print_typeof_type_pre(&type->typeoft);
730 print_string("unknown");
734 * Prints the postfix part of a type.
736 * @param type The type.
738 static void intern_print_type_post(const type_t *const type)
742 print_function_type_post(&type->function, NULL);
745 print_pointer_type_post(&type->pointer);
748 print_reference_type_post(&type->reference);
751 print_array_type_post(&type->array);
754 print_bitfield_type_post(&type->bitfield);
762 case TYPE_COMPOUND_STRUCT:
763 case TYPE_COMPOUND_UNION:
773 * @param type The type.
775 void print_type(const type_t *const type)
777 print_type_ext(type, NULL, NULL);
780 void print_type_ext(const type_t *const type, const symbol_t *symbol,
781 const scope_t *parameters)
783 intern_print_type_pre(type);
784 if (symbol != NULL) {
786 print_string(symbol->string);
788 if (type->kind == TYPE_FUNCTION) {
789 print_function_type_post(&type->function, parameters);
791 intern_print_type_post(type);
798 * @param type The type to copy.
799 * @return A copy of the type.
801 * @note This does not produce a deep copy!
803 type_t *duplicate_type(const type_t *type)
805 size_t size = get_type_struct_size(type->kind);
807 type_t *copy = obstack_alloc(type_obst, size);
808 memcpy(copy, type, size);
809 copy->base.firm_type = NULL;
815 * Returns the unqualified type of a given type.
817 * @param type The type.
818 * @returns The unqualified type.
820 type_t *get_unqualified_type(type_t *type)
822 assert(!is_typeref(type));
824 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
827 type_t *unqualified_type = duplicate_type(type);
828 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
830 return identify_new_type(unqualified_type);
833 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
835 type_t *type = skip_typeref(orig_type);
838 if (is_type_array(type)) {
839 /* For array types the element type has to be adjusted */
840 type_t *element_type = type->array.element_type;
841 type_t *qual_element_type = get_qualified_type(element_type, qual);
843 if (qual_element_type == element_type)
846 copy = duplicate_type(type);
847 copy->array.element_type = qual_element_type;
848 } else if (is_type_valid(type)) {
849 if ((type->base.qualifiers & qual) == qual)
852 copy = duplicate_type(type);
853 copy->base.qualifiers |= qual;
858 return identify_new_type(copy);
862 * Check if a type is valid.
864 * @param type The type to check.
865 * @return true if type represents a valid type.
867 bool type_valid(const type_t *type)
869 return type->kind != TYPE_INVALID;
872 static bool test_atomic_type_flag(atomic_type_kind_t kind,
873 atomic_type_flag_t flag)
875 assert(kind <= ATOMIC_TYPE_LAST);
876 return (atomic_type_properties[kind].flags & flag) != 0;
880 * Returns true if the given type is an integer type.
882 * @param type The type to check.
883 * @return True if type is an integer type.
885 bool is_type_integer(const type_t *type)
887 assert(!is_typeref(type));
889 if (type->kind == TYPE_ENUM)
891 if (type->kind == TYPE_BITFIELD)
894 if (type->kind != TYPE_ATOMIC)
897 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
901 * Returns true if the given type is an enum type.
903 * @param type The type to check.
904 * @return True if type is an enum type.
906 bool is_type_enum(const type_t *type)
908 assert(!is_typeref(type));
909 return type->kind == TYPE_ENUM;
913 * Returns true if the given type is an floating point type.
915 * @param type The type to check.
916 * @return True if type is a floating point type.
918 bool is_type_float(const type_t *type)
920 assert(!is_typeref(type));
922 if (type->kind != TYPE_ATOMIC)
925 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
929 * Returns true if the given type is an complex type.
931 * @param type The type to check.
932 * @return True if type is a complex type.
934 bool is_type_complex(const type_t *type)
936 assert(!is_typeref(type));
938 if (type->kind != TYPE_ATOMIC)
941 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
945 * Returns true if the given type is a signed type.
947 * @param type The type to check.
948 * @return True if type is a signed type.
950 bool is_type_signed(const type_t *type)
952 assert(!is_typeref(type));
954 /* enum types are int for now */
955 if (type->kind == TYPE_ENUM)
957 if (type->kind == TYPE_BITFIELD)
958 return is_type_signed(type->bitfield.base_type);
960 if (type->kind != TYPE_ATOMIC)
963 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
967 * Returns true if the given type represents an arithmetic type.
969 * @param type The type to check.
970 * @return True if type represents an arithmetic type.
972 bool is_type_arithmetic(const type_t *type)
974 assert(!is_typeref(type));
981 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
983 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
985 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
992 * Returns true if the given type is an integer or float type.
994 * @param type The type to check.
995 * @return True if type is an integer or float type.
997 bool is_type_real(const type_t *type)
1000 return is_type_integer(type) || is_type_float(type);
1004 * Returns true if the given type represents a scalar type.
1006 * @param type The type to check.
1007 * @return True if type represents a scalar type.
1009 bool is_type_scalar(const type_t *type)
1011 assert(!is_typeref(type));
1013 if (type->kind == TYPE_POINTER)
1016 return is_type_arithmetic(type);
1020 * Check if a given type is incomplete.
1022 * @param type The type to check.
1023 * @return True if the given type is incomplete (ie. just forward).
1025 bool is_type_incomplete(const type_t *type)
1027 assert(!is_typeref(type));
1029 switch(type->kind) {
1030 case TYPE_COMPOUND_STRUCT:
1031 case TYPE_COMPOUND_UNION: {
1032 const compound_type_t *compound_type = &type->compound;
1033 return !compound_type->compound->complete;
1039 return type->array.size_expression == NULL
1040 && !type->array.size_constant;
1043 return type->atomic.akind == ATOMIC_TYPE_VOID;
1046 return type->complex.akind == ATOMIC_TYPE_VOID;
1048 case TYPE_IMAGINARY:
1049 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1054 case TYPE_REFERENCE:
1060 panic("is_type_incomplete called without typerefs skipped");
1065 panic("invalid type found");
1068 bool is_type_object(const type_t *type)
1070 return !is_type_function(type) && !is_type_incomplete(type);
1074 * Check if two function types are compatible.
1076 static bool function_types_compatible(const function_type_t *func1,
1077 const function_type_t *func2)
1079 const type_t* const ret1 = skip_typeref(func1->return_type);
1080 const type_t* const ret2 = skip_typeref(func2->return_type);
1081 if (!types_compatible(ret1, ret2))
1084 if (func1->linkage != func2->linkage)
1087 cc_kind_t cc1 = func1->calling_convention;
1088 if (cc1 == CC_DEFAULT)
1089 cc1 = default_calling_convention;
1090 cc_kind_t cc2 = func2->calling_convention;
1091 if (cc2 == CC_DEFAULT)
1092 cc2 = default_calling_convention;
1097 if (func1->variadic != func2->variadic)
1100 /* can parameters be compared? */
1101 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1102 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1105 /* TODO: handling of unspecified parameters not correct yet */
1107 /* all argument types must be compatible */
1108 function_parameter_t *parameter1 = func1->parameters;
1109 function_parameter_t *parameter2 = func2->parameters;
1110 for ( ; parameter1 != NULL && parameter2 != NULL;
1111 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1112 type_t *parameter1_type = skip_typeref(parameter1->type);
1113 type_t *parameter2_type = skip_typeref(parameter2->type);
1115 parameter1_type = get_unqualified_type(parameter1_type);
1116 parameter2_type = get_unqualified_type(parameter2_type);
1118 if (!types_compatible(parameter1_type, parameter2_type))
1121 /* same number of arguments? */
1122 if (parameter1 != NULL || parameter2 != NULL)
1129 * Check if two array types are compatible.
1131 static bool array_types_compatible(const array_type_t *array1,
1132 const array_type_t *array2)
1134 type_t *element_type1 = skip_typeref(array1->element_type);
1135 type_t *element_type2 = skip_typeref(array2->element_type);
1136 if (!types_compatible(element_type1, element_type2))
1139 if (!array1->size_constant || !array2->size_constant)
1142 return array1->size == array2->size;
1146 * Check if two types are compatible.
1148 bool types_compatible(const type_t *type1, const type_t *type2)
1150 assert(!is_typeref(type1));
1151 assert(!is_typeref(type2));
1153 /* shortcut: the same type is always compatible */
1157 if (!is_type_valid(type1) || !is_type_valid(type2))
1160 if (type1->base.qualifiers != type2->base.qualifiers)
1162 if (type1->kind != type2->kind)
1165 switch (type1->kind) {
1167 return function_types_compatible(&type1->function, &type2->function);
1169 return type1->atomic.akind == type2->atomic.akind;
1171 return type1->complex.akind == type2->complex.akind;
1172 case TYPE_IMAGINARY:
1173 return type1->imaginary.akind == type2->imaginary.akind;
1175 return array_types_compatible(&type1->array, &type2->array);
1177 case TYPE_POINTER: {
1178 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1179 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1180 return types_compatible(to1, to2);
1183 case TYPE_REFERENCE: {
1184 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1185 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1186 return types_compatible(to1, to2);
1189 case TYPE_COMPOUND_STRUCT:
1190 case TYPE_COMPOUND_UNION: {
1196 /* TODO: not implemented */
1200 /* not sure if this makes sense or is even needed, implement it if you
1201 * really need it! */
1202 panic("type compatibility check for bitfield type");
1205 /* Hmm, the error type should be compatible to all other types */
1208 panic("invalid type found in compatible types");
1211 panic("typerefs not skipped in compatible types?!?");
1214 /* TODO: incomplete */
1219 * Skip all typerefs and return the underlying type.
1221 type_t *skip_typeref(type_t *type)
1223 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1226 switch (type->kind) {
1229 case TYPE_TYPEDEF: {
1230 qualifiers |= type->base.qualifiers;
1232 const typedef_type_t *typedef_type = &type->typedeft;
1233 if (typedef_type->resolved_type != NULL) {
1234 type = typedef_type->resolved_type;
1237 type = typedef_type->typedefe->type;
1241 qualifiers |= type->base.qualifiers;
1242 type = type->typeoft.typeof_type;
1250 if (qualifiers != TYPE_QUALIFIER_NONE) {
1251 type_t *const copy = duplicate_type(type);
1253 /* for const with typedefed array type the element type has to be
1255 if (is_type_array(copy)) {
1256 type_t *element_type = copy->array.element_type;
1257 element_type = duplicate_type(element_type);
1258 element_type->base.qualifiers |= qualifiers;
1259 copy->array.element_type = element_type;
1261 copy->base.qualifiers |= qualifiers;
1264 type = identify_new_type(copy);
1270 unsigned get_type_size(type_t *type)
1272 switch (type->kind) {
1278 return get_atomic_type_size(type->atomic.akind);
1280 return get_atomic_type_size(type->complex.akind) * 2;
1281 case TYPE_IMAGINARY:
1282 return get_atomic_type_size(type->imaginary.akind);
1283 case TYPE_COMPOUND_UNION:
1284 layout_union_type(&type->compound);
1285 return type->compound.compound->size;
1286 case TYPE_COMPOUND_STRUCT:
1287 layout_struct_type(&type->compound);
1288 return type->compound.compound->size;
1290 return get_atomic_type_size(type->enumt.akind);
1292 return 0; /* non-const (but "address-const") */
1293 case TYPE_REFERENCE:
1295 /* TODO: make configurable by backend */
1298 /* TODO: correct if element_type is aligned? */
1299 il_size_t element_size = get_type_size(type->array.element_type);
1300 return type->array.size * element_size;
1305 return get_type_size(type->typedeft.typedefe->type);
1307 if (type->typeoft.typeof_type) {
1308 return get_type_size(type->typeoft.typeof_type);
1310 return get_type_size(type->typeoft.expression->base.type);
1313 panic("invalid type in get_type_size");
1316 unsigned get_type_alignment(type_t *type)
1318 switch (type->kind) {
1324 return get_atomic_type_alignment(type->atomic.akind);
1326 return get_atomic_type_alignment(type->complex.akind);
1327 case TYPE_IMAGINARY:
1328 return get_atomic_type_alignment(type->imaginary.akind);
1329 case TYPE_COMPOUND_UNION:
1330 layout_union_type(&type->compound);
1331 return type->compound.compound->alignment;
1332 case TYPE_COMPOUND_STRUCT:
1333 layout_struct_type(&type->compound);
1334 return type->compound.compound->alignment;
1336 return get_atomic_type_alignment(type->enumt.akind);
1338 /* what is correct here? */
1340 case TYPE_REFERENCE:
1342 /* TODO: make configurable by backend */
1345 return get_type_alignment(type->array.element_type);
1348 case TYPE_TYPEDEF: {
1349 il_alignment_t alignment
1350 = get_type_alignment(type->typedeft.typedefe->type);
1351 if (type->typedeft.typedefe->alignment > alignment)
1352 alignment = type->typedeft.typedefe->alignment;
1357 if (type->typeoft.typeof_type) {
1358 return get_type_alignment(type->typeoft.typeof_type);
1360 return get_type_alignment(type->typeoft.expression->base.type);
1363 panic("invalid type in get_type_alignment");
1366 decl_modifiers_t get_type_modifiers(const type_t *type)
1368 switch(type->kind) {
1372 case TYPE_COMPOUND_STRUCT:
1373 case TYPE_COMPOUND_UNION:
1374 return type->compound.compound->modifiers;
1376 return type->function.modifiers;
1380 case TYPE_IMAGINARY:
1381 case TYPE_REFERENCE:
1386 case TYPE_TYPEDEF: {
1387 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1388 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1392 if (type->typeoft.typeof_type) {
1393 return get_type_modifiers(type->typeoft.typeof_type);
1395 return get_type_modifiers(type->typeoft.expression->base.type);
1398 panic("invalid type found in get_type_modifiers");
1401 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1403 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1406 switch (type->base.kind) {
1408 return TYPE_QUALIFIER_NONE;
1410 qualifiers |= type->base.qualifiers;
1411 const typedef_type_t *typedef_type = &type->typedeft;
1412 if (typedef_type->resolved_type != NULL)
1413 type = typedef_type->resolved_type;
1415 type = typedef_type->typedefe->type;
1418 type = type->typeoft.typeof_type;
1421 if (skip_array_type) {
1422 type = type->array.element_type;
1431 return type->base.qualifiers | qualifiers;
1434 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1436 assert(kind <= ATOMIC_TYPE_LAST);
1437 return atomic_type_properties[kind].size;
1440 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1442 assert(kind <= ATOMIC_TYPE_LAST);
1443 return atomic_type_properties[kind].alignment;
1446 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1448 assert(kind <= ATOMIC_TYPE_LAST);
1449 return atomic_type_properties[kind].flags;
1452 atomic_type_kind_t get_intptr_kind(void)
1454 if (machine_size <= 32)
1455 return ATOMIC_TYPE_INT;
1456 else if (machine_size <= 64)
1457 return ATOMIC_TYPE_LONG;
1459 return ATOMIC_TYPE_LONGLONG;
1462 atomic_type_kind_t get_uintptr_kind(void)
1464 if (machine_size <= 32)
1465 return ATOMIC_TYPE_UINT;
1466 else if (machine_size <= 64)
1467 return ATOMIC_TYPE_ULONG;
1469 return ATOMIC_TYPE_ULONGLONG;
1473 * Find the atomic type kind representing a given size (signed).
1475 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1477 static atomic_type_kind_t kinds[32];
1480 atomic_type_kind_t kind = kinds[size];
1481 if (kind == ATOMIC_TYPE_INVALID) {
1482 static const atomic_type_kind_t possible_kinds[] = {
1487 ATOMIC_TYPE_LONGLONG
1489 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1490 if (get_atomic_type_size(possible_kinds[i]) == size) {
1491 kind = possible_kinds[i];
1501 * Find the atomic type kind representing a given size (signed).
1503 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1505 static atomic_type_kind_t kinds[32];
1508 atomic_type_kind_t kind = kinds[size];
1509 if (kind == ATOMIC_TYPE_INVALID) {
1510 static const atomic_type_kind_t possible_kinds[] = {
1515 ATOMIC_TYPE_ULONGLONG
1517 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1518 if (get_atomic_type_size(possible_kinds[i]) == size) {
1519 kind = possible_kinds[i];
1529 * Hash the given type and return the "singleton" version
1532 type_t *identify_new_type(type_t *type)
1534 type_t *result = typehash_insert(type);
1535 if (result != type) {
1536 obstack_free(type_obst, type);
1542 * Creates a new atomic type.
1544 * @param akind The kind of the atomic type.
1545 * @param qualifiers Type qualifiers for the new type.
1547 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1549 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1550 memset(type, 0, sizeof(atomic_type_t));
1552 type->kind = TYPE_ATOMIC;
1553 type->base.qualifiers = qualifiers;
1554 type->atomic.akind = akind;
1556 return identify_new_type(type);
1560 * Creates a new complex type.
1562 * @param akind The kind of the atomic type.
1563 * @param qualifiers Type qualifiers for the new type.
1565 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1567 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1568 memset(type, 0, sizeof(complex_type_t));
1570 type->kind = TYPE_COMPLEX;
1571 type->base.qualifiers = qualifiers;
1572 type->complex.akind = akind;
1574 return identify_new_type(type);
1578 * Creates a new imaginary type.
1580 * @param akind The kind of the atomic type.
1581 * @param qualifiers Type qualifiers for the new type.
1583 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1585 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1586 memset(type, 0, sizeof(imaginary_type_t));
1588 type->kind = TYPE_IMAGINARY;
1589 type->base.qualifiers = qualifiers;
1590 type->imaginary.akind = akind;
1592 return identify_new_type(type);
1596 * Creates a new pointer type.
1598 * @param points_to The points-to type for the new type.
1599 * @param qualifiers Type qualifiers for the new type.
1601 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1603 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1604 memset(type, 0, sizeof(pointer_type_t));
1606 type->kind = TYPE_POINTER;
1607 type->base.qualifiers = qualifiers;
1608 type->pointer.points_to = points_to;
1609 type->pointer.base_variable = NULL;
1611 return identify_new_type(type);
1615 * Creates a new reference type.
1617 * @param refers_to The referred-to type for the new type.
1619 type_t *make_reference_type(type_t *refers_to)
1621 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1622 memset(type, 0, sizeof(reference_type_t));
1624 type->kind = TYPE_REFERENCE;
1625 type->base.qualifiers = 0;
1626 type->reference.refers_to = refers_to;
1628 return identify_new_type(type);
1632 * Creates a new based pointer type.
1634 * @param points_to The points-to type for the new type.
1635 * @param qualifiers Type qualifiers for the new type.
1636 * @param variable The based variable
1638 type_t *make_based_pointer_type(type_t *points_to,
1639 type_qualifiers_t qualifiers, variable_t *variable)
1641 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1642 memset(type, 0, sizeof(pointer_type_t));
1644 type->kind = TYPE_POINTER;
1645 type->base.qualifiers = qualifiers;
1646 type->pointer.points_to = points_to;
1647 type->pointer.base_variable = variable;
1649 return identify_new_type(type);
1653 type_t *make_array_type(type_t *element_type, size_t size,
1654 type_qualifiers_t qualifiers)
1656 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1657 memset(type, 0, sizeof(array_type_t));
1659 type->kind = TYPE_ARRAY;
1660 type->base.qualifiers = qualifiers;
1661 type->array.element_type = element_type;
1662 type->array.size = size;
1663 type->array.size_constant = true;
1665 return identify_new_type(type);
1668 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1669 il_alignment_t *struct_alignment,
1670 bool packed, entity_t *first)
1672 il_size_t offset = *struct_offset;
1673 il_alignment_t alignment = *struct_alignment;
1674 size_t bit_offset = 0;
1677 for (member = first; member != NULL; member = member->base.next) {
1678 if (member->kind != ENTITY_COMPOUND_MEMBER)
1681 type_t *type = member->declaration.type;
1682 if (type->kind != TYPE_BITFIELD)
1685 type_t *base_type = skip_typeref(type->bitfield.base_type);
1686 il_alignment_t base_alignment = get_type_alignment(base_type);
1687 il_alignment_t alignment_mask = base_alignment-1;
1688 if (base_alignment > alignment)
1689 alignment = base_alignment;
1691 size_t bit_size = type->bitfield.bit_size;
1693 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1694 offset &= ~alignment_mask;
1695 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1697 if (bit_offset + bit_size > base_size || bit_size == 0) {
1698 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1699 offset = (offset + base_alignment-1) & ~alignment_mask;
1704 if (byte_order_big_endian) {
1705 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1706 member->compound_member.offset = offset & ~alignment_mask;
1707 member->compound_member.bit_offset = base_size - bit_offset - bit_size;
1709 member->compound_member.offset = offset;
1710 member->compound_member.bit_offset = bit_offset;
1713 bit_offset += bit_size;
1714 offset += bit_offset / BITS_PER_BYTE;
1715 bit_offset %= BITS_PER_BYTE;
1721 *struct_offset = offset;
1722 *struct_alignment = alignment;
1726 void layout_struct_type(compound_type_t *type)
1728 assert(type->compound != NULL);
1730 compound_t *compound = type->compound;
1731 if (!compound->complete)
1733 if (type->compound->layouted)
1736 il_size_t offset = 0;
1737 il_alignment_t alignment = compound->alignment;
1738 bool need_pad = false;
1740 entity_t *entry = compound->members.entities;
1741 while (entry != NULL) {
1742 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1743 entry = entry->base.next;
1747 type_t *m_type = entry->declaration.type;
1748 type_t *skipped = skip_typeref(m_type);
1749 if (! is_type_valid(skipped)) {
1750 entry = entry->base.next;
1754 if (skipped->kind == TYPE_BITFIELD) {
1755 entry = pack_bitfield_members(&offset, &alignment,
1756 compound->packed, entry);
1760 il_alignment_t m_alignment = get_type_alignment(m_type);
1761 if (m_alignment > alignment)
1762 alignment = m_alignment;
1764 if (!compound->packed) {
1765 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1767 if (new_offset > offset) {
1769 offset = new_offset;
1773 entry->compound_member.offset = offset;
1774 offset += get_type_size(m_type);
1776 entry = entry->base.next;
1779 if (!compound->packed) {
1780 il_size_t new_offset = (offset + alignment-1) & -alignment;
1781 if (new_offset > offset) {
1783 offset = new_offset;
1788 if (warning.padded) {
1789 warningf(&compound->base.source_position, "'%T' needs padding",
1792 } else if (compound->packed && warning.packed) {
1793 warningf(&compound->base.source_position,
1794 "superfluous packed attribute on '%T'", type);
1797 compound->size = offset;
1798 compound->alignment = alignment;
1799 compound->layouted = true;
1802 void layout_union_type(compound_type_t *type)
1804 assert(type->compound != NULL);
1806 compound_t *compound = type->compound;
1807 if (! compound->complete)
1811 il_alignment_t alignment = compound->alignment;
1813 entity_t *entry = compound->members.entities;
1814 for (; entry != NULL; entry = entry->base.next) {
1815 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1818 type_t *m_type = entry->declaration.type;
1819 if (! is_type_valid(skip_typeref(m_type)))
1822 entry->compound_member.offset = 0;
1823 il_size_t m_size = get_type_size(m_type);
1826 il_alignment_t m_alignment = get_type_alignment(m_type);
1827 if (m_alignment > alignment)
1828 alignment = m_alignment;
1830 size = (size + alignment - 1) & -alignment;
1832 compound->size = size;
1833 compound->alignment = alignment;
1836 static function_parameter_t *allocate_parameter(type_t *const type)
1838 function_parameter_t *const param
1839 = obstack_alloc(type_obst, sizeof(*param));
1840 memset(param, 0, sizeof(*param));
1845 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1846 type_t *argument_type2)
1848 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1849 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1850 parameter1->next = parameter2;
1852 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1853 type->function.return_type = return_type;
1854 type->function.parameters = parameter1;
1855 type->function.linkage = LINKAGE_C;
1857 return identify_new_type(type);
1860 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1862 function_parameter_t *const parameter = allocate_parameter(argument_type);
1864 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1865 type->function.return_type = return_type;
1866 type->function.parameters = parameter;
1867 type->function.linkage = LINKAGE_C;
1869 return identify_new_type(type);
1872 type_t *make_function_1_type_variadic(type_t *return_type,
1873 type_t *argument_type)
1875 function_parameter_t *const parameter = allocate_parameter(argument_type);
1877 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1878 type->function.return_type = return_type;
1879 type->function.parameters = parameter;
1880 type->function.variadic = true;
1881 type->function.linkage = LINKAGE_C;
1883 return identify_new_type(type);
1886 type_t *make_function_0_type(type_t *return_type)
1888 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1889 type->function.return_type = return_type;
1890 type->function.parameters = NULL;
1891 type->function.linkage = LINKAGE_C;
1893 return identify_new_type(type);
1896 type_t *make_function_type(type_t *return_type, int n_types,
1897 type_t *const *argument_types,
1898 decl_modifiers_t modifiers)
1900 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1901 type->function.return_type = return_type;
1902 type->function.modifiers |= modifiers;
1903 type->function.linkage = LINKAGE_C;
1905 function_parameter_t *last = NULL;
1906 for (int i = 0; i < n_types; ++i) {
1907 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1909 type->function.parameters = parameter;
1911 last->next = parameter;
1916 return identify_new_type(type);
1920 * Debug helper. Prints the given type to stdout.
1922 static __attribute__((unused))
1923 void dbg_type(const type_t *type)
1925 print_to_file(stderr);