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 void print_atomic_type(const atomic_type_t *type)
47 print_type_qualifiers(type->type.qualifiers);
51 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
52 case ATOMIC_TYPE_VOID: s = "void"; break;
53 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
54 case ATOMIC_TYPE_CHAR: s = "char"; break;
55 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
56 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
57 case ATOMIC_TYPE_INT: s = "int"; break;
58 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
59 case ATOMIC_TYPE_SHORT: s = "short"; break;
60 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
61 case ATOMIC_TYPE_LONG: s = "long"; break;
62 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
63 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
64 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
65 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
66 case ATOMIC_TYPE_FLOAT: s = "float"; break;
67 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
68 default: s = "UNKNOWNATOMIC"; break;
73 static void print_function_type_pre(const function_type_t *type, bool top)
75 print_type_qualifiers(type->type.qualifiers);
77 intern_print_type_pre(type->return_type, false);
79 /* don't emit braces if we're the toplevel type... */
84 static void print_function_type_post(const function_type_t *type,
85 const context_t *context, bool top)
87 intern_print_type_post(type->return_type, false);
88 /* don't emit braces if we're the toplevel type... */
96 function_parameter_t *parameter = type->parameters;
97 for( ; parameter != NULL; parameter = parameter->next) {
103 print_type(parameter->type);
106 declaration_t *parameter = context->declarations;
107 for( ; parameter != NULL; parameter = parameter->next) {
113 print_type_ext(parameter->type, parameter->symbol,
114 ¶meter->context);
125 if(first && !type->unspecified_parameters) {
131 static void print_pointer_type_pre(const pointer_type_t *type)
133 intern_print_type_pre(type->points_to, false);
135 print_type_qualifiers(type->type.qualifiers);
138 static void print_pointer_type_post(const pointer_type_t *type)
140 intern_print_type_post(type->points_to, false);
143 static void print_array_type_pre(const array_type_t *type)
145 intern_print_type_pre(type->element_type, false);
148 static void print_array_type_post(const array_type_t *type)
151 if(type->is_static) {
152 fputs("static ", out);
154 print_type_qualifiers(type->type.qualifiers);
155 if(type->size != NULL) {
156 print_expression(type->size);
159 intern_print_type_post(type->element_type, false);
162 void print_enum_definition(const declaration_t *declaration)
168 declaration_t *entry = declaration->next;
169 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
170 entry = entry->next) {
173 fprintf(out, "%s", entry->symbol->string);
174 if(entry->init.initializer != NULL) {
176 print_expression(entry->init.enum_value);
186 static void print_type_enum(const enum_type_t *type)
188 print_type_qualifiers(type->type.qualifiers);
191 declaration_t *declaration = type->declaration;
192 symbol_t *symbol = declaration->symbol;
194 fputs(symbol->string, out);
196 print_enum_definition(declaration);
200 void print_compound_definition(const declaration_t *declaration)
205 declaration_t *iter = declaration->context.declarations;
206 for( ; iter != NULL; iter = iter->next) {
208 print_declaration(iter);
217 static void print_compound_type(const compound_type_t *type)
219 print_type_qualifiers(type->type.qualifiers);
221 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
222 fputs("struct ", out);
224 assert(type->type.kind == TYPE_COMPOUND_UNION);
225 fputs("union ", out);
228 declaration_t *declaration = type->declaration;
229 symbol_t *symbol = declaration->symbol;
231 fputs(symbol->string, out);
233 print_compound_definition(declaration);
237 static void print_typedef_type_pre(const typedef_type_t *const type)
239 fputs(type->declaration->symbol->string, out);
242 static void print_typeof_type_pre(const typeof_type_t *const type)
244 fputs("typeof(", out);
245 if(type->expression != NULL) {
246 assert(type->typeof_type == NULL);
247 print_expression(type->expression);
249 print_type(type->typeof_type);
254 static void intern_print_type_pre(const type_t *const type, const bool top)
258 fputs("invalid", out);
261 print_type_enum(&type->enumt);
264 print_atomic_type(&type->atomic);
266 case TYPE_COMPOUND_STRUCT:
267 case TYPE_COMPOUND_UNION:
268 print_compound_type(&type->compound);
271 fputs(type->builtin.symbol->string, out);
274 print_function_type_pre(&type->function, top);
277 print_pointer_type_pre(&type->pointer);
280 print_array_type_pre(&type->array);
283 print_typedef_type_pre(&type->typedeft);
286 print_typeof_type_pre(&type->typeoft);
289 fputs("unknown", out);
292 static void intern_print_type_post(const type_t *const type, const bool top)
296 print_function_type_post(&type->function, NULL, top);
299 print_pointer_type_post(&type->pointer);
302 print_array_type_post(&type->array);
307 case TYPE_COMPOUND_STRUCT:
308 case TYPE_COMPOUND_UNION:
316 void print_type(const type_t *const type)
318 print_type_ext(type, NULL, NULL);
321 void print_type_ext(const type_t *const type, const symbol_t *symbol,
322 const context_t *context)
325 fputs("nil type", out);
329 intern_print_type_pre(type, true);
332 fputs(symbol->string, out);
334 if(type->kind == TYPE_FUNCTION) {
335 print_function_type_post(&type->function, context, true);
337 intern_print_type_post(type, true);
341 static size_t get_type_size(type_t *type)
344 case TYPE_ATOMIC: return sizeof(atomic_type_t);
345 case TYPE_COMPOUND_STRUCT:
346 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
347 case TYPE_ENUM: return sizeof(enum_type_t);
348 case TYPE_FUNCTION: return sizeof(function_type_t);
349 case TYPE_POINTER: return sizeof(pointer_type_t);
350 case TYPE_ARRAY: return sizeof(array_type_t);
351 case TYPE_BUILTIN: return sizeof(builtin_type_t);
352 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
353 case TYPE_TYPEOF: return sizeof(typeof_type_t);
354 case TYPE_INVALID: panic("invalid type found");
356 panic("unknown type found");
361 * note that this does not produce a deep copy!
363 type_t *duplicate_type(type_t *type)
365 size_t size = get_type_size(type);
367 type_t *copy = obstack_alloc(type_obst, size);
368 memcpy(copy, type, size);
373 type_t *get_unqualified_type(type_t *type)
375 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
378 type_t *unqualified_type = duplicate_type(type);
379 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
381 type_t *result = typehash_insert(unqualified_type);
382 if(result != unqualified_type) {
383 obstack_free(type_obst, unqualified_type);
389 bool type_valid(const type_t *type)
391 return type->kind != TYPE_INVALID;
394 bool is_type_integer(const type_t *type)
396 assert(!is_typeref(type));
398 if(type->kind == TYPE_ENUM)
401 if(type->kind != TYPE_ATOMIC)
404 switch(type->atomic.atype) {
405 case ATOMIC_TYPE_BOOL:
406 case ATOMIC_TYPE_CHAR:
407 case ATOMIC_TYPE_SCHAR:
408 case ATOMIC_TYPE_UCHAR:
409 case ATOMIC_TYPE_SHORT:
410 case ATOMIC_TYPE_USHORT:
411 case ATOMIC_TYPE_INT:
412 case ATOMIC_TYPE_UINT:
413 case ATOMIC_TYPE_LONG:
414 case ATOMIC_TYPE_ULONG:
415 case ATOMIC_TYPE_LONGLONG:
416 case ATOMIC_TYPE_ULONGLONG:
423 bool is_type_floating(const type_t *type)
425 assert(!is_typeref(type));
427 if(type->kind != TYPE_ATOMIC)
430 switch(type->atomic.atype) {
431 case ATOMIC_TYPE_FLOAT:
432 case ATOMIC_TYPE_DOUBLE:
433 case ATOMIC_TYPE_LONG_DOUBLE:
434 #ifdef PROVIDE_COMPLEX
435 case ATOMIC_TYPE_FLOAT_COMPLEX:
436 case ATOMIC_TYPE_DOUBLE_COMPLEX:
437 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
438 case ATOMIC_TYPE_FLOAT_IMAGINARY:
439 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
440 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
448 bool is_type_signed(const type_t *type)
450 assert(!is_typeref(type));
452 /* enum types are int for now */
453 if(type->kind == TYPE_ENUM)
456 if(type->kind != TYPE_ATOMIC)
459 switch(type->atomic.atype) {
460 case ATOMIC_TYPE_CHAR:
461 case ATOMIC_TYPE_SCHAR:
462 case ATOMIC_TYPE_SHORT:
463 case ATOMIC_TYPE_INT:
464 case ATOMIC_TYPE_LONG:
465 case ATOMIC_TYPE_LONGLONG:
466 case ATOMIC_TYPE_FLOAT:
467 case ATOMIC_TYPE_DOUBLE:
468 case ATOMIC_TYPE_LONG_DOUBLE:
469 #ifdef PROVIDE_COMPLEX
470 case ATOMIC_TYPE_FLOAT_COMPLEX:
471 case ATOMIC_TYPE_DOUBLE_COMPLEX:
472 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
473 case ATOMIC_TYPE_FLOAT_IMAGINARY:
474 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
475 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
479 case ATOMIC_TYPE_BOOL:
480 case ATOMIC_TYPE_UCHAR:
481 case ATOMIC_TYPE_USHORT:
482 case ATOMIC_TYPE_UINT:
483 case ATOMIC_TYPE_ULONG:
484 case ATOMIC_TYPE_ULONGLONG:
487 case ATOMIC_TYPE_VOID:
488 case ATOMIC_TYPE_INVALID:
489 case ATOMIC_TYPE_LAST:
493 panic("invalid atomic type found");
497 bool is_type_arithmetic(const type_t *type)
499 assert(!is_typeref(type));
501 if(is_type_integer(type) || is_type_floating(type))
507 bool is_type_scalar(const type_t *type)
509 assert(!is_typeref(type));
511 switch (type->kind) {
512 case TYPE_POINTER: return true;
513 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
517 return is_type_arithmetic(type);
520 bool is_type_incomplete(const type_t *type)
522 assert(!is_typeref(type));
525 case TYPE_COMPOUND_STRUCT:
526 case TYPE_COMPOUND_UNION: {
527 const compound_type_t *compound_type = &type->compound;
528 declaration_t *declaration = compound_type->declaration;
529 return !declaration->init.is_defined;
535 return type->array.size == NULL;
545 panic("is_type_incomplete called without typerefs skipped");
550 panic("invalid type found");
553 static bool function_types_compatible(const function_type_t *func1,
554 const function_type_t *func2)
556 if(!types_compatible(func1->return_type, func2->return_type))
559 /* can parameters be compared? */
560 if(func1->unspecified_parameters || func2->unspecified_parameters)
563 if(func1->variadic != func2->variadic)
566 /* TODO: handling of unspecified parameters not correct yet */
568 /* all argument types must be compatible */
569 function_parameter_t *parameter1 = func1->parameters;
570 function_parameter_t *parameter2 = func2->parameters;
571 for( ; parameter1 != NULL && parameter2 != NULL;
572 parameter1 = parameter1->next, parameter2 = parameter2->next) {
573 type_t *parameter1_type = skip_typeref(parameter1->type);
574 type_t *parameter2_type = skip_typeref(parameter2->type);
576 parameter1_type = get_unqualified_type(parameter1_type);
577 parameter2_type = get_unqualified_type(parameter2_type);
579 if(!types_compatible(parameter1_type, parameter2_type))
582 /* same number of arguments? */
583 if(parameter1 != NULL || parameter2 != NULL)
589 static bool array_types_compatible(const array_type_t *array1,
590 const array_type_t *array2)
592 type_t *element_type1 = skip_typeref(array1->element_type);
593 type_t *element_type2 = skip_typeref(array2->element_type);
594 if(!types_compatible(element_type1, element_type2))
597 if(array1->size != NULL && array2->size != NULL) {
598 /* TODO: check if size expression evaulate to the same value
599 * if they are constant */
605 bool types_compatible(const type_t *type1, const type_t *type2)
607 assert(!is_typeref(type1));
608 assert(!is_typeref(type2));
610 /* shortcut: the same type is always compatible */
614 if(type1->base.qualifiers != type2->base.qualifiers)
616 if(type1->kind != type2->kind)
619 switch(type1->kind) {
621 return function_types_compatible(&type1->function, &type2->function);
623 return type1->atomic.atype == type2->atomic.atype;
625 return array_types_compatible(&type1->array, &type2->array);
627 return types_compatible(type1->pointer.points_to,
628 type2->pointer.points_to);
629 case TYPE_COMPOUND_STRUCT:
630 case TYPE_COMPOUND_UNION:
633 /* TODO: not implemented */
637 panic("invalid type found in compatible types");
640 panic("typerefs not skipped in compatible types?!?");
643 /* TODO: incomplete */
647 bool pointers_compatible(const type_t *type1, const type_t *type2)
649 assert(!is_typeref(type1));
650 assert(!is_typeref(type2));
652 assert(type1->kind == TYPE_POINTER);
653 assert(type2->kind == TYPE_POINTER);
658 type_t *skip_typeref(type_t *type)
660 unsigned qualifiers = type->base.qualifiers;
665 qualifiers |= type->base.qualifiers;
666 const typedef_type_t *typedef_type = &type->typedeft;
667 if(typedef_type->resolved_type != NULL) {
668 type = typedef_type->resolved_type;
671 type = typedef_type->declaration->type;
675 const typeof_type_t *typeof_type = &type->typeoft;
676 if(typeof_type->typeof_type != NULL) {
677 type = typeof_type->typeof_type;
679 type = typeof_type->expression->base.datatype;
694 static type_t *identify_new_type(type_t *type)
696 type_t *result = typehash_insert(type);
698 obstack_free(type_obst, type);
703 type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
705 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
706 memset(type, 0, sizeof(atomic_type_t));
708 type->kind = TYPE_ATOMIC;
709 type->base.qualifiers = qualifiers;
710 type->atomic.atype = atype;
712 return identify_new_type(type);
715 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
717 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
718 memset(type, 0, sizeof(pointer_type_t));
720 type->kind = TYPE_POINTER;
721 type->base.qualifiers = qualifiers;
722 type->pointer.points_to = points_to;
724 return identify_new_type(type);
727 static __attribute__((unused))
728 void dbg_type(type_t *type)