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