10 #define HashSet type_hash_t
11 #define HashSetIterator type_hash_iterator_t
12 #define ValueType type_t*
13 #include "adt/hashset.h"
15 #undef HashSetIterator
18 /* TODO: ^= is a bad way of combining hashes since most addresses are very
21 static unsigned hash_ptr(const void *ptr)
23 unsigned ptr_int = ((char*) ptr - (char*) NULL);
27 static unsigned hash_atomic_type(const atomic_type_t *type)
29 unsigned some_prime = 27644437;
30 unsigned result = type->atype * some_prime;
35 static unsigned hash_pointer_type(const pointer_type_t *type)
37 return hash_ptr(type->points_to);
40 static unsigned hash_compound_type(const compound_type_t *type)
42 unsigned result = hash_ptr(type->symbol);
47 static unsigned hash_type(const type_t *type);
49 static unsigned hash_method_type(const method_type_t *type)
51 unsigned result = hash_ptr(type->result_type);
53 method_parameter_t *parameter = type->parameters;
54 while(parameter != NULL) {
55 result ^= hash_ptr(parameter->type);
56 parameter = parameter->next;
62 static unsigned hash_enum_type(const enum_type_t *type)
64 unsigned result = hash_ptr(type->symbol);
69 static unsigned hash_type(const type_t *type)
75 panic("internalizing void or invalid types not possible");
78 hash = hash_atomic_type((const atomic_type_t*) type);
81 hash = hash_enum_type((const enum_type_t*) type);
83 case TYPE_COMPOUND_STRUCT:
84 case TYPE_COMPOUND_UNION:
85 hash = hash_compound_type((const compound_type_t*) type);
88 hash = hash_method_type((const method_type_t*) type);
91 hash = hash_pointer_type((const pointer_type_t*) type);
94 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
98 unsigned some_prime = 99991;
99 hash ^= some_prime * type->qualifiers;
104 static int atomic_types_equal(const atomic_type_t *type1,
105 const atomic_type_t *type2)
107 return type1->atype == type2->atype;
110 static int compound_types_equal(const compound_type_t *type1,
111 const compound_type_t *type2)
113 if(type1->type.type != type2->type.type)
115 if(type1->symbol != type2->symbol)
118 /* anonymous types? */
119 if(type1->symbol == NULL) {
120 /* previous tests should already have checked for this */
121 assert(type1 != type2);
122 /* anonymous types are only equal if they are the very same type */
126 /* non-anonymous types with same symbol are equal */
130 static int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
132 if(type1->symbol != type2->symbol)
135 /* anonymous types? */
136 if(type1->symbol == NULL) {
137 /* previous tests should already have checked for this */
138 assert(type1 != type2);
139 /* 2 anonymous enums are never equal */
143 /* non-anonymous types with same symbol are equal */
147 static int method_types_equal(const method_type_t *type1,
148 const method_type_t *type2)
150 if(type1->result_type != type2->result_type)
152 if(type1->variadic != type2->variadic)
154 if(type1->unspecified_parameters != type2->unspecified_parameters)
157 method_parameter_t *param1 = type1->parameters;
158 method_parameter_t *param2 = type2->parameters;
159 while(param1 != NULL && param2 != NULL) {
160 if(param1->type != param2->type)
162 param1 = param1->next;
163 param2 = param2->next;
165 if(param1 != NULL || param2 != NULL)
171 static int pointer_types_equal(const pointer_type_t *type1,
172 const pointer_type_t *type2)
174 return type1->points_to == type2->points_to;
177 static int builtin_types_equal(const builtin_type_t *type1,
178 const builtin_type_t *type2)
180 return type1->symbol == type2->symbol;
183 static int types_equal(const type_t *type1, const type_t *type2)
187 if(type1->type != type2->type)
189 if(type1->qualifiers != type2->qualifiers)
192 switch(type1->type) {
196 return atomic_types_equal((const atomic_type_t*) type1,
197 (const atomic_type_t*) type2);
198 case TYPE_COMPOUND_STRUCT:
199 case TYPE_COMPOUND_UNION:
200 return compound_types_equal((const compound_type_t*) type1,
201 (const compound_type_t*) type2);
203 return enum_types_equal((const enum_type_t*) type1,
204 (const enum_type_t*) type2);
206 return method_types_equal((const method_type_t*) type1,
207 (const method_type_t*) type2);
209 return pointer_types_equal((const pointer_type_t*) type1,
210 (const pointer_type_t*) type2);
212 return builtin_types_equal((const builtin_type_t*) type1,
213 (const builtin_type_t*) type2);
219 #define HashSet type_hash_t
220 #define HashSetIterator type_hash_iterator_t
221 #define ValueType type_t*
222 #define NullValue NULL
223 #define DeletedValue ((type_t*)-1)
224 #define Hash(this, key) hash_type(key)
225 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
226 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
228 #define hashset_init _typehash_init
229 #define hashset_init_size _typehash_init_size
230 #define hashset_destroy _typehash_destroy
231 #define hashset_insert _typehash_insert
232 #define hashset_remove typehash_remove
233 #define hashset_find typehash_find
234 #define hashset_size typehash_size
235 #define hashset_iterator_init typehash_iterator_init
236 #define hashset_iterator_next typehash_iterator_next
237 #define hashset_remove_iterator typehash_remove_iterator
239 #include "adt/hashset.c"
241 static type_hash_t typehash;
243 void init_typehash(void)
245 _typehash_init(&typehash);
248 void exit_typehash(void)
250 _typehash_destroy(&typehash);
253 type_t *typehash_insert(type_t *type)
255 return _typehash_insert(&typehash, type);