OSDN Git Service

* c-common.c: Include c-lex.h.
[pf3gnuchains/gcc-fork.git] / gcc / cpphash.c
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
7
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.
12
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.
17
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.
21
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!  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30
31 static cpp_hashnode *alloc_node PARAMS ((hash_table *));
32
33 /* Return an identifier node for hashtable.c.  Used by cpplib except
34    when integrated with the C front ends.  */
35
36 static cpp_hashnode *
37 alloc_node (table)
38      hash_table *table;
39 {
40   cpp_hashnode *node;
41   
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;
46 }
47
48 /* Set up the identifier hash table.  Use TABLE if non-null, otherwise
49    create our own.  */
50
51 void
52 _cpp_init_hashtable (pfile, table)
53      cpp_reader *pfile;
54      hash_table *table;
55 {
56   struct spec_nodes *s;
57
58   if (table == NULL)
59     {
60       pfile->our_hashtable = 1;
61       table = ht_create (13);   /* 8K (=2^13) entries.  */
62       table->alloc_node = (hashnode (*) PARAMS ((hash_table *))) alloc_node;
63       gcc_obstack_init (&pfile->hash_ob);
64     }
65
66   table->pfile = pfile;
67   pfile->hash_table = table;
68
69   /* Now we can initialize things that use the hash table.  */
70   _cpp_init_directives (pfile);
71   _cpp_init_internal_pragmas (pfile);
72
73   s = &pfile->spec_nodes;
74   s->n_L                = cpp_lookup (pfile, DSC("L"));
75   s->n_defined          = cpp_lookup (pfile, DSC("defined"));
76   s->n_true             = cpp_lookup (pfile, DSC("true"));
77   s->n_false            = cpp_lookup (pfile, DSC("false"));
78   s->n__STRICT_ANSI__   = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
79   s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
80   s->n__VA_ARGS__       = cpp_lookup (pfile, DSC("__VA_ARGS__"));
81   s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
82 }
83
84 /* Tear down the identifier hash table.  */
85
86 void
87 _cpp_destroy_hashtable (pfile)
88      cpp_reader *pfile;
89 {
90   if (pfile->our_hashtable)
91     {
92       ht_destroy (pfile->hash_table);
93       obstack_free (&pfile->hash_ob, 0);
94     }
95 }
96
97 /* Returns the hash entry for the STR of length LEN, creating one
98    if necessary.  */
99
100 cpp_hashnode *
101 cpp_lookup (pfile, str, len)
102      cpp_reader *pfile;
103      const unsigned char *str;
104      unsigned int len;
105 {
106   /* ht_lookup cannot return NULL.  */
107   return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
108 }
109
110 /* Determine whether the str STR, of length LEN, is a defined macro.  */
111
112 int
113 cpp_defined (pfile, str, len)
114      cpp_reader *pfile;
115      const unsigned char *str;
116      int len;
117 {
118   cpp_hashnode *node;
119
120   node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
121
122   /* If it's of type NT_MACRO, it cannot be poisoned.  */
123   return node && node->type == NT_MACRO;
124 }
125
126 /* For all nodes in the hashtable, callback CB with parameters PFILE,
127    the node, and V.  */
128
129 void
130 cpp_forall_identifiers (pfile, cb, v)
131      cpp_reader *pfile;
132      cpp_cb cb;
133      PTR v;
134 {
135   /* We don't need a proxy since the hash table's identifier comes
136      first in cpp_hashnode.  */
137   ht_forall (pfile->hash_table, (ht_cb) cb, v);
138 }