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