9 static struct obstack _type_obst;
11 struct obstack *type_obst = &_type_obst;
12 static int type_visited = 0;
13 static bool print_implicit_array_size = true;
15 static void intern_print_type_pre(const type_t *type, bool top);
16 static void intern_print_type_post(const type_t *type, bool top);
20 obstack_init(type_obst);
25 obstack_free(type_obst, NULL);
28 void type_set_output(FILE *stream)
33 void inc_type_visited(void)
38 void print_type_qualifiers(type_qualifiers_t qualifiers)
40 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
41 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
42 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
46 * Prints the name of a atomic type.
48 * @param type The type.
51 void print_atomic_type(const atomic_type_t *type)
53 print_type_qualifiers(type->type.qualifiers);
57 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
58 case ATOMIC_TYPE_VOID: s = "void"; break;
59 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
60 case ATOMIC_TYPE_CHAR: s = "char"; break;
61 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
62 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
63 case ATOMIC_TYPE_INT: s = "int"; break;
64 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
65 case ATOMIC_TYPE_SHORT: s = "short"; break;
66 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
67 case ATOMIC_TYPE_LONG: s = "long"; break;
68 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
69 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
70 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
71 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
72 case ATOMIC_TYPE_FLOAT: s = "float"; break;
73 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
74 default: s = "UNKNOWNATOMIC"; break;
80 * Print the first part (the prefix) of a type.
82 * @param type The type to print.
83 * @param top true, if this is the top type, false if it's an embedded type.
85 static void print_function_type_pre(const function_type_t *type, bool top)
87 print_type_qualifiers(type->type.qualifiers);
89 intern_print_type_pre(type->return_type, false);
91 /* don't emit braces if we're the toplevel type... */
97 * Print the second part (the postfix) of a type.
99 * @param type The type to print.
100 * @param top true, if this is the top type, false if it's an embedded type.
102 static void print_function_type_post(const function_type_t *type,
103 const scope_t *scope, bool top)
105 intern_print_type_post(type->return_type, false);
106 /* don't emit braces if we're the toplevel type... */
114 function_parameter_t *parameter = type->parameters;
115 for( ; parameter != NULL; parameter = parameter->next) {
121 print_type(parameter->type);
124 declaration_t *parameter = scope->declarations;
125 for( ; parameter != NULL; parameter = parameter->next) {
131 print_type_ext(parameter->type, parameter->symbol,
143 if(first && !type->unspecified_parameters) {
150 * Prints the prefix part of a pointer type.
152 * @param type The pointer type.
154 static void print_pointer_type_pre(const pointer_type_t *type)
156 intern_print_type_pre(type->points_to, false);
158 print_type_qualifiers(type->type.qualifiers);
162 * Prints the postfix part of a pointer type.
164 * @param type The pointer type.
166 static void print_pointer_type_post(const pointer_type_t *type)
168 intern_print_type_post(type->points_to, false);
172 * Prints the prefix part of an array type.
174 * @param type The array type.
176 static void print_array_type_pre(const array_type_t *type)
178 intern_print_type_pre(type->element_type, false);
182 * Prints the postfix part of an array type.
184 * @param type The array type.
186 static void print_array_type_post(const array_type_t *type)
189 if(type->is_static) {
190 fputs("static ", out);
192 print_type_qualifiers(type->type.qualifiers);
193 if(type->size_expression != NULL
194 && (print_implicit_array_size || !type->has_implicit_size)) {
195 print_expression(type->size_expression);
198 intern_print_type_post(type->element_type, false);
202 * Prints the postfix part of a bitfield type.
204 * @param type The array type.
206 static void print_bitfield_type_post(const bitfield_type_t *type)
209 print_expression(type->size);
210 intern_print_type_post(type->base, false);
214 * Prints an enum definition.
216 * @param declaration The enum's type declaration.
218 void print_enum_definition(const declaration_t *declaration)
224 declaration_t *entry = declaration->next;
225 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
226 entry = entry->next) {
229 fprintf(out, "%s", entry->symbol->string);
230 if(entry->init.initializer != NULL) {
232 print_expression(entry->init.enum_value);
243 * Prints an enum type.
245 * @param type The enum type.
247 static void print_type_enum(const enum_type_t *type)
249 print_type_qualifiers(type->type.qualifiers);
252 declaration_t *declaration = type->declaration;
253 symbol_t *symbol = declaration->symbol;
255 fputs(symbol->string, out);
257 print_enum_definition(declaration);
262 * Print the compound part of a compound type.
264 * @param declaration The declaration of the compound type.
266 void print_compound_definition(const declaration_t *declaration)
271 declaration_t *iter = declaration->scope.declarations;
272 for( ; iter != NULL; iter = iter->next) {
274 print_declaration(iter);
284 * Prints a compound type.
286 * @param type The compound type.
288 static void print_compound_type(const compound_type_t *type)
290 print_type_qualifiers(type->type.qualifiers);
292 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
293 fputs("struct ", out);
295 assert(type->type.kind == TYPE_COMPOUND_UNION);
296 fputs("union ", out);
299 declaration_t *declaration = type->declaration;
300 symbol_t *symbol = declaration->symbol;
302 fputs(symbol->string, out);
304 print_compound_definition(declaration);
309 * Prints the prefix part of a typedef type.
311 * @param type The typedef type.
313 static void print_typedef_type_pre(const typedef_type_t *const type)
315 print_type_qualifiers(type->type.qualifiers);
316 fputs(type->declaration->symbol->string, out);
320 * Prints the prefix part of a typeof type.
322 * @param type The typeof type.
324 static void print_typeof_type_pre(const typeof_type_t *const type)
326 fputs("typeof(", out);
327 if(type->expression != NULL) {
328 assert(type->typeof_type == NULL);
329 print_expression(type->expression);
331 print_type(type->typeof_type);
337 * Prints the prefix part of a type.
339 * @param type The type.
340 * @param top true if we print the toplevel type, false else.
342 static void intern_print_type_pre(const type_t *const type, const bool top)
346 fputs("<error>", out);
348 fputs("<invalid>", out);
351 print_type_enum(&type->enumt);
354 print_atomic_type(&type->atomic);
356 case TYPE_COMPOUND_STRUCT:
357 case TYPE_COMPOUND_UNION:
358 print_compound_type(&type->compound);
361 fputs(type->builtin.symbol->string, out);
364 print_function_type_pre(&type->function, top);
367 print_pointer_type_pre(&type->pointer);
370 intern_print_type_pre(type->bitfield.base, top);
373 print_array_type_pre(&type->array);
376 print_typedef_type_pre(&type->typedeft);
379 print_typeof_type_pre(&type->typeoft);
382 fputs("unknown", out);
386 * Prints the postfix part of a type.
388 * @param type The type.
389 * @param top true if we print the toplevel type, false else.
391 static void intern_print_type_post(const type_t *const type, const bool top)
395 print_function_type_post(&type->function, NULL, top);
398 print_pointer_type_post(&type->pointer);
401 print_array_type_post(&type->array);
404 print_bitfield_type_post(&type->bitfield);
410 case TYPE_COMPOUND_STRUCT:
411 case TYPE_COMPOUND_UNION:
422 * @param type The type.
424 void print_type(const type_t *const type)
426 print_type_ext(type, NULL, NULL);
429 void print_type_ext(const type_t *const type, const symbol_t *symbol,
430 const scope_t *scope)
433 fputs("nil type", out);
437 intern_print_type_pre(type, true);
440 fputs(symbol->string, out);
442 if(type->kind == TYPE_FUNCTION) {
443 print_function_type_post(&type->function, scope, true);
445 intern_print_type_post(type, true);
450 * Return the size of a type AST node.
452 * @param type The type.
454 static size_t get_type_size(const type_t *type)
457 case TYPE_ATOMIC: return sizeof(atomic_type_t);
458 case TYPE_COMPOUND_STRUCT:
459 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
460 case TYPE_ENUM: return sizeof(enum_type_t);
461 case TYPE_FUNCTION: return sizeof(function_type_t);
462 case TYPE_POINTER: return sizeof(pointer_type_t);
463 case TYPE_ARRAY: return sizeof(array_type_t);
464 case TYPE_BUILTIN: return sizeof(builtin_type_t);
465 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
466 case TYPE_TYPEOF: return sizeof(typeof_type_t);
467 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
468 case TYPE_ERROR: panic("error type found");
469 case TYPE_INVALID: panic("invalid type found");
471 panic("unknown type found");
477 * @param type The type to copy.
478 * @return A copy of the type.
480 * @note This does not produce a deep copy!
482 type_t *duplicate_type(const type_t *type)
484 size_t size = get_type_size(type);
486 type_t *copy = obstack_alloc(type_obst, size);
487 memcpy(copy, type, size);
493 * Returns the unqualified type of a given type.
495 * @param type The type.
496 * @returns The unqualified type.
498 type_t *get_unqualified_type(type_t *type)
500 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
503 type_t *unqualified_type = duplicate_type(type);
504 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
506 type_t *result = typehash_insert(unqualified_type);
507 if(result != unqualified_type) {
508 obstack_free(type_obst, unqualified_type);
515 * Check if a type is valid.
517 * @param type The type to check.
518 * @return true if type represents a valid type.
520 bool type_valid(const type_t *type)
522 return type->kind != TYPE_INVALID;
526 * Returns true if the given type is an integer type.
528 * @param type The type to check.
529 * @return True if type is an integer type.
531 bool is_type_integer(const type_t *type)
533 assert(!is_typeref(type));
535 if(type->kind == TYPE_ENUM)
538 if(type->kind != TYPE_ATOMIC)
541 switch(type->atomic.akind) {
542 case ATOMIC_TYPE_BOOL:
543 case ATOMIC_TYPE_CHAR:
544 case ATOMIC_TYPE_SCHAR:
545 case ATOMIC_TYPE_UCHAR:
546 case ATOMIC_TYPE_SHORT:
547 case ATOMIC_TYPE_USHORT:
548 case ATOMIC_TYPE_INT:
549 case ATOMIC_TYPE_UINT:
550 case ATOMIC_TYPE_LONG:
551 case ATOMIC_TYPE_ULONG:
552 case ATOMIC_TYPE_LONGLONG:
553 case ATOMIC_TYPE_ULONGLONG:
561 * Returns true if the given type is an floating point type.
563 * @param type The type to check.
564 * @return True if type is a floating point type.
566 bool is_type_float(const type_t *type)
568 assert(!is_typeref(type));
570 if(type->kind != TYPE_ATOMIC)
573 switch(type->atomic.akind) {
574 case ATOMIC_TYPE_FLOAT:
575 case ATOMIC_TYPE_DOUBLE:
576 case ATOMIC_TYPE_LONG_DOUBLE:
577 #ifdef PROVIDE_COMPLEX
578 case ATOMIC_TYPE_FLOAT_COMPLEX:
579 case ATOMIC_TYPE_DOUBLE_COMPLEX:
580 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
581 case ATOMIC_TYPE_FLOAT_IMAGINARY:
582 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
583 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
592 * Returns true if the given type is a signed type.
594 * @param type The type to check.
595 * @return True if type is a signed type.
597 bool is_type_signed(const type_t *type)
599 assert(!is_typeref(type));
601 /* enum types are int for now */
602 if(type->kind == TYPE_ENUM)
605 if(type->kind != TYPE_ATOMIC)
608 switch(type->atomic.akind) {
609 case ATOMIC_TYPE_CHAR:
610 case ATOMIC_TYPE_SCHAR:
611 case ATOMIC_TYPE_SHORT:
612 case ATOMIC_TYPE_INT:
613 case ATOMIC_TYPE_LONG:
614 case ATOMIC_TYPE_LONGLONG:
615 case ATOMIC_TYPE_FLOAT:
616 case ATOMIC_TYPE_DOUBLE:
617 case ATOMIC_TYPE_LONG_DOUBLE:
618 #ifdef PROVIDE_COMPLEX
619 case ATOMIC_TYPE_FLOAT_COMPLEX:
620 case ATOMIC_TYPE_DOUBLE_COMPLEX:
621 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
622 case ATOMIC_TYPE_FLOAT_IMAGINARY:
623 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
624 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
628 case ATOMIC_TYPE_BOOL:
629 case ATOMIC_TYPE_UCHAR:
630 case ATOMIC_TYPE_USHORT:
631 case ATOMIC_TYPE_UINT:
632 case ATOMIC_TYPE_ULONG:
633 case ATOMIC_TYPE_ULONGLONG:
636 case ATOMIC_TYPE_VOID:
637 case ATOMIC_TYPE_INVALID:
638 case ATOMIC_TYPE_LAST:
642 panic("invalid atomic type found");
647 * Returns true if the given type represents an arithmetic type.
649 * @param type The type to check.
650 * @return True if type represents an arithmetic type.
652 bool is_type_arithmetic(const type_t *type)
654 assert(!is_typeref(type));
656 if(type->kind == TYPE_BITFIELD)
659 if(is_type_integer(type) || is_type_float(type))
666 * Returns true if the given type represents a scalar type.
668 * @param type The type to check.
669 * @return True if type represents a scalar type.
671 bool is_type_scalar(const type_t *type)
673 assert(!is_typeref(type));
675 switch (type->kind) {
676 case TYPE_POINTER: return true;
677 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
681 return is_type_arithmetic(type);
685 * Check if a given type is incomplete.
687 * @param type The type to check.
688 * @return True if the given type is incomplete (ie. just forward).
690 bool is_type_incomplete(const type_t *type)
692 assert(!is_typeref(type));
695 case TYPE_COMPOUND_STRUCT:
696 case TYPE_COMPOUND_UNION: {
697 const compound_type_t *compound_type = &type->compound;
698 declaration_t *declaration = compound_type->declaration;
699 return !declaration->init.is_defined;
702 const enum_type_t *enum_type = &type->enumt;
703 declaration_t *declaration = enum_type->declaration;
704 return !declaration->init.is_defined;
711 return type->array.size_expression == NULL;
714 return type->atomic.akind == ATOMIC_TYPE_VOID;
723 panic("is_type_incomplete called without typerefs skipped");
728 panic("invalid type found");
732 * Check if two function types are compatible.
734 static bool function_types_compatible(const function_type_t *func1,
735 const function_type_t *func2)
737 const type_t* const ret1 = skip_typeref(func1->return_type);
738 const type_t* const ret2 = skip_typeref(func2->return_type);
739 if (!types_compatible(ret1, ret2))
742 /* can parameters be compared? */
743 if(func1->unspecified_parameters || func2->unspecified_parameters)
746 if(func1->variadic != func2->variadic)
749 /* TODO: handling of unspecified parameters not correct yet */
751 /* all argument types must be compatible */
752 function_parameter_t *parameter1 = func1->parameters;
753 function_parameter_t *parameter2 = func2->parameters;
754 for( ; parameter1 != NULL && parameter2 != NULL;
755 parameter1 = parameter1->next, parameter2 = parameter2->next) {
756 type_t *parameter1_type = skip_typeref(parameter1->type);
757 type_t *parameter2_type = skip_typeref(parameter2->type);
759 parameter1_type = get_unqualified_type(parameter1_type);
760 parameter2_type = get_unqualified_type(parameter2_type);
762 if(!types_compatible(parameter1_type, parameter2_type))
765 /* same number of arguments? */
766 if(parameter1 != NULL || parameter2 != NULL)
773 * Check if two array types are compatible.
775 static bool array_types_compatible(const array_type_t *array1,
776 const array_type_t *array2)
778 type_t *element_type1 = skip_typeref(array1->element_type);
779 type_t *element_type2 = skip_typeref(array2->element_type);
780 if(!types_compatible(element_type1, element_type2))
783 if(!array1->size_constant || !array2->size_constant)
786 return array1->size == array2->size;
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);
863 * Skip all typerefs and return the underlying type.
865 type_t *skip_typeref(type_t *type)
867 unsigned qualifiers = TYPE_QUALIFIER_NONE;
874 qualifiers |= type->base.qualifiers;
875 const typedef_type_t *typedef_type = &type->typedeft;
876 if(typedef_type->resolved_type != NULL) {
877 type = typedef_type->resolved_type;
880 type = typedef_type->declaration->type;
884 const typeof_type_t *typeof_type = &type->typeoft;
885 if(typeof_type->typeof_type != NULL) {
886 type = typeof_type->typeof_type;
888 type = typeof_type->expression->base.type;
898 if (qualifiers != TYPE_QUALIFIER_NONE) {
899 type_t *const copy = duplicate_type(type);
900 copy->base.qualifiers |= qualifiers;
902 type = typehash_insert(copy);
904 obstack_free(type_obst, copy);
912 * Hash the given type and return the "singleton" version
915 static type_t *identify_new_type(type_t *type)
917 type_t *result = typehash_insert(type);
919 obstack_free(type_obst, type);
925 * Creates a new atomic type.
927 * @param akind The kind of the atomic type.
928 * @param qualifiers Type qualifiers for the new type.
930 type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
932 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
933 memset(type, 0, sizeof(atomic_type_t));
935 type->kind = TYPE_ATOMIC;
936 type->base.qualifiers = qualifiers;
937 type->atomic.akind = atype;
939 return identify_new_type(type);
943 * Creates a new pointer type.
945 * @param points_to The points-to type for teh new type.
946 * @param qualifiers Type qualifiers for the new type.
948 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
950 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
951 memset(type, 0, sizeof(pointer_type_t));
953 type->kind = TYPE_POINTER;
954 type->base.qualifiers = qualifiers;
955 type->pointer.points_to = points_to;
957 return identify_new_type(type);
961 * Debug helper. Prints the given type to stdout.
963 static __attribute__((unused))
964 void dbg_type(const type_t *type)