* g++.dg/pph/c120060625-1.cc: New.
[official-gcc.git] / libcpp / identifiers.c
blobe02a06982769a2285569575d46ddd978274b4c33
1 /* Hash tables for the CPP library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001, 2002, 2007, 2009 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 3, 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; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
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 "internal.h"
31 static hashnode alloc_node (hash_table *);
33 /* Return an identifier node for hashtable.c. Used by cpplib except
34 when integrated with the C front ends. */
35 static hashnode
36 alloc_node (hash_table *table)
38 cpp_hashnode *node;
40 node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode);
41 memset (node, 0, sizeof (cpp_hashnode));
42 return HT_NODE (node);
45 /* Set up the identifier hash table. Use TABLE if non-null, otherwise
46 create our own. */
47 void
48 _cpp_init_hashtable (cpp_reader *pfile, hash_table *table)
50 struct spec_nodes *s;
52 if (table == NULL)
54 pfile->our_hashtable = 1;
55 table = ht_create (13); /* 8K (=2^13) entries. */
56 table->alloc_node = alloc_node;
58 _obstack_begin (&pfile->hash_ob, 0, 0,
59 (void *(*) (long)) xmalloc,
60 (void (*) (void *)) free);
63 table->pfile = pfile;
64 pfile->hash_table = table;
66 /* Now we can initialize things that use the hash table. */
67 _cpp_init_directives (pfile);
68 _cpp_init_internal_pragmas (pfile);
70 s = &pfile->spec_nodes;
71 s->n_defined = cpp_lookup (pfile, DSC("defined"));
72 s->n_true = cpp_lookup (pfile, DSC("true"));
73 s->n_false = cpp_lookup (pfile, DSC("false"));
74 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
75 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
78 /* Tear down the identifier hash table. */
79 void
80 _cpp_destroy_hashtable (cpp_reader *pfile)
82 if (pfile->our_hashtable)
84 ht_destroy (pfile->hash_table);
85 obstack_free (&pfile->hash_ob, 0);
89 /* Returns the hash entry for the STR of length LEN with hash HASH,
90 creating one if necessary. The return is not NULL. */
91 cpp_hashnode *
92 cpp_lookup_with_hash (cpp_reader *pfile,
93 const unsigned char *str, unsigned int len,
94 unsigned int hash)
96 cpp_hashnode *n;
98 if (pfile->lookaside_table)
99 n = lt_lookup (pfile, str, len, hash);
100 else
101 n = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table, str, len,
102 hash, HT_ALLOC));
104 return n;
107 /* Returns the hash entry for the STR of length LEN, creating one
108 if necessary. The return is not NULL. */
109 cpp_hashnode *
110 cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len)
112 unsigned int hash = ht_calc_hash (str, len);
113 return cpp_lookup_with_hash (pfile, str, len, hash);
116 /* Returns the hash entry for STR of length LEN from PFILE's symbol
117 table. If no entry exists, it returns NULL. */
118 cpp_hashnode *
119 cpp_peek_sym (cpp_reader *pfile, const unsigned char *str, unsigned int len)
121 cpp_hashnode *node;
122 node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
123 return node;
126 /* Determine whether the str STR, of length LEN, is a defined macro. */
128 cpp_defined (cpp_reader *pfile, const unsigned char *str, int len)
130 cpp_hashnode *node = cpp_peek_sym (pfile, str, len);
132 /* If it's of type NT_MACRO, it cannot be poisoned. */
133 return node && node->type == NT_MACRO;
136 /* We don't need a proxy since the hash table's identifier comes first
137 in cpp_hashnode. However, in case this is ever changed, we have a
138 static assertion for it. */
139 extern char proxy_assertion_broken[offsetof (struct cpp_hashnode, ident) == 0 ? 1 : -1];
141 /* For all nodes in the hashtable, callback CB with parameters PFILE,
142 the node, and V. */
143 void
144 cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v)
146 ht_forall (pfile->hash_table, (ht_cb) cb, v);
149 /* Dump a single identifier in PFILE to FILE. */
150 void
151 cpp_dump_identifier (cpp_reader *pfile, FILE *file, cpp_hashnode *node)
153 const unsigned char *name;
154 unsigned int len;
156 name = NODE_NAME (node);
157 len = NODE_LEN (node);
159 fprintf (file, "%.*s ", len, name);
160 if (node->is_directive)
161 fprintf (file, " [directive]");
163 if (node->type == NT_MACRO)
164 fprintf (file, " = %s", cpp_macro_definition (pfile, node));
166 fprintf (file, "\n");
170 /* Dump a single identifier in PFILE to stderr. */
171 void
172 cpp_debug_identifier (cpp_reader *pfile, cpp_hashnode *node)
174 cpp_dump_identifier (pfile, stderr, node);
178 /* Callback for cpp_dump_identifiers. */
179 static int
180 cpp_dump_identifiers_r (cpp_reader *pfile, cpp_hashnode *node, void *data)
182 cpp_dump_identifier (pfile, (FILE *) data, node);
183 return 1;
187 /* Dump all identifiers in PFILE to FILE. */
188 void
189 cpp_dump_identifiers (cpp_reader *pfile, FILE *file)
191 cpp_forall_identifiers (pfile, cpp_dump_identifiers_r, file);
195 /* Dump all identifiers in PFILE to stderr. */
196 void
197 cpp_debug_identifiers (cpp_reader *pfile)
199 cpp_dump_identifiers (pfile, stderr);