9 static struct obstack _type_obst;
10 struct obstack *type_obst = &_type_obst;
12 static int type_visited = 0;
14 static void intern_print_type_pre(const type_t *type, bool top);
15 static void intern_print_type_post(const type_t *type, bool top);
19 obstack_init(type_obst);
24 obstack_free(type_obst, NULL);
27 void type_set_output(FILE *stream)
32 void inc_type_visited(void)
37 void print_type_qualifiers(type_qualifiers_t qualifiers)
39 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
40 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
41 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
45 * Prints the name of a atomic type.
47 * @param type The type.
50 void print_atomic_type(const atomic_type_t *type)
52 print_type_qualifiers(type->type.qualifiers);
56 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
57 case ATOMIC_TYPE_VOID: s = "void"; break;
58 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
59 case ATOMIC_TYPE_CHAR: s = "char"; break;
60 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
61 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
62 case ATOMIC_TYPE_INT: s = "int"; break;
63 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
64 case ATOMIC_TYPE_SHORT: s = "short"; break;
65 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
66 case ATOMIC_TYPE_LONG: s = "long"; break;
67 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
68 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
69 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
70 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
71 case ATOMIC_TYPE_FLOAT: s = "float"; break;
72 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
73 default: s = "UNKNOWNATOMIC"; break;
79 * Print the first part (the prefix) of a type.
81 * @param type The type to print.
82 * @param top true, if this is the top type, false if it's an embedded type.
84 static void print_function_type_pre(const function_type_t *type, bool top)
86 print_type_qualifiers(type->type.qualifiers);
88 intern_print_type_pre(type->return_type, false);
90 /* don't emit braces if we're the toplevel type... */
96 * Print the second part (the postfix) of a type.
98 * @param type The type to print.
99 * @param top true, if this is the top type, false if it's an embedded type.
101 static void print_function_type_post(const function_type_t *type,
102 const scope_t *scope, bool top)
104 intern_print_type_post(type->return_type, false);
105 /* don't emit braces if we're the toplevel type... */
113 function_parameter_t *parameter = type->parameters;
114 for( ; parameter != NULL; parameter = parameter->next) {
120 print_type(parameter->type);
123 declaration_t *parameter = scope->declarations;
124 for( ; parameter != NULL; parameter = parameter->next) {
130 print_type_ext(parameter->type, parameter->symbol,
142 if(first && !type->unspecified_parameters) {
149 * Prints the prefix part of a pointer type.
151 * @param type The pointer type.
153 static void print_pointer_type_pre(const pointer_type_t *type)
155 intern_print_type_pre(type->points_to, false);
157 print_type_qualifiers(type->type.qualifiers);
161 * Prints the postfix part of a pointer type.
163 * @param type The pointer type.
165 static void print_pointer_type_post(const pointer_type_t *type)
167 intern_print_type_post(type->points_to, false);
171 * Prints the prefix part of an array type.
173 * @param type The array type.
175 static void print_array_type_pre(const array_type_t *type)
177 intern_print_type_pre(type->element_type, false);
181 * Prints the postfix part of an array type.
183 * @param type The array type.
185 static void print_array_type_post(const array_type_t *type)
188 if(type->is_static) {
189 fputs("static ", out);
191 print_type_qualifiers(type->type.qualifiers);
192 if(type->size != NULL) {
193 print_expression(type->size);
196 intern_print_type_post(type->element_type, false);
200 * Prints the postfix part of a bitfield type.
202 * @param type The array type.
204 static void print_bitfield_type_post(const bitfield_type_t *type)
207 print_expression(type->size);
208 intern_print_type_post(type->base, false);
212 * Prints an enum definition.
214 * @param declaration The enum's type declaration.
216 void print_enum_definition(const declaration_t *declaration)
222 declaration_t *entry = declaration->next;
223 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
224 entry = entry->next) {
227 fprintf(out, "%s", entry->symbol->string);
228 if(entry->init.initializer != NULL) {
230 print_expression(entry->init.enum_value);
241 * Prints an enum type.
243 * @param type The enum type.
245 static void print_type_enum(const enum_type_t *type)
247 print_type_qualifiers(type->type.qualifiers);
250 declaration_t *declaration = type->declaration;
251 symbol_t *symbol = declaration->symbol;
253 fputs(symbol->string, out);
255 print_enum_definition(declaration);
260 * Print the compound part of a compound type.
262 * @param declaration The declaration of the compound type.
264 void print_compound_definition(const declaration_t *declaration)
269 declaration_t *iter = declaration->scope.declarations;
270 for( ; iter != NULL; iter = iter->next) {
272 print_declaration(iter);
282 * Prints a compound type.
284 * @param type The compound type.
286 static void print_compound_type(const compound_type_t *type)
288 print_type_qualifiers(type->type.qualifiers);
290 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
291 fputs("struct ", out);
293 assert(type->type.kind == TYPE_COMPOUND_UNION);
294 fputs("union ", out);
297 declaration_t *declaration = type->declaration;
298 symbol_t *symbol = declaration->symbol;
300 fputs(symbol->string, out);
302 print_compound_definition(declaration);
307 * Prints the prefix part of a typedef type.
309 * @param type The typedef type.
311 static void print_typedef_type_pre(const typedef_type_t *const type)
313 print_type_qualifiers(type->type.qualifiers);
314 fputs(type->declaration->symbol->string, out);
318 * Prints the prefix part of a typeof type.
320 * @param type The typeof type.
322 static void print_typeof_type_pre(const typeof_type_t *const type)
324 fputs("typeof(", out);
325 if(type->expression != NULL) {
326 assert(type->typeof_type == NULL);
327 print_expression(type->expression);
329 print_type(type->typeof_type);
335 * Prints the prefix part of a type.
337 * @param type The type.
338 * @param top true if we print the toplevel type, false else.
340 static void intern_print_type_pre(const type_t *const type, const bool top)
344 fputs("<error>", out);
346 fputs("<invalid>", out);
349 print_type_enum(&type->enumt);
352 print_atomic_type(&type->atomic);
354 case TYPE_COMPOUND_STRUCT:
355 case TYPE_COMPOUND_UNION:
356 print_compound_type(&type->compound);
359 fputs(type->builtin.symbol->string, out);
362 print_function_type_pre(&type->function, top);
365 print_pointer_type_pre(&type->pointer);
368 intern_print_type_pre(type->bitfield.base, top);
371 print_array_type_pre(&type->array);
374 print_typedef_type_pre(&type->typedeft);
377 print_typeof_type_pre(&type->typeoft);
380 fputs("unknown", out);
384 * Prints the postfix part of a type.
386 * @param type The type.
387 * @param top true if we print the toplevel type, false else.
389 static void intern_print_type_post(const type_t *const type, const bool top)
393 print_function_type_post(&type->function, NULL, top);
396 print_pointer_type_post(&type->pointer);
399 print_array_type_post(&type->array);
402 print_bitfield_type_post(&type->bitfield);
408 case TYPE_COMPOUND_STRUCT:
409 case TYPE_COMPOUND_UNION:
420 * @param type The type.
422 void print_type(const type_t *const type)
424 print_type_ext(type, NULL, NULL);
427 void print_type_ext(const type_t *const type, const symbol_t *symbol,
428 const scope_t *scope)
431 fputs("nil type", out);
435 intern_print_type_pre(type, true);
438 fputs(symbol->string, out);
440 if(type->kind == TYPE_FUNCTION) {
441 print_function_type_post(&type->function, scope, true);
443 intern_print_type_post(type, true);
448 * Return the size of a type AST node.
450 * @param type The type.
452 static size_t get_type_size(const type_t *type)
455 case TYPE_ATOMIC: return sizeof(atomic_type_t);
456 case TYPE_COMPOUND_STRUCT:
457 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
458 case TYPE_ENUM: return sizeof(enum_type_t);
459 case TYPE_FUNCTION: return sizeof(function_type_t);
460 case TYPE_POINTER: return sizeof(pointer_type_t);
461 case TYPE_ARRAY: return sizeof(array_type_t);
462 case TYPE_BUILTIN: return sizeof(builtin_type_t);
463 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
464 case TYPE_TYPEOF: return sizeof(typeof_type_t);
465 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
466 case TYPE_ERROR: panic("error type found");
467 case TYPE_INVALID: panic("invalid type found");
469 panic("unknown type found");
475 * @param type The type to copy.
476 * @return A copy of the type.
478 * @note This does not produce a deep copy!
480 type_t *duplicate_type(const type_t *type)
482 size_t size = get_type_size(type);
484 type_t *copy = obstack_alloc(type_obst, size);
485 memcpy(copy, type, size);
491 * Returns the unqualified type of a given type.
493 * @param type The type.
494 * @returns The unqualified type.
496 type_t *get_unqualified_type(type_t *type)
498 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
501 type_t *unqualified_type = duplicate_type(type);
502 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
504 type_t *result = typehash_insert(unqualified_type);
505 if(result != unqualified_type) {
506 obstack_free(type_obst, unqualified_type);
513 * Check if a type is valid.
515 * @param type The type to check.
516 * @return true if type represents a valid type.
518 bool type_valid(const type_t *type)
520 return type->kind != TYPE_INVALID;
524 * Returns true if the given type is an integer type.
526 * @param type The type to check.
527 * @return True if type is an integer type.
529 bool is_type_integer(const type_t *type)
531 assert(!is_typeref(type));
533 if(type->kind == TYPE_ENUM)
536 if(type->kind != TYPE_ATOMIC)
539 switch(type->atomic.akind) {
540 case ATOMIC_TYPE_BOOL:
541 case ATOMIC_TYPE_CHAR:
542 case ATOMIC_TYPE_SCHAR:
543 case ATOMIC_TYPE_UCHAR:
544 case ATOMIC_TYPE_SHORT:
545 case ATOMIC_TYPE_USHORT:
546 case ATOMIC_TYPE_INT:
547 case ATOMIC_TYPE_UINT:
548 case ATOMIC_TYPE_LONG:
549 case ATOMIC_TYPE_ULONG:
550 case ATOMIC_TYPE_LONGLONG:
551 case ATOMIC_TYPE_ULONGLONG:
559 * Returns true if the given type is an floating point type.
561 * @param type The type to check.
562 * @return True if type is a floating point type.
564 bool is_type_float(const type_t *type)
566 assert(!is_typeref(type));
568 if(type->kind != TYPE_ATOMIC)
571 switch(type->atomic.akind) {
572 case ATOMIC_TYPE_FLOAT:
573 case ATOMIC_TYPE_DOUBLE:
574 case ATOMIC_TYPE_LONG_DOUBLE:
575 #ifdef PROVIDE_COMPLEX
576 case ATOMIC_TYPE_FLOAT_COMPLEX:
577 case ATOMIC_TYPE_DOUBLE_COMPLEX:
578 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
579 case ATOMIC_TYPE_FLOAT_IMAGINARY:
580 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
581 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
590 * Returns true if the given type is a signed type.
592 * @param type The type to check.
593 * @return True if type is a signed type.
595 bool is_type_signed(const type_t *type)
597 assert(!is_typeref(type));
599 /* enum types are int for now */
600 if(type->kind == TYPE_ENUM)
603 if(type->kind != TYPE_ATOMIC)
606 switch(type->atomic.akind) {
607 case ATOMIC_TYPE_CHAR:
608 case ATOMIC_TYPE_SCHAR:
609 case ATOMIC_TYPE_SHORT:
610 case ATOMIC_TYPE_INT:
611 case ATOMIC_TYPE_LONG:
612 case ATOMIC_TYPE_LONGLONG:
613 case ATOMIC_TYPE_FLOAT:
614 case ATOMIC_TYPE_DOUBLE:
615 case ATOMIC_TYPE_LONG_DOUBLE:
616 #ifdef PROVIDE_COMPLEX
617 case ATOMIC_TYPE_FLOAT_COMPLEX:
618 case ATOMIC_TYPE_DOUBLE_COMPLEX:
619 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
620 case ATOMIC_TYPE_FLOAT_IMAGINARY:
621 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
622 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
626 case ATOMIC_TYPE_BOOL:
627 case ATOMIC_TYPE_UCHAR:
628 case ATOMIC_TYPE_USHORT:
629 case ATOMIC_TYPE_UINT:
630 case ATOMIC_TYPE_ULONG:
631 case ATOMIC_TYPE_ULONGLONG:
634 case ATOMIC_TYPE_VOID:
635 case ATOMIC_TYPE_INVALID:
636 case ATOMIC_TYPE_LAST:
640 panic("invalid atomic type found");
645 * Returns true if the given type represents an arithmetic type.
647 * @param type The type to check.
648 * @return True if type represents an arithmetic type.
650 bool is_type_arithmetic(const type_t *type)
652 assert(!is_typeref(type));
654 if(type->kind == TYPE_BITFIELD)
657 if(is_type_integer(type) || is_type_float(type))
664 * Returns true if the given type represents a scalar type.
666 * @param type The type to check.
667 * @return True if type represents a scalar type.
669 bool is_type_scalar(const type_t *type)
671 assert(!is_typeref(type));
673 switch (type->kind) {
674 case TYPE_POINTER: return true;
675 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
679 return is_type_arithmetic(type);
683 * Check if a given type is incomplete.
685 * @param type The type to check.
686 * @return True if the given type is incomplete (ie. just forward).
688 bool is_type_incomplete(const type_t *type)
690 assert(!is_typeref(type));
693 case TYPE_COMPOUND_STRUCT:
694 case TYPE_COMPOUND_UNION: {
695 const compound_type_t *compound_type = &type->compound;
696 declaration_t *declaration = compound_type->declaration;
697 return !declaration->init.is_defined;
700 const enum_type_t *enum_type = &type->enumt;
701 declaration_t *declaration = enum_type->declaration;
702 return !declaration->init.is_defined;
709 return type->array.size == NULL;
712 return type->atomic.akind == ATOMIC_TYPE_VOID;
721 panic("is_type_incomplete called without typerefs skipped");
726 panic("invalid type found");
730 * Check if two function types are compatible.
732 static bool function_types_compatible(const function_type_t *func1,
733 const function_type_t *func2)
735 const type_t* const ret1 = skip_typeref(func1->return_type);
736 const type_t* const ret2 = skip_typeref(func2->return_type);
737 if (!types_compatible(ret1, ret2))
740 /* can parameters be compared? */
741 if(func1->unspecified_parameters || func2->unspecified_parameters)
744 if(func1->variadic != func2->variadic)
747 /* TODO: handling of unspecified parameters not correct yet */
749 /* all argument types must be compatible */
750 function_parameter_t *parameter1 = func1->parameters;
751 function_parameter_t *parameter2 = func2->parameters;
752 for( ; parameter1 != NULL && parameter2 != NULL;
753 parameter1 = parameter1->next, parameter2 = parameter2->next) {
754 type_t *parameter1_type = skip_typeref(parameter1->type);
755 type_t *parameter2_type = skip_typeref(parameter2->type);
757 parameter1_type = get_unqualified_type(parameter1_type);
758 parameter2_type = get_unqualified_type(parameter2_type);
760 if(!types_compatible(parameter1_type, parameter2_type))
763 /* same number of arguments? */
764 if(parameter1 != NULL || parameter2 != NULL)
771 * Check if two array types are compatible.
773 static bool array_types_compatible(const array_type_t *array1,
774 const array_type_t *array2)
776 type_t *element_type1 = skip_typeref(array1->element_type);
777 type_t *element_type2 = skip_typeref(array2->element_type);
778 if(!types_compatible(element_type1, element_type2))
781 if(array1->size != NULL && array2->size != NULL) {
782 /* TODO: check if size expression evaluate to the same value
783 * if they are constant */
790 * Check if two types are compatible.
792 bool types_compatible(const type_t *type1, const type_t *type2)
794 assert(!is_typeref(type1));
795 assert(!is_typeref(type2));
797 /* shortcut: the same type is always compatible */
801 if(type1->base.qualifiers != type2->base.qualifiers)
803 if(type1->kind != type2->kind)
806 switch(type1->kind) {
808 return function_types_compatible(&type1->function, &type2->function);
810 return type1->atomic.akind == type2->atomic.akind;
812 return array_types_compatible(&type1->array, &type2->array);
815 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
816 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
817 return types_compatible(to1, to2);
820 case TYPE_COMPOUND_STRUCT:
821 case TYPE_COMPOUND_UNION:
824 /* TODO: not implemented */
828 /* not sure if this makes sense or is even needed, implement it if you
830 panic("type compatibility check for bitfield type");
833 /* Hmm, the error type should be compatible to all other types */
836 panic("invalid type found in compatible types");
839 panic("typerefs not skipped in compatible types?!?");
842 /* TODO: incomplete */
847 * Check if two pointer types are compatible.
849 bool pointers_compatible(const type_t *type1, const type_t *type2)
851 assert(!is_typeref(type1));
852 assert(!is_typeref(type2));
854 assert(type1->kind == TYPE_POINTER);
855 assert(type2->kind == TYPE_POINTER);
861 * Skip all typerefs and return the underlying type.
863 type_t *skip_typeref(type_t *type)
865 unsigned qualifiers = TYPE_QUALIFIER_NONE;
872 qualifiers |= type->base.qualifiers;
873 const typedef_type_t *typedef_type = &type->typedeft;
874 if(typedef_type->resolved_type != NULL) {
875 type = typedef_type->resolved_type;
878 type = typedef_type->declaration->type;
882 const typeof_type_t *typeof_type = &type->typeoft;
883 if(typeof_type->typeof_type != NULL) {
884 type = typeof_type->typeof_type;
886 type = typeof_type->expression->base.datatype;
896 if (qualifiers != TYPE_QUALIFIER_NONE) {
897 type_t *const copy = duplicate_type(type);
898 copy->base.qualifiers |= qualifiers;
900 type = typehash_insert(copy);
902 obstack_free(type_obst, copy);
910 * Hash the given type and return the "singleton" version
913 static type_t *identify_new_type(type_t *type)
915 type_t *result = typehash_insert(type);
917 obstack_free(type_obst, type);
923 * Creates a new atomic type.
925 * @param akind The kind of the atomic type.
926 * @param qualifiers Type qualifiers for the new type.
928 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
930 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
931 memset(type, 0, sizeof(atomic_type_t));
933 type->kind = TYPE_ATOMIC;
934 type->base.qualifiers = qualifiers;
935 type->atomic.akind = atype;
937 return identify_new_type(type);
941 * Creates a new pointer type.
943 * @param points_to The points-to type for teh new type.
944 * @param qualifiers Type qualifiers for the new type.
946 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
948 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
949 memset(type, 0, sizeof(pointer_type_t));
951 type->kind = TYPE_POINTER;
952 type->base.qualifiers = qualifiers;
953 type->pointer.points_to = points_to;
955 return identify_new_type(type);
959 * Debug helper. Prints the given type to stdout.
961 static __attribute__((unused))
962 void dbg_type(const type_t *type)