OSDN Git Service

* cpphash.c: Don't use const on compilers that don't support it.
[pf3gnuchains/gcc-fork.git] / gcc / cpphash.c
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 #ifndef __STDC__
35 #define const
36 #define volatile
37 #endif
38
39 /*
40  * return hash function on name.  must be compatible with the one
41  * computed a step at a time, elsewhere
42  */
43 int
44 hashf (name, len, hashsize)
45      register const U_CHAR *name;
46      register int len;
47      int hashsize;
48 {
49   register int r = 0;
50
51   while (len--)
52     r = HASHSTEP (r, *name++);
53
54   return MAKE_POS (r) % hashsize;
55 }
56
57 /*
58  * find the most recent hash node for name name (ending with first
59  * non-identifier char) installed by install
60  *
61  * If LEN is >= 0, it is the length of the name.
62  * Otherwise, compute the length by scanning the entire name.
63  *
64  * If HASH is >= 0, it is the precomputed hash code.
65  * Otherwise, compute the hash code.
66  */
67 HASHNODE *
68 cpp_lookup (pfile, name, len, hash)
69      struct parse_file *pfile;
70      const U_CHAR *name;
71      int len;
72      int hash;
73 {
74   register const U_CHAR *bp;
75   register HASHNODE *bucket;
76
77   if (len < 0)
78     {
79       for (bp = name; is_idchar[*bp]; bp++) ;
80       len = bp - name;
81     }
82
83   if (hash < 0)
84     hash = hashf (name, len, HASHSIZE);
85
86   bucket = hashtab[hash];
87   while (bucket) {
88     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
89       return bucket;
90     bucket = bucket->next;
91   }
92   return (HASHNODE*) 0;
93 }
94
95 /*
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
98  * types that need it.
99  */
100
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.  */
108
109 void
110 delete_macro (hp)
111      HASHNODE *hp;
112 {
113
114   if (hp->prev != NULL)
115     hp->prev->next = hp->next;
116   if (hp->next != NULL)
117     hp->next->prev = hp->prev;
118
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;
123
124 #if 0
125   if (hp->type == T_MACRO) {
126     DEFINITION *d = hp->value.defn;
127     struct reflist *ap, *nextap;
128
129     for (ap = d->pattern; ap != NULL; ap = nextap) {
130       nextap = ap->next;
131       free (ap);
132     }
133     free (d);
134   }
135 #endif
136   free (hp);
137 }
138 /*
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.
145  *
146  * If LEN is >= 0, it is the length of the name.
147  * Otherwise, compute the length by scanning the entire name.
148  *
149  * If HASH is >= 0, it is the precomputed hash code.
150  * Otherwise, compute the hash code.
151  */
152 HASHNODE *
153 install (name, len, type, ivalue, value, hash)
154      U_CHAR *name;
155      int len;
156      enum node_type type;
157      int ivalue;
158      char *value;
159      int hash;
160 {
161   register HASHNODE *hp;
162   register int i, bucket;
163   register U_CHAR *p, *q;
164
165   if (len < 0) {
166     p = name;
167     while (is_idchar[*p])
168       p++;
169     len = p - name;
170   }
171
172   if (hash < 0)
173     hash = hashf (name, len, HASHSIZE);
174
175   i = sizeof (HASHNODE) + len + 1;
176   hp = (HASHNODE *) xmalloc (i);
177   bucket = hash;
178   hp->bucket_hdr = &hashtab[bucket];
179   hp->next = hashtab[bucket];
180   hashtab[bucket] = hp;
181   hp->prev = NULL;
182   if (hp->next != NULL)
183     hp->next->prev = hp;
184   hp->type = type;
185   hp->length = len;
186   if (hp->type == T_CONST)
187     hp->value.ival = ivalue;
188   else
189     hp->value.cpval = value;
190   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
191   p = hp->name;
192   q = name;
193   for (i = 0; i < len; i++)
194     *p++ = *q++;
195   hp->name[len] = 0;
196   return hp;
197 }