started working on bitfields
[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         fputs(type->declaration->symbol->string, out);
247 }
248
249 static void print_typeof_type_pre(const typeof_type_t *const type)
250 {
251         fputs("typeof(", out);
252         if(type->expression != NULL) {
253                 assert(type->typeof_type == NULL);
254                 print_expression(type->expression);
255         } else {
256                 print_type(type->typeof_type);
257         }
258         fputc(')', out);
259 }
260
261 static void intern_print_type_pre(const type_t *const type, const bool top)
262 {
263         switch(type->kind) {
264         case TYPE_INVALID:
265                 fputs("invalid", out);
266                 return;
267         case TYPE_ENUM:
268                 print_type_enum(&type->enumt);
269                 return;
270         case TYPE_ATOMIC:
271                 print_atomic_type(&type->atomic);
272                 return;
273         case TYPE_COMPOUND_STRUCT:
274         case TYPE_COMPOUND_UNION:
275                 print_compound_type(&type->compound);
276                 return;
277         case TYPE_BUILTIN:
278                 fputs(type->builtin.symbol->string, out);
279                 return;
280         case TYPE_FUNCTION:
281                 print_function_type_pre(&type->function, top);
282                 return;
283         case TYPE_POINTER:
284                 print_pointer_type_pre(&type->pointer);
285                 return;
286         case TYPE_BITFIELD:
287                 intern_print_type_pre(type->bitfield.base, top);
288                 return;
289         case TYPE_ARRAY:
290                 print_array_type_pre(&type->array);
291                 return;
292         case TYPE_TYPEDEF:
293                 print_typedef_type_pre(&type->typedeft);
294                 return;
295         case TYPE_TYPEOF:
296                 print_typeof_type_pre(&type->typeoft);
297                 return;
298         }
299         fputs("unknown", out);
300 }
301
302 static void intern_print_type_post(const type_t *const type, const bool top)
303 {
304         switch(type->kind) {
305         case TYPE_FUNCTION:
306                 print_function_type_post(&type->function, NULL, top);
307                 return;
308         case TYPE_POINTER:
309                 print_pointer_type_post(&type->pointer);
310                 return;
311         case TYPE_ARRAY:
312                 print_array_type_post(&type->array);
313                 return;
314         case TYPE_BITFIELD:
315                 print_bitfield_type_post(&type->bitfield);
316                 return;
317         case TYPE_INVALID:
318         case TYPE_ATOMIC:
319         case TYPE_ENUM:
320         case TYPE_COMPOUND_STRUCT:
321         case TYPE_COMPOUND_UNION:
322         case TYPE_BUILTIN:
323         case TYPE_TYPEOF:
324         case TYPE_TYPEDEF:
325                 break;
326         }
327 }
328
329 void print_type(const type_t *const type)
330 {
331         print_type_ext(type, NULL, NULL);
332 }
333
334 void print_type_ext(const type_t *const type, const symbol_t *symbol,
335                     const context_t *context)
336 {
337         if(type == NULL) {
338                 fputs("nil type", out);
339                 return;
340         }
341
342         intern_print_type_pre(type, true);
343         if(symbol != NULL) {
344                 fputc(' ', out);
345                 fputs(symbol->string, out);
346         }
347         if(type->kind == TYPE_FUNCTION) {
348                 print_function_type_post(&type->function, context, true);
349         } else {
350                 intern_print_type_post(type, true);
351         }
352 }
353
354 static size_t get_type_size(type_t *type)
355 {
356         switch(type->kind) {
357         case TYPE_ATOMIC:          return sizeof(atomic_type_t);
358         case TYPE_COMPOUND_STRUCT:
359         case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
360         case TYPE_ENUM:            return sizeof(enum_type_t);
361         case TYPE_FUNCTION:        return sizeof(function_type_t);
362         case TYPE_POINTER:         return sizeof(pointer_type_t);
363         case TYPE_ARRAY:           return sizeof(array_type_t);
364         case TYPE_BUILTIN:         return sizeof(builtin_type_t);
365         case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
366         case TYPE_TYPEOF:          return sizeof(typeof_type_t);
367         case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
368         case TYPE_INVALID:         panic("invalid type found");
369         }
370         panic("unknown type found");
371 }
372
373 /**
374  * duplicates a type
375  * note that this does not produce a deep copy!
376  */
377 type_t *duplicate_type(type_t *type)
378 {
379         size_t size = get_type_size(type);
380
381         type_t *copy = obstack_alloc(type_obst, size);
382         memcpy(copy, type, size);
383
384         return copy;
385 }
386
387 type_t *get_unqualified_type(type_t *type)
388 {
389         if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
390                 return type;
391
392         type_t *unqualified_type          = duplicate_type(type);
393         unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
394
395         type_t *result = typehash_insert(unqualified_type);
396         if(result != unqualified_type) {
397                 obstack_free(type_obst, unqualified_type);
398         }
399
400         return result;
401 }
402
403 bool type_valid(const type_t *type)
404 {
405         return type->kind != TYPE_INVALID;
406 }
407
408 bool is_type_integer(const type_t *type)
409 {
410         assert(!is_typeref(type));
411
412         if(type->kind == TYPE_ENUM)
413                 return true;
414
415         if(type->kind != TYPE_ATOMIC)
416                 return false;
417
418         switch(type->atomic.atype) {
419         case ATOMIC_TYPE_BOOL:
420         case ATOMIC_TYPE_CHAR:
421         case ATOMIC_TYPE_SCHAR:
422         case ATOMIC_TYPE_UCHAR:
423         case ATOMIC_TYPE_SHORT:
424         case ATOMIC_TYPE_USHORT:
425         case ATOMIC_TYPE_INT:
426         case ATOMIC_TYPE_UINT:
427         case ATOMIC_TYPE_LONG:
428         case ATOMIC_TYPE_ULONG:
429         case ATOMIC_TYPE_LONGLONG:
430         case ATOMIC_TYPE_ULONGLONG:
431                 return true;
432         default:
433                 return false;
434         }
435 }
436
437 bool is_type_floating(const type_t *type)
438 {
439         assert(!is_typeref(type));
440
441         if(type->kind != TYPE_ATOMIC)
442                 return false;
443
444         switch(type->atomic.atype) {
445         case ATOMIC_TYPE_FLOAT:
446         case ATOMIC_TYPE_DOUBLE:
447         case ATOMIC_TYPE_LONG_DOUBLE:
448 #ifdef PROVIDE_COMPLEX
449         case ATOMIC_TYPE_FLOAT_COMPLEX:
450         case ATOMIC_TYPE_DOUBLE_COMPLEX:
451         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
452         case ATOMIC_TYPE_FLOAT_IMAGINARY:
453         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
454         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
455 #endif
456                 return true;
457         default:
458                 return false;
459         }
460 }
461
462 bool is_type_signed(const type_t *type)
463 {
464         assert(!is_typeref(type));
465
466         /* enum types are int for now */
467         if(type->kind == TYPE_ENUM)
468                 return true;
469
470         if(type->kind != TYPE_ATOMIC)
471                 return false;
472
473         switch(type->atomic.atype) {
474         case ATOMIC_TYPE_CHAR:
475         case ATOMIC_TYPE_SCHAR:
476         case ATOMIC_TYPE_SHORT:
477         case ATOMIC_TYPE_INT:
478         case ATOMIC_TYPE_LONG:
479         case ATOMIC_TYPE_LONGLONG:
480         case ATOMIC_TYPE_FLOAT:
481         case ATOMIC_TYPE_DOUBLE:
482         case ATOMIC_TYPE_LONG_DOUBLE:
483 #ifdef PROVIDE_COMPLEX
484         case ATOMIC_TYPE_FLOAT_COMPLEX:
485         case ATOMIC_TYPE_DOUBLE_COMPLEX:
486         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
487         case ATOMIC_TYPE_FLOAT_IMAGINARY:
488         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
489         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
490 #endif
491                 return true;
492
493         case ATOMIC_TYPE_BOOL:
494         case ATOMIC_TYPE_UCHAR:
495         case ATOMIC_TYPE_USHORT:
496         case ATOMIC_TYPE_UINT:
497         case ATOMIC_TYPE_ULONG:
498         case ATOMIC_TYPE_ULONGLONG:
499                 return false;
500
501         case ATOMIC_TYPE_VOID:
502         case ATOMIC_TYPE_INVALID:
503         case ATOMIC_TYPE_LAST:
504                 return false;
505         }
506
507         panic("invalid atomic type found");
508         return false;
509 }
510
511 bool is_type_arithmetic(const type_t *type)
512 {
513         assert(!is_typeref(type));
514
515         if(type->kind == TYPE_BITFIELD)
516                 return true;
517
518         if(is_type_integer(type) || is_type_floating(type))
519                 return true;
520
521         return false;
522 }
523
524 bool is_type_scalar(const type_t *type)
525 {
526         assert(!is_typeref(type));
527
528         switch (type->kind) {
529                 case TYPE_POINTER: return true;
530                 case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
531                 default:            break;
532         }
533
534         return is_type_arithmetic(type);
535 }
536
537 bool is_type_incomplete(const type_t *type)
538 {
539         assert(!is_typeref(type));
540
541         switch(type->kind) {
542         case TYPE_COMPOUND_STRUCT:
543         case TYPE_COMPOUND_UNION: {
544                 const compound_type_t *compound_type = &type->compound;
545                 declaration_t         *declaration   = compound_type->declaration;
546                 return !declaration->init.is_defined;
547         }
548         case TYPE_BITFIELD:
549         case TYPE_FUNCTION:
550                 return true;
551
552         case TYPE_ARRAY:
553                 return type->array.size == NULL;
554
555         case TYPE_ATOMIC:
556                 return type->atomic.atype == ATOMIC_TYPE_VOID;
557
558         case TYPE_POINTER:
559         case TYPE_ENUM:
560         case TYPE_BUILTIN:
561                 return false;
562
563         case TYPE_TYPEDEF:
564         case TYPE_TYPEOF:
565                 panic("is_type_incomplete called without typerefs skipped");
566         case TYPE_INVALID:
567                 break;
568         }
569
570         panic("invalid type found");
571 }
572
573 static bool function_types_compatible(const function_type_t *func1,
574                                       const function_type_t *func2)
575 {
576         if(!types_compatible(func1->return_type, func2->return_type))
577                 return false;
578
579         /* can parameters be compared? */
580         if(func1->unspecified_parameters || func2->unspecified_parameters)
581                 return true;
582
583         if(func1->variadic != func2->variadic)
584                 return false;
585
586         /* TODO: handling of unspecified parameters not correct yet */
587
588         /* all argument types must be compatible */
589         function_parameter_t *parameter1 = func1->parameters;
590         function_parameter_t *parameter2 = func2->parameters;
591         for( ; parameter1 != NULL && parameter2 != NULL;
592                         parameter1 = parameter1->next, parameter2 = parameter2->next) {
593                 type_t *parameter1_type = skip_typeref(parameter1->type);
594                 type_t *parameter2_type = skip_typeref(parameter2->type);
595
596                 parameter1_type = get_unqualified_type(parameter1_type);
597                 parameter2_type = get_unqualified_type(parameter2_type);
598
599                 if(!types_compatible(parameter1_type, parameter2_type))
600                         return false;
601         }
602         /* same number of arguments? */
603         if(parameter1 != NULL || parameter2 != NULL)
604                 return false;
605
606         return true;
607 }
608
609 static bool array_types_compatible(const array_type_t *array1,
610                                    const array_type_t *array2)
611 {
612         type_t *element_type1 = skip_typeref(array1->element_type);
613         type_t *element_type2 = skip_typeref(array2->element_type);
614         if(!types_compatible(element_type1, element_type2))
615                 return false;
616
617         if(array1->size != NULL && array2->size != NULL) {
618                 /* TODO: check if size expression evaluate to the same value
619                  * if they are constant */
620         }
621
622         return true;
623 }
624
625 bool types_compatible(const type_t *type1, const type_t *type2)
626 {
627         assert(!is_typeref(type1));
628         assert(!is_typeref(type2));
629
630         /* shortcut: the same type is always compatible */
631         if(type1 == type2)
632                 return true;
633
634         if(type1->base.qualifiers != type2->base.qualifiers)
635                 return false;
636         if(type1->kind != type2->kind)
637                 return false;
638
639         switch(type1->kind) {
640         case TYPE_FUNCTION:
641                 return function_types_compatible(&type1->function, &type2->function);
642         case TYPE_ATOMIC:
643                 return type1->atomic.atype == type2->atomic.atype;
644         case TYPE_ARRAY:
645                 return array_types_compatible(&type1->array, &type2->array);
646         case TYPE_POINTER:
647                 return types_compatible(type1->pointer.points_to,
648                                         type2->pointer.points_to);
649         case TYPE_COMPOUND_STRUCT:
650         case TYPE_COMPOUND_UNION:
651         case TYPE_ENUM:
652         case TYPE_BUILTIN:
653                 /* TODO: not implemented */
654                 break;
655
656         case TYPE_BITFIELD:
657                 /* not sure if this makes sense or is even needed, implement it if you
658                  * really need it! */
659                 panic("type compatibility check for bitfield type");
660
661         case TYPE_INVALID:
662                 panic("invalid type found in compatible types");
663         case TYPE_TYPEDEF:
664         case TYPE_TYPEOF:
665                 panic("typerefs not skipped in compatible types?!?");
666         }
667
668         /* TODO: incomplete */
669         return false;
670 }
671
672 bool pointers_compatible(const type_t *type1, const type_t *type2)
673 {
674         assert(!is_typeref(type1));
675         assert(!is_typeref(type2));
676
677         assert(type1->kind == TYPE_POINTER);
678         assert(type2->kind == TYPE_POINTER);
679         /* TODO */
680         return true;
681 }
682
683 type_t *skip_typeref(type_t *type)
684 {
685         unsigned qualifiers = type->base.qualifiers;
686
687         while(true) {
688                 switch(type->kind) {
689                 case TYPE_TYPEDEF: {
690                         qualifiers |= type->base.qualifiers;
691                         const typedef_type_t *typedef_type = &type->typedeft;
692                         if(typedef_type->resolved_type != NULL) {
693                                 type = typedef_type->resolved_type;
694                                 break;
695                         }
696                         type = typedef_type->declaration->type;
697                         continue;
698                 }
699                 case TYPE_TYPEOF: {
700                         const typeof_type_t *typeof_type = &type->typeoft;
701                         if(typeof_type->typeof_type != NULL) {
702                                 type = typeof_type->typeof_type;
703                         } else {
704                                 type = typeof_type->expression->base.datatype;
705                         }
706                         continue;
707                 }
708                 default:
709                         break;
710                 }
711                 break;
712         }
713
714         return type;
715 }
716
717
718
719 static type_t *identify_new_type(type_t *type)
720 {
721         type_t *result = typehash_insert(type);
722         if(result != type) {
723                 obstack_free(type_obst, type);
724         }
725         return result;
726 }
727
728 type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
729 {
730         type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
731         memset(type, 0, sizeof(atomic_type_t));
732
733         type->kind            = TYPE_ATOMIC;
734         type->base.qualifiers = qualifiers;
735         type->atomic.atype    = atype;
736
737         return identify_new_type(type);
738 }
739
740 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
741 {
742         type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
743         memset(type, 0, sizeof(pointer_type_t));
744
745         type->kind              = TYPE_POINTER;
746         type->base.qualifiers   = qualifiers;
747         type->pointer.points_to = points_to;
748
749         return identify_new_type(type);
750 }
751
752 static __attribute__((unused))
753 void dbg_type(type_t *type)
754 {
755         FILE *old_out = out;
756         out = stderr;
757         print_type(type);
758         puts("\n");
759         fflush(stderr);
760         out = old_out;
761 }