2001-08-28 Alexandre Petit-Bianco <apbianco@redhat.com>
[official-gcc.git] / gcc / cpphash.c
blobdef51715f43a0abadc5eb27c02686f5b864cc834
1 /* Hash tables for the CPP library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
31 static cpp_hashnode *alloc_node PARAMS ((hash_table *));
33 /* Return an identifier node for hashtable.c. Used by cpplib except
34 when integrated with the C front ends. */
36 static cpp_hashnode *
37 alloc_node (table)
38 hash_table *table;
40 cpp_hashnode *node;
42 node = (cpp_hashnode *) obstack_alloc (&table->pfile->hash_ob,
43 sizeof (cpp_hashnode));
44 memset ((PTR) node, 0, sizeof (cpp_hashnode));
45 return node;
48 /* Set up the identifier hash table. Use TABLE if non-null, otherwise
49 create our own. */
51 void
52 _cpp_init_hashtable (pfile, table)
53 cpp_reader *pfile;
54 hash_table *table;
56 if (table == NULL)
58 pfile->our_hashtable = 1;
59 table = ht_create (13); /* 8K (=2^13) entries. */
60 table->alloc_node = (hashnode (*) PARAMS ((hash_table *))) alloc_node;
61 gcc_obstack_init (&pfile->hash_ob);
64 table->pfile = pfile;
65 pfile->hash_table = table;
68 /* Tear down the identifier hash table. */
70 void
71 _cpp_destroy_hashtable (pfile)
72 cpp_reader *pfile;
74 if (pfile->our_hashtable)
76 ht_destroy (pfile->hash_table);
77 obstack_free (&pfile->hash_ob, 0);
81 /* Returns the hash entry for the STR of length LEN, creating one
82 if necessary. */
84 cpp_hashnode *
85 cpp_lookup (pfile, str, len)
86 cpp_reader *pfile;
87 const unsigned char *str;
88 unsigned int len;
90 /* ht_lookup cannot return NULL. */
91 return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
94 /* Determine whether the str STR, of length LEN, is a defined macro. */
96 int
97 cpp_defined (pfile, str, len)
98 cpp_reader *pfile;
99 const unsigned char *str;
100 int len;
102 cpp_hashnode *node;
104 node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
106 /* If it's of type NT_MACRO, it cannot be poisoned. */
107 return node && node->type == NT_MACRO;
110 /* For all nodes in the hashtable, callback CB with parameters PFILE,
111 the node, and V. */
113 void
114 cpp_forall_identifiers (pfile, cb, v)
115 cpp_reader *pfile;
116 cpp_cb cb;
117 PTR v;
119 /* We don't need a proxy since the hash table's identifier comes
120 first in cpp_hashnode. */
121 ht_forall (pfile->hash_table, (ht_cb) cb, v);