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);
54 case ATOMIC_TYPE_INVALID: fputs("INVALIDATOMIC", out); return;
55 case ATOMIC_TYPE_VOID: fputs("void", out); return;
56 case ATOMIC_TYPE_BOOL: fputs("bool", out); return;
57 case ATOMIC_TYPE_CHAR: fputs("char", out); return;
58 case ATOMIC_TYPE_SCHAR: fputs("signed char", out); return;
59 case ATOMIC_TYPE_UCHAR: fputs("unsigned char", out); return;
60 case ATOMIC_TYPE_INT: fputs("int", out); return;
61 case ATOMIC_TYPE_UINT: fputs("unsigned int", out); return;
62 case ATOMIC_TYPE_SHORT: fputs("short", out); return;
63 case ATOMIC_TYPE_USHORT: fputs("unsigned short", out); return;
64 case ATOMIC_TYPE_LONG: fputs("long", out); return;
65 case ATOMIC_TYPE_ULONG: fputs("unsigned long", out); return;
66 case ATOMIC_TYPE_LONGLONG: fputs("long long", out); return;
67 case ATOMIC_TYPE_ULONGLONG: fputs("unsigned long long", out); return;
68 case ATOMIC_TYPE_LONG_DOUBLE: fputs("long double", out); return;
69 case ATOMIC_TYPE_FLOAT: fputs("float", out); return;
70 case ATOMIC_TYPE_DOUBLE: fputs("double", out); return;
72 fputs("UNKNOWNATOMIC", out);
76 void print_method_type_pre(const method_type_t *type)
78 print_type_qualifiers(type->type.qualifiers);
80 intern_print_type_pre(type->result_type);
82 /* TODO: don't emit braces if we're the toplevel type... */
87 void print_method_type_post(const method_type_t *type, const context_t *context)
89 /* TODO: don't emit braces if we're the toplevel type... */
90 intern_print_type_post(type->result_type);
97 method_parameter_t *parameter = type->parameters;
98 for( ; parameter != NULL; parameter = parameter->next) {
104 print_type(parameter->type);
107 declaration_t *parameter = context->declarations;
108 for( ; parameter != NULL; parameter = parameter->next) {
114 print_type_ext(parameter->type, parameter->symbol,
115 ¶meter->context);
126 if(first && !type->unspecified_parameters) {
133 void print_pointer_type_pre(const pointer_type_t *type)
135 intern_print_type_pre(type->points_to);
137 print_type_qualifiers(type->type.qualifiers);
140 static void print_pointer_type_post(const pointer_type_t *type)
142 intern_print_type_post(type->points_to);
145 static void print_array_type_post(const array_type_t *type)
148 if(type->is_static) {
149 fputs("static ", out);
151 print_type_qualifiers(type->type.qualifiers);
152 if(type->size != NULL) {
153 print_expression(type->size);
158 static void print_type_enum(const enum_type_t *type)
160 print_type_qualifiers(type->type.qualifiers);
161 if(type->symbol != NULL) {
162 fprintf(out, "enum %s", type->symbol->string);
164 fprintf(out, "enum {\n");
166 declaration_t *entry = type->entries_begin;
167 for( ; entry != type->entries_end->next; entry = entry->next) {
168 fprintf(out, "\t%s", entry->symbol->string);
169 if(entry->initializer != NULL) {
171 print_expression(entry->initializer);
180 static void intern_print_type_pre(const type_t *type)
184 fputs("invalid", out);
187 print_type_enum((const enum_type_t*) type);
190 print_atomic_type((const atomic_type_t*) type);
192 case TYPE_COMPOUND_STRUCT:
193 case TYPE_COMPOUND_UNION:
194 print_type_qualifiers(type->qualifiers);
195 if(((const compound_type_t*) type)->symbol != NULL) {
196 fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
200 fputs(((builtin_type_t*) type)->symbol->string, out);
203 print_method_type_pre((const method_type_t*) type);
206 print_pointer_type_pre((const pointer_type_t*) type);
211 fputs("unknown", out);
215 void intern_print_type_post(const type_t *type)
219 print_method_type_post((const method_type_t*) type, NULL);
222 print_pointer_type_post((const pointer_type_t*) type);
225 print_array_type_post((const array_type_t*) type);
230 case TYPE_COMPOUND_STRUCT:
231 case TYPE_COMPOUND_UNION:
237 void print_type(const type_t *type)
239 print_type_ext(type, NULL, NULL);
242 void print_type_ext(const type_t *type, const symbol_t *symbol,
243 const context_t *context)
246 fputs("nil type", out);
250 intern_print_type_pre(type);
253 fputs(symbol->string, out);
255 if(type->type == TYPE_METHOD) {
256 print_method_type_post((const method_type_t*) type, context);
258 intern_print_type_post(type);
262 bool type_valid(const type_t *type)
264 return type->type != TYPE_INVALID;
267 bool is_type_int(const type_t *type)
269 if(type->type != TYPE_ATOMIC)
272 atomic_type_t *atomic_type = (atomic_type_t*) type;
273 switch(atomic_type->atype) {
274 case ATOMIC_TYPE_CHAR:
275 case ATOMIC_TYPE_SCHAR:
276 case ATOMIC_TYPE_UCHAR:
277 case ATOMIC_TYPE_SHORT:
278 case ATOMIC_TYPE_USHORT:
279 case ATOMIC_TYPE_INT:
280 case ATOMIC_TYPE_UINT:
281 case ATOMIC_TYPE_LONG:
282 case ATOMIC_TYPE_ULONG:
283 case ATOMIC_TYPE_LONGLONG:
284 case ATOMIC_TYPE_ULONGLONG:
291 bool is_type_arithmetic(const type_t *type)
293 if(type->type != TYPE_ATOMIC)
296 atomic_type_t *atomic_type = (atomic_type_t*) type;
297 switch(atomic_type->atype) {
298 case ATOMIC_TYPE_CHAR:
299 case ATOMIC_TYPE_SCHAR:
300 case ATOMIC_TYPE_UCHAR:
301 case ATOMIC_TYPE_SHORT:
302 case ATOMIC_TYPE_USHORT:
303 case ATOMIC_TYPE_INT:
304 case ATOMIC_TYPE_UINT:
305 case ATOMIC_TYPE_LONG:
306 case ATOMIC_TYPE_ULONG:
307 case ATOMIC_TYPE_LONGLONG:
308 case ATOMIC_TYPE_ULONGLONG:
309 case ATOMIC_TYPE_FLOAT:
310 case ATOMIC_TYPE_DOUBLE:
311 case ATOMIC_TYPE_LONG_DOUBLE:
312 #ifdef PROVIDE_COMPLEX
313 case ATOMIC_TYPE_FLOAT_COMPLEX:
314 case ATOMIC_TYPE_DOUBLE_COMPLEX:
315 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
317 #ifdef PROVIDE_IMAGINARY
318 case ATOMIC_TYPE_FLOAT_IMAGINARY:
319 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
320 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
323 case ATOMIC_TYPE_INVALID:
324 case ATOMIC_TYPE_VOID:
325 case ATOMIC_TYPE_BOOL:
332 bool is_type_scalar(const type_t *type)
334 if(type->type == TYPE_POINTER)
337 return is_type_arithmetic(type);
340 static type_t *identify_new_type(type_t *type)
342 type_t *result = typehash_insert(type);
344 obstack_free(type_obst, type);
349 type_t *make_atomic_type(atomic_type_type_t type, unsigned qualifiers)
351 atomic_type_t *atomic_type
352 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
353 memset(atomic_type, 0, sizeof(atomic_type[0]));
354 atomic_type->type.type = TYPE_ATOMIC;
355 atomic_type->type.qualifiers = qualifiers;
356 atomic_type->atype = type;
358 return identify_new_type((type_t*) atomic_type);
361 type_t *make_pointer_type(type_t *points_to, unsigned qualifiers)
363 pointer_type_t *pointer_type
364 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
365 memset(pointer_type, 0, sizeof(pointer_type[0]));
366 pointer_type->type.type = TYPE_POINTER;
367 pointer_type->type.qualifiers = qualifiers;
368 pointer_type->points_to = points_to;
370 return identify_new_type((type_t*) pointer_type);
373 static __attribute__((unused))
374 void dbg_type(const type_t *type)