]>
Commit | Line | Data |
---|---|---|
7f2935c7 PB |
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 | |
6 | ||
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 | |
10 | later version. | |
11 | ||
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. | |
16 | ||
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. | |
20 | ||
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! */ | |
24 | ||
25 | #include "cpplib.h" | |
26 | #include "cpphash.h" | |
27 | ||
28 | /* Define a generic NULL if one hasn't already been defined. */ | |
29 | ||
30 | #ifndef NULL | |
31 | #define NULL 0 | |
32 | #endif | |
33 | ||
34 | /* | |
35 | * return hash function on name. must be compatible with the one | |
36 | * computed a step at a time, elsewhere | |
37 | */ | |
38 | int | |
39 | hashf (name, len, hashsize) | |
40 | register const U_CHAR *name; | |
41 | register int len; | |
42 | int hashsize; | |
43 | { | |
44 | register int r = 0; | |
45 | ||
46 | while (len--) | |
47 | r = HASHSTEP (r, *name++); | |
48 | ||
49 | return MAKE_POS (r) % hashsize; | |
50 | } | |
51 | ||
52 | /* | |
53 | * find the most recent hash node for name name (ending with first | |
54 | * non-identifier char) installed by install | |
55 | * | |
56 | * If LEN is >= 0, it is the length of the name. | |
57 | * Otherwise, compute the length by scanning the entire name. | |
58 | * | |
59 | * If HASH is >= 0, it is the precomputed hash code. | |
60 | * Otherwise, compute the hash code. | |
61 | */ | |
62 | HASHNODE * | |
63 | cpp_lookup (pfile, name, len, hash) | |
64 | struct parse_file *pfile; | |
65 | const U_CHAR *name; | |
66 | int len; | |
67 | int hash; | |
68 | { | |
69 | register const U_CHAR *bp; | |
70 | register HASHNODE *bucket; | |
71 | ||
72 | if (len < 0) | |
73 | { | |
74 | for (bp = name; is_idchar[*bp]; bp++) ; | |
75 | len = bp - name; | |
76 | } | |
77 | ||
78 | if (hash < 0) | |
79 | hash = hashf (name, len, HASHSIZE); | |
80 | ||
81 | bucket = hashtab[hash]; | |
82 | while (bucket) { | |
83 | if (bucket->length == len && strncmp (bucket->name, name, len) == 0) | |
84 | return bucket; | |
85 | bucket = bucket->next; | |
86 | } | |
87 | return (HASHNODE*) 0; | |
88 | } | |
89 | ||
90 | /* | |
91 | * Delete a hash node. Some weirdness to free junk from macros. | |
92 | * More such weirdness will have to be added if you define more hash | |
93 | * types that need it. | |
94 | */ | |
95 | ||
96 | /* Note that the DEFINITION of a macro is removed from the hash table | |
97 | but its storage is not freed. This would be a storage leak | |
98 | except that it is not reasonable to keep undefining and redefining | |
99 | large numbers of macros many times. | |
100 | In any case, this is necessary, because a macro can be #undef'd | |
101 | in the middle of reading the arguments to a call to it. | |
102 | If #undef freed the DEFINITION, that would crash. */ | |
103 | ||
104 | void | |
105 | delete_macro (hp) | |
106 | HASHNODE *hp; | |
107 | { | |
108 | ||
109 | if (hp->prev != NULL) | |
110 | hp->prev->next = hp->next; | |
111 | if (hp->next != NULL) | |
112 | hp->next->prev = hp->prev; | |
113 | ||
114 | /* make sure that the bucket chain header that | |
115 | the deleted guy was on points to the right thing afterwards. */ | |
116 | if (hp == *hp->bucket_hdr) | |
117 | *hp->bucket_hdr = hp->next; | |
118 | ||
119 | #if 0 | |
120 | if (hp->type == T_MACRO) { | |
121 | DEFINITION *d = hp->value.defn; | |
122 | struct reflist *ap, *nextap; | |
123 | ||
124 | for (ap = d->pattern; ap != NULL; ap = nextap) { | |
125 | nextap = ap->next; | |
126 | free (ap); | |
127 | } | |
128 | free (d); | |
129 | } | |
130 | #endif | |
131 | free (hp); | |
132 | } | |
133 | /* | |
134 | * install a name in the main hash table, even if it is already there. | |
135 | * name stops with first non alphanumeric, except leading '#'. | |
136 | * caller must check against redefinition if that is desired. | |
137 | * delete_macro () removes things installed by install () in fifo order. | |
138 | * this is important because of the `defined' special symbol used | |
139 | * in #if, and also if pushdef/popdef directives are ever implemented. | |
140 | * | |
141 | * If LEN is >= 0, it is the length of the name. | |
142 | * Otherwise, compute the length by scanning the entire name. | |
143 | * | |
144 | * If HASH is >= 0, it is the precomputed hash code. | |
145 | * Otherwise, compute the hash code. | |
146 | */ | |
147 | HASHNODE * | |
148 | install (name, len, type, ivalue, value, hash) | |
149 | U_CHAR *name; | |
150 | int len; | |
151 | enum node_type type; | |
152 | int ivalue; | |
153 | char *value; | |
154 | int hash; | |
155 | { | |
156 | register HASHNODE *hp; | |
157 | register int i, bucket; | |
158 | register U_CHAR *p, *q; | |
159 | ||
160 | if (len < 0) { | |
161 | p = name; | |
162 | while (is_idchar[*p]) | |
163 | p++; | |
164 | len = p - name; | |
165 | } | |
166 | ||
167 | if (hash < 0) | |
168 | hash = hashf (name, len, HASHSIZE); | |
169 | ||
170 | i = sizeof (HASHNODE) + len + 1; | |
171 | hp = (HASHNODE *) xmalloc (i); | |
172 | bucket = hash; | |
173 | hp->bucket_hdr = &hashtab[bucket]; | |
174 | hp->next = hashtab[bucket]; | |
175 | hashtab[bucket] = hp; | |
176 | hp->prev = NULL; | |
177 | if (hp->next != NULL) | |
178 | hp->next->prev = hp; | |
179 | hp->type = type; | |
180 | hp->length = len; | |
181 | if (hp->type == T_CONST) | |
182 | hp->value.ival = ivalue; | |
183 | else | |
184 | hp->value.cpval = value; | |
185 | hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE); | |
186 | p = hp->name; | |
187 | q = name; | |
188 | for (i = 0; i < len; i++) | |
189 | *p++ = *q++; | |
190 | hp->name[len] = 0; | |
191 | return hp; | |
192 | } |