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