change back union stuff and expriment with new union mode for initializers
[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->atype * 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->result_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_type(const type_t *type)
81 {
82         unsigned hash = 0;
83
84         switch(type->type) {
85         case TYPE_INVALID:
86                 panic("internalizing void or invalid types not possible");
87                 return 0;
88         case TYPE_ATOMIC:
89                 hash = hash_atomic_type((const atomic_type_t*) type);
90                 break;
91         case TYPE_ENUM:
92                 hash = hash_enum_type((const enum_type_t*) type);
93                 break;
94         case TYPE_COMPOUND_STRUCT:
95         case TYPE_COMPOUND_UNION:
96                 hash = hash_compound_type((const compound_type_t*) type);
97                 break;
98         case TYPE_FUNCTION:
99                 hash = hash_function_type((const function_type_t*) type);
100                 break;
101         case TYPE_POINTER:
102                 hash = hash_pointer_type((const pointer_type_t*) type);
103                 break;
104         case TYPE_ARRAY:
105                 hash = hash_array_type((const array_type_t*) type);
106                 break;
107         case TYPE_BUILTIN:
108                 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
109                 break;
110         case TYPE_TYPEDEF:
111                 hash = hash_ptr(((const compound_type_t*) type)->declaration);
112                 break;
113         case TYPE_TYPEOF:
114                 hash = hash_typeof_type((const typeof_type_t*) type);
115                 break;
116         }
117
118         unsigned some_prime = 99991;
119         hash ^= some_prime * type->qualifiers;
120
121         return hash;
122 }
123
124 static bool atomic_types_equal(const atomic_type_t *type1,
125                                const atomic_type_t *type2)
126 {
127         return type1->atype == type2->atype;
128 }
129
130 static bool function_types_equal(const function_type_t *type1,
131                                  const function_type_t *type2)
132 {
133         if(type1->result_type != type2->result_type)
134                 return false;
135         if(type1->variadic != type2->variadic)
136                 return false;
137         if(type1->unspecified_parameters != type2->unspecified_parameters)
138                 return false;
139
140         function_parameter_t *param1 = type1->parameters;
141         function_parameter_t *param2 = type2->parameters;
142         while(param1 != NULL && param2 != NULL) {
143                 if(param1->type != param2->type)
144                         return false;
145                 param1 = param1->next;
146                 param2 = param2->next;
147         }
148         if(param1 != NULL || param2 != NULL)
149                 return false;
150
151         return true;
152 }
153
154 static bool pointer_types_equal(const pointer_type_t *type1,
155                                 const pointer_type_t *type2)
156 {
157         return type1->points_to == type2->points_to;
158 }
159
160 static bool array_types_equal(const array_type_t *type1,
161                               const array_type_t *type2)
162 {
163         if(type1->element_type != type2->element_type)
164                 return false;
165         if(type1->is_variable != type2->is_variable)
166                 return false;
167         if(type1->is_static != type2->is_static)
168                 return false;
169         /* TODO: compare expressions for equality... */
170         if(type1->size != type2->size)
171                 return false;
172
173         return true;
174 }
175
176 static bool builtin_types_equal(const builtin_type_t *type1,
177                                 const builtin_type_t *type2)
178 {
179         return type1->symbol == type2->symbol;
180 }
181
182 static bool compound_types_equal(const compound_type_t *type1,
183                                  const compound_type_t *type2)
184 {
185         return type1->declaration == type2->declaration;
186 }
187
188 static bool enum_types_equal(const enum_type_t *type1,
189                              const enum_type_t *type2)
190 {
191         return type1->declaration == type2->declaration;
192 }
193
194 static bool typedef_types_equal(const typedef_type_t *type1,
195                                 const typedef_type_t *type2)
196 {
197         return type1->declaration == type2->declaration;
198 }
199
200 static bool typeof_types_equal(const typeof_type_t *type1,
201                                const typeof_type_t *type2)
202 {
203         if(type1->expression != type2->expression)
204                 return false;
205         if(type1->typeof_type != type2->typeof_type)
206                 return false;
207
208         return true;
209 }
210
211 static bool types_equal(const type_t *type1, const type_t *type2)
212 {
213         if(type1 == type2)
214                 return true;
215         if(type1->type != type2->type)
216                 return false;
217         if(type1->qualifiers != type2->qualifiers)
218                 return false;
219
220         switch(type1->type) {
221         case TYPE_INVALID:
222                 return false;
223         case TYPE_ATOMIC:
224                 return atomic_types_equal((const atomic_type_t*) type1,
225                                           (const atomic_type_t*) type2);
226         case TYPE_ENUM:
227                 return enum_types_equal((const enum_type_t*) type1,
228                                         (const enum_type_t*) type2);
229         case TYPE_COMPOUND_STRUCT:
230         case TYPE_COMPOUND_UNION:
231                 return compound_types_equal((const compound_type_t*) type1,
232                                             (const compound_type_t*) type2);
233         case TYPE_FUNCTION:
234                 return function_types_equal((const function_type_t*) type1,
235                                             (const function_type_t*) type2);
236         case TYPE_POINTER:
237                 return pointer_types_equal((const pointer_type_t*) type1,
238                                            (const pointer_type_t*) type2);
239         case TYPE_ARRAY:
240                 return array_types_equal((const array_type_t*) type1,
241                                          (const array_type_t*) type2);
242         case TYPE_BUILTIN:
243                 return builtin_types_equal((const builtin_type_t*) type1,
244                                            (const builtin_type_t*) type2);
245         case TYPE_TYPEOF:
246                 return typeof_types_equal((const typeof_type_t*) type1,
247                                           (const typeof_type_t*) type2);
248         case TYPE_TYPEDEF:
249                 return typedef_types_equal((const typedef_type_t*) type1,
250                                            (const typedef_type_t*) type2);
251         }
252
253         abort();
254 }
255
256 #define HashSet                    type_hash_t
257 #define HashSetIterator            type_hash_iterator_t
258 #define ValueType                  type_t*
259 #define NullValue                  NULL
260 #define DeletedValue               ((type_t*)-1)
261 #define Hash(this, key)            hash_type(key)
262 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
263 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
264
265 #define hashset_init             _typehash_init
266 #define hashset_init_size        _typehash_init_size
267 #define hashset_destroy          _typehash_destroy
268 #define hashset_insert           _typehash_insert
269 #define hashset_remove           typehash_remove
270 #define hashset_find             typehash_find
271 #define hashset_size             typehash_size
272 #define hashset_iterator_init    typehash_iterator_init
273 #define hashset_iterator_next    typehash_iterator_next
274 #define hashset_remove_iterator  typehash_remove_iterator
275
276 #include "adt/hashset.c"
277
278 static type_hash_t typehash;
279
280 void init_typehash(void)
281 {
282         _typehash_init(&typehash);
283 }
284
285 void exit_typehash(void)
286 {
287         _typehash_destroy(&typehash);
288 }
289
290 type_t *typehash_insert(type_t *type)
291 {
292         return _typehash_insert(&typehash, type);
293 }