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;
31 return type->atype * some_prime;
34 static unsigned hash_pointer_type(const pointer_type_t *type)
36 return hash_ptr(type->points_to);
39 static unsigned hash_compound_type(const compound_type_t *type)
41 unsigned result = hash_ptr(type->symbol);
46 static unsigned hash_type(const type_t *type);
48 static unsigned hash_method_type(const method_type_t *type)
50 unsigned result = hash_ptr(type->result_type);
52 method_parameter_t *parameter = type->parameters;
53 while(parameter != NULL) {
54 result ^= hash_ptr(parameter->type);
55 parameter = parameter->next;
61 static unsigned hash_enum_type(const enum_type_t *type)
63 unsigned result = hash_ptr(type->symbol);
68 static unsigned hash_type(const type_t *type)
74 panic("internalizing void or invalid types not possible");
77 hash = hash_atomic_type((const atomic_type_t*) type);
80 hash = hash_enum_type((const enum_type_t*) type);
82 case TYPE_COMPOUND_STRUCT:
83 case TYPE_COMPOUND_UNION:
84 hash = hash_compound_type((const compound_type_t*) type);
87 hash = hash_method_type((const method_type_t*) type);
90 hash = hash_pointer_type((const pointer_type_t*) type);
93 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
97 unsigned some_prime = 99991;
98 hash ^= some_prime * type->qualifiers;
103 static int atomic_types_equal(const atomic_type_t *type1,
104 const atomic_type_t *type2)
106 return type1->atype == type2->atype;
109 static int compound_types_equal(const compound_type_t *type1,
110 const compound_type_t *type2)
112 if(type1->type.type != type2->type.type)
114 if(type1->symbol != type2->symbol)
117 /* anonymous types? */
118 if(type1->symbol == NULL) {
119 /* previous tests should already have checked for this */
120 assert(type1 != type2);
121 /* anonymous types are only equal if they are the very same type */
125 /* non-anonymous types with same symbol are equal */
129 static int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
131 if(type1->symbol != type2->symbol)
134 /* anonymous types? */
135 if(type1->symbol == NULL) {
136 /* previous tests should already have checked for this */
137 assert(type1 != type2);
138 /* 2 anonymous enums are never equal */
142 /* non-anonymous types with same symbol are equal */
146 static int method_types_equal(const method_type_t *type1,
147 const method_type_t *type2)
149 if(type1->result_type != type2->result_type)
151 if(type1->variadic != type2->variadic)
153 if(type1->unspecified_parameters != type2->unspecified_parameters)
156 method_parameter_t *param1 = type1->parameters;
157 method_parameter_t *param2 = type2->parameters;
158 while(param1 != NULL && param2 != NULL) {
159 if(param1->type != param2->type)
161 param1 = param1->next;
162 param2 = param2->next;
164 if(param1 != NULL || param2 != NULL)
170 static int pointer_types_equal(const pointer_type_t *type1,
171 const pointer_type_t *type2)
173 return type1->points_to == type2->points_to;
176 static int builtin_types_equal(const builtin_type_t *type1,
177 const builtin_type_t *type2)
179 return type1->symbol == type2->symbol;
182 static int types_equal(const type_t *type1, const type_t *type2)
186 if(type1->type != type2->type)
188 if(type1->qualifiers != type2->qualifiers)
191 switch(type1->type) {
195 return atomic_types_equal((const atomic_type_t*) type1,
196 (const atomic_type_t*) type2);
197 case TYPE_COMPOUND_STRUCT:
198 case TYPE_COMPOUND_UNION:
199 return compound_types_equal((const compound_type_t*) type1,
200 (const compound_type_t*) type2);
202 return enum_types_equal((const enum_type_t*) type1,
203 (const enum_type_t*) type2);
205 return method_types_equal((const method_type_t*) type1,
206 (const method_type_t*) type2);
208 return pointer_types_equal((const pointer_type_t*) type1,
209 (const pointer_type_t*) type2);
211 return builtin_types_equal((const builtin_type_t*) type1,
212 (const builtin_type_t*) type2);
218 #define HashSet type_hash_t
219 #define HashSetIterator type_hash_iterator_t
220 #define ValueType type_t*
221 #define NullValue NULL
222 #define DeletedValue ((type_t*)-1)
223 #define Hash(this, key) hash_type(key)
224 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
225 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
227 #define hashset_init _typehash_init
228 #define hashset_init_size _typehash_init_size
229 #define hashset_destroy _typehash_destroy
230 #define hashset_insert _typehash_insert
231 #define hashset_remove typehash_remove
232 #define hashset_find typehash_find
233 #define hashset_size typehash_size
234 #define hashset_iterator_init typehash_iterator_init
235 #define hashset_iterator_next typehash_iterator_next
236 #define hashset_remove_iterator typehash_remove_iterator
238 #include "adt/hashset.c"
240 static type_hash_t typehash;
242 void init_typehash(void)
244 _typehash_init(&typehash);
247 void exit_typehash(void)
249 _typehash_destroy(&typehash);
252 type_t *typehash_insert(type_t *type)
254 return _typehash_insert(&typehash, type);