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 atomic_type_t *type)
31 unsigned some_prime = 27644437;
32 unsigned result = type->akind * some_prime;
37 static unsigned hash_pointer_type(const pointer_type_t *type)
39 return hash_ptr(type->points_to);
42 static unsigned hash_array_type(const array_type_t *type)
44 return hash_ptr(type->element_type);
47 static unsigned hash_compound_type(const compound_type_t *type)
49 return hash_ptr(type->declaration);
52 static unsigned hash_type(const type_t *type);
54 static unsigned hash_function_type(const function_type_t *type)
56 unsigned result = hash_ptr(type->return_type);
58 function_parameter_t *parameter = type->parameters;
59 while(parameter != NULL) {
60 result ^= hash_ptr(parameter->type);
61 parameter = parameter->next;
67 static unsigned hash_enum_type(const enum_type_t *type)
69 return hash_ptr(type->declaration);
72 static unsigned hash_typeof_type(const typeof_type_t *type)
74 unsigned result = hash_ptr(type->expression);
75 result ^= hash_ptr(type->typeof_type);
80 static unsigned hash_bitfield_type(const bitfield_type_t *type)
82 unsigned result = hash_ptr(type->base);
88 static unsigned hash_type(const type_t *type)
94 panic("internalizing void or invalid types not possible");
99 hash = hash_atomic_type(&type->atomic);
102 hash = hash_enum_type(&type->enumt);
104 case TYPE_COMPOUND_STRUCT:
105 case TYPE_COMPOUND_UNION:
106 hash = hash_compound_type(&type->compound);
109 hash = hash_function_type(&type->function);
112 hash = hash_pointer_type(&type->pointer);
115 hash = hash_array_type(&type->array);
118 hash = hash_ptr(type->builtin.symbol);
121 hash = hash_ptr(type->typedeft.declaration);
124 hash = hash_typeof_type(&type->typeoft);
127 hash = hash_bitfield_type(&type->bitfield);
131 unsigned some_prime = 99991;
132 hash ^= some_prime * type->base.qualifiers;
137 static bool atomic_types_equal(const atomic_type_t *type1,
138 const atomic_type_t *type2)
140 return type1->akind == type2->akind;
143 static bool function_types_equal(const function_type_t *type1,
144 const function_type_t *type2)
146 if(type1->return_type != type2->return_type)
148 if(type1->variadic != type2->variadic)
150 if(type1->unspecified_parameters != type2->unspecified_parameters)
152 if(type1->kr_style_parameters != type2->kr_style_parameters)
155 function_parameter_t *param1 = type1->parameters;
156 function_parameter_t *param2 = type2->parameters;
157 while(param1 != NULL && param2 != NULL) {
158 if(param1->type != param2->type)
160 param1 = param1->next;
161 param2 = param2->next;
163 if(param1 != NULL || param2 != NULL)
169 static bool pointer_types_equal(const pointer_type_t *type1,
170 const pointer_type_t *type2)
172 return type1->points_to == type2->points_to;
175 static bool array_types_equal(const array_type_t *type1,
176 const array_type_t *type2)
178 if(type1->element_type != type2->element_type)
180 if(type1->is_variable != type2->is_variable)
182 if(type1->is_static != type2->is_static)
185 /* TODO: compare size expressions for equality... */
190 static bool builtin_types_equal(const builtin_type_t *type1,
191 const builtin_type_t *type2)
193 return type1->symbol == type2->symbol;
196 static bool compound_types_equal(const compound_type_t *type1,
197 const compound_type_t *type2)
199 return type1->declaration == type2->declaration;
202 static bool enum_types_equal(const enum_type_t *type1,
203 const enum_type_t *type2)
205 return type1->declaration == type2->declaration;
208 static bool typedef_types_equal(const typedef_type_t *type1,
209 const typedef_type_t *type2)
211 return type1->declaration == type2->declaration;
214 static bool typeof_types_equal(const typeof_type_t *type1,
215 const typeof_type_t *type2)
217 if(type1->expression != type2->expression)
219 if(type1->typeof_type != type2->typeof_type)
225 static bool bitfield_types_equal(const bitfield_type_t *type1,
226 const bitfield_type_t *type2)
228 if(type1->base != type2->base)
230 /* TODO: compare size expression */
234 static bool types_equal(const type_t *type1, const type_t *type2)
238 if(type1->kind != type2->kind)
240 if(type1->base.qualifiers != type2->base.qualifiers)
243 switch(type1->kind) {
245 /* Hmm, the error type is never equal */
250 return atomic_types_equal(&type1->atomic, &type2->atomic);
252 return enum_types_equal(&type1->enumt, &type2->enumt);
253 case TYPE_COMPOUND_STRUCT:
254 case TYPE_COMPOUND_UNION:
255 return compound_types_equal(&type1->compound, &type2->compound);
257 return function_types_equal(&type1->function, &type2->function);
259 return pointer_types_equal(&type1->pointer, &type2->pointer);
261 return array_types_equal(&type1->array, &type2->array);
263 return builtin_types_equal(&type1->builtin, &type2->builtin);
265 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
267 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
269 return bitfield_types_equal(&type1->bitfield, &type2->bitfield);
275 #define HashSet type_hash_t
276 #define HashSetIterator type_hash_iterator_t
277 #define ValueType type_t*
278 #define NullValue NULL
279 #define DeletedValue ((type_t*)-1)
280 #define Hash(this, key) hash_type(key)
281 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
282 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
284 #define hashset_init _typehash_init
285 #define hashset_init_size _typehash_init_size
286 #define hashset_destroy _typehash_destroy
287 #define hashset_insert _typehash_insert
288 #define hashset_remove typehash_remove
289 #define hashset_find typehash_find
290 #define hashset_size typehash_size
291 #define hashset_iterator_init typehash_iterator_init
292 #define hashset_iterator_next typehash_iterator_next
293 #define hashset_remove_iterator typehash_remove_iterator
295 #include "adt/hashset.c"
297 static type_hash_t typehash;
299 void init_typehash(void)
301 _typehash_init(&typehash);
304 void exit_typehash(void)
306 _typehash_destroy(&typehash);
309 type_t *typehash_insert(type_t *type)
311 return _typehash_insert(&typehash, type);