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)
44 void print_type_qualifiers(unsigned qualifiers)
46 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
47 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
48 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
52 void print_atomic_type(const atomic_type_t *type)
54 print_type_qualifiers(type->type.qualifiers);
58 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
59 case ATOMIC_TYPE_VOID: s = "void"; break;
60 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
61 case ATOMIC_TYPE_CHAR: s = "char"; break;
62 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
63 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
64 case ATOMIC_TYPE_INT: s = "int"; break;
65 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
66 case ATOMIC_TYPE_SHORT: s = "short"; break;
67 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
68 case ATOMIC_TYPE_LONG: s = "long"; break;
69 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
70 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
71 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
72 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
73 case ATOMIC_TYPE_FLOAT: s = "float"; break;
74 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
75 default: s = "UNKNOWNATOMIC"; break;
80 static void print_function_type_pre(const function_type_t *type)
82 print_type_qualifiers(type->type.qualifiers);
84 intern_print_type_pre(type->result_type);
86 /* TODO: don't emit braces if we're the toplevel type... */
90 static void print_function_type_post(const function_type_t *type,
91 const context_t *context)
93 /* TODO: don't emit braces if we're the toplevel type... */
94 intern_print_type_post(type->result_type);
100 if(context == NULL) {
101 function_parameter_t *parameter = type->parameters;
102 for( ; parameter != NULL; parameter = parameter->next) {
108 print_type(parameter->type);
111 declaration_t *parameter = context->declarations;
112 for( ; parameter != NULL; parameter = parameter->next) {
118 print_type_ext(parameter->type, parameter->symbol,
119 ¶meter->context);
130 if(first && !type->unspecified_parameters) {
136 static void print_pointer_type_pre(const pointer_type_t *type)
138 intern_print_type_pre(type->points_to);
140 print_type_qualifiers(type->type.qualifiers);
143 static void print_pointer_type_post(const pointer_type_t *type)
145 intern_print_type_post(type->points_to);
148 static void print_array_type_pre(const array_type_t *type)
150 intern_print_type_pre(type->element_type);
153 static void print_array_type_post(const array_type_t *type)
155 intern_print_type_post(type->element_type);
157 if(type->is_static) {
158 fputs("static ", out);
160 print_type_qualifiers(type->type.qualifiers);
161 if(type->size != NULL) {
162 print_expression(type->size);
167 void print_enum_definition(const declaration_t *declaration)
173 declaration_t *entry = declaration->next;
174 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
175 entry = entry->next) {
178 fprintf(out, "%s", entry->symbol->string);
179 if(entry->init.initializer != NULL) {
181 print_expression(entry->init.enum_value);
191 static void print_type_enum(const enum_type_t *type)
193 print_type_qualifiers(type->type.qualifiers);
196 declaration_t *declaration = type->declaration;
197 symbol_t *symbol = declaration->symbol;
199 fputs(symbol->string, out);
201 print_enum_definition(declaration);
205 void print_compound_definition(const declaration_t *declaration)
210 declaration_t *iter = declaration->context.declarations;
211 for( ; iter != NULL; iter = iter->next) {
213 print_declaration(iter);
222 static void print_compound_type(const compound_type_t *type)
224 print_type_qualifiers(type->type.qualifiers);
226 if(type->type.type == TYPE_COMPOUND_STRUCT) {
227 fputs("struct ", out);
229 assert(type->type.type == TYPE_COMPOUND_UNION);
230 fputs("union ", out);
233 declaration_t *declaration = type->declaration;
234 symbol_t *symbol = declaration->symbol;
236 fputs(symbol->string, out);
238 print_compound_definition(declaration);
242 static void print_typedef_type_pre(typedef_type_t *type)
244 fputs(type->declaration->symbol->string, out);
247 static void print_typeof_type_pre(typeof_type_t *type)
249 fputs("typeof(", out);
250 if(type->expression != NULL) {
251 assert(type->typeof_type == NULL);
252 print_expression(type->expression);
254 print_type(type->typeof_type);
259 static void intern_print_type_pre(type_t *type)
263 fputs("invalid", out);
266 print_type_enum((enum_type_t*) type);
269 print_atomic_type((atomic_type_t*) type);
271 case TYPE_COMPOUND_STRUCT:
272 case TYPE_COMPOUND_UNION:
273 print_compound_type((compound_type_t*) type);
276 fputs(((builtin_type_t*) type)->symbol->string, out);
279 print_function_type_pre((function_type_t*) type);
282 print_pointer_type_pre((pointer_type_t*) type);
285 print_array_type_pre((array_type_t*) type);
288 print_typedef_type_pre((typedef_type_t*) type);
291 print_typeof_type_pre((typeof_type_t*) type);
294 fputs("unknown", out);
297 static void intern_print_type_post(type_t *type)
301 print_function_type_post((const function_type_t*) type, NULL);
304 print_pointer_type_post((const pointer_type_t*) type);
307 print_array_type_post((const array_type_t*) type);
312 case TYPE_COMPOUND_STRUCT:
313 case TYPE_COMPOUND_UNION:
321 void print_type(type_t *type)
323 print_type_ext(type, NULL, NULL);
326 void print_type_ext(type_t *type, const symbol_t *symbol,
327 const context_t *context)
330 fputs("nil type", out);
334 intern_print_type_pre(type);
337 fputs(symbol->string, out);
339 if(type->type == TYPE_FUNCTION) {
340 print_function_type_post((const function_type_t*) type, context);
342 intern_print_type_post(type);
346 bool type_valid(const type_t *type)
348 return type->type != TYPE_INVALID;
351 bool is_type_integer(const type_t *type)
353 if(type->type == TYPE_ENUM)
356 if(type->type != TYPE_ATOMIC)
359 atomic_type_t *atomic_type = (atomic_type_t*) type;
360 switch(atomic_type->atype) {
361 case ATOMIC_TYPE_BOOL:
362 case ATOMIC_TYPE_CHAR:
363 case ATOMIC_TYPE_SCHAR:
364 case ATOMIC_TYPE_UCHAR:
365 case ATOMIC_TYPE_SHORT:
366 case ATOMIC_TYPE_USHORT:
367 case ATOMIC_TYPE_INT:
368 case ATOMIC_TYPE_UINT:
369 case ATOMIC_TYPE_LONG:
370 case ATOMIC_TYPE_ULONG:
371 case ATOMIC_TYPE_LONGLONG:
372 case ATOMIC_TYPE_ULONGLONG:
379 bool is_type_floating(const type_t *type)
381 if(type->type != TYPE_ATOMIC)
384 atomic_type_t *atomic_type = (atomic_type_t*) type;
385 switch(atomic_type->atype) {
386 case ATOMIC_TYPE_FLOAT:
387 case ATOMIC_TYPE_DOUBLE:
388 case ATOMIC_TYPE_LONG_DOUBLE:
389 #ifdef PROVIDE_COMPLEX
390 case ATOMIC_TYPE_FLOAT_COMPLEX:
391 case ATOMIC_TYPE_DOUBLE_COMPLEX:
392 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
393 case ATOMIC_TYPE_FLOAT_IMAGINARY:
394 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
395 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
403 bool is_type_signed(const type_t *type)
405 /* enum types are int for now */
406 if(type->type == TYPE_ENUM)
409 if(type->type != TYPE_ATOMIC)
412 atomic_type_t *atomic_type = (atomic_type_t*) type;
413 switch(atomic_type->atype) {
414 case ATOMIC_TYPE_CHAR:
415 case ATOMIC_TYPE_SCHAR:
416 case ATOMIC_TYPE_SHORT:
417 case ATOMIC_TYPE_INT:
418 case ATOMIC_TYPE_LONG:
419 case ATOMIC_TYPE_LONGLONG:
420 case ATOMIC_TYPE_FLOAT:
421 case ATOMIC_TYPE_DOUBLE:
422 case ATOMIC_TYPE_LONG_DOUBLE:
423 #ifdef PROVIDE_COMPLEX
424 case ATOMIC_TYPE_FLOAT_COMPLEX:
425 case ATOMIC_TYPE_DOUBLE_COMPLEX:
426 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
427 case ATOMIC_TYPE_FLOAT_IMAGINARY:
428 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
429 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
433 case ATOMIC_TYPE_BOOL:
434 case ATOMIC_TYPE_UCHAR:
435 case ATOMIC_TYPE_USHORT:
436 case ATOMIC_TYPE_UINT:
437 case ATOMIC_TYPE_ULONG:
438 case ATOMIC_TYPE_ULONGLONG:
441 case ATOMIC_TYPE_INVALID:
442 case ATOMIC_TYPE_VOID:
446 panic("invalid atomic type found");
450 bool is_type_arithmetic(const type_t *type)
452 if(is_type_integer(type) || is_type_floating(type))
458 bool is_type_scalar(const type_t *type)
460 if(type->type == TYPE_POINTER)
463 return is_type_arithmetic(type);
466 bool is_type_incomplete(const type_t *type)
469 case TYPE_COMPOUND_STRUCT:
470 case TYPE_COMPOUND_UNION: {
471 const compound_type_t *compound_type
472 = (const compound_type_t*) type;
473 declaration_t *declaration = compound_type->declaration;
474 return !declaration->init.is_defined;
488 panic("is_type_incomplete called without typerefs skipped");
493 panic("invalid type found");
496 bool types_compatible(const type_t *type1, const type_t *type2)
503 bool pointers_compatible(const type_t *type1, const type_t *type2)
505 assert(type1->type == TYPE_POINTER);
506 assert(type2->type == TYPE_POINTER);
507 pointer_type_t *pointer_type1 = (pointer_type_t*) type1;
508 pointer_type_t *pointer_type2 = (pointer_type_t*) type2;
509 return types_compatible(pointer_type1->points_to,
510 pointer_type2->points_to);
513 type_t *skip_typeref(type_t *type)
518 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
519 type = typedef_type->declaration->type;
523 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
524 if(typeof_type->typeof_type != NULL) {
525 type = typeof_type->typeof_type;
527 type = typeof_type->expression->datatype;
532 const builtin_type_t *builtin_type = (const builtin_type_t*) type;
533 type = builtin_type->real_type;
547 static type_t *identify_new_type(type_t *type)
549 type_t *result = typehash_insert(type);
551 obstack_free(type_obst, type);
556 type_t *make_atomic_type(atomic_type_type_t type, type_qualifier_t qualifiers)
558 atomic_type_t *atomic_type
559 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
560 memset(atomic_type, 0, sizeof(atomic_type[0]));
561 atomic_type->type.type = TYPE_ATOMIC;
562 atomic_type->type.qualifiers = qualifiers;
563 atomic_type->atype = type;
565 return identify_new_type((type_t*) atomic_type);
568 type_t *make_pointer_type(type_t *points_to, type_qualifier_t qualifiers)
570 pointer_type_t *pointer_type
571 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
572 memset(pointer_type, 0, sizeof(pointer_type[0]));
573 pointer_type->type.type = TYPE_POINTER;
574 pointer_type->type.qualifiers = qualifiers;
575 pointer_type->points_to = points_to;
577 return identify_new_type((type_t*) pointer_type);
580 static __attribute__((unused))
581 void dbg_type(type_t *type)