]>
Commit | Line | Data |
---|---|---|
1 | /* Symbol table manager for Bison, | |
2 | Copyright (C) 1984, 1989, 2000, 2001, 2002 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 "hash.h" | |
24 | #include "symtab.h" | |
25 | #include "gram.h" | |
26 | ||
27 | /*---------------------------------. | |
28 | | Create a new symbol, named TAG. | | |
29 | `---------------------------------*/ | |
30 | ||
31 | static symbol_t * | |
32 | symbol_new (const char *tag) | |
33 | { | |
34 | symbol_t *res = XMALLOC (symbol_t, 1); | |
35 | ||
36 | res->tag = xstrdup (tag); | |
37 | res->type_name = NULL; | |
38 | res->number = NUMBER_UNDEFINED; | |
39 | res->prec = 0; | |
40 | res->assoc = right_assoc; | |
41 | res->user_token_number = USER_NUMBER_UNDEFINED; | |
42 | res->alias = NULL; | |
43 | res->class = unknown_sym; | |
44 | ||
45 | nsyms++; | |
46 | ||
47 | return res; | |
48 | } | |
49 | ||
50 | ||
51 | /*------------. | |
52 | | Free THIS. | | |
53 | `------------*/ | |
54 | ||
55 | static void | |
56 | symbol_free (symbol_t *this) | |
57 | { | |
58 | #if 0 | |
59 | /* This causes crashes because one string can appear more | |
60 | than once. */ | |
61 | XFREE (this->type_name); | |
62 | #endif | |
63 | XFREE (this->tag); | |
64 | XFREE (this); | |
65 | } | |
66 | ||
67 | ||
68 | ||
69 | /*----------------------. | |
70 | | A symbol_t hash table. | | |
71 | `----------------------*/ | |
72 | ||
73 | /* Initial capacity of symbols hash table. */ | |
74 | #define HT_INITIAL_CAPACITY 257 | |
75 | ||
76 | static struct hash_table *symbol_table = NULL; | |
77 | ||
78 | static bool | |
79 | hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2) | |
80 | { | |
81 | return strcmp (m1->tag, m2->tag) ? FALSE : TRUE; | |
82 | } | |
83 | ||
84 | static unsigned int | |
85 | hash_symbol_t (const symbol_t *m, unsigned int tablesize) | |
86 | { | |
87 | return hash_string (m->tag, tablesize); | |
88 | } | |
89 | ||
90 | ||
91 | /*-------------------------------. | |
92 | | Create the symbol_t hash table. | | |
93 | `-------------------------------*/ | |
94 | ||
95 | void | |
96 | symbols_new (void) | |
97 | { | |
98 | symbol_table = hash_initialize (HT_INITIAL_CAPACITY, | |
99 | NULL, | |
100 | (Hash_hasher) hash_symbol_t, | |
101 | (Hash_comparator) hash_compare_symbol_t, | |
102 | (Hash_data_freer) symbol_free); | |
103 | } | |
104 | ||
105 | ||
106 | /*----------------------------------------------------------------. | |
107 | | Find the symbol named KEY, and return it. If it does not exist | | |
108 | | yet, create it. | | |
109 | `----------------------------------------------------------------*/ | |
110 | ||
111 | symbol_t * | |
112 | getsym (const char *key) | |
113 | { | |
114 | symbol_t probe; | |
115 | symbol_t *entry; | |
116 | ||
117 | (const char *) probe.tag = key; | |
118 | entry = hash_lookup (symbol_table, &probe); | |
119 | ||
120 | if (!entry) | |
121 | { | |
122 | /* First insertion in the hash. */ | |
123 | entry = symbol_new (key); | |
124 | hash_insert (symbol_table, entry); | |
125 | } | |
126 | return entry; | |
127 | } | |
128 | ||
129 | ||
130 | /*-------------------. | |
131 | | Free the symbols. | | |
132 | `-------------------*/ | |
133 | ||
134 | void | |
135 | symbols_free (void) | |
136 | { | |
137 | hash_free (symbol_table); | |
138 | } | |
139 | ||
140 | ||
141 | /*---------------------------------------------------------------. | |
142 | | Look for undefined symbols, report an error, and consider them | | |
143 | | terminals. | | |
144 | `---------------------------------------------------------------*/ | |
145 | ||
146 | void | |
147 | symbols_do (symbol_processor processor, void *processor_data) | |
148 | { | |
149 | hash_do_for_each (symbol_table, | |
150 | (Hash_processor) processor, | |
151 | processor_data); | |
152 | } |