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