]> git.saurik.com Git - bison.git/blame - src/symtab.c
* src/bison.simple (YYSTACK_USE_ALLOCA): Changed to allow users
[bison.git] / src / symtab.c
CommitLineData
40675e7c 1/* Symbol table manager for Bison,
aa7815f5 2 Copyright 1984, 1989, 2000 Free Software Foundation, Inc.
40675e7c 3
95e36146 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
95e36146
AD
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.
40675e7c 10
95e36146
AD
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.
40675e7c 15
95e36146
AD
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. */
40675e7c
DM
20
21
40675e7c 22#include "system.h"
d7913476 23#include "xalloc.h"
40675e7c
DM
24#include "symtab.h"
25#include "gram.h"
26
27
40675e7c 28bucket *firstsymbol;
4a120d45
JT
29static bucket *lastsymbol;
30static bucket **symtab;
40675e7c 31
d2729d44 32static int
4a120d45 33hash (const char *key)
40675e7c 34{
95e36146
AD
35 const char *cp;
36 int k;
40675e7c
DM
37
38 cp = key;
39 k = 0;
40 while (*cp)
41 k = ((k << 1) ^ (*cp++)) & 0x3fff;
42
36281465 43 return k % TABSIZE;
40675e7c
DM
44}
45
1e9798d5
AD
46/*--------------------------------------------------------------.
47| Create a new symbol, named TAG, which hash value is HASHVAL. |
48`--------------------------------------------------------------*/
49
50static bucket *
51bucket_new (const char *tag, int hashval)
52{
53 bucket *res = XMALLOC (bucket, 1);
54
55 res->link = symtab[hashval];
56 res->next = NULL;
57 res->tag = xstrdup (tag);
58 res->type_name = NULL;
59 res->value = 0;
60 res->prec = 0;
61 res->assoc = right_assoc;
62 res->user_token_number = 0;
63 res->alias = NULL;
64 res->class = unknown_sym;
65
66 nsyms++;
67
68 return res;
69}
70
40675e7c 71
40675e7c 72void
d2729d44 73tabinit (void)
40675e7c 74{
d7913476 75 symtab = XCALLOC (bucket *, TABSIZE);
40675e7c
DM
76
77 firstsymbol = NULL;
78 lastsymbol = NULL;
79}
80
81
1e9798d5
AD
82/*----------------------------------------------------------------.
83| Find the symbol named KEY, and return it. If it does not exist |
84| yet, create it. |
85`----------------------------------------------------------------*/
86
40675e7c 87bucket *
4a120d45 88getsym (const char *key)
40675e7c 89{
95e36146
AD
90 int hashval;
91 bucket *bp;
92 int found;
40675e7c 93
95e36146 94 hashval = hash (key);
40675e7c
DM
95 bp = symtab[hashval];
96
97 found = 0;
98 while (bp != NULL && found == 0)
99 {
95e36146 100 if (strcmp (key, bp->tag) == 0)
40675e7c
DM
101 found = 1;
102 else
103 bp = bp->link;
104 }
105
106 if (found == 0)
107 {
1e9798d5 108 bp = bucket_new (key, hashval);
40675e7c
DM
109
110 if (firstsymbol == NULL)
111 {
112 firstsymbol = bp;
113 lastsymbol = bp;
114 }
115 else
116 {
117 lastsymbol->next = bp;
118 lastsymbol = bp;
119 }
120
121 symtab[hashval] = bp;
122 }
123
36281465 124 return bp;
40675e7c
DM
125}
126
127
128void
d2729d44 129free_symtab (void)
40675e7c 130{
95e36146
AD
131 int i;
132 bucket *bp, *bptmp; /* JF don't use ptr after free */
40675e7c
DM
133
134 for (i = 0; i < TABSIZE; i++)
135 {
136 bp = symtab[i];
137 while (bp)
138 {
139 bptmp = bp->link;
95e36146
AD
140#if 0
141 /* This causes crashes because one string can appear more
142 than once. */
40675e7c 143 if (bp->type_name)
95e36146 144 XFREE (bp->type_name);
40675e7c 145#endif
95e36146 146 XFREE (bp);
40675e7c
DM
147 bp = bptmp;
148 }
149 }
95e36146 150 XFREE (symtab);
40675e7c 151}