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
22 unsigned hash_ptr(const void *ptr)
24 unsigned ptr_int = ((char*) ptr - (char*) NULL);
29 unsigned hash_atomic_type(const atomic_type_t *type)
31 unsigned some_prime = 27644437;
33 return type->atype * some_prime;
37 unsigned hash_pointer_type(const pointer_type_t *type)
39 return hash_ptr(type->points_to);
43 unsigned hash_compound_type(const compound_type_t *type)
45 unsigned result = hash_ptr(type->symbol);
51 unsigned hash_type(const type_t *type);
54 unsigned hash_method_type(const method_type_t *type)
56 unsigned result = hash_ptr(type->result_type);
58 method_parameter_t *parameter = type->parameters;
59 while(parameter != NULL) {
60 result ^= hash_ptr(parameter->type);
61 parameter = parameter->next;
68 unsigned hash_enum_type(const enum_type_t *type)
70 unsigned result = hash_ptr(type->symbol);
78 unsigned hash_type(const type_t *type)
84 panic("internalizing void or invalid types not possible");
87 hash = hash_atomic_type((const atomic_type_t*) type);
90 hash = hash_enum_type((const enum_type_t*) type);
92 case TYPE_COMPOUND_STRUCT:
93 case TYPE_COMPOUND_UNION:
94 hash = hash_compound_type((const compound_type_t*) type);
97 hash = hash_method_type((const method_type_t*) type);
100 hash = hash_pointer_type((const pointer_type_t*) type);
103 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
107 unsigned some_prime = 99991;
108 hash ^= some_prime * type->qualifiers;
114 int atomic_types_equal(const atomic_type_t *type1, const atomic_type_t *type2)
116 return type1->atype == type2->atype;
120 int 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 if(type1->symbol == NULL) {
129 /* previous tests should already have checked for this */
130 assert(type1 != type2);
131 /* anonymous types are only equal if they are the very same type */
134 /* non-anonymous types are equal if they have the same symbol */
135 /* TODO: is this correct */
140 declaration_t *entry1 = type1->context.declarations;
141 declaration_t *entry2 = type2->context.declarations;
143 while(entry1 != NULL && entry2 != NULL) {
144 if(entry1->type != entry2->type)
146 if(entry1->symbol != entry2->symbol)
148 entry1 = entry1->next;
149 entry2 = entry2->next;
151 if(entry1 != NULL || entry2 != NULL)
159 int method_types_equal(const method_type_t *type1, const method_type_t *type2)
161 if(type1->result_type != type2->result_type)
163 if(type1->variadic != type2->variadic)
165 if(type1->unspecified_parameters != type2->unspecified_parameters)
168 method_parameter_t *param1 = type1->parameters;
169 method_parameter_t *param2 = type2->parameters;
170 while(param1 != NULL && param2 != NULL) {
171 if(param1->type != param2->type)
173 param1 = param1->next;
174 param2 = param2->next;
176 if(param1 != NULL || param2 != NULL)
183 int pointer_types_equal(const pointer_type_t *type1,
184 const pointer_type_t *type2)
186 return type1->points_to == type2->points_to;
190 int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
192 if(type1->symbol != NULL && type1->symbol == type2->symbol)
195 enum_entry_t *entry1 = type1->entries;
196 enum_entry_t *entry2 = type2->entries;
197 while(entry1 != NULL && entry2 != NULL) {
198 if(entry1->symbol != entry2->symbol)
200 /* TODO: compare expressions */
201 entry1 = entry1->next;
202 entry2 = entry2->next;
204 if(entry1 != NULL || entry2 != NULL)
211 int builtin_types_equal(const builtin_type_t *type1,
212 const builtin_type_t *type2)
214 return type1->symbol == type2->symbol;
218 int types_equal(const type_t *type1, const type_t *type2)
222 if(type1->type != type2->type)
224 if(type1->qualifiers != type2->qualifiers)
227 switch(type1->type) {
231 return atomic_types_equal((const atomic_type_t*) type1,
232 (const atomic_type_t*) type2);
233 case TYPE_COMPOUND_STRUCT:
234 case TYPE_COMPOUND_UNION:
235 return compound_types_equal((const compound_type_t*) type1,
236 (const compound_type_t*) type2);
238 return enum_types_equal((const enum_type_t*) type1,
239 (const enum_type_t*) type2);
241 return method_types_equal((const method_type_t*) type1,
242 (const method_type_t*) type2);
244 return pointer_types_equal((const pointer_type_t*) type1,
245 (const pointer_type_t*) type2);
247 return builtin_types_equal((const builtin_type_t*) type1,
248 (const builtin_type_t*) type2);
254 #define HashSet type_hash_t
255 #define HashSetIterator type_hash_iterator_t
256 #define ValueType type_t*
257 #define NullValue NULL
258 #define DeletedValue ((type_t*)-1)
259 #define Hash(this, key) hash_type(key)
260 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
261 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
263 #define hashset_init _typehash_init
264 #define hashset_init_size _typehash_init_size
265 #define hashset_destroy _typehash_destroy
266 #define hashset_insert _typehash_insert
267 #define hashset_remove typehash_remove
268 #define hashset_find typehash_find
269 #define hashset_size typehash_size
270 #define hashset_iterator_init typehash_iterator_init
271 #define hashset_iterator_next typehash_iterator_next
272 #define hashset_remove_iterator typehash_remove_iterator
274 #include "adt/hashset.c"
276 static type_hash_t typehash;
278 void init_typehash(void)
280 _typehash_init(&typehash);
283 void exit_typehash(void)
285 _typehash_destroy(&typehash);
288 type_t *typehash_insert(type_t *type)
290 return _typehash_insert(&typehash, type);