Do not consider alignment in the type hash. It is just wrong.
[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_function_type(const function_type_t *type)
96 {
97         unsigned result = hash_ptr(type->return_type);
98
99         function_parameter_t *parameter = type->parameters;
100         while (parameter != NULL) {
101                 result   ^= hash_ptr(parameter->type);
102                 parameter = parameter->next;
103         }
104         result += type->linkage;
105         result += type->calling_convention;
106
107         return result;
108 }
109
110 static unsigned hash_enum_type(const enum_type_t *type)
111 {
112         return hash_ptr(type->enume);
113 }
114
115 static unsigned hash_typeof_type(const typeof_type_t *type)
116 {
117         unsigned result = hash_ptr(type->expression);
118         result         ^= hash_ptr(type->typeof_type);
119
120         return result;
121 }
122
123 static unsigned hash_bitfield_type(const bitfield_type_t *type)
124 {
125         unsigned result  = hash_ptr(type->base_type);
126         result          ^= 27172145;
127
128         return result;
129 }
130
131 static unsigned hash_type(const type_t *type)
132 {
133         unsigned hash = 0;
134
135         switch (type->kind) {
136         case TYPE_INVALID:
137                 panic("internalizing void or invalid types not possible");
138         case TYPE_ERROR:
139                 return 0;
140         case TYPE_ATOMIC:
141                 hash = hash_atomic_type(&type->atomic);
142                 break;
143         case TYPE_COMPLEX:
144                 hash = hash_complex_type(&type->complex);
145                 break;
146         case TYPE_IMAGINARY:
147                 hash = hash_imaginary_type(&type->imaginary);
148                 break;
149         case TYPE_ENUM:
150                 hash = hash_enum_type(&type->enumt);
151                 break;
152         case TYPE_COMPOUND_STRUCT:
153         case TYPE_COMPOUND_UNION:
154                 hash = hash_compound_type(&type->compound);
155                 break;
156         case TYPE_FUNCTION:
157                 hash = hash_function_type(&type->function);
158                 break;
159         case TYPE_POINTER:
160                 hash = hash_pointer_type(&type->pointer);
161                 break;
162         case TYPE_REFERENCE:
163                 hash = hash_reference_type(&type->reference);
164                 break;
165         case TYPE_ARRAY:
166                 hash = hash_array_type(&type->array);
167                 break;
168         case TYPE_BUILTIN:
169                 hash = hash_ptr(type->builtin.symbol);
170                 break;
171         case TYPE_TYPEDEF:
172                 hash = hash_ptr(type->typedeft.typedefe);
173                 break;
174         case TYPE_TYPEOF:
175                 hash = hash_typeof_type(&type->typeoft);
176                 break;
177         case TYPE_BITFIELD:
178                 hash = hash_bitfield_type(&type->bitfield);
179                 break;
180         }
181
182         unsigned some_prime = 99991;
183         hash ^= some_prime * type->base.qualifiers;
184
185         return hash;
186 }
187
188 static bool atomic_types_equal(const atomic_type_t *type1,
189                                                            const atomic_type_t *type2)
190 {
191         return type1->akind == type2->akind;
192 }
193
194 static bool complex_types_equal(const complex_type_t *type1,
195                                                             const complex_type_t *type2)
196 {
197         return type1->akind == type2->akind;
198 }
199
200 static bool imaginary_types_equal(const imaginary_type_t *type1,
201                                                               const imaginary_type_t *type2)
202 {
203         return type1->akind == type2->akind;
204 }
205
206 static bool function_types_equal(const function_type_t *type1,
207                                  const function_type_t *type2)
208 {
209         if (type1->return_type != type2->return_type)
210                 return false;
211         if (type1->variadic != type2->variadic)
212                 return false;
213         if (type1->unspecified_parameters != type2->unspecified_parameters)
214                 return false;
215         if (type1->kr_style_parameters != type2->kr_style_parameters)
216                 return false;
217         if (type1->linkage != type2->linkage)
218                 return false;
219         if (type1->calling_convention != type2->calling_convention)
220                 return false;
221
222         function_parameter_t *param1 = type1->parameters;
223         function_parameter_t *param2 = type2->parameters;
224         while (param1 != NULL && param2 != NULL) {
225                 if (param1->type != param2->type)
226                         return false;
227                 param1 = param1->next;
228                 param2 = param2->next;
229         }
230         if (param1 != NULL || param2 != NULL)
231                 return false;
232
233         return true;
234 }
235
236 static bool pointer_types_equal(const pointer_type_t *type1,
237                                 const pointer_type_t *type2)
238 {
239         return type1->points_to     == type2->points_to &&
240                type1->base_variable == type2->base_variable;
241 }
242
243 static bool reference_types_equal(const reference_type_t *type1,
244                                   const reference_type_t *type2)
245 {
246         return type1->refers_to == type2->refers_to;
247 }
248
249 static bool array_types_equal(const array_type_t *type1,
250                               const array_type_t *type2)
251 {
252         if (type1->element_type != type2->element_type)
253                 return false;
254         if (type1->is_variable != type2->is_variable)
255                 return false;
256         if (type1->is_static != type2->is_static)
257                 return false;
258         if (type1->size_constant != type2->size_constant)
259                 return false;
260
261         /* never identify vla types, because we need them for caching calculated
262          * sizes later in ast2firm */
263         if (type1->is_vla || type2->is_vla)
264                 return false;
265
266         /* TODO: compare size expressions for equality... */
267
268         return false;
269 }
270
271 static bool builtin_types_equal(const builtin_type_t *type1,
272                                 const builtin_type_t *type2)
273 {
274         return type1->symbol == type2->symbol;
275 }
276
277 static bool compound_types_equal(const compound_type_t *type1,
278                                  const compound_type_t *type2)
279 {
280         return type1->compound == type2->compound;
281 }
282
283 static bool enum_types_equal(const enum_type_t *type1,
284                              const enum_type_t *type2)
285 {
286         return type1->enume == type2->enume;
287 }
288
289 static bool typedef_types_equal(const typedef_type_t *type1,
290                                 const typedef_type_t *type2)
291 {
292         return type1->typedefe == type2->typedefe;
293 }
294
295 static bool typeof_types_equal(const typeof_type_t *type1,
296                                const typeof_type_t *type2)
297 {
298         if (type1->expression != type2->expression)
299                 return false;
300         if (type1->typeof_type != type2->typeof_type)
301                 return false;
302
303         return true;
304 }
305
306 static bool bitfield_types_equal(const bitfield_type_t *type1,
307                                  const bitfield_type_t *type2)
308 {
309         if (type1->base_type != type2->base_type)
310                 return false;
311         /* TODO: compare size expression */
312         return false;
313 }
314
315 static bool types_equal(const type_t *type1, const type_t *type2)
316 {
317         if (type1 == type2)
318                 return true;
319         if (type1->kind != type2->kind)
320                 return false;
321         if (type1->base.qualifiers != type2->base.qualifiers)
322                 return false;
323         if (type1->base.modifiers != type2->base.modifiers)
324                 return false;
325
326         switch (type1->kind) {
327         case TYPE_ERROR:
328                 /* Hmm, the error type is never equal */
329                 return false;
330         case TYPE_INVALID:
331                 return false;
332         case TYPE_ATOMIC:
333                 return atomic_types_equal(&type1->atomic, &type2->atomic);
334         case TYPE_COMPLEX:
335                 return complex_types_equal(&type1->complex, &type2->complex);
336         case TYPE_IMAGINARY:
337                 return imaginary_types_equal(&type1->imaginary, &type2->imaginary);
338         case TYPE_ENUM:
339                 return enum_types_equal(&type1->enumt, &type2->enumt);
340         case TYPE_COMPOUND_STRUCT:
341         case TYPE_COMPOUND_UNION:
342                 return compound_types_equal(&type1->compound, &type2->compound);
343         case TYPE_FUNCTION:
344                 return function_types_equal(&type1->function, &type2->function);
345         case TYPE_POINTER:
346                 return pointer_types_equal(&type1->pointer, &type2->pointer);
347         case TYPE_REFERENCE:
348                 return reference_types_equal(&type1->reference, &type2->reference);
349         case TYPE_ARRAY:
350                 return array_types_equal(&type1->array, &type2->array);
351         case TYPE_BUILTIN:
352                 return builtin_types_equal(&type1->builtin, &type2->builtin);
353         case TYPE_TYPEOF:
354                 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
355         case TYPE_TYPEDEF:
356                 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
357         case TYPE_BITFIELD:
358                 return bitfield_types_equal(&type1->bitfield, &type2->bitfield);
359         }
360
361         abort();
362 }
363
364 #define HashSet                    type_hash_t
365 #define HashSetIterator            type_hash_iterator_t
366 #define ValueType                  type_t*
367 #define NullValue                  NULL
368 #define DeletedValue               ((type_t*)-1)
369 #define Hash(this, key)            hash_type(key)
370 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
371 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
372
373 #define hashset_init             _typehash_init
374 #define hashset_init_size        _typehash_init_size
375 #define hashset_destroy          _typehash_destroy
376 #define hashset_insert           _typehash_insert
377 #define hashset_remove           typehash_remove
378 #define hashset_find             typehash_find
379 #define hashset_size             typehash_size
380 #define hashset_iterator_init    typehash_iterator_init
381 #define hashset_iterator_next    typehash_iterator_next
382 #define hashset_remove_iterator  typehash_remove_iterator
383 #define SCALAR_RETURN
384
385 #include "adt/hashset.c"
386
387 static type_hash_t typehash;
388
389 void init_typehash(void)
390 {
391         _typehash_init(&typehash);
392 }
393
394 void exit_typehash(void)
395 {
396         _typehash_destroy(&typehash);
397 }
398
399 type_t *typehash_insert(type_t *type)
400 {
401         return _typehash_insert(&typehash, type);
402 }