2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
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.
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.
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
24 #include "type_hash.h"
26 #include "adt/error.h"
31 #define HashSet type_hash_t
32 #define HashSetIterator type_hash_iterator_t
33 #define ValueType type_t*
34 #include "adt/hashset.h"
36 #undef HashSetIterator
39 typedef struct type_hash_iterator_t type_hash_iterator_t;
40 typedef struct type_hash_t type_hash_t;
42 /* TODO: ^= is a bad way of combining hashes since most addresses are very
45 static unsigned hash_ptr(const void *ptr)
47 unsigned ptr_int = ((char*) ptr - (char*) NULL);
51 static unsigned hash_atomic_type(const atomic_type_t *type)
53 unsigned some_prime = 27644437;
54 unsigned result = type->akind * some_prime;
59 static unsigned hash_complex_type(const complex_type_t *type)
61 unsigned some_prime = 27644437;
62 unsigned result = type->akind * some_prime;
67 static unsigned hash_imaginary_type(const imaginary_type_t *type)
69 unsigned some_prime = 27644437;
70 unsigned result = type->akind * some_prime;
75 static unsigned hash_pointer_type(const pointer_type_t *type)
77 return hash_ptr(type->points_to);
80 static unsigned hash_array_type(const array_type_t *type)
82 return hash_ptr(type->element_type);
85 static unsigned hash_compound_type(const compound_type_t *type)
87 return hash_ptr(type->compound);
90 static unsigned hash_type(const type_t *type);
92 static unsigned hash_function_type(const function_type_t *type)
94 unsigned result = hash_ptr(type->return_type);
96 function_parameter_t *parameter = type->parameters;
97 while (parameter != NULL) {
98 result ^= hash_ptr(parameter->type);
99 parameter = parameter->next;
101 result += type->linkage;
102 result += type->calling_convention;
107 static unsigned hash_enum_type(const enum_type_t *type)
109 return hash_ptr(type->enume);
112 static unsigned hash_typeof_type(const typeof_type_t *type)
114 unsigned result = hash_ptr(type->expression);
115 result ^= hash_ptr(type->typeof_type);
120 static unsigned hash_bitfield_type(const bitfield_type_t *type)
122 unsigned result = hash_ptr(type->base_type);
128 static unsigned hash_type(const type_t *type)
132 switch (type->kind) {
134 panic("internalizing void or invalid types not possible");
139 hash = hash_atomic_type(&type->atomic);
142 hash = hash_complex_type(&type->complex);
145 hash = hash_imaginary_type(&type->imaginary);
148 hash = hash_enum_type(&type->enumt);
150 case TYPE_COMPOUND_STRUCT:
151 case TYPE_COMPOUND_UNION:
152 hash = hash_compound_type(&type->compound);
155 hash = hash_function_type(&type->function);
158 hash = hash_pointer_type(&type->pointer);
161 hash = hash_array_type(&type->array);
164 hash = hash_ptr(type->builtin.symbol);
167 hash = hash_ptr(type->typedeft.typedefe);
170 hash = hash_typeof_type(&type->typeoft);
173 hash = hash_bitfield_type(&type->bitfield);
177 unsigned some_prime = 99991;
178 hash ^= some_prime * type->base.qualifiers;
183 static bool atomic_types_equal(const atomic_type_t *type1,
184 const atomic_type_t *type2)
186 return type1->akind == type2->akind;
189 static bool complex_types_equal(const complex_type_t *type1,
190 const complex_type_t *type2)
192 return type1->akind == type2->akind;
195 static bool imaginary_types_equal(const imaginary_type_t *type1,
196 const imaginary_type_t *type2)
198 return type1->akind == type2->akind;
201 static bool function_types_equal(const function_type_t *type1,
202 const function_type_t *type2)
204 if (type1->return_type != type2->return_type)
206 if (type1->variadic != type2->variadic)
208 if (type1->unspecified_parameters != type2->unspecified_parameters)
210 if (type1->kr_style_parameters != type2->kr_style_parameters)
212 if (type1->linkage != type2->linkage)
214 if (type1->calling_convention != type2->calling_convention)
217 function_parameter_t *param1 = type1->parameters;
218 function_parameter_t *param2 = type2->parameters;
219 while (param1 != NULL && param2 != NULL) {
220 if (param1->type != param2->type)
222 param1 = param1->next;
223 param2 = param2->next;
225 if (param1 != NULL || param2 != NULL)
231 static bool pointer_types_equal(const pointer_type_t *type1,
232 const pointer_type_t *type2)
234 return type1->points_to == type2->points_to &&
235 type1->base_variable == type2->base_variable;
238 static bool array_types_equal(const array_type_t *type1,
239 const array_type_t *type2)
241 if (type1->element_type != type2->element_type)
243 if (type1->is_variable != type2->is_variable)
245 if (type1->is_static != type2->is_static)
247 if (type1->size_constant != type2->size_constant)
250 /* never identify vla types, because we need them for caching calculated
251 * sizes later in ast2firm */
252 if (type1->is_vla || type2->is_vla)
255 /* TODO: compare size expressions for equality... */
260 static bool builtin_types_equal(const builtin_type_t *type1,
261 const builtin_type_t *type2)
263 return type1->symbol == type2->symbol;
266 static bool compound_types_equal(const compound_type_t *type1,
267 const compound_type_t *type2)
269 return type1->compound == type2->compound;
272 static bool enum_types_equal(const enum_type_t *type1,
273 const enum_type_t *type2)
275 return type1->enume == type2->enume;
278 static bool typedef_types_equal(const typedef_type_t *type1,
279 const typedef_type_t *type2)
281 return type1->typedefe == type2->typedefe;
284 static bool typeof_types_equal(const typeof_type_t *type1,
285 const typeof_type_t *type2)
287 if (type1->expression != type2->expression)
289 if (type1->typeof_type != type2->typeof_type)
295 static bool bitfield_types_equal(const bitfield_type_t *type1,
296 const bitfield_type_t *type2)
298 if (type1->base_type != type2->base_type)
300 /* TODO: compare size expression */
304 static bool types_equal(const type_t *type1, const type_t *type2)
308 if (type1->kind != type2->kind)
310 if (type1->base.qualifiers != type2->base.qualifiers)
312 if (type1->base.modifiers != type2->base.modifiers)
315 switch (type1->kind) {
317 /* Hmm, the error type is never equal */
322 return atomic_types_equal(&type1->atomic, &type2->atomic);
324 return complex_types_equal(&type1->complex, &type2->complex);
326 return imaginary_types_equal(&type1->imaginary, &type2->imaginary);
328 return enum_types_equal(&type1->enumt, &type2->enumt);
329 case TYPE_COMPOUND_STRUCT:
330 case TYPE_COMPOUND_UNION:
331 return compound_types_equal(&type1->compound, &type2->compound);
333 return function_types_equal(&type1->function, &type2->function);
335 return pointer_types_equal(&type1->pointer, &type2->pointer);
337 return array_types_equal(&type1->array, &type2->array);
339 return builtin_types_equal(&type1->builtin, &type2->builtin);
341 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
343 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
345 return bitfield_types_equal(&type1->bitfield, &type2->bitfield);
351 #define HashSet type_hash_t
352 #define HashSetIterator type_hash_iterator_t
353 #define ValueType type_t*
354 #define NullValue NULL
355 #define DeletedValue ((type_t*)-1)
356 #define Hash(this, key) hash_type(key)
357 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
358 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
360 #define hashset_init _typehash_init
361 #define hashset_init_size _typehash_init_size
362 #define hashset_destroy _typehash_destroy
363 #define hashset_insert _typehash_insert
364 #define hashset_remove typehash_remove
365 #define hashset_find typehash_find
366 #define hashset_size typehash_size
367 #define hashset_iterator_init typehash_iterator_init
368 #define hashset_iterator_next typehash_iterator_next
369 #define hashset_remove_iterator typehash_remove_iterator
370 #define SCALAR_RETURN
372 #include "adt/hashset.c"
374 static type_hash_t typehash;
376 void init_typehash(void)
378 _typehash_init(&typehash);
381 void exit_typehash(void)
383 _typehash_destroy(&typehash);
386 type_t *typehash_insert(type_t *type)
388 return _typehash_insert(&typehash, type);