Merge from mainline
[official-gcc.git] / gcc / cpphash.c
blob98d51be0a4c0be6c26a6db02b9cf29dfcd2cb21e
1 /* Hash tables for the CPP library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001, 2002 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. */
35 static cpp_hashnode *
36 alloc_node (table)
37 hash_table *table;
39 cpp_hashnode *node;
41 node = (cpp_hashnode *) obstack_alloc (&table->pfile->hash_ob,
42 sizeof (cpp_hashnode));
43 memset ((PTR) node, 0, sizeof (cpp_hashnode));
44 return node;
47 /* Set up the identifier hash table. Use TABLE if non-null, otherwise
48 create our own. */
49 void
50 _cpp_init_hashtable (pfile, table)
51 cpp_reader *pfile;
52 hash_table *table;
54 struct spec_nodes *s;
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;
67 /* Now we can initialize things that use the hash table. */
68 _cpp_init_directives (pfile);
69 _cpp_init_internal_pragmas (pfile);
71 s = &pfile->spec_nodes;
72 s->n_defined = cpp_lookup (pfile, DSC("defined"));
73 s->n_true = cpp_lookup (pfile, DSC("true"));
74 s->n_false = cpp_lookup (pfile, DSC("false"));
75 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
76 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
79 /* Tear down the identifier hash table. */
80 void
81 _cpp_destroy_hashtable (pfile)
82 cpp_reader *pfile;
84 if (pfile->our_hashtable)
86 ht_destroy (pfile->hash_table);
87 obstack_free (&pfile->hash_ob, 0);
91 /* Returns the hash entry for the STR of length LEN, creating one
92 if necessary. */
93 cpp_hashnode *
94 cpp_lookup (pfile, str, len)
95 cpp_reader *pfile;
96 const unsigned char *str;
97 unsigned int len;
99 /* ht_lookup cannot return NULL. */
100 return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
103 /* Determine whether the str STR, of length LEN, is a defined macro. */
105 cpp_defined (pfile, str, len)
106 cpp_reader *pfile;
107 const unsigned char *str;
108 int len;
110 cpp_hashnode *node;
112 node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
114 /* If it's of type NT_MACRO, it cannot be poisoned. */
115 return node && node->type == NT_MACRO;
118 /* For all nodes in the hashtable, callback CB with parameters PFILE,
119 the node, and V. */
120 void
121 cpp_forall_identifiers (pfile, cb, v)
122 cpp_reader *pfile;
123 cpp_cb cb;
124 PTR v;
126 /* We don't need a proxy since the hash table's identifier comes
127 first in cpp_hashnode. */
128 ht_forall (pfile->hash_table, (ht_cb) cb, v);