2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "type_hash.h"
30 #include "adt/error.h"
32 #include "lang_features.h"
34 #include "diagnostic.h"
36 #include "driver/firm_cmdline.h"
38 /** The default calling convention. */
39 cc_kind_t default_calling_convention = CC_CDECL;
41 static struct obstack _type_obst;
42 struct obstack *type_obst = &_type_obst;
43 static bool print_implicit_array_size = false;
45 static void intern_print_type_pre(const type_t *type);
46 static void intern_print_type_post(const type_t *type);
48 typedef struct atomic_type_properties_t atomic_type_properties_t;
49 struct atomic_type_properties_t {
50 unsigned size; /**< type size in bytes */
51 unsigned alignment; /**< type alignment in bytes */
52 unsigned flags; /**< type flags from atomic_type_flag_t */
56 * Returns the size of a type node.
58 * @param kind the type kind
60 static size_t get_type_struct_size(type_kind_t kind)
62 static const size_t sizes[] = {
63 [TYPE_ATOMIC] = sizeof(atomic_type_t),
64 [TYPE_COMPLEX] = sizeof(complex_type_t),
65 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
66 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
67 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
68 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
69 [TYPE_ENUM] = sizeof(enum_type_t),
70 [TYPE_FUNCTION] = sizeof(function_type_t),
71 [TYPE_POINTER] = sizeof(pointer_type_t),
72 [TYPE_ARRAY] = sizeof(array_type_t),
73 [TYPE_BUILTIN] = sizeof(builtin_type_t),
74 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
75 [TYPE_TYPEOF] = sizeof(typeof_type_t),
77 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
78 assert(kind <= TYPE_TYPEOF);
79 assert(sizes[kind] != 0);
83 type_t *allocate_type_zero(type_kind_t kind)
85 size_t size = get_type_struct_size(kind);
86 type_t *res = obstack_alloc(type_obst, size);
88 res->base.kind = kind;
94 * Properties of atomic types.
96 static atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
97 //ATOMIC_TYPE_INVALID = 0,
98 [ATOMIC_TYPE_VOID] = {
101 .flags = ATOMIC_TYPE_FLAG_NONE
103 [ATOMIC_TYPE_WCHAR_T] = {
104 .size = (unsigned)-1,
105 .alignment = (unsigned)-1,
106 /* signed flag will be set when known */
107 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
109 [ATOMIC_TYPE_CHAR] = {
112 /* signed flag will be set when known */
113 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
115 [ATOMIC_TYPE_SCHAR] = {
118 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
119 | ATOMIC_TYPE_FLAG_SIGNED,
121 [ATOMIC_TYPE_UCHAR] = {
124 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
126 [ATOMIC_TYPE_SHORT] = {
129 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
130 | ATOMIC_TYPE_FLAG_SIGNED
132 [ATOMIC_TYPE_USHORT] = {
135 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
137 [ATOMIC_TYPE_INT] = {
138 .size = (unsigned) -1,
139 .alignment = (unsigned) -1,
140 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
141 | ATOMIC_TYPE_FLAG_SIGNED,
143 [ATOMIC_TYPE_UINT] = {
144 .size = (unsigned) -1,
145 .alignment = (unsigned) -1,
146 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
148 [ATOMIC_TYPE_LONG] = {
149 .size = (unsigned) -1,
150 .alignment = (unsigned) -1,
151 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
152 | ATOMIC_TYPE_FLAG_SIGNED,
154 [ATOMIC_TYPE_ULONG] = {
155 .size = (unsigned) -1,
156 .alignment = (unsigned) -1,
157 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
159 [ATOMIC_TYPE_LONGLONG] = {
160 .size = (unsigned) -1,
161 .alignment = (unsigned) -1,
162 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
163 | ATOMIC_TYPE_FLAG_SIGNED,
165 [ATOMIC_TYPE_ULONGLONG] = {
166 .size = (unsigned) -1,
167 .alignment = (unsigned) -1,
168 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
170 [ATOMIC_TYPE_BOOL] = {
171 .size = (unsigned) -1,
172 .alignment = (unsigned) -1,
173 .flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
175 [ATOMIC_TYPE_FLOAT] = {
177 .alignment = (unsigned) -1,
178 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
179 | ATOMIC_TYPE_FLAG_SIGNED,
181 [ATOMIC_TYPE_DOUBLE] = {
183 .alignment = (unsigned) -1,
184 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
185 | ATOMIC_TYPE_FLAG_SIGNED,
187 [ATOMIC_TYPE_LONG_DOUBLE] = {
189 .alignment = (unsigned) -1,
190 .flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
191 | ATOMIC_TYPE_FLAG_SIGNED,
193 /* complex and imaginary types are set in init_types */
196 void init_types(void)
198 obstack_init(type_obst);
200 atomic_type_properties_t *props = atomic_type_properties;
202 if (char_is_signed) {
203 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
206 unsigned int_size = machine_size < 32 ? 2 : 4;
207 /* long is always 32bit on windows */
208 unsigned long_size = c_mode & _MS ? 4 : (machine_size < 64 ? 4 : 8);
209 unsigned llong_size = machine_size < 32 ? 4 : 8;
211 props[ATOMIC_TYPE_INT].size = int_size;
212 props[ATOMIC_TYPE_INT].alignment = int_size;
213 props[ATOMIC_TYPE_UINT].size = int_size;
214 props[ATOMIC_TYPE_UINT].alignment = int_size;
215 props[ATOMIC_TYPE_LONG].size = long_size;
216 props[ATOMIC_TYPE_LONG].alignment = long_size;
217 props[ATOMIC_TYPE_ULONG].size = long_size;
218 props[ATOMIC_TYPE_ULONG].alignment = long_size;
219 props[ATOMIC_TYPE_LONGLONG].size = llong_size;
220 props[ATOMIC_TYPE_LONGLONG].alignment = llong_size;
221 props[ATOMIC_TYPE_ULONGLONG].size = llong_size;
222 props[ATOMIC_TYPE_ULONGLONG].alignment = llong_size;
224 /* TODO: backend specific, need a way to query the backend for this.
225 * The following are good settings for x86 */
226 if (machine_size <= 32) {
227 props[ATOMIC_TYPE_FLOAT].alignment = 4;
228 props[ATOMIC_TYPE_DOUBLE].alignment = 4;
229 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 4;
230 props[ATOMIC_TYPE_LONGLONG].alignment = 4;
231 props[ATOMIC_TYPE_ULONGLONG].alignment = 4;
233 props[ATOMIC_TYPE_FLOAT].alignment = 4;
234 props[ATOMIC_TYPE_DOUBLE].alignment = 8;
235 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 8;
236 props[ATOMIC_TYPE_LONGLONG].alignment = 8;
237 props[ATOMIC_TYPE_ULONGLONG].alignment = 8;
239 if (force_long_double_size > 0) {
240 props[ATOMIC_TYPE_LONG_DOUBLE].size = force_long_double_size;
241 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = force_long_double_size;
244 /* TODO: make this configurable for platforms which do not use byte sized
246 props[ATOMIC_TYPE_BOOL] = props[ATOMIC_TYPE_UCHAR];
248 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
251 void exit_types(void)
253 obstack_free(type_obst, NULL);
256 void print_type_qualifiers(type_qualifiers_t qualifiers)
258 if (qualifiers & TYPE_QUALIFIER_CONST) {
259 print_string("const ");
261 if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
262 print_string("volatile ");
264 if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
265 print_string("restrict ");
269 const char *get_atomic_kind_name(atomic_type_kind_t kind)
272 case ATOMIC_TYPE_INVALID: break;
273 case ATOMIC_TYPE_VOID: return "void";
274 case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
275 case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
276 case ATOMIC_TYPE_CHAR: return "char";
277 case ATOMIC_TYPE_SCHAR: return "signed char";
278 case ATOMIC_TYPE_UCHAR: return "unsigned char";
279 case ATOMIC_TYPE_INT: return "int";
280 case ATOMIC_TYPE_UINT: return "unsigned int";
281 case ATOMIC_TYPE_SHORT: return "short";
282 case ATOMIC_TYPE_USHORT: return "unsigned short";
283 case ATOMIC_TYPE_LONG: return "long";
284 case ATOMIC_TYPE_ULONG: return "unsigned long";
285 case ATOMIC_TYPE_LONGLONG: return "long long";
286 case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
287 case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
288 case ATOMIC_TYPE_FLOAT: return "float";
289 case ATOMIC_TYPE_DOUBLE: return "double";
291 return "INVALIDATOMIC";
295 * Prints the name of an atomic type kinds.
297 * @param kind The type kind.
299 static void print_atomic_kinds(atomic_type_kind_t kind)
301 const char *s = get_atomic_kind_name(kind);
306 * Prints the name of an atomic type.
308 * @param type The type.
310 static void print_atomic_type(const atomic_type_t *type)
312 print_type_qualifiers(type->base.qualifiers);
313 print_atomic_kinds(type->akind);
317 * Prints the name of a complex type.
319 * @param type The type.
321 static void print_complex_type(const complex_type_t *type)
323 print_type_qualifiers(type->base.qualifiers);
324 print_string("_Complex");
325 print_atomic_kinds(type->akind);
329 * Prints the name of an imaginary type.
331 * @param type The type.
333 static void print_imaginary_type(const imaginary_type_t *type)
335 print_type_qualifiers(type->base.qualifiers);
336 print_string("_Imaginary ");
337 print_atomic_kinds(type->akind);
341 * Print the first part (the prefix) of a type.
343 * @param type The type to print.
345 static void print_function_type_pre(const function_type_t *type)
347 switch (type->linkage) {
348 case LINKAGE_INVALID:
353 print_string("extern \"C\" ");
357 if (!(c_mode & _CXX))
358 print_string("extern \"C++\" ");
362 print_type_qualifiers(type->base.qualifiers);
364 intern_print_type_pre(type->return_type);
366 cc_kind_t cc = type->calling_convention;
369 case CC_CDECL: print_string(" __cdecl"); break;
370 case CC_STDCALL: print_string(" __stdcall"); break;
371 case CC_FASTCALL: print_string(" __fastcall"); break;
372 case CC_THISCALL: print_string(" __thiscall"); break;
374 if (default_calling_convention != CC_CDECL) {
375 /* show the default calling convention if its not cdecl */
376 cc = default_calling_convention;
384 * Print the second part (the postfix) of a type.
386 * @param type The type to print.
388 static void print_function_type_post(const function_type_t *type,
389 const scope_t *parameters)
393 if (parameters == NULL) {
394 function_parameter_t *parameter = type->parameters;
395 for( ; parameter != NULL; parameter = parameter->next) {
401 print_type(parameter->type);
404 entity_t *parameter = parameters->entities;
405 for (; parameter != NULL; parameter = parameter->base.next) {
406 if (parameter->kind != ENTITY_PARAMETER)
414 const type_t *const type = parameter->declaration.type;
416 print_string(parameter->base.symbol->string);
418 print_type_ext(type, parameter->base.symbol, NULL);
422 if (type->variadic) {
430 if (first && !type->unspecified_parameters) {
431 print_string("void");
435 intern_print_type_post(type->return_type);
439 * Prints the prefix part of a pointer type.
441 * @param type The pointer type.
443 static void print_pointer_type_pre(const pointer_type_t *type)
445 type_t const *const points_to = type->points_to;
446 intern_print_type_pre(points_to);
447 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
449 variable_t *const variable = type->base_variable;
450 if (variable != NULL) {
451 print_string(" __based(");
452 print_string(variable->base.base.symbol->string);
456 type_qualifiers_t const qual = type->base.qualifiers;
459 print_type_qualifiers(qual);
463 * Prints the postfix part of a pointer type.
465 * @param type The pointer type.
467 static void print_pointer_type_post(const pointer_type_t *type)
469 type_t const *const points_to = type->points_to;
470 if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
472 intern_print_type_post(points_to);
476 * Prints the prefix part of a reference type.
478 * @param type The reference type.
480 static void print_reference_type_pre(const reference_type_t *type)
482 type_t const *const refers_to = type->refers_to;
483 intern_print_type_pre(refers_to);
484 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
490 * Prints the postfix part of a reference type.
492 * @param type The reference type.
494 static void print_reference_type_post(const reference_type_t *type)
496 type_t const *const refers_to = type->refers_to;
497 if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
499 intern_print_type_post(refers_to);
503 * Prints the prefix part of an array type.
505 * @param type The array type.
507 static void print_array_type_pre(const array_type_t *type)
509 intern_print_type_pre(type->element_type);
513 * Prints the postfix part of an array type.
515 * @param type The array type.
517 static void print_array_type_post(const array_type_t *type)
520 if (type->is_static) {
521 print_string("static ");
523 print_type_qualifiers(type->base.qualifiers);
524 if (type->size_expression != NULL
525 && (print_implicit_array_size || !type->has_implicit_size)) {
526 print_expression(type->size_expression);
529 intern_print_type_post(type->element_type);
533 * Prints the postfix part of a bitfield type.
535 * @param type The array type.
537 static void print_bitfield_type_post(const bitfield_type_t *type)
540 print_expression(type->size_expression);
541 intern_print_type_post(type->base_type);
545 * Prints an enum definition.
547 * @param declaration The enum's type declaration.
549 void print_enum_definition(const enum_t *enume)
555 entity_t *entry = enume->base.next;
556 for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
557 entry = entry->base.next) {
560 print_string(entry->base.symbol->string);
561 if (entry->enum_value.value != NULL) {
564 /* skip the implicit cast */
565 expression_t *expression = entry->enum_value.value;
566 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
567 expression = expression->unary.value;
569 print_expression(expression);
580 * Prints an enum type.
582 * @param type The enum type.
584 static void print_type_enum(const enum_type_t *type)
586 print_type_qualifiers(type->base.qualifiers);
587 print_string("enum ");
589 enum_t *enume = type->enume;
590 symbol_t *symbol = enume->base.symbol;
591 if (symbol != NULL) {
592 print_string(symbol->string);
594 print_enum_definition(enume);
599 * Print the compound part of a compound type.
601 void print_compound_definition(const compound_t *compound)
606 entity_t *entity = compound->members.entities;
607 for( ; entity != NULL; entity = entity->base.next) {
608 if (entity->kind != ENTITY_COMPOUND_MEMBER)
612 print_entity(entity);
619 if (compound->modifiers & DM_TRANSPARENT_UNION) {
620 print_string("__attribute__((__transparent_union__))");
625 * Prints a compound type.
627 * @param type The compound type.
629 static void print_compound_type(const compound_type_t *type)
631 print_type_qualifiers(type->base.qualifiers);
633 if (type->base.kind == TYPE_COMPOUND_STRUCT) {
634 print_string("struct ");
636 assert(type->base.kind == TYPE_COMPOUND_UNION);
637 print_string("union ");
640 compound_t *compound = type->compound;
641 symbol_t *symbol = compound->base.symbol;
642 if (symbol != NULL) {
643 print_string(symbol->string);
645 print_compound_definition(compound);
650 * Prints the prefix part of a typedef type.
652 * @param type The typedef type.
654 static void print_typedef_type_pre(const typedef_type_t *const type)
656 print_type_qualifiers(type->base.qualifiers);
657 print_string(type->typedefe->base.symbol->string);
661 * Prints the prefix part of a typeof type.
663 * @param type The typeof type.
665 static void print_typeof_type_pre(const typeof_type_t *const type)
667 print_string("typeof(");
668 if (type->expression != NULL) {
669 print_expression(type->expression);
671 print_type(type->typeof_type);
677 * Prints the prefix part of a type.
679 * @param type The type.
681 static void intern_print_type_pre(const type_t *const type)
685 print_string("<error>");
688 print_string("<invalid>");
691 print_type_enum(&type->enumt);
694 print_atomic_type(&type->atomic);
697 print_complex_type(&type->complex);
700 print_imaginary_type(&type->imaginary);
702 case TYPE_COMPOUND_STRUCT:
703 case TYPE_COMPOUND_UNION:
704 print_compound_type(&type->compound);
707 print_string(type->builtin.symbol->string);
710 print_function_type_pre(&type->function);
713 print_pointer_type_pre(&type->pointer);
716 print_reference_type_pre(&type->reference);
719 intern_print_type_pre(type->bitfield.base_type);
722 print_array_type_pre(&type->array);
725 print_typedef_type_pre(&type->typedeft);
728 print_typeof_type_pre(&type->typeoft);
731 print_string("unknown");
735 * Prints the postfix part of a type.
737 * @param type The type.
739 static void intern_print_type_post(const type_t *const type)
743 print_function_type_post(&type->function, NULL);
746 print_pointer_type_post(&type->pointer);
749 print_reference_type_post(&type->reference);
752 print_array_type_post(&type->array);
755 print_bitfield_type_post(&type->bitfield);
763 case TYPE_COMPOUND_STRUCT:
764 case TYPE_COMPOUND_UNION:
775 * @param type The type.
777 void print_type(const type_t *const type)
779 print_type_ext(type, NULL, NULL);
782 void print_type_ext(const type_t *const type, const symbol_t *symbol,
783 const scope_t *parameters)
785 intern_print_type_pre(type);
786 if (symbol != NULL) {
788 print_string(symbol->string);
790 if (type->kind == TYPE_FUNCTION) {
791 print_function_type_post(&type->function, parameters);
793 intern_print_type_post(type);
800 * @param type The type to copy.
801 * @return A copy of the type.
803 * @note This does not produce a deep copy!
805 type_t *duplicate_type(const type_t *type)
807 size_t size = get_type_struct_size(type->kind);
809 type_t *copy = obstack_alloc(type_obst, size);
810 memcpy(copy, type, size);
811 copy->base.firm_type = NULL;
817 * Returns the unqualified type of a given type.
819 * @param type The type.
820 * @returns The unqualified type.
822 type_t *get_unqualified_type(type_t *type)
824 assert(!is_typeref(type));
826 if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
829 type_t *unqualified_type = duplicate_type(type);
830 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
832 return identify_new_type(unqualified_type);
835 type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
837 type_t *type = skip_typeref(orig_type);
840 if (is_type_array(type)) {
841 /* For array types the element type has to be adjusted */
842 type_t *element_type = type->array.element_type;
843 type_t *qual_element_type = get_qualified_type(element_type, qual);
845 if (qual_element_type == element_type)
848 copy = duplicate_type(type);
849 copy->array.element_type = qual_element_type;
850 } else if (is_type_valid(type)) {
851 if ((type->base.qualifiers & qual) == qual)
854 copy = duplicate_type(type);
855 copy->base.qualifiers |= qual;
860 return identify_new_type(copy);
864 * Check if a type is valid.
866 * @param type The type to check.
867 * @return true if type represents a valid type.
869 bool type_valid(const type_t *type)
871 return type->kind != TYPE_INVALID;
874 static bool test_atomic_type_flag(atomic_type_kind_t kind,
875 atomic_type_flag_t flag)
877 assert(kind <= ATOMIC_TYPE_LAST);
878 return (atomic_type_properties[kind].flags & flag) != 0;
882 * Returns true if the given type is an integer type.
884 * @param type The type to check.
885 * @return True if type is an integer type.
887 bool is_type_integer(const type_t *type)
889 assert(!is_typeref(type));
891 if (type->kind == TYPE_ENUM)
893 if (type->kind == TYPE_BITFIELD)
896 if (type->kind != TYPE_ATOMIC)
899 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
903 * Returns true if the given type is an enum type.
905 * @param type The type to check.
906 * @return True if type is an enum type.
908 bool is_type_enum(const type_t *type)
910 assert(!is_typeref(type));
911 return type->kind == TYPE_ENUM;
915 * Returns true if the given type is an floating point type.
917 * @param type The type to check.
918 * @return True if type is a floating point type.
920 bool is_type_float(const type_t *type)
922 assert(!is_typeref(type));
924 if (type->kind != TYPE_ATOMIC)
927 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
931 * Returns true if the given type is an complex type.
933 * @param type The type to check.
934 * @return True if type is a complex type.
936 bool is_type_complex(const type_t *type)
938 assert(!is_typeref(type));
940 if (type->kind != TYPE_ATOMIC)
943 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
947 * Returns true if the given type is a signed type.
949 * @param type The type to check.
950 * @return True if type is a signed type.
952 bool is_type_signed(const type_t *type)
954 assert(!is_typeref(type));
956 /* enum types are int for now */
957 if (type->kind == TYPE_ENUM)
959 if (type->kind == TYPE_BITFIELD)
960 return is_type_signed(type->bitfield.base_type);
962 if (type->kind != TYPE_ATOMIC)
965 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
969 * Returns true if the given type represents an arithmetic type.
971 * @param type The type to check.
972 * @return True if type represents an arithmetic type.
974 bool is_type_arithmetic(const type_t *type)
976 assert(!is_typeref(type));
983 return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
985 return test_atomic_type_flag(type->complex.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
987 return test_atomic_type_flag(type->imaginary.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
994 * Returns true if the given type is an integer or float type.
996 * @param type The type to check.
997 * @return True if type is an integer or float type.
999 bool is_type_real(const type_t *type)
1002 return is_type_integer(type) || is_type_float(type);
1006 * Returns true if the given type represents a scalar type.
1008 * @param type The type to check.
1009 * @return True if type represents a scalar type.
1011 bool is_type_scalar(const type_t *type)
1013 assert(!is_typeref(type));
1015 switch (type->kind) {
1016 case TYPE_POINTER: return true;
1017 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
1021 return is_type_arithmetic(type);
1025 * Check if a given type is incomplete.
1027 * @param type The type to check.
1028 * @return True if the given type is incomplete (ie. just forward).
1030 bool is_type_incomplete(const type_t *type)
1032 assert(!is_typeref(type));
1034 switch(type->kind) {
1035 case TYPE_COMPOUND_STRUCT:
1036 case TYPE_COMPOUND_UNION: {
1037 const compound_type_t *compound_type = &type->compound;
1038 return !compound_type->compound->complete;
1044 return type->array.size_expression == NULL
1045 && !type->array.size_constant;
1048 return type->atomic.akind == ATOMIC_TYPE_VOID;
1051 return type->complex.akind == ATOMIC_TYPE_VOID;
1053 case TYPE_IMAGINARY:
1054 return type->imaginary.akind == ATOMIC_TYPE_VOID;
1059 case TYPE_REFERENCE:
1066 panic("is_type_incomplete called without typerefs skipped");
1071 panic("invalid type found");
1074 bool is_type_object(const type_t *type)
1076 return !is_type_function(type) && !is_type_incomplete(type);
1079 bool is_builtin_va_list(type_t *type)
1081 type_t *tp = skip_typeref(type);
1083 return tp->kind == type_valist->kind &&
1084 tp->builtin.symbol == type_valist->builtin.symbol;
1088 * Check if two function types are compatible.
1090 static bool function_types_compatible(const function_type_t *func1,
1091 const function_type_t *func2)
1093 const type_t* const ret1 = skip_typeref(func1->return_type);
1094 const type_t* const ret2 = skip_typeref(func2->return_type);
1095 if (!types_compatible(ret1, ret2))
1098 if (func1->linkage != func2->linkage)
1101 cc_kind_t cc1 = func1->calling_convention;
1102 if (cc1 == CC_DEFAULT)
1103 cc1 = default_calling_convention;
1104 cc_kind_t cc2 = func2->calling_convention;
1105 if (cc2 == CC_DEFAULT)
1106 cc2 = default_calling_convention;
1111 if (func1->variadic != func2->variadic)
1114 /* can parameters be compared? */
1115 if ((func1->unspecified_parameters && !func1->kr_style_parameters)
1116 || (func2->unspecified_parameters && !func2->kr_style_parameters))
1119 /* TODO: handling of unspecified parameters not correct yet */
1121 /* all argument types must be compatible */
1122 function_parameter_t *parameter1 = func1->parameters;
1123 function_parameter_t *parameter2 = func2->parameters;
1124 for ( ; parameter1 != NULL && parameter2 != NULL;
1125 parameter1 = parameter1->next, parameter2 = parameter2->next) {
1126 type_t *parameter1_type = skip_typeref(parameter1->type);
1127 type_t *parameter2_type = skip_typeref(parameter2->type);
1129 parameter1_type = get_unqualified_type(parameter1_type);
1130 parameter2_type = get_unqualified_type(parameter2_type);
1132 if (!types_compatible(parameter1_type, parameter2_type))
1135 /* same number of arguments? */
1136 if (parameter1 != NULL || parameter2 != NULL)
1143 * Check if two array types are compatible.
1145 static bool array_types_compatible(const array_type_t *array1,
1146 const array_type_t *array2)
1148 type_t *element_type1 = skip_typeref(array1->element_type);
1149 type_t *element_type2 = skip_typeref(array2->element_type);
1150 if (!types_compatible(element_type1, element_type2))
1153 if (!array1->size_constant || !array2->size_constant)
1156 return array1->size == array2->size;
1160 * Check if two types are compatible.
1162 bool types_compatible(const type_t *type1, const type_t *type2)
1164 assert(!is_typeref(type1));
1165 assert(!is_typeref(type2));
1167 /* shortcut: the same type is always compatible */
1171 if (!is_type_valid(type1) || !is_type_valid(type2))
1174 if (type1->base.qualifiers != type2->base.qualifiers)
1176 if (type1->kind != type2->kind)
1179 switch (type1->kind) {
1181 return function_types_compatible(&type1->function, &type2->function);
1183 return type1->atomic.akind == type2->atomic.akind;
1185 return type1->complex.akind == type2->complex.akind;
1186 case TYPE_IMAGINARY:
1187 return type1->imaginary.akind == type2->imaginary.akind;
1189 return array_types_compatible(&type1->array, &type2->array);
1191 case TYPE_POINTER: {
1192 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
1193 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
1194 return types_compatible(to1, to2);
1197 case TYPE_REFERENCE: {
1198 const type_t *const to1 = skip_typeref(type1->reference.refers_to);
1199 const type_t *const to2 = skip_typeref(type2->reference.refers_to);
1200 return types_compatible(to1, to2);
1203 case TYPE_COMPOUND_STRUCT:
1204 case TYPE_COMPOUND_UNION: {
1211 /* TODO: not implemented */
1215 /* not sure if this makes sense or is even needed, implement it if you
1216 * really need it! */
1217 panic("type compatibility check for bitfield type");
1220 /* Hmm, the error type should be compatible to all other types */
1223 panic("invalid type found in compatible types");
1226 panic("typerefs not skipped in compatible types?!?");
1229 /* TODO: incomplete */
1234 * Skip all typerefs and return the underlying type.
1236 type_t *skip_typeref(type_t *type)
1238 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1241 switch (type->kind) {
1244 case TYPE_TYPEDEF: {
1245 qualifiers |= type->base.qualifiers;
1247 const typedef_type_t *typedef_type = &type->typedeft;
1248 if (typedef_type->resolved_type != NULL) {
1249 type = typedef_type->resolved_type;
1252 type = typedef_type->typedefe->type;
1256 qualifiers |= type->base.qualifiers;
1257 type = type->typeoft.typeof_type;
1265 if (qualifiers != TYPE_QUALIFIER_NONE) {
1266 type_t *const copy = duplicate_type(type);
1268 /* for const with typedefed array type the element type has to be
1270 if (is_type_array(copy)) {
1271 type_t *element_type = copy->array.element_type;
1272 element_type = duplicate_type(element_type);
1273 element_type->base.qualifiers |= qualifiers;
1274 copy->array.element_type = element_type;
1276 copy->base.qualifiers |= qualifiers;
1279 type = identify_new_type(copy);
1285 unsigned get_type_size(type_t *type)
1287 switch (type->kind) {
1293 return get_atomic_type_size(type->atomic.akind);
1295 return get_atomic_type_size(type->complex.akind) * 2;
1296 case TYPE_IMAGINARY:
1297 return get_atomic_type_size(type->imaginary.akind);
1298 case TYPE_COMPOUND_UNION:
1299 layout_union_type(&type->compound);
1300 return type->compound.compound->size;
1301 case TYPE_COMPOUND_STRUCT:
1302 layout_struct_type(&type->compound);
1303 return type->compound.compound->size;
1305 return get_atomic_type_size(type->enumt.akind);
1307 return 0; /* non-const (but "address-const") */
1308 case TYPE_REFERENCE:
1310 /* TODO: make configurable by backend */
1313 /* TODO: correct if element_type is aligned? */
1314 il_size_t element_size = get_type_size(type->array.element_type);
1315 return type->array.size * element_size;
1320 return get_type_size(type->builtin.real_type);
1322 return get_type_size(type->typedeft.typedefe->type);
1324 if (type->typeoft.typeof_type) {
1325 return get_type_size(type->typeoft.typeof_type);
1327 return get_type_size(type->typeoft.expression->base.type);
1330 panic("invalid type in get_type_size");
1333 unsigned get_type_alignment(type_t *type)
1335 switch (type->kind) {
1341 return get_atomic_type_alignment(type->atomic.akind);
1343 return get_atomic_type_alignment(type->complex.akind);
1344 case TYPE_IMAGINARY:
1345 return get_atomic_type_alignment(type->imaginary.akind);
1346 case TYPE_COMPOUND_UNION:
1347 layout_union_type(&type->compound);
1348 return type->compound.compound->alignment;
1349 case TYPE_COMPOUND_STRUCT:
1350 layout_struct_type(&type->compound);
1351 return type->compound.compound->alignment;
1353 return get_atomic_type_alignment(type->enumt.akind);
1355 /* what is correct here? */
1357 case TYPE_REFERENCE:
1359 /* TODO: make configurable by backend */
1362 return get_type_alignment(type->array.element_type);
1366 return get_type_alignment(type->builtin.real_type);
1367 case TYPE_TYPEDEF: {
1368 il_alignment_t alignment
1369 = get_type_alignment(type->typedeft.typedefe->type);
1370 if (type->typedeft.typedefe->alignment > alignment)
1371 alignment = type->typedeft.typedefe->alignment;
1376 if (type->typeoft.typeof_type) {
1377 return get_type_alignment(type->typeoft.typeof_type);
1379 return get_type_alignment(type->typeoft.expression->base.type);
1382 panic("invalid type in get_type_alignment");
1385 decl_modifiers_t get_type_modifiers(const type_t *type)
1387 switch(type->kind) {
1391 case TYPE_COMPOUND_STRUCT:
1392 case TYPE_COMPOUND_UNION:
1393 return type->compound.compound->modifiers;
1395 return type->function.modifiers;
1399 case TYPE_IMAGINARY:
1400 case TYPE_REFERENCE:
1406 return get_type_modifiers(type->builtin.real_type);
1407 case TYPE_TYPEDEF: {
1408 decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
1409 modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
1413 if (type->typeoft.typeof_type) {
1414 return get_type_modifiers(type->typeoft.typeof_type);
1416 return get_type_modifiers(type->typeoft.expression->base.type);
1419 panic("invalid type found in get_type_modifiers");
1422 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
1424 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
1427 switch (type->base.kind) {
1429 return TYPE_QUALIFIER_NONE;
1431 qualifiers |= type->base.qualifiers;
1432 const typedef_type_t *typedef_type = &type->typedeft;
1433 if (typedef_type->resolved_type != NULL)
1434 type = typedef_type->resolved_type;
1436 type = typedef_type->typedefe->type;
1439 type = type->typeoft.typeof_type;
1442 if (skip_array_type) {
1443 type = type->array.element_type;
1452 return type->base.qualifiers | qualifiers;
1455 unsigned get_atomic_type_size(atomic_type_kind_t kind)
1457 assert(kind <= ATOMIC_TYPE_LAST);
1458 return atomic_type_properties[kind].size;
1461 unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
1463 assert(kind <= ATOMIC_TYPE_LAST);
1464 return atomic_type_properties[kind].alignment;
1467 unsigned get_atomic_type_flags(atomic_type_kind_t kind)
1469 assert(kind <= ATOMIC_TYPE_LAST);
1470 return atomic_type_properties[kind].flags;
1473 atomic_type_kind_t get_intptr_kind(void)
1475 if (machine_size <= 32)
1476 return ATOMIC_TYPE_INT;
1477 else if (machine_size <= 64)
1478 return ATOMIC_TYPE_LONG;
1480 return ATOMIC_TYPE_LONGLONG;
1483 atomic_type_kind_t get_uintptr_kind(void)
1485 if (machine_size <= 32)
1486 return ATOMIC_TYPE_UINT;
1487 else if (machine_size <= 64)
1488 return ATOMIC_TYPE_ULONG;
1490 return ATOMIC_TYPE_ULONGLONG;
1494 * Find the atomic type kind representing a given size (signed).
1496 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size)
1498 static atomic_type_kind_t kinds[32];
1501 atomic_type_kind_t kind = kinds[size];
1502 if (kind == ATOMIC_TYPE_INVALID) {
1503 static const atomic_type_kind_t possible_kinds[] = {
1508 ATOMIC_TYPE_LONGLONG
1510 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1511 if (get_atomic_type_size(possible_kinds[i]) == size) {
1512 kind = possible_kinds[i];
1522 * Find the atomic type kind representing a given size (signed).
1524 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size)
1526 static atomic_type_kind_t kinds[32];
1529 atomic_type_kind_t kind = kinds[size];
1530 if (kind == ATOMIC_TYPE_INVALID) {
1531 static const atomic_type_kind_t possible_kinds[] = {
1536 ATOMIC_TYPE_ULONGLONG
1538 for (size_t i = 0; i < lengthof(possible_kinds); ++i) {
1539 if (get_atomic_type_size(possible_kinds[i]) == size) {
1540 kind = possible_kinds[i];
1550 * Hash the given type and return the "singleton" version
1553 type_t *identify_new_type(type_t *type)
1555 type_t *result = typehash_insert(type);
1556 if (result != type) {
1557 obstack_free(type_obst, type);
1563 * Creates a new atomic type.
1565 * @param akind The kind of the atomic type.
1566 * @param qualifiers Type qualifiers for the new type.
1568 type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1570 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1571 memset(type, 0, sizeof(atomic_type_t));
1573 type->kind = TYPE_ATOMIC;
1574 type->base.qualifiers = qualifiers;
1575 type->atomic.akind = akind;
1577 return identify_new_type(type);
1581 * Creates a new complex type.
1583 * @param akind The kind of the atomic type.
1584 * @param qualifiers Type qualifiers for the new type.
1586 type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1588 type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
1589 memset(type, 0, sizeof(complex_type_t));
1591 type->kind = TYPE_COMPLEX;
1592 type->base.qualifiers = qualifiers;
1593 type->complex.akind = akind;
1595 return identify_new_type(type);
1599 * Creates a new imaginary type.
1601 * @param akind The kind of the atomic type.
1602 * @param qualifiers Type qualifiers for the new type.
1604 type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
1606 type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
1607 memset(type, 0, sizeof(imaginary_type_t));
1609 type->kind = TYPE_IMAGINARY;
1610 type->base.qualifiers = qualifiers;
1611 type->imaginary.akind = akind;
1613 return identify_new_type(type);
1617 * Creates a new pointer type.
1619 * @param points_to The points-to type for the new type.
1620 * @param qualifiers Type qualifiers for the new type.
1622 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1624 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1625 memset(type, 0, sizeof(pointer_type_t));
1627 type->kind = TYPE_POINTER;
1628 type->base.qualifiers = qualifiers;
1629 type->pointer.points_to = points_to;
1630 type->pointer.base_variable = NULL;
1632 return identify_new_type(type);
1636 * Creates a new reference type.
1638 * @param refers_to The referred-to type for the new type.
1640 type_t *make_reference_type(type_t *refers_to)
1642 type_t *type = obstack_alloc(type_obst, sizeof(reference_type_t));
1643 memset(type, 0, sizeof(reference_type_t));
1645 type->kind = TYPE_REFERENCE;
1646 type->base.qualifiers = 0;
1647 type->reference.refers_to = refers_to;
1649 return identify_new_type(type);
1653 * Creates a new based pointer type.
1655 * @param points_to The points-to type for the new type.
1656 * @param qualifiers Type qualifiers for the new type.
1657 * @param variable The based variable
1659 type_t *make_based_pointer_type(type_t *points_to,
1660 type_qualifiers_t qualifiers, variable_t *variable)
1662 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1663 memset(type, 0, sizeof(pointer_type_t));
1665 type->kind = TYPE_POINTER;
1666 type->base.qualifiers = qualifiers;
1667 type->pointer.points_to = points_to;
1668 type->pointer.base_variable = variable;
1670 return identify_new_type(type);
1674 type_t *make_array_type(type_t *element_type, size_t size,
1675 type_qualifiers_t qualifiers)
1677 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1678 memset(type, 0, sizeof(array_type_t));
1680 type->kind = TYPE_ARRAY;
1681 type->base.qualifiers = qualifiers;
1682 type->array.element_type = element_type;
1683 type->array.size = size;
1684 type->array.size_constant = true;
1686 return identify_new_type(type);
1689 static entity_t *pack_bitfield_members_big_endian(il_size_t *struct_offset,
1690 il_alignment_t *struct_alignment, bool packed, entity_t *first)
1692 type_t *current_base_type = NULL;
1693 il_size_t offset = *struct_offset;
1694 il_alignment_t alignment = *struct_alignment;
1695 size_t bit_offset = 0;
1698 panic("packed bitfields on big-endian arch not supported yet");
1701 for (member = first; member != NULL; member = member->base.next) {
1702 if (member->kind != ENTITY_COMPOUND_MEMBER)
1705 type_t *type = member->declaration.type;
1706 if (type->kind != TYPE_BITFIELD)
1709 size_t bit_size = type->bitfield.bit_size;
1710 type_t *base_type = skip_typeref(type->bitfield.base_type);
1712 /* see if we need to start a new "bucket" */
1713 if (base_type != current_base_type || bit_size > bit_offset) {
1714 if (current_base_type != NULL)
1715 offset += get_type_size(current_base_type);
1717 current_base_type = base_type;
1718 il_alignment_t base_alignment = get_type_alignment(base_type);
1719 il_alignment_t alignment_mask = base_alignment-1;
1720 if (base_alignment > alignment)
1721 alignment = base_alignment;
1722 offset = (offset + base_alignment-1) & ~alignment_mask;
1723 bit_offset = get_type_size(base_type) * BITS_PER_BYTE;
1724 assert(bit_offset >= bit_size);
1727 bit_offset -= bit_size;
1728 member->compound_member.offset = offset;
1729 member->compound_member.bit_offset = bit_offset;
1732 if (current_base_type != NULL)
1733 offset += get_type_size(current_base_type);
1735 *struct_offset = offset;
1736 *struct_alignment = alignment;
1740 static entity_t *pack_bitfield_members(il_size_t *struct_offset,
1741 il_alignment_t *struct_alignment,
1742 bool packed, entity_t *first)
1744 il_size_t offset = *struct_offset;
1745 il_alignment_t alignment = *struct_alignment;
1746 size_t bit_offset = 0;
1749 for (member = first; member != NULL; member = member->base.next) {
1750 if (member->kind != ENTITY_COMPOUND_MEMBER)
1753 type_t *type = member->declaration.type;
1754 if (type->kind != TYPE_BITFIELD)
1757 type_t *base_type = skip_typeref(type->bitfield.base_type);
1758 il_alignment_t base_alignment = get_type_alignment(base_type);
1759 il_alignment_t alignment_mask = base_alignment-1;
1760 if (base_alignment > alignment)
1761 alignment = base_alignment;
1763 size_t bit_size = type->bitfield.bit_size;
1765 bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
1766 offset &= ~alignment_mask;
1767 size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
1769 if (bit_offset + bit_size > base_size || bit_size == 0) {
1770 offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
1771 offset = (offset + base_alignment-1) & ~alignment_mask;
1776 member->compound_member.offset = offset;
1777 member->compound_member.bit_offset = bit_offset;
1779 bit_offset += bit_size;
1780 offset += bit_offset / BITS_PER_BYTE;
1781 bit_offset %= BITS_PER_BYTE;
1787 *struct_offset = offset;
1788 *struct_alignment = alignment;
1792 void layout_struct_type(compound_type_t *type)
1794 assert(type->compound != NULL);
1796 compound_t *compound = type->compound;
1797 if (!compound->complete)
1799 if (type->compound->layouted)
1802 il_size_t offset = 0;
1803 il_alignment_t alignment = compound->alignment;
1804 bool need_pad = false;
1806 entity_t *entry = compound->members.entities;
1807 while (entry != NULL) {
1808 if (entry->kind != ENTITY_COMPOUND_MEMBER) {
1809 entry = entry->base.next;
1813 type_t *m_type = entry->declaration.type;
1814 type_t *skipped = skip_typeref(m_type);
1815 if (! is_type_valid(skipped)) {
1816 entry = entry->base.next;
1820 if (skipped->kind == TYPE_BITFIELD) {
1821 if (byte_order_big_endian) {
1822 entry = pack_bitfield_members_big_endian(&offset, &alignment,
1826 entry = pack_bitfield_members(&offset, &alignment,
1827 compound->packed, entry);
1832 il_alignment_t m_alignment = get_type_alignment(m_type);
1833 if (m_alignment > alignment)
1834 alignment = m_alignment;
1836 if (!compound->packed) {
1837 il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
1839 if (new_offset > offset) {
1841 offset = new_offset;
1845 entry->compound_member.offset = offset;
1846 offset += get_type_size(m_type);
1848 entry = entry->base.next;
1851 if (!compound->packed) {
1852 il_size_t new_offset = (offset + alignment-1) & -alignment;
1853 if (new_offset > offset) {
1855 offset = new_offset;
1860 if (warning.padded) {
1861 warningf(&compound->base.source_position, "'%T' needs padding",
1864 } else if (compound->packed && warning.packed) {
1865 warningf(&compound->base.source_position,
1866 "superfluous packed attribute on '%T'", type);
1869 compound->size = offset;
1870 compound->alignment = alignment;
1871 compound->layouted = true;
1874 void layout_union_type(compound_type_t *type)
1876 assert(type->compound != NULL);
1878 compound_t *compound = type->compound;
1879 if (! compound->complete)
1883 il_alignment_t alignment = compound->alignment;
1885 entity_t *entry = compound->members.entities;
1886 for (; entry != NULL; entry = entry->base.next) {
1887 if (entry->kind != ENTITY_COMPOUND_MEMBER)
1890 type_t *m_type = entry->declaration.type;
1891 if (! is_type_valid(skip_typeref(m_type)))
1894 entry->compound_member.offset = 0;
1895 il_size_t m_size = get_type_size(m_type);
1898 il_alignment_t m_alignment = get_type_alignment(m_type);
1899 if (m_alignment > alignment)
1900 alignment = m_alignment;
1902 size = (size + alignment - 1) & -alignment;
1904 compound->size = size;
1905 compound->alignment = alignment;
1908 static function_parameter_t *allocate_parameter(type_t *const type)
1910 function_parameter_t *const param
1911 = obstack_alloc(type_obst, sizeof(*param));
1912 memset(param, 0, sizeof(*param));
1917 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
1918 type_t *argument_type2)
1920 function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
1921 function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
1922 parameter1->next = parameter2;
1924 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1925 type->function.return_type = return_type;
1926 type->function.parameters = parameter1;
1927 type->function.linkage = LINKAGE_C;
1929 return identify_new_type(type);
1932 type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
1934 function_parameter_t *const parameter = allocate_parameter(argument_type);
1936 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1937 type->function.return_type = return_type;
1938 type->function.parameters = parameter;
1939 type->function.linkage = LINKAGE_C;
1941 return identify_new_type(type);
1944 type_t *make_function_1_type_variadic(type_t *return_type,
1945 type_t *argument_type)
1947 function_parameter_t *const parameter = allocate_parameter(argument_type);
1949 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1950 type->function.return_type = return_type;
1951 type->function.parameters = parameter;
1952 type->function.variadic = true;
1953 type->function.linkage = LINKAGE_C;
1955 return identify_new_type(type);
1958 type_t *make_function_0_type(type_t *return_type)
1960 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1961 type->function.return_type = return_type;
1962 type->function.parameters = NULL;
1963 type->function.linkage = LINKAGE_C;
1965 return identify_new_type(type);
1968 type_t *make_function_type(type_t *return_type, int n_types,
1969 type_t *const *argument_types,
1970 decl_modifiers_t modifiers)
1972 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1973 type->function.return_type = return_type;
1974 type->function.modifiers |= modifiers;
1975 type->function.linkage = LINKAGE_C;
1977 function_parameter_t *last = NULL;
1978 for (int i = 0; i < n_types; ++i) {
1979 function_parameter_t *parameter = allocate_parameter(argument_types[i]);
1981 type->function.parameters = parameter;
1983 last->next = parameter;
1988 return identify_new_type(type);
1992 * Debug helper. Prints the given type to stdout.
1994 static __attribute__((unused))
1995 void dbg_type(const type_t *type)
1997 print_to_file(stderr);