filter trigraphs in advance and simplify lexer code because of that
[cparser] / type_hash.c
1 #include <config.h>
2
3 #include "type_hash.h"
4
5 #include "adt/error.h"
6 #include "type_t.h"
7
8 #include <assert.h>
9
10 #define HashSet         type_hash_t
11 #define HashSetIterator type_hash_iterator_t
12 #define ValueType       type_t*
13 #include "adt/hashset.h"
14 #undef ValueType
15 #undef HashSetIterator
16 #undef HashSet
17
18 /* TODO: ^= is a bad way of combining hashes since most addresses are very
19  * similar */
20
21 static unsigned hash_ptr(const void *ptr)
22 {
23         unsigned ptr_int = ((char*) ptr - (char*) NULL);
24         return ptr_int >> 3;
25 }
26
27 static unsigned hash_atomic_type(const atomic_type_t *type)
28 {
29         unsigned some_prime = 27644437;
30         unsigned result     = type->atype * some_prime;
31
32         return result;
33 }
34
35 static unsigned hash_pointer_type(const pointer_type_t *type)
36 {
37         return hash_ptr(type->points_to);
38 }
39
40 static unsigned hash_compound_type(const compound_type_t *type)
41 {
42         unsigned result = hash_ptr(type->symbol);
43
44         return result;
45 }
46
47 static unsigned hash_type(const type_t *type);
48
49 static unsigned hash_method_type(const method_type_t *type)
50 {
51         unsigned result = hash_ptr(type->result_type);
52
53         method_parameter_t *parameter = type->parameters;
54         while(parameter != NULL) {
55                 result   ^= hash_ptr(parameter->type);
56                 parameter = parameter->next;
57         }
58
59         return result;
60 }
61
62 static unsigned hash_enum_type(const enum_type_t *type)
63 {
64         unsigned result = hash_ptr(type->symbol);
65
66         return result;
67 }
68
69 static unsigned hash_type(const type_t *type)
70 {
71         unsigned hash;
72
73         switch(type->type) {
74         case TYPE_INVALID:
75                 panic("internalizing void or invalid types not possible");
76                 return 0;
77         case TYPE_ATOMIC:
78                 hash = hash_atomic_type((const atomic_type_t*) type);
79                 break;
80         case TYPE_ENUM:
81                 hash = hash_enum_type((const enum_type_t*) type);
82                 break;
83         case TYPE_COMPOUND_STRUCT:
84         case TYPE_COMPOUND_UNION:
85                 hash = hash_compound_type((const compound_type_t*) type);
86                 break;
87         case TYPE_METHOD:
88                 hash = hash_method_type((const method_type_t*) type);
89                 break;
90         case TYPE_POINTER:
91                 hash = hash_pointer_type((const pointer_type_t*) type);
92                 break;
93         case TYPE_BUILTIN:
94                 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
95                 break;
96         }
97
98         unsigned some_prime = 99991;
99         hash ^= some_prime * type->qualifiers;
100
101         return hash;
102 }
103
104 static int atomic_types_equal(const atomic_type_t *type1,
105                               const atomic_type_t *type2)
106 {
107         return type1->atype == type2->atype;
108 }
109
110 static int compound_types_equal(const compound_type_t *type1,
111                                 const compound_type_t *type2)
112 {
113         if(type1->type.type != type2->type.type)
114                 return 0;
115         if(type1->symbol != type2->symbol)
116                 return 0;
117
118         /* anonymous types? */
119         if(type1->symbol == NULL) {
120                 /* previous tests should already have checked for this */
121                 assert(type1 != type2);
122                 /* anonymous types are only equal if they are the very same type */
123                 return 0;
124         }
125
126         /* non-anonymous types with same symbol are equal */
127         return 1;
128 }
129
130 static int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
131 {
132         if(type1->symbol != type2->symbol)
133                 return 0;
134
135         /* anonymous types? */
136         if(type1->symbol == NULL) {
137                 /* previous tests should already have checked for this */
138                 assert(type1 != type2);
139                 /* 2 anonymous enums are never equal */
140                 return 0;
141         }
142
143         /* non-anonymous types with same symbol are equal */
144         return 1;
145 }
146
147 static int method_types_equal(const method_type_t *type1,
148                               const method_type_t *type2)
149 {
150         if(type1->result_type != type2->result_type)
151                 return 0;
152         if(type1->variadic != type2->variadic)
153                 return 0;
154         if(type1->unspecified_parameters != type2->unspecified_parameters)
155                 return 0;
156
157         method_parameter_t *param1 = type1->parameters;
158         method_parameter_t *param2 = type2->parameters;
159         while(param1 != NULL && param2 != NULL) {
160                 if(param1->type != param2->type)
161                         return 0;
162                 param1 = param1->next;
163                 param2 = param2->next;
164         }
165         if(param1 != NULL || param2 != NULL)
166                 return 0;
167
168         return 1;
169 }
170
171 static int pointer_types_equal(const pointer_type_t *type1,
172                                const pointer_type_t *type2)
173 {
174         return type1->points_to == type2->points_to;
175 }
176
177 static int builtin_types_equal(const builtin_type_t *type1,
178                                const builtin_type_t *type2)
179 {
180         return type1->symbol == type2->symbol;
181 }
182
183 static int types_equal(const type_t *type1, const type_t *type2)
184 {
185         if(type1 == type2)
186                 return 1;
187         if(type1->type != type2->type)
188                 return 0;
189         if(type1->qualifiers != type2->qualifiers)
190                 return 0;
191
192         switch(type1->type) {
193         case TYPE_INVALID:
194                 return 0;
195         case TYPE_ATOMIC:
196                 return atomic_types_equal((const atomic_type_t*) type1,
197                                           (const atomic_type_t*) type2);
198         case TYPE_COMPOUND_STRUCT:
199         case TYPE_COMPOUND_UNION:
200                 return compound_types_equal((const compound_type_t*) type1,
201                                             (const compound_type_t*) type2);
202         case TYPE_ENUM:
203                 return enum_types_equal((const enum_type_t*) type1,
204                                         (const enum_type_t*) type2);
205         case TYPE_METHOD:
206                 return method_types_equal((const method_type_t*) type1,
207                                           (const method_type_t*) type2);
208         case TYPE_POINTER:
209                 return pointer_types_equal((const pointer_type_t*) type1,
210                                            (const pointer_type_t*) type2);
211         case TYPE_BUILTIN:
212                 return builtin_types_equal((const builtin_type_t*) type1,
213                                            (const builtin_type_t*) type2);
214         }
215
216         abort();
217 }
218
219 #define HashSet                    type_hash_t
220 #define HashSetIterator            type_hash_iterator_t
221 #define ValueType                  type_t*
222 #define NullValue                  NULL
223 #define DeletedValue               ((type_t*)-1)
224 #define Hash(this, key)            hash_type(key)
225 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
226 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
227
228 #define hashset_init             _typehash_init
229 #define hashset_init_size        _typehash_init_size
230 #define hashset_destroy          _typehash_destroy
231 #define hashset_insert           _typehash_insert
232 #define hashset_remove           typehash_remove
233 #define hashset_find             typehash_find
234 #define hashset_size             typehash_size
235 #define hashset_iterator_init    typehash_iterator_init
236 #define hashset_iterator_next    typehash_iterator_next
237 #define hashset_remove_iterator  typehash_remove_iterator
238
239 #include "adt/hashset.c"
240
241 static type_hash_t typehash;
242
243 void init_typehash(void)
244 {
245         _typehash_init(&typehash);
246 }
247
248 void exit_typehash(void)
249 {
250         _typehash_destroy(&typehash);
251 }
252
253 type_t *typehash_insert(type_t *type)
254 {
255         return _typehash_insert(&typehash, type);
256 }