1 /* Part of CPP library. (Macro hash table support.)
2 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
28 /* Define a generic NULL if one hasn't already been defined. */
40 * return hash function on name. must be compatible with the one
41 * computed a step at a time, elsewhere
44 hashf (name, len, hashsize)
45 register const U_CHAR *name;
52 r = HASHSTEP (r, *name++);
54 return MAKE_POS (r) % hashsize;
58 * find the most recent hash node for name name (ending with first
59 * non-identifier char) installed by install
61 * If LEN is >= 0, it is the length of the name.
62 * Otherwise, compute the length by scanning the entire name.
64 * If HASH is >= 0, it is the precomputed hash code.
65 * Otherwise, compute the hash code.
68 cpp_lookup (pfile, name, len, hash)
69 struct parse_file *pfile;
74 register const U_CHAR *bp;
75 register HASHNODE *bucket;
79 for (bp = name; is_idchar[*bp]; bp++) ;
84 hash = hashf (name, len, HASHSIZE);
86 bucket = hashtab[hash];
88 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
90 bucket = bucket->next;
96 * Delete a hash node. Some weirdness to free junk from macros.
97 * More such weirdness will have to be added if you define more hash
101 /* Note that the DEFINITION of a macro is removed from the hash table
102 but its storage is not freed. This would be a storage leak
103 except that it is not reasonable to keep undefining and redefining
104 large numbers of macros many times.
105 In any case, this is necessary, because a macro can be #undef'd
106 in the middle of reading the arguments to a call to it.
107 If #undef freed the DEFINITION, that would crash. */
114 if (hp->prev != NULL)
115 hp->prev->next = hp->next;
116 if (hp->next != NULL)
117 hp->next->prev = hp->prev;
119 /* make sure that the bucket chain header that
120 the deleted guy was on points to the right thing afterwards. */
121 if (hp == *hp->bucket_hdr)
122 *hp->bucket_hdr = hp->next;
125 if (hp->type == T_MACRO) {
126 DEFINITION *d = hp->value.defn;
127 struct reflist *ap, *nextap;
129 for (ap = d->pattern; ap != NULL; ap = nextap) {
139 * install a name in the main hash table, even if it is already there.
140 * name stops with first non alphanumeric, except leading '#'.
141 * caller must check against redefinition if that is desired.
142 * delete_macro () removes things installed by install () in fifo order.
143 * this is important because of the `defined' special symbol used
144 * in #if, and also if pushdef/popdef directives are ever implemented.
146 * If LEN is >= 0, it is the length of the name.
147 * Otherwise, compute the length by scanning the entire name.
149 * If HASH is >= 0, it is the precomputed hash code.
150 * Otherwise, compute the hash code.
153 install (name, len, type, ivalue, value, hash)
161 register HASHNODE *hp;
162 register int i, bucket;
163 register U_CHAR *p, *q;
167 while (is_idchar[*p])
173 hash = hashf (name, len, HASHSIZE);
175 i = sizeof (HASHNODE) + len + 1;
176 hp = (HASHNODE *) xmalloc (i);
178 hp->bucket_hdr = &hashtab[bucket];
179 hp->next = hashtab[bucket];
180 hashtab[bucket] = hp;
182 if (hp->next != NULL)
186 if (hp->type == T_CONST)
187 hp->value.ival = ivalue;
189 hp->value.cpval = value;
190 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
193 for (i = 0; i < len; i++)