8568ed6274128eb61f00f307bdcb5e292b83f703
[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
14 static void intern_print_type_pre(const type_t *type, bool top);
15 static void intern_print_type_post(const type_t *type, bool top);
16
17 void init_types(void)
18 {
19         obstack_init(type_obst);
20 }
21
22 void exit_types(void)
23 {
24         obstack_free(type_obst, NULL);
25 }
26
27 void type_set_output(FILE *stream)
28 {
29         out = stream;
30 }
31
32 void inc_type_visited(void)
33 {
34         type_visited++;
35 }
36
37 void print_type_qualifiers(type_qualifiers_t qualifiers)
38 {
39         if(qualifiers & TYPE_QUALIFIER_CONST)    fputs("const ",    out);
40         if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
41         if(qualifiers & TYPE_QUALIFIER_RESTRICT) fputs("restrict ", out);
42 }
43
44 static
45 void print_atomic_type(const atomic_type_t *type)
46 {
47         print_type_qualifiers(type->type.qualifiers);
48
49         const char *s;
50         switch(type->atype) {
51         case ATOMIC_TYPE_INVALID:     s = "INVALIDATOMIC";      break;
52         case ATOMIC_TYPE_VOID:        s = "void";               break;
53         case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
54         case ATOMIC_TYPE_CHAR:        s = "char";               break;
55         case ATOMIC_TYPE_SCHAR:       s = "signed char";        break;
56         case ATOMIC_TYPE_UCHAR:       s = "unsigned char";      break;
57         case ATOMIC_TYPE_INT:         s = "int";                break;
58         case ATOMIC_TYPE_UINT:        s = "unsigned int";       break;
59         case ATOMIC_TYPE_SHORT:       s = "short";              break;
60         case ATOMIC_TYPE_USHORT:      s = "unsigned short";     break;
61         case ATOMIC_TYPE_LONG:        s = "long";               break;
62         case ATOMIC_TYPE_ULONG:       s = "unsigned long";      break;
63         case ATOMIC_TYPE_LONGLONG:    s = "long long";          break;
64         case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long"; break;
65         case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";        break;
66         case ATOMIC_TYPE_FLOAT:       s = "float";              break;
67         case ATOMIC_TYPE_DOUBLE:      s = "double";             break;
68         default:                      s = "UNKNOWNATOMIC";      break;
69         }
70         fputs(s, out);
71 }
72
73 static void print_function_type_pre(const function_type_t *type, bool top)
74 {
75         print_type_qualifiers(type->type.qualifiers);
76
77         intern_print_type_pre(type->return_type, false);
78
79         /* don't emit braces if we're the toplevel type... */
80         if(!top)
81                 fputc('(', out);
82 }
83
84 static void print_function_type_post(const function_type_t *type,
85                                      const context_t *context, bool top)
86 {
87         intern_print_type_post(type->return_type, false);
88         /* don't emit braces if we're the toplevel type... */
89         if(!top)
90                 fputc(')', out);
91
92         fputc('(', out);
93
94         int                 first     = 1;
95         if(context == NULL) {
96                 function_parameter_t *parameter = type->parameters;
97                 for( ; parameter != NULL; parameter = parameter->next) {
98                         if(first) {
99                                 first = 0;
100                         } else {
101                                 fputs(", ", out);
102                         }
103                         print_type(parameter->type);
104                 }
105         } else {
106                 declaration_t *parameter = context->declarations;
107                 for( ; parameter != NULL; parameter = parameter->next) {
108                         if(first) {
109                                 first = 0;
110                         } else {
111                                 fputs(", ", out);
112                         }
113                         print_type_ext(parameter->type, parameter->symbol,
114                                        &parameter->context);
115                 }
116         }
117         if(type->variadic) {
118                 if(first) {
119                         first = 0;
120                 } else {
121                         fputs(", ", out);
122                 }
123                 fputs("...", out);
124         }
125         if(first && !type->unspecified_parameters) {
126                 fputs("void", out);
127         }
128         fputc(')', out);
129 }
130
131 static void print_pointer_type_pre(const pointer_type_t *type)
132 {
133         intern_print_type_pre(type->points_to, false);
134         fputs("*", out);
135         print_type_qualifiers(type->type.qualifiers);
136 }
137
138 static void print_pointer_type_post(const pointer_type_t *type)
139 {
140         intern_print_type_post(type->points_to, false);
141 }
142
143 static void print_array_type_pre(const array_type_t *type)
144 {
145         intern_print_type_pre(type->element_type, false);
146 }
147
148 static void print_array_type_post(const array_type_t *type)
149 {
150         fputc('[', out);
151         if(type->is_static) {
152                 fputs("static ", out);
153         }
154         print_type_qualifiers(type->type.qualifiers);
155         if(type->size != NULL) {
156                 print_expression(type->size);
157         }
158         fputc(']', out);
159         intern_print_type_post(type->element_type, false);
160 }
161
162 static void print_bitfield_type_post(const bitfield_type_t *type)
163 {
164         fputs(" : ", out);
165         print_expression(type->size);
166         intern_print_type_post(type->base, false);
167 }
168
169 void print_enum_definition(const declaration_t *declaration)
170 {
171         fputs("{\n", out);
172
173         change_indent(1);
174
175         declaration_t *entry = declaration->next;
176         for( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
177                entry = entry->next) {
178
179                 print_indent();
180                 fprintf(out, "%s", entry->symbol->string);
181                 if(entry->init.initializer != NULL) {
182                         fprintf(out, " = ");
183                         print_expression(entry->init.enum_value);
184                 }
185                 fprintf(out, ",\n");
186         }
187
188         change_indent(-1);
189         print_indent();
190         fputs("}", out);
191 }
192
193 static void print_type_enum(const enum_type_t *type)
194 {
195         print_type_qualifiers(type->type.qualifiers);
196         fputs("enum ", out);
197
198         declaration_t *declaration = type->declaration;
199         symbol_t      *symbol      = declaration->symbol;
200         if(symbol != NULL) {
201                 fputs(symbol->string, out);
202         } else {
203                 print_enum_definition(declaration);
204         }
205 }
206
207 void print_compound_definition(const declaration_t *declaration)
208 {
209         fputs("{\n", out);
210         change_indent(1);
211
212         declaration_t *iter = declaration->context.declarations;
213         for( ; iter != NULL; iter = iter->next) {
214                 print_indent();
215                 print_declaration(iter);
216                 fputc('\n', out);
217         }
218
219         change_indent(-1);
220         print_indent();
221         fputs("}", out);
222 }
223
224 static void print_compound_type(const compound_type_t *type)
225 {
226         print_type_qualifiers(type->type.qualifiers);
227
228         if(type->type.kind == TYPE_COMPOUND_STRUCT) {
229                 fputs("struct ", out);
230         } else {
231                 assert(type->type.kind == TYPE_COMPOUND_UNION);
232                 fputs("union ", out);
233         }
234
235         declaration_t *declaration = type->declaration;
236         symbol_t      *symbol      = declaration->symbol;
237         if(symbol != NULL) {
238                 fputs(symbol->string, out);
239         } else {
240                 print_compound_definition(declaration);
241         }
242 }
243
244 static void print_typedef_type_pre(const typedef_type_t *const type)
245 {
246         print_type_qualifiers(type->type.qualifiers);
247         fputs(type->declaration->symbol->string, out);
248 }
249
250 static void print_typeof_type_pre(const typeof_type_t *const type)
251 {
252         fputs("typeof(", out);
253         if(type->expression != NULL) {
254                 assert(type->typeof_type == NULL);
255                 print_expression(type->expression);
256         } else {
257                 print_type(type->typeof_type);
258         }
259         fputc(')', out);
260 }
261
262 static void intern_print_type_pre(const type_t *const type, const bool top)
263 {
264         switch(type->kind) {
265         case TYPE_INVALID:
266                 fputs("invalid", out);
267                 return;
268         case TYPE_ENUM:
269                 print_type_enum(&type->enumt);
270                 return;
271         case TYPE_ATOMIC:
272                 print_atomic_type(&type->atomic);
273                 return;
274         case TYPE_COMPOUND_STRUCT:
275         case TYPE_COMPOUND_UNION:
276                 print_compound_type(&type->compound);
277                 return;
278         case TYPE_BUILTIN:
279                 fputs(type->builtin.symbol->string, out);
280                 return;
281         case TYPE_FUNCTION:
282                 print_function_type_pre(&type->function, top);
283                 return;
284         case TYPE_POINTER:
285                 print_pointer_type_pre(&type->pointer);
286                 return;
287         case TYPE_BITFIELD:
288                 intern_print_type_pre(type->bitfield.base, top);
289                 return;
290         case TYPE_ARRAY:
291                 print_array_type_pre(&type->array);
292                 return;
293         case TYPE_TYPEDEF:
294                 print_typedef_type_pre(&type->typedeft);
295                 return;
296         case TYPE_TYPEOF:
297                 print_typeof_type_pre(&type->typeoft);
298                 return;
299         }
300         fputs("unknown", out);
301 }
302
303 static void intern_print_type_post(const type_t *const type, const bool top)
304 {
305         switch(type->kind) {
306         case TYPE_FUNCTION:
307                 print_function_type_post(&type->function, NULL, top);
308                 return;
309         case TYPE_POINTER:
310                 print_pointer_type_post(&type->pointer);
311                 return;
312         case TYPE_ARRAY:
313                 print_array_type_post(&type->array);
314                 return;
315         case TYPE_BITFIELD:
316                 print_bitfield_type_post(&type->bitfield);
317                 return;
318         case TYPE_INVALID:
319         case TYPE_ATOMIC:
320         case TYPE_ENUM:
321         case TYPE_COMPOUND_STRUCT:
322         case TYPE_COMPOUND_UNION:
323         case TYPE_BUILTIN:
324         case TYPE_TYPEOF:
325         case TYPE_TYPEDEF:
326                 break;
327         }
328 }
329
330 void print_type(const type_t *const type)
331 {
332         print_type_ext(type, NULL, NULL);
333 }
334
335 void print_type_ext(const type_t *const type, const symbol_t *symbol,
336                     const context_t *context)
337 {
338         if(type == NULL) {
339                 fputs("nil type", out);
340                 return;
341         }
342
343         intern_print_type_pre(type, true);
344         if(symbol != NULL) {
345                 fputc(' ', out);
346                 fputs(symbol->string, out);
347         }
348         if(type->kind == TYPE_FUNCTION) {
349                 print_function_type_post(&type->function, context, true);
350         } else {
351                 intern_print_type_post(type, true);
352         }
353 }
354
355 static size_t get_type_size(type_t *type)
356 {
357         switch(type->kind) {
358         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
359         case TYPE_COMPOUND_STRUCT:
360         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
361         case TYPE_ENUM:            return sizeof(enum_type_t);
362         case TYPE_FUNCTION:        return sizeof(function_type_t);
363         case TYPE_POINTER:         return sizeof(pointer_type_t);
364         case TYPE_ARRAY:           return sizeof(array_type_t);
365         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
366         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
367         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
368         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
369         case TYPE_INVALID:         panic("invalid type found");
370         }
371         panic("unknown type found");
372 }
373
374 /**
375  * duplicates a type
376  * note that this does not produce a deep copy!
377  */
378 type_t *duplicate_type(type_t *type)
379 {
380         size_t size = get_type_size(type);
381
382         type_t *copy = obstack_alloc(type_obst, size);
383         memcpy(copy, type, size);
384
385         return copy;
386 }
387
388 type_t *get_unqualified_type(type_t *type)
389 {
390         if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
391                 return type;
392
393         type_t *unqualified_type          = duplicate_type(type);
394         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
395
396         type_t *result = typehash_insert(unqualified_type);
397         if(result != unqualified_type) {
398                 obstack_free(type_obst, unqualified_type);
399         }
400
401         return result;
402 }
403
404 bool type_valid(const type_t *type)
405 {
406         return type->kind != TYPE_INVALID;
407 }
408
409 bool is_type_integer(const type_t *type)
410 {
411         assert(!is_typeref(type));
412
413         if(type->kind == TYPE_ENUM)
414                 return true;
415
416         if(type->kind != TYPE_ATOMIC)
417                 return false;
418
419         switch(type->atomic.atype) {
420         case ATOMIC_TYPE_BOOL:
421         case ATOMIC_TYPE_CHAR:
422         case ATOMIC_TYPE_SCHAR:
423         case ATOMIC_TYPE_UCHAR:
424         case ATOMIC_TYPE_SHORT:
425         case ATOMIC_TYPE_USHORT:
426         case ATOMIC_TYPE_INT:
427         case ATOMIC_TYPE_UINT:
428         case ATOMIC_TYPE_LONG:
429         case ATOMIC_TYPE_ULONG:
430         case ATOMIC_TYPE_LONGLONG:
431         case ATOMIC_TYPE_ULONGLONG:
432                 return true;
433         default:
434                 return false;
435         }
436 }
437
438 bool is_type_floating(const type_t *type)
439 {
440         assert(!is_typeref(type));
441
442         if(type->kind != TYPE_ATOMIC)
443                 return false;
444
445         switch(type->atomic.atype) {
446         case ATOMIC_TYPE_FLOAT:
447         case ATOMIC_TYPE_DOUBLE:
448         case ATOMIC_TYPE_LONG_DOUBLE:
449 #ifdef PROVIDE_COMPLEX
450         case ATOMIC_TYPE_FLOAT_COMPLEX:
451         case ATOMIC_TYPE_DOUBLE_COMPLEX:
452         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
453         case ATOMIC_TYPE_FLOAT_IMAGINARY:
454         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
455         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
456 #endif
457                 return true;
458         default:
459                 return false;
460         }
461 }
462
463 bool is_type_signed(const type_t *type)
464 {
465         assert(!is_typeref(type));
466
467         /* enum types are int for now */
468         if(type->kind == TYPE_ENUM)
469                 return true;
470
471         if(type->kind != TYPE_ATOMIC)
472                 return false;
473
474         switch(type->atomic.atype) {
475         case ATOMIC_TYPE_CHAR:
476         case ATOMIC_TYPE_SCHAR:
477         case ATOMIC_TYPE_SHORT:
478         case ATOMIC_TYPE_INT:
479         case ATOMIC_TYPE_LONG:
480         case ATOMIC_TYPE_LONGLONG:
481         case ATOMIC_TYPE_FLOAT:
482         case ATOMIC_TYPE_DOUBLE:
483         case ATOMIC_TYPE_LONG_DOUBLE:
484 #ifdef PROVIDE_COMPLEX
485         case ATOMIC_TYPE_FLOAT_COMPLEX:
486         case ATOMIC_TYPE_DOUBLE_COMPLEX:
487         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
488         case ATOMIC_TYPE_FLOAT_IMAGINARY:
489         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
490         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
491 #endif
492                 return true;
493
494         case ATOMIC_TYPE_BOOL:
495         case ATOMIC_TYPE_UCHAR:
496         case ATOMIC_TYPE_USHORT:
497         case ATOMIC_TYPE_UINT:
498         case ATOMIC_TYPE_ULONG:
499         case ATOMIC_TYPE_ULONGLONG:
500                 return false;
501
502         case ATOMIC_TYPE_VOID:
503         case ATOMIC_TYPE_INVALID:
504         case ATOMIC_TYPE_LAST:
505                 return false;
506         }
507
508         panic("invalid atomic type found");
509         return false;
510 }
511
512 bool is_type_arithmetic(const type_t *type)
513 {
514         assert(!is_typeref(type));
515
516         if(type->kind == TYPE_BITFIELD)
517                 return true;
518
519         if(is_type_integer(type) || is_type_floating(type))
520                 return true;
521
522         return false;
523 }
524
525 bool is_type_scalar(const type_t *type)
526 {
527         assert(!is_typeref(type));
528
529         switch (type->kind) {
530                 case TYPE_POINTER: return true;
531                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
532                 default:            break;
533         }
534
535         return is_type_arithmetic(type);
536 }
537
538 bool is_type_incomplete(const type_t *type)
539 {
540         assert(!is_typeref(type));
541
542         switch(type->kind) {
543         case TYPE_COMPOUND_STRUCT:
544         case TYPE_COMPOUND_UNION: {
545                 const compound_type_t *compound_type = &type->compound;
546                 declaration_t         *declaration   = compound_type->declaration;
547                 return !declaration->init.is_defined;
548         }
549         case TYPE_BITFIELD:
550         case TYPE_FUNCTION:
551                 return true;
552
553         case TYPE_ARRAY:
554                 return type->array.size == NULL;
555
556         case TYPE_ATOMIC:
557                 return type->atomic.atype == ATOMIC_TYPE_VOID;
558
559         case TYPE_POINTER:
560         case TYPE_ENUM:
561         case TYPE_BUILTIN:
562                 return false;
563
564         case TYPE_TYPEDEF:
565         case TYPE_TYPEOF:
566                 panic("is_type_incomplete called without typerefs skipped");
567         case TYPE_INVALID:
568                 break;
569         }
570
571         panic("invalid type found");
572 }
573
574 static bool function_types_compatible(const function_type_t *func1,
575                                       const function_type_t *func2)
576 {
577         const type_t* const ret1 = skip_typeref(func1->return_type);
578         const type_t* const ret2 = skip_typeref(func2->return_type);
579         if (!types_compatible(ret1, ret2))
580                 return false;
581
582         /* can parameters be compared? */
583         if(func1->unspecified_parameters || func2->unspecified_parameters)
584                 return true;
585
586         if(func1->variadic != func2->variadic)
587                 return false;
588
589         /* TODO: handling of unspecified parameters not correct yet */
590
591         /* all argument types must be compatible */
592         function_parameter_t *parameter1 = func1->parameters;
593         function_parameter_t *parameter2 = func2->parameters;
594         for( ; parameter1 != NULL && parameter2 != NULL;
595                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
596                 type_t *parameter1_type = skip_typeref(parameter1->type);
597                 type_t *parameter2_type = skip_typeref(parameter2->type);
598
599                 parameter1_type = get_unqualified_type(parameter1_type);
600                 parameter2_type = get_unqualified_type(parameter2_type);
601
602                 if(!types_compatible(parameter1_type, parameter2_type))
603                         return false;
604         }
605         /* same number of arguments? */
606         if(parameter1 != NULL || parameter2 != NULL)
607                 return false;
608
609         return true;
610 }
611
612 static bool array_types_compatible(const array_type_t *array1,
613                                    const array_type_t *array2)
614 {
615         type_t *element_type1 = skip_typeref(array1->element_type);
616         type_t *element_type2 = skip_typeref(array2->element_type);
617         if(!types_compatible(element_type1, element_type2))
618                 return false;
619
620         if(array1->size != NULL && array2->size != NULL) {
621                 /* TODO: check if size expression evaluate to the same value
622                  * if they are constant */
623         }
624
625         return true;
626 }
627
628 bool types_compatible(const type_t *type1, const type_t *type2)
629 {
630         assert(!is_typeref(type1));
631         assert(!is_typeref(type2));
632
633         /* shortcut: the same type is always compatible */
634         if(type1 == type2)
635                 return true;
636
637         if(type1->base.qualifiers != type2->base.qualifiers)
638                 return false;
639         if(type1->kind != type2->kind)
640                 return false;
641
642         switch(type1->kind) {
643         case TYPE_FUNCTION:
644                 return function_types_compatible(&type1->function, &type2->function);
645         case TYPE_ATOMIC:
646                 return type1->atomic.atype == type2->atomic.atype;
647         case TYPE_ARRAY:
648                 return array_types_compatible(&type1->array, &type2->array);
649
650         case TYPE_POINTER: {
651                 const type_t *const to1 = skip_typeref(type1->pointer.points_to);
652                 const type_t *const to2 = skip_typeref(type2->pointer.points_to);
653                 return types_compatible(to1, to2);
654         }
655
656         case TYPE_COMPOUND_STRUCT:
657         case TYPE_COMPOUND_UNION:
658         case TYPE_ENUM:
659         case TYPE_BUILTIN:
660                 /* TODO: not implemented */
661                 break;
662
663         case TYPE_BITFIELD:
664                 /* not sure if this makes sense or is even needed, implement it if you
665                  * really need it! */
666                 panic("type compatibility check for bitfield type");
667
668         case TYPE_INVALID:
669                 panic("invalid type found in compatible types");
670         case TYPE_TYPEDEF:
671         case TYPE_TYPEOF:
672                 panic("typerefs not skipped in compatible types?!?");
673         }
674
675         /* TODO: incomplete */
676         return false;
677 }
678
679 bool pointers_compatible(const type_t *type1, const type_t *type2)
680 {
681         assert(!is_typeref(type1));
682         assert(!is_typeref(type2));
683
684         assert(type1->kind == TYPE_POINTER);
685         assert(type2->kind == TYPE_POINTER);
686         /* TODO */
687         return true;
688 }
689
690 type_t *skip_typeref(type_t *type)
691 {
692         unsigned qualifiers = type->base.qualifiers;
693
694         while(true) {
695                 switch(type->kind) {
696                 case TYPE_TYPEDEF: {
697                         qualifiers |= type->base.qualifiers;
698                         const typedef_type_t *typedef_type = &type->typedeft;
699                         if(typedef_type->resolved_type != NULL) {
700                                 type = typedef_type->resolved_type;
701                                 break;
702                         }
703                         type = typedef_type->declaration->type;
704                         continue;
705                 }
706                 case TYPE_TYPEOF: {
707                         const typeof_type_t *typeof_type = &type->typeoft;
708                         if(typeof_type->typeof_type != NULL) {
709                                 type = typeof_type->typeof_type;
710                         } else {
711                                 type = typeof_type->expression->base.datatype;
712                         }
713                         continue;
714                 }
715                 default:
716                         break;
717                 }
718                 break;
719         }
720
721         return type;
722 }
723
724
725
726 static type_t *identify_new_type(type_t *type)
727 {
728         type_t *result = typehash_insert(type);
729         if(result != type) {
730                 obstack_free(type_obst, type);
731         }
732         return result;
733 }
734
735 type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
736 {
737         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
738         memset(type, 0, sizeof(atomic_type_t));
739
740         type->kind            = TYPE_ATOMIC;
741         type->base.qualifiers = qualifiers;
742         type->atomic.atype    = atype;
743
744         return identify_new_type(type);
745 }
746
747 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
748 {
749         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
750         memset(type, 0, sizeof(pointer_type_t));
751
752         type->kind              = TYPE_POINTER;
753         type->base.qualifiers   = qualifiers;
754         type->pointer.points_to = points_to;
755
756         return identify_new_type(type);
757 }
758
759 static __attribute__((unused))
760 void dbg_type(type_t *type)
761 {
762         FILE *old_out = out;
763         out = stderr;
764         print_type(type);
765         puts("\n");
766         fflush(stderr);
767         out = old_out;
768 }