9 static struct obstack _type_obst;
10 struct obstack *type_obst = &_type_obst;
13 static void intern_print_type_pre(const type_t *type);
14 static void intern_print_type_post(const type_t *type);
18 obstack_init(type_obst);
23 obstack_free(type_obst, NULL);
26 void type_set_output(FILE *stream)
32 void print_type_qualifiers(unsigned qualifiers)
34 if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
35 if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
36 if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
37 if(qualifiers & TYPE_QUALIFIER_INLINE) fputs("inline ", out);
41 void print_atomic_type(const atomic_type_t *type)
43 print_type_qualifiers(type->type.qualifiers);
47 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
48 case ATOMIC_TYPE_VOID: s = "void"; break;
49 case ATOMIC_TYPE_BOOL: s = "_Bool"; break;
50 case ATOMIC_TYPE_CHAR: s = "char"; break;
51 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
52 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
53 case ATOMIC_TYPE_INT: s = "int"; break;
54 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
55 case ATOMIC_TYPE_SHORT: s = "short"; break;
56 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
57 case ATOMIC_TYPE_LONG: s = "long"; break;
58 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
59 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
60 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
61 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
62 case ATOMIC_TYPE_FLOAT: s = "float"; break;
63 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
64 default: s = "UNKNOWNATOMIC"; break;
70 void print_method_type_pre(const method_type_t *type)
72 print_type_qualifiers(type->type.qualifiers);
74 intern_print_type_pre(type->result_type);
76 /* TODO: don't emit braces if we're the toplevel type... */
81 void print_method_type_post(const method_type_t *type, const context_t *context)
83 /* TODO: don't emit braces if we're the toplevel type... */
84 intern_print_type_post(type->result_type);
91 method_parameter_t *parameter = type->parameters;
92 for( ; parameter != NULL; parameter = parameter->next) {
98 print_type(parameter->type);
101 declaration_t *parameter = context->declarations;
102 for( ; parameter != NULL; parameter = parameter->next) {
108 print_type_ext(parameter->type, parameter->symbol,
109 ¶meter->context);
120 if(first && !type->unspecified_parameters) {
127 void print_pointer_type_pre(const pointer_type_t *type)
129 intern_print_type_pre(type->points_to);
131 print_type_qualifiers(type->type.qualifiers);
134 static void print_pointer_type_post(const pointer_type_t *type)
136 intern_print_type_post(type->points_to);
139 static void print_array_type_post(const array_type_t *type)
142 if(type->is_static) {
143 fputs("static ", out);
145 print_type_qualifiers(type->type.qualifiers);
146 if(type->size != NULL) {
147 print_expression(type->size);
152 static void print_type_enum(const enum_type_t *type)
154 print_type_qualifiers(type->type.qualifiers);
155 if(type->symbol != NULL) {
156 fprintf(out, "enum %s", type->symbol->string);
158 fprintf(out, "enum {\n");
160 declaration_t *entry = type->entries_begin;
161 for( ; entry != type->entries_end->next; entry = entry->next) {
162 fprintf(out, "\t%s", entry->symbol->string);
163 if(entry->initializer != NULL) {
165 print_initializer(entry->initializer);
174 static void intern_print_type_pre(const type_t *type)
178 fputs("invalid", out);
181 print_type_enum((const enum_type_t*) type);
184 print_atomic_type((const atomic_type_t*) type);
186 case TYPE_COMPOUND_STRUCT:
187 case TYPE_COMPOUND_UNION:
188 print_type_qualifiers(type->qualifiers);
189 if(((const compound_type_t*) type)->symbol != NULL) {
190 fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
194 fputs(((builtin_type_t*) type)->symbol->string, out);
197 print_method_type_pre((const method_type_t*) type);
200 print_pointer_type_pre((const pointer_type_t*) type);
205 fputs("unknown", out);
209 void intern_print_type_post(const type_t *type)
213 print_method_type_post((const method_type_t*) type, NULL);
216 print_pointer_type_post((const pointer_type_t*) type);
219 print_array_type_post((const array_type_t*) type);
224 case TYPE_COMPOUND_STRUCT:
225 case TYPE_COMPOUND_UNION:
231 void print_type(const type_t *type)
233 print_type_ext(type, NULL, NULL);
236 void print_type_ext(const type_t *type, const symbol_t *symbol,
237 const context_t *context)
240 fputs("nil type", out);
244 intern_print_type_pre(type);
247 fputs(symbol->string, out);
249 if(type->type == TYPE_METHOD) {
250 print_method_type_post((const method_type_t*) type, context);
252 intern_print_type_post(type);
256 bool type_valid(const type_t *type)
258 return type->type != TYPE_INVALID;
261 bool is_type_integer(const type_t *type)
263 if(type->type == TYPE_ENUM)
266 if(type->type != TYPE_ATOMIC)
269 atomic_type_t *atomic_type = (atomic_type_t*) type;
270 switch(atomic_type->atype) {
271 case ATOMIC_TYPE_BOOL:
272 case ATOMIC_TYPE_CHAR:
273 case ATOMIC_TYPE_SCHAR:
274 case ATOMIC_TYPE_UCHAR:
275 case ATOMIC_TYPE_SHORT:
276 case ATOMIC_TYPE_USHORT:
277 case ATOMIC_TYPE_INT:
278 case ATOMIC_TYPE_UINT:
279 case ATOMIC_TYPE_LONG:
280 case ATOMIC_TYPE_ULONG:
281 case ATOMIC_TYPE_LONGLONG:
282 case ATOMIC_TYPE_ULONGLONG:
289 bool is_type_floating(const type_t *type)
291 if(type->type != TYPE_ATOMIC)
294 atomic_type_t *atomic_type = (atomic_type_t*) type;
295 switch(atomic_type->atype) {
296 case ATOMIC_TYPE_FLOAT:
297 case ATOMIC_TYPE_DOUBLE:
298 case ATOMIC_TYPE_LONG_DOUBLE:
299 #ifdef PROVIDE_COMPLEX
300 case ATOMIC_TYPE_FLOAT_COMPLEX:
301 case ATOMIC_TYPE_DOUBLE_COMPLEX:
302 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
304 #ifdef PROVIDE_IMAGINARY
305 case ATOMIC_TYPE_FLOAT_IMAGINARY:
306 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
307 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
315 bool is_type_arithmetic(const type_t *type)
317 if(is_type_integer(type) || is_type_floating(type))
323 bool is_type_scalar(const type_t *type)
325 if(type->type == TYPE_POINTER)
328 return is_type_arithmetic(type);
331 static type_t *identify_new_type(type_t *type)
333 type_t *result = typehash_insert(type);
335 obstack_free(type_obst, type);
340 type_t *make_atomic_type(atomic_type_type_t type, type_qualifier_t qualifiers)
342 atomic_type_t *atomic_type
343 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
344 memset(atomic_type, 0, sizeof(atomic_type[0]));
345 atomic_type->type.type = TYPE_ATOMIC;
346 atomic_type->type.qualifiers = qualifiers;
347 atomic_type->atype = type;
349 return identify_new_type((type_t*) atomic_type);
352 type_t *make_pointer_type(type_t *points_to, type_qualifier_t qualifiers)
354 pointer_type_t *pointer_type
355 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
356 memset(pointer_type, 0, sizeof(pointer_type[0]));
357 pointer_type->type.type = TYPE_POINTER;
358 pointer_type->type.qualifiers = qualifiers;
359 pointer_type->points_to = points_to;
361 return identify_new_type((type_t*) pointer_type);
364 static __attribute__((unused))
365 void dbg_type(const type_t *type)