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