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 static void print_bitfield_type_post(const bitfield_type_t *type)
165 print_expression(type->size);
166 intern_print_type_post(type->base, false);
169 void print_enum_definition(const declaration_t *declaration)
175 declaration_t *entry = declaration->next;
176 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
177 entry = entry->next) {
180 fprintf(out, "%s", entry->symbol->string);
181 if(entry->init.initializer != NULL) {
183 print_expression(entry->init.enum_value);
193 static void print_type_enum(const enum_type_t *type)
195 print_type_qualifiers(type->type.qualifiers);
198 declaration_t *declaration = type->declaration;
199 symbol_t *symbol = declaration->symbol;
201 fputs(symbol->string, out);
203 print_enum_definition(declaration);
207 void print_compound_definition(const declaration_t *declaration)
212 declaration_t *iter = declaration->context.declarations;
213 for( ; iter != NULL; iter = iter->next) {
215 print_declaration(iter);
224 static void print_compound_type(const compound_type_t *type)
226 print_type_qualifiers(type->type.qualifiers);
228 if(type->type.kind == TYPE_COMPOUND_STRUCT) {
229 fputs("struct ", out);
231 assert(type->type.kind == TYPE_COMPOUND_UNION);
232 fputs("union ", out);
235 declaration_t *declaration = type->declaration;
236 symbol_t *symbol = declaration->symbol;
238 fputs(symbol->string, out);
240 print_compound_definition(declaration);
244 static void print_typedef_type_pre(const typedef_type_t *const type)
246 print_type_qualifiers(type->type.qualifiers);
247 fputs(type->declaration->symbol->string, out);
250 static void print_typeof_type_pre(const typeof_type_t *const type)
252 fputs("typeof(", out);
253 if(type->expression != NULL) {
254 assert(type->typeof_type == NULL);
255 print_expression(type->expression);
257 print_type(type->typeof_type);
262 static void intern_print_type_pre(const type_t *const type, const bool top)
266 fputs("invalid", out);
269 print_type_enum(&type->enumt);
272 print_atomic_type(&type->atomic);
274 case TYPE_COMPOUND_STRUCT:
275 case TYPE_COMPOUND_UNION:
276 print_compound_type(&type->compound);
279 fputs(type->builtin.symbol->string, out);
282 print_function_type_pre(&type->function, top);
285 print_pointer_type_pre(&type->pointer);
288 intern_print_type_pre(type->bitfield.base, top);
291 print_array_type_pre(&type->array);
294 print_typedef_type_pre(&type->typedeft);
297 print_typeof_type_pre(&type->typeoft);
300 fputs("unknown", out);
303 static void intern_print_type_post(const type_t *const type, const bool top)
307 print_function_type_post(&type->function, NULL, top);
310 print_pointer_type_post(&type->pointer);
313 print_array_type_post(&type->array);
316 print_bitfield_type_post(&type->bitfield);
321 case TYPE_COMPOUND_STRUCT:
322 case TYPE_COMPOUND_UNION:
330 void print_type(const type_t *const type)
332 print_type_ext(type, NULL, NULL);
335 void print_type_ext(const type_t *const type, const symbol_t *symbol,
336 const context_t *context)
339 fputs("nil type", out);
343 intern_print_type_pre(type, true);
346 fputs(symbol->string, out);
348 if(type->kind == TYPE_FUNCTION) {
349 print_function_type_post(&type->function, context, true);
351 intern_print_type_post(type, true);
355 static size_t get_type_size(type_t *type)
358 case TYPE_ATOMIC: return sizeof(atomic_type_t);
359 case TYPE_COMPOUND_STRUCT:
360 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
361 case TYPE_ENUM: return sizeof(enum_type_t);
362 case TYPE_FUNCTION: return sizeof(function_type_t);
363 case TYPE_POINTER: return sizeof(pointer_type_t);
364 case TYPE_ARRAY: return sizeof(array_type_t);
365 case TYPE_BUILTIN: return sizeof(builtin_type_t);
366 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
367 case TYPE_TYPEOF: return sizeof(typeof_type_t);
368 case TYPE_BITFIELD: return sizeof(bitfield_type_t);
369 case TYPE_INVALID: panic("invalid type found");
371 panic("unknown type found");
376 * note that this does not produce a deep copy!
378 type_t *duplicate_type(type_t *type)
380 size_t size = get_type_size(type);
382 type_t *copy = obstack_alloc(type_obst, size);
383 memcpy(copy, type, size);
388 type_t *get_unqualified_type(type_t *type)
390 if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
393 type_t *unqualified_type = duplicate_type(type);
394 unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
396 type_t *result = typehash_insert(unqualified_type);
397 if(result != unqualified_type) {
398 obstack_free(type_obst, unqualified_type);
404 bool type_valid(const type_t *type)
406 return type->kind != TYPE_INVALID;
409 bool is_type_integer(const type_t *type)
411 assert(!is_typeref(type));
413 if(type->kind == TYPE_ENUM)
416 if(type->kind != TYPE_ATOMIC)
419 switch(type->atomic.atype) {
420 case ATOMIC_TYPE_BOOL:
421 case ATOMIC_TYPE_CHAR:
422 case ATOMIC_TYPE_SCHAR:
423 case ATOMIC_TYPE_UCHAR:
424 case ATOMIC_TYPE_SHORT:
425 case ATOMIC_TYPE_USHORT:
426 case ATOMIC_TYPE_INT:
427 case ATOMIC_TYPE_UINT:
428 case ATOMIC_TYPE_LONG:
429 case ATOMIC_TYPE_ULONG:
430 case ATOMIC_TYPE_LONGLONG:
431 case ATOMIC_TYPE_ULONGLONG:
438 bool is_type_floating(const type_t *type)
440 assert(!is_typeref(type));
442 if(type->kind != TYPE_ATOMIC)
445 switch(type->atomic.atype) {
446 case ATOMIC_TYPE_FLOAT:
447 case ATOMIC_TYPE_DOUBLE:
448 case ATOMIC_TYPE_LONG_DOUBLE:
449 #ifdef PROVIDE_COMPLEX
450 case ATOMIC_TYPE_FLOAT_COMPLEX:
451 case ATOMIC_TYPE_DOUBLE_COMPLEX:
452 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
453 case ATOMIC_TYPE_FLOAT_IMAGINARY:
454 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
455 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
463 bool is_type_signed(const type_t *type)
465 assert(!is_typeref(type));
467 /* enum types are int for now */
468 if(type->kind == TYPE_ENUM)
471 if(type->kind != TYPE_ATOMIC)
474 switch(type->atomic.atype) {
475 case ATOMIC_TYPE_CHAR:
476 case ATOMIC_TYPE_SCHAR:
477 case ATOMIC_TYPE_SHORT:
478 case ATOMIC_TYPE_INT:
479 case ATOMIC_TYPE_LONG:
480 case ATOMIC_TYPE_LONGLONG:
481 case ATOMIC_TYPE_FLOAT:
482 case ATOMIC_TYPE_DOUBLE:
483 case ATOMIC_TYPE_LONG_DOUBLE:
484 #ifdef PROVIDE_COMPLEX
485 case ATOMIC_TYPE_FLOAT_COMPLEX:
486 case ATOMIC_TYPE_DOUBLE_COMPLEX:
487 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
488 case ATOMIC_TYPE_FLOAT_IMAGINARY:
489 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
490 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
494 case ATOMIC_TYPE_BOOL:
495 case ATOMIC_TYPE_UCHAR:
496 case ATOMIC_TYPE_USHORT:
497 case ATOMIC_TYPE_UINT:
498 case ATOMIC_TYPE_ULONG:
499 case ATOMIC_TYPE_ULONGLONG:
502 case ATOMIC_TYPE_VOID:
503 case ATOMIC_TYPE_INVALID:
504 case ATOMIC_TYPE_LAST:
508 panic("invalid atomic type found");
512 bool is_type_arithmetic(const type_t *type)
514 assert(!is_typeref(type));
516 if(type->kind == TYPE_BITFIELD)
519 if(is_type_integer(type) || is_type_floating(type))
525 bool is_type_scalar(const type_t *type)
527 assert(!is_typeref(type));
529 switch (type->kind) {
530 case TYPE_POINTER: return true;
531 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
535 return is_type_arithmetic(type);
538 bool is_type_incomplete(const type_t *type)
540 assert(!is_typeref(type));
543 case TYPE_COMPOUND_STRUCT:
544 case TYPE_COMPOUND_UNION: {
545 const compound_type_t *compound_type = &type->compound;
546 declaration_t *declaration = compound_type->declaration;
547 return !declaration->init.is_defined;
554 return type->array.size == NULL;
557 return type->atomic.atype == ATOMIC_TYPE_VOID;
566 panic("is_type_incomplete called without typerefs skipped");
571 panic("invalid type found");
574 static bool function_types_compatible(const function_type_t *func1,
575 const function_type_t *func2)
577 const type_t* const ret1 = skip_typeref(func1->return_type);
578 const type_t* const ret2 = skip_typeref(func2->return_type);
579 if (!types_compatible(ret1, ret2))
582 /* can parameters be compared? */
583 if(func1->unspecified_parameters || func2->unspecified_parameters)
586 if(func1->variadic != func2->variadic)
589 /* TODO: handling of unspecified parameters not correct yet */
591 /* all argument types must be compatible */
592 function_parameter_t *parameter1 = func1->parameters;
593 function_parameter_t *parameter2 = func2->parameters;
594 for( ; parameter1 != NULL && parameter2 != NULL;
595 parameter1 = parameter1->next, parameter2 = parameter2->next) {
596 type_t *parameter1_type = skip_typeref(parameter1->type);
597 type_t *parameter2_type = skip_typeref(parameter2->type);
599 parameter1_type = get_unqualified_type(parameter1_type);
600 parameter2_type = get_unqualified_type(parameter2_type);
602 if(!types_compatible(parameter1_type, parameter2_type))
605 /* same number of arguments? */
606 if(parameter1 != NULL || parameter2 != NULL)
612 static bool array_types_compatible(const array_type_t *array1,
613 const array_type_t *array2)
615 type_t *element_type1 = skip_typeref(array1->element_type);
616 type_t *element_type2 = skip_typeref(array2->element_type);
617 if(!types_compatible(element_type1, element_type2))
620 if(array1->size != NULL && array2->size != NULL) {
621 /* TODO: check if size expression evaluate to the same value
622 * if they are constant */
628 bool types_compatible(const type_t *type1, const type_t *type2)
630 assert(!is_typeref(type1));
631 assert(!is_typeref(type2));
633 /* shortcut: the same type is always compatible */
637 if(type1->base.qualifiers != type2->base.qualifiers)
639 if(type1->kind != type2->kind)
642 switch(type1->kind) {
644 return function_types_compatible(&type1->function, &type2->function);
646 return type1->atomic.atype == type2->atomic.atype;
648 return array_types_compatible(&type1->array, &type2->array);
651 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
652 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
653 return types_compatible(to1, to2);
656 case TYPE_COMPOUND_STRUCT:
657 case TYPE_COMPOUND_UNION:
660 /* TODO: not implemented */
664 /* not sure if this makes sense or is even needed, implement it if you
666 panic("type compatibility check for bitfield type");
669 panic("invalid type found in compatible types");
672 panic("typerefs not skipped in compatible types?!?");
675 /* TODO: incomplete */
679 bool pointers_compatible(const type_t *type1, const type_t *type2)
681 assert(!is_typeref(type1));
682 assert(!is_typeref(type2));
684 assert(type1->kind == TYPE_POINTER);
685 assert(type2->kind == TYPE_POINTER);
690 type_t *skip_typeref(type_t *type)
692 unsigned qualifiers = type->base.qualifiers;
697 qualifiers |= type->base.qualifiers;
698 const typedef_type_t *typedef_type = &type->typedeft;
699 if(typedef_type->resolved_type != NULL) {
700 type = typedef_type->resolved_type;
703 type = typedef_type->declaration->type;
707 const typeof_type_t *typeof_type = &type->typeoft;
708 if(typeof_type->typeof_type != NULL) {
709 type = typeof_type->typeof_type;
711 type = typeof_type->expression->base.datatype;
726 static type_t *identify_new_type(type_t *type)
728 type_t *result = typehash_insert(type);
730 obstack_free(type_obst, type);
735 type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
737 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
738 memset(type, 0, sizeof(atomic_type_t));
740 type->kind = TYPE_ATOMIC;
741 type->base.qualifiers = qualifiers;
742 type->atomic.atype = atype;
744 return identify_new_type(type);
747 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
749 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
750 memset(type, 0, sizeof(pointer_type_t));
752 type->kind = TYPE_POINTER;
753 type->base.qualifiers = qualifiers;
754 type->pointer.points_to = points_to;
756 return identify_new_type(type);
759 static __attribute__((unused))
760 void dbg_type(type_t *type)