inline is not a type qualifier anymore, fix function entity creation
[cparser] / type.c
1 #include <config.h>
2
3 #include <stdio.h>
4 #include <assert.h>
5 #include "type_t.h"
6 #include "type_hash.h"
7 #include "adt/error.h"
8
9 static struct obstack   _type_obst;
10 struct obstack         *type_obst = &_type_obst;
11 static FILE            *out;
12 static int              type_visited = 0;
13 static bool             print_compound_entries;
14
15 static void intern_print_type_pre(type_t *type);
16 static void intern_print_type_post(type_t *type);
17
18 void init_types(void)
19 {
20         obstack_init(type_obst);
21 }
22
23 void exit_types(void)
24 {
25         obstack_free(type_obst, NULL);
26 }
27
28 void type_set_output(FILE *stream)
29 {
30         out = stream;
31 }
32
33 void set_print_compound_entries(bool enabled)
34 {
35         print_compound_entries = enabled;
36 }
37
38 void inc_type_visited(void)
39 {
40         type_visited++;
41 }
42
43 static
44 void print_type_qualifiers(unsigned qualifiers)
45 {
46         if(qualifiers & TYPE_QUALIFIER_CONST)    fputs("const ",    out);
47         if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
48         if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
49 }
50
51 static
52 void print_atomic_type(const atomic_type_t *type)
53 {
54         print_type_qualifiers(type->type.qualifiers);
55
56         const char *s;
57         switch(type->atype) {
58         case ATOMIC_TYPE_INVALID:     s = "INVALIDATOMIC";      break;
59         case ATOMIC_TYPE_VOID:        s = "void";               break;
60         case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
61         case ATOMIC_TYPE_CHAR:        s = "char";               break;
62         case ATOMIC_TYPE_SCHAR:       s = "signed char";        break;
63         case ATOMIC_TYPE_UCHAR:       s = "unsigned char";      break;
64         case ATOMIC_TYPE_INT:         s = "int";                break;
65         case ATOMIC_TYPE_UINT:        s = "unsigned int";       break;
66         case ATOMIC_TYPE_SHORT:       s = "short";              break;
67         case ATOMIC_TYPE_USHORT:      s = "unsigned short";     break;
68         case ATOMIC_TYPE_LONG:        s = "long";               break;
69         case ATOMIC_TYPE_ULONG:       s = "unsigned long";      break;
70         case ATOMIC_TYPE_LONGLONG:    s = "long long";          break;
71         case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long"; break;
72         case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";        break;
73         case ATOMIC_TYPE_FLOAT:       s = "float";              break;
74         case ATOMIC_TYPE_DOUBLE:      s = "double";             break;
75         default:                      s = "UNKNOWNATOMIC";      break;
76         }
77         fputs(s, out);
78 }
79
80 static void print_function_type_pre(const function_type_t *type)
81 {
82         print_type_qualifiers(type->type.qualifiers);
83
84         intern_print_type_pre(type->result_type);
85
86         /* TODO: don't emit braces if we're the toplevel type... */
87         fputc('(', out);
88 }
89
90 static void print_function_type_post(const function_type_t *type,
91                                      const context_t *context)
92 {
93         /* TODO: don't emit braces if we're the toplevel type... */
94         intern_print_type_post(type->result_type);
95         fputc(')', out);
96
97         fputc('(', out);
98
99         int                 first     = 1;
100         if(context == NULL) {
101                 function_parameter_t *parameter = type->parameters;
102                 for( ; parameter != NULL; parameter = parameter->next) {
103                         if(first) {
104                                 first = 0;
105                         } else {
106                                 fputs(", ", out);
107                         }
108                         print_type(parameter->type);
109                 }
110         } else {
111                 declaration_t *parameter = context->declarations;
112                 for( ; parameter != NULL; parameter = parameter->next) {
113                         if(first) {
114                                 first = 0;
115                         } else {
116                                 fputs(", ", out);
117                         }
118                         print_type_ext(parameter->type, parameter->symbol,
119                                        &parameter->context);
120                 }
121         }
122         if(type->variadic) {
123                 if(first) {
124                         first = 0;
125                 } else {
126                         fputs(", ", out);
127                 }
128                 fputs("...", out);
129         }
130         if(first && !type->unspecified_parameters) {
131                 fputs("void", out);
132         }
133         fputc(')', out);
134 }
135
136 static
137 void print_pointer_type_pre(const pointer_type_t *type)
138 {
139         intern_print_type_pre(type->points_to);
140         fputs("*", out);
141         print_type_qualifiers(type->type.qualifiers);
142 }
143
144 static void print_pointer_type_post(const pointer_type_t *type)
145 {
146         intern_print_type_post(type->points_to);
147 }
148
149 static void print_array_type_post(const array_type_t *type)
150 {
151         fputc('[', out);
152         if(type->is_static) {
153                 fputs("static ", out);
154         }
155         print_type_qualifiers(type->type.qualifiers);
156         if(type->size != NULL) {
157                 print_expression(type->size);
158         }
159         fputc(']', out);
160 }
161
162 void print_enum_definition(const declaration_t *declaration)
163 {
164         fputs("{\n", out);
165
166         change_indent(1);
167
168         declaration_t *entry = declaration->next;
169         for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
170                entry = entry->next) {
171
172                 print_indent();
173                 fprintf(out, "%s", entry->symbol->string);
174                 if(entry->init.initializer != NULL) {
175                         fprintf(out, " = ");
176                         print_initializer(entry->init.initializer);
177                 }
178                 fprintf(out, ",\n");
179         }
180
181         change_indent(-1);
182         print_indent();
183         fputs("}", out);
184 }
185
186 static void print_type_enum(const enum_type_t *type)
187 {
188         print_type_qualifiers(type->type.qualifiers);
189         fputs("enum ", out);
190
191         declaration_t *declaration = type->declaration;
192         symbol_t      *symbol      = declaration->symbol;
193         if(symbol != NULL) {
194                 fputs(symbol->string, out);
195         } else {
196                 print_enum_definition(declaration);
197         }
198 }
199
200 void print_compound_definition(const declaration_t *declaration)
201 {
202         fputs("{\n", out);
203         change_indent(1);
204
205         declaration_t *iter = declaration->context.declarations;
206         for( ; iter != NULL; iter = iter->next) {
207                 print_indent();
208                 print_declaration(iter);
209                 fputc('\n', out);
210         }
211
212         change_indent(-1);
213         print_indent();
214         fputs("}", out);
215 }
216
217 static void print_compound_type(const compound_type_t *type)
218 {
219         print_type_qualifiers(type->type.qualifiers);
220
221         if(type->type.type == TYPE_COMPOUND_STRUCT) {
222                 fputs("struct ", out);
223         } else {
224                 assert(type->type.type == TYPE_COMPOUND_UNION);
225                 fputs("union ", out);
226         }
227
228         declaration_t *declaration = type->declaration;
229         symbol_t      *symbol      = declaration->symbol;
230         if(symbol != NULL) {
231                 fputs(symbol->string, out);
232         } else {
233                 print_compound_definition(declaration);
234         }
235 }
236
237 static void print_typedef_type_pre(typedef_type_t *type)
238 {
239         fputs(type->declaration->symbol->string, out);
240 }
241
242 static void print_typeof_type_pre(typeof_type_t *type)
243 {
244         fputs("typeof(", out);
245         if(type->expression != NULL) {
246                 assert(type->typeof_type == NULL);
247                 print_expression(type->expression);
248         } else {
249                 print_type(type->typeof_type);
250         }
251         fputc(')', out);
252 }
253
254 static void intern_print_type_pre(type_t *type)
255 {
256         switch(type->type) {
257         case TYPE_INVALID:
258                 fputs("invalid", out);
259                 return;
260         case TYPE_ENUM:
261                 print_type_enum((enum_type_t*) type);
262                 return;
263         case TYPE_ATOMIC:
264                 print_atomic_type((atomic_type_t*) type);
265                 return;
266         case TYPE_COMPOUND_STRUCT:
267         case TYPE_COMPOUND_UNION:
268                 print_compound_type((compound_type_t*) type);
269                 return;
270         case TYPE_BUILTIN:
271                 fputs(((builtin_type_t*) type)->symbol->string, out);
272                 return;
273         case TYPE_FUNCTION:
274                 print_function_type_pre((function_type_t*) type);
275                 return;
276         case TYPE_POINTER:
277                 print_pointer_type_pre((pointer_type_t*) type);
278                 return;
279         case TYPE_ARRAY:
280                 return;
281         case TYPE_TYPEDEF:
282                 print_typedef_type_pre((typedef_type_t*) type);
283                 return;
284         case TYPE_TYPEOF:
285                 print_typeof_type_pre((typeof_type_t*) type);
286                 return;
287         }
288         fputs("unknown", out);
289 }
290
291 static void intern_print_type_post(type_t *type)
292 {
293         switch(type->type) {
294         case TYPE_FUNCTION:
295                 print_function_type_post((const function_type_t*) type, NULL);
296                 return;
297         case TYPE_POINTER:
298                 print_pointer_type_post((const pointer_type_t*) type);
299                 return;
300         case TYPE_ARRAY:
301                 print_array_type_post((const array_type_t*) type);
302                 return;
303         case TYPE_INVALID:
304         case TYPE_ATOMIC:
305         case TYPE_ENUM:
306         case TYPE_COMPOUND_STRUCT:
307         case TYPE_COMPOUND_UNION:
308         case TYPE_BUILTIN:
309         case TYPE_TYPEOF:
310         case TYPE_TYPEDEF:
311                 break;
312         }
313 }
314
315 void print_type(type_t *type)
316 {
317         print_type_ext(type, NULL, NULL);
318 }
319
320 void print_type_ext(type_t *type, const symbol_t *symbol,
321                     const context_t *context)
322 {
323         if(type == NULL) {
324                 fputs("nil type", out);
325                 return;
326         }
327
328         intern_print_type_pre(type);
329         if(symbol != NULL) {
330                 fputc(' ', out);
331                 fputs(symbol->string, out);
332         }
333         if(type->type == TYPE_FUNCTION) {
334                 print_function_type_post((const function_type_t*) type, context);
335         } else {
336                 intern_print_type_post(type);
337         }
338 }
339
340 bool type_valid(const type_t *type)
341 {
342         return type->type != TYPE_INVALID;
343 }
344
345 bool is_type_integer(const type_t *type)
346 {
347         if(type->type == TYPE_ENUM)
348                 return true;
349
350         if(type->type != TYPE_ATOMIC)
351                 return false;
352
353         atomic_type_t *atomic_type = (atomic_type_t*) type;
354         switch(atomic_type->atype) {
355         case ATOMIC_TYPE_BOOL:
356         case ATOMIC_TYPE_CHAR:
357         case ATOMIC_TYPE_SCHAR:
358         case ATOMIC_TYPE_UCHAR:
359         case ATOMIC_TYPE_SHORT:
360         case ATOMIC_TYPE_USHORT:
361         case ATOMIC_TYPE_INT:
362         case ATOMIC_TYPE_UINT:
363         case ATOMIC_TYPE_LONG:
364         case ATOMIC_TYPE_ULONG:
365         case ATOMIC_TYPE_LONGLONG:
366         case ATOMIC_TYPE_ULONGLONG:
367                 return true;
368         default:
369                 return false;
370         }
371 }
372
373 bool is_type_floating(const type_t *type)
374 {
375         if(type->type != TYPE_ATOMIC)
376                 return false;
377
378         atomic_type_t *atomic_type = (atomic_type_t*) type;
379         switch(atomic_type->atype) {
380         case ATOMIC_TYPE_FLOAT:
381         case ATOMIC_TYPE_DOUBLE:
382         case ATOMIC_TYPE_LONG_DOUBLE:
383 #ifdef PROVIDE_COMPLEX
384         case ATOMIC_TYPE_FLOAT_COMPLEX:
385         case ATOMIC_TYPE_DOUBLE_COMPLEX:
386         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
387 #endif
388 #ifdef PROVIDE_IMAGINARY
389         case ATOMIC_TYPE_FLOAT_IMAGINARY:
390         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
391         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
392 #endif
393                 return true;
394         default:
395                 return false;
396         }
397 }
398
399 bool is_type_signed(const type_t *type)
400 {
401         /* enum types are int for now */
402         if(type->type == TYPE_ENUM)
403                 return true;
404
405         if(type->type != TYPE_ATOMIC)
406                 return false;
407
408         atomic_type_t *atomic_type = (atomic_type_t*) type;
409         switch(atomic_type->atype) {
410         case ATOMIC_TYPE_CHAR:
411         case ATOMIC_TYPE_SCHAR:
412         case ATOMIC_TYPE_SHORT:
413         case ATOMIC_TYPE_INT:
414         case ATOMIC_TYPE_LONG:
415         case ATOMIC_TYPE_LONGLONG:
416         case ATOMIC_TYPE_FLOAT:
417         case ATOMIC_TYPE_DOUBLE:
418         case ATOMIC_TYPE_LONG_DOUBLE:
419 #ifdef PROVIDE_COMPLEX
420         case ATOMIC_TYPE_FLOAT_COMPLEX:
421         case ATOMIC_TYPE_DOUBLE_COMPLEX:
422         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
423 #endif
424 #ifdef PROVIDE_IMAGINARY
425         case ATOMIC_TYPE_FLOAT_IMAGINARY:
426         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
427         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
428 #endif
429                 return true;
430
431         case ATOMIC_TYPE_BOOL:
432         case ATOMIC_TYPE_UCHAR:
433         case ATOMIC_TYPE_USHORT:
434         case ATOMIC_TYPE_UINT:
435         case ATOMIC_TYPE_ULONG:
436         case ATOMIC_TYPE_ULONGLONG:
437                 return false;
438
439         case ATOMIC_TYPE_INVALID:
440         case ATOMIC_TYPE_VOID:
441                 return false;
442         }
443
444         panic("invalid atomic type found");
445         return false;
446 }
447
448 bool is_type_arithmetic(const type_t *type)
449 {
450         if(is_type_integer(type) || is_type_floating(type))
451                 return 1;
452
453         return 0;
454 }
455
456 bool is_type_scalar(const type_t *type)
457 {
458         if(type->type == TYPE_POINTER)
459                 return 1;
460
461         return is_type_arithmetic(type);
462 }
463
464 bool pointers_compatible(const type_t *type1, const type_t *type2)
465 {
466         assert(type1->type == TYPE_POINTER);
467         assert(type2->type == TYPE_POINTER);
468         return true;
469 }
470
471 type_t *skip_typeref(type_t *type)
472 {
473         while(1) {
474                 switch(type->type) {
475                 case TYPE_TYPEDEF: {
476                         const typedef_type_t *typedef_type = (const typedef_type_t*) type;
477                         type = typedef_type->declaration->type;
478                         continue;
479                 }
480                 case TYPE_TYPEOF: {
481                         const typeof_type_t *typeof_type = (const typeof_type_t *) type;
482                         if(typeof_type->typeof_type != NULL) {
483                                 type = typeof_type->typeof_type;
484                         } else {
485                                 type = typeof_type->expression->datatype;
486                         }
487                         continue;
488                 }
489                 case TYPE_BUILTIN: {
490                         const builtin_type_t *builtin_type = (const builtin_type_t*) type;
491                         type = builtin_type->real_type;
492                         continue;
493                 }
494                 default:
495                         break;
496                 }
497                 break;
498         }
499
500         return type;
501 }
502
503
504
505 static type_t *identify_new_type(type_t *type)
506 {
507         type_t *result = typehash_insert(type);
508         if(result != type) {
509                 obstack_free(type_obst, type);
510         }
511         return result;
512 }
513
514 type_t *make_atomic_type(atomic_type_type_t type, type_qualifier_t qualifiers)
515 {
516         atomic_type_t *atomic_type
517                 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
518         memset(atomic_type, 0, sizeof(atomic_type[0]));
519         atomic_type->type.type       = TYPE_ATOMIC;
520         atomic_type->type.qualifiers = qualifiers;
521         atomic_type->atype           = type;
522
523         return identify_new_type((type_t*) atomic_type);
524 }
525
526 type_t *make_pointer_type(type_t *points_to, type_qualifier_t qualifiers)
527 {
528         pointer_type_t *pointer_type
529                 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
530         memset(pointer_type, 0, sizeof(pointer_type[0]));
531         pointer_type->type.type       = TYPE_POINTER;
532         pointer_type->type.qualifiers = qualifiers;
533         pointer_type->points_to       = points_to;
534
535         return identify_new_type((type_t*) pointer_type);
536 }
537
538 static __attribute__((unused))
539 void dbg_type(type_t *type)
540 {
541         FILE *old_out = out;
542         out = stderr;
543         print_type(type);
544         puts("\n");
545         fflush(stderr);
546         out = old_out;
547 }