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_expression(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_int(const type_t *type)
263 if(type->type != TYPE_ATOMIC)
266 atomic_type_t *atomic_type = (atomic_type_t*) type;
267 switch(atomic_type->atype) {
268 case ATOMIC_TYPE_CHAR:
269 case ATOMIC_TYPE_SCHAR:
270 case ATOMIC_TYPE_UCHAR:
271 case ATOMIC_TYPE_SHORT:
272 case ATOMIC_TYPE_USHORT:
273 case ATOMIC_TYPE_INT:
274 case ATOMIC_TYPE_UINT:
275 case ATOMIC_TYPE_LONG:
276 case ATOMIC_TYPE_ULONG:
277 case ATOMIC_TYPE_LONGLONG:
278 case ATOMIC_TYPE_ULONGLONG:
285 bool is_type_arithmetic(const type_t *type)
287 if(type->type != TYPE_ATOMIC)
290 atomic_type_t *atomic_type = (atomic_type_t*) type;
291 switch(atomic_type->atype) {
292 case ATOMIC_TYPE_CHAR:
293 case ATOMIC_TYPE_SCHAR:
294 case ATOMIC_TYPE_UCHAR:
295 case ATOMIC_TYPE_SHORT:
296 case ATOMIC_TYPE_USHORT:
297 case ATOMIC_TYPE_INT:
298 case ATOMIC_TYPE_UINT:
299 case ATOMIC_TYPE_LONG:
300 case ATOMIC_TYPE_ULONG:
301 case ATOMIC_TYPE_LONGLONG:
302 case ATOMIC_TYPE_ULONGLONG:
303 case ATOMIC_TYPE_FLOAT:
304 case ATOMIC_TYPE_DOUBLE:
305 case ATOMIC_TYPE_LONG_DOUBLE:
306 #ifdef PROVIDE_COMPLEX
307 case ATOMIC_TYPE_FLOAT_COMPLEX:
308 case ATOMIC_TYPE_DOUBLE_COMPLEX:
309 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
311 #ifdef PROVIDE_IMAGINARY
312 case ATOMIC_TYPE_FLOAT_IMAGINARY:
313 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
314 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
317 case ATOMIC_TYPE_INVALID:
318 case ATOMIC_TYPE_VOID:
319 case ATOMIC_TYPE_BOOL:
326 bool is_type_scalar(const type_t *type)
328 if(type->type == TYPE_POINTER)
331 return is_type_arithmetic(type);
334 static type_t *identify_new_type(type_t *type)
336 type_t *result = typehash_insert(type);
338 obstack_free(type_obst, type);
343 type_t *make_atomic_type(atomic_type_type_t type, unsigned qualifiers)
345 atomic_type_t *atomic_type
346 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
347 memset(atomic_type, 0, sizeof(atomic_type[0]));
348 atomic_type->type.type = TYPE_ATOMIC;
349 atomic_type->type.qualifiers = qualifiers;
350 atomic_type->atype = type;
352 return identify_new_type((type_t*) atomic_type);
355 type_t *make_pointer_type(type_t *points_to, unsigned qualifiers)
357 pointer_type_t *pointer_type
358 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
359 memset(pointer_type, 0, sizeof(pointer_type[0]));
360 pointer_type->type.type = TYPE_POINTER;
361 pointer_type->type.qualifiers = qualifiers;
362 pointer_type->points_to = points_to;
364 return identify_new_type((type_t*) pointer_type);
367 static __attribute__((unused))
368 void dbg_type(const type_t *type)