]>
Commit | Line | Data |
---|---|---|
2a967f3d | 1 | /* Hash tables for the CPP library. |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, |
5d8ebbd8 | 3 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
7f2935c7 | 4 | Written by Per Bothner, 1994. |
38e01259 | 5 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
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 | |
200031d1 | 20 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
7f2935c7 PB |
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 | ||
b04cd507 KG |
26 | #include "config.h" |
27 | #include "system.h" | |
7f2935c7 | 28 | #include "cpplib.h" |
4f4e53dd | 29 | #include "internal.h" |
e38992e8 | 30 | |
6cf87ca4 | 31 | static cpp_hashnode *alloc_node (hash_table *); |
7f2935c7 | 32 | |
2a967f3d NB |
33 | /* Return an identifier node for hashtable.c. Used by cpplib except |
34 | when integrated with the C front ends. */ | |
2a967f3d | 35 | static cpp_hashnode * |
6cf87ca4 | 36 | alloc_node (hash_table *table) |
2964d54f | 37 | { |
2a967f3d | 38 | cpp_hashnode *node; |
df383483 | 39 | |
c3f829c1 | 40 | node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode); |
fad205ff | 41 | memset (node, 0, sizeof (cpp_hashnode)); |
2a967f3d | 42 | return node; |
d9e0bd53 ZW |
43 | } |
44 | ||
2a967f3d NB |
45 | /* Set up the identifier hash table. Use TABLE if non-null, otherwise |
46 | create our own. */ | |
711b8824 | 47 | void |
6cf87ca4 | 48 | _cpp_init_hashtable (cpp_reader *pfile, hash_table *table) |
6de1e2a9 | 49 | { |
f5e99456 NB |
50 | struct spec_nodes *s; |
51 | ||
2a967f3d | 52 | if (table == NULL) |
6de1e2a9 | 53 | { |
2a967f3d NB |
54 | pfile->our_hashtable = 1; |
55 | table = ht_create (13); /* 8K (=2^13) entries. */ | |
6cf87ca4 | 56 | table->alloc_node = (hashnode (*) (hash_table *)) alloc_node; |
43839642 ZW |
57 | |
58 | _obstack_begin (&pfile->hash_ob, 0, 0, | |
59 | (void *(*) (long)) xmalloc, | |
60 | (void (*) (void *)) free); | |
b30892f9 | 61 | } |
711b8824 | 62 | |
2a967f3d NB |
63 | table->pfile = pfile; |
64 | pfile->hash_table = table; | |
f5e99456 NB |
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; | |
f5e99456 NB |
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")); | |
f5e99456 NB |
74 | s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__")); |
75 | s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC; | |
ba412f14 ZW |
76 | } |
77 | ||
2a967f3d | 78 | /* Tear down the identifier hash table. */ |
2a967f3d | 79 | void |
6cf87ca4 | 80 | _cpp_destroy_hashtable (cpp_reader *pfile) |
ba412f14 | 81 | { |
2a967f3d | 82 | if (pfile->our_hashtable) |
b30892f9 | 83 | { |
bef985f3 | 84 | ht_destroy (pfile->hash_table); |
2a967f3d | 85 | obstack_free (&pfile->hash_ob, 0); |
b30892f9 | 86 | } |
45b966db ZW |
87 | } |
88 | ||
2a967f3d NB |
89 | /* Returns the hash entry for the STR of length LEN, creating one |
90 | if necessary. */ | |
711b8824 | 91 | cpp_hashnode * |
6cf87ca4 | 92 | cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len) |
6de1e2a9 | 93 | { |
2a967f3d NB |
94 | /* ht_lookup cannot return NULL. */ |
95 | return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC)); | |
d9e0bd53 | 96 | } |
3caee4a8 | 97 | |
2a967f3d | 98 | /* Determine whether the str STR, of length LEN, is a defined macro. */ |
2a967f3d | 99 | int |
6cf87ca4 | 100 | cpp_defined (cpp_reader *pfile, const unsigned char *str, int len) |
d9e0bd53 | 101 | { |
2a967f3d | 102 | cpp_hashnode *node; |
3caee4a8 | 103 | |
2a967f3d | 104 | node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT)); |
d35364d1 | 105 | |
2a967f3d NB |
106 | /* If it's of type NT_MACRO, it cannot be poisoned. */ |
107 | return node && node->type == NT_MACRO; | |
d35364d1 ZW |
108 | } |
109 | ||
2a967f3d NB |
110 | /* For all nodes in the hashtable, callback CB with parameters PFILE, |
111 | the node, and V. */ | |
d35364d1 | 112 | void |
6cf87ca4 | 113 | cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v) |
d35364d1 | 114 | { |
2a967f3d NB |
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); | |
a949941c | 118 | } |