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 static HASHNODE *hashtab[HASHSIZE];
33 #define IS_IDCHAR(ch) is_idchar[(unsigned char)(ch)]
36 * return hash function on name. must be compatible with the one
37 * computed a step at a time, elsewhere
40 hashf(const char *name, int len, int hashsize)
45 r = HASHSTEP(r, *name++);
47 return MAKE_POS(r) % hashsize;
51 * find the most recent hash node for name name (ending with first
52 * non-identifier char) installed by install
54 * If LEN is >= 0, it is the length of the name.
55 * Otherwise, compute the length by scanning the entire name.
57 * If HASH is >= 0, it is the precomputed hash code.
58 * Otherwise, compute the hash code.
61 cpp_lookup(const char *name, int len, int hash)
68 for (bp = name; IS_IDCHAR(*bp); bp++)
73 hash = hashf(name, len, HASHSIZE);
75 bucket = hashtab[hash];
78 if (bucket->length == len
79 && strncmp((const char *)bucket->name, name, len) == 0)
81 bucket = bucket->next;
83 return (HASHNODE *) 0;
87 * Delete a hash node. Some weirdness to free junk from macros.
88 * More such weirdness will have to be added if you define more hash
92 /* Note that the DEFINITION of a macro is removed from the hash table
93 * but its storage is not freed. This would be a storage leak
94 * except that it is not reasonable to keep undefining and redefining
95 * large numbers of macros many times.
96 * In any case, this is necessary, because a macro can be #undef'd
97 * in the middle of reading the arguments to a call to it.
98 * If #undef freed the DEFINITION, that would crash. */
101 delete_macro(HASHNODE * hp)
104 if (hp->prev != NULL)
105 hp->prev->next = hp->next;
106 if (hp->next != NULL)
107 hp->next->prev = hp->prev;
109 /* make sure that the bucket chain header that
110 * the deleted guy was on points to the right thing afterwards. */
111 if (hp == *hp->bucket_hdr)
112 *hp->bucket_hdr = hp->next;
114 if (hp->type == T_MACRO)
116 DEFINITION *d = hp->value.defn;
117 struct reflist *ap, *nextap;
119 for (ap = d->pattern; ap != NULL; ap = nextap)
125 free(d->args.argnames);
131 * install a name in the main hash table, even if it is already there.
132 * name stops with first non alphanumeric, except leading '#'.
133 * caller must check against redefinition if that is desired.
134 * delete_macro () removes things installed by install () in fifo order.
135 * this is important because of the `defined' special symbol used
136 * in #if, and also if pushdef/popdef directives are ever implemented.
138 * If LEN is >= 0, it is the length of the name.
139 * Otherwise, compute the length by scanning the entire name.
141 * If HASH is >= 0, it is the precomputed hash code.
142 * Otherwise, compute the hash code.
145 install(const char *name, int len, enum node_type type, int ivalue, char *value,
155 while (IS_IDCHAR(*p))
160 hash = hashf(name, len, HASHSIZE);
162 i = sizeof(HASHNODE) + len + 1;
163 hp = (HASHNODE *) xmalloc(i);
165 hp->bucket_hdr = &hashtab[bucket];
166 hp->next = hashtab[bucket];
167 hashtab[bucket] = hp;
169 if (hp->next != NULL)
173 if (hp->type == T_CONST)
174 hp->value.ival = ivalue;
176 hp->value.cpval = value;
177 hp->name = ((char *)hp) + sizeof(HASHNODE);
178 memcpy(hp->name, name, len);
184 cpp_hash_cleanup(cpp_reader * pfile)
189 for (i = HASHSIZE; --i >= 0;)
192 delete_macro(hashtab[i]);