blob: c22f4a714bb558dc49965da9ea788855dc76c5b6 [file] [log] [blame]
Neil Booth2a967f32001-05-20 06:26:45 +00001/* Hash tables for the CPP library.
Jeff Law5e7b4e22000-02-25 22:59:31 -07002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
Neil Booth5d8ebbd2002-01-03 21:43:09 +00003 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
Per Bothner7f2935c1995-03-16 13:59:07 -08004 Written by Per Bothner, 1994.
Jeff Law38e01251998-05-06 15:09:07 -06005 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08006 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
Kelley Cook200031d2005-06-29 02:34:39 +000020Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Per Bothner7f2935c1995-03-16 13:59:07 -080021
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
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000026#include "config.h"
27#include "system.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080028#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000029#include "internal.h"
Richard Kennere38992e2000-04-18 20:42:00 +000030
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000031static cpp_hashnode *alloc_node (hash_table *);
Per Bothner7f2935c1995-03-16 13:59:07 -080032
Neil Booth2a967f32001-05-20 06:26:45 +000033/* Return an identifier node for hashtable.c. Used by cpplib except
34 when integrated with the C front ends. */
Neil Booth2a967f32001-05-20 06:26:45 +000035static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000036alloc_node (hash_table *table)
Zack Weinbergfa557272000-05-10 19:11:02 +000037{
Neil Booth2a967f32001-05-20 06:26:45 +000038 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +000039
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000040 node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode);
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +000041 memset (node, 0, sizeof (cpp_hashnode));
Neil Booth2a967f32001-05-20 06:26:45 +000042 return node;
Zack Weinbergc71f8352000-07-05 05:33:57 +000043}
44
Neil Booth2a967f32001-05-20 06:26:45 +000045/* Set up the identifier hash table. Use TABLE if non-null, otherwise
46 create our own. */
Zack Weinbergc71f8352000-07-05 05:33:57 +000047void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000048_cpp_init_hashtable (cpp_reader *pfile, hash_table *table)
Zack Weinbergc71f8352000-07-05 05:33:57 +000049{
Neil Boothf5e99452001-11-15 10:01:10 +000050 struct spec_nodes *s;
51
Neil Booth2a967f32001-05-20 06:26:45 +000052 if (table == NULL)
Zack Weinberg711b8822000-07-18 00:59:49 +000053 {
Neil Booth2a967f32001-05-20 06:26:45 +000054 pfile->our_hashtable = 1;
55 table = ht_create (13); /* 8K (=2^13) entries. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000056 table->alloc_node = (hashnode (*) (hash_table *)) alloc_node;
Zack Weinberg43839642003-07-13 17:34:18 +000057
58 _obstack_begin (&pfile->hash_ob, 0, 0,
59 (void *(*) (long)) xmalloc,
60 (void (*) (void *)) free);
Zack Weinberg711b8822000-07-18 00:59:49 +000061 }
Zack Weinberg711b8822000-07-18 00:59:49 +000062
Neil Booth2a967f32001-05-20 06:26:45 +000063 table->pfile = pfile;
64 pfile->hash_table = table;
Neil Boothf5e99452001-11-15 10:01:10 +000065
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;
Neil Boothf5e99452001-11-15 10:01:10 +000071 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"));
Neil Boothf5e99452001-11-15 10:01:10 +000074 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
75 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
Per Bothner7f2935c1995-03-16 13:59:07 -080076}
77
Neil Booth2a967f32001-05-20 06:26:45 +000078/* Tear down the identifier hash table. */
Neil Booth2a967f32001-05-20 06:26:45 +000079void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000080_cpp_destroy_hashtable (cpp_reader *pfile)
Neil Booth2a967f32001-05-20 06:26:45 +000081{
82 if (pfile->our_hashtable)
83 {
Neil Boothbef985f2001-08-11 12:37:19 +000084 ht_destroy (pfile->hash_table);
Neil Booth2a967f32001-05-20 06:26:45 +000085 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. */
Zack Weinberg711b8822000-07-18 00:59:49 +000091cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000092cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len)
Zack Weinbergcf4ed942000-02-10 23:47:04 +000093{
Neil Booth2a967f32001-05-20 06:26:45 +000094 /* ht_lookup cannot return NULL. */
95 return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC));
Zack Weinbergcf4ed942000-02-10 23:47:04 +000096}
97
Neil Booth2a967f32001-05-20 06:26:45 +000098/* Determine whether the str STR, of length LEN, is a defined macro. */
Neil Booth2a967f32001-05-20 06:26:45 +000099int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000100cpp_defined (cpp_reader *pfile, const unsigned char *str, int len)
Zack Weinberg0f89df62000-04-22 23:02:08 +0000101{
Neil Booth2a967f32001-05-20 06:26:45 +0000102 cpp_hashnode *node;
Zack Weinberg0f89df62000-04-22 23:02:08 +0000103
Neil Booth2a967f32001-05-20 06:26:45 +0000104 node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT));
Zack Weinberg711b8822000-07-18 00:59:49 +0000105
Neil Booth2a967f32001-05-20 06:26:45 +0000106 /* If it's of type NT_MACRO, it cannot be poisoned. */
107 return node && node->type == NT_MACRO;
Zack Weinberg711b8822000-07-18 00:59:49 +0000108}
109
Neil Booth2a967f32001-05-20 06:26:45 +0000110/* For all nodes in the hashtable, callback CB with parameters PFILE,
111 the node, and V. */
Zack Weinbergd35364d2000-03-12 23:46:05 +0000112void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000113cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v)
Zack Weinbergd35364d2000-03-12 23:46:05 +0000114{
Neil Booth2a967f32001-05-20 06:26:45 +0000115 /* 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);
Neil Bootha9499412000-11-09 21:18:15 +0000118}