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