string literals as array intiailizers are supported now
[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 void print_pointer_type_pre(const pointer_type_t *type)
137 {
138         intern_print_type_pre(type->points_to);
139         fputs("*", out);
140         print_type_qualifiers(type->type.qualifiers);
141 }
142
143 static void print_pointer_type_post(const pointer_type_t *type)
144 {
145         intern_print_type_post(type->points_to);
146 }
147
148 static void print_array_type_pre(const array_type_t *type)
149 {
150         intern_print_type_pre(type->element_type);
151 }
152
153 static void print_array_type_post(const array_type_t *type)
154 {
155         fputc('[', out);
156         if(type->is_static) {
157                 fputs("static ", out);
158         }
159         print_type_qualifiers(type->type.qualifiers);
160         if(type->size != NULL) {
161                 print_expression(type->size);
162         }
163         fputc(']', out);
164         intern_print_type_post(type->element_type);
165 }
166
167 void print_enum_definition(const declaration_t *declaration)
168 {
169         fputs("{\n", out);
170
171         change_indent(1);
172
173         declaration_t *entry = declaration->next;
174         for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
175                entry = entry->next) {
176
177                 print_indent();
178                 fprintf(out, "%s", entry->symbol->string);
179                 if(entry->init.initializer != NULL) {
180                         fprintf(out, " = ");
181                         print_expression(entry->init.enum_value);
182                 }
183                 fprintf(out, ",\n");
184         }
185
186         change_indent(-1);
187         print_indent();
188         fputs("}", out);
189 }
190
191 static void print_type_enum(const enum_type_t *type)
192 {
193         print_type_qualifiers(type->type.qualifiers);
194         fputs("enum ", out);
195
196         declaration_t *declaration = type->declaration;
197         symbol_t      *symbol      = declaration->symbol;
198         if(symbol != NULL) {
199                 fputs(symbol->string, out);
200         } else {
201                 print_enum_definition(declaration);
202         }
203 }
204
205 void print_compound_definition(const declaration_t *declaration)
206 {
207         fputs("{\n", out);
208         change_indent(1);
209
210         declaration_t *iter = declaration->context.declarations;
211         for( ; iter != NULL; iter = iter->next) {
212                 print_indent();
213                 print_declaration(iter);
214                 fputc('\n', out);
215         }
216
217         change_indent(-1);
218         print_indent();
219         fputs("}", out);
220 }
221
222 static void print_compound_type(const compound_type_t *type)
223 {
224         print_type_qualifiers(type->type.qualifiers);
225
226         if(type->type.type == TYPE_COMPOUND_STRUCT) {
227                 fputs("struct ", out);
228         } else {
229                 assert(type->type.type == TYPE_COMPOUND_UNION);
230                 fputs("union ", out);
231         }
232
233         declaration_t *declaration = type->declaration;
234         symbol_t      *symbol      = declaration->symbol;
235         if(symbol != NULL) {
236                 fputs(symbol->string, out);
237         } else {
238                 print_compound_definition(declaration);
239         }
240 }
241
242 static void print_typedef_type_pre(typedef_type_t *type)
243 {
244         fputs(type->declaration->symbol->string, out);
245 }
246
247 static void print_typeof_type_pre(typeof_type_t *type)
248 {
249         fputs("typeof(", out);
250         if(type->expression != NULL) {
251                 assert(type->typeof_type == NULL);
252                 print_expression(type->expression);
253         } else {
254                 print_type(type->typeof_type);
255         }
256         fputc(')', out);
257 }
258
259 static void intern_print_type_pre(type_t *type)
260 {
261         switch(type->type) {
262         case TYPE_INVALID:
263                 fputs("invalid", out);
264                 return;
265         case TYPE_ENUM:
266                 print_type_enum((enum_type_t*) type);
267                 return;
268         case TYPE_ATOMIC:
269                 print_atomic_type((atomic_type_t*) type);
270                 return;
271         case TYPE_COMPOUND_STRUCT:
272         case TYPE_COMPOUND_UNION:
273                 print_compound_type((compound_type_t*) type);
274                 return;
275         case TYPE_BUILTIN:
276                 fputs(((builtin_type_t*) type)->symbol->string, out);
277                 return;
278         case TYPE_FUNCTION:
279                 print_function_type_pre((function_type_t*) type);
280                 return;
281         case TYPE_POINTER:
282                 print_pointer_type_pre((pointer_type_t*) type);
283                 return;
284         case TYPE_ARRAY:
285                 print_array_type_pre((array_type_t*) type);
286                 return;
287         case TYPE_TYPEDEF:
288                 print_typedef_type_pre((typedef_type_t*) type);
289                 return;
290         case TYPE_TYPEOF:
291                 print_typeof_type_pre((typeof_type_t*) type);
292                 return;
293         }
294         fputs("unknown", out);
295 }
296
297 static void intern_print_type_post(type_t *type)
298 {
299         switch(type->type) {
300         case TYPE_FUNCTION:
301                 print_function_type_post((const function_type_t*) type, NULL);
302                 return;
303         case TYPE_POINTER:
304                 print_pointer_type_post((const pointer_type_t*) type);
305                 return;
306         case TYPE_ARRAY:
307                 print_array_type_post((const array_type_t*) type);
308                 return;
309         case TYPE_INVALID:
310         case TYPE_ATOMIC:
311         case TYPE_ENUM:
312         case TYPE_COMPOUND_STRUCT:
313         case TYPE_COMPOUND_UNION:
314         case TYPE_BUILTIN:
315         case TYPE_TYPEOF:
316         case TYPE_TYPEDEF:
317                 break;
318         }
319 }
320
321 void print_type(type_t *type)
322 {
323         print_type_ext(type, NULL, NULL);
324 }
325
326 void print_type_ext(type_t *type, const symbol_t *symbol,
327                     const context_t *context)
328 {
329         if(type == NULL) {
330                 fputs("nil type", out);
331                 return;
332         }
333
334         intern_print_type_pre(type);
335         if(symbol != NULL) {
336                 fputc(' ', out);
337                 fputs(symbol->string, out);
338         }
339         if(type->type == TYPE_FUNCTION) {
340                 print_function_type_post((const function_type_t*) type, context);
341         } else {
342                 intern_print_type_post(type);
343         }
344 }
345
346 bool type_valid(const type_t *type)
347 {
348         return type->type != TYPE_INVALID;
349 }
350
351 bool is_type_integer(const type_t *type)
352 {
353         if(type->type == TYPE_ENUM)
354                 return true;
355
356         if(type->type != TYPE_ATOMIC)
357                 return false;
358
359         atomic_type_t *atomic_type = (atomic_type_t*) type;
360         switch(atomic_type->atype) {
361         case ATOMIC_TYPE_BOOL:
362         case ATOMIC_TYPE_CHAR:
363         case ATOMIC_TYPE_SCHAR:
364         case ATOMIC_TYPE_UCHAR:
365         case ATOMIC_TYPE_SHORT:
366         case ATOMIC_TYPE_USHORT:
367         case ATOMIC_TYPE_INT:
368         case ATOMIC_TYPE_UINT:
369         case ATOMIC_TYPE_LONG:
370         case ATOMIC_TYPE_ULONG:
371         case ATOMIC_TYPE_LONGLONG:
372         case ATOMIC_TYPE_ULONGLONG:
373                 return true;
374         default:
375                 return false;
376         }
377 }
378
379 bool is_type_floating(const type_t *type)
380 {
381         if(type->type != TYPE_ATOMIC)
382                 return false;
383
384         atomic_type_t *atomic_type = (atomic_type_t*) type;
385         switch(atomic_type->atype) {
386         case ATOMIC_TYPE_FLOAT:
387         case ATOMIC_TYPE_DOUBLE:
388         case ATOMIC_TYPE_LONG_DOUBLE:
389 #ifdef PROVIDE_COMPLEX
390         case ATOMIC_TYPE_FLOAT_COMPLEX:
391         case ATOMIC_TYPE_DOUBLE_COMPLEX:
392         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
393         case ATOMIC_TYPE_FLOAT_IMAGINARY:
394         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
395         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
396 #endif
397                 return true;
398         default:
399                 return false;
400         }
401 }
402
403 bool is_type_signed(const type_t *type)
404 {
405         /* enum types are int for now */
406         if(type->type == TYPE_ENUM)
407                 return true;
408
409         if(type->type != TYPE_ATOMIC)
410                 return false;
411
412         atomic_type_t *atomic_type = (atomic_type_t*) type;
413         switch(atomic_type->atype) {
414         case ATOMIC_TYPE_CHAR:
415         case ATOMIC_TYPE_SCHAR:
416         case ATOMIC_TYPE_SHORT:
417         case ATOMIC_TYPE_INT:
418         case ATOMIC_TYPE_LONG:
419         case ATOMIC_TYPE_LONGLONG:
420         case ATOMIC_TYPE_FLOAT:
421         case ATOMIC_TYPE_DOUBLE:
422         case ATOMIC_TYPE_LONG_DOUBLE:
423 #ifdef PROVIDE_COMPLEX
424         case ATOMIC_TYPE_FLOAT_COMPLEX:
425         case ATOMIC_TYPE_DOUBLE_COMPLEX:
426         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
427         case ATOMIC_TYPE_FLOAT_IMAGINARY:
428         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
429         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
430 #endif
431                 return true;
432
433         case ATOMIC_TYPE_BOOL:
434         case ATOMIC_TYPE_UCHAR:
435         case ATOMIC_TYPE_USHORT:
436         case ATOMIC_TYPE_UINT:
437         case ATOMIC_TYPE_ULONG:
438         case ATOMIC_TYPE_ULONGLONG:
439                 return false;
440
441         case ATOMIC_TYPE_INVALID:
442         case ATOMIC_TYPE_VOID:
443                 return false;
444         }
445
446         panic("invalid atomic type found");
447         return false;
448 }
449
450 bool is_type_arithmetic(const type_t *type)
451 {
452         if(is_type_integer(type) || is_type_floating(type))
453                 return 1;
454
455         return 0;
456 }
457
458 bool is_type_scalar(const type_t *type)
459 {
460         if(type->type == TYPE_POINTER)
461                 return 1;
462
463         return is_type_arithmetic(type);
464 }
465
466 bool is_type_incomplete(const type_t *type)
467 {
468         switch(type->type) {
469         case TYPE_COMPOUND_STRUCT:
470         case TYPE_COMPOUND_UNION: {
471                 const compound_type_t *compound_type
472                         = (const compound_type_t*) type;
473                 declaration_t *declaration = compound_type->declaration;
474                 return !declaration->init.is_defined;
475         }
476         case TYPE_FUNCTION:
477                 return true;
478
479         case TYPE_ARRAY: {
480                 const array_type_t *array_type = (const array_type_t*) type;
481
482                 return array_type->size == NULL;
483         }
484
485         case TYPE_ATOMIC:
486         case TYPE_POINTER:
487         case TYPE_ENUM:
488                 return false;
489
490         case TYPE_TYPEDEF:
491         case TYPE_TYPEOF:
492         case TYPE_BUILTIN:
493                 panic("is_type_incomplete called without typerefs skipped");
494         case TYPE_INVALID:
495                 break;
496         }
497
498         panic("invalid type found");
499 }
500
501 bool types_compatible(const type_t *type1, const type_t *type2)
502 {
503         if(type1 == type2)
504                 return true;
505
506         return true;
507 }
508
509 bool pointers_compatible(const type_t *type1, const type_t *type2)
510 {
511         assert(type1->type == TYPE_POINTER);
512         assert(type2->type == TYPE_POINTER);
513         pointer_type_t *pointer_type1 = (pointer_type_t*) type1;
514         pointer_type_t *pointer_type2 = (pointer_type_t*) type2;
515         return types_compatible(pointer_type1->points_to,
516                                 pointer_type2->points_to);
517 }
518
519 type_t *skip_typeref(type_t *type)
520 {
521         while(1) {
522                 switch(type->type) {
523                 case TYPE_TYPEDEF: {
524                         const typedef_type_t *typedef_type = (const typedef_type_t*) type;
525                         type = typedef_type->declaration->type;
526                         continue;
527                 }
528                 case TYPE_TYPEOF: {
529                         const typeof_type_t *typeof_type = (const typeof_type_t *) type;
530                         if(typeof_type->typeof_type != NULL) {
531                                 type = typeof_type->typeof_type;
532                         } else {
533                                 type = typeof_type->expression->datatype;
534                         }
535                         continue;
536                 }
537                 case TYPE_BUILTIN: {
538                         const builtin_type_t *builtin_type = (const builtin_type_t*) type;
539                         type = builtin_type->real_type;
540                         continue;
541                 }
542                 default:
543                         break;
544                 }
545                 break;
546         }
547
548         return type;
549 }
550
551
552
553 static type_t *identify_new_type(type_t *type)
554 {
555         type_t *result = typehash_insert(type);
556         if(result != type) {
557                 obstack_free(type_obst, type);
558         }
559         return result;
560 }
561
562 type_t *make_atomic_type(atomic_type_type_t type, type_qualifier_t qualifiers)
563 {
564         atomic_type_t *atomic_type
565                 = obstack_alloc(type_obst, sizeof(atomic_type[0]));
566         memset(atomic_type, 0, sizeof(atomic_type[0]));
567         atomic_type->type.type       = TYPE_ATOMIC;
568         atomic_type->type.qualifiers = qualifiers;
569         atomic_type->atype           = type;
570
571         return identify_new_type((type_t*) atomic_type);
572 }
573
574 type_t *make_pointer_type(type_t *points_to, type_qualifier_t qualifiers)
575 {
576         pointer_type_t *pointer_type
577                 = obstack_alloc(type_obst, sizeof(pointer_type[0]));
578         memset(pointer_type, 0, sizeof(pointer_type[0]));
579         pointer_type->type.type       = TYPE_POINTER;
580         pointer_type->type.qualifiers = qualifiers;
581         pointer_type->points_to       = points_to;
582
583         return identify_new_type((type_t*) pointer_type);
584 }
585
586 static __attribute__((unused))
587 void dbg_type(type_t *type)
588 {
589         FILE *old_out = out;
590         out = stderr;
591         print_type(type);
592         puts("\n");
593         fflush(stderr);
594         out = old_out;
595 }