9 static struct obstack _type_obst;
10 struct obstack *type_obst = &_type_obst;
12 static int type_visited = 0;
13 static bool print_compound_entries;
15 static void intern_print_type_pre(type_t *type);
16 static void intern_print_type_post(type_t *type);
20 obstack_init(type_obst);
25 obstack_free(type_obst, NULL);
28 void type_set_output(FILE *stream)
33 void set_print_compound_entries(bool enabled)
35 print_compound_entries = enabled;
38 void inc_type_visited(void)
43 void print_type_qualifiers(unsigned qualifiers)
45 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
46 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
47 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
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;
79 static void print_function_type_pre(const function_type_t *type)
81 print_type_qualifiers(type->type.qualifiers);
83 intern_print_type_pre(type->result_type);
85 /* TODO: don't emit braces if we're the toplevel type... */
89 static void print_function_type_post(const function_type_t *type,
90 const context_t *context)
92 /* TODO: don't emit braces if we're the toplevel type... */
93 intern_print_type_post(type->result_type);
100 function_parameter_t *parameter = type->parameters;
101 for( ; parameter != NULL; parameter = parameter->next) {
107 print_type(parameter->type);
110 declaration_t *parameter = context->declarations;
111 for( ; parameter != NULL; parameter = parameter->next) {
117 print_type_ext(parameter->type, parameter->symbol,
118 ¶meter->context);
129 if(first && !type->unspecified_parameters) {
135 static void print_pointer_type_pre(const pointer_type_t *type)
137 intern_print_type_pre(type->points_to);
139 print_type_qualifiers(type->type.qualifiers);
142 static void print_pointer_type_post(const pointer_type_t *type)
144 intern_print_type_post(type->points_to);
147 static void print_array_type_pre(const array_type_t *type)
149 intern_print_type_pre(type->element_type);
152 static void print_array_type_post(const array_type_t *type)
155 if(type->is_static) {
156 fputs("static ", out);
158 print_type_qualifiers(type->type.qualifiers);
159 if(type->size != NULL) {
160 print_expression(type->size);
163 intern_print_type_post(type->element_type);
166 void print_enum_definition(const declaration_t *declaration)
172 declaration_t *entry = declaration->next;
173 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
174 entry = entry->next) {
177 fprintf(out, "%s", entry->symbol->string);
178 if(entry->init.initializer != NULL) {
180 print_expression(entry->init.enum_value);
190 static void print_type_enum(const enum_type_t *type)
192 print_type_qualifiers(type->type.qualifiers);
195 declaration_t *declaration = type->declaration;
196 symbol_t *symbol = declaration->symbol;
198 fputs(symbol->string, out);
200 print_enum_definition(declaration);
204 void print_compound_definition(const declaration_t *declaration)
209 declaration_t *iter = declaration->context.declarations;
210 for( ; iter != NULL; iter = iter->next) {
212 print_declaration(iter);
221 static void print_compound_type(const compound_type_t *type)
223 print_type_qualifiers(type->type.qualifiers);
225 if(type->type.type == TYPE_COMPOUND_STRUCT) {
226 fputs("struct ", out);
228 assert(type->type.type == TYPE_COMPOUND_UNION);
229 fputs("union ", out);
232 declaration_t *declaration = type->declaration;
233 symbol_t *symbol = declaration->symbol;
235 fputs(symbol->string, out);
237 print_compound_definition(declaration);
241 static void print_typedef_type_pre(typedef_type_t *type)
243 fputs(type->declaration->symbol->string, out);
246 static void print_typeof_type_pre(typeof_type_t *type)
248 fputs("typeof(", out);
249 if(type->expression != NULL) {
250 assert(type->typeof_type == NULL);
251 print_expression(type->expression);
253 print_type(type->typeof_type);
258 static void intern_print_type_pre(type_t *type)
262 fputs("invalid", out);
265 print_type_enum((enum_type_t*) type);
268 print_atomic_type((atomic_type_t*) type);
270 case TYPE_COMPOUND_STRUCT:
271 case TYPE_COMPOUND_UNION:
272 print_compound_type((compound_type_t*) type);
275 fputs(((builtin_type_t*) type)->symbol->string, out);
278 print_function_type_pre((function_type_t*) type);
281 print_pointer_type_pre((pointer_type_t*) type);
284 print_array_type_pre((array_type_t*) type);
287 print_typedef_type_pre((typedef_type_t*) type);
290 print_typeof_type_pre((typeof_type_t*) type);
293 fputs("unknown", out);
296 static void intern_print_type_post(type_t *type)
300 print_function_type_post((const function_type_t*) type, NULL);
303 print_pointer_type_post((const pointer_type_t*) type);
306 print_array_type_post((const array_type_t*) type);
311 case TYPE_COMPOUND_STRUCT:
312 case TYPE_COMPOUND_UNION:
320 void print_type(type_t *type)
322 print_type_ext(type, NULL, NULL);
325 void print_type_ext(type_t *type, const symbol_t *symbol,
326 const context_t *context)
329 fputs("nil type", out);
333 intern_print_type_pre(type);
336 fputs(symbol->string, out);
338 if(type->type == TYPE_FUNCTION) {
339 print_function_type_post((const function_type_t*) type, context);
341 intern_print_type_post(type);
345 bool type_valid(const type_t *type)
347 return type->type != TYPE_INVALID;
350 bool is_type_integer(const type_t *type)
352 assert(!is_typeref(type));
354 if(type->type == TYPE_ENUM)
357 if(type->type != TYPE_ATOMIC)
360 atomic_type_t *atomic_type = (atomic_type_t*) type;
361 switch(atomic_type->atype) {
362 case ATOMIC_TYPE_BOOL:
363 case ATOMIC_TYPE_CHAR:
364 case ATOMIC_TYPE_SCHAR:
365 case ATOMIC_TYPE_UCHAR:
366 case ATOMIC_TYPE_SHORT:
367 case ATOMIC_TYPE_USHORT:
368 case ATOMIC_TYPE_INT:
369 case ATOMIC_TYPE_UINT:
370 case ATOMIC_TYPE_LONG:
371 case ATOMIC_TYPE_ULONG:
372 case ATOMIC_TYPE_LONGLONG:
373 case ATOMIC_TYPE_ULONGLONG:
380 bool is_type_floating(const type_t *type)
382 assert(!is_typeref(type));
384 if(type->type != TYPE_ATOMIC)
387 atomic_type_t *atomic_type = (atomic_type_t*) type;
388 switch(atomic_type->atype) {
389 case ATOMIC_TYPE_FLOAT:
390 case ATOMIC_TYPE_DOUBLE:
391 case ATOMIC_TYPE_LONG_DOUBLE:
392 #ifdef PROVIDE_COMPLEX
393 case ATOMIC_TYPE_FLOAT_COMPLEX:
394 case ATOMIC_TYPE_DOUBLE_COMPLEX:
395 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
396 case ATOMIC_TYPE_FLOAT_IMAGINARY:
397 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
398 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
406 bool is_type_signed(const type_t *type)
408 assert(!is_typeref(type));
410 /* enum types are int for now */
411 if(type->type == TYPE_ENUM)
414 if(type->type != TYPE_ATOMIC)
417 atomic_type_t *atomic_type = (atomic_type_t*) type;
418 switch(atomic_type->atype) {
419 case ATOMIC_TYPE_CHAR:
420 case ATOMIC_TYPE_SCHAR:
421 case ATOMIC_TYPE_SHORT:
422 case ATOMIC_TYPE_INT:
423 case ATOMIC_TYPE_LONG:
424 case ATOMIC_TYPE_LONGLONG:
425 case ATOMIC_TYPE_FLOAT:
426 case ATOMIC_TYPE_DOUBLE:
427 case ATOMIC_TYPE_LONG_DOUBLE:
428 #ifdef PROVIDE_COMPLEX
429 case ATOMIC_TYPE_FLOAT_COMPLEX:
430 case ATOMIC_TYPE_DOUBLE_COMPLEX:
431 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
432 case ATOMIC_TYPE_FLOAT_IMAGINARY:
433 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
434 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
438 case ATOMIC_TYPE_BOOL:
439 case ATOMIC_TYPE_UCHAR:
440 case ATOMIC_TYPE_USHORT:
441 case ATOMIC_TYPE_UINT:
442 case ATOMIC_TYPE_ULONG:
443 case ATOMIC_TYPE_ULONGLONG:
446 case ATOMIC_TYPE_INVALID:
447 case ATOMIC_TYPE_VOID:
451 panic("invalid atomic type found");
455 bool is_type_arithmetic(const type_t *type)
457 assert(!is_typeref(type));
459 if(is_type_integer(type) || is_type_floating(type))
465 bool is_type_scalar(const type_t *type)
467 assert(!is_typeref(type));
469 if(type->type == TYPE_POINTER)
472 return is_type_arithmetic(type);
475 bool is_type_incomplete(const type_t *type)
477 assert(!is_typeref(type));
480 case TYPE_COMPOUND_STRUCT:
481 case TYPE_COMPOUND_UNION: {
482 const compound_type_t *compound_type
483 = (const compound_type_t*) type;
484 declaration_t *declaration = compound_type->declaration;
485 return !declaration->init.is_defined;
491 const array_type_t *array_type = (const array_type_t*) type;
493 return array_type->size == NULL;
504 panic("is_type_incomplete called without typerefs skipped");
509 panic("invalid type found");
512 bool types_compatible(const type_t *type1, const type_t *type2)
514 assert(!is_typeref(type1));
515 assert(!is_typeref(type2));
517 /* TODO: really incomplete */
521 if(type1->type == TYPE_ATOMIC && type2->type == TYPE_ATOMIC) {
522 const atomic_type_t *atomic1 = (const atomic_type_t*) type1;
523 const atomic_type_t *atomic2 = (const atomic_type_t*) type2;
525 return atomic1->atype == atomic2->atype;
531 bool pointers_compatible(const type_t *type1, const type_t *type2)
533 assert(!is_typeref(type1));
534 assert(!is_typeref(type2));
536 assert(type1->type == TYPE_POINTER);
537 assert(type2->type == TYPE_POINTER);
539 pointer_type_t *pointer_type1 = (pointer_type_t*) type1;
540 pointer_type_t *pointer_type2 = (pointer_type_t*) type2;
541 return types_compatible(pointer_type1->points_to,
542 pointer_type2->points_to);
547 static size_t get_type_size(type_t *type)
550 case TYPE_ATOMIC: return sizeof(atomic_type_t); break;
551 case TYPE_COMPOUND_STRUCT:
552 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t); break;
553 case TYPE_ENUM: return sizeof(enum_type_t); break;
554 case TYPE_FUNCTION: return sizeof(function_type_t); break;
555 case TYPE_POINTER: return sizeof(pointer_type_t); break;
556 case TYPE_ARRAY: return sizeof(array_type_t); break;
557 case TYPE_BUILTIN: return sizeof(builtin_type_t); break;
558 case TYPE_TYPEDEF: return sizeof(typedef_type_t); break;
559 case TYPE_TYPEOF: return sizeof(typeof_type_t); break;
560 case TYPE_INVALID: panic("invalid type found"); break;
562 panic("unknown type found");
567 * note that this does not produce a deep copy!
569 static type_t *duplicate_type(type_t *type)
571 size_t size = get_type_size(type);
573 type_t *copy = obstack_alloc(type_obst, size);
574 memcpy(copy, type, size);
576 (void) duplicate_type;
581 type_t *skip_typeref(type_t *type)
583 unsigned qualifiers = type->qualifiers;
588 qualifiers |= type->qualifiers;
589 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
590 if(typedef_type->resolved_type != NULL) {
591 type = typedef_type->resolved_type;
594 type = typedef_type->declaration->type;
598 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
599 if(typeof_type->typeof_type != NULL) {
600 type = typeof_type->typeof_type;
602 type = typeof_type->expression->datatype;
607 const builtin_type_t *builtin_type = (const builtin_type_t*) type;
608 type = builtin_type->real_type;
622 static type_t *identify_new_type(type_t *type)
624 type_t *result = typehash_insert(type);
626 obstack_free(type_obst, type);
631 type_t *make_atomic_type(atomic_type_type_t type, type_qualifiers_t qualifiers)
633 atomic_type_t *atomic_type
634 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
635 memset(atomic_type, 0, sizeof(atomic_type[0]));
636 atomic_type->type.type = TYPE_ATOMIC;
637 atomic_type->type.qualifiers = qualifiers;
638 atomic_type->atype = type;
640 return identify_new_type((type_t*) atomic_type);
643 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
645 pointer_type_t *pointer_type
646 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
647 memset(pointer_type, 0, sizeof(pointer_type[0]));
648 pointer_type->type.type = TYPE_POINTER;
649 pointer_type->type.qualifiers = qualifiers;
650 pointer_type->points_to = points_to;
652 return identify_new_type((type_t*) pointer_type);
655 static __attribute__((unused))
656 void dbg_type(type_t *type)