xref: /netbsd-src/external/gpl3/gcc.old/dist/libcpp/include/symtab.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg /* Hash tables.
2*8feb0f0bSmrg    Copyright (C) 2000-2020 Free Software Foundation, Inc.
336ac495dSmrg 
436ac495dSmrg This program is free software; you can redistribute it and/or modify it
536ac495dSmrg under the terms of the GNU General Public License as published by the
636ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
736ac495dSmrg later version.
836ac495dSmrg 
936ac495dSmrg This program is distributed in the hope that it will be useful,
1036ac495dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1136ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1236ac495dSmrg GNU General Public License for more details.
1336ac495dSmrg 
1436ac495dSmrg You should have received a copy of the GNU General Public License
1536ac495dSmrg along with this program; see the file COPYING3.  If not see
1636ac495dSmrg <http://www.gnu.org/licenses/>.  */
1736ac495dSmrg 
1836ac495dSmrg #ifndef LIBCPP_SYMTAB_H
1936ac495dSmrg #define LIBCPP_SYMTAB_H
2036ac495dSmrg 
2136ac495dSmrg #include "obstack.h"
2236ac495dSmrg 
2336ac495dSmrg #ifndef GTY
2436ac495dSmrg #define GTY(x) /* nothing */
2536ac495dSmrg #endif
2636ac495dSmrg 
2736ac495dSmrg /* This is what each hash table entry points to.  It may be embedded
2836ac495dSmrg    deeply within another object.  */
2936ac495dSmrg typedef struct ht_identifier ht_identifier;
3036ac495dSmrg typedef struct ht_identifier *ht_identifier_ptr;
3136ac495dSmrg struct GTY(()) ht_identifier {
3236ac495dSmrg   const unsigned char *str;
3336ac495dSmrg   unsigned int len;
3436ac495dSmrg   unsigned int hash_value;
3536ac495dSmrg };
3636ac495dSmrg 
3736ac495dSmrg #define HT_LEN(NODE) ((NODE)->len)
3836ac495dSmrg #define HT_STR(NODE) ((NODE)->str)
3936ac495dSmrg 
4036ac495dSmrg typedef struct ht cpp_hash_table;
4136ac495dSmrg typedef struct ht_identifier *hashnode;
4236ac495dSmrg 
4336ac495dSmrg enum ht_lookup_option {HT_NO_INSERT = 0, HT_ALLOC};
4436ac495dSmrg 
4536ac495dSmrg /* An identifier hash table for cpplib and the front ends.  */
4636ac495dSmrg struct ht
4736ac495dSmrg {
4836ac495dSmrg   /* Identifiers are allocated from here.  */
4936ac495dSmrg   struct obstack stack;
5036ac495dSmrg 
5136ac495dSmrg   hashnode *entries;
5236ac495dSmrg   /* Call back, allocate a node.  */
5336ac495dSmrg   hashnode (*alloc_node) (cpp_hash_table *);
5436ac495dSmrg   /* Call back, allocate something that hangs off a node like a cpp_macro.
5536ac495dSmrg      NULL means use the usual allocator.  */
5636ac495dSmrg   void * (*alloc_subobject) (size_t);
5736ac495dSmrg 
5836ac495dSmrg   unsigned int nslots;		/* Total slots in the entries array.  */
5936ac495dSmrg   unsigned int nelements;	/* Number of live elements.  */
6036ac495dSmrg 
6136ac495dSmrg   /* Link to reader, if any.  For the benefit of cpplib.  */
6236ac495dSmrg   struct cpp_reader *pfile;
6336ac495dSmrg 
6436ac495dSmrg   /* Table usage statistics.  */
6536ac495dSmrg   unsigned int searches;
6636ac495dSmrg   unsigned int collisions;
6736ac495dSmrg 
6836ac495dSmrg   /* Should 'entries' be freed when it is no longer needed?  */
6936ac495dSmrg   bool entries_owned;
7036ac495dSmrg };
7136ac495dSmrg 
7236ac495dSmrg /* Initialize the hashtable with 2 ^ order entries.  */
7336ac495dSmrg extern cpp_hash_table *ht_create (unsigned int order);
7436ac495dSmrg 
7536ac495dSmrg /* Frees all memory associated with a hash table.  */
7636ac495dSmrg extern void ht_destroy (cpp_hash_table *);
7736ac495dSmrg 
7836ac495dSmrg extern hashnode ht_lookup (cpp_hash_table *, const unsigned char *,
7936ac495dSmrg 			   size_t, enum ht_lookup_option);
8036ac495dSmrg extern hashnode ht_lookup_with_hash (cpp_hash_table *, const unsigned char *,
8136ac495dSmrg                                      size_t, unsigned int,
8236ac495dSmrg                                      enum ht_lookup_option);
8336ac495dSmrg #define HT_HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
8436ac495dSmrg #define HT_HASHFINISH(r, len) ((r) + (len))
8536ac495dSmrg 
8636ac495dSmrg /* For all nodes in TABLE, make a callback.  The callback takes
8736ac495dSmrg    TABLE->PFILE, the node, and a PTR, and the callback sequence stops
8836ac495dSmrg    if the callback returns zero.  */
8936ac495dSmrg typedef int (*ht_cb) (struct cpp_reader *, hashnode, const void *);
9036ac495dSmrg extern void ht_forall (cpp_hash_table *, ht_cb, const void *);
9136ac495dSmrg 
9236ac495dSmrg /* For all nodes in TABLE, call the callback.  If the callback returns
9336ac495dSmrg    a nonzero value, the node is removed from the table.  */
9436ac495dSmrg extern void ht_purge (cpp_hash_table *, ht_cb, const void *);
9536ac495dSmrg 
9636ac495dSmrg /* Restore the hash table.  */
9736ac495dSmrg extern void ht_load (cpp_hash_table *ht, hashnode *entries,
9836ac495dSmrg 		     unsigned int nslots, unsigned int nelements, bool own);
9936ac495dSmrg 
10036ac495dSmrg /* Dump allocation statistics to stderr.  */
10136ac495dSmrg extern void ht_dump_statistics (cpp_hash_table *);
10236ac495dSmrg 
10336ac495dSmrg #endif /* LIBCPP_SYMTAB_H */
104