- implemented decimal floating pointer numbers in lexer
[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         unsigned result = hash_ptr(type->symbol);
50
51         return result;
52 }
53
54 static unsigned hash_type(const type_t *type);
55
56 static unsigned hash_method_type(const method_type_t *type)
57 {
58         unsigned result = hash_ptr(type->result_type);
59
60         method_parameter_t *parameter = type->parameters;
61         while(parameter != NULL) {
62                 result   ^= hash_ptr(parameter->type);
63                 parameter = parameter->next;
64         }
65
66         return result;
67 }
68
69 static unsigned hash_enum_type(const enum_type_t *type)
70 {
71         unsigned result = hash_ptr(type->symbol);
72
73         return result;
74 }
75
76 static unsigned hash_type(const type_t *type)
77 {
78         unsigned hash;
79
80         switch(type->type) {
81         case TYPE_INVALID:
82                 panic("internalizing void or invalid types not possible");
83                 return 0;
84         case TYPE_ATOMIC:
85                 hash = hash_atomic_type((const atomic_type_t*) type);
86                 break;
87         case TYPE_ENUM:
88                 hash = hash_enum_type((const enum_type_t*) type);
89                 break;
90         case TYPE_COMPOUND_STRUCT:
91         case TYPE_COMPOUND_UNION:
92                 hash = hash_compound_type((const compound_type_t*) type);
93                 break;
94         case TYPE_METHOD:
95                 hash = hash_method_type((const method_type_t*) type);
96                 break;
97         case TYPE_POINTER:
98                 hash = hash_pointer_type((const pointer_type_t*) type);
99                 break;
100         case TYPE_ARRAY:
101                 hash = hash_array_type((const array_type_t*) type);
102                 break;
103         case TYPE_BUILTIN:
104                 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
105                 break;
106         }
107
108         unsigned some_prime = 99991;
109         hash ^= some_prime * type->qualifiers;
110
111         return hash;
112 }
113
114 static bool atomic_types_equal(const atomic_type_t *type1,
115                                const atomic_type_t *type2)
116 {
117         return type1->atype == type2->atype;
118 }
119
120 static bool compound_types_equal(const compound_type_t *type1,
121                                  const compound_type_t *type2)
122 {
123         if(type1->type.type != type2->type.type)
124                 return false;
125         if(type1->symbol != type2->symbol)
126                 return false;
127
128         /* anonymous types? */
129         if(type1->symbol == NULL) {
130                 /* previous tests should already have checked for this */
131                 assert(type1 != type2);
132                 /* anonymous types are only equal if they are the very same type */
133                 return false;
134         }
135
136         /* non-anonymous types with same symbol are equal */
137         return true;
138 }
139
140 static bool enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
141 {
142         if(type1->symbol != type2->symbol)
143                 return false;
144
145         /* anonymous types? */
146         if(type1->symbol == NULL) {
147                 /* previous tests should already have checked for this */
148                 assert(type1 != type2);
149                 /* 2 anonymous enums are never equal */
150                 return false;
151         }
152
153         /* non-anonymous types with same symbol are equal */
154         return true;
155 }
156
157 static bool method_types_equal(const method_type_t *type1,
158                                const method_type_t *type2)
159 {
160         if(type1->result_type != type2->result_type)
161                 return false;
162         if(type1->variadic != type2->variadic)
163                 return false;
164         if(type1->unspecified_parameters != type2->unspecified_parameters)
165                 return false;
166
167         method_parameter_t *param1 = type1->parameters;
168         method_parameter_t *param2 = type2->parameters;
169         while(param1 != NULL && param2 != NULL) {
170                 if(param1->type != param2->type)
171                         return false;
172                 param1 = param1->next;
173                 param2 = param2->next;
174         }
175         if(param1 != NULL || param2 != NULL)
176                 return false;
177
178         return true;
179 }
180
181 static bool pointer_types_equal(const pointer_type_t *type1,
182                                 const pointer_type_t *type2)
183 {
184         return type1->points_to == type2->points_to;
185 }
186
187 static bool array_types_equal(const array_type_t *type1,
188                               const array_type_t *type2)
189 {
190         if(type1->element_type != type2->element_type)
191                 return false;
192         if(type1->is_variable != type2->is_variable)
193                 return false;
194         if(type1->is_static != type2->is_static)
195                 return false;
196         /* TODO: compare expressions for equality... */
197         if(type1->size != type2->size)
198                 return false;
199
200         return true;
201 }
202
203 static bool builtin_types_equal(const builtin_type_t *type1,
204                                 const builtin_type_t *type2)
205 {
206         return type1->symbol == type2->symbol;
207 }
208
209 static bool types_equal(const type_t *type1, const type_t *type2)
210 {
211         if(type1 == type2)
212                 return true;
213         if(type1->type != type2->type)
214                 return false;
215         if(type1->qualifiers != type2->qualifiers)
216                 return false;
217
218         switch(type1->type) {
219         case TYPE_INVALID:
220                 return false;
221         case TYPE_ATOMIC:
222                 return atomic_types_equal((const atomic_type_t*) type1,
223                                           (const atomic_type_t*) type2);
224         case TYPE_COMPOUND_STRUCT:
225         case TYPE_COMPOUND_UNION:
226                 return compound_types_equal((const compound_type_t*) type1,
227                                             (const compound_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_METHOD:
232                 return method_types_equal((const method_type_t*) type1,
233                                           (const method_type_t*) type2);
234         case TYPE_POINTER:
235                 return pointer_types_equal((const pointer_type_t*) type1,
236                                            (const pointer_type_t*) type2);
237         case TYPE_ARRAY:
238                 return array_types_equal((const array_type_t*) type1,
239                                          (const array_type_t*) type2);
240         case TYPE_BUILTIN:
241                 return builtin_types_equal((const builtin_type_t*) type1,
242                                            (const builtin_type_t*) type2);
243         }
244
245         abort();
246 }
247
248 #define HashSet                    type_hash_t
249 #define HashSetIterator            type_hash_iterator_t
250 #define ValueType                  type_t*
251 #define NullValue                  NULL
252 #define DeletedValue               ((type_t*)-1)
253 #define Hash(this, key)            hash_type(key)
254 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
255 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
256
257 #define hashset_init             _typehash_init
258 #define hashset_init_size        _typehash_init_size
259 #define hashset_destroy          _typehash_destroy
260 #define hashset_insert           _typehash_insert
261 #define hashset_remove           typehash_remove
262 #define hashset_find             typehash_find
263 #define hashset_size             typehash_size
264 #define hashset_iterator_init    typehash_iterator_init
265 #define hashset_iterator_next    typehash_iterator_next
266 #define hashset_remove_iterator  typehash_remove_iterator
267
268 #include "adt/hashset.c"
269
270 static type_hash_t typehash;
271
272 void init_typehash(void)
273 {
274         _typehash_init(&typehash);
275 }
276
277 void exit_typehash(void)
278 {
279         _typehash_destroy(&typehash);
280 }
281
282 type_t *typehash_insert(type_t *type)
283 {
284         return _typehash_insert(&typehash, type);
285 }