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_type_enum(const enum_type_t *type)
147 print_type_qualifiers(type->type.qualifiers);
148 if(type->symbol != NULL) {
149 fprintf(out, "enum %s", type->symbol->string);
151 fprintf(out, "enum {\n");
153 declaration_t *entry = type->entries_begin;
154 for( ; entry != type->entries_end->next; entry = entry->next) {
155 fprintf(out, "\t%s", entry->symbol->string);
156 if(entry->initializer != NULL) {
158 print_expression(entry->initializer);
167 static void intern_print_type_pre(const type_t *type)
171 fputs("invalid", out);
174 print_type_enum((const enum_type_t*) type);
177 print_atomic_type((const atomic_type_t*) type);
179 case TYPE_COMPOUND_STRUCT:
180 case TYPE_COMPOUND_UNION:
181 print_type_qualifiers(type->qualifiers);
182 if(((const compound_type_t*) type)->symbol != NULL) {
183 fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
187 fputs(((builtin_type_t*) type)->symbol->string, out);
190 print_method_type_pre((const method_type_t*) type);
193 print_pointer_type_pre((const pointer_type_t*) type);
196 fputs("unknown", out);
200 void intern_print_type_post(const type_t *type)
204 print_method_type_post((const method_type_t*) type, NULL);
207 print_pointer_type_post((const pointer_type_t*) type);
212 case TYPE_COMPOUND_STRUCT:
213 case TYPE_COMPOUND_UNION:
219 void print_type(const type_t *type)
221 print_type_ext(type, NULL, NULL);
224 void print_type_ext(const type_t *type, const symbol_t *symbol,
225 const context_t *context)
228 fputs("nil type", out);
232 intern_print_type_pre(type);
235 fputs(symbol->string, out);
237 if(type->type == TYPE_METHOD) {
238 print_method_type_post((const method_type_t*) type, context);
240 intern_print_type_post(type);
244 bool type_valid(const type_t *type)
246 return type->type != TYPE_INVALID;
249 bool is_type_int(const type_t *type)
251 if(type->type != TYPE_ATOMIC)
254 atomic_type_t *atomic_type = (atomic_type_t*) type;
255 switch(atomic_type->atype) {
256 case ATOMIC_TYPE_CHAR:
257 case ATOMIC_TYPE_SCHAR:
258 case ATOMIC_TYPE_UCHAR:
259 case ATOMIC_TYPE_SHORT:
260 case ATOMIC_TYPE_USHORT:
261 case ATOMIC_TYPE_INT:
262 case ATOMIC_TYPE_UINT:
263 case ATOMIC_TYPE_LONG:
264 case ATOMIC_TYPE_ULONG:
265 case ATOMIC_TYPE_LONGLONG:
266 case ATOMIC_TYPE_ULONGLONG:
273 bool is_type_arithmetic(const type_t *type)
275 if(type->type != TYPE_ATOMIC)
278 atomic_type_t *atomic_type = (atomic_type_t*) type;
279 switch(atomic_type->atype) {
280 case ATOMIC_TYPE_CHAR:
281 case ATOMIC_TYPE_SCHAR:
282 case ATOMIC_TYPE_UCHAR:
283 case ATOMIC_TYPE_SHORT:
284 case ATOMIC_TYPE_USHORT:
285 case ATOMIC_TYPE_INT:
286 case ATOMIC_TYPE_UINT:
287 case ATOMIC_TYPE_LONG:
288 case ATOMIC_TYPE_ULONG:
289 case ATOMIC_TYPE_LONGLONG:
290 case ATOMIC_TYPE_ULONGLONG:
291 case ATOMIC_TYPE_FLOAT:
292 case ATOMIC_TYPE_DOUBLE:
293 case ATOMIC_TYPE_LONG_DOUBLE:
294 #ifdef PROVIDE_COMPLEX
295 case ATOMIC_TYPE_FLOAT_COMPLEX:
296 case ATOMIC_TYPE_DOUBLE_COMPLEX:
297 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
299 #ifdef PROVIDE_IMAGINARY
300 case ATOMIC_TYPE_FLOAT_IMAGINARY:
301 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
302 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
305 case ATOMIC_TYPE_INVALID:
306 case ATOMIC_TYPE_VOID:
307 case ATOMIC_TYPE_BOOL:
314 bool is_type_scalar(const type_t *type)
316 if(type->type == TYPE_POINTER)
319 return is_type_arithmetic(type);
322 static type_t *identify_new_type(type_t *type)
324 type_t *result = typehash_insert(type);
326 obstack_free(type_obst, type);
331 type_t *make_atomic_type(atomic_type_type_t type, unsigned qualifiers)
333 atomic_type_t *atomic_type
334 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
335 memset(atomic_type, 0, sizeof(atomic_type[0]));
336 atomic_type->type.type = TYPE_ATOMIC;
337 atomic_type->type.qualifiers = qualifiers;
338 atomic_type->atype = type;
340 return identify_new_type((type_t*) atomic_type);
343 type_t *make_pointer_type(type_t *points_to, unsigned qualifiers)
345 pointer_type_t *pointer_type
346 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
347 memset(pointer_type, 0, sizeof(pointer_type[0]));
348 pointer_type->type.type = TYPE_POINTER;
349 pointer_type->type.qualifiers = qualifiers;
350 pointer_type->points_to = points_to;
352 return identify_new_type((type_t*) pointer_type);
355 static __attribute__((unused))
356 void dbg_type(const type_t *type)