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