]>
Commit | Line | Data |
---|---|---|
1 | /* Symbol table manager for Bison, | |
2 | Copyright (C) 1984, 1989, 2000 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 "xalloc.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 | const char *cp; | |
36 | 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 | void | |
48 | tabinit (void) | |
49 | { | |
50 | symtab = XCALLOC (bucket *, TABSIZE); | |
51 | ||
52 | firstsymbol = NULL; | |
53 | lastsymbol = NULL; | |
54 | } | |
55 | ||
56 | ||
57 | bucket * | |
58 | getsym (const char *key) | |
59 | { | |
60 | int hashval; | |
61 | bucket *bp; | |
62 | int found; | |
63 | ||
64 | hashval = hash (key); | |
65 | bp = symtab[hashval]; | |
66 | ||
67 | found = 0; | |
68 | while (bp != NULL && found == 0) | |
69 | { | |
70 | if (strcmp (key, bp->tag) == 0) | |
71 | found = 1; | |
72 | else | |
73 | bp = bp->link; | |
74 | } | |
75 | ||
76 | if (found == 0) | |
77 | { | |
78 | nsyms++; | |
79 | ||
80 | bp = XCALLOC (bucket, 1); | |
81 | bp->link = symtab[hashval]; | |
82 | bp->next = NULL; | |
83 | bp->tag = xstrdup (key); | |
84 | bp->class = unknown_sym; | |
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 | ||
100 | return bp; | |
101 | } | |
102 | ||
103 | ||
104 | void | |
105 | free_symtab (void) | |
106 | { | |
107 | int i; | |
108 | bucket *bp, *bptmp; /* JF don't use ptr after free */ | |
109 | ||
110 | for (i = 0; i < TABSIZE; i++) | |
111 | { | |
112 | bp = symtab[i]; | |
113 | while (bp) | |
114 | { | |
115 | bptmp = bp->link; | |
116 | #if 0 | |
117 | /* This causes crashes because one string can appear more | |
118 | than once. */ | |
119 | if (bp->type_name) | |
120 | XFREE (bp->type_name); | |
121 | #endif | |
122 | XFREE (bp); | |
123 | bp = bptmp; | |
124 | } | |
125 | } | |
126 | XFREE (symtab); | |
127 | } |