1169695Skan/* Hash tables for the CPP library. 2169695Skan Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, 3169695Skan 1999, 2000, 2001, 2002 Free Software Foundation, Inc. 4169695Skan Written by Per Bothner, 1994. 5169695Skan Based on CCCP program by Paul Rubin, June 1986 6169695Skan Adapted to ANSI C, Richard Stallman, Jan 1987 7169695Skan 8169695SkanThis program is free software; you can redistribute it and/or modify it 9169695Skanunder the terms of the GNU General Public License as published by the 10169695SkanFree Software Foundation; either version 2, or (at your option) any 11169695Skanlater version. 12169695Skan 13169695SkanThis program is distributed in the hope that it will be useful, 14169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 15169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16169695SkanGNU General Public License for more details. 17169695Skan 18169695SkanYou should have received a copy of the GNU General Public License 19169695Skanalong with this program; if not, write to the Free Software 20169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21169695Skan 22169695Skan In other words, you are welcome to use, share and improve this program. 23169695Skan You are forbidden to forbid anyone else to use, share and improve 24169695Skan what you give them. Help stamp out software-hoarding! */ 25169695Skan 26169695Skan#include "config.h" 27169695Skan#include "system.h" 28169695Skan#include "cpplib.h" 29169695Skan#include "internal.h" 30169695Skan 31169695Skanstatic cpp_hashnode *alloc_node (hash_table *); 32169695Skan 33169695Skan/* Return an identifier node for hashtable.c. Used by cpplib except 34169695Skan when integrated with the C front ends. */ 35169695Skanstatic cpp_hashnode * 36169695Skanalloc_node (hash_table *table) 37169695Skan{ 38169695Skan cpp_hashnode *node; 39169695Skan 40169695Skan node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode); 41169695Skan memset (node, 0, sizeof (cpp_hashnode)); 42169695Skan return node; 43169695Skan} 44169695Skan 45169695Skan/* Set up the identifier hash table. Use TABLE if non-null, otherwise 46169695Skan create our own. */ 47169695Skanvoid 48169695Skan_cpp_init_hashtable (cpp_reader *pfile, hash_table *table) 49169695Skan{ 50169695Skan struct spec_nodes *s; 51169695Skan 52169695Skan if (table == NULL) 53169695Skan { 54169695Skan pfile->our_hashtable = 1; 55169695Skan table = ht_create (13); /* 8K (=2^13) entries. */ 56169695Skan table->alloc_node = (hashnode (*) (hash_table *)) alloc_node; 57169695Skan 58169695Skan _obstack_begin (&pfile->hash_ob, 0, 0, 59169695Skan (void *(*) (long)) xmalloc, 60169695Skan (void (*) (void *)) free); 61169695Skan } 62169695Skan 63169695Skan table->pfile = pfile; 64169695Skan pfile->hash_table = table; 65169695Skan 66169695Skan /* Now we can initialize things that use the hash table. */ 67169695Skan _cpp_init_directives (pfile); 68169695Skan _cpp_init_internal_pragmas (pfile); 69169695Skan 70169695Skan s = &pfile->spec_nodes; 71169695Skan s->n_defined = cpp_lookup (pfile, DSC("defined")); 72169695Skan s->n_true = cpp_lookup (pfile, DSC("true")); 73169695Skan s->n_false = cpp_lookup (pfile, DSC("false")); 74169695Skan s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__")); 75169695Skan s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC; 76169695Skan} 77169695Skan 78169695Skan/* Tear down the identifier hash table. */ 79169695Skanvoid 80169695Skan_cpp_destroy_hashtable (cpp_reader *pfile) 81169695Skan{ 82169695Skan if (pfile->our_hashtable) 83169695Skan { 84169695Skan ht_destroy (pfile->hash_table); 85169695Skan obstack_free (&pfile->hash_ob, 0); 86169695Skan } 87169695Skan} 88169695Skan 89169695Skan/* Returns the hash entry for the STR of length LEN, creating one 90169695Skan if necessary. */ 91169695Skancpp_hashnode * 92169695Skancpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len) 93169695Skan{ 94169695Skan /* ht_lookup cannot return NULL. */ 95169695Skan return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC)); 96169695Skan} 97169695Skan 98169695Skan/* Determine whether the str STR, of length LEN, is a defined macro. */ 99169695Skanint 100169695Skancpp_defined (cpp_reader *pfile, const unsigned char *str, int len) 101169695Skan{ 102169695Skan cpp_hashnode *node; 103169695Skan 104169695Skan node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT)); 105169695Skan 106169695Skan /* If it's of type NT_MACRO, it cannot be poisoned. */ 107169695Skan return node && node->type == NT_MACRO; 108169695Skan} 109169695Skan 110169695Skan/* For all nodes in the hashtable, callback CB with parameters PFILE, 111169695Skan the node, and V. */ 112169695Skanvoid 113169695Skancpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v) 114169695Skan{ 115169695Skan /* We don't need a proxy since the hash table's identifier comes 116169695Skan first in cpp_hashnode. */ 117169695Skan ht_forall (pfile->hash_table, (ht_cb) cb, v); 118169695Skan} 119