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
19 unsigned hash_ptr(const void *ptr)
21 unsigned ptr_int = ((char*) ptr - (char*) NULL);
26 unsigned hash_atomic_type(const atomic_type_t *type)
28 unsigned some_prime = 27644437;
30 return type->atype * some_prime;
34 unsigned hash_pointer_type(const pointer_type_t *type)
36 return hash_ptr(type->points_to);
40 unsigned hash_compound_type(const compound_type_t *type)
42 unsigned result = hash_ptr(type->symbol);
48 unsigned hash_type(const type_t *type);
51 unsigned hash_method_type(const method_type_t *type)
53 unsigned result = hash_ptr(type->result_type);
55 method_parameter_t *parameter = type->parameters;
56 while(parameter != NULL) {
57 result ^= hash_ptr(parameter);
58 parameter = parameter->next;
65 unsigned hash_enum_type(const enum_type_t *type)
67 unsigned result = hash_ptr(type->symbol);
75 unsigned hash_type(const type_t *type)
81 panic("internalizing void or invalid types not possible");
84 hash = hash_atomic_type((const atomic_type_t*) type);
87 hash = hash_enum_type((const enum_type_t*) type);
89 case TYPE_COMPOUND_STRUCT:
90 case TYPE_COMPOUND_UNION:
91 hash = hash_compound_type((const compound_type_t*) type);
94 hash = hash_method_type((const method_type_t*) type);
97 hash = hash_pointer_type((const pointer_type_t*) type);
100 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
104 unsigned some_prime = 99991;
105 hash ^= some_prime * type->qualifiers;
111 int atomic_types_equal(const atomic_type_t *type1, const atomic_type_t *type2)
113 return type1->atype == type2->atype;
117 int compound_types_equal(const compound_type_t *type1,
118 const compound_type_t *type2)
120 if(type1->type.type != type2->type.type)
122 if(type1->symbol != type2->symbol)
125 if(type1->symbol == NULL) {
126 /* previous tests should already have checked for this */
127 assert(type1 != type2);
128 /* anonymous types are only equal if they are the very same type */
131 /* non-anonymous types are equal if they have the same symbol */
132 /* TODO: is this correct */
137 declaration_t *entry1 = type1->context.declarations;
138 declaration_t *entry2 = type2->context.declarations;
140 while(entry1 != NULL && entry2 != NULL) {
141 if(entry1->type != entry2->type)
143 if(entry1->symbol != entry2->symbol)
145 entry1 = entry1->next;
146 entry2 = entry2->next;
148 if(entry1 != NULL || entry2 != NULL)
156 int method_types_equal(const method_type_t *type1, const method_type_t *type2)
158 if(type1->result_type != type2->result_type)
161 method_parameter_t *param1 = type1->parameters;
162 method_parameter_t *param2 = type2->parameters;
163 while(param1 != NULL && param2 != NULL) {
164 if(param1->type != param2->type)
166 param1 = param1->next;
167 param2 = param2->next;
169 if(param1 != NULL || param2 != NULL)
176 int pointer_types_equal(const pointer_type_t *type1,
177 const pointer_type_t *type2)
179 return type1->points_to == type2->points_to;
183 int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
185 if(type1->symbol != NULL && type1->symbol == type2->symbol)
188 enum_entry_t *entry1 = type1->entries;
189 enum_entry_t *entry2 = type2->entries;
190 while(entry1 != NULL && entry2 != NULL) {
191 if(entry1->symbol != entry2->symbol)
193 /* TODO: compare expressions */
194 entry1 = entry1->next;
195 entry2 = entry2->next;
197 if(entry1 != NULL || entry2 != NULL)
204 int builtin_types_equal(const builtin_type_t *type1,
205 const builtin_type_t *type2)
207 return type1->symbol == type2->symbol;
211 int types_equal(const type_t *type1, const type_t *type2)
215 if(type1->type != type2->type)
217 if(type1->qualifiers != type2->qualifiers)
220 switch(type1->type) {
224 return atomic_types_equal((const atomic_type_t*) type1,
225 (const atomic_type_t*) type2);
226 case TYPE_COMPOUND_STRUCT:
227 case TYPE_COMPOUND_UNION:
228 return compound_types_equal((const compound_type_t*) type1,
229 (const compound_type_t*) type2);
231 return enum_types_equal((const enum_type_t*) type1,
232 (const enum_type_t*) type2);
234 return method_types_equal((const method_type_t*) type1,
235 (const method_type_t*) type2);
237 return pointer_types_equal((const pointer_type_t*) type1,
238 (const pointer_type_t*) type2);
240 return builtin_types_equal((const builtin_type_t*) type1,
241 (const builtin_type_t*) type2);
247 #define HashSet type_hash_t
248 #define HashSetIterator type_hash_iterator_t
249 #define ValueType type_t*
250 #define NullValue NULL
251 #define DeletedValue ((type_t*)-1)
252 #define Hash(this, key) hash_type(key)
253 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
254 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
256 #define hashset_init _typehash_init
257 #define hashset_init_size _typehash_init_size
258 #define hashset_destroy _typehash_destroy
259 #define hashset_insert _typehash_insert
260 #define hashset_remove typehash_remove
261 #define hashset_find typehash_find
262 #define hashset_size typehash_size
263 #define hashset_iterator_init typehash_iterator_init
264 #define hashset_iterator_next typehash_iterator_next
265 #define hashset_remove_iterator typehash_remove_iterator
267 #include "adt/hashset.c"
269 static type_hash_t typehash;
271 void init_typehash(void)
273 _typehash_init(&typehash);
276 void exit_typehash(void)
278 _typehash_destroy(&typehash);
281 type_t *typehash_insert(type_t *type)
283 return _typehash_insert(&typehash, type);