]> git.saurik.com Git - bison.git/blob - src/symtab.c
451c518f339475f9a4e60c21941f2e8ff89af639
[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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include "system.h"
23 #include "alloc.h"
24 #include "symtab.h"
25 #include "gram.h"
26
27
28 bucket *firstsymbol;
29 static bucket *lastsymbol;
30 static bucket **symtab;
31
32 static int
33 hash (const char *key)
34 {
35 register const char *cp;
36 register int k;
37
38 cp = key;
39 k = 0;
40 while (*cp)
41 k = ((k << 1) ^ (*cp++)) & 0x3fff;
42
43 return k % TABSIZE;
44 }
45
46
47
48 static char *
49 copys (const char *s)
50 {
51 register int i;
52 register const char *cp;
53 register char *result;
54
55 i = 1;
56 for (cp = s; *cp; cp++)
57 i++;
58
59 result = xmalloc((unsigned int)i);
60 strcpy(result, s);
61 return result;
62 }
63
64
65 void
66 tabinit (void)
67 {
68 /* register int i; JF unused */
69
70 symtab = NEW2(TABSIZE, bucket *);
71
72 firstsymbol = NULL;
73 lastsymbol = NULL;
74 }
75
76
77 bucket *
78 getsym (const char *key)
79 {
80 register int hashval;
81 register bucket *bp;
82 register int found;
83
84 hashval = hash(key);
85 bp = symtab[hashval];
86
87 found = 0;
88 while (bp != NULL && found == 0)
89 {
90 if (strcmp(key, bp->tag) == 0)
91 found = 1;
92 else
93 bp = bp->link;
94 }
95
96 if (found == 0)
97 {
98 nsyms++;
99
100 bp = NEW(bucket);
101 bp->link = symtab[hashval];
102 bp->next = NULL;
103 bp->tag = copys(key);
104 bp->class = SUNKNOWN;
105
106 if (firstsymbol == NULL)
107 {
108 firstsymbol = bp;
109 lastsymbol = bp;
110 }
111 else
112 {
113 lastsymbol->next = bp;
114 lastsymbol = bp;
115 }
116
117 symtab[hashval] = bp;
118 }
119
120 return bp;
121 }
122
123
124 void
125 free_symtab (void)
126 {
127 register int i;
128 register bucket *bp,*bptmp;/* JF don't use ptr after free */
129
130 for (i = 0; i < TABSIZE; i++)
131 {
132 bp = symtab[i];
133 while (bp)
134 {
135 bptmp = bp->link;
136 #if 0 /* This causes crashes because one string can appear more than once. */
137 if (bp->type_name)
138 FREE(bp->type_name);
139 #endif
140 FREE(bp);
141 bp = bptmp;
142 }
143 }
144 FREE(symtab);
145 }