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