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