2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
11 #include "adt/error.h"
16 #define HashSet type_hash_t
17 #define ValueType type_t*
18 #include "adt/hashset.h"
22 typedef struct type_hash_iterator_t type_hash_iterator_t;
23 typedef struct type_hash_t type_hash_t;
25 /* TODO: ^= is a bad way of combining hashes since most addresses are very
28 static unsigned hash_ptr(const void *ptr)
30 unsigned ptr_int = ((char*) ptr - (char*) NULL);
34 static unsigned hash_atomic_type(const atomic_type_t *type)
36 unsigned some_prime = 27644437;
37 unsigned result = type->akind * some_prime;
42 static unsigned hash_pointer_type(const pointer_type_t *type)
44 return hash_ptr(type->points_to) ^ hash_ptr(type->base_variable);
47 static unsigned hash_reference_type(const reference_type_t *type)
49 return hash_ptr(type->refers_to);
52 static unsigned hash_array_type(const array_type_t *type)
54 return hash_ptr(type->element_type);
57 static unsigned hash_compound_type(const compound_type_t *type)
59 return hash_ptr(type->compound);
62 static unsigned hash_function_type(const function_type_t *type)
64 unsigned result = hash_ptr(type->return_type);
66 function_parameter_t *parameter = type->parameters;
67 while (parameter != NULL) {
68 result ^= hash_ptr(parameter->type);
69 parameter = parameter->next;
71 result += type->modifiers;
72 result += type->linkage;
73 result += type->calling_convention;
78 static unsigned hash_enum_type(const enum_type_t *type)
80 return hash_ptr(type->enume);
83 static unsigned hash_typeof_type(const typeof_type_t *type)
85 unsigned result = hash_ptr(type->expression);
86 result ^= hash_ptr(type->typeof_type);
91 static unsigned hash_type(const type_t *type)
101 hash = hash_atomic_type(&type->atomic);
104 hash = hash_enum_type(&type->enumt);
106 case TYPE_COMPOUND_STRUCT:
107 case TYPE_COMPOUND_UNION:
108 hash = hash_compound_type(&type->compound);
111 hash = hash_function_type(&type->function);
114 hash = hash_pointer_type(&type->pointer);
117 hash = hash_reference_type(&type->reference);
120 hash = hash_array_type(&type->array);
123 hash = hash_ptr(type->typedeft.typedefe);
126 hash = hash_typeof_type(&type->typeoft);
130 unsigned some_prime = 99991;
131 hash ^= some_prime * type->base.qualifiers;
136 static bool atomic_types_equal(const atomic_type_t *type1,
137 const atomic_type_t *type2)
139 return type1->akind == type2->akind;
142 static bool function_types_equal(const function_type_t *type1,
143 const function_type_t *type2)
145 if (type1->return_type != type2->return_type)
147 if (type1->variadic != type2->variadic)
149 if (type1->unspecified_parameters != type2->unspecified_parameters)
151 if (type1->kr_style_parameters != type2->kr_style_parameters)
153 if (type1->linkage != type2->linkage)
155 if (type1->modifiers != type2->modifiers)
157 if (type1->calling_convention != type2->calling_convention)
160 function_parameter_t *param1 = type1->parameters;
161 function_parameter_t *param2 = type2->parameters;
162 while (param1 != NULL && param2 != NULL) {
163 if (param1->type != param2->type)
165 param1 = param1->next;
166 param2 = param2->next;
168 if (param1 != NULL || param2 != NULL)
174 static bool pointer_types_equal(const pointer_type_t *type1,
175 const pointer_type_t *type2)
177 return type1->points_to == type2->points_to &&
178 type1->base_variable == type2->base_variable;
181 static bool reference_types_equal(const reference_type_t *type1,
182 const reference_type_t *type2)
184 return type1->refers_to == type2->refers_to;
187 static bool array_types_equal(const array_type_t *type1,
188 const array_type_t *type2)
190 if (type1->element_type != type2->element_type)
192 if (type1->is_variable != type2->is_variable)
194 if (type1->is_static != type2->is_static)
196 if (type1->size_constant != type2->size_constant)
199 /* never identify vla types, because we need them for caching calculated
200 * sizes later in ast2firm */
201 if (type1->is_vla || type2->is_vla)
204 /* TODO: compare size expressions for equality... */
209 static bool compound_types_equal(const compound_type_t *type1,
210 const compound_type_t *type2)
212 return type1->compound == type2->compound;
215 static bool enum_types_equal(const enum_type_t *type1,
216 const enum_type_t *type2)
218 return type1->enume == type2->enume;
221 static bool typedef_types_equal(const typedef_type_t *type1,
222 const typedef_type_t *type2)
224 return type1->typedefe == type2->typedefe;
227 static bool typeof_types_equal(const typeof_type_t *type1,
228 const typeof_type_t *type2)
230 if (type1->expression != type2->expression)
232 if (type1->typeof_type != type2->typeof_type)
238 static bool types_equal(const type_t *type1, const type_t *type2)
242 if (type1->kind != type2->kind)
244 if (type1->base.qualifiers != type2->base.qualifiers)
247 switch (type1->kind) {
249 /* Hmm, the error type is never equal */
254 return atomic_types_equal(&type1->atomic, &type2->atomic);
256 return enum_types_equal(&type1->enumt, &type2->enumt);
257 case TYPE_COMPOUND_STRUCT:
258 case TYPE_COMPOUND_UNION:
259 return compound_types_equal(&type1->compound, &type2->compound);
261 return function_types_equal(&type1->function, &type2->function);
263 return pointer_types_equal(&type1->pointer, &type2->pointer);
265 return reference_types_equal(&type1->reference, &type2->reference);
267 return array_types_equal(&type1->array, &type2->array);
269 return typeof_types_equal(&type1->typeoft, &type2->typeoft);
271 return typedef_types_equal(&type1->typedeft, &type2->typedeft);
277 #define HashSet type_hash_t
278 #define ValueType type_t*
279 #define NullValue NULL
280 #define DeletedValue ((type_t*)-1)
281 #define Hash(this, key) hash_type(key)
282 #define KeysEqual(this,key1,key2) types_equal(key1, key2)
283 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof(*(ptr)))
285 void _typehash_init(type_hash_t *hash);
286 #define hashset_init _typehash_init
287 void _typehash_destroy(type_hash_t *hash);
288 #define hashset_destroy _typehash_destroy
289 type_t *_typehash_insert(type_hash_t *hash, type_t *type);
290 #define hashset_insert _typehash_insert
291 #define SCALAR_RETURN
293 #include "adt/hashset.c.inl"
295 static type_hash_t typehash;
297 void init_typehash(void)
299 _typehash_init(&typehash);
302 void exit_typehash(void)
304 _typehash_destroy(&typehash);
307 type_t *typehash_insert(type_t *type)
309 return _typehash_insert(&typehash, type);