12 #define HashSet type_hash_t
13 #define HashSetIterator type_hash_iterator_t
14 #define ValueType type_t*
15 #include "adt/hashset.h"
17 #undef HashSetIterator
20 /* TODO: ^= is a bad way of combining hashes since most addresses are very
23 static unsigned hash_ptr(const void *ptr)
25 unsigned ptr_int = ((char*) ptr - (char*) NULL);
29 static unsigned hash_atomic_type(const type_t *type)
31 unsigned some_prime = 27644437;
32 unsigned result = type->v.atomic_type.atype * some_prime;
37 static unsigned hash_pointer_type(const type_t *type)
39 return hash_ptr(type->v.pointer_type.points_to);
42 static unsigned hash_array_type(const type_t *type)
44 return hash_ptr(type->v.array_type.element_type);
47 static unsigned hash_compound_type(const type_t *type)
49 return hash_ptr(type->v.compound_type.declaration);
52 static unsigned hash_type(const type_t *type);
54 static unsigned hash_function_type(const type_t *type)
56 unsigned result = hash_ptr(type->v.function_type.result_type);
58 function_parameter_t *parameter = type->v.function_type.parameters;
59 while(parameter != NULL) {
60 result ^= hash_ptr(parameter->type);
61 parameter = parameter->next;
67 static unsigned hash_enum_type(const type_t *type)
69 return hash_ptr(type->v.enum_type.declaration);
72 static unsigned hash_typeof_type(const type_t *type)
74 unsigned result = hash_ptr(type->v.typeof_type.expression);
75 result ^= hash_ptr(type->v.typeof_type.typeof_type);
80 static unsigned hash_type(const type_t *type)
86 panic("internalizing void or invalid types not possible");
89 hash = hash_atomic_type(type);
92 hash = hash_enum_type(type);
94 case TYPE_COMPOUND_STRUCT:
95 case TYPE_COMPOUND_UNION:
96 hash = hash_compound_type(type);
99 hash = hash_function_type(type);
102 hash = hash_pointer_type(type);
105 hash = hash_array_type(type);
108 hash = hash_ptr(type->v.builtin_type.symbol);
111 hash = hash_ptr(type->v.typedef_type.declaration);
114 hash = hash_typeof_type(type);
118 unsigned some_prime = 99991;
119 hash ^= some_prime * type->qualifiers;
124 static bool atomic_types_equal(const type_t *type1,
127 return type1->v.atomic_type.atype == type2->v.atomic_type.atype;
130 static bool function_types_equal(const type_t *type1,
133 if(type1->v.function_type.result_type != type2->v.function_type.result_type)
135 if(type1->v.function_type.variadic != type2->v.function_type.variadic)
137 if(type1->v.function_type.unspecified_parameters !=
138 type2->v.function_type.unspecified_parameters)
141 function_parameter_t *param1 = type1->v.function_type.parameters;
142 function_parameter_t *param2 = type2->v.function_type.parameters;
143 while(param1 != NULL && param2 != NULL) {
144 if(param1->type != param2->type)
146 param1 = param1->next;
147 param2 = param2->next;
149 if(param1 != NULL || param2 != NULL)
155 static bool pointer_types_equal(const type_t *type1,
158 return type1->v.pointer_type.points_to == type2->v.pointer_type.points_to;
161 static bool array_types_equal(const type_t *type1,
164 if(type1->v.array_type.element_type != type2->v.array_type.element_type)
166 if(type1->v.array_type.is_variable != type2->v.array_type.is_variable)
168 if(type1->v.array_type.is_static != type2->v.array_type.is_static)
170 /* TODO: compare expressions for equality... */
171 if(type1->v.array_type.size != type2->v.array_type.size)
177 static bool builtin_types_equal(const type_t *type1,
180 return type1->v.builtin_type.symbol == type2->v.builtin_type.symbol;
183 static bool compound_types_equal(const type_t *type1,
186 return type1->v.compound_type.declaration == type2->v.compound_type.declaration;
189 static bool enum_types_equal(const type_t *type1,
192 return type1->v.enum_type.declaration == type2->v.enum_type.declaration;
195 static bool typedef_types_equal(const type_t *type1,
198 return type1->v.typedef_type.declaration == type2->v.typedef_type.declaration;
201 static bool typeof_types_equal(const type_t *type1,
204 if(type1->v.typeof_type.expression != type2->v.typeof_type.expression)
206 if(type1->v.typeof_type.typeof_type != type2->v.typeof_type.typeof_type)
212 static bool types_equal(const type_t *type1, const type_t *type2)
216 if(type1->type != type2->type)
218 if(type1->qualifiers != type2->qualifiers)
221 switch(type1->type) {
225 return atomic_types_equal(type1, type2);
227 return enum_types_equal(type1, type2);
228 case TYPE_COMPOUND_STRUCT:
229 case TYPE_COMPOUND_UNION:
230 return compound_types_equal(type1, type2);
232 return function_types_equal(type1, type2);
234 return pointer_types_equal(type1, type2);
236 return array_types_equal(type1, type2);
238 return builtin_types_equal(type1, type2);
240 return typeof_types_equal(type1, type2);
242 return typedef_types_equal(type1, type2);
248 #define HashSet type_hash_t
249 #define HashSetIterator type_hash_iterator_t
250 #define ValueType type_t*
251 #define NullValue NULL
252 #define DeletedValue ((type_t*)-1)
253 #define Hash(this, key) hash_type(key)
254 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
255 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
257 #define hashset_init _typehash_init
258 #define hashset_init_size _typehash_init_size
259 #define hashset_destroy _typehash_destroy
260 #define hashset_insert _typehash_insert
261 #define hashset_remove typehash_remove
262 #define hashset_find typehash_find
263 #define hashset_size typehash_size
264 #define hashset_iterator_init typehash_iterator_init
265 #define hashset_iterator_next typehash_iterator_next
266 #define hashset_remove_iterator typehash_remove_iterator
268 #include "adt/hashset.c"
270 static type_hash_t typehash;
272 void init_typehash(void)
274 _typehash_init(&typehash);
277 void exit_typehash(void)
279 _typehash_destroy(&typehash);
282 type_t *typehash_insert(type_t *type)
284 return _typehash_insert(&typehash, type);