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
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "internal.h"
30
31static cpp_hashnode *alloc_node (hash_table *);
32
33/* Return an identifier node for hashtable.c.  Used by cpplib except
34   when integrated with the C front ends.  */
35static cpp_hashnode *
36alloc_node (hash_table *table)
37{
38  cpp_hashnode *node;
39
40  node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode);
41  memset (node, 0, sizeof (cpp_hashnode));
42  return node;
43}
44
45/* Set up the identifier hash table.  Use TABLE if non-null, otherwise
46   create our own.  */
47void
48_cpp_init_hashtable (cpp_reader *pfile, hash_table *table)
49{
50  struct spec_nodes *s;
51
52  if (table == NULL)
53    {
54      pfile->our_hashtable = 1;
55      table = ht_create (13);	/* 8K (=2^13) entries.  */
56      table->alloc_node = (hashnode (*) (hash_table *)) alloc_node;
57
58      _obstack_begin (&pfile->hash_ob, 0, 0,
59		      (void *(*) (long)) xmalloc,
60		      (void (*) (void *)) free);
61    }
62
63  table->pfile = pfile;
64  pfile->hash_table = table;
65
66  /* Now we can initialize things that use the hash table.  */
67  _cpp_init_directives (pfile);
68  _cpp_init_internal_pragmas (pfile);
69
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;
76}
77
78/* Tear down the identifier hash table.  */
79void
80_cpp_destroy_hashtable (cpp_reader *pfile)
81{
82  if (pfile->our_hashtable)
83    {
84      ht_destroy (pfile->hash_table);
85      obstack_free (&pfile->hash_ob, 0);
86    }
87}
88
89/* Returns the hash entry for the STR of length LEN, creating one
90   if necessary.  */
91cpp_hashnode *
92cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len)
93{
94  /* ht_lookup cannot return NULL.  */
95  return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
96}
97
98/* Determine whether the str STR, of length LEN, is a defined macro.  */
99int
100cpp_defined (cpp_reader *pfile, const unsigned char *str, int len)
101{
102  cpp_hashnode *node;
103
104  node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
105
106  /* If it's of type NT_MACRO, it cannot be poisoned.  */
107  return node && node->type == NT_MACRO;
108}
109
110/* For all nodes in the hashtable, callback CB with parameters PFILE,
111   the node, and V.  */
112void
113cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v)
114{
115  /* We don't need a proxy since the hash table's identifier comes
116     first in cpp_hashnode.  */
117  ht_forall (pfile->hash_table, (ht_cb) cb, v);
118}
119