Daily bump.
[official-gcc.git] / gcc / hash-traits.h
blob57c81bfd34e5fadc2d58d055af289dd3638c065b
1 /* Traits for hashable types.
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef hash_traits_h
21 #define hash_traits_h
23 /* Helpful type for removing with free. */
25 template <typename Type>
26 struct typed_free_remove
28 static inline void remove (Type *p);
32 /* Remove with free. */
34 template <typename Type>
35 inline void
36 typed_free_remove <Type>::remove (Type *p)
38 free (p);
41 /* Helpful type for removing with delete. */
43 template <typename Type>
44 struct typed_delete_remove
46 static inline void remove (Type *p);
50 /* Remove with delete. */
52 template <typename Type>
53 inline void
54 typed_delete_remove <Type>::remove (Type *p)
56 delete p;
59 /* Helpful type for a no-op remove. */
61 template <typename Type>
62 struct typed_noop_remove
64 static inline void remove (Type &);
68 /* Remove doing nothing. */
70 template <typename Type>
71 inline void
72 typed_noop_remove <Type>::remove (Type &)
77 /* Hasher for integer type Type in which Empty is a spare value that can be
78 used to mark empty slots. If Deleted != Empty then Deleted is another
79 spare value that can be used for deleted slots; if Deleted == Empty then
80 hash table entries cannot be deleted. */
82 template <typename Type, Type Empty, Type Deleted = Empty>
83 struct int_hash : typed_noop_remove <Type>
85 typedef Type value_type;
86 typedef Type compare_type;
88 static inline hashval_t hash (value_type);
89 static inline bool equal (value_type existing, value_type candidate);
90 static inline void mark_deleted (Type &);
91 static const bool empty_zero_p = Empty == 0;
92 static inline void mark_empty (Type &);
93 static inline bool is_deleted (Type);
94 static inline bool is_empty (Type);
97 template <typename Type, Type Empty, Type Deleted>
98 inline hashval_t
99 int_hash <Type, Empty, Deleted>::hash (value_type x)
101 return x;
104 template <typename Type, Type Empty, Type Deleted>
105 inline bool
106 int_hash <Type, Empty, Deleted>::equal (value_type x, value_type y)
108 return x == y;
111 template <typename Type, Type Empty, Type Deleted>
112 inline void
113 int_hash <Type, Empty, Deleted>::mark_deleted (Type &x)
115 gcc_assert (Empty != Deleted);
116 x = Deleted;
119 template <typename Type, Type Empty, Type Deleted>
120 inline void
121 int_hash <Type, Empty, Deleted>::mark_empty (Type &x)
123 x = Empty;
126 template <typename Type, Type Empty, Type Deleted>
127 inline bool
128 int_hash <Type, Empty, Deleted>::is_deleted (Type x)
130 return Empty != Deleted && x == Deleted;
133 template <typename Type, Type Empty, Type Deleted>
134 inline bool
135 int_hash <Type, Empty, Deleted>::is_empty (Type x)
137 return x == Empty;
140 /* Pointer hasher based on pointer equality. Other types of pointer hash
141 can inherit this and override the hash and equal functions with some
142 other form of equality (such as string equality). */
144 template <typename Type>
145 struct pointer_hash
147 typedef Type *value_type;
148 typedef Type *compare_type;
150 static inline hashval_t hash (const value_type &);
151 static inline bool equal (const value_type &existing,
152 const compare_type &candidate);
153 static inline void mark_deleted (Type *&);
154 static const bool empty_zero_p = true;
155 static inline void mark_empty (Type *&);
156 static inline bool is_deleted (Type *);
157 static inline bool is_empty (Type *);
160 template <typename Type>
161 inline hashval_t
162 pointer_hash <Type>::hash (const value_type &candidate)
164 /* This is a really poor hash function, but it is what the current code uses,
165 so I am reusing it to avoid an additional axis in testing. */
166 return (hashval_t) ((intptr_t)candidate >> 3);
169 template <typename Type>
170 inline bool
171 pointer_hash <Type>::equal (const value_type &existing,
172 const compare_type &candidate)
174 return existing == candidate;
177 template <typename Type>
178 inline void
179 pointer_hash <Type>::mark_deleted (Type *&e)
181 e = reinterpret_cast<Type *> (1);
184 template <typename Type>
185 inline void
186 pointer_hash <Type>::mark_empty (Type *&e)
188 e = NULL;
191 template <typename Type>
192 inline bool
193 pointer_hash <Type>::is_deleted (Type *e)
195 return e == reinterpret_cast<Type *> (1);
198 template <typename Type>
199 inline bool
200 pointer_hash <Type>::is_empty (Type *e)
202 return e == NULL;
205 /* Hasher for "const char *" strings, using string rather than pointer
206 equality. */
208 struct string_hash : pointer_hash <const char>
210 static inline hashval_t hash (const char *);
211 static inline bool equal (const char *, const char *);
214 inline hashval_t
215 string_hash::hash (const char *id)
217 return htab_hash_string (id);
220 inline bool
221 string_hash::equal (const char *id1, const char *id2)
223 return strcmp (id1, id2) == 0;
226 /* Remover and marker for entries in gc memory. */
228 template<typename T>
229 struct ggc_remove
231 static void remove (T &) {}
233 static void
234 ggc_mx (T &p)
236 extern void gt_ggc_mx (T &);
237 gt_ggc_mx (p);
240 /* Overridden in ggc_cache_remove. */
241 static void
242 ggc_maybe_mx (T &p)
244 ggc_mx (p);
247 static void
248 pch_nx (T &p)
250 extern void gt_pch_nx (T &);
251 gt_pch_nx (p);
254 static void
255 pch_nx (T &p, gt_pointer_operator op, void *cookie)
257 op (&p, cookie);
261 /* Remover and marker for "cache" entries in gc memory. These entries can
262 be deleted if there are no non-cache references to the data. */
264 template<typename T>
265 struct ggc_cache_remove : ggc_remove<T>
267 /* Entries are weakly held because this is for caches. */
268 static void ggc_maybe_mx (T &) {}
270 static int
271 keep_cache_entry (T &e)
273 return ggc_marked_p (e) ? -1 : 0;
277 /* Traits for pointer elements that should not be freed when an element
278 is deleted. */
280 template <typename T>
281 struct nofree_ptr_hash : pointer_hash <T>, typed_noop_remove <T *> {};
283 /* Traits for pointer elements that should be freed via free() when an
284 element is deleted. */
286 template <typename T>
287 struct free_ptr_hash : pointer_hash <T>, typed_free_remove <T> {};
289 /* Traits for pointer elements that should be freed via delete operand when an
290 element is deleted. */
292 template <typename T>
293 struct delete_ptr_hash : pointer_hash <T>, typed_delete_remove <T> {};
295 /* Traits for elements that point to gc memory. The pointed-to data
296 must be kept across collections. */
298 template <typename T>
299 struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
301 /* Traits for elements that point to gc memory. The elements don't
302 in themselves keep the pointed-to data alive and they can be deleted
303 if the pointed-to data is going to be collected. */
305 template <typename T>
306 struct ggc_cache_ptr_hash : pointer_hash <T>, ggc_cache_remove <T *> {};
308 /* Traits for string elements that should not be freed when an element
309 is deleted. */
311 struct nofree_string_hash : string_hash, typed_noop_remove <const char *> {};
313 /* Traits for pairs of values, using the first to record empty and
314 deleted slots. */
316 template <typename T1, typename T2>
317 struct pair_hash
319 typedef std::pair <typename T1::value_type,
320 typename T2::value_type> value_type;
321 typedef std::pair <typename T1::compare_type,
322 typename T2::compare_type> compare_type;
324 static inline hashval_t hash (const value_type &);
325 static inline bool equal (const value_type &, const compare_type &);
326 static inline void remove (value_type &);
327 static inline void mark_deleted (value_type &);
328 static const bool empty_zero_p = T1::empty_zero_p;
329 static inline void mark_empty (value_type &);
330 static inline bool is_deleted (const value_type &);
331 static inline bool is_empty (const value_type &);
334 template <typename T1, typename T2>
335 inline hashval_t
336 pair_hash <T1, T2>::hash (const value_type &x)
338 return iterative_hash_hashval_t (T1::hash (x.first), T2::hash (x.second));
341 template <typename T1, typename T2>
342 inline bool
343 pair_hash <T1, T2>::equal (const value_type &x, const compare_type &y)
345 return T1::equal (x.first, y.first) && T2::equal (x.second, y.second);
348 template <typename T1, typename T2>
349 inline void
350 pair_hash <T1, T2>::remove (value_type &x)
352 T1::remove (x.first);
353 T2::remove (x.second);
356 template <typename T1, typename T2>
357 inline void
358 pair_hash <T1, T2>::mark_deleted (value_type &x)
360 T1::mark_deleted (x.first);
363 template <typename T1, typename T2>
364 inline void
365 pair_hash <T1, T2>::mark_empty (value_type &x)
367 T1::mark_empty (x.first);
370 template <typename T1, typename T2>
371 inline bool
372 pair_hash <T1, T2>::is_deleted (const value_type &x)
374 return T1::is_deleted (x.first);
377 template <typename T1, typename T2>
378 inline bool
379 pair_hash <T1, T2>::is_empty (const value_type &x)
381 return T1::is_empty (x.first);
384 template <typename T> struct default_hash_traits : T {};
386 template <typename T>
387 struct default_hash_traits <T *> : ggc_ptr_hash <T> {};
389 #endif