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
25 #include "type_hash.h"
26 #include "adt/error.h"
28 static struct obstack _type_obst;
30 struct obstack *type_obst = &_type_obst;
31 static int type_visited = 0;
32 static bool print_implicit_array_size = true;
34 static void intern_print_type_pre(const type_t *type, bool top);
35 static void intern_print_type_post(const type_t *type, bool top);
39 obstack_init(type_obst);
44 obstack_free(type_obst, NULL);
47 void type_set_output(FILE *stream)
52 void inc_type_visited(void)
57 void print_type_qualifiers(type_qualifiers_t qualifiers)
59 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
60 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
61 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
65 * Prints the name of a atomic type.
67 * @param type The type.
70 void print_atomic_type(const atomic_type_t *type)
72 print_type_qualifiers(type->type.qualifiers);
76 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
77 case ATOMIC_TYPE_VOID: s = "void"; break;
78 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
79 case ATOMIC_TYPE_CHAR: s = "char"; break;
80 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
81 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
82 case ATOMIC_TYPE_INT: s = "int"; break;
83 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
84 case ATOMIC_TYPE_SHORT: s = "short"; break;
85 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
86 case ATOMIC_TYPE_LONG: s = "long"; break;
87 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
88 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
89 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
90 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
91 case ATOMIC_TYPE_FLOAT: s = "float"; break;
92 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
93 default: s = "UNKNOWNATOMIC"; break;
99 * Print the first part (the prefix) of a type.
101 * @param type The type to print.
102 * @param top true, if this is the top type, false if it's an embedded type.
104 static void print_function_type_pre(const function_type_t *type, bool top)
106 print_type_qualifiers(type->type.qualifiers);
108 intern_print_type_pre(type->return_type, false);
110 /* don't emit braces if we're the toplevel type... */
116 * Print the second part (the postfix) of a type.
118 * @param type The type to print.
119 * @param top true, if this is the top type, false if it's an embedded type.
121 static void print_function_type_post(const function_type_t *type,
122 const scope_t *scope, bool top)
124 intern_print_type_post(type->return_type, false);
125 /* don't emit braces if we're the toplevel type... */
133 function_parameter_t *parameter = type->parameters;
134 for( ; parameter != NULL; parameter = parameter->next) {
140 print_type(parameter->type);
143 declaration_t *parameter = scope->declarations;
144 for( ; parameter != NULL; parameter = parameter->next) {
150 print_type_ext(parameter->type, parameter->symbol,
162 if(first && !type->unspecified_parameters) {
169 * Prints the prefix part of a pointer type.
171 * @param type The pointer type.
173 static void print_pointer_type_pre(const pointer_type_t *type)
175 intern_print_type_pre(type->points_to, false);
177 print_type_qualifiers(type->type.qualifiers);
181 * Prints the postfix part of a pointer type.
183 * @param type The pointer type.
185 static void print_pointer_type_post(const pointer_type_t *type)
187 intern_print_type_post(type->points_to, false);
191 * Prints the prefix part of an array type.
193 * @param type The array type.
195 static void print_array_type_pre(const array_type_t *type)
197 intern_print_type_pre(type->element_type, false);
201 * Prints the postfix part of an array type.
203 * @param type The array type.
205 static void print_array_type_post(const array_type_t *type)
208 if(type->is_static) {
209 fputs("static ", out);
211 print_type_qualifiers(type->type.qualifiers);
212 if(type->size_expression != NULL
213 && (print_implicit_array_size || !type->has_implicit_size)) {
214 print_expression(type->size_expression);
217 intern_print_type_post(type->element_type, false);
221 * Prints the postfix part of a bitfield type.
223 * @param type The array type.
225 static void print_bitfield_type_post(const bitfield_type_t *type)
228 print_expression(type->size);
229 intern_print_type_post(type->base, false);
233 * Prints an enum definition.
235 * @param declaration The enum's type declaration.
237 void print_enum_definition(const declaration_t *declaration)
243 declaration_t *entry = declaration->next;
244 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
245 entry = entry->next) {
248 fprintf(out, "%s", entry->symbol->string);
249 if(entry->init.initializer != NULL) {
251 print_expression(entry->init.enum_value);
262 * Prints an enum type.
264 * @param type The enum type.
266 static void print_type_enum(const enum_type_t *type)
268 print_type_qualifiers(type->type.qualifiers);
271 declaration_t *declaration = type->declaration;
272 symbol_t *symbol = declaration->symbol;
274 fputs(symbol->string, out);
276 print_enum_definition(declaration);
281 * Print the compound part of a compound type.
283 * @param declaration The declaration of the compound type.
285 void print_compound_definition(const declaration_t *declaration)
290 declaration_t *iter = declaration->scope.declarations;
291 for( ; iter != NULL; iter = iter->next) {
293 print_declaration(iter);
303 * Prints a compound type.
305 * @param type The compound type.
307 static void print_compound_type(const compound_type_t *type)
309 print_type_qualifiers(type->type.qualifiers);
311 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
312 fputs("struct ", out);
314 assert(type->type.kind == TYPE_COMPOUND_UNION);
315 fputs("union ", out);
318 declaration_t *declaration = type->declaration;
319 symbol_t *symbol = declaration->symbol;
321 fputs(symbol->string, out);
323 print_compound_definition(declaration);
328 * Prints the prefix part of a typedef type.
330 * @param type The typedef type.
332 static void print_typedef_type_pre(const typedef_type_t *const type)
334 print_type_qualifiers(type->type.qualifiers);
335 fputs(type->declaration->symbol->string, out);
339 * Prints the prefix part of a typeof type.
341 * @param type The typeof type.
343 static void print_typeof_type_pre(const typeof_type_t *const type)
345 fputs("typeof(", out);
346 if(type->expression != NULL) {
347 assert(type->typeof_type == NULL);
348 print_expression(type->expression);
350 print_type(type->typeof_type);
356 * Prints the prefix part of a type.
358 * @param type The type.
359 * @param top true if we print the toplevel type, false else.
361 static void intern_print_type_pre(const type_t *const type, const bool top)
365 fputs("<error>", out);
367 fputs("<invalid>", out);
370 print_type_enum(&type->enumt);
373 print_atomic_type(&type->atomic);
375 case TYPE_COMPOUND_STRUCT:
376 case TYPE_COMPOUND_UNION:
377 print_compound_type(&type->compound);
380 fputs(type->builtin.symbol->string, out);
383 print_function_type_pre(&type->function, top);
386 print_pointer_type_pre(&type->pointer);
389 intern_print_type_pre(type->bitfield.base, top);
392 print_array_type_pre(&type->array);
395 print_typedef_type_pre(&type->typedeft);
398 print_typeof_type_pre(&type->typeoft);
401 fputs("unknown", out);
405 * Prints the postfix part of a type.
407 * @param type The type.
408 * @param top true if we print the toplevel type, false else.
410 static void intern_print_type_post(const type_t *const type, const bool top)
414 print_function_type_post(&type->function, NULL, top);
417 print_pointer_type_post(&type->pointer);
420 print_array_type_post(&type->array);
423 print_bitfield_type_post(&type->bitfield);
429 case TYPE_COMPOUND_STRUCT:
430 case TYPE_COMPOUND_UNION:
441 * @param type The type.
443 void print_type(const type_t *const type)
445 print_type_ext(type, NULL, NULL);
448 void print_type_ext(const type_t *const type, const symbol_t *symbol,
449 const scope_t *scope)
452 fputs("nil type", out);
456 intern_print_type_pre(type, true);
459 fputs(symbol->string, out);
461 if(type->kind == TYPE_FUNCTION) {
462 print_function_type_post(&type->function, scope, true);
464 intern_print_type_post(type, true);
469 * Return the size of a type AST node.
471 * @param type The type.
473 static size_t get_type_size(const type_t *type)
476 case TYPE_ATOMIC: return sizeof(atomic_type_t);
477 case TYPE_COMPOUND_STRUCT:
478 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
479 case TYPE_ENUM: return sizeof(enum_type_t);
480 case TYPE_FUNCTION: return sizeof(function_type_t);
481 case TYPE_POINTER: return sizeof(pointer_type_t);
482 case TYPE_ARRAY: return sizeof(array_type_t);
483 case TYPE_BUILTIN: return sizeof(builtin_type_t);
484 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
485 case TYPE_TYPEOF: return sizeof(typeof_type_t);
486 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
487 case TYPE_ERROR: panic("error type found");
488 case TYPE_INVALID: panic("invalid type found");
490 panic("unknown type found");
496 * @param type The type to copy.
497 * @return A copy of the type.
499 * @note This does not produce a deep copy!
501 type_t *duplicate_type(const type_t *type)
503 size_t size = get_type_size(type);
505 type_t *copy = obstack_alloc(type_obst, size);
506 memcpy(copy, type, size);
512 * Returns the unqualified type of a given type.
514 * @param type The type.
515 * @returns The unqualified type.
517 type_t *get_unqualified_type(type_t *type)
519 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
522 type_t *unqualified_type = duplicate_type(type);
523 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
525 type_t *result = typehash_insert(unqualified_type);
526 if(result != unqualified_type) {
527 obstack_free(type_obst, unqualified_type);
534 * Check if a type is valid.
536 * @param type The type to check.
537 * @return true if type represents a valid type.
539 bool type_valid(const type_t *type)
541 return type->kind != TYPE_INVALID;
545 * Returns true if the given type is an integer type.
547 * @param type The type to check.
548 * @return True if type is an integer type.
550 bool is_type_integer(const type_t *type)
552 assert(!is_typeref(type));
554 if(type->kind == TYPE_ENUM)
557 if(type->kind != TYPE_ATOMIC)
560 switch(type->atomic.akind) {
561 case ATOMIC_TYPE_BOOL:
562 case ATOMIC_TYPE_CHAR:
563 case ATOMIC_TYPE_SCHAR:
564 case ATOMIC_TYPE_UCHAR:
565 case ATOMIC_TYPE_SHORT:
566 case ATOMIC_TYPE_USHORT:
567 case ATOMIC_TYPE_INT:
568 case ATOMIC_TYPE_UINT:
569 case ATOMIC_TYPE_LONG:
570 case ATOMIC_TYPE_ULONG:
571 case ATOMIC_TYPE_LONGLONG:
572 case ATOMIC_TYPE_ULONGLONG:
580 * Returns true if the given type is an floating point type.
582 * @param type The type to check.
583 * @return True if type is a floating point type.
585 bool is_type_float(const type_t *type)
587 assert(!is_typeref(type));
589 if(type->kind != TYPE_ATOMIC)
592 switch(type->atomic.akind) {
593 case ATOMIC_TYPE_FLOAT:
594 case ATOMIC_TYPE_DOUBLE:
595 case ATOMIC_TYPE_LONG_DOUBLE:
596 #ifdef PROVIDE_COMPLEX
597 case ATOMIC_TYPE_FLOAT_COMPLEX:
598 case ATOMIC_TYPE_DOUBLE_COMPLEX:
599 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
600 case ATOMIC_TYPE_FLOAT_IMAGINARY:
601 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
602 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
611 * Returns true if the given type is a signed type.
613 * @param type The type to check.
614 * @return True if type is a signed type.
616 bool is_type_signed(const type_t *type)
618 assert(!is_typeref(type));
620 /* enum types are int for now */
621 if(type->kind == TYPE_ENUM)
624 if(type->kind != TYPE_ATOMIC)
627 switch(type->atomic.akind) {
628 case ATOMIC_TYPE_CHAR:
629 case ATOMIC_TYPE_SCHAR:
630 case ATOMIC_TYPE_SHORT:
631 case ATOMIC_TYPE_INT:
632 case ATOMIC_TYPE_LONG:
633 case ATOMIC_TYPE_LONGLONG:
634 case ATOMIC_TYPE_FLOAT:
635 case ATOMIC_TYPE_DOUBLE:
636 case ATOMIC_TYPE_LONG_DOUBLE:
637 #ifdef PROVIDE_COMPLEX
638 case ATOMIC_TYPE_FLOAT_COMPLEX:
639 case ATOMIC_TYPE_DOUBLE_COMPLEX:
640 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
641 case ATOMIC_TYPE_FLOAT_IMAGINARY:
642 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
643 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
647 case ATOMIC_TYPE_BOOL:
648 case ATOMIC_TYPE_UCHAR:
649 case ATOMIC_TYPE_USHORT:
650 case ATOMIC_TYPE_UINT:
651 case ATOMIC_TYPE_ULONG:
652 case ATOMIC_TYPE_ULONGLONG:
655 case ATOMIC_TYPE_VOID:
656 case ATOMIC_TYPE_INVALID:
657 case ATOMIC_TYPE_LAST:
661 panic("invalid atomic type found");
666 * Returns true if the given type represents an arithmetic type.
668 * @param type The type to check.
669 * @return True if type represents an arithmetic type.
671 bool is_type_arithmetic(const type_t *type)
673 assert(!is_typeref(type));
675 if(type->kind == TYPE_BITFIELD)
678 if(is_type_integer(type) || is_type_float(type))
685 * Returns true if the given type represents a scalar type.
687 * @param type The type to check.
688 * @return True if type represents a scalar type.
690 bool is_type_scalar(const type_t *type)
692 assert(!is_typeref(type));
694 switch (type->kind) {
695 case TYPE_POINTER: return true;
696 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
700 return is_type_arithmetic(type);
704 * Check if a given type is incomplete.
706 * @param type The type to check.
707 * @return True if the given type is incomplete (ie. just forward).
709 bool is_type_incomplete(const type_t *type)
711 assert(!is_typeref(type));
714 case TYPE_COMPOUND_STRUCT:
715 case TYPE_COMPOUND_UNION: {
716 const compound_type_t *compound_type = &type->compound;
717 declaration_t *declaration = compound_type->declaration;
718 return !declaration->init.is_defined;
721 const enum_type_t *enum_type = &type->enumt;
722 declaration_t *declaration = enum_type->declaration;
723 return !declaration->init.is_defined;
730 return type->array.size_expression == NULL;
733 return type->atomic.akind == ATOMIC_TYPE_VOID;
742 panic("is_type_incomplete called without typerefs skipped");
747 panic("invalid type found");
751 * Check if two function types are compatible.
753 static bool function_types_compatible(const function_type_t *func1,
754 const function_type_t *func2)
756 const type_t* const ret1 = skip_typeref(func1->return_type);
757 const type_t* const ret2 = skip_typeref(func2->return_type);
758 if (!types_compatible(ret1, ret2))
761 /* can parameters be compared? */
762 if(func1->unspecified_parameters || func2->unspecified_parameters)
765 if(func1->variadic != func2->variadic)
768 /* TODO: handling of unspecified parameters not correct yet */
770 /* all argument types must be compatible */
771 function_parameter_t *parameter1 = func1->parameters;
772 function_parameter_t *parameter2 = func2->parameters;
773 for( ; parameter1 != NULL && parameter2 != NULL;
774 parameter1 = parameter1->next, parameter2 = parameter2->next) {
775 type_t *parameter1_type = skip_typeref(parameter1->type);
776 type_t *parameter2_type = skip_typeref(parameter2->type);
778 parameter1_type = get_unqualified_type(parameter1_type);
779 parameter2_type = get_unqualified_type(parameter2_type);
781 if(!types_compatible(parameter1_type, parameter2_type))
784 /* same number of arguments? */
785 if(parameter1 != NULL || parameter2 != NULL)
792 * Check if two array types are compatible.
794 static bool array_types_compatible(const array_type_t *array1,
795 const array_type_t *array2)
797 type_t *element_type1 = skip_typeref(array1->element_type);
798 type_t *element_type2 = skip_typeref(array2->element_type);
799 if(!types_compatible(element_type1, element_type2))
802 if(!array1->size_constant || !array2->size_constant)
805 return array1->size == array2->size;
809 * Check if two types are compatible.
811 bool types_compatible(const type_t *type1, const type_t *type2)
813 assert(!is_typeref(type1));
814 assert(!is_typeref(type2));
816 /* shortcut: the same type is always compatible */
820 if(type1->base.qualifiers != type2->base.qualifiers)
822 if(type1->kind != type2->kind)
825 switch(type1->kind) {
827 return function_types_compatible(&type1->function, &type2->function);
829 return type1->atomic.akind == type2->atomic.akind;
831 return array_types_compatible(&type1->array, &type2->array);
834 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
835 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
836 return types_compatible(to1, to2);
839 case TYPE_COMPOUND_STRUCT:
840 case TYPE_COMPOUND_UNION:
843 /* TODO: not implemented */
847 /* not sure if this makes sense or is even needed, implement it if you
849 panic("type compatibility check for bitfield type");
852 /* Hmm, the error type should be compatible to all other types */
855 panic("invalid type found in compatible types");
858 panic("typerefs not skipped in compatible types?!?");
861 /* TODO: incomplete */
866 * Check if two pointer types are compatible.
868 bool pointers_compatible(const type_t *type1, const type_t *type2)
870 assert(!is_typeref(type1));
871 assert(!is_typeref(type2));
873 assert(type1->kind == TYPE_POINTER);
874 assert(type2->kind == TYPE_POINTER);
882 * Skip all typerefs and return the underlying type.
884 type_t *skip_typeref(type_t *type)
886 unsigned qualifiers = TYPE_QUALIFIER_NONE;
893 qualifiers |= type->base.qualifiers;
894 const typedef_type_t *typedef_type = &type->typedeft;
895 if(typedef_type->resolved_type != NULL) {
896 type = typedef_type->resolved_type;
899 type = typedef_type->declaration->type;
903 const typeof_type_t *typeof_type = &type->typeoft;
904 if(typeof_type->typeof_type != NULL) {
905 type = typeof_type->typeof_type;
907 type = typeof_type->expression->base.type;
917 if (qualifiers != TYPE_QUALIFIER_NONE) {
918 type_t *const copy = duplicate_type(type);
919 copy->base.qualifiers |= qualifiers;
921 type = typehash_insert(copy);
923 obstack_free(type_obst, copy);
931 * Hash the given type and return the "singleton" version
934 static type_t *identify_new_type(type_t *type)
936 type_t *result = typehash_insert(type);
938 obstack_free(type_obst, type);
944 * Creates a new atomic type.
946 * @param akind The kind of the atomic type.
947 * @param qualifiers Type qualifiers for the new type.
949 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
951 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
952 memset(type, 0, sizeof(atomic_type_t));
954 type->kind = TYPE_ATOMIC;
955 type->base.qualifiers = qualifiers;
956 type->atomic.akind = atype;
958 return identify_new_type(type);
962 * Creates a new pointer type.
964 * @param points_to The points-to type for teh new type.
965 * @param qualifiers Type qualifiers for the new type.
967 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
969 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
970 memset(type, 0, sizeof(pointer_type_t));
972 type->kind = TYPE_POINTER;
973 type->base.qualifiers = qualifiers;
974 type->pointer.points_to = points_to;
976 return identify_new_type(type);
979 type_t *make_array_type(type_t *element_type, size_t size,
980 type_qualifiers_t qualifiers)
982 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
983 memset(type, 0, sizeof(array_type_t));
985 type->kind = TYPE_ARRAY;
986 type->base.qualifiers = qualifiers;
987 type->array.element_type = element_type;
988 type->array.size = size;
989 type->array.size_constant = true;
991 return identify_new_type(type);
995 * Debug helper. Prints the given type to stdout.
997 static __attribute__((unused))
998 void dbg_type(const type_t *type)
1000 FILE *old_out = out;