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"
30 static struct obstack _type_obst;
32 struct obstack *type_obst = &_type_obst;
33 static int type_visited = 0;
34 static bool print_implicit_array_size = false;
36 static void intern_print_type_pre(const type_t *type, bool top);
37 static void intern_print_type_post(const type_t *type, bool top);
41 obstack_init(type_obst);
46 obstack_free(type_obst, NULL);
49 void type_set_output(FILE *stream)
54 void inc_type_visited(void)
59 void print_type_qualifiers(type_qualifiers_t qualifiers)
61 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
62 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
63 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
67 * Prints the name of a atomic type.
69 * @param type The type.
72 void print_atomic_type(const atomic_type_t *type)
74 print_type_qualifiers(type->type.qualifiers);
78 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
79 case ATOMIC_TYPE_VOID: s = "void"; break;
80 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
81 case ATOMIC_TYPE_CHAR: s = "char"; break;
82 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
83 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
84 case ATOMIC_TYPE_INT: s = "int"; break;
85 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
86 case ATOMIC_TYPE_SHORT: s = "short"; break;
87 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
88 case ATOMIC_TYPE_LONG: s = "long"; break;
89 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
90 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
91 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
92 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
93 case ATOMIC_TYPE_FLOAT: s = "float"; break;
94 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
95 default: s = "UNKNOWNATOMIC"; break;
101 * Print the first part (the prefix) of a type.
103 * @param type The type to print.
104 * @param top true, if this is the top type, false if it's an embedded type.
106 static void print_function_type_pre(const function_type_t *type, bool top)
108 print_type_qualifiers(type->type.qualifiers);
110 intern_print_type_pre(type->return_type, false);
112 /* don't emit braces if we're the toplevel type... */
118 * Print the second part (the postfix) of a type.
120 * @param type The type to print.
121 * @param top true, if this is the top type, false if it's an embedded type.
123 static void print_function_type_post(const function_type_t *type,
124 const scope_t *scope, bool top)
126 intern_print_type_post(type->return_type, false);
127 /* don't emit braces if we're the toplevel type... */
135 function_parameter_t *parameter = type->parameters;
136 for( ; parameter != NULL; parameter = parameter->next) {
142 print_type(parameter->type);
145 declaration_t *parameter = scope->declarations;
146 for( ; parameter != NULL; parameter = parameter->next) {
152 print_type_ext(parameter->type, parameter->symbol,
164 if(first && !type->unspecified_parameters) {
171 * Prints the prefix part of a pointer type.
173 * @param type The pointer type.
175 static void print_pointer_type_pre(const pointer_type_t *type)
177 intern_print_type_pre(type->points_to, false);
179 print_type_qualifiers(type->type.qualifiers);
183 * Prints the postfix part of a pointer type.
185 * @param type The pointer type.
187 static void print_pointer_type_post(const pointer_type_t *type)
189 intern_print_type_post(type->points_to, false);
193 * Prints the prefix part of an array type.
195 * @param type The array type.
197 static void print_array_type_pre(const array_type_t *type)
199 intern_print_type_pre(type->element_type, false);
203 * Prints the postfix part of an array type.
205 * @param type The array type.
207 static void print_array_type_post(const array_type_t *type)
210 if(type->is_static) {
211 fputs("static ", out);
213 print_type_qualifiers(type->type.qualifiers);
214 if(type->size_expression != NULL
215 && (print_implicit_array_size || !type->has_implicit_size)) {
216 print_expression(type->size_expression);
219 intern_print_type_post(type->element_type, false);
223 * Prints the postfix part of a bitfield type.
225 * @param type The array type.
227 static void print_bitfield_type_post(const bitfield_type_t *type)
230 print_expression(type->size);
231 intern_print_type_post(type->base, false);
235 * Prints an enum definition.
237 * @param declaration The enum's type declaration.
239 void print_enum_definition(const declaration_t *declaration)
245 declaration_t *entry = declaration->next;
246 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
247 entry = entry->next) {
250 fprintf(out, "%s", entry->symbol->string);
251 if(entry->init.initializer != NULL) {
254 /* skip the implicit cast */
255 expression_t *expression = entry->init.enum_value;
256 if(expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
257 expression = expression->unary.value;
259 print_expression(expression);
270 * Prints an enum type.
272 * @param type The enum type.
274 static void print_type_enum(const enum_type_t *type)
276 print_type_qualifiers(type->type.qualifiers);
279 declaration_t *declaration = type->declaration;
280 symbol_t *symbol = declaration->symbol;
282 fputs(symbol->string, out);
284 print_enum_definition(declaration);
289 * Print the compound part of a compound type.
291 * @param declaration The declaration of the compound type.
293 void print_compound_definition(const declaration_t *declaration)
298 declaration_t *iter = declaration->scope.declarations;
299 for( ; iter != NULL; iter = iter->next) {
301 print_declaration(iter);
311 * Prints a compound type.
313 * @param type The compound type.
315 static void print_compound_type(const compound_type_t *type)
317 print_type_qualifiers(type->type.qualifiers);
319 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
320 fputs("struct ", out);
322 assert(type->type.kind == TYPE_COMPOUND_UNION);
323 fputs("union ", out);
326 declaration_t *declaration = type->declaration;
327 symbol_t *symbol = declaration->symbol;
329 fputs(symbol->string, out);
331 print_compound_definition(declaration);
336 * Prints the prefix part of a typedef type.
338 * @param type The typedef type.
340 static void print_typedef_type_pre(const typedef_type_t *const type)
342 print_type_qualifiers(type->type.qualifiers);
343 fputs(type->declaration->symbol->string, out);
347 * Prints the prefix part of a typeof type.
349 * @param type The typeof type.
351 static void print_typeof_type_pre(const typeof_type_t *const type)
353 fputs("typeof(", out);
354 if(type->expression != NULL) {
355 assert(type->typeof_type == NULL);
356 print_expression(type->expression);
358 print_type(type->typeof_type);
364 * Prints the prefix part of a type.
366 * @param type The type.
367 * @param top true if we print the toplevel type, false else.
369 static void intern_print_type_pre(const type_t *const type, const bool top)
373 fputs("<error>", out);
375 fputs("<invalid>", out);
378 print_type_enum(&type->enumt);
381 print_atomic_type(&type->atomic);
383 case TYPE_COMPOUND_STRUCT:
384 case TYPE_COMPOUND_UNION:
385 print_compound_type(&type->compound);
388 fputs(type->builtin.symbol->string, out);
391 print_function_type_pre(&type->function, top);
394 print_pointer_type_pre(&type->pointer);
397 intern_print_type_pre(type->bitfield.base, top);
400 print_array_type_pre(&type->array);
403 print_typedef_type_pre(&type->typedeft);
406 print_typeof_type_pre(&type->typeoft);
409 fputs("unknown", out);
413 * Prints the postfix part of a type.
415 * @param type The type.
416 * @param top true if we print the toplevel type, false else.
418 static void intern_print_type_post(const type_t *const type, const bool top)
422 print_function_type_post(&type->function, NULL, top);
425 print_pointer_type_post(&type->pointer);
428 print_array_type_post(&type->array);
431 print_bitfield_type_post(&type->bitfield);
437 case TYPE_COMPOUND_STRUCT:
438 case TYPE_COMPOUND_UNION:
449 * @param type The type.
451 void print_type(const type_t *const type)
453 print_type_ext(type, NULL, NULL);
456 void print_type_ext(const type_t *const type, const symbol_t *symbol,
457 const scope_t *scope)
460 fputs("nil type", out);
464 intern_print_type_pre(type, true);
467 fputs(symbol->string, out);
469 if(type->kind == TYPE_FUNCTION) {
470 print_function_type_post(&type->function, scope, true);
472 intern_print_type_post(type, true);
477 * Return the size of a type AST node.
479 * @param type The type.
481 static size_t get_type_size(const type_t *type)
484 case TYPE_ATOMIC: return sizeof(atomic_type_t);
485 case TYPE_COMPOUND_STRUCT:
486 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
487 case TYPE_ENUM: return sizeof(enum_type_t);
488 case TYPE_FUNCTION: return sizeof(function_type_t);
489 case TYPE_POINTER: return sizeof(pointer_type_t);
490 case TYPE_ARRAY: return sizeof(array_type_t);
491 case TYPE_BUILTIN: return sizeof(builtin_type_t);
492 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
493 case TYPE_TYPEOF: return sizeof(typeof_type_t);
494 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
495 case TYPE_ERROR: panic("error type found");
496 case TYPE_INVALID: panic("invalid type found");
498 panic("unknown type found");
504 * @param type The type to copy.
505 * @return A copy of the type.
507 * @note This does not produce a deep copy!
509 type_t *duplicate_type(const type_t *type)
511 size_t size = get_type_size(type);
513 type_t *copy = obstack_alloc(type_obst, size);
514 memcpy(copy, type, size);
520 * Returns the unqualified type of a given type.
522 * @param type The type.
523 * @returns The unqualified type.
525 type_t *get_unqualified_type(type_t *type)
527 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
530 type_t *unqualified_type = duplicate_type(type);
531 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
533 type_t *result = typehash_insert(unqualified_type);
534 if(result != unqualified_type) {
535 obstack_free(type_obst, unqualified_type);
542 * Check if a type is valid.
544 * @param type The type to check.
545 * @return true if type represents a valid type.
547 bool type_valid(const type_t *type)
549 return type->kind != TYPE_INVALID;
553 * Returns true if the given type is an integer type.
555 * @param type The type to check.
556 * @return True if type is an integer type.
558 bool is_type_integer(const type_t *type)
560 assert(!is_typeref(type));
562 if(type->kind == TYPE_ENUM)
565 if(type->kind != TYPE_ATOMIC)
568 switch(type->atomic.akind) {
569 case ATOMIC_TYPE_BOOL:
570 case ATOMIC_TYPE_CHAR:
571 case ATOMIC_TYPE_SCHAR:
572 case ATOMIC_TYPE_UCHAR:
573 case ATOMIC_TYPE_SHORT:
574 case ATOMIC_TYPE_USHORT:
575 case ATOMIC_TYPE_INT:
576 case ATOMIC_TYPE_UINT:
577 case ATOMIC_TYPE_LONG:
578 case ATOMIC_TYPE_ULONG:
579 case ATOMIC_TYPE_LONGLONG:
580 case ATOMIC_TYPE_ULONGLONG:
588 * Returns true if the given type is an floating point type.
590 * @param type The type to check.
591 * @return True if type is a floating point type.
593 bool is_type_float(const type_t *type)
595 assert(!is_typeref(type));
597 if(type->kind != TYPE_ATOMIC)
600 switch(type->atomic.akind) {
601 case ATOMIC_TYPE_FLOAT:
602 case ATOMIC_TYPE_DOUBLE:
603 case ATOMIC_TYPE_LONG_DOUBLE:
604 #ifdef PROVIDE_COMPLEX
605 case ATOMIC_TYPE_FLOAT_COMPLEX:
606 case ATOMIC_TYPE_DOUBLE_COMPLEX:
607 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
608 case ATOMIC_TYPE_FLOAT_IMAGINARY:
609 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
610 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
619 * Returns true if the given type is a signed type.
621 * @param type The type to check.
622 * @return True if type is a signed type.
624 bool is_type_signed(const type_t *type)
626 assert(!is_typeref(type));
628 /* enum types are int for now */
629 if(type->kind == TYPE_ENUM)
632 if(type->kind != TYPE_ATOMIC)
635 switch(type->atomic.akind) {
636 case ATOMIC_TYPE_CHAR:
637 case ATOMIC_TYPE_SCHAR:
638 case ATOMIC_TYPE_SHORT:
639 case ATOMIC_TYPE_INT:
640 case ATOMIC_TYPE_LONG:
641 case ATOMIC_TYPE_LONGLONG:
642 case ATOMIC_TYPE_FLOAT:
643 case ATOMIC_TYPE_DOUBLE:
644 case ATOMIC_TYPE_LONG_DOUBLE:
645 #ifdef PROVIDE_COMPLEX
646 case ATOMIC_TYPE_FLOAT_COMPLEX:
647 case ATOMIC_TYPE_DOUBLE_COMPLEX:
648 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
649 case ATOMIC_TYPE_FLOAT_IMAGINARY:
650 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
651 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
655 case ATOMIC_TYPE_BOOL:
656 case ATOMIC_TYPE_UCHAR:
657 case ATOMIC_TYPE_USHORT:
658 case ATOMIC_TYPE_UINT:
659 case ATOMIC_TYPE_ULONG:
660 case ATOMIC_TYPE_ULONGLONG:
663 case ATOMIC_TYPE_VOID:
664 case ATOMIC_TYPE_INVALID:
665 case ATOMIC_TYPE_LAST:
669 panic("invalid atomic type found");
674 * Returns true if the given type represents an arithmetic type.
676 * @param type The type to check.
677 * @return True if type represents an arithmetic type.
679 bool is_type_arithmetic(const type_t *type)
681 assert(!is_typeref(type));
683 if(type->kind == TYPE_BITFIELD)
686 if(is_type_integer(type) || is_type_float(type))
693 * Returns true if the given type represents a scalar type.
695 * @param type The type to check.
696 * @return True if type represents a scalar type.
698 bool is_type_scalar(const type_t *type)
700 assert(!is_typeref(type));
702 switch (type->kind) {
703 case TYPE_POINTER: return true;
704 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
708 return is_type_arithmetic(type);
712 * Check if a given type is incomplete.
714 * @param type The type to check.
715 * @return True if the given type is incomplete (ie. just forward).
717 bool is_type_incomplete(const type_t *type)
719 assert(!is_typeref(type));
722 case TYPE_COMPOUND_STRUCT:
723 case TYPE_COMPOUND_UNION: {
724 const compound_type_t *compound_type = &type->compound;
725 declaration_t *declaration = compound_type->declaration;
726 return !declaration->init.is_defined;
729 const enum_type_t *enum_type = &type->enumt;
730 declaration_t *declaration = enum_type->declaration;
731 return !declaration->init.is_defined;
738 return type->array.size_expression == NULL;
741 return type->atomic.akind == ATOMIC_TYPE_VOID;
750 panic("is_type_incomplete called without typerefs skipped");
755 panic("invalid type found");
759 * Check if two function types are compatible.
761 static bool function_types_compatible(const function_type_t *func1,
762 const function_type_t *func2)
764 const type_t* const ret1 = skip_typeref(func1->return_type);
765 const type_t* const ret2 = skip_typeref(func2->return_type);
766 if (!types_compatible(ret1, ret2))
769 /* can parameters be compared? */
770 if(func1->unspecified_parameters || func2->unspecified_parameters)
773 if(func1->variadic != func2->variadic)
776 /* TODO: handling of unspecified parameters not correct yet */
778 /* all argument types must be compatible */
779 function_parameter_t *parameter1 = func1->parameters;
780 function_parameter_t *parameter2 = func2->parameters;
781 for( ; parameter1 != NULL && parameter2 != NULL;
782 parameter1 = parameter1->next, parameter2 = parameter2->next) {
783 type_t *parameter1_type = skip_typeref(parameter1->type);
784 type_t *parameter2_type = skip_typeref(parameter2->type);
786 parameter1_type = get_unqualified_type(parameter1_type);
787 parameter2_type = get_unqualified_type(parameter2_type);
789 if(!types_compatible(parameter1_type, parameter2_type))
792 /* same number of arguments? */
793 if(parameter1 != NULL || parameter2 != NULL)
800 * Check if two array types are compatible.
802 static bool array_types_compatible(const array_type_t *array1,
803 const array_type_t *array2)
805 type_t *element_type1 = skip_typeref(array1->element_type);
806 type_t *element_type2 = skip_typeref(array2->element_type);
807 if(!types_compatible(element_type1, element_type2))
810 if(!array1->size_constant || !array2->size_constant)
813 return array1->size == array2->size;
817 * Check if two types are compatible.
819 bool types_compatible(const type_t *type1, const type_t *type2)
821 assert(!is_typeref(type1));
822 assert(!is_typeref(type2));
824 /* shortcut: the same type is always compatible */
828 if(type1->base.qualifiers != type2->base.qualifiers)
830 if(type1->kind != type2->kind)
833 switch(type1->kind) {
835 return function_types_compatible(&type1->function, &type2->function);
837 return type1->atomic.akind == type2->atomic.akind;
839 return array_types_compatible(&type1->array, &type2->array);
842 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
843 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
844 return types_compatible(to1, to2);
847 case TYPE_COMPOUND_STRUCT:
848 case TYPE_COMPOUND_UNION:
851 /* TODO: not implemented */
855 /* not sure if this makes sense or is even needed, implement it if you
857 panic("type compatibility check for bitfield type");
860 /* Hmm, the error type should be compatible to all other types */
863 panic("invalid type found in compatible types");
866 panic("typerefs not skipped in compatible types?!?");
869 /* TODO: incomplete */
874 * Check if two pointer types are compatible.
876 bool pointers_compatible(const type_t *type1, const type_t *type2)
878 assert(!is_typeref(type1));
879 assert(!is_typeref(type2));
881 assert(type1->kind == TYPE_POINTER);
882 assert(type2->kind == TYPE_POINTER);
890 * Skip all typerefs and return the underlying type.
892 type_t *skip_typeref(type_t *type)
894 unsigned qualifiers = TYPE_QUALIFIER_NONE;
901 qualifiers |= type->base.qualifiers;
902 const typedef_type_t *typedef_type = &type->typedeft;
903 if(typedef_type->resolved_type != NULL) {
904 type = typedef_type->resolved_type;
907 type = typedef_type->declaration->type;
911 const typeof_type_t *typeof_type = &type->typeoft;
912 if(typeof_type->typeof_type != NULL) {
913 type = typeof_type->typeof_type;
915 type = typeof_type->expression->base.type;
925 if (qualifiers != TYPE_QUALIFIER_NONE) {
926 type_t *const copy = duplicate_type(type);
927 copy->base.qualifiers |= qualifiers;
929 type = typehash_insert(copy);
931 obstack_free(type_obst, copy);
939 * Hash the given type and return the "singleton" version
942 static type_t *identify_new_type(type_t *type)
944 type_t *result = typehash_insert(type);
946 obstack_free(type_obst, type);
952 * Creates a new atomic type.
954 * @param akind The kind of the atomic type.
955 * @param qualifiers Type qualifiers for the new type.
957 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
959 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
960 memset(type, 0, sizeof(atomic_type_t));
962 type->kind = TYPE_ATOMIC;
963 type->base.qualifiers = qualifiers;
964 type->base.alignment = 0;
965 type->atomic.akind = atype;
967 /* TODO: set the aligmnent depending on the atype here */
969 return identify_new_type(type);
973 * Creates a new pointer type.
975 * @param points_to The points-to type for teh new type.
976 * @param qualifiers Type qualifiers for the new type.
978 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
980 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
981 memset(type, 0, sizeof(pointer_type_t));
983 type->kind = TYPE_POINTER;
984 type->base.qualifiers = qualifiers;
985 type->base.alignment = 0;
986 type->pointer.points_to = points_to;
988 return identify_new_type(type);
991 type_t *make_array_type(type_t *element_type, size_t size,
992 type_qualifiers_t qualifiers)
994 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
995 memset(type, 0, sizeof(array_type_t));
997 type->kind = TYPE_ARRAY;
998 type->base.qualifiers = qualifiers;
999 type->base.alignment = 0;
1000 type->array.element_type = element_type;
1001 type->array.size = size;
1002 type->array.size_constant = true;
1004 return identify_new_type(type);
1008 * Debug helper. Prints the given type to stdout.
1010 static __attribute__((unused))
1011 void dbg_type(const type_t *type)
1013 FILE *old_out = out;