- updated hash_pointer_type for based pointers
[cparser] / type_hash.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include <stdbool.h>
23
24 #include "type_hash.h"
25
26 #include "adt/error.h"
27 #include "type_t.h"
28
29 #include <assert.h>
30
31 #define HashSet         type_hash_t
32 #define HashSetIterator type_hash_iterator_t
33 #define ValueType       type_t*
34 #include "adt/hashset.h"
35 #undef ValueType
36 #undef HashSetIterator
37 #undef HashSet
38
39 typedef struct type_hash_iterator_t  type_hash_iterator_t;
40 typedef struct type_hash_t           type_hash_t;
41
42 /* TODO: ^= is a bad way of combining hashes since most addresses are very
43  * similar */
44
45 static unsigned hash_ptr(const void *ptr)
46 {
47         unsigned ptr_int = ((char*) ptr - (char*) NULL);
48         return ptr_int >> 3;
49 }
50
51 static unsigned hash_atomic_type(const atomic_type_t *type)
52 {
53         unsigned some_prime = 27644437;
54         unsigned result     = type->akind * some_prime;
55
56         return result;
57 }
58
59 static unsigned hash_complex_type(const complex_type_t *type)
60 {
61         unsigned some_prime = 27644437;
62         unsigned result     = type->akind * some_prime;
63
64         return result;
65 }
66
67 static unsigned hash_imaginary_type(const imaginary_type_t *type)
68 {
69         unsigned some_prime = 27644437;
70         unsigned result     = type->akind * some_prime;
71
72         return result;
73 }
74
75 static unsigned hash_pointer_type(const pointer_type_t *type)
76 {
77         return hash_ptr(type->points_to) ^ hash_ptr(type->base_variable);
78 }
79
80 static unsigned hash_reference_type(const reference_type_t *type)
81 {
82         return hash_ptr(type->refers_to);
83 }
84
85 static unsigned hash_array_type(const array_type_t *type)
86 {
87         return hash_ptr(type->element_type);
88 }
89
90 static unsigned hash_compound_type(const compound_type_t *type)
91 {
92         return hash_ptr(type->compound);
93 }
94
95 static unsigned hash_type(const type_t *type);
96
97 static unsigned hash_function_type(const function_type_t *type)
98 {
99         unsigned result = hash_ptr(type->return_type);
100
101         function_parameter_t *parameter = type->parameters;
102         while (parameter != NULL) {
103                 result   ^= hash_ptr(parameter->type);
104                 parameter = parameter->next;
105         }
106         result += type->linkage;
107         result += type->calling_convention;
108
109         return result;
110 }
111
112 static unsigned hash_enum_type(const enum_type_t *type)
113 {
114         return hash_ptr(type->enume);
115 }
116
117 static unsigned hash_typeof_type(const typeof_type_t *type)
118 {
119         unsigned result = hash_ptr(type->expression);
120         result         ^= hash_ptr(type->typeof_type);
121
122         return result;
123 }
124
125 static unsigned hash_bitfield_type(const bitfield_type_t *type)
126 {
127         unsigned result  = hash_ptr(type->base_type);
128         result          ^= 27172145;
129
130         return result;
131 }
132
133 static unsigned hash_type(const type_t *type)
134 {
135         unsigned hash = 0;
136
137         switch (type->kind) {
138         case TYPE_INVALID:
139                 panic("internalizing void or invalid types not possible");
140         case TYPE_ERROR:
141                 return 0;
142         case TYPE_ATOMIC:
143                 hash = hash_atomic_type(&type->atomic);
144                 break;
145         case TYPE_COMPLEX:
146                 hash = hash_complex_type(&type->complex);
147                 break;
148         case TYPE_IMAGINARY:
149                 hash = hash_imaginary_type(&type->imaginary);
150                 break;
151         case TYPE_ENUM:
152                 hash = hash_enum_type(&type->enumt);
153                 break;
154         case TYPE_COMPOUND_STRUCT:
155         case TYPE_COMPOUND_UNION:
156                 hash = hash_compound_type(&type->compound);
157                 break;
158         case TYPE_FUNCTION:
159                 hash = hash_function_type(&type->function);
160                 break;
161         case TYPE_POINTER:
162                 hash = hash_pointer_type(&type->pointer);
163                 break;
164         case TYPE_REFERENCE:
165                 hash = hash_reference_type(&type->reference);
166                 break;
167         case TYPE_ARRAY:
168                 hash = hash_array_type(&type->array);
169                 break;
170         case TYPE_BUILTIN:
171                 hash = hash_ptr(type->builtin.symbol);
172                 break;
173         case TYPE_TYPEDEF:
174                 hash = hash_ptr(type->typedeft.typedefe);
175                 break;
176         case TYPE_TYPEOF:
177                 hash = hash_typeof_type(&type->typeoft);
178                 break;
179         case TYPE_BITFIELD:
180                 hash = hash_bitfield_type(&type->bitfield);
181                 break;
182         }
183
184         unsigned some_prime = 99991;
185         hash ^= some_prime * type->base.qualifiers;
186
187         return hash;
188 }
189
190 static bool atomic_types_equal(const atomic_type_t *type1,
191                                                            const atomic_type_t *type2)
192 {
193         return type1->akind == type2->akind;
194 }
195
196 static bool complex_types_equal(const complex_type_t *type1,
197                                                             const complex_type_t *type2)
198 {
199         return type1->akind == type2->akind;
200 }
201
202 static bool imaginary_types_equal(const imaginary_type_t *type1,
203                                                               const imaginary_type_t *type2)
204 {
205         return type1->akind == type2->akind;
206 }
207
208 static bool function_types_equal(const function_type_t *type1,
209                                  const function_type_t *type2)
210 {
211         if (type1->return_type != type2->return_type)
212                 return false;
213         if (type1->variadic != type2->variadic)
214                 return false;
215         if (type1->unspecified_parameters != type2->unspecified_parameters)
216                 return false;
217         if (type1->kr_style_parameters != type2->kr_style_parameters)
218                 return false;
219         if (type1->linkage != type2->linkage)
220                 return false;
221         if (type1->calling_convention != type2->calling_convention)
222                 return false;
223
224         function_parameter_t *param1 = type1->parameters;
225         function_parameter_t *param2 = type2->parameters;
226         while (param1 != NULL && param2 != NULL) {
227                 if (param1->type != param2->type)
228                         return false;
229                 param1 = param1->next;
230                 param2 = param2->next;
231         }
232         if (param1 != NULL || param2 != NULL)
233                 return false;
234
235         return true;
236 }
237
238 static bool pointer_types_equal(const pointer_type_t *type1,
239                                 const pointer_type_t *type2)
240 {
241         return type1->points_to     == type2->points_to &&
242                type1->base_variable == type2->base_variable;
243 }
244
245 static bool reference_types_equal(const reference_type_t *type1,
246                                   const reference_type_t *type2)
247 {
248         return type1->refers_to == type2->refers_to;
249 }
250
251 static bool array_types_equal(const array_type_t *type1,
252                               const array_type_t *type2)
253 {
254         if (type1->element_type != type2->element_type)
255                 return false;
256         if (type1->is_variable != type2->is_variable)
257                 return false;
258         if (type1->is_static != type2->is_static)
259                 return false;
260         if (type1->size_constant != type2->size_constant)
261                 return false;
262
263         /* never identify vla types, because we need them for caching calculated
264          * sizes later in ast2firm */
265         if (type1->is_vla || type2->is_vla)
266                 return false;
267
268         /* TODO: compare size expressions for equality... */
269
270         return false;
271 }
272
273 static bool builtin_types_equal(const builtin_type_t *type1,
274                                 const builtin_type_t *type2)
275 {
276         return type1->symbol == type2->symbol;
277 }
278
279 static bool compound_types_equal(const compound_type_t *type1,
280                                  const compound_type_t *type2)
281 {
282         return type1->compound == type2->compound;
283 }
284
285 static bool enum_types_equal(const enum_type_t *type1,
286                              const enum_type_t *type2)
287 {
288         return type1->enume == type2->enume;
289 }
290
291 static bool typedef_types_equal(const typedef_type_t *type1,
292                                 const typedef_type_t *type2)
293 {
294         return type1->typedefe == type2->typedefe;
295 }
296
297 static bool typeof_types_equal(const typeof_type_t *type1,
298                                const typeof_type_t *type2)
299 {
300         if (type1->expression != type2->expression)
301                 return false;
302         if (type1->typeof_type != type2->typeof_type)
303                 return false;
304
305         return true;
306 }
307
308 static bool bitfield_types_equal(const bitfield_type_t *type1,
309                                  const bitfield_type_t *type2)
310 {
311         if (type1->base_type != type2->base_type)
312                 return false;
313         /* TODO: compare size expression */
314         return false;
315 }
316
317 static bool types_equal(const type_t *type1, const type_t *type2)
318 {
319         if (type1 == type2)
320                 return true;
321         if (type1->kind != type2->kind)
322                 return false;
323         if (type1->base.qualifiers != type2->base.qualifiers)
324                 return false;
325         if (type1->base.modifiers != type2->base.modifiers)
326                 return false;
327
328         switch (type1->kind) {
329         case TYPE_ERROR:
330                 /* Hmm, the error type is never equal */
331                 return false;
332         case TYPE_INVALID:
333                 return false;
334         case TYPE_ATOMIC:
335                 return atomic_types_equal(&type1->atomic, &type2->atomic);
336         case TYPE_COMPLEX:
337                 return complex_types_equal(&type1->complex, &type2->complex);
338         case TYPE_IMAGINARY:
339                 return imaginary_types_equal(&type1->imaginary, &type2->imaginary);
340         case TYPE_ENUM:
341                 return enum_types_equal(&type1->enumt, &type2->enumt);
342         case TYPE_COMPOUND_STRUCT:
343         case TYPE_COMPOUND_UNION:
344                 return compound_types_equal(&type1->compound, &type2->compound);
345         case TYPE_FUNCTION:
346                 return function_types_equal(&type1->function, &type2->function);
347         case TYPE_POINTER:
348                 return pointer_types_equal(&type1->pointer, &type2->pointer);
349         case TYPE_REFERENCE:
350                 return reference_types_equal(&type1->reference, &type2->reference);
351         case TYPE_ARRAY:
352                 return array_types_equal(&type1->array, &type2->array);
353         case TYPE_BUILTIN:
354                 return builtin_types_equal(&type1->builtin, &type2->builtin);
355         case TYPE_TYPEOF:
356                 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
357         case TYPE_TYPEDEF:
358                 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
359         case TYPE_BITFIELD:
360                 return bitfield_types_equal(&type1->bitfield, &type2->bitfield);
361         }
362
363         abort();
364 }
365
366 #define HashSet                    type_hash_t
367 #define HashSetIterator            type_hash_iterator_t
368 #define ValueType                  type_t*
369 #define NullValue                  NULL
370 #define DeletedValue               ((type_t*)-1)
371 #define Hash(this, key)            hash_type(key)
372 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
373 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
374
375 #define hashset_init             _typehash_init
376 #define hashset_init_size        _typehash_init_size
377 #define hashset_destroy          _typehash_destroy
378 #define hashset_insert           _typehash_insert
379 #define hashset_remove           typehash_remove
380 #define hashset_find             typehash_find
381 #define hashset_size             typehash_size
382 #define hashset_iterator_init    typehash_iterator_init
383 #define hashset_iterator_next    typehash_iterator_next
384 #define hashset_remove_iterator  typehash_remove_iterator
385 #define SCALAR_RETURN
386
387 #include "adt/hashset.c"
388
389 static type_hash_t typehash;
390
391 void init_typehash(void)
392 {
393         _typehash_init(&typehash);
394 }
395
396 void exit_typehash(void)
397 {
398         _typehash_destroy(&typehash);
399 }
400
401 type_t *typehash_insert(type_t *type)
402 {
403         return _typehash_insert(&typehash, type);
404 }