]>
Commit | Line | Data |
---|---|---|
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 | 27 | bucket *firstsymbol; |
4a120d45 JT |
28 | static bucket *lastsymbol; |
29 | static bucket **symtab; | |
40675e7c | 30 | |
d2729d44 | 31 | static int |
4a120d45 | 32 | hash (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 | ||
49 | static bucket * | |
50 | bucket_new (const char *tag, int hashval) | |
51 | { | |
e41dc700 AD |
52 | /* Hack, until we have a Bison parser. */ |
53 | extern int lineno; | |
54 | ||
1e9798d5 AD |
55 | bucket *res = XMALLOC (bucket, 1); |
56 | ||
57 | res->link = symtab[hashval]; | |
58 | res->next = NULL; | |
59 | res->tag = xstrdup (tag); | |
60 | res->type_name = NULL; | |
61 | res->value = 0; | |
62 | res->prec = 0; | |
63 | res->assoc = right_assoc; | |
6b7e85b9 | 64 | res->user_token_number = SUNDEF; |
1e9798d5 AD |
65 | res->alias = NULL; |
66 | res->class = unknown_sym; | |
e41dc700 | 67 | res->line = lineno; |
1e9798d5 AD |
68 | |
69 | nsyms++; | |
70 | ||
71 | return res; | |
72 | } | |
73 | ||
40675e7c | 74 | |
40675e7c | 75 | void |
d2729d44 | 76 | tabinit (void) |
40675e7c | 77 | { |
d7913476 | 78 | symtab = XCALLOC (bucket *, TABSIZE); |
40675e7c DM |
79 | |
80 | firstsymbol = NULL; | |
81 | lastsymbol = NULL; | |
82 | } | |
83 | ||
84 | ||
1e9798d5 AD |
85 | /*----------------------------------------------------------------. |
86 | | Find the symbol named KEY, and return it. If it does not exist | | |
87 | | yet, create it. | | |
88 | `----------------------------------------------------------------*/ | |
89 | ||
40675e7c | 90 | bucket * |
4a120d45 | 91 | getsym (const char *key) |
40675e7c | 92 | { |
95e36146 AD |
93 | int hashval; |
94 | bucket *bp; | |
95 | int found; | |
40675e7c | 96 | |
95e36146 | 97 | hashval = hash (key); |
40675e7c DM |
98 | bp = symtab[hashval]; |
99 | ||
100 | found = 0; | |
101 | while (bp != NULL && found == 0) | |
102 | { | |
95e36146 | 103 | if (strcmp (key, bp->tag) == 0) |
40675e7c DM |
104 | found = 1; |
105 | else | |
106 | bp = bp->link; | |
107 | } | |
108 | ||
109 | if (found == 0) | |
110 | { | |
1e9798d5 | 111 | bp = bucket_new (key, hashval); |
40675e7c DM |
112 | |
113 | if (firstsymbol == NULL) | |
114 | { | |
115 | firstsymbol = bp; | |
116 | lastsymbol = bp; | |
117 | } | |
118 | else | |
119 | { | |
120 | lastsymbol->next = bp; | |
121 | lastsymbol = bp; | |
122 | } | |
123 | ||
124 | symtab[hashval] = bp; | |
125 | } | |
126 | ||
36281465 | 127 | return bp; |
40675e7c DM |
128 | } |
129 | ||
130 | ||
131 | void | |
d2729d44 | 132 | free_symtab (void) |
40675e7c | 133 | { |
95e36146 AD |
134 | int i; |
135 | bucket *bp, *bptmp; /* JF don't use ptr after free */ | |
40675e7c DM |
136 | |
137 | for (i = 0; i < TABSIZE; i++) | |
138 | { | |
139 | bp = symtab[i]; | |
140 | while (bp) | |
141 | { | |
142 | bptmp = bp->link; | |
95e36146 AD |
143 | #if 0 |
144 | /* This causes crashes because one string can appear more | |
145 | than once. */ | |
40675e7c | 146 | if (bp->type_name) |
95e36146 | 147 | XFREE (bp->type_name); |
40675e7c | 148 | #endif |
342b8b6e | 149 | XFREE (bp->tag); |
95e36146 | 150 | XFREE (bp); |
40675e7c DM |
151 | bp = bptmp; |
152 | } | |
153 | } | |
95e36146 | 154 | XFREE (symtab); |
40675e7c | 155 | } |