]> git.saurik.com Git - bison.git/blob - src/symtab.c
Don't define PACKAGE here, since config.h defines it.
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <stdio.h>
22 #include "system.h"
23 #include "alloc.h"
24 #include "symtab.h"
25 #include "gram.h"
26
27
28 bucket **symtab;
29 bucket *firstsymbol;
30 bucket *lastsymbol;
31
32 void tabinit PARAMS((void));
33 void free_symtab PARAMS((void));
34
35
36 static int
37 hash (char *key)
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
52 static char *
53 copys (char *s)
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
69 void
70 tabinit (void)
71 {
72 /* register int i; JF unused */
73
74 symtab = NEW2(TABSIZE, bucket *);
75
76 firstsymbol = NULL;
77 lastsymbol = NULL;
78 }
79
80
81 bucket *
82 getsym (char *key)
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
128 void
129 free_symtab (void)
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 }