libstdc++: Implement C++23 std::bind_back from P2387R3 [PR108827]
[official-gcc.git] / libcpp / identifiers.cc
blob8b91bb2f0886adde8c9b3a8ce9a49146018b301b
1 /* Hash tables for the CPP library.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
30 /* Return an identifier node for hashtable.c. Used by cpplib except
31 when integrated with the C front ends. */
32 template<typename Node>
33 static hashnode
34 alloc_node (cpp_hash_table *table)
36 const auto node = XOBNEW (&table->pfile->hash_ob, Node);
37 memset (node, 0, sizeof (Node));
38 return HT_NODE (node);
41 /* Set up the identifier hash table. Use TABLE if non-null, otherwise
42 create our own. */
43 void
44 _cpp_init_hashtable (cpp_reader *pfile, cpp_hash_table *table,
45 cpp_hash_table *extra_table)
47 struct spec_nodes *s;
49 if (table == NULL)
51 pfile->our_hashtable = 1;
52 table = ht_create (13); /* 8K (=2^13) entries. */
53 table->alloc_node = alloc_node<cpp_hashnode>;
56 if (extra_table == NULL)
58 pfile->our_extra_hashtable = true;
59 extra_table = ht_create (6); /* 64 entries. */
60 extra_table->alloc_node = alloc_node<cpp_hashnode_extra>;
63 if (pfile->our_hashtable || pfile->our_extra_hashtable)
64 obstack_specify_allocation (&pfile->hash_ob, 0, 0, xmalloc, free);
66 table->pfile = pfile;
67 extra_table->pfile = pfile;
68 pfile->hash_table = table;
69 pfile->extra_hash_table = extra_table;
71 /* Now we can initialize things that use the hash table. */
72 _cpp_init_directives (pfile);
73 _cpp_init_internal_pragmas (pfile);
75 s = &pfile->spec_nodes;
76 s->n_defined = cpp_lookup (pfile, DSC("defined"));
77 s->n_true = cpp_lookup (pfile, DSC("true"));
78 s->n_false = cpp_lookup (pfile, DSC("false"));
79 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
80 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
81 s->n__VA_OPT__ = cpp_lookup (pfile, DSC("__VA_OPT__"));
82 s->n__VA_OPT__->flags |= NODE_DIAGNOSTIC;
83 /* __has_include{,_next} are inited in cpp_init_builtins. */
86 /* Tear down the identifier hash table. */
87 void
88 _cpp_destroy_hashtable (cpp_reader *pfile)
90 if (pfile->our_hashtable)
91 ht_destroy (pfile->hash_table);
92 if (pfile->our_extra_hashtable)
93 ht_destroy (pfile->extra_hash_table);
94 if (pfile->our_hashtable || pfile->our_extra_hashtable)
95 obstack_free (&pfile->hash_ob, 0);
98 /* Returns the hash entry for the STR of length LEN, creating one
99 if necessary. */
100 cpp_hashnode *
101 cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len)
103 /* ht_lookup cannot return NULL. */
104 return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
107 /* Determine whether the str STR, of length LEN, is a defined macro. */
109 cpp_defined (cpp_reader *pfile, const unsigned char *str, int len)
111 cpp_hashnode *node;
113 node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
115 /* If it's a macro, it cannot have been poisoned. */
116 return node && cpp_macro_p (node);
119 /* We don't need a proxy since the hash table's identifier comes first
120 in cpp_hashnode. However, in case this is ever changed, we have a
121 static assertion for it. */
122 static_assert (offsetof (cpp_hashnode, ident) == 0,
123 "struct cpp_hashnode must have a struct ht_identifier as"
124 " its first member");
125 static_assert (offsetof (cpp_hashnode_extra, ident) == 0,
126 "struct cpp_hashnode_extra must have a struct ht_identifier as"
127 " its first member");
129 /* For all nodes in the hashtable, callback CB with parameters PFILE,
130 the node, and V. */
131 void
132 cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v)
134 ht_forall (pfile->hash_table, (ht_cb) cb, v);