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 = true;
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) {
253 print_expression(entry->init.enum_value);
264 * Prints an enum type.
266 * @param type The enum type.
268 static void print_type_enum(const enum_type_t *type)
270 print_type_qualifiers(type->type.qualifiers);
273 declaration_t *declaration = type->declaration;
274 symbol_t *symbol = declaration->symbol;
276 fputs(symbol->string, out);
278 print_enum_definition(declaration);
283 * Print the compound part of a compound type.
285 * @param declaration The declaration of the compound type.
287 void print_compound_definition(const declaration_t *declaration)
292 declaration_t *iter = declaration->scope.declarations;
293 for( ; iter != NULL; iter = iter->next) {
295 print_declaration(iter);
305 * Prints a compound type.
307 * @param type The compound type.
309 static void print_compound_type(const compound_type_t *type)
311 print_type_qualifiers(type->type.qualifiers);
313 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
314 fputs("struct ", out);
316 assert(type->type.kind == TYPE_COMPOUND_UNION);
317 fputs("union ", out);
320 declaration_t *declaration = type->declaration;
321 symbol_t *symbol = declaration->symbol;
323 fputs(symbol->string, out);
325 print_compound_definition(declaration);
330 * Prints the prefix part of a typedef type.
332 * @param type The typedef type.
334 static void print_typedef_type_pre(const typedef_type_t *const type)
336 print_type_qualifiers(type->type.qualifiers);
337 fputs(type->declaration->symbol->string, out);
341 * Prints the prefix part of a typeof type.
343 * @param type The typeof type.
345 static void print_typeof_type_pre(const typeof_type_t *const type)
347 fputs("typeof(", out);
348 if(type->expression != NULL) {
349 assert(type->typeof_type == NULL);
350 print_expression(type->expression);
352 print_type(type->typeof_type);
358 * Prints the prefix part of a type.
360 * @param type The type.
361 * @param top true if we print the toplevel type, false else.
363 static void intern_print_type_pre(const type_t *const type, const bool top)
367 fputs("<error>", out);
369 fputs("<invalid>", out);
372 print_type_enum(&type->enumt);
375 print_atomic_type(&type->atomic);
377 case TYPE_COMPOUND_STRUCT:
378 case TYPE_COMPOUND_UNION:
379 print_compound_type(&type->compound);
382 fputs(type->builtin.symbol->string, out);
385 print_function_type_pre(&type->function, top);
388 print_pointer_type_pre(&type->pointer);
391 intern_print_type_pre(type->bitfield.base, top);
394 print_array_type_pre(&type->array);
397 print_typedef_type_pre(&type->typedeft);
400 print_typeof_type_pre(&type->typeoft);
403 fputs("unknown", out);
407 * Prints the postfix part of a type.
409 * @param type The type.
410 * @param top true if we print the toplevel type, false else.
412 static void intern_print_type_post(const type_t *const type, const bool top)
416 print_function_type_post(&type->function, NULL, top);
419 print_pointer_type_post(&type->pointer);
422 print_array_type_post(&type->array);
425 print_bitfield_type_post(&type->bitfield);
431 case TYPE_COMPOUND_STRUCT:
432 case TYPE_COMPOUND_UNION:
443 * @param type The type.
445 void print_type(const type_t *const type)
447 print_type_ext(type, NULL, NULL);
450 void print_type_ext(const type_t *const type, const symbol_t *symbol,
451 const scope_t *scope)
454 fputs("nil type", out);
458 intern_print_type_pre(type, true);
461 fputs(symbol->string, out);
463 if(type->kind == TYPE_FUNCTION) {
464 print_function_type_post(&type->function, scope, true);
466 intern_print_type_post(type, true);
471 * Return the size of a type AST node.
473 * @param type The type.
475 static size_t get_type_size(const type_t *type)
478 case TYPE_ATOMIC: return sizeof(atomic_type_t);
479 case TYPE_COMPOUND_STRUCT:
480 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
481 case TYPE_ENUM: return sizeof(enum_type_t);
482 case TYPE_FUNCTION: return sizeof(function_type_t);
483 case TYPE_POINTER: return sizeof(pointer_type_t);
484 case TYPE_ARRAY: return sizeof(array_type_t);
485 case TYPE_BUILTIN: return sizeof(builtin_type_t);
486 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
487 case TYPE_TYPEOF: return sizeof(typeof_type_t);
488 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
489 case TYPE_ERROR: panic("error type found");
490 case TYPE_INVALID: panic("invalid type found");
492 panic("unknown type found");
498 * @param type The type to copy.
499 * @return A copy of the type.
501 * @note This does not produce a deep copy!
503 type_t *duplicate_type(const type_t *type)
505 size_t size = get_type_size(type);
507 type_t *copy = obstack_alloc(type_obst, size);
508 memcpy(copy, type, size);
514 * Returns the unqualified type of a given type.
516 * @param type The type.
517 * @returns The unqualified type.
519 type_t *get_unqualified_type(type_t *type)
521 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
524 type_t *unqualified_type = duplicate_type(type);
525 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
527 type_t *result = typehash_insert(unqualified_type);
528 if(result != unqualified_type) {
529 obstack_free(type_obst, unqualified_type);
536 * Check if a type is valid.
538 * @param type The type to check.
539 * @return true if type represents a valid type.
541 bool type_valid(const type_t *type)
543 return type->kind != TYPE_INVALID;
547 * Returns true if the given type is an integer type.
549 * @param type The type to check.
550 * @return True if type is an integer type.
552 bool is_type_integer(const type_t *type)
554 assert(!is_typeref(type));
556 if(type->kind == TYPE_ENUM)
559 if(type->kind != TYPE_ATOMIC)
562 switch(type->atomic.akind) {
563 case ATOMIC_TYPE_BOOL:
564 case ATOMIC_TYPE_CHAR:
565 case ATOMIC_TYPE_SCHAR:
566 case ATOMIC_TYPE_UCHAR:
567 case ATOMIC_TYPE_SHORT:
568 case ATOMIC_TYPE_USHORT:
569 case ATOMIC_TYPE_INT:
570 case ATOMIC_TYPE_UINT:
571 case ATOMIC_TYPE_LONG:
572 case ATOMIC_TYPE_ULONG:
573 case ATOMIC_TYPE_LONGLONG:
574 case ATOMIC_TYPE_ULONGLONG:
582 * Returns true if the given type is an floating point type.
584 * @param type The type to check.
585 * @return True if type is a floating point type.
587 bool is_type_float(const type_t *type)
589 assert(!is_typeref(type));
591 if(type->kind != TYPE_ATOMIC)
594 switch(type->atomic.akind) {
595 case ATOMIC_TYPE_FLOAT:
596 case ATOMIC_TYPE_DOUBLE:
597 case ATOMIC_TYPE_LONG_DOUBLE:
598 #ifdef PROVIDE_COMPLEX
599 case ATOMIC_TYPE_FLOAT_COMPLEX:
600 case ATOMIC_TYPE_DOUBLE_COMPLEX:
601 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
602 case ATOMIC_TYPE_FLOAT_IMAGINARY:
603 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
604 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
613 * Returns true if the given type is a signed type.
615 * @param type The type to check.
616 * @return True if type is a signed type.
618 bool is_type_signed(const type_t *type)
620 assert(!is_typeref(type));
622 /* enum types are int for now */
623 if(type->kind == TYPE_ENUM)
626 if(type->kind != TYPE_ATOMIC)
629 switch(type->atomic.akind) {
630 case ATOMIC_TYPE_CHAR:
631 case ATOMIC_TYPE_SCHAR:
632 case ATOMIC_TYPE_SHORT:
633 case ATOMIC_TYPE_INT:
634 case ATOMIC_TYPE_LONG:
635 case ATOMIC_TYPE_LONGLONG:
636 case ATOMIC_TYPE_FLOAT:
637 case ATOMIC_TYPE_DOUBLE:
638 case ATOMIC_TYPE_LONG_DOUBLE:
639 #ifdef PROVIDE_COMPLEX
640 case ATOMIC_TYPE_FLOAT_COMPLEX:
641 case ATOMIC_TYPE_DOUBLE_COMPLEX:
642 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
643 case ATOMIC_TYPE_FLOAT_IMAGINARY:
644 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
645 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
649 case ATOMIC_TYPE_BOOL:
650 case ATOMIC_TYPE_UCHAR:
651 case ATOMIC_TYPE_USHORT:
652 case ATOMIC_TYPE_UINT:
653 case ATOMIC_TYPE_ULONG:
654 case ATOMIC_TYPE_ULONGLONG:
657 case ATOMIC_TYPE_VOID:
658 case ATOMIC_TYPE_INVALID:
659 case ATOMIC_TYPE_LAST:
663 panic("invalid atomic type found");
668 * Returns true if the given type represents an arithmetic type.
670 * @param type The type to check.
671 * @return True if type represents an arithmetic type.
673 bool is_type_arithmetic(const type_t *type)
675 assert(!is_typeref(type));
677 if(type->kind == TYPE_BITFIELD)
680 if(is_type_integer(type) || is_type_float(type))
687 * Returns true if the given type represents a scalar type.
689 * @param type The type to check.
690 * @return True if type represents a scalar type.
692 bool is_type_scalar(const type_t *type)
694 assert(!is_typeref(type));
696 switch (type->kind) {
697 case TYPE_POINTER: return true;
698 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
702 return is_type_arithmetic(type);
706 * Check if a given type is incomplete.
708 * @param type The type to check.
709 * @return True if the given type is incomplete (ie. just forward).
711 bool is_type_incomplete(const type_t *type)
713 assert(!is_typeref(type));
716 case TYPE_COMPOUND_STRUCT:
717 case TYPE_COMPOUND_UNION: {
718 const compound_type_t *compound_type = &type->compound;
719 declaration_t *declaration = compound_type->declaration;
720 return !declaration->init.is_defined;
723 const enum_type_t *enum_type = &type->enumt;
724 declaration_t *declaration = enum_type->declaration;
725 return !declaration->init.is_defined;
732 return type->array.size_expression == NULL;
735 return type->atomic.akind == ATOMIC_TYPE_VOID;
744 panic("is_type_incomplete called without typerefs skipped");
749 panic("invalid type found");
753 * Check if two function types are compatible.
755 static bool function_types_compatible(const function_type_t *func1,
756 const function_type_t *func2)
758 const type_t* const ret1 = skip_typeref(func1->return_type);
759 const type_t* const ret2 = skip_typeref(func2->return_type);
760 if (!types_compatible(ret1, ret2))
763 /* can parameters be compared? */
764 if(func1->unspecified_parameters || func2->unspecified_parameters)
767 if(func1->variadic != func2->variadic)
770 /* TODO: handling of unspecified parameters not correct yet */
772 /* all argument types must be compatible */
773 function_parameter_t *parameter1 = func1->parameters;
774 function_parameter_t *parameter2 = func2->parameters;
775 for( ; parameter1 != NULL && parameter2 != NULL;
776 parameter1 = parameter1->next, parameter2 = parameter2->next) {
777 type_t *parameter1_type = skip_typeref(parameter1->type);
778 type_t *parameter2_type = skip_typeref(parameter2->type);
780 parameter1_type = get_unqualified_type(parameter1_type);
781 parameter2_type = get_unqualified_type(parameter2_type);
783 if(!types_compatible(parameter1_type, parameter2_type))
786 /* same number of arguments? */
787 if(parameter1 != NULL || parameter2 != NULL)
794 * Check if two array types are compatible.
796 static bool array_types_compatible(const array_type_t *array1,
797 const array_type_t *array2)
799 type_t *element_type1 = skip_typeref(array1->element_type);
800 type_t *element_type2 = skip_typeref(array2->element_type);
801 if(!types_compatible(element_type1, element_type2))
804 if(!array1->size_constant || !array2->size_constant)
807 return array1->size == array2->size;
811 * Check if two types are compatible.
813 bool types_compatible(const type_t *type1, const type_t *type2)
815 assert(!is_typeref(type1));
816 assert(!is_typeref(type2));
818 /* shortcut: the same type is always compatible */
822 if(type1->base.qualifiers != type2->base.qualifiers)
824 if(type1->kind != type2->kind)
827 switch(type1->kind) {
829 return function_types_compatible(&type1->function, &type2->function);
831 return type1->atomic.akind == type2->atomic.akind;
833 return array_types_compatible(&type1->array, &type2->array);
836 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
837 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
838 return types_compatible(to1, to2);
841 case TYPE_COMPOUND_STRUCT:
842 case TYPE_COMPOUND_UNION:
845 /* TODO: not implemented */
849 /* not sure if this makes sense or is even needed, implement it if you
851 panic("type compatibility check for bitfield type");
854 /* Hmm, the error type should be compatible to all other types */
857 panic("invalid type found in compatible types");
860 panic("typerefs not skipped in compatible types?!?");
863 /* TODO: incomplete */
868 * Check if two pointer types are compatible.
870 bool pointers_compatible(const type_t *type1, const type_t *type2)
872 assert(!is_typeref(type1));
873 assert(!is_typeref(type2));
875 assert(type1->kind == TYPE_POINTER);
876 assert(type2->kind == TYPE_POINTER);
884 * Skip all typerefs and return the underlying type.
886 type_t *skip_typeref(type_t *type)
888 unsigned qualifiers = TYPE_QUALIFIER_NONE;
895 qualifiers |= type->base.qualifiers;
896 const typedef_type_t *typedef_type = &type->typedeft;
897 if(typedef_type->resolved_type != NULL) {
898 type = typedef_type->resolved_type;
901 type = typedef_type->declaration->type;
905 const typeof_type_t *typeof_type = &type->typeoft;
906 if(typeof_type->typeof_type != NULL) {
907 type = typeof_type->typeof_type;
909 type = typeof_type->expression->base.type;
919 if (qualifiers != TYPE_QUALIFIER_NONE) {
920 type_t *const copy = duplicate_type(type);
921 copy->base.qualifiers |= qualifiers;
923 type = typehash_insert(copy);
925 obstack_free(type_obst, copy);
933 * Hash the given type and return the "singleton" version
936 static type_t *identify_new_type(type_t *type)
938 type_t *result = typehash_insert(type);
940 obstack_free(type_obst, type);
946 * Creates a new atomic type.
948 * @param akind The kind of the atomic type.
949 * @param qualifiers Type qualifiers for the new type.
951 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
953 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
954 memset(type, 0, sizeof(atomic_type_t));
956 type->kind = TYPE_ATOMIC;
957 type->base.qualifiers = qualifiers;
958 type->base.alignment = 0;
959 type->atomic.akind = atype;
961 /* TODO: set the aligmnent depending on the atype here */
963 return identify_new_type(type);
967 * Creates a new pointer type.
969 * @param points_to The points-to type for teh new type.
970 * @param qualifiers Type qualifiers for the new type.
972 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
974 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
975 memset(type, 0, sizeof(pointer_type_t));
977 type->kind = TYPE_POINTER;
978 type->base.qualifiers = qualifiers;
979 type->base.alignment = 0;
980 type->pointer.points_to = points_to;
982 return identify_new_type(type);
985 type_t *make_array_type(type_t *element_type, size_t size,
986 type_qualifiers_t qualifiers)
988 type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
989 memset(type, 0, sizeof(array_type_t));
991 type->kind = TYPE_ARRAY;
992 type->base.qualifiers = qualifiers;
993 type->base.alignment = 0;
994 type->array.element_type = element_type;
995 type->array.size = size;
996 type->array.size_constant = true;
998 return identify_new_type(type);
1002 * Debug helper. Prints the given type to stdout.
1004 static __attribute__((unused))
1005 void dbg_type(const type_t *type)
1007 FILE *old_out = out;