1*404b540aSrobert /* Hash tables for the CPP library.
2*404b540aSrobert Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3*404b540aSrobert 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4*404b540aSrobert Written by Per Bothner, 1994.
5*404b540aSrobert Based on CCCP program by Paul Rubin, June 1986
6*404b540aSrobert Adapted to ANSI C, Richard Stallman, Jan 1987
7*404b540aSrobert
8*404b540aSrobert This program is free software; you can redistribute it and/or modify it
9*404b540aSrobert under the terms of the GNU General Public License as published by the
10*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
11*404b540aSrobert later version.
12*404b540aSrobert
13*404b540aSrobert This program is distributed in the hope that it will be useful,
14*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*404b540aSrobert GNU General Public License for more details.
17*404b540aSrobert
18*404b540aSrobert You should have received a copy of the GNU General Public License
19*404b540aSrobert along with this program; if not, write to the Free Software
20*404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21*404b540aSrobert
22*404b540aSrobert In other words, you are welcome to use, share and improve this program.
23*404b540aSrobert You are forbidden to forbid anyone else to use, share and improve
24*404b540aSrobert what you give them. Help stamp out software-hoarding! */
25*404b540aSrobert
26*404b540aSrobert #include "config.h"
27*404b540aSrobert #include "system.h"
28*404b540aSrobert #include "cpplib.h"
29*404b540aSrobert #include "internal.h"
30*404b540aSrobert
31*404b540aSrobert static cpp_hashnode *alloc_node (hash_table *);
32*404b540aSrobert
33*404b540aSrobert /* Return an identifier node for hashtable.c. Used by cpplib except
34*404b540aSrobert when integrated with the C front ends. */
35*404b540aSrobert static cpp_hashnode *
alloc_node(hash_table * table)36*404b540aSrobert alloc_node (hash_table *table)
37*404b540aSrobert {
38*404b540aSrobert cpp_hashnode *node;
39*404b540aSrobert
40*404b540aSrobert node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode);
41*404b540aSrobert memset (node, 0, sizeof (cpp_hashnode));
42*404b540aSrobert return node;
43*404b540aSrobert }
44*404b540aSrobert
45*404b540aSrobert /* Set up the identifier hash table. Use TABLE if non-null, otherwise
46*404b540aSrobert create our own. */
47*404b540aSrobert void
_cpp_init_hashtable(cpp_reader * pfile,hash_table * table)48*404b540aSrobert _cpp_init_hashtable (cpp_reader *pfile, hash_table *table)
49*404b540aSrobert {
50*404b540aSrobert struct spec_nodes *s;
51*404b540aSrobert
52*404b540aSrobert if (table == NULL)
53*404b540aSrobert {
54*404b540aSrobert pfile->our_hashtable = 1;
55*404b540aSrobert table = ht_create (13); /* 8K (=2^13) entries. */
56*404b540aSrobert table->alloc_node = (hashnode (*) (hash_table *)) alloc_node;
57*404b540aSrobert
58*404b540aSrobert _obstack_begin (&pfile->hash_ob, 0, 0,
59*404b540aSrobert (void *(*) (long)) xmalloc,
60*404b540aSrobert (void (*) (void *)) free);
61*404b540aSrobert }
62*404b540aSrobert
63*404b540aSrobert table->pfile = pfile;
64*404b540aSrobert pfile->hash_table = table;
65*404b540aSrobert
66*404b540aSrobert /* Now we can initialize things that use the hash table. */
67*404b540aSrobert _cpp_init_directives (pfile);
68*404b540aSrobert _cpp_init_internal_pragmas (pfile);
69*404b540aSrobert
70*404b540aSrobert s = &pfile->spec_nodes;
71*404b540aSrobert s->n_defined = cpp_lookup (pfile, DSC("defined"));
72*404b540aSrobert s->n_true = cpp_lookup (pfile, DSC("true"));
73*404b540aSrobert s->n_false = cpp_lookup (pfile, DSC("false"));
74*404b540aSrobert s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
75*404b540aSrobert s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
76*404b540aSrobert }
77*404b540aSrobert
78*404b540aSrobert /* Tear down the identifier hash table. */
79*404b540aSrobert void
_cpp_destroy_hashtable(cpp_reader * pfile)80*404b540aSrobert _cpp_destroy_hashtable (cpp_reader *pfile)
81*404b540aSrobert {
82*404b540aSrobert if (pfile->our_hashtable)
83*404b540aSrobert {
84*404b540aSrobert ht_destroy (pfile->hash_table);
85*404b540aSrobert obstack_free (&pfile->hash_ob, 0);
86*404b540aSrobert }
87*404b540aSrobert }
88*404b540aSrobert
89*404b540aSrobert /* Returns the hash entry for the STR of length LEN, creating one
90*404b540aSrobert if necessary. */
91*404b540aSrobert cpp_hashnode *
cpp_lookup(cpp_reader * pfile,const unsigned char * str,unsigned int len)92*404b540aSrobert cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len)
93*404b540aSrobert {
94*404b540aSrobert /* ht_lookup cannot return NULL. */
95*404b540aSrobert return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
96*404b540aSrobert }
97*404b540aSrobert
98*404b540aSrobert /* Determine whether the str STR, of length LEN, is a defined macro. */
99*404b540aSrobert int
cpp_defined(cpp_reader * pfile,const unsigned char * str,int len)100*404b540aSrobert cpp_defined (cpp_reader *pfile, const unsigned char *str, int len)
101*404b540aSrobert {
102*404b540aSrobert cpp_hashnode *node;
103*404b540aSrobert
104*404b540aSrobert node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
105*404b540aSrobert
106*404b540aSrobert /* If it's of type NT_MACRO, it cannot be poisoned. */
107*404b540aSrobert return node && node->type == NT_MACRO;
108*404b540aSrobert }
109*404b540aSrobert
110*404b540aSrobert /* For all nodes in the hashtable, callback CB with parameters PFILE,
111*404b540aSrobert the node, and V. */
112*404b540aSrobert void
cpp_forall_identifiers(cpp_reader * pfile,cpp_cb cb,void * v)113*404b540aSrobert cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v)
114*404b540aSrobert {
115*404b540aSrobert /* We don't need a proxy since the hash table's identifier comes
116*404b540aSrobert first in cpp_hashnode. */
117*404b540aSrobert ht_forall (pfile->hash_table, (ht_cb) cb, v);
118*404b540aSrobert }
119