improved support for enums
[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 static
19 unsigned hash_ptr(const void *ptr)
20 {
21         unsigned ptr_int = ((char*) ptr - (char*) NULL);
22         return ptr_int >> 3;
23 }
24
25 static
26 unsigned hash_atomic_type(const atomic_type_t *type)
27 {
28         unsigned some_prime = 27644437;
29
30         return type->atype * some_prime;
31 }
32
33 static
34 unsigned hash_pointer_type(const pointer_type_t *type)
35 {
36         return hash_ptr(type->points_to);
37 }
38
39 static
40 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
48 unsigned hash_type(const type_t *type);
49
50 static
51 unsigned hash_method_type(const method_type_t *type)
52 {
53         unsigned result = hash_ptr(type->result_type);
54
55         method_parameter_type_t *parameter = type->parameter_types;
56         while(parameter != NULL) {
57                 result ^= hash_ptr(parameter->type);
58                 parameter = parameter->next;
59         }
60
61         return result;
62 }
63
64 static
65 unsigned hash_enum_type(const enum_type_t *type)
66 {
67         unsigned result = hash_ptr(type->symbol);
68
69         /* TODO */
70
71         return result;
72 }
73
74 static
75 unsigned hash_type(const type_t *type)
76 {
77         unsigned hash;
78
79         switch(type->type) {
80         case TYPE_INVALID:
81                 panic("internalizing void or invalid types not possible");
82                 return 0;
83         case TYPE_ATOMIC:
84                 hash = hash_atomic_type((const atomic_type_t*) type);
85                 break;
86         case TYPE_ENUM:
87                 hash = hash_enum_type((const enum_type_t*) type);
88                 break;
89         case TYPE_COMPOUND_STRUCT:
90         case TYPE_COMPOUND_UNION:
91                 hash = hash_compound_type((const compound_type_t*) type);
92                 break;
93         case TYPE_METHOD:
94                 hash = hash_method_type((const method_type_t*) type);
95                 break;
96         case TYPE_POINTER:
97                 hash = hash_pointer_type((const pointer_type_t*) type);
98                 break;
99         case TYPE_BUILTIN:
100                 hash = hash_ptr(((const builtin_type_t*) type)->symbol);
101                 break;
102         }
103
104         unsigned some_prime = 99991;
105         hash ^= some_prime * type->qualifiers;
106
107         return hash;
108 }
109
110 static
111 int atomic_types_equal(const atomic_type_t *type1, const atomic_type_t *type2)
112 {
113         return type1->atype == type2->atype;
114 }
115
116 static
117 int compound_types_equal(const compound_type_t *type1,
118                          const compound_type_t *type2)
119 {
120         if(type1->type.type != type2->type.type)
121                 return 0;
122         if(type1->symbol != type2->symbol)
123                 return 0;
124
125         declaration_t *entry1 = type1->context.declarations;
126         declaration_t *entry2 = type2->context.declarations;
127
128         while(entry1 != NULL && entry2 != NULL) {
129                 if(entry1->type != entry2->type)
130                         return 0;
131                 if(entry1->symbol != entry2->symbol)
132                         return 0;
133                 entry1 = entry1->next;
134                 entry2 = entry2->next;
135         }
136         if(entry1 != NULL || entry2 != NULL)
137                 return 0;
138
139         return 1;
140 }
141
142 static
143 int method_types_equal(const method_type_t *type1, const method_type_t *type2)
144 {
145         if(type1->result_type != type2->result_type)
146                 return 0;
147
148         method_parameter_type_t *param1 = type1->parameter_types;
149         method_parameter_type_t *param2 = type2->parameter_types;
150         while(param1 != NULL && param2 != NULL) {
151                 if(param1->type != param2->type)
152                         return 0;
153                 param1 = param1->next;
154                 param2 = param2->next;
155         }
156         if(param1 != NULL || param2 != NULL)
157                 return 0;
158
159         return 1;
160 }
161
162 static
163 int pointer_types_equal(const pointer_type_t *type1,
164                         const pointer_type_t *type2)
165 {
166         return type1->points_to == type2->points_to;
167 }
168
169 static
170 int enum_types_equal(const enum_type_t *type1, const enum_type_t *type2)
171 {
172         if(type1->symbol != NULL && type1->symbol == type2->symbol)
173                 return 1;
174
175         enum_entry_t *entry1 = type1->entries;
176         enum_entry_t *entry2 = type2->entries;
177         while(entry1 != NULL && entry2 != NULL) {
178                 if(entry1->symbol != entry2->symbol)
179                         return 0;
180                 /* TODO: compare expressions */
181                 entry1 = entry1->next;
182                 entry2 = entry2->next;
183         }
184         if(entry1 != NULL || entry2 != NULL)
185                 return 0;
186
187         return 1;
188 }
189
190 static
191 int builtin_types_equal(const builtin_type_t *type1,
192                         const builtin_type_t *type2)
193 {
194         return type1->symbol == type2->symbol;
195 }
196
197 static
198 int types_equal(const type_t *type1, const type_t *type2)
199 {
200         if(type1 == type2)
201                 return 1;
202         if(type1->type != type2->type)
203                 return 0;
204         if(type1->qualifiers != type2->qualifiers)
205                 return 0;
206
207         switch(type1->type) {
208         case TYPE_INVALID:
209                 return 0;
210         case TYPE_ATOMIC:
211                 return atomic_types_equal((const atomic_type_t*) type1,
212                                           (const atomic_type_t*) type2);
213         case TYPE_COMPOUND_STRUCT:
214         case TYPE_COMPOUND_UNION:
215                 return compound_types_equal((const compound_type_t*) type1,
216                                             (const compound_type_t*) type2);
217         case TYPE_ENUM:
218                 return enum_types_equal((const enum_type_t*) type1,
219                                         (const enum_type_t*) type2);
220         case TYPE_METHOD:
221                 return method_types_equal((const method_type_t*) type1,
222                                           (const method_type_t*) type2);
223         case TYPE_POINTER:
224                 return pointer_types_equal((const pointer_type_t*) type1,
225                                            (const pointer_type_t*) type2);
226         case TYPE_BUILTIN:
227                 return builtin_types_equal((const builtin_type_t*) type1,
228                                            (const builtin_type_t*) type2);
229         }
230
231         abort();
232 }
233
234 #define HashSet                    type_hash_t
235 #define HashSetIterator            type_hash_iterator_t
236 #define ValueType                  type_t*
237 #define NullValue                  NULL
238 #define DeletedValue               ((type_t*)-1)
239 #define Hash(this, key)            hash_type(key)
240 #define KeysEqual(this,key1,key2)  types_equal(key1, key2)
241 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(*(ptr)))
242
243 #define hashset_init             _typehash_init
244 #define hashset_init_size        _typehash_init_size
245 #define hashset_destroy          _typehash_destroy
246 #define hashset_insert           _typehash_insert
247 #define hashset_remove           typehash_remove
248 #define hashset_find             typehash_find
249 #define hashset_size             typehash_size
250 #define hashset_iterator_init    typehash_iterator_init
251 #define hashset_iterator_next    typehash_iterator_next
252 #define hashset_remove_iterator  typehash_remove_iterator
253
254 #include "adt/hashset.c"
255
256 static type_hash_t typehash;
257
258 void init_typehash(void)
259 {
260         _typehash_init(&typehash);
261 }
262
263 void exit_typehash(void)
264 {
265         _typehash_destroy(&typehash);
266 }
267
268 type_t *typehash_insert(type_t *type)
269 {
270         return _typehash_insert(&typehash, type);
271 }