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);
49 if(qualifiers & TYPE_QUALIFIER_INLINE) fputs("inline ", out);
53 void print_atomic_type(const atomic_type_t *type)
55 print_type_qualifiers(type->type.qualifiers);
59 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
60 case ATOMIC_TYPE_VOID: s = "void"; break;
61 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
62 case ATOMIC_TYPE_CHAR: s = "char"; break;
63 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
64 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
65 case ATOMIC_TYPE_INT: s = "int"; break;
66 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
67 case ATOMIC_TYPE_SHORT: s = "short"; break;
68 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
69 case ATOMIC_TYPE_LONG: s = "long"; break;
70 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
71 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
72 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
73 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
74 case ATOMIC_TYPE_FLOAT: s = "float"; break;
75 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
76 default: s = "UNKNOWNATOMIC"; break;
81 static void print_function_type_pre(const function_type_t *type)
83 print_type_qualifiers(type->type.qualifiers);
85 intern_print_type_pre(type->result_type);
87 /* TODO: don't emit braces if we're the toplevel type... */
91 static void print_function_type_post(const function_type_t *type,
92 const context_t *context)
94 /* TODO: don't emit braces if we're the toplevel type... */
95 intern_print_type_post(type->result_type);
101 if(context == NULL) {
102 function_parameter_t *parameter = type->parameters;
103 for( ; parameter != NULL; parameter = parameter->next) {
109 print_type(parameter->type);
112 declaration_t *parameter = context->declarations;
113 for( ; parameter != NULL; parameter = parameter->next) {
119 print_type_ext(parameter->type, parameter->symbol,
120 ¶meter->context);
131 if(first && !type->unspecified_parameters) {
138 void print_pointer_type_pre(const pointer_type_t *type)
140 intern_print_type_pre(type->points_to);
142 print_type_qualifiers(type->type.qualifiers);
145 static void print_pointer_type_post(const pointer_type_t *type)
147 intern_print_type_post(type->points_to);
150 static void print_array_type_post(const array_type_t *type)
153 if(type->is_static) {
154 fputs("static ", out);
156 print_type_qualifiers(type->type.qualifiers);
157 if(type->size != NULL) {
158 print_expression(type->size);
163 void print_enum_definition(const declaration_t *declaration)
169 declaration_t *entry = declaration->next;
170 for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
171 entry = entry->next) {
174 fprintf(out, "%s", entry->symbol->string);
175 if(entry->init.initializer != NULL) {
177 print_initializer(entry->init.initializer);
187 static void print_type_enum(const enum_type_t *type)
189 print_type_qualifiers(type->type.qualifiers);
192 declaration_t *declaration = type->declaration;
193 symbol_t *symbol = declaration->symbol;
195 fputs(symbol->string, out);
197 print_enum_definition(declaration);
201 void print_compound_definition(const declaration_t *declaration)
206 declaration_t *iter = declaration->context.declarations;
207 for( ; iter != NULL; iter = iter->next) {
209 print_declaration(iter);
218 static void print_compound_type(const compound_type_t *type)
220 print_type_qualifiers(type->type.qualifiers);
222 if(type->type.type == TYPE_COMPOUND_STRUCT) {
223 fputs("struct ", out);
225 assert(type->type.type == TYPE_COMPOUND_UNION);
226 fputs("union ", out);
229 declaration_t *declaration = type->declaration;
230 symbol_t *symbol = declaration->symbol;
232 fputs(symbol->string, out);
234 print_compound_definition(declaration);
238 static void print_typedef_type_pre(typedef_type_t *type)
240 fputs(type->declaration->symbol->string, out);
243 static void print_typeof_type_pre(typeof_type_t *type)
245 fputs("typeof(", out);
246 if(type->expression != NULL) {
247 assert(type->typeof_type == NULL);
248 print_expression(type->expression);
250 print_type(type->typeof_type);
255 static void intern_print_type_pre(type_t *type)
259 fputs("invalid", out);
262 print_type_enum((enum_type_t*) type);
265 print_atomic_type((atomic_type_t*) type);
267 case TYPE_COMPOUND_STRUCT:
268 case TYPE_COMPOUND_UNION:
269 print_compound_type((compound_type_t*) type);
272 fputs(((builtin_type_t*) type)->symbol->string, out);
275 print_function_type_pre((function_type_t*) type);
278 print_pointer_type_pre((pointer_type_t*) type);
283 print_typedef_type_pre((typedef_type_t*) type);
286 print_typeof_type_pre((typeof_type_t*) type);
289 fputs("unknown", out);
293 void intern_print_type_post(type_t *type)
297 print_function_type_post((const function_type_t*) type, NULL);
300 print_pointer_type_post((const pointer_type_t*) type);
303 print_array_type_post((const array_type_t*) type);
308 case TYPE_COMPOUND_STRUCT:
309 case TYPE_COMPOUND_UNION:
317 void print_type(type_t *type)
319 print_type_ext(type, NULL, NULL);
322 void print_type_ext(type_t *type, const symbol_t *symbol,
323 const context_t *context)
326 fputs("nil type", out);
330 intern_print_type_pre(type);
333 fputs(symbol->string, out);
335 if(type->type == TYPE_FUNCTION) {
336 print_function_type_post((const function_type_t*) type, context);
338 intern_print_type_post(type);
342 bool type_valid(const type_t *type)
344 return type->type != TYPE_INVALID;
347 bool is_type_integer(const type_t *type)
349 if(type->type == TYPE_ENUM)
352 if(type->type != TYPE_ATOMIC)
355 atomic_type_t *atomic_type = (atomic_type_t*) type;
356 switch(atomic_type->atype) {
357 case ATOMIC_TYPE_BOOL:
358 case ATOMIC_TYPE_CHAR:
359 case ATOMIC_TYPE_SCHAR:
360 case ATOMIC_TYPE_UCHAR:
361 case ATOMIC_TYPE_SHORT:
362 case ATOMIC_TYPE_USHORT:
363 case ATOMIC_TYPE_INT:
364 case ATOMIC_TYPE_UINT:
365 case ATOMIC_TYPE_LONG:
366 case ATOMIC_TYPE_ULONG:
367 case ATOMIC_TYPE_LONGLONG:
368 case ATOMIC_TYPE_ULONGLONG:
375 bool is_type_floating(const type_t *type)
377 if(type->type != TYPE_ATOMIC)
380 atomic_type_t *atomic_type = (atomic_type_t*) type;
381 switch(atomic_type->atype) {
382 case ATOMIC_TYPE_FLOAT:
383 case ATOMIC_TYPE_DOUBLE:
384 case ATOMIC_TYPE_LONG_DOUBLE:
385 #ifdef PROVIDE_COMPLEX
386 case ATOMIC_TYPE_FLOAT_COMPLEX:
387 case ATOMIC_TYPE_DOUBLE_COMPLEX:
388 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
390 #ifdef PROVIDE_IMAGINARY
391 case ATOMIC_TYPE_FLOAT_IMAGINARY:
392 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
393 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
401 bool is_type_arithmetic(const type_t *type)
403 if(is_type_integer(type) || is_type_floating(type))
409 bool is_type_scalar(const type_t *type)
411 if(type->type == TYPE_POINTER)
414 return is_type_arithmetic(type);
417 static type_t *identify_new_type(type_t *type)
419 type_t *result = typehash_insert(type);
421 obstack_free(type_obst, type);
426 type_t *make_atomic_type(atomic_type_type_t type, type_qualifier_t qualifiers)
428 atomic_type_t *atomic_type
429 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
430 memset(atomic_type, 0, sizeof(atomic_type[0]));
431 atomic_type->type.type = TYPE_ATOMIC;
432 atomic_type->type.qualifiers = qualifiers;
433 atomic_type->atype = type;
435 return identify_new_type((type_t*) atomic_type);
438 type_t *make_pointer_type(type_t *points_to, type_qualifier_t qualifiers)
440 pointer_type_t *pointer_type
441 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
442 memset(pointer_type, 0, sizeof(pointer_type[0]));
443 pointer_type->type.type = TYPE_POINTER;
444 pointer_type->type.qualifiers = qualifiers;
445 pointer_type->points_to = points_to;
447 return identify_new_type((type_t*) pointer_type);
450 static __attribute__((unused))
451 void dbg_type(type_t *type)