make it compile
[cparser] / type.c
1 #include <config.h>
2
3 #include "type_t.h"
4 #include "adt/error.h"
5
6 static struct obstack  _type_obst;
7 struct obstack        *type_obst = &_type_obst;
8
9 void init_types()
10 {
11         obstack_init(type_obst);
12 }
13
14 void exit_types()
15 {
16         obstack_free(type_obst, NULL);
17 }
18
19 static
20 void print_type_qualifiers(FILE *out, const type_t *type)
21 {
22         unsigned qualifiers = type->qualifiers;
23         if(qualifiers & TYPE_QUALIFIER_CONST) {
24                 fputs("const ", out);
25         }
26         if(qualifiers & TYPE_QUALIFIER_VOLATILE) {
27                 fputs("volatile ", out);
28         }
29         if(qualifiers & TYPE_QUALIFIER_RESTRICT) {
30                 fputs("restrict ", out);
31         }
32         if(qualifiers & TYPE_QUALIFIER_INLINE) {
33                 fputs("inline ", out);
34         }
35 }
36
37 static
38 void print_atomic_type(FILE *out, const atomic_type_t *type)
39 {
40         print_type_qualifiers(out, & type->type);
41
42         switch(type->atype) {
43         case ATOMIC_TYPE_INVALID:   fputs("INVALIDATOMIC", out); break;
44         case ATOMIC_TYPE_BOOL:      fputs("bool", out); break;
45         case ATOMIC_TYPE_CHAR:      fputs("char", out); break;
46         case ATOMIC_TYPE_SCHAR:     fputs("signed char", out); break;
47         case ATOMIC_TYPE_UCHAR:     fputs("unsigned char", out); break;
48         case ATOMIC_TYPE_INT:       fputs("int", out); break;
49         case ATOMIC_TYPE_UINT:      fputs("unsigned int", out); break;
50         case ATOMIC_TYPE_SHORT:     fputs("short", out); break;
51         case ATOMIC_TYPE_USHORT:    fputs("unsigned short", out); break;
52         case ATOMIC_TYPE_LONG:      fputs("long", out); break;
53         case ATOMIC_TYPE_ULONG:     fputs("unsigned long", out); break;
54         case ATOMIC_TYPE_LONGLONG:  fputs("long long", out); break;
55         case ATOMIC_TYPE_ULONGLONG: fputs("unsigned long long", out); break;
56         case ATOMIC_TYPE_FLOAT:     fputs("float", out); break;
57         case ATOMIC_TYPE_DOUBLE:    fputs("double", out); break;
58         default:                    fputs("UNKNOWNATOMIC", out); break;
59         }
60 }
61
62 static
63 void print_method_type(FILE *out, const method_type_t *type)
64 {
65         print_type_qualifiers(out, & type->type);
66
67         fputs("<", out);
68         print_type(out, type->result_type);
69         fputs(" ", out);
70
71         if(type->abi_style != NULL) {
72                 fprintf(out, "\"%s\" ", type->abi_style);
73         }
74         fputs("method(", out);
75         method_parameter_type_t *param_type = type->parameter_types;
76         int first = 1;
77         while(param_type != NULL) {
78                 if(first) {
79                         first = 0;
80                 } else {
81                         fputs(", ", out);
82                 }
83                 print_type(out, param_type->type);
84                 param_type = param_type->next;
85         }
86         fputs(")>", out);
87 }
88
89 static
90 void print_pointer_type(FILE *out, const pointer_type_t *type)
91 {
92         print_type(out, type->points_to);
93         fputs("*", out);
94         print_type_qualifiers(out, &type->type);
95 }
96
97 void print_type(FILE *out, const type_t *type)
98 {
99         if(type == NULL) {
100                 fputs("nil type", out);
101                 return;
102         }
103
104         switch(type->type) {
105         case TYPE_INVALID:
106                 fputs("invalid", out);
107                 return;
108         case TYPE_ENUM:
109                 print_type_qualifiers(out, type);
110                 fputs("TODO", out);
111                 return;
112         case TYPE_ATOMIC:
113                 print_atomic_type(out, (const atomic_type_t*) type);
114                 return;
115         case TYPE_COMPOUND_STRUCT:
116         case TYPE_COMPOUND_UNION:
117                 print_type_qualifiers(out, type);
118                 if(((const compound_type_t*) type)->symbol != NULL) {
119                         fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
120                 }
121                 return;
122         case TYPE_METHOD:
123                 print_method_type(out, (const method_type_t*) type);
124                 return;
125         case TYPE_POINTER:
126                 print_pointer_type(out, (const pointer_type_t*) type);
127                 return;
128         }
129         fputs("unknown", out);
130 }
131
132 int type_valid(const type_t *type)
133 {
134         return type->type != TYPE_INVALID;
135 }
136
137 int is_type_int(const type_t *type)
138 {
139         if(type->type != TYPE_ATOMIC)
140                 return 0;
141
142         atomic_type_t *atomic_type = (atomic_type_t*) type;
143         switch(atomic_type->atype) {
144         case ATOMIC_TYPE_CHAR:
145         case ATOMIC_TYPE_SCHAR:
146         case ATOMIC_TYPE_UCHAR:
147         case ATOMIC_TYPE_SHORT:
148         case ATOMIC_TYPE_USHORT:
149         case ATOMIC_TYPE_INT:
150         case ATOMIC_TYPE_UINT:
151         case ATOMIC_TYPE_LONG:
152         case ATOMIC_TYPE_ULONG:
153         case ATOMIC_TYPE_LONGLONG:
154         case ATOMIC_TYPE_ULONGLONG:
155                 return 1;
156         default:
157                 return 0;
158         }
159 }
160
161 static __attribute__((unused))
162 void dbg_type(const type_t *type)
163 {
164         print_type(stdout,type);
165         puts("\n");
166         fflush(stdout);
167 }