]> git.saurik.com Git - bison.git/blob - src/symtab.c
Update.
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison,
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include "system.h"
23 #include "symtab.h"
24 #include "gram.h"
25
26
27 bucket *firstsymbol;
28 static bucket *lastsymbol;
29 static bucket **symtab;
30
31 static int
32 hash (const char *key)
33 {
34 const char *cp;
35 int k;
36
37 cp = key;
38 k = 0;
39 while (*cp)
40 k = ((k << 1) ^ (*cp++)) & 0x3fff;
41
42 return k % TABSIZE;
43 }
44
45 /*--------------------------------------------------------------.
46 | Create a new symbol, named TAG, which hash value is HASHVAL. |
47 `--------------------------------------------------------------*/
48
49 static bucket *
50 bucket_new (const char *tag, int hashval)
51 {
52 /* Hack, until we have a Bison parser. */
53 extern int lineno;
54
55 bucket *res = XMALLOC (bucket, 1);
56
57 res->link = symtab[hashval];
58 res->next = NULL;
59 res->tag = xstrdup (tag);
60 res->type_name = NULL;
61 res->value = 0;
62 res->prec = 0;
63 res->assoc = right_assoc;
64 res->user_token_number = SUNDEF;
65 res->alias = NULL;
66 res->class = unknown_sym;
67 res->line = lineno;
68
69 nsyms++;
70
71 return res;
72 }
73
74
75 void
76 tabinit (void)
77 {
78 symtab = XCALLOC (bucket *, TABSIZE);
79
80 firstsymbol = NULL;
81 lastsymbol = NULL;
82 }
83
84
85 /*----------------------------------------------------------------.
86 | Find the symbol named KEY, and return it. If it does not exist |
87 | yet, create it. |
88 `----------------------------------------------------------------*/
89
90 bucket *
91 getsym (const char *key)
92 {
93 int hashval;
94 bucket *bp;
95 int found;
96
97 hashval = hash (key);
98 bp = symtab[hashval];
99
100 found = 0;
101 while (bp != NULL && found == 0)
102 {
103 if (strcmp (key, bp->tag) == 0)
104 found = 1;
105 else
106 bp = bp->link;
107 }
108
109 if (found == 0)
110 {
111 bp = bucket_new (key, hashval);
112
113 if (firstsymbol == NULL)
114 {
115 firstsymbol = bp;
116 lastsymbol = bp;
117 }
118 else
119 {
120 lastsymbol->next = bp;
121 lastsymbol = bp;
122 }
123
124 symtab[hashval] = bp;
125 }
126
127 return bp;
128 }
129
130
131 void
132 free_symtab (void)
133 {
134 int i;
135 bucket *bp, *bptmp; /* JF don't use ptr after free */
136
137 for (i = 0; i < TABSIZE; i++)
138 {
139 bp = symtab[i];
140 while (bp)
141 {
142 bptmp = bp->link;
143 #if 0
144 /* This causes crashes because one string can appear more
145 than once. */
146 if (bp->type_name)
147 XFREE (bp->type_name);
148 #endif
149 XFREE (bp->tag);
150 XFREE (bp);
151 bp = bptmp;
152 }
153 }
154 XFREE (symtab);
155 }