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) {
37 if(qualifiers & TYPE_QUALIFIER_VOLATILE) {
38 fputs("volatile ", out);
40 if(qualifiers & TYPE_QUALIFIER_RESTRICT) {
41 fputs("__restrict ", out);
43 if(qualifiers & TYPE_QUALIFIER_INLINE) {
44 fputs("inline ", out);
49 void print_atomic_type(const atomic_type_t *type)
51 print_type_qualifiers(type->type.qualifiers);
55 case ATOMIC_TYPE_INVALID: s = "INVALIDATOMIC"; break;
56 case ATOMIC_TYPE_VOID: s = "void"; break;
57 case ATOMIC_TYPE_BOOL: s = "bool"; break;
58 case ATOMIC_TYPE_CHAR: s = "char"; break;
59 case ATOMIC_TYPE_SCHAR: s = "signed char"; break;
60 case ATOMIC_TYPE_UCHAR: s = "unsigned char"; break;
61 case ATOMIC_TYPE_INT: s = "int"; break;
62 case ATOMIC_TYPE_UINT: s = "unsigned int"; break;
63 case ATOMIC_TYPE_SHORT: s = "short"; break;
64 case ATOMIC_TYPE_USHORT: s = "unsigned short"; break;
65 case ATOMIC_TYPE_LONG: s = "long"; break;
66 case ATOMIC_TYPE_ULONG: s = "unsigned long"; break;
67 case ATOMIC_TYPE_LONGLONG: s = "long long"; break;
68 case ATOMIC_TYPE_ULONGLONG: s = "unsigned long long"; break;
69 case ATOMIC_TYPE_LONG_DOUBLE: s = "long double"; break;
70 case ATOMIC_TYPE_FLOAT: s = "float"; break;
71 case ATOMIC_TYPE_DOUBLE: s = "double"; break;
72 default: s = "UNKNOWNATOMIC"; break;
78 void print_method_type_pre(const method_type_t *type)
80 print_type_qualifiers(type->type.qualifiers);
82 intern_print_type_pre(type->result_type);
84 /* TODO: don't emit braces if we're the toplevel type... */
89 void print_method_type_post(const method_type_t *type, const context_t *context)
91 /* TODO: don't emit braces if we're the toplevel type... */
92 intern_print_type_post(type->result_type);
99 method_parameter_t *parameter = type->parameters;
100 for( ; parameter != NULL; parameter = parameter->next) {
106 print_type(parameter->type);
109 declaration_t *parameter = context->declarations;
110 for( ; parameter != NULL; parameter = parameter->next) {
116 print_type_ext(parameter->type, parameter->symbol,
117 ¶meter->context);
128 if(first && !type->unspecified_parameters) {
135 void print_pointer_type_pre(const pointer_type_t *type)
137 intern_print_type_pre(type->points_to);
139 print_type_qualifiers(type->type.qualifiers);
142 static void print_pointer_type_post(const pointer_type_t *type)
144 intern_print_type_post(type->points_to);
147 static void print_array_type_post(const array_type_t *type)
150 if(type->is_static) {
151 fputs("static ", out);
153 print_type_qualifiers(type->type.qualifiers);
154 if(type->size != NULL) {
155 print_expression(type->size);
160 static void print_type_enum(const enum_type_t *type)
162 print_type_qualifiers(type->type.qualifiers);
163 if(type->symbol != NULL) {
164 fprintf(out, "enum %s", type->symbol->string);
166 fprintf(out, "enum {\n");
168 declaration_t *entry = type->entries_begin;
169 for( ; entry != type->entries_end->next; entry = entry->next) {
170 fprintf(out, "\t%s", entry->symbol->string);
171 if(entry->initializer != NULL) {
173 print_expression(entry->initializer);
182 static void intern_print_type_pre(const type_t *type)
186 fputs("invalid", out);
189 print_type_enum((const enum_type_t*) type);
192 print_atomic_type((const atomic_type_t*) type);
194 case TYPE_COMPOUND_STRUCT:
195 case TYPE_COMPOUND_UNION:
196 print_type_qualifiers(type->qualifiers);
197 if(((const compound_type_t*) type)->symbol != NULL) {
198 fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
202 fputs(((builtin_type_t*) type)->symbol->string, out);
205 print_method_type_pre((const method_type_t*) type);
208 print_pointer_type_pre((const pointer_type_t*) type);
213 fputs("unknown", out);
217 void intern_print_type_post(const type_t *type)
221 print_method_type_post((const method_type_t*) type, NULL);
224 print_pointer_type_post((const pointer_type_t*) type);
227 print_array_type_post((const array_type_t*) type);
232 case TYPE_COMPOUND_STRUCT:
233 case TYPE_COMPOUND_UNION:
239 void print_type(const type_t *type)
241 print_type_ext(type, NULL, NULL);
244 void print_type_ext(const type_t *type, const symbol_t *symbol,
245 const context_t *context)
248 fputs("nil type", out);
252 intern_print_type_pre(type);
255 fputs(symbol->string, out);
257 if(type->type == TYPE_METHOD) {
258 print_method_type_post((const method_type_t*) type, context);
260 intern_print_type_post(type);
264 bool type_valid(const type_t *type)
266 return type->type != TYPE_INVALID;
269 bool is_type_int(const type_t *type)
271 if(type->type != TYPE_ATOMIC)
274 atomic_type_t *atomic_type = (atomic_type_t*) type;
275 switch(atomic_type->atype) {
276 case ATOMIC_TYPE_CHAR:
277 case ATOMIC_TYPE_SCHAR:
278 case ATOMIC_TYPE_UCHAR:
279 case ATOMIC_TYPE_SHORT:
280 case ATOMIC_TYPE_USHORT:
281 case ATOMIC_TYPE_INT:
282 case ATOMIC_TYPE_UINT:
283 case ATOMIC_TYPE_LONG:
284 case ATOMIC_TYPE_ULONG:
285 case ATOMIC_TYPE_LONGLONG:
286 case ATOMIC_TYPE_ULONGLONG:
293 bool is_type_arithmetic(const type_t *type)
295 if(type->type != TYPE_ATOMIC)
298 atomic_type_t *atomic_type = (atomic_type_t*) type;
299 switch(atomic_type->atype) {
300 case ATOMIC_TYPE_CHAR:
301 case ATOMIC_TYPE_SCHAR:
302 case ATOMIC_TYPE_UCHAR:
303 case ATOMIC_TYPE_SHORT:
304 case ATOMIC_TYPE_USHORT:
305 case ATOMIC_TYPE_INT:
306 case ATOMIC_TYPE_UINT:
307 case ATOMIC_TYPE_LONG:
308 case ATOMIC_TYPE_ULONG:
309 case ATOMIC_TYPE_LONGLONG:
310 case ATOMIC_TYPE_ULONGLONG:
311 case ATOMIC_TYPE_FLOAT:
312 case ATOMIC_TYPE_DOUBLE:
313 case ATOMIC_TYPE_LONG_DOUBLE:
314 #ifdef PROVIDE_COMPLEX
315 case ATOMIC_TYPE_FLOAT_COMPLEX:
316 case ATOMIC_TYPE_DOUBLE_COMPLEX:
317 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
319 #ifdef PROVIDE_IMAGINARY
320 case ATOMIC_TYPE_FLOAT_IMAGINARY:
321 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
322 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
325 case ATOMIC_TYPE_INVALID:
326 case ATOMIC_TYPE_VOID:
327 case ATOMIC_TYPE_BOOL:
334 bool is_type_scalar(const type_t *type)
336 if(type->type == TYPE_POINTER)
339 return is_type_arithmetic(type);
342 static type_t *identify_new_type(type_t *type)
344 type_t *result = typehash_insert(type);
346 obstack_free(type_obst, type);
351 type_t *make_atomic_type(atomic_type_type_t type, unsigned qualifiers)
353 atomic_type_t *atomic_type
354 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
355 memset(atomic_type, 0, sizeof(atomic_type[0]));
356 atomic_type->type.type = TYPE_ATOMIC;
357 atomic_type->type.qualifiers = qualifiers;
358 atomic_type->atype = type;
360 return identify_new_type((type_t*) atomic_type);
363 type_t *make_pointer_type(type_t *points_to, unsigned qualifiers)
365 pointer_type_t *pointer_type
366 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
367 memset(pointer_type, 0, sizeof(pointer_type[0]));
368 pointer_type->type.type = TYPE_POINTER;
369 pointer_type->type.qualifiers = qualifiers;
370 pointer_type->points_to = points_to;
372 return identify_new_type((type_t*) pointer_type);
375 static __attribute__((unused))
376 void dbg_type(const type_t *type)