1*e4b17023SJohn Marino /* Hash tables for the CPP library. 2*e4b17023SJohn Marino Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, 3*e4b17023SJohn Marino 1999, 2000, 2001, 2002, 2007, 2009 Free Software Foundation, Inc. 4*e4b17023SJohn Marino Written by Per Bothner, 1994. 5*e4b17023SJohn Marino Based on CCCP program by Paul Rubin, June 1986 6*e4b17023SJohn Marino Adapted to ANSI C, Richard Stallman, Jan 1987 7*e4b17023SJohn Marino 8*e4b17023SJohn Marino This program is free software; you can redistribute it and/or modify it 9*e4b17023SJohn Marino under the terms of the GNU General Public License as published by the 10*e4b17023SJohn Marino Free Software Foundation; either version 3, or (at your option) any 11*e4b17023SJohn Marino later version. 12*e4b17023SJohn Marino 13*e4b17023SJohn Marino This program is distributed in the hope that it will be useful, 14*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of 15*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*e4b17023SJohn Marino GNU General Public License for more details. 17*e4b17023SJohn Marino 18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License 19*e4b17023SJohn Marino along with this program; see the file COPYING3. If not see 20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. 21*e4b17023SJohn Marino 22*e4b17023SJohn Marino In other words, you are welcome to use, share and improve this program. 23*e4b17023SJohn Marino You are forbidden to forbid anyone else to use, share and improve 24*e4b17023SJohn Marino what you give them. Help stamp out software-hoarding! */ 25*e4b17023SJohn Marino 26*e4b17023SJohn Marino #include "config.h" 27*e4b17023SJohn Marino #include "system.h" 28*e4b17023SJohn Marino #include "cpplib.h" 29*e4b17023SJohn Marino #include "internal.h" 30*e4b17023SJohn Marino 31*e4b17023SJohn Marino static hashnode alloc_node (hash_table *); 32*e4b17023SJohn Marino 33*e4b17023SJohn Marino /* Return an identifier node for hashtable.c. Used by cpplib except 34*e4b17023SJohn Marino when integrated with the C front ends. */ 35*e4b17023SJohn Marino static hashnode 36*e4b17023SJohn Marino alloc_node (hash_table *table) 37*e4b17023SJohn Marino { 38*e4b17023SJohn Marino cpp_hashnode *node; 39*e4b17023SJohn Marino 40*e4b17023SJohn Marino node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode); 41*e4b17023SJohn Marino memset (node, 0, sizeof (cpp_hashnode)); 42*e4b17023SJohn Marino return HT_NODE (node); 43*e4b17023SJohn Marino } 44*e4b17023SJohn Marino 45*e4b17023SJohn Marino /* Set up the identifier hash table. Use TABLE if non-null, otherwise 46*e4b17023SJohn Marino create our own. */ 47*e4b17023SJohn Marino void 48*e4b17023SJohn Marino _cpp_init_hashtable (cpp_reader *pfile, hash_table *table) 49*e4b17023SJohn Marino { 50*e4b17023SJohn Marino struct spec_nodes *s; 51*e4b17023SJohn Marino 52*e4b17023SJohn Marino if (table == NULL) 53*e4b17023SJohn Marino { 54*e4b17023SJohn Marino pfile->our_hashtable = 1; 55*e4b17023SJohn Marino table = ht_create (13); /* 8K (=2^13) entries. */ 56*e4b17023SJohn Marino table->alloc_node = alloc_node; 57*e4b17023SJohn Marino 58*e4b17023SJohn Marino _obstack_begin (&pfile->hash_ob, 0, 0, 59*e4b17023SJohn Marino (void *(*) (long)) xmalloc, 60*e4b17023SJohn Marino (void (*) (void *)) free); 61*e4b17023SJohn Marino } 62*e4b17023SJohn Marino 63*e4b17023SJohn Marino table->pfile = pfile; 64*e4b17023SJohn Marino pfile->hash_table = table; 65*e4b17023SJohn Marino 66*e4b17023SJohn Marino /* Now we can initialize things that use the hash table. */ 67*e4b17023SJohn Marino _cpp_init_directives (pfile); 68*e4b17023SJohn Marino _cpp_init_internal_pragmas (pfile); 69*e4b17023SJohn Marino 70*e4b17023SJohn Marino s = &pfile->spec_nodes; 71*e4b17023SJohn Marino s->n_defined = cpp_lookup (pfile, DSC("defined")); 72*e4b17023SJohn Marino s->n_true = cpp_lookup (pfile, DSC("true")); 73*e4b17023SJohn Marino s->n_false = cpp_lookup (pfile, DSC("false")); 74*e4b17023SJohn Marino s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__")); 75*e4b17023SJohn Marino s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC; 76*e4b17023SJohn Marino } 77*e4b17023SJohn Marino 78*e4b17023SJohn Marino /* Tear down the identifier hash table. */ 79*e4b17023SJohn Marino void 80*e4b17023SJohn Marino _cpp_destroy_hashtable (cpp_reader *pfile) 81*e4b17023SJohn Marino { 82*e4b17023SJohn Marino if (pfile->our_hashtable) 83*e4b17023SJohn Marino { 84*e4b17023SJohn Marino ht_destroy (pfile->hash_table); 85*e4b17023SJohn Marino obstack_free (&pfile->hash_ob, 0); 86*e4b17023SJohn Marino } 87*e4b17023SJohn Marino } 88*e4b17023SJohn Marino 89*e4b17023SJohn Marino /* Returns the hash entry for the STR of length LEN, creating one 90*e4b17023SJohn Marino if necessary. */ 91*e4b17023SJohn Marino cpp_hashnode * 92*e4b17023SJohn Marino cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len) 93*e4b17023SJohn Marino { 94*e4b17023SJohn Marino /* ht_lookup cannot return NULL. */ 95*e4b17023SJohn Marino return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC)); 96*e4b17023SJohn Marino } 97*e4b17023SJohn Marino 98*e4b17023SJohn Marino /* Determine whether the str STR, of length LEN, is a defined macro. */ 99*e4b17023SJohn Marino int 100*e4b17023SJohn Marino cpp_defined (cpp_reader *pfile, const unsigned char *str, int len) 101*e4b17023SJohn Marino { 102*e4b17023SJohn Marino cpp_hashnode *node; 103*e4b17023SJohn Marino 104*e4b17023SJohn Marino node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT)); 105*e4b17023SJohn Marino 106*e4b17023SJohn Marino /* If it's of type NT_MACRO, it cannot be poisoned. */ 107*e4b17023SJohn Marino return node && node->type == NT_MACRO; 108*e4b17023SJohn Marino } 109*e4b17023SJohn Marino 110*e4b17023SJohn Marino /* We don't need a proxy since the hash table's identifier comes first 111*e4b17023SJohn Marino in cpp_hashnode. However, in case this is ever changed, we have a 112*e4b17023SJohn Marino static assertion for it. */ 113*e4b17023SJohn Marino extern char proxy_assertion_broken[offsetof (struct cpp_hashnode, ident) == 0 ? 1 : -1]; 114*e4b17023SJohn Marino 115*e4b17023SJohn Marino /* For all nodes in the hashtable, callback CB with parameters PFILE, 116*e4b17023SJohn Marino the node, and V. */ 117*e4b17023SJohn Marino void 118*e4b17023SJohn Marino cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v) 119*e4b17023SJohn Marino { 120*e4b17023SJohn Marino ht_forall (pfile->hash_table, (ht_cb) cb, v); 121*e4b17023SJohn Marino } 122