]> git.saurik.com Git - bison.git/blame - src/symtab.c
* src/reader.c (copy_comment2): Expect the input stream to be on
[bison.git] / src / symtab.c
CommitLineData
40675e7c 1/* Symbol table manager for Bison,
95e36146 2 Copyright (C) 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
46
40675e7c 47void
d2729d44 48tabinit (void)
40675e7c 49{
d7913476 50 symtab = XCALLOC (bucket *, TABSIZE);
40675e7c
DM
51
52 firstsymbol = NULL;
53 lastsymbol = NULL;
54}
55
56
57bucket *
4a120d45 58getsym (const char *key)
40675e7c 59{
95e36146
AD
60 int hashval;
61 bucket *bp;
62 int found;
40675e7c 63
95e36146 64 hashval = hash (key);
40675e7c
DM
65 bp = symtab[hashval];
66
67 found = 0;
68 while (bp != NULL && found == 0)
69 {
95e36146 70 if (strcmp (key, bp->tag) == 0)
40675e7c
DM
71 found = 1;
72 else
73 bp = bp->link;
74 }
75
76 if (found == 0)
77 {
78 nsyms++;
79
d7913476 80 bp = XCALLOC (bucket, 1);
40675e7c
DM
81 bp->link = symtab[hashval];
82 bp->next = NULL;
95e36146 83 bp->tag = xstrdup (key);
d7020c20 84 bp->class = unknown_sym;
40675e7c
DM
85
86 if (firstsymbol == NULL)
87 {
88 firstsymbol = bp;
89 lastsymbol = bp;
90 }
91 else
92 {
93 lastsymbol->next = bp;
94 lastsymbol = bp;
95 }
96
97 symtab[hashval] = bp;
98 }
99
36281465 100 return bp;
40675e7c
DM
101}
102
103
104void
d2729d44 105free_symtab (void)
40675e7c 106{
95e36146
AD
107 int i;
108 bucket *bp, *bptmp; /* JF don't use ptr after free */
40675e7c
DM
109
110 for (i = 0; i < TABSIZE; i++)
111 {
112 bp = symtab[i];
113 while (bp)
114 {
115 bptmp = bp->link;
95e36146
AD
116#if 0
117 /* This causes crashes because one string can appear more
118 than once. */
40675e7c 119 if (bp->type_name)
95e36146 120 XFREE (bp->type_name);
40675e7c 121#endif
95e36146 122 XFREE (bp);
40675e7c
DM
123 bp = bptmp;
124 }
125 }
95e36146 126 XFREE (symtab);
40675e7c 127}