main: rework preprocessor invocation
[cparser] / type_hash.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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 ValueType       type_t*
33 #include "adt/hashset.h"
34 #undef ValueType
35 #undef HashSet
36
37 typedef struct type_hash_iterator_t  type_hash_iterator_t;
38 typedef struct type_hash_t           type_hash_t;
39
40 /* TODO: ^= is a bad way of combining hashes since most addresses are very
41  * similar */
42
43 static unsigned hash_ptr(const void *ptr)
44 {
45         unsigned ptr_int = ((char*) ptr - (char*) NULL);
46         return ptr_int >> 3;
47 }
48
49 static unsigned hash_atomic_type(const atomic_type_t *type)
50 {
51         unsigned some_prime = 27644437;
52         unsigned result     = type->akind * some_prime;
53
54         return result;
55 }
56
57 static unsigned hash_pointer_type(const pointer_type_t *type)
58 {
59         return hash_ptr(type->points_to) ^ hash_ptr(type->base_variable);
60 }
61
62 static unsigned hash_reference_type(const reference_type_t *type)
63 {
64         return hash_ptr(type->refers_to);
65 }
66
67 static unsigned hash_array_type(const array_type_t *type)
68 {
69         return hash_ptr(type->element_type);
70 }
71
72 static unsigned hash_compound_type(const compound_type_t *type)
73 {
74         return hash_ptr(type->compound);
75 }
76
77 static unsigned hash_function_type(const function_type_t *type)
78 {
79         unsigned result = hash_ptr(type->return_type);
80
81         function_parameter_t *parameter = type->parameters;
82         while (parameter != NULL) {
83                 result   ^= hash_ptr(parameter->type);
84                 parameter = parameter->next;
85         }
86         result += type->modifiers;
87         result += type->linkage;
88         result += type->calling_convention;
89
90         return result;
91 }
92
93 static unsigned hash_enum_type(const enum_type_t *type)
94 {
95         return hash_ptr(type->enume);
96 }
97
98 static unsigned hash_typeof_type(const typeof_type_t *type)
99 {
100         unsigned result = hash_ptr(type->expression);
101         result         ^= hash_ptr(type->typeof_type);
102
103         return result;
104 }
105
106 static unsigned hash_type(const type_t *type)
107 {
108         unsigned hash = 0;
109
110         switch (type->kind) {
111         case TYPE_ERROR:
112                 return 0;
113         case TYPE_IMAGINARY:
114         case TYPE_COMPLEX:
115         case TYPE_ATOMIC:
116                 hash = hash_atomic_type(&type->atomic);
117                 break;
118         case TYPE_ENUM:
119                 hash = hash_enum_type(&type->enumt);
120                 break;
121         case TYPE_COMPOUND_STRUCT:
122         case TYPE_COMPOUND_UNION:
123                 hash = hash_compound_type(&type->compound);
124                 break;
125         case TYPE_FUNCTION:
126                 hash = hash_function_type(&type->function);
127                 break;
128         case TYPE_POINTER:
129                 hash = hash_pointer_type(&type->pointer);
130                 break;
131         case TYPE_REFERENCE:
132                 hash = hash_reference_type(&type->reference);
133                 break;
134         case TYPE_ARRAY:
135                 hash = hash_array_type(&type->array);
136                 break;
137         case TYPE_TYPEDEF:
138                 hash = hash_ptr(type->typedeft.typedefe);
139                 break;
140         case TYPE_TYPEOF:
141                 hash = hash_typeof_type(&type->typeoft);
142                 break;
143         }
144
145         unsigned some_prime = 99991;
146         hash ^= some_prime * type->base.qualifiers;
147
148         return hash;
149 }
150
151 static bool atomic_types_equal(const atomic_type_t *type1,
152                                                            const atomic_type_t *type2)
153 {
154         return type1->akind == type2->akind;
155 }
156
157 static bool function_types_equal(const function_type_t *type1,
158                                  const function_type_t *type2)
159 {
160         if (type1->return_type != type2->return_type)
161                 return false;
162         if (type1->variadic != type2->variadic)
163                 return false;
164         if (type1->unspecified_parameters != type2->unspecified_parameters)
165                 return false;
166         if (type1->kr_style_parameters != type2->kr_style_parameters)
167                 return false;
168         if (type1->linkage != type2->linkage)
169                 return false;
170         if (type1->modifiers != type2->modifiers)
171                 return false;
172         if (type1->calling_convention != type2->calling_convention)
173                 return false;
174
175         function_parameter_t *param1 = type1->parameters;
176         function_parameter_t *param2 = type2->parameters;
177         while (param1 != NULL && param2 != NULL) {
178                 if (param1->type != param2->type)
179                         return false;
180                 param1 = param1->next;
181                 param2 = param2->next;
182         }
183         if (param1 != NULL || param2 != NULL)
184                 return false;
185
186         return true;
187 }
188
189 static bool pointer_types_equal(const pointer_type_t *type1,
190                                 const pointer_type_t *type2)
191 {
192         return type1->points_to     == type2->points_to &&
193                type1->base_variable == type2->base_variable;
194 }
195
196 static bool reference_types_equal(const reference_type_t *type1,
197                                   const reference_type_t *type2)
198 {
199         return type1->refers_to == type2->refers_to;
200 }
201
202 static bool array_types_equal(const array_type_t *type1,
203                               const array_type_t *type2)
204 {
205         if (type1->element_type != type2->element_type)
206                 return false;
207         if (type1->is_variable != type2->is_variable)
208                 return false;
209         if (type1->is_static != type2->is_static)
210                 return false;
211         if (type1->size_constant != type2->size_constant)
212                 return false;
213
214         /* never identify vla types, because we need them for caching calculated
215          * sizes later in ast2firm */
216         if (type1->is_vla || type2->is_vla)
217                 return false;
218
219         /* TODO: compare size expressions for equality... */
220
221         return false;
222 }
223
224 static bool compound_types_equal(const compound_type_t *type1,
225                                  const compound_type_t *type2)
226 {
227         return type1->compound == type2->compound;
228 }
229
230 static bool enum_types_equal(const enum_type_t *type1,
231                              const enum_type_t *type2)
232 {
233         return type1->enume == type2->enume;
234 }
235
236 static bool typedef_types_equal(const typedef_type_t *type1,
237                                 const typedef_type_t *type2)
238 {
239         return type1->typedefe == type2->typedefe;
240 }
241
242 static bool typeof_types_equal(const typeof_type_t *type1,
243                                const typeof_type_t *type2)
244 {
245         if (type1->expression != type2->expression)
246                 return false;
247         if (type1->typeof_type != type2->typeof_type)
248                 return false;
249
250         return true;
251 }
252
253 static bool types_equal(const type_t *type1, const type_t *type2)
254 {
255         if (type1 == type2)
256                 return true;
257         if (type1->kind != type2->kind)
258                 return false;
259         if (type1->base.qualifiers != type2->base.qualifiers)
260                 return false;
261
262         switch (type1->kind) {
263         case TYPE_ERROR:
264                 /* Hmm, the error type is never equal */
265                 return false;
266         case TYPE_ATOMIC:
267         case TYPE_IMAGINARY:
268         case TYPE_COMPLEX:
269                 return atomic_types_equal(&type1->atomic, &type2->atomic);
270         case TYPE_ENUM:
271                 return enum_types_equal(&type1->enumt, &type2->enumt);
272         case TYPE_COMPOUND_STRUCT:
273         case TYPE_COMPOUND_UNION:
274                 return compound_types_equal(&type1->compound, &type2->compound);
275         case TYPE_FUNCTION:
276                 return function_types_equal(&type1->function, &type2->function);
277         case TYPE_POINTER:
278                 return pointer_types_equal(&type1->pointer, &type2->pointer);
279         case TYPE_REFERENCE:
280                 return reference_types_equal(&type1->reference, &type2->reference);
281         case TYPE_ARRAY:
282                 return array_types_equal(&type1->array, &type2->array);
283         case TYPE_TYPEOF:
284                 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
285         case TYPE_TYPEDEF:
286                 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
287         }
288
289         abort();
290 }
291
292 #define HashSet                    type_hash_t
293 #define ValueType                  type_t*
294 #define NullValue                  NULL
295 #define DeletedValue               ((type_t*)-1)
296 #define Hash(this, key)            hash_type(key)
297 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
298 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
299
300 void _typehash_init(type_hash_t *hash);
301 #define hashset_init             _typehash_init
302 void _typehash_destroy(type_hash_t *hash);
303 #define hashset_destroy          _typehash_destroy
304 type_t *_typehash_insert(type_hash_t *hash, type_t *type);
305 #define hashset_insert           _typehash_insert
306 #define SCALAR_RETURN
307
308 #include "adt/hashset.c.inl"
309
310 static type_hash_t typehash;
311
312 void init_typehash(void)
313 {
314         _typehash_init(&typehash);
315 }
316
317 void exit_typehash(void)
318 {
319         _typehash_destroy(&typehash);
320 }
321
322 type_t *typehash_insert(type_t *type)
323 {
324         return _typehash_insert(&typehash, type);
325 }