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