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 = (ptr - 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_type_t *parameter = type->parameter_types;
56 while(parameter != NULL) {
57 result ^= hash_ptr(parameter->type);
58 parameter = parameter->next;
60 result ^= hash_ptr(type->abi_style);
66 unsigned hash_enum_type(const enum_type_t *type)
68 unsigned result = hash_ptr(type->symbol);
76 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);
102 unsigned some_prime = 99991;
103 hash ^= some_prime * type->qualifiers;
109 int atomic_types_equal(const atomic_type_t *type1, const atomic_type_t *type2)
111 return type1->atype == type2->atype;
115 int compound_types_equal(const compound_type_t *type1,
116 const compound_type_t *type2)
118 if(type1->symbol != type2->symbol)
122 struct_entry_t *entry1 = type1->entries;
123 struct_entry_t *entry2 = type2->entries;
125 while(entry1 != NULL && entry2 != NULL) {
126 if(entry1->type != entry2->type)
128 entry1 = entry1->next;
129 entry2 = entry2->next;
131 if(entry1 != NULL || entry2 != NULL)
139 int method_types_equal(const method_type_t *type1, const method_type_t *type2)
141 if(type1->result_type != type2->result_type)
144 if(type1->abi_style != type2->abi_style)
147 method_parameter_type_t *param1 = type1->parameter_types;
148 method_parameter_type_t *param2 = type2->parameter_types;
149 while(param1 != NULL && param2 != NULL) {
150 if(param1->type != param2->type)
152 param1 = param1->next;
153 param2 = param2->next;
155 if(param1 != NULL || param2 != NULL)
162 int pointer_types_equal(const pointer_type_t *type1,
163 const pointer_type_t *type2)
165 return type1->points_to == type2->points_to;
169 int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
172 return type1->symbol == type2->symbol;
176 int types_equal(const type_t *type1, const type_t *type2)
180 if(type1->type != type2->type)
182 if(type1->qualifiers != type2->qualifiers)
185 switch(type1->type) {
189 return atomic_types_equal((const atomic_type_t*) type1,
190 (const atomic_type_t*) type2);
191 case TYPE_COMPOUND_STRUCT:
192 case TYPE_COMPOUND_UNION:
193 return compound_types_equal((const compound_type_t*) type1,
194 (const compound_type_t*) type2);
196 return enum_types_equal((const enum_type_t*) type1,
197 (const enum_type_t*) type2);
199 return method_types_equal((const method_type_t*) type1,
200 (const method_type_t*) type2);
202 return pointer_types_equal((const pointer_type_t*) type1,
203 (const pointer_type_t*) type2);
209 #define HashSet type_hash_t
210 #define HashSetIterator type_hash_iterator_t
211 #define ValueType type_t*
212 #define NullValue NULL
213 #define DeletedValue ((type_t*)-1)
214 #define Hash(this, key) hash_type(key)
215 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
216 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
218 #define hashset_init _typehash_init
219 #define hashset_init_size _typehash_init_size
220 #define hashset_destroy _typehash_destroy
221 #define hashset_insert _typehash_insert
222 #define hashset_remove typehash_remove
223 #define hashset_find typehash_find
224 #define hashset_size typehash_size
225 #define hashset_iterator_init typehash_iterator_init
226 #define hashset_iterator_next typehash_iterator_next
227 #define hashset_remove_iterator typehash_remove_iterator
229 #include "adt/hashset.c"
231 static type_hash_t typehash;
233 void init_typehash(void)
235 _typehash_init(&typehash);
238 void exit_typehash(void)
240 _typehash_destroy(&typehash);
243 type_t *typehash_insert(type_t *type)
245 return _typehash_insert(&typehash, type);