Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 1 | /* Hash tables for the CPP library. |
Jeff Law | 5e7b4e2 | 2000-02-25 22:59:31 -0700 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 3 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 4 | Written by Per Bothner, 1994. |
Jeff Law | 38e0125 | 1998-05-06 15:09:07 -0600 | [diff] [blame] | 5 | Based on CCCP program by Paul Rubin, June 1986 |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 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 |
Kelley Cook | 200031d | 2005-06-29 02:34:39 +0000 | [diff] [blame] | 20 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 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 | |
Kaveh R. Ghazi | b04cd507 | 1998-03-30 12:05:54 +0000 | [diff] [blame] | 26 | #include "config.h" |
| 27 | #include "system.h" |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 28 | #include "cpplib.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 29 | #include "internal.h" |
Richard Kenner | e38992e | 2000-04-18 20:42:00 +0000 | [diff] [blame] | 30 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 31 | static cpp_hashnode *alloc_node (hash_table *); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 32 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 33 | /* Return an identifier node for hashtable.c. Used by cpplib except |
| 34 | when integrated with the C front ends. */ |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 35 | static cpp_hashnode * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 36 | alloc_node (hash_table *table) |
Zack Weinberg | fa55727 | 2000-05-10 19:11:02 +0000 | [diff] [blame] | 37 | { |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 38 | cpp_hashnode *node; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 39 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 40 | node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode); |
Kaveh R. Ghazi | fad205f | 2003-06-16 21:41:10 +0000 | [diff] [blame] | 41 | memset (node, 0, sizeof (cpp_hashnode)); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 42 | return node; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 45 | /* Set up the identifier hash table. Use TABLE if non-null, otherwise |
| 46 | create our own. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 47 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 48 | _cpp_init_hashtable (cpp_reader *pfile, hash_table *table) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 49 | { |
Neil Booth | f5e9945 | 2001-11-15 10:01:10 +0000 | [diff] [blame] | 50 | struct spec_nodes *s; |
| 51 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 52 | if (table == NULL) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 53 | { |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 54 | pfile->our_hashtable = 1; |
| 55 | table = ht_create (13); /* 8K (=2^13) entries. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 56 | table->alloc_node = (hashnode (*) (hash_table *)) alloc_node; |
Zack Weinberg | 4383964 | 2003-07-13 17:34:18 +0000 | [diff] [blame] | 57 | |
| 58 | _obstack_begin (&pfile->hash_ob, 0, 0, |
| 59 | (void *(*) (long)) xmalloc, |
| 60 | (void (*) (void *)) free); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 61 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 62 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 63 | table->pfile = pfile; |
| 64 | pfile->hash_table = table; |
Neil Booth | f5e9945 | 2001-11-15 10:01:10 +0000 | [diff] [blame] | 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; |
Neil Booth | f5e9945 | 2001-11-15 10:01:10 +0000 | [diff] [blame] | 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")); |
Neil Booth | f5e9945 | 2001-11-15 10:01:10 +0000 | [diff] [blame] | 74 | s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__")); |
| 75 | s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 78 | /* Tear down the identifier hash table. */ |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 79 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 80 | _cpp_destroy_hashtable (cpp_reader *pfile) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 81 | { |
| 82 | if (pfile->our_hashtable) |
| 83 | { |
Neil Booth | bef985f | 2001-08-11 12:37:19 +0000 | [diff] [blame] | 84 | ht_destroy (pfile->hash_table); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 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. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 91 | cpp_hashnode * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 92 | cpp_lookup (cpp_reader *pfile, const unsigned char *str, unsigned int len) |
Zack Weinberg | cf4ed94 | 2000-02-10 23:47:04 +0000 | [diff] [blame] | 93 | { |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 94 | /* ht_lookup cannot return NULL. */ |
| 95 | return CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_ALLOC)); |
Zack Weinberg | cf4ed94 | 2000-02-10 23:47:04 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 98 | /* Determine whether the str STR, of length LEN, is a defined macro. */ |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 99 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 100 | cpp_defined (cpp_reader *pfile, const unsigned char *str, int len) |
Zack Weinberg | 0f89df6 | 2000-04-22 23:02:08 +0000 | [diff] [blame] | 101 | { |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 102 | cpp_hashnode *node; |
Zack Weinberg | 0f89df6 | 2000-04-22 23:02:08 +0000 | [diff] [blame] | 103 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 104 | node = CPP_HASHNODE (ht_lookup (pfile->hash_table, str, len, HT_NO_INSERT)); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 105 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 106 | /* If it's of type NT_MACRO, it cannot be poisoned. */ |
| 107 | return node && node->type == NT_MACRO; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 110 | /* For all nodes in the hashtable, callback CB with parameters PFILE, |
| 111 | the node, and V. */ |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 112 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 113 | cpp_forall_identifiers (cpp_reader *pfile, cpp_cb cb, void *v) |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 114 | { |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 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); |
Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 118 | } |