12 #define HashSet type_hash_t
13 #define HashSetIterator type_hash_iterator_t
14 #define ValueType type_t*
15 #include "adt/hashset.h"
17 #undef HashSetIterator
20 /* TODO: ^= is a bad way of combining hashes since most addresses are very
23 static unsigned hash_ptr(const void *ptr)
25 unsigned ptr_int = ((char*) ptr - (char*) NULL);
29 static unsigned hash_atomic_type(const atomic_type_t *type)
31 unsigned some_prime = 27644437;
32 unsigned result = type->atype * some_prime;
37 static unsigned hash_pointer_type(const pointer_type_t *type)
39 return hash_ptr(type->points_to);
42 static unsigned hash_array_type(const array_type_t *type)
44 return hash_ptr(type->element_type);
47 static unsigned hash_compound_type(const compound_type_t *type)
49 unsigned result = hash_ptr(type->symbol);
54 static unsigned hash_type(const type_t *type);
56 static unsigned hash_method_type(const method_type_t *type)
58 unsigned result = hash_ptr(type->result_type);
60 method_parameter_t *parameter = type->parameters;
61 while(parameter != NULL) {
62 result ^= hash_ptr(parameter->type);
63 parameter = parameter->next;
69 static unsigned hash_enum_type(const enum_type_t *type)
71 unsigned result = hash_ptr(type->symbol);
76 static unsigned hash_type(const type_t *type)
82 panic("internalizing void or invalid types not possible");
85 hash = hash_atomic_type((const atomic_type_t*) type);
88 hash = hash_enum_type((const enum_type_t*) type);
90 case TYPE_COMPOUND_STRUCT:
91 case TYPE_COMPOUND_UNION:
92 hash = hash_compound_type((const compound_type_t*) type);
95 hash = hash_method_type((const method_type_t*) type);
98 hash = hash_pointer_type((const pointer_type_t*) type);
101 hash = hash_array_type((const array_type_t*) type);
104 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
108 unsigned some_prime = 99991;
109 hash ^= some_prime * type->qualifiers;
114 static bool atomic_types_equal(const atomic_type_t *type1,
115 const atomic_type_t *type2)
117 return type1->atype == type2->atype;
120 static bool compound_types_equal(const compound_type_t *type1,
121 const compound_type_t *type2)
123 if(type1->type.type != type2->type.type)
125 if(type1->symbol != type2->symbol)
128 /* anonymous types? */
129 if(type1->symbol == NULL) {
130 /* previous tests should already have checked for this */
131 assert(type1 != type2);
132 /* anonymous types are only equal if they are the very same type */
136 /* non-anonymous types with same symbol are equal */
140 static bool enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
142 if(type1->symbol != type2->symbol)
145 /* anonymous types? */
146 if(type1->symbol == NULL) {
147 /* previous tests should already have checked for this */
148 assert(type1 != type2);
149 /* 2 anonymous enums are never equal */
153 /* non-anonymous types with same symbol are equal */
157 static bool method_types_equal(const method_type_t *type1,
158 const method_type_t *type2)
160 if(type1->result_type != type2->result_type)
162 if(type1->variadic != type2->variadic)
164 if(type1->unspecified_parameters != type2->unspecified_parameters)
167 method_parameter_t *param1 = type1->parameters;
168 method_parameter_t *param2 = type2->parameters;
169 while(param1 != NULL && param2 != NULL) {
170 if(param1->type != param2->type)
172 param1 = param1->next;
173 param2 = param2->next;
175 if(param1 != NULL || param2 != NULL)
181 static bool pointer_types_equal(const pointer_type_t *type1,
182 const pointer_type_t *type2)
184 return type1->points_to == type2->points_to;
187 static bool array_types_equal(const array_type_t *type1,
188 const array_type_t *type2)
190 if(type1->element_type != type2->element_type)
192 if(type1->is_variable != type2->is_variable)
194 if(type1->is_static != type2->is_static)
196 /* TODO: compare expressions for equality... */
197 if(type1->size != type2->size)
203 static bool builtin_types_equal(const builtin_type_t *type1,
204 const builtin_type_t *type2)
206 return type1->symbol == type2->symbol;
209 static bool types_equal(const type_t *type1, const type_t *type2)
213 if(type1->type != type2->type)
215 if(type1->qualifiers != type2->qualifiers)
218 switch(type1->type) {
222 return atomic_types_equal((const atomic_type_t*) type1,
223 (const atomic_type_t*) type2);
224 case TYPE_COMPOUND_STRUCT:
225 case TYPE_COMPOUND_UNION:
226 return compound_types_equal((const compound_type_t*) type1,
227 (const compound_type_t*) type2);
229 return enum_types_equal((const enum_type_t*) type1,
230 (const enum_type_t*) type2);
232 return method_types_equal((const method_type_t*) type1,
233 (const method_type_t*) type2);
235 return pointer_types_equal((const pointer_type_t*) type1,
236 (const pointer_type_t*) type2);
238 return array_types_equal((const array_type_t*) type1,
239 (const array_type_t*) type2);
241 return builtin_types_equal((const builtin_type_t*) type1,
242 (const builtin_type_t*) type2);
248 #define HashSet type_hash_t
249 #define HashSetIterator type_hash_iterator_t
250 #define ValueType type_t*
251 #define NullValue NULL
252 #define DeletedValue ((type_t*)-1)
253 #define Hash(this, key) hash_type(key)
254 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
255 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
257 #define hashset_init _typehash_init
258 #define hashset_init_size _typehash_init_size
259 #define hashset_destroy _typehash_destroy
260 #define hashset_insert _typehash_insert
261 #define hashset_remove typehash_remove
262 #define hashset_find typehash_find
263 #define hashset_size typehash_size
264 #define hashset_iterator_init typehash_iterator_init
265 #define hashset_iterator_next typehash_iterator_next
266 #define hashset_remove_iterator typehash_remove_iterator
268 #include "adt/hashset.c"
270 static type_hash_t typehash;
272 void init_typehash(void)
274 _typehash_init(&typehash);
277 void exit_typehash(void)
279 _typehash_destroy(&typehash);
282 type_t *typehash_insert(type_t *type)
284 return _typehash_insert(&typehash, type);