2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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
27 #include "type_hash.h"
28 #include "adt/error.h"
29 #include "lang_features.h"
31 static struct obstack _type_obst;
33 struct obstack *type_obst = &_type_obst;
34 static int type_visited = 0;
35 static bool print_implicit_array_size = false;
37 static void intern_print_type_pre(const type_t *type, bool top);
38 static void intern_print_type_post(const type_t *type, bool top);
42 obstack_init(type_obst);
47 obstack_free(type_obst, NULL);
50 void type_set_output(FILE *stream)
55 void inc_type_visited(void)
60 void print_type_qualifiers(type_qualifiers_t qualifiers)
62 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
63 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
64 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
68 * Prints the name of a atomic type.
70 * @param type The type.
73 void print_atomic_type(const atomic_type_t *type)
75 print_type_qualifiers(type->type.qualifiers);
77 const char *s = "INVALIDATOMIC";
78 switch((atomic_type_kind_t) type->akind) {
79 case ATOMIC_TYPE_INVALID: break;
80 case ATOMIC_TYPE_VOID: s = "void"; break;
81 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
82 case ATOMIC_TYPE_CHAR: s = "char"; break;
83 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
84 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
85 case ATOMIC_TYPE_INT: s = "int"; break;
86 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
87 case ATOMIC_TYPE_SHORT: s = "short"; break;
88 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
89 case ATOMIC_TYPE_LONG: s = "long"; break;
90 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
91 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
92 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
93 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
94 case ATOMIC_TYPE_FLOAT: s = "float"; break;
95 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
96 case ATOMIC_TYPE_FLOAT_COMPLEX: s = "_Complex float"; break;
97 case ATOMIC_TYPE_DOUBLE_COMPLEX: s = "_Complex float"; break;
98 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX: s = "_Complex float"; break;
99 case ATOMIC_TYPE_FLOAT_IMAGINARY: s = "_Imaginary float"; break;
100 case ATOMIC_TYPE_DOUBLE_IMAGINARY: s = "_Imaginary float"; break;
101 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY: s = "_Imaginary float"; break;
107 * Print the first part (the prefix) of a type.
109 * @param type The type to print.
110 * @param top true, if this is the top type, false if it's an embedded type.
112 static void print_function_type_pre(const function_type_t *type, bool top)
114 print_type_qualifiers(type->type.qualifiers);
116 intern_print_type_pre(type->return_type, false);
118 /* don't emit braces if we're the toplevel type... */
124 * Print the second part (the postfix) of a type.
126 * @param type The type to print.
127 * @param top true, if this is the top type, false if it's an embedded type.
129 static void print_function_type_post(const function_type_t *type,
130 const scope_t *scope, bool top)
132 intern_print_type_post(type->return_type, false);
133 /* don't emit braces if we're the toplevel type... */
141 function_parameter_t *parameter = type->parameters;
142 for( ; parameter != NULL; parameter = parameter->next) {
148 print_type(parameter->type);
151 declaration_t *parameter = scope->declarations;
152 for( ; parameter != NULL; parameter = parameter->next) {
158 print_type_ext(parameter->type, parameter->symbol,
170 if(first && !type->unspecified_parameters) {
177 * Prints the prefix part of a pointer type.
179 * @param type The pointer type.
181 static void print_pointer_type_pre(const pointer_type_t *type)
183 intern_print_type_pre(type->points_to, false);
185 print_type_qualifiers(type->type.qualifiers);
189 * Prints the postfix part of a pointer type.
191 * @param type The pointer type.
193 static void print_pointer_type_post(const pointer_type_t *type)
195 intern_print_type_post(type->points_to, false);
199 * Prints the prefix part of an array type.
201 * @param type The array type.
203 static void print_array_type_pre(const array_type_t *type)
205 intern_print_type_pre(type->element_type, false);
209 * Prints the postfix part of an array type.
211 * @param type The array type.
213 static void print_array_type_post(const array_type_t *type)
216 if(type->is_static) {
217 fputs("static ", out);
219 print_type_qualifiers(type->type.qualifiers);
220 if(type->size_expression != NULL
221 && (print_implicit_array_size || !type->has_implicit_size)) {
222 print_expression(type->size_expression);
225 intern_print_type_post(type->element_type, false);
229 * Prints the postfix part of a bitfield type.
231 * @param type The array type.
233 static void print_bitfield_type_post(const bitfield_type_t *type)
236 print_expression(type->size);
237 intern_print_type_post(type->base, false);
241 * Prints an enum definition.
243 * @param declaration The enum's type declaration.
245 void print_enum_definition(const declaration_t *declaration)
251 declaration_t *entry = declaration->next;
252 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
253 entry = entry->next) {
256 fprintf(out, "%s", entry->symbol->string);
257 if(entry->init.initializer != NULL) {
260 /* skip the implicit cast */
261 expression_t *expression = entry->init.enum_value;
262 if(expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
263 expression = expression->unary.value;
265 print_expression(expression);
276 * Prints an enum type.
278 * @param type The enum type.
280 static void print_type_enum(const enum_type_t *type)
282 print_type_qualifiers(type->type.qualifiers);
285 declaration_t *declaration = type->declaration;
286 symbol_t *symbol = declaration->symbol;
288 fputs(symbol->string, out);
290 print_enum_definition(declaration);
295 * Print the compound part of a compound type.
297 * @param declaration The declaration of the compound type.
299 void print_compound_definition(const declaration_t *declaration)
304 declaration_t *iter = declaration->scope.declarations;
305 for( ; iter != NULL; iter = iter->next) {
307 print_declaration(iter);
317 * Prints a compound type.
319 * @param type The compound type.
321 static void print_compound_type(const compound_type_t *type)
323 print_type_qualifiers(type->type.qualifiers);
325 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
326 fputs("struct ", out);
328 assert(type->type.kind == TYPE_COMPOUND_UNION);
329 fputs("union ", out);
332 declaration_t *declaration = type->declaration;
333 symbol_t *symbol = declaration->symbol;
335 fputs(symbol->string, out);
337 print_compound_definition(declaration);
342 * Prints the prefix part of a typedef type.
344 * @param type The typedef type.
346 static void print_typedef_type_pre(const typedef_type_t *const type)
348 print_type_qualifiers(type->type.qualifiers);
349 fputs(type->declaration->symbol->string, out);
353 * Prints the prefix part of a typeof type.
355 * @param type The typeof type.
357 static void print_typeof_type_pre(const typeof_type_t *const type)
359 fputs("typeof(", out);
360 if(type->expression != NULL) {
361 assert(type->typeof_type == NULL);
362 print_expression(type->expression);
364 print_type(type->typeof_type);
370 * Prints the prefix part of a type.
372 * @param type The type.
373 * @param top true if we print the toplevel type, false else.
375 static void intern_print_type_pre(const type_t *const type, const bool top)
379 fputs("<error>", out);
381 fputs("<invalid>", out);
384 print_type_enum(&type->enumt);
387 print_atomic_type(&type->atomic);
389 case TYPE_COMPOUND_STRUCT:
390 case TYPE_COMPOUND_UNION:
391 print_compound_type(&type->compound);
394 fputs(type->builtin.symbol->string, out);
397 print_function_type_pre(&type->function, top);
400 print_pointer_type_pre(&type->pointer);
403 intern_print_type_pre(type->bitfield.base, top);
406 print_array_type_pre(&type->array);
409 print_typedef_type_pre(&type->typedeft);
412 print_typeof_type_pre(&type->typeoft);
415 fputs("unknown", out);
419 * Prints the postfix part of a type.
421 * @param type The type.
422 * @param top true if we print the toplevel type, false else.
424 static void intern_print_type_post(const type_t *const type, const bool top)
428 print_function_type_post(&type->function, NULL, top);
431 print_pointer_type_post(&type->pointer);
434 print_array_type_post(&type->array);
437 print_bitfield_type_post(&type->bitfield);
443 case TYPE_COMPOUND_STRUCT:
444 case TYPE_COMPOUND_UNION:
455 * @param type The type.
457 void print_type(const type_t *const type)
459 print_type_ext(type, NULL, NULL);
462 void print_type_ext(const type_t *const type, const symbol_t *symbol,
463 const scope_t *scope)
466 fputs("nil type", out);
470 intern_print_type_pre(type, true);
473 fputs(symbol->string, out);
475 if(type->kind == TYPE_FUNCTION) {
476 print_function_type_post(&type->function, scope, true);
478 intern_print_type_post(type, true);
483 * Return the size of a type AST node.
485 * @param type The type.
487 static size_t get_type_size(const type_t *type)
490 case TYPE_ATOMIC: return sizeof(atomic_type_t);
491 case TYPE_COMPOUND_STRUCT:
492 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
493 case TYPE_ENUM: return sizeof(enum_type_t);
494 case TYPE_FUNCTION: return sizeof(function_type_t);
495 case TYPE_POINTER: return sizeof(pointer_type_t);
496 case TYPE_ARRAY: return sizeof(array_type_t);
497 case TYPE_BUILTIN: return sizeof(builtin_type_t);
498 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
499 case TYPE_TYPEOF: return sizeof(typeof_type_t);
500 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
501 case TYPE_ERROR: panic("error type found");
502 case TYPE_INVALID: panic("invalid type found");
504 panic("unknown type found");
510 * @param type The type to copy.
511 * @return A copy of the type.
513 * @note This does not produce a deep copy!
515 type_t *duplicate_type(const type_t *type)
517 size_t size = get_type_size(type);
519 type_t *copy = obstack_alloc(type_obst, size);
520 memcpy(copy, type, size);
526 * Returns the unqualified type of a given type.
528 * @param type The type.
529 * @returns The unqualified type.
531 type_t *get_unqualified_type(type_t *type)
533 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
536 type_t *unqualified_type = duplicate_type(type);
537 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
539 type_t *result = typehash_insert(unqualified_type);
540 if(result != unqualified_type) {
541 obstack_free(type_obst, unqualified_type);
548 * Check if a type is valid.
550 * @param type The type to check.
551 * @return true if type represents a valid type.
553 bool type_valid(const type_t *type)
555 return type->kind != TYPE_INVALID;
559 * Returns true if the given type is an integer type.
561 * @param type The type to check.
562 * @return True if type is an integer type.
564 bool is_type_integer(const type_t *type)
566 assert(!is_typeref(type));
568 if(type->kind == TYPE_ENUM)
571 if(type->kind != TYPE_ATOMIC)
574 switch((atomic_type_kind_t) type->atomic.akind) {
575 case ATOMIC_TYPE_BOOL:
576 case ATOMIC_TYPE_CHAR:
577 case ATOMIC_TYPE_SCHAR:
578 case ATOMIC_TYPE_UCHAR:
579 case ATOMIC_TYPE_SHORT:
580 case ATOMIC_TYPE_USHORT:
581 case ATOMIC_TYPE_INT:
582 case ATOMIC_TYPE_UINT:
583 case ATOMIC_TYPE_LONG:
584 case ATOMIC_TYPE_ULONG:
585 case ATOMIC_TYPE_LONGLONG:
586 case ATOMIC_TYPE_ULONGLONG:
589 case ATOMIC_TYPE_INVALID:
590 case ATOMIC_TYPE_VOID:
591 case ATOMIC_TYPE_FLOAT:
592 case ATOMIC_TYPE_DOUBLE:
593 case ATOMIC_TYPE_LONG_DOUBLE:
594 case ATOMIC_TYPE_FLOAT_COMPLEX:
595 case ATOMIC_TYPE_DOUBLE_COMPLEX:
596 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
597 case ATOMIC_TYPE_FLOAT_IMAGINARY:
598 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
599 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
603 panic("unexpected atomic type kind");
607 * Returns true if the given type is an floating point type.
609 * @param type The type to check.
610 * @return True if type is a floating point type.
612 bool is_type_float(const type_t *type)
614 assert(!is_typeref(type));
616 if(type->kind != TYPE_ATOMIC)
619 switch((atomic_type_kind_t) type->atomic.akind) {
620 case ATOMIC_TYPE_FLOAT:
621 case ATOMIC_TYPE_DOUBLE:
622 case ATOMIC_TYPE_LONG_DOUBLE:
623 case ATOMIC_TYPE_FLOAT_COMPLEX:
624 case ATOMIC_TYPE_DOUBLE_COMPLEX:
625 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
626 case ATOMIC_TYPE_FLOAT_IMAGINARY:
627 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
628 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
631 case ATOMIC_TYPE_INVALID:
632 case ATOMIC_TYPE_VOID:
633 case ATOMIC_TYPE_BOOL:
634 case ATOMIC_TYPE_CHAR:
635 case ATOMIC_TYPE_SCHAR:
636 case ATOMIC_TYPE_UCHAR:
637 case ATOMIC_TYPE_SHORT:
638 case ATOMIC_TYPE_USHORT:
639 case ATOMIC_TYPE_INT:
640 case ATOMIC_TYPE_UINT:
641 case ATOMIC_TYPE_LONG:
642 case ATOMIC_TYPE_ULONG:
643 case ATOMIC_TYPE_LONGLONG:
644 case ATOMIC_TYPE_ULONGLONG:
648 panic("unexpected atomic type kind");
652 * Returns true if the given type is a signed type.
654 * @param type The type to check.
655 * @return True if type is a signed type.
657 bool is_type_signed(const type_t *type)
659 assert(!is_typeref(type));
661 /* enum types are int for now */
662 if(type->kind == TYPE_ENUM)
665 if(type->kind != TYPE_ATOMIC)
668 switch((atomic_type_kind_t) type->atomic.akind) {
669 case ATOMIC_TYPE_CHAR:
670 case ATOMIC_TYPE_SCHAR:
671 case ATOMIC_TYPE_SHORT:
672 case ATOMIC_TYPE_INT:
673 case ATOMIC_TYPE_LONG:
674 case ATOMIC_TYPE_LONGLONG:
675 case ATOMIC_TYPE_FLOAT:
676 case ATOMIC_TYPE_DOUBLE:
677 case ATOMIC_TYPE_LONG_DOUBLE:
678 case ATOMIC_TYPE_FLOAT_COMPLEX:
679 case ATOMIC_TYPE_DOUBLE_COMPLEX:
680 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
681 case ATOMIC_TYPE_FLOAT_IMAGINARY:
682 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
683 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
686 case ATOMIC_TYPE_BOOL:
687 case ATOMIC_TYPE_UCHAR:
688 case ATOMIC_TYPE_USHORT:
689 case ATOMIC_TYPE_UINT:
690 case ATOMIC_TYPE_ULONG:
691 case ATOMIC_TYPE_ULONGLONG:
694 case ATOMIC_TYPE_VOID:
695 case ATOMIC_TYPE_INVALID:
699 panic("invalid atomic type found");
704 * Returns true if the given type represents an arithmetic type.
706 * @param type The type to check.
707 * @return True if type represents an arithmetic type.
709 bool is_type_arithmetic(const type_t *type)
711 assert(!is_typeref(type));
713 if(type->kind == TYPE_BITFIELD)
716 if(is_type_integer(type) || is_type_float(type))
723 * Returns true if the given type represents a scalar type.
725 * @param type The type to check.
726 * @return True if type represents a scalar type.
728 bool is_type_scalar(const type_t *type)
730 assert(!is_typeref(type));
732 switch (type->kind) {
733 case TYPE_POINTER: return true;
734 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
738 return is_type_arithmetic(type);
742 * Check if a given type is incomplete.
744 * @param type The type to check.
745 * @return True if the given type is incomplete (ie. just forward).
747 bool is_type_incomplete(const type_t *type)
749 assert(!is_typeref(type));
752 case TYPE_COMPOUND_STRUCT:
753 case TYPE_COMPOUND_UNION: {
754 const compound_type_t *compound_type = &type->compound;
755 declaration_t *declaration = compound_type->declaration;
756 return !declaration->init.is_defined;
759 const enum_type_t *enum_type = &type->enumt;
760 declaration_t *declaration = enum_type->declaration;
761 return !declaration->init.is_defined;
768 return type->array.size_expression == NULL;
771 return type->atomic.akind == ATOMIC_TYPE_VOID;
780 panic("is_type_incomplete called without typerefs skipped");
785 panic("invalid type found");
789 * Check if two function types are compatible.
791 static bool function_types_compatible(const function_type_t *func1,
792 const function_type_t *func2)
794 const type_t* const ret1 = skip_typeref(func1->return_type);
795 const type_t* const ret2 = skip_typeref(func2->return_type);
796 if (!types_compatible(ret1, ret2))
799 /* can parameters be compared? */
800 if(func1->unspecified_parameters || func2->unspecified_parameters)
803 if(func1->variadic != func2->variadic)
806 /* TODO: handling of unspecified parameters not correct yet */
808 /* all argument types must be compatible */
809 function_parameter_t *parameter1 = func1->parameters;
810 function_parameter_t *parameter2 = func2->parameters;
811 for( ; parameter1 != NULL && parameter2 != NULL;
812 parameter1 = parameter1->next, parameter2 = parameter2->next) {
813 type_t *parameter1_type = skip_typeref(parameter1->type);
814 type_t *parameter2_type = skip_typeref(parameter2->type);
816 parameter1_type = get_unqualified_type(parameter1_type);
817 parameter2_type = get_unqualified_type(parameter2_type);
819 if(!types_compatible(parameter1_type, parameter2_type))
822 /* same number of arguments? */
823 if(parameter1 != NULL || parameter2 != NULL)
830 * Check if two array types are compatible.
832 static bool array_types_compatible(const array_type_t *array1,
833 const array_type_t *array2)
835 type_t *element_type1 = skip_typeref(array1->element_type);
836 type_t *element_type2 = skip_typeref(array2->element_type);
837 if(!types_compatible(element_type1, element_type2))
840 if(!array1->size_constant || !array2->size_constant)
843 return array1->size == array2->size;
847 * Check if two types are compatible.
849 bool types_compatible(const type_t *type1, const type_t *type2)
851 assert(!is_typeref(type1));
852 assert(!is_typeref(type2));
854 /* shortcut: the same type is always compatible */
858 if(type1->base.qualifiers != type2->base.qualifiers)
860 if(type1->kind != type2->kind)
863 switch(type1->kind) {
865 return function_types_compatible(&type1->function, &type2->function);
867 return type1->atomic.akind == type2->atomic.akind;
869 return array_types_compatible(&type1->array, &type2->array);
872 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
873 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
874 return types_compatible(to1, to2);
877 case TYPE_COMPOUND_STRUCT:
878 case TYPE_COMPOUND_UNION:
881 /* TODO: not implemented */
885 /* not sure if this makes sense or is even needed, implement it if you
887 panic("type compatibility check for bitfield type");
890 /* Hmm, the error type should be compatible to all other types */
893 panic("invalid type found in compatible types");
896 panic("typerefs not skipped in compatible types?!?");
899 /* TODO: incomplete */
904 * Check if two pointer types are compatible.
906 bool pointers_compatible(const type_t *type1, const type_t *type2)
908 assert(!is_typeref(type1));
909 assert(!is_typeref(type2));
911 assert(type1->kind == TYPE_POINTER);
912 assert(type2->kind == TYPE_POINTER);
920 * Skip all typerefs and return the underlying type.
922 type_t *skip_typeref(type_t *type)
924 unsigned qualifiers = TYPE_QUALIFIER_NONE;
931 qualifiers |= type->base.qualifiers;
932 const typedef_type_t *typedef_type = &type->typedeft;
933 if(typedef_type->resolved_type != NULL) {
934 type = typedef_type->resolved_type;
937 type = typedef_type->declaration->type;
941 const typeof_type_t *typeof_type = &type->typeoft;
942 if(typeof_type->typeof_type != NULL) {
943 type = typeof_type->typeof_type;
945 type = typeof_type->expression->base.type;
955 if (qualifiers != TYPE_QUALIFIER_NONE) {
956 type_t *const copy = duplicate_type(type);
957 copy->base.qualifiers |= qualifiers;
959 type = typehash_insert(copy);
961 obstack_free(type_obst, copy);
968 unsigned get_atomic_type_size(atomic_type_kind_t kind)
971 case ATOMIC_TYPE_CHAR:
972 case ATOMIC_TYPE_SCHAR:
973 case ATOMIC_TYPE_UCHAR:
976 case ATOMIC_TYPE_SHORT:
977 case ATOMIC_TYPE_USHORT:
980 case ATOMIC_TYPE_BOOL:
981 case ATOMIC_TYPE_INT:
982 case ATOMIC_TYPE_UINT:
983 return machine_size >> 3;
985 case ATOMIC_TYPE_LONG:
986 case ATOMIC_TYPE_ULONG:
987 return machine_size > 16 ? machine_size >> 3 : 4;
989 case ATOMIC_TYPE_LONGLONG:
990 case ATOMIC_TYPE_ULONGLONG:
991 return machine_size > 16 ? 8 : 4;
993 case ATOMIC_TYPE_FLOAT_IMAGINARY:
994 case ATOMIC_TYPE_FLOAT:
997 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
998 case ATOMIC_TYPE_DOUBLE:
1001 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1002 case ATOMIC_TYPE_LONG_DOUBLE:
1005 case ATOMIC_TYPE_VOID:
1008 case ATOMIC_TYPE_FLOAT_COMPLEX:
1009 return 2 * get_atomic_type_size(ATOMIC_TYPE_FLOAT);
1011 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1012 return 2 * get_atomic_type_size(ATOMIC_TYPE_DOUBLE);
1014 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1015 return 2 * get_atomic_type_size(ATOMIC_TYPE_LONG_DOUBLE);
1017 case ATOMIC_TYPE_INVALID:
1020 panic("Trying to determine size of invalid atomic type");
1024 * Find the atomic type kind representing a given size (signed).
1026 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
1027 static atomic_type_kind_t kinds[32];
1030 atomic_type_kind_t kind = kinds[size];
1031 if(kind == ATOMIC_TYPE_INVALID) {
1032 static const possible_kinds[] = {
1037 ATOMIC_TYPE_LONGLONG
1039 for(int i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1040 if(get_atomic_type_size(possible_kinds[i]) == size) {
1041 kind = possible_kinds[i];
1051 * Find the atomic type kind representing a given size (signed).
1053 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
1054 static atomic_type_kind_t kinds[32];
1057 atomic_type_kind_t kind = kinds[size];
1058 if(kind == ATOMIC_TYPE_INVALID) {
1059 static const possible_kinds[] = {
1064 ATOMIC_TYPE_ULONGLONG
1066 for(int i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
1067 if(get_atomic_type_size(possible_kinds[i]) == size) {
1068 kind = possible_kinds[i];
1078 * Hash the given type and return the "singleton" version
1081 static type_t *identify_new_type(type_t *type)
1083 type_t *result = typehash_insert(type);
1084 if(result != type) {
1085 obstack_free(type_obst, type);
1091 * Creates a new atomic type.
1093 * @param akind The kind of the atomic type.
1094 * @param qualifiers Type qualifiers for the new type.
1096 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
1098 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
1099 memset(type, 0, sizeof(atomic_type_t));
1101 type->kind = TYPE_ATOMIC;
1102 type->base.qualifiers = qualifiers;
1103 type->base.alignment = 0;
1104 type->atomic.akind = atype;
1106 /* TODO: set the alignment depending on the atype here */
1108 return identify_new_type(type);
1112 * Creates a new pointer type.
1114 * @param points_to The points-to type for the new type.
1115 * @param qualifiers Type qualifiers for the new type.
1117 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
1119 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
1120 memset(type, 0, sizeof(pointer_type_t));
1122 type->kind = TYPE_POINTER;
1123 type->base.qualifiers = qualifiers;
1124 type->base.alignment = 0;
1125 type->pointer.points_to = points_to;
1127 return identify_new_type(type);
1130 type_t *make_array_type(type_t *element_type, size_t size,
1131 type_qualifiers_t qualifiers)
1133 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
1134 memset(type, 0, sizeof(array_type_t));
1136 type->kind = TYPE_ARRAY;
1137 type->base.qualifiers = qualifiers;
1138 type->base.alignment = 0;
1139 type->array.element_type = element_type;
1140 type->array.size = size;
1141 type->array.size_constant = true;
1143 return identify_new_type(type);
1147 * Debug helper. Prints the given type to stdout.
1149 static __attribute__((unused))
1150 void dbg_type(const type_t *type)
1152 FILE *old_out = out;