implement VLAs (just the Free nodes aren't constructed yet)
[cparser] / type_hash.c
1 #include <config.h>
2
3 #include <stdbool.h>
4
5 #include "type_hash.h"
6
7 #include "adt/error.h"
8 #include "type_t.h"
9
10 #include <assert.h>
11
12 #define HashSet         type_hash_t
13 #define HashSetIterator type_hash_iterator_t
14 #define ValueType       type_t*
15 #include "adt/hashset.h"
16 #undef ValueType
17 #undef HashSetIterator
18 #undef HashSet
19
20 /* TODO: ^= is a bad way of combining hashes since most addresses are very
21  * similar */
22
23 static unsigned hash_ptr(const void *ptr)
24 {
25         unsigned ptr_int = ((char*) ptr - (char*) NULL);
26         return ptr_int >> 3;
27 }
28
29 static unsigned hash_atomic_type(const atomic_type_t *type)
30 {
31         unsigned some_prime = 27644437;
32         unsigned result     = type->akind * some_prime;
33
34         return result;
35 }
36
37 static unsigned hash_pointer_type(const pointer_type_t *type)
38 {
39         return hash_ptr(type->points_to);
40 }
41
42 static unsigned hash_array_type(const array_type_t *type)
43 {
44         return hash_ptr(type->element_type);
45 }
46
47 static unsigned hash_compound_type(const compound_type_t *type)
48 {
49         return hash_ptr(type->declaration);
50 }
51
52 static unsigned hash_type(const type_t *type);
53
54 static unsigned hash_function_type(const function_type_t *type)
55 {
56         unsigned result = hash_ptr(type->return_type);
57
58         function_parameter_t *parameter = type->parameters;
59         while(parameter != NULL) {
60                 result   ^= hash_ptr(parameter->type);
61                 parameter = parameter->next;
62         }
63
64         return result;
65 }
66
67 static unsigned hash_enum_type(const enum_type_t *type)
68 {
69         return hash_ptr(type->declaration);
70 }
71
72 static unsigned hash_typeof_type(const typeof_type_t *type)
73 {
74         unsigned result = hash_ptr(type->expression);
75         result         ^= hash_ptr(type->typeof_type);
76
77         return result;
78 }
79
80 static unsigned hash_bitfield_type(const bitfield_type_t *type)
81 {
82         unsigned result  = hash_ptr(type->base);
83         result          ^= 27172145;
84
85         return result;
86 }
87
88 static unsigned hash_type(const type_t *type)
89 {
90         unsigned hash = 0;
91
92         switch(type->kind) {
93         case TYPE_INVALID:
94                 panic("internalizing void or invalid types not possible");
95                 return 0;
96         case TYPE_ERROR:
97                 return 0;
98         case TYPE_ATOMIC:
99                 hash = hash_atomic_type(&type->atomic);
100                 break;
101         case TYPE_ENUM:
102                 hash = hash_enum_type(&type->enumt);
103                 break;
104         case TYPE_COMPOUND_STRUCT:
105         case TYPE_COMPOUND_UNION:
106                 hash = hash_compound_type(&type->compound);
107                 break;
108         case TYPE_FUNCTION:
109                 hash = hash_function_type(&type->function);
110                 break;
111         case TYPE_POINTER:
112                 hash = hash_pointer_type(&type->pointer);
113                 break;
114         case TYPE_ARRAY:
115                 hash = hash_array_type(&type->array);
116                 break;
117         case TYPE_BUILTIN:
118                 hash = hash_ptr(type->builtin.symbol);
119                 break;
120         case TYPE_TYPEDEF:
121                 hash = hash_ptr(type->typedeft.declaration);
122                 break;
123         case TYPE_TYPEOF:
124                 hash = hash_typeof_type(&type->typeoft);
125                 break;
126         case TYPE_BITFIELD:
127                 hash = hash_bitfield_type(&type->bitfield);
128                 break;
129         }
130
131         unsigned some_prime = 99991;
132         hash ^= some_prime * type->base.qualifiers;
133
134         return hash;
135 }
136
137 static bool atomic_types_equal(const atomic_type_t *type1,
138                                const atomic_type_t *type2)
139 {
140         return type1->akind == type2->akind;
141 }
142
143 static bool function_types_equal(const function_type_t *type1,
144                                  const function_type_t *type2)
145 {
146         if(type1->return_type != type2->return_type)
147                 return false;
148         if(type1->variadic != type2->variadic)
149                 return false;
150         if(type1->unspecified_parameters != type2->unspecified_parameters)
151                 return false;
152         if(type1->kr_style_parameters != type2->kr_style_parameters)
153                 return false;
154
155         function_parameter_t *param1 = type1->parameters;
156         function_parameter_t *param2 = type2->parameters;
157         while(param1 != NULL && param2 != NULL) {
158                 if(param1->type != param2->type)
159                         return false;
160                 param1 = param1->next;
161                 param2 = param2->next;
162         }
163         if(param1 != NULL || param2 != NULL)
164                 return false;
165
166         return true;
167 }
168
169 static bool pointer_types_equal(const pointer_type_t *type1,
170                                 const pointer_type_t *type2)
171 {
172         return type1->points_to == type2->points_to;
173 }
174
175 static bool array_types_equal(const array_type_t *type1,
176                               const array_type_t *type2)
177 {
178         if(type1->element_type != type2->element_type)
179                 return false;
180         if(type1->is_variable != type2->is_variable)
181                 return false;
182         if(type1->is_static != type2->is_static)
183                 return false;
184         if(type1->size_constant != type2->size_constant)
185                 return false;
186
187         /* never identify vla types, because we need them for caching calculated
188          * sizes later in ast2firm */
189         if(type1->is_vla || type2->is_vla)
190                 return false;
191
192         /* TODO: compare size expressions for equality... */
193
194         return false;
195 }
196
197 static bool builtin_types_equal(const builtin_type_t *type1,
198                                 const builtin_type_t *type2)
199 {
200         return type1->symbol == type2->symbol;
201 }
202
203 static bool compound_types_equal(const compound_type_t *type1,
204                                  const compound_type_t *type2)
205 {
206         return type1->declaration == type2->declaration;
207 }
208
209 static bool enum_types_equal(const enum_type_t *type1,
210                              const enum_type_t *type2)
211 {
212         return type1->declaration == type2->declaration;
213 }
214
215 static bool typedef_types_equal(const typedef_type_t *type1,
216                                 const typedef_type_t *type2)
217 {
218         return type1->declaration == type2->declaration;
219 }
220
221 static bool typeof_types_equal(const typeof_type_t *type1,
222                                const typeof_type_t *type2)
223 {
224         if(type1->expression != type2->expression)
225                 return false;
226         if(type1->typeof_type != type2->typeof_type)
227                 return false;
228
229         return true;
230 }
231
232 static bool bitfield_types_equal(const bitfield_type_t *type1,
233                                  const bitfield_type_t *type2)
234 {
235         if(type1->base != type2->base)
236                 return false;
237         /* TODO: compare size expression */
238         return false;
239 }
240
241 static bool types_equal(const type_t *type1, const type_t *type2)
242 {
243         if(type1 == type2)
244                 return true;
245         if(type1->kind != type2->kind)
246                 return false;
247         if(type1->base.qualifiers != type2->base.qualifiers)
248                 return false;
249
250         switch(type1->kind) {
251         case TYPE_ERROR:
252                 /* Hmm, the error type is never equal */
253                 return false;
254         case TYPE_INVALID:
255                 return false;
256         case TYPE_ATOMIC:
257                 return atomic_types_equal(&type1->atomic, &type2->atomic);
258         case TYPE_ENUM:
259                 return enum_types_equal(&type1->enumt, &type2->enumt);
260         case TYPE_COMPOUND_STRUCT:
261         case TYPE_COMPOUND_UNION:
262                 return compound_types_equal(&type1->compound, &type2->compound);
263         case TYPE_FUNCTION:
264                 return function_types_equal(&type1->function, &type2->function);
265         case TYPE_POINTER:
266                 return pointer_types_equal(&type1->pointer, &type2->pointer);
267         case TYPE_ARRAY:
268                 return array_types_equal(&type1->array, &type2->array);
269         case TYPE_BUILTIN:
270                 return builtin_types_equal(&type1->builtin, &type2->builtin);
271         case TYPE_TYPEOF:
272                 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
273         case TYPE_TYPEDEF:
274                 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
275         case TYPE_BITFIELD:
276                 return bitfield_types_equal(&type1->bitfield, &type2->bitfield);
277         }
278
279         abort();
280 }
281
282 #define HashSet                    type_hash_t
283 #define HashSetIterator            type_hash_iterator_t
284 #define ValueType                  type_t*
285 #define NullValue                  NULL
286 #define DeletedValue               ((type_t*)-1)
287 #define Hash(this, key)            hash_type(key)
288 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
289 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
290
291 #define hashset_init             _typehash_init
292 #define hashset_init_size        _typehash_init_size
293 #define hashset_destroy          _typehash_destroy
294 #define hashset_insert           _typehash_insert
295 #define hashset_remove           typehash_remove
296 #define hashset_find             typehash_find
297 #define hashset_size             typehash_size
298 #define hashset_iterator_init    typehash_iterator_init
299 #define hashset_iterator_next    typehash_iterator_next
300 #define hashset_remove_iterator  typehash_remove_iterator
301
302 #include "adt/hashset.c"
303
304 static type_hash_t typehash;
305
306 void init_typehash(void)
307 {
308         _typehash_init(&typehash);
309 }
310
311 void exit_typehash(void)
312 {
313         _typehash_destroy(&typehash);
314 }
315
316 type_t *typehash_insert(type_t *type)
317 {
318         return _typehash_insert(&typehash, type);
319 }