annotate gcc/hash-traits.h @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Traits for hashable types.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #ifndef hash_traits_h
kono
parents:
diff changeset
21 #define hash_traits_h
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 /* Helpful type for removing with free. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 template <typename Type>
kono
parents:
diff changeset
26 struct typed_free_remove
kono
parents:
diff changeset
27 {
kono
parents:
diff changeset
28 static inline void remove (Type *p);
kono
parents:
diff changeset
29 };
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 /* Remove with free. */
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 template <typename Type>
kono
parents:
diff changeset
35 inline void
kono
parents:
diff changeset
36 typed_free_remove <Type>::remove (Type *p)
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 free (p);
kono
parents:
diff changeset
39 }
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 /* Helpful type for removing with delete. */
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 template <typename Type>
kono
parents:
diff changeset
44 struct typed_delete_remove
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 static inline void remove (Type *p);
kono
parents:
diff changeset
47 };
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 /* Remove with delete. */
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 template <typename Type>
kono
parents:
diff changeset
53 inline void
kono
parents:
diff changeset
54 typed_delete_remove <Type>::remove (Type *p)
kono
parents:
diff changeset
55 {
kono
parents:
diff changeset
56 delete p;
kono
parents:
diff changeset
57 }
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Helpful type for a no-op remove. */
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 template <typename Type>
kono
parents:
diff changeset
62 struct typed_noop_remove
kono
parents:
diff changeset
63 {
kono
parents:
diff changeset
64 static inline void remove (Type &);
kono
parents:
diff changeset
65 };
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 /* Remove doing nothing. */
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 template <typename Type>
kono
parents:
diff changeset
71 inline void
kono
parents:
diff changeset
72 typed_noop_remove <Type>::remove (Type &)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 }
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* Hasher for integer type Type in which Empty is a spare value that can be
kono
parents:
diff changeset
78 used to mark empty slots. If Deleted != Empty then Deleted is another
kono
parents:
diff changeset
79 spare value that can be used for deleted slots; if Deleted == Empty then
kono
parents:
diff changeset
80 hash table entries cannot be deleted. */
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 template <typename Type, Type Empty, Type Deleted = Empty>
kono
parents:
diff changeset
83 struct int_hash : typed_noop_remove <Type>
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 typedef Type value_type;
kono
parents:
diff changeset
86 typedef Type compare_type;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 static inline hashval_t hash (value_type);
kono
parents:
diff changeset
89 static inline bool equal (value_type existing, value_type candidate);
kono
parents:
diff changeset
90 static inline void mark_deleted (Type &);
kono
parents:
diff changeset
91 static inline void mark_empty (Type &);
kono
parents:
diff changeset
92 static inline bool is_deleted (Type);
kono
parents:
diff changeset
93 static inline bool is_empty (Type);
kono
parents:
diff changeset
94 };
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
97 inline hashval_t
kono
parents:
diff changeset
98 int_hash <Type, Empty, Deleted>::hash (value_type x)
kono
parents:
diff changeset
99 {
kono
parents:
diff changeset
100 return x;
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
104 inline bool
kono
parents:
diff changeset
105 int_hash <Type, Empty, Deleted>::equal (value_type x, value_type y)
kono
parents:
diff changeset
106 {
kono
parents:
diff changeset
107 return x == y;
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
111 inline void
kono
parents:
diff changeset
112 int_hash <Type, Empty, Deleted>::mark_deleted (Type &x)
kono
parents:
diff changeset
113 {
kono
parents:
diff changeset
114 gcc_assert (Empty != Deleted);
kono
parents:
diff changeset
115 x = Deleted;
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
119 inline void
kono
parents:
diff changeset
120 int_hash <Type, Empty, Deleted>::mark_empty (Type &x)
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 x = Empty;
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
126 inline bool
kono
parents:
diff changeset
127 int_hash <Type, Empty, Deleted>::is_deleted (Type x)
kono
parents:
diff changeset
128 {
kono
parents:
diff changeset
129 return Empty != Deleted && x == Deleted;
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
133 inline bool
kono
parents:
diff changeset
134 int_hash <Type, Empty, Deleted>::is_empty (Type x)
kono
parents:
diff changeset
135 {
kono
parents:
diff changeset
136 return x == Empty;
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 /* Pointer hasher based on pointer equality. Other types of pointer hash
kono
parents:
diff changeset
140 can inherit this and override the hash and equal functions with some
kono
parents:
diff changeset
141 other form of equality (such as string equality). */
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 template <typename Type>
kono
parents:
diff changeset
144 struct pointer_hash
kono
parents:
diff changeset
145 {
kono
parents:
diff changeset
146 typedef Type *value_type;
kono
parents:
diff changeset
147 typedef Type *compare_type;
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 static inline hashval_t hash (const value_type &);
kono
parents:
diff changeset
150 static inline bool equal (const value_type &existing,
kono
parents:
diff changeset
151 const compare_type &candidate);
kono
parents:
diff changeset
152 static inline void mark_deleted (Type *&);
kono
parents:
diff changeset
153 static inline void mark_empty (Type *&);
kono
parents:
diff changeset
154 static inline bool is_deleted (Type *);
kono
parents:
diff changeset
155 static inline bool is_empty (Type *);
kono
parents:
diff changeset
156 };
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 template <typename Type>
kono
parents:
diff changeset
159 inline hashval_t
kono
parents:
diff changeset
160 pointer_hash <Type>::hash (const value_type &candidate)
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 /* This is a really poor hash function, but it is what the current code uses,
kono
parents:
diff changeset
163 so I am reusing it to avoid an additional axis in testing. */
kono
parents:
diff changeset
164 return (hashval_t) ((intptr_t)candidate >> 3);
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 template <typename Type>
kono
parents:
diff changeset
168 inline bool
kono
parents:
diff changeset
169 pointer_hash <Type>::equal (const value_type &existing,
kono
parents:
diff changeset
170 const compare_type &candidate)
kono
parents:
diff changeset
171 {
kono
parents:
diff changeset
172 return existing == candidate;
kono
parents:
diff changeset
173 }
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 template <typename Type>
kono
parents:
diff changeset
176 inline void
kono
parents:
diff changeset
177 pointer_hash <Type>::mark_deleted (Type *&e)
kono
parents:
diff changeset
178 {
kono
parents:
diff changeset
179 e = reinterpret_cast<Type *> (1);
kono
parents:
diff changeset
180 }
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 template <typename Type>
kono
parents:
diff changeset
183 inline void
kono
parents:
diff changeset
184 pointer_hash <Type>::mark_empty (Type *&e)
kono
parents:
diff changeset
185 {
kono
parents:
diff changeset
186 e = NULL;
kono
parents:
diff changeset
187 }
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 template <typename Type>
kono
parents:
diff changeset
190 inline bool
kono
parents:
diff changeset
191 pointer_hash <Type>::is_deleted (Type *e)
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 return e == reinterpret_cast<Type *> (1);
kono
parents:
diff changeset
194 }
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 template <typename Type>
kono
parents:
diff changeset
197 inline bool
kono
parents:
diff changeset
198 pointer_hash <Type>::is_empty (Type *e)
kono
parents:
diff changeset
199 {
kono
parents:
diff changeset
200 return e == NULL;
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 /* Hasher for "const char *" strings, using string rather than pointer
kono
parents:
diff changeset
204 equality. */
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 struct string_hash : pointer_hash <const char>
kono
parents:
diff changeset
207 {
kono
parents:
diff changeset
208 static inline hashval_t hash (const char *);
kono
parents:
diff changeset
209 static inline bool equal (const char *, const char *);
kono
parents:
diff changeset
210 };
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 inline hashval_t
kono
parents:
diff changeset
213 string_hash::hash (const char *id)
kono
parents:
diff changeset
214 {
kono
parents:
diff changeset
215 return htab_hash_string (id);
kono
parents:
diff changeset
216 }
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 inline bool
kono
parents:
diff changeset
219 string_hash::equal (const char *id1, const char *id2)
kono
parents:
diff changeset
220 {
kono
parents:
diff changeset
221 return strcmp (id1, id2) == 0;
kono
parents:
diff changeset
222 }
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 /* Remover and marker for entries in gc memory. */
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 template<typename T>
kono
parents:
diff changeset
227 struct ggc_remove
kono
parents:
diff changeset
228 {
kono
parents:
diff changeset
229 static void remove (T &) {}
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 static void
kono
parents:
diff changeset
232 ggc_mx (T &p)
kono
parents:
diff changeset
233 {
kono
parents:
diff changeset
234 extern void gt_ggc_mx (T &);
kono
parents:
diff changeset
235 gt_ggc_mx (p);
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
238 /* Overridden in ggc_cache_remove. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
239 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
240 ggc_maybe_mx (T &p)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
241 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
242 ggc_mx (p);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
243 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
244
111
kono
parents:
diff changeset
245 static void
kono
parents:
diff changeset
246 pch_nx (T &p)
kono
parents:
diff changeset
247 {
kono
parents:
diff changeset
248 extern void gt_pch_nx (T &);
kono
parents:
diff changeset
249 gt_pch_nx (p);
kono
parents:
diff changeset
250 }
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 static void
kono
parents:
diff changeset
253 pch_nx (T &p, gt_pointer_operator op, void *cookie)
kono
parents:
diff changeset
254 {
kono
parents:
diff changeset
255 op (&p, cookie);
kono
parents:
diff changeset
256 }
kono
parents:
diff changeset
257 };
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 /* Remover and marker for "cache" entries in gc memory. These entries can
kono
parents:
diff changeset
260 be deleted if there are no non-cache references to the data. */
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 template<typename T>
kono
parents:
diff changeset
263 struct ggc_cache_remove : ggc_remove<T>
kono
parents:
diff changeset
264 {
kono
parents:
diff changeset
265 /* Entries are weakly held because this is for caches. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
266 static void ggc_maybe_mx (T &) {}
111
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 static int
kono
parents:
diff changeset
269 keep_cache_entry (T &e)
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 return ggc_marked_p (e) ? -1 : 0;
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273 };
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 /* Traits for pointer elements that should not be freed when an element
kono
parents:
diff changeset
276 is deleted. */
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 template <typename T>
kono
parents:
diff changeset
279 struct nofree_ptr_hash : pointer_hash <T>, typed_noop_remove <T *> {};
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 /* Traits for pointer elements that should be freed via free() when an
kono
parents:
diff changeset
282 element is deleted. */
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 template <typename T>
kono
parents:
diff changeset
285 struct free_ptr_hash : pointer_hash <T>, typed_free_remove <T> {};
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 /* Traits for pointer elements that should be freed via delete operand when an
kono
parents:
diff changeset
288 element is deleted. */
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 template <typename T>
kono
parents:
diff changeset
291 struct delete_ptr_hash : pointer_hash <T>, typed_delete_remove <T> {};
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 /* Traits for elements that point to gc memory. The pointed-to data
kono
parents:
diff changeset
294 must be kept across collections. */
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 template <typename T>
kono
parents:
diff changeset
297 struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 /* Traits for elements that point to gc memory. The elements don't
kono
parents:
diff changeset
300 in themselves keep the pointed-to data alive and they can be deleted
kono
parents:
diff changeset
301 if the pointed-to data is going to be collected. */
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 template <typename T>
kono
parents:
diff changeset
304 struct ggc_cache_ptr_hash : pointer_hash <T>, ggc_cache_remove <T *> {};
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 /* Traits for string elements that should not be freed when an element
kono
parents:
diff changeset
307 is deleted. */
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 struct nofree_string_hash : string_hash, typed_noop_remove <const char *> {};
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 /* Traits for pairs of values, using the first to record empty and
kono
parents:
diff changeset
312 deleted slots. */
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 template <typename T1, typename T2>
kono
parents:
diff changeset
315 struct pair_hash
kono
parents:
diff changeset
316 {
kono
parents:
diff changeset
317 typedef std::pair <typename T1::value_type,
kono
parents:
diff changeset
318 typename T2::value_type> value_type;
kono
parents:
diff changeset
319 typedef std::pair <typename T1::compare_type,
kono
parents:
diff changeset
320 typename T2::compare_type> compare_type;
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 static inline hashval_t hash (const value_type &);
kono
parents:
diff changeset
323 static inline bool equal (const value_type &, const compare_type &);
kono
parents:
diff changeset
324 static inline void remove (value_type &);
kono
parents:
diff changeset
325 static inline void mark_deleted (value_type &);
kono
parents:
diff changeset
326 static inline void mark_empty (value_type &);
kono
parents:
diff changeset
327 static inline bool is_deleted (const value_type &);
kono
parents:
diff changeset
328 static inline bool is_empty (const value_type &);
kono
parents:
diff changeset
329 };
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 template <typename T1, typename T2>
kono
parents:
diff changeset
332 inline hashval_t
kono
parents:
diff changeset
333 pair_hash <T1, T2>::hash (const value_type &x)
kono
parents:
diff changeset
334 {
kono
parents:
diff changeset
335 return iterative_hash_hashval_t (T1::hash (x.first), T2::hash (x.second));
kono
parents:
diff changeset
336 }
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 template <typename T1, typename T2>
kono
parents:
diff changeset
339 inline bool
kono
parents:
diff changeset
340 pair_hash <T1, T2>::equal (const value_type &x, const compare_type &y)
kono
parents:
diff changeset
341 {
kono
parents:
diff changeset
342 return T1::equal (x.first, y.first) && T2::equal (x.second, y.second);
kono
parents:
diff changeset
343 }
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 template <typename T1, typename T2>
kono
parents:
diff changeset
346 inline void
kono
parents:
diff changeset
347 pair_hash <T1, T2>::remove (value_type &x)
kono
parents:
diff changeset
348 {
kono
parents:
diff changeset
349 T1::remove (x.first);
kono
parents:
diff changeset
350 T2::remove (x.second);
kono
parents:
diff changeset
351 }
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 template <typename T1, typename T2>
kono
parents:
diff changeset
354 inline void
kono
parents:
diff changeset
355 pair_hash <T1, T2>::mark_deleted (value_type &x)
kono
parents:
diff changeset
356 {
kono
parents:
diff changeset
357 T1::mark_deleted (x.first);
kono
parents:
diff changeset
358 }
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 template <typename T1, typename T2>
kono
parents:
diff changeset
361 inline void
kono
parents:
diff changeset
362 pair_hash <T1, T2>::mark_empty (value_type &x)
kono
parents:
diff changeset
363 {
kono
parents:
diff changeset
364 T1::mark_empty (x.first);
kono
parents:
diff changeset
365 }
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 template <typename T1, typename T2>
kono
parents:
diff changeset
368 inline bool
kono
parents:
diff changeset
369 pair_hash <T1, T2>::is_deleted (const value_type &x)
kono
parents:
diff changeset
370 {
kono
parents:
diff changeset
371 return T1::is_deleted (x.first);
kono
parents:
diff changeset
372 }
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 template <typename T1, typename T2>
kono
parents:
diff changeset
375 inline bool
kono
parents:
diff changeset
376 pair_hash <T1, T2>::is_empty (const value_type &x)
kono
parents:
diff changeset
377 {
kono
parents:
diff changeset
378 return T1::is_empty (x.first);
kono
parents:
diff changeset
379 }
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 template <typename T> struct default_hash_traits : T {};
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 template <typename T>
kono
parents:
diff changeset
384 struct default_hash_traits <T *> : ggc_ptr_hash <T> {};
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 #endif