]> git.saurik.com Git - bison.git/blame - src/symtab.c
Don't define PACKAGE here, since config.h defines it.
[bison.git] / src / symtab.c
CommitLineData
40675e7c
DM
1/* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <stdio.h>
22#include "system.h"
7612000c 23#include "alloc.h"
40675e7c
DM
24#include "symtab.h"
25#include "gram.h"
26
27
28bucket **symtab;
29bucket *firstsymbol;
30bucket *lastsymbol;
31
d2729d44
JT
32void tabinit PARAMS((void));
33void free_symtab PARAMS((void));
40675e7c
DM
34
35
d2729d44
JT
36static int
37hash (char *key)
40675e7c
DM
38{
39 register char *cp;
40 register int k;
41
42 cp = key;
43 k = 0;
44 while (*cp)
45 k = ((k << 1) ^ (*cp++)) & 0x3fff;
46
47 return (k % TABSIZE);
48}
49
50
51
d2729d44
JT
52static char *
53copys (char *s)
40675e7c
DM
54{
55 register int i;
56 register char *cp;
57 register char *result;
58
59 i = 1;
60 for (cp = s; *cp; cp++)
61 i++;
62
63 result = xmalloc((unsigned int)i);
64 strcpy(result, s);
65 return (result);
66}
67
68
69void
d2729d44 70tabinit (void)
40675e7c
DM
71{
72/* register int i; JF unused */
73
74 symtab = NEW2(TABSIZE, bucket *);
75
76 firstsymbol = NULL;
77 lastsymbol = NULL;
78}
79
80
81bucket *
d2729d44 82getsym (char *key)
40675e7c
DM
83{
84 register int hashval;
85 register bucket *bp;
86 register int found;
87
88 hashval = hash(key);
89 bp = symtab[hashval];
90
91 found = 0;
92 while (bp != NULL && found == 0)
93 {
94 if (strcmp(key, bp->tag) == 0)
95 found = 1;
96 else
97 bp = bp->link;
98 }
99
100 if (found == 0)
101 {
102 nsyms++;
103
104 bp = NEW(bucket);
105 bp->link = symtab[hashval];
106 bp->next = NULL;
107 bp->tag = copys(key);
108 bp->class = SUNKNOWN;
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
124 return (bp);
125}
126
127
128void
d2729d44 129free_symtab (void)
40675e7c
DM
130{
131 register int i;
132 register bucket *bp,*bptmp;/* JF don't use ptr after free */
133
134 for (i = 0; i < TABSIZE; i++)
135 {
136 bp = symtab[i];
137 while (bp)
138 {
139 bptmp = bp->link;
140#if 0 /* This causes crashes because one string can appear more than once. */
141 if (bp->type_name)
142 FREE(bp->type_name);
143#endif
144 FREE(bp);
145 bp = bptmp;
146 }
147 }
148 FREE(symtab);
149}