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