OSDN Git Service

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