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(type_t *type);
15 static void intern_print_type_post(type_t *type);
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)
75 print_type_qualifiers(type->type.qualifiers);
77 intern_print_type_pre(type->result_type);
79 /* TODO: don't emit braces if we're the toplevel type... */
83 static void print_function_type_post(const function_type_t *type,
84 const context_t *context)
86 /* TODO: don't emit braces if we're the toplevel type... */
87 intern_print_type_post(type->result_type);
94 function_parameter_t *parameter = type->parameters;
95 for( ; parameter != NULL; parameter = parameter->next) {
101 print_type(parameter->type);
104 declaration_t *parameter = context->declarations;
105 for( ; parameter != NULL; parameter = parameter->next) {
111 print_type_ext(parameter->type, parameter->symbol,
112 ¶meter->context);
123 if(first && !type->unspecified_parameters) {
129 static void print_pointer_type_pre(const pointer_type_t *type)
131 intern_print_type_pre(type->points_to);
133 print_type_qualifiers(type->type.qualifiers);
136 static void print_pointer_type_post(const pointer_type_t *type)
138 intern_print_type_post(type->points_to);
141 static void print_array_type_pre(const array_type_t *type)
143 intern_print_type_pre(type->element_type);
146 static void print_array_type_post(const array_type_t *type)
149 if(type->is_static) {
150 fputs("static ", out);
152 print_type_qualifiers(type->type.qualifiers);
153 if(type->size != NULL) {
154 print_expression(type->size);
157 intern_print_type_post(type->element_type);
160 void print_enum_definition(const declaration_t *declaration)
166 declaration_t *entry = declaration->next;
167 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
168 entry = entry->next) {
171 fprintf(out, "%s", entry->symbol->string);
172 if(entry->init.initializer != NULL) {
174 print_expression(entry->init.enum_value);
184 static void print_type_enum(const enum_type_t *type)
186 print_type_qualifiers(type->type.qualifiers);
189 declaration_t *declaration = type->declaration;
190 symbol_t *symbol = declaration->symbol;
192 fputs(symbol->string, out);
194 print_enum_definition(declaration);
198 void print_compound_definition(const declaration_t *declaration)
203 declaration_t *iter = declaration->context.declarations;
204 for( ; iter != NULL; iter = iter->next) {
206 print_declaration(iter);
215 static void print_compound_type(const compound_type_t *type)
217 print_type_qualifiers(type->type.qualifiers);
219 if(type->type.type == TYPE_COMPOUND_STRUCT) {
220 fputs("struct ", out);
222 assert(type->type.type == TYPE_COMPOUND_UNION);
223 fputs("union ", out);
226 declaration_t *declaration = type->declaration;
227 symbol_t *symbol = declaration->symbol;
229 fputs(symbol->string, out);
231 print_compound_definition(declaration);
235 static void print_typedef_type_pre(typedef_type_t *type)
237 fputs(type->declaration->symbol->string, out);
240 static void print_typeof_type_pre(typeof_type_t *type)
242 fputs("typeof(", out);
243 if(type->expression != NULL) {
244 assert(type->typeof_type == NULL);
245 print_expression(type->expression);
247 print_type(type->typeof_type);
252 static void intern_print_type_pre(type_t *type)
256 fputs("invalid", out);
259 print_type_enum(&type->enumt);
262 print_atomic_type(&type->atomic);
264 case TYPE_COMPOUND_STRUCT:
265 case TYPE_COMPOUND_UNION:
266 print_compound_type(&type->compound);
269 fputs(type->builtin.symbol->string, out);
272 print_function_type_pre(&type->function);
275 print_pointer_type_pre(&type->pointer);
278 print_array_type_pre(&type->array);
281 print_typedef_type_pre(&type->typedeft);
284 print_typeof_type_pre(&type->typeoft);
287 fputs("unknown", out);
290 static void intern_print_type_post(type_t *type)
294 print_function_type_post(&type->function, NULL);
297 print_pointer_type_post(&type->pointer);
300 print_array_type_post(&type->array);
305 case TYPE_COMPOUND_STRUCT:
306 case TYPE_COMPOUND_UNION:
314 void print_type(type_t *type)
316 print_type_ext(type, NULL, NULL);
319 void print_type_ext(type_t *type, const symbol_t *symbol,
320 const context_t *context)
323 fputs("nil type", out);
327 intern_print_type_pre(type);
330 fputs(symbol->string, out);
332 if(type->type == TYPE_FUNCTION) {
333 print_function_type_post((const function_type_t*) type, context);
335 intern_print_type_post(type);
339 bool type_valid(const type_t *type)
341 return type->type != TYPE_INVALID;
344 bool is_type_integer(const type_t *type)
346 assert(!is_typeref(type));
348 if(type->type == TYPE_ENUM)
351 if(type->type != TYPE_ATOMIC)
354 switch(type->atomic.atype) {
355 case ATOMIC_TYPE_BOOL:
356 case ATOMIC_TYPE_CHAR:
357 case ATOMIC_TYPE_SCHAR:
358 case ATOMIC_TYPE_UCHAR:
359 case ATOMIC_TYPE_SHORT:
360 case ATOMIC_TYPE_USHORT:
361 case ATOMIC_TYPE_INT:
362 case ATOMIC_TYPE_UINT:
363 case ATOMIC_TYPE_LONG:
364 case ATOMIC_TYPE_ULONG:
365 case ATOMIC_TYPE_LONGLONG:
366 case ATOMIC_TYPE_ULONGLONG:
373 bool is_type_floating(const type_t *type)
375 assert(!is_typeref(type));
377 if(type->type != TYPE_ATOMIC)
380 switch(type->atomic.atype) {
381 case ATOMIC_TYPE_FLOAT:
382 case ATOMIC_TYPE_DOUBLE:
383 case ATOMIC_TYPE_LONG_DOUBLE:
384 #ifdef PROVIDE_COMPLEX
385 case ATOMIC_TYPE_FLOAT_COMPLEX:
386 case ATOMIC_TYPE_DOUBLE_COMPLEX:
387 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
388 case ATOMIC_TYPE_FLOAT_IMAGINARY:
389 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
390 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
398 bool is_type_signed(const type_t *type)
400 assert(!is_typeref(type));
402 /* enum types are int for now */
403 if(type->type == TYPE_ENUM)
406 if(type->type != TYPE_ATOMIC)
409 switch(type->atomic.atype) {
410 case ATOMIC_TYPE_CHAR:
411 case ATOMIC_TYPE_SCHAR:
412 case ATOMIC_TYPE_SHORT:
413 case ATOMIC_TYPE_INT:
414 case ATOMIC_TYPE_LONG:
415 case ATOMIC_TYPE_LONGLONG:
416 case ATOMIC_TYPE_FLOAT:
417 case ATOMIC_TYPE_DOUBLE:
418 case ATOMIC_TYPE_LONG_DOUBLE:
419 #ifdef PROVIDE_COMPLEX
420 case ATOMIC_TYPE_FLOAT_COMPLEX:
421 case ATOMIC_TYPE_DOUBLE_COMPLEX:
422 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
423 case ATOMIC_TYPE_FLOAT_IMAGINARY:
424 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
425 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
429 case ATOMIC_TYPE_BOOL:
430 case ATOMIC_TYPE_UCHAR:
431 case ATOMIC_TYPE_USHORT:
432 case ATOMIC_TYPE_UINT:
433 case ATOMIC_TYPE_ULONG:
434 case ATOMIC_TYPE_ULONGLONG:
437 case ATOMIC_TYPE_INVALID:
438 case ATOMIC_TYPE_VOID:
442 panic("invalid atomic type found");
446 bool is_type_arithmetic(const type_t *type)
448 assert(!is_typeref(type));
450 if(is_type_integer(type) || is_type_floating(type))
456 bool is_type_scalar(const type_t *type)
458 assert(!is_typeref(type));
460 if(type->type == TYPE_POINTER)
463 return is_type_arithmetic(type);
466 bool is_type_incomplete(const type_t *type)
468 assert(!is_typeref(type));
471 case TYPE_COMPOUND_STRUCT:
472 case TYPE_COMPOUND_UNION: {
473 const compound_type_t *compound_type = &type->compound;
474 declaration_t *declaration = compound_type->declaration;
475 return !declaration->init.is_defined;
481 return type->array.size == NULL;
491 panic("is_type_incomplete called without typerefs skipped");
496 panic("invalid type found");
499 bool types_compatible(const type_t *type1, const type_t *type2)
501 assert(!is_typeref(type1));
502 assert(!is_typeref(type2));
504 /* TODO: really incomplete */
508 if(type1->type == TYPE_ATOMIC && type2->type == TYPE_ATOMIC) {
509 return type1->atomic.atype == type2->atomic.atype;
515 bool pointers_compatible(const type_t *type1, const type_t *type2)
517 assert(!is_typeref(type1));
518 assert(!is_typeref(type2));
520 assert(type1->type == TYPE_POINTER);
521 assert(type2->type == TYPE_POINTER);
526 static size_t get_type_size(type_t *type)
529 case TYPE_ATOMIC: return sizeof(atomic_type_t);
530 case TYPE_COMPOUND_STRUCT:
531 case TYPE_COMPOUND_UNION: return sizeof(compound_type_t);
532 case TYPE_ENUM: return sizeof(enum_type_t);
533 case TYPE_FUNCTION: return sizeof(function_type_t);
534 case TYPE_POINTER: return sizeof(pointer_type_t);
535 case TYPE_ARRAY: return sizeof(array_type_t);
536 case TYPE_BUILTIN: return sizeof(builtin_type_t);
537 case TYPE_TYPEDEF: return sizeof(typedef_type_t);
538 case TYPE_TYPEOF: return sizeof(typeof_type_t);
539 case TYPE_INVALID: panic("invalid type found");
541 panic("unknown type found");
546 * note that this does not produce a deep copy!
548 type_t *duplicate_type(type_t *type)
550 size_t size = get_type_size(type);
552 type_t *copy = obstack_alloc(type_obst, size);
553 memcpy(copy, type, size);
558 type_t *skip_typeref(type_t *type)
560 unsigned qualifiers = type->base.qualifiers;
565 qualifiers |= type->base.qualifiers;
566 const typedef_type_t *typedef_type = &type->typedeft;
567 if(typedef_type->resolved_type != NULL) {
568 type = typedef_type->resolved_type;
571 type = typedef_type->declaration->type;
575 const typeof_type_t *typeof_type = &type->typeoft;
576 if(typeof_type->typeof_type != NULL) {
577 type = typeof_type->typeof_type;
579 type = typeof_type->expression->base.datatype;
584 const builtin_type_t *builtin_type = &type->builtin;
585 type = builtin_type->real_type;
599 static type_t *identify_new_type(type_t *type)
601 type_t *result = typehash_insert(type);
603 obstack_free(type_obst, type);
608 type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
610 type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
611 memset(type, 0, sizeof(atomic_type_t));
613 type->type = TYPE_ATOMIC;
614 type->base.qualifiers = qualifiers;
615 type->atomic.atype = atype;
617 return identify_new_type(type);
620 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
622 type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
623 memset(type, 0, sizeof(pointer_type_t));
625 type->type = TYPE_POINTER;
626 type->base.qualifiers = qualifiers;
627 type->pointer.points_to = points_to;
629 return identify_new_type(type);
632 static __attribute__((unused))
633 void dbg_type(type_t *type)