- countless bugfixes
[cparser] / type_hash.c
1 #include <config.h>
2
3 #include "type_hash.h"
4
5 #include "adt/error.h"
6 #include "type_t.h"
7
8 #include <assert.h>
9
10 #define HashSet         type_hash_t
11 #define HashSetIterator type_hash_iterator_t
12 #define ValueType       type_t*
13 #include "adt/hashset.h"
14 #undef ValueType
15 #undef HashSetIterator
16 #undef HashSet
17
18 /* TODO: ^= is a bad way of combining hashes since most addresses are very
19  * similar */
20
21 static unsigned hash_ptr(const void *ptr)
22 {
23         unsigned ptr_int = ((char*) ptr - (char*) NULL);
24         return ptr_int >> 3;
25 }
26
27 static unsigned hash_atomic_type(const atomic_type_t *type)
28 {
29         unsigned some_prime = 27644437;
30
31         return type->atype * some_prime;
32 }
33
34 static unsigned hash_pointer_type(const pointer_type_t *type)
35 {
36         return hash_ptr(type->points_to);
37 }
38
39 static unsigned hash_compound_type(const compound_type_t *type)
40 {
41         unsigned result = hash_ptr(type->symbol);
42
43         return result;
44 }
45
46 static unsigned hash_type(const type_t *type);
47
48 static unsigned hash_method_type(const method_type_t *type)
49 {
50         unsigned result = hash_ptr(type->result_type);
51
52         method_parameter_t *parameter = type->parameters;
53         while(parameter != NULL) {
54                 result   ^= hash_ptr(parameter->type);
55                 parameter = parameter->next;
56         }
57
58         return result;
59 }
60
61 static unsigned hash_enum_type(const enum_type_t *type)
62 {
63         unsigned result = hash_ptr(type->symbol);
64
65         return result;
66 }
67
68 static unsigned hash_type(const type_t *type)
69 {
70         unsigned hash;
71
72         switch(type->type) {
73         case TYPE_INVALID:
74                 panic("internalizing void or invalid types not possible");
75                 return 0;
76         case TYPE_ATOMIC:
77                 hash = hash_atomic_type((const atomic_type_t*) type);
78                 break;
79         case TYPE_ENUM:
80                 hash = hash_enum_type((const enum_type_t*) type);
81                 break;
82         case TYPE_COMPOUND_STRUCT:
83         case TYPE_COMPOUND_UNION:
84                 hash = hash_compound_type((const compound_type_t*) type);
85                 break;
86         case TYPE_METHOD:
87                 hash = hash_method_type((const method_type_t*) type);
88                 break;
89         case TYPE_POINTER:
90                 hash = hash_pointer_type((const pointer_type_t*) type);
91                 break;
92         case TYPE_BUILTIN:
93                 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
94                 break;
95         }
96
97         unsigned some_prime = 99991;
98         hash ^= some_prime * type->qualifiers;
99
100         return hash;
101 }
102
103 static int atomic_types_equal(const atomic_type_t *type1,
104                               const atomic_type_t *type2)
105 {
106         return type1->atype == type2->atype;
107 }
108
109 static int compound_types_equal(const compound_type_t *type1,
110                                 const compound_type_t *type2)
111 {
112         if(type1->type.type != type2->type.type)
113                 return 0;
114         if(type1->symbol != type2->symbol)
115                 return 0;
116
117         /* anonymous types? */
118         if(type1->symbol == NULL) {
119                 /* previous tests should already have checked for this */
120                 assert(type1 != type2);
121                 /* anonymous types are only equal if they are the very same type */
122                 return 0;
123         }
124
125         /* non-anonymous types with same symbol are equal */
126         return 1;
127 }
128
129 static int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
130 {
131         if(type1->symbol != type2->symbol)
132                 return 0;
133
134         /* anonymous types? */
135         if(type1->symbol == NULL) {
136                 /* previous tests should already have checked for this */
137                 assert(type1 != type2);
138                 /* 2 anonymous enums are never equal */
139                 return 0;
140         }
141
142         /* non-anonymous types with same symbol are equal */
143         return 1;
144 }
145
146 static int method_types_equal(const method_type_t *type1,
147                               const method_type_t *type2)
148 {
149         if(type1->result_type != type2->result_type)
150                 return 0;
151         if(type1->variadic != type2->variadic)
152                 return 0;
153         if(type1->unspecified_parameters != type2->unspecified_parameters)
154                 return 0;
155
156         method_parameter_t *param1 = type1->parameters;
157         method_parameter_t *param2 = type2->parameters;
158         while(param1 != NULL && param2 != NULL) {
159                 if(param1->type != param2->type)
160                         return 0;
161                 param1 = param1->next;
162                 param2 = param2->next;
163         }
164         if(param1 != NULL || param2 != NULL)
165                 return 0;
166
167         return 1;
168 }
169
170 static int pointer_types_equal(const pointer_type_t *type1,
171                                const pointer_type_t *type2)
172 {
173         return type1->points_to == type2->points_to;
174 }
175
176 static int 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 int types_equal(const type_t *type1, const type_t *type2)
183 {
184         if(type1 == type2)
185                 return 1;
186         if(type1->type != type2->type)
187                 return 0;
188         if(type1->qualifiers != type2->qualifiers)
189                 return 0;
190
191         switch(type1->type) {
192         case TYPE_INVALID:
193                 return 0;
194         case TYPE_ATOMIC:
195                 return atomic_types_equal((const atomic_type_t*) type1,
196                                           (const atomic_type_t*) type2);
197         case TYPE_COMPOUND_STRUCT:
198         case TYPE_COMPOUND_UNION:
199                 return compound_types_equal((const compound_type_t*) type1,
200                                             (const compound_type_t*) type2);
201         case TYPE_ENUM:
202                 return enum_types_equal((const enum_type_t*) type1,
203                                         (const enum_type_t*) type2);
204         case TYPE_METHOD:
205                 return method_types_equal((const method_type_t*) type1,
206                                           (const method_type_t*) type2);
207         case TYPE_POINTER:
208                 return pointer_types_equal((const pointer_type_t*) type1,
209                                            (const pointer_type_t*) type2);
210         case TYPE_BUILTIN:
211                 return builtin_types_equal((const builtin_type_t*) type1,
212                                            (const builtin_type_t*) type2);
213         }
214
215         abort();
216 }
217
218 #define HashSet                    type_hash_t
219 #define HashSetIterator            type_hash_iterator_t
220 #define ValueType                  type_t*
221 #define NullValue                  NULL
222 #define DeletedValue               ((type_t*)-1)
223 #define Hash(this, key)            hash_type(key)
224 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
225 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
226
227 #define hashset_init             _typehash_init
228 #define hashset_init_size        _typehash_init_size
229 #define hashset_destroy          _typehash_destroy
230 #define hashset_insert           _typehash_insert
231 #define hashset_remove           typehash_remove
232 #define hashset_find             typehash_find
233 #define hashset_size             typehash_size
234 #define hashset_iterator_init    typehash_iterator_init
235 #define hashset_iterator_next    typehash_iterator_next
236 #define hashset_remove_iterator  typehash_remove_iterator
237
238 #include "adt/hashset.c"
239
240 static type_hash_t typehash;
241
242 void init_typehash(void)
243 {
244         _typehash_init(&typehash);
245 }
246
247 void exit_typehash(void)
248 {
249         _typehash_destroy(&typehash);
250 }
251
252 type_t *typehash_insert(type_t *type)
253 {
254         return _typehash_insert(&typehash, type);
255 }