]> git.saurik.com Git - bison.git/blame - src/symtab.c
(struniq_assert): Now returns void, and aborts if the assertion is false.
[bison.git] / src / symtab.c
CommitLineData
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"
6b98e4b5 23#include "quotearg.h"
72a23c97 24#include "hash.h"
2f1afb73 25#include "complain.h"
40675e7c
DM
26#include "symtab.h"
27#include "gram.h"
28
2f1afb73
AD
29/*------------------------.
30| Distinguished symbols. |
31`------------------------*/
32
33symbol_t *errtoken = NULL;
34symbol_t *undeftoken = NULL;
88bce5a2
AD
35symbol_t *endtoken = NULL;
36symbol_t *accept = NULL;
2f1afb73 37symbol_t *startsymbol = NULL;
8efe435c 38location_t startsymbol_location;
2f1afb73 39
72a23c97
AD
40/*---------------------------------.
41| Create a new symbol, named TAG. |
42`---------------------------------*/
1e9798d5 43
db8837cb 44static symbol_t *
95612cfa 45symbol_new (struniq_t tag, location_t location)
1e9798d5 46{
db8837cb 47 symbol_t *res = XMALLOC (symbol_t, 1);
1e9798d5 48
95612cfa
AD
49 struniq_assert (tag);
50 res->tag = tag;
24c0aad7
AD
51 res->location = location;
52
1e9798d5 53 res->type_name = NULL;
9280d3ef 54 res->destructor = NULL;
260008e5 55 res->printer = NULL;
24c0aad7 56
5fbb0954 57 res->number = NUMBER_UNDEFINED;
1e9798d5 58 res->prec = 0;
a945ec39 59 res->assoc = undef_assoc;
b87f8b21 60 res->user_token_number = USER_NUMBER_UNDEFINED;
260008e5 61
1e9798d5
AD
62 res->alias = NULL;
63 res->class = unknown_sym;
64
65 nsyms++;
1e9798d5
AD
66 return res;
67}
68
40675e7c 69
e9955c83
AD
70/*------------------------------------------------------------------.
71| Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
72| as TYPE_NAME. |
73`------------------------------------------------------------------*/
3ae2b51f
AD
74
75void
95612cfa 76symbol_type_set (symbol_t *symbol, struniq_t type_name, location_t location)
3ae2b51f 77{
e9955c83
AD
78 if (type_name)
79 {
80 if (symbol->type_name)
1e0bab92 81 complain_at (location,
97650f4e 82 _("type redeclaration for %s"), symbol->tag);
95612cfa 83 struniq_assert (type_name);
e9955c83
AD
84 symbol->type_name = type_name;
85 }
3ae2b51f
AD
86}
87
88
9280d3ef 89/*-------------------------------------------------------------------.
366eea36 90| Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
9280d3ef
AD
91`-------------------------------------------------------------------*/
92
93void
1a31ed21 94symbol_destructor_set (symbol_t *symbol, char *destructor, location_t location)
9280d3ef
AD
95{
96 if (destructor)
97 {
98 if (symbol->destructor)
99 complain_at (location,
366eea36 100 _("%s redeclaration for %s"),
97650f4e 101 "%destructor", symbol->tag);
9280d3ef 102 symbol->destructor = destructor;
24c0aad7 103 symbol->destructor_location = location;
9280d3ef
AD
104 }
105}
106
107
366eea36 108/*----------------------------------------------------------------.
b9e00562 109| Set the PRINTER associated to SYMBOL. Do nothing if passed 0. |
366eea36
AD
110`----------------------------------------------------------------*/
111
112void
113symbol_printer_set (symbol_t *symbol, char *printer, location_t location)
114{
115 if (printer)
116 {
117 if (symbol->printer)
118 complain_at (location,
119 _("%s redeclaration for %s"),
97650f4e 120 "%printer", symbol->tag);
366eea36
AD
121 symbol->printer = printer;
122 symbol->printer_location = location;
123 }
124}
125
126
e9955c83
AD
127/*------------------------------------------------------------------.
128| Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
129| with UNDEF_ASSOC as ASSOC. |
130`------------------------------------------------------------------*/
3ae2b51f
AD
131
132void
1a31ed21 133symbol_precedence_set (symbol_t *symbol,
a945ec39 134 int prec, assoc_t assoc, location_t location)
3ae2b51f 135{
e9955c83
AD
136 if (assoc != undef_assoc)
137 {
138 if (symbol->prec != 0)
1e0bab92 139 complain_at (location,
dafdc66f 140 _("redefining precedence of %s"),
97650f4e 141 symbol->tag);
e9955c83
AD
142 symbol->prec = prec;
143 symbol->assoc = assoc;
144 }
145
146 /* Only terminals have a precedence. */
e776192e 147 symbol_class_set (symbol, token_sym, location);
3ae2b51f
AD
148}
149
150
44536b35
AD
151/*-------------------------------------.
152| Set the CLASS associated to SYMBOL. |
153`-------------------------------------*/
154
155void
e776192e 156symbol_class_set (symbol_t *symbol, symbol_class class, location_t location)
44536b35
AD
157{
158 if (symbol->class != unknown_sym && symbol->class != class)
97650f4e 159 complain_at (location, _("symbol %s redefined"), symbol->tag);
44536b35
AD
160
161 if (class == nterm_sym && symbol->class != nterm_sym)
162 symbol->number = nvars++;
163 else if (class == token_sym && symbol->number == NUMBER_UNDEFINED)
164 symbol->number = ntokens++;
165
166 symbol->class = class;
167}
168
169
170/*-------------------------------------------------.
171| Set the USER_TOKEN_NUMBER associated to SYMBOL. |
172`-------------------------------------------------*/
173
174void
e776192e
AD
175symbol_user_token_number_set (symbol_t *symbol,
176 int user_token_number, location_t location)
44536b35
AD
177{
178 assert (symbol->class == token_sym);
179
5e424082
AD
180 if (symbol->user_token_number != USER_NUMBER_UNDEFINED
181 && symbol->user_token_number != user_token_number)
e776192e 182 complain_at (location, _("redefining user token number of %s"),
97650f4e 183 symbol->tag);
44536b35
AD
184
185 symbol->user_token_number = user_token_number;
88bce5a2 186 /* User defined $end token? */
44536b35
AD
187 if (user_token_number == 0)
188 {
88bce5a2
AD
189 endtoken = symbol;
190 endtoken->number = 0;
44536b35
AD
191 /* It is always mapped to 0, so it was already counted in
192 NTOKENS. */
193 --ntokens;
194 }
195}
196
197
72a23c97
AD
198/*------------.
199| Free THIS. |
200`------------*/
201
202static void
db8837cb 203symbol_free (symbol_t *this)
40675e7c 204{
3e6656f9 205 free (this);
72a23c97
AD
206}
207
208
2f1afb73
AD
209/*-----------------------------------------------------------.
210| If THIS is not defined, report an error, and consider it a |
211| nonterminal. |
212`-----------------------------------------------------------*/
213
214static bool
215symbol_check_defined (symbol_t *this)
216{
217 if (this->class == unknown_sym)
218 {
ee000ba4
AD
219 complain_at
220 (this->location,
221 _("symbol %s is used, but is not defined as a token and has no rules"),
97650f4e 222 this->tag);
2f1afb73
AD
223 this->class = nterm_sym;
224 this->number = nvars++;
225 }
226
a3714bce 227 return true;
2f1afb73
AD
228}
229
230
231/*-------------------------------------------------------------------.
232| Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
233| with TYPENAME. |
234`-------------------------------------------------------------------*/
235
236void
a5d50994 237symbol_make_alias (symbol_t *symbol, symbol_t *symval, location_t loc)
2f1afb73
AD
238{
239 if (symval->alias)
a5d50994 240 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
97650f4e 241 symval->tag);
2f1afb73 242 else if (symbol->alias)
a5d50994 243 warn_at (loc, _("symbol `%s' given more than one literal string"),
97650f4e 244 symbol->tag);
2f1afb73
AD
245 else
246 {
247 symval->class = token_sym;
2f1afb73
AD
248 symval->user_token_number = symbol->user_token_number;
249 symbol->user_token_number = USER_NUMBER_ALIAS;
250 symval->alias = symbol;
251 symbol->alias = symval;
252 /* symbol and symval combined are only one symbol */
253 nsyms--;
254 ntokens--;
255 assert (ntokens == symbol->number || ntokens == symval->number);
256 symbol->number = symval->number =
257 (symval->number < symbol->number) ? symval->number : symbol->number;
258 }
259}
260
261
262/*---------------------------------------------------------.
263| Check that THIS, and its alias, have same precedence and |
264| associativity. |
265`---------------------------------------------------------*/
266
267static bool
268symbol_check_alias_consistence (symbol_t *this)
269{
270 /* Check only those who _are_ the aliases. */
271 if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
272 {
273 if (this->prec != this->alias->prec)
274 {
275 if (this->prec != 0 && this->alias->prec != 0)
a5d50994
AD
276 complain_at (this->alias->location,
277 _("conflicting precedences for %s and %s"),
278 this->tag, this->alias->tag);
2f1afb73
AD
279 if (this->prec != 0)
280 this->alias->prec = this->prec;
281 else
282 this->prec = this->alias->prec;
283 }
284
285 if (this->assoc != this->alias->assoc)
286 {
a945ec39 287 if (this->assoc != undef_assoc && this->alias->assoc != undef_assoc)
a5d50994 288 complain_at (this->alias->location,
a945ec39
AD
289 _("conflicting associativities for %s (%s) and %s (%s)"),
290 this->tag, assoc_to_string (this->assoc),
291 this->alias->tag, assoc_to_string (this->alias->assoc));
292 if (this->assoc != undef_assoc)
2f1afb73
AD
293 this->alias->assoc = this->assoc;
294 else
295 this->assoc = this->alias->assoc;
296 }
297 }
a3714bce 298 return true;
2f1afb73
AD
299}
300
301
302/*-------------------------------------------------------------------.
303| Assign a symbol number, and write the definition of the token name |
304| into FDEFINES. Put in SYMBOLS. |
305`-------------------------------------------------------------------*/
306
307static bool
308symbol_pack (symbol_t *this)
309{
310 if (this->class == nterm_sym)
311 {
312 this->number += ntokens;
313 }
314 else if (this->alias)
315 {
316 /* This symbol and its alias are a single token defn.
317 Allocate a tokno, and assign to both check agreement of
318 prec and assoc fields and make both the same */
319 if (this->number == NUMBER_UNDEFINED)
320 {
88bce5a2 321 if (this == endtoken || this->alias == endtoken)
2f1afb73
AD
322 this->number = this->alias->number = 0;
323 else
324 {
325 assert (this->alias->number != NUMBER_UNDEFINED);
326 this->number = this->alias->number;
327 }
328 }
b9e00562 329 /* Do not do processing below for USER_NUMBER_ALIASes. */
2f1afb73 330 if (this->user_token_number == USER_NUMBER_ALIAS)
a3714bce 331 return true;
2f1afb73
AD
332 }
333 else /* this->class == token_sym */
334 {
335 assert (this->number != NUMBER_UNDEFINED);
336 }
337
338 symbols[this->number] = this;
a3714bce 339 return true;
2f1afb73
AD
340}
341
342
343
344
345/*--------------------------------------------------.
346| Put THIS in TOKEN_TRANSLATIONS if it is a token. |
347`--------------------------------------------------*/
348
349static bool
350symbol_translation (symbol_t *this)
351{
352 /* Non-terminal? */
353 if (this->class == token_sym
354 && this->user_token_number != USER_NUMBER_ALIAS)
355 {
356 /* A token which translation has already been set? */
357 if (token_translations[this->user_token_number] != undeftoken->number)
a5d50994
AD
358 complain_at (this->location,
359 _("tokens %s and %s both assigned number %d"),
360 symbols[token_translations[this->user_token_number]]->tag,
361 this->tag, this->user_token_number);
2f1afb73
AD
362
363 token_translations[this->user_token_number] = this->number;
364 }
365
a3714bce 366 return true;
2f1afb73
AD
367}
368
72a23c97
AD
369
370/*----------------------.
2f1afb73 371| A symbol hash table. |
72a23c97 372`----------------------*/
40675e7c 373
db8837cb 374/* Initial capacity of symbols hash table. */
72a23c97
AD
375#define HT_INITIAL_CAPACITY 257
376
db8837cb 377static struct hash_table *symbol_table = NULL;
72a23c97
AD
378
379static bool
95612cfa 380hash_compare_symbol (const symbol_t *m1, const symbol_t *m2)
72a23c97 381{
95612cfa
AD
382 /* Since tags are unique, we can compare the pointers themselves. */
383 return STRUNIQ_EQ (m1->tag, m2->tag);
72a23c97
AD
384}
385
386static unsigned int
95612cfa 387hash_symbol (const symbol_t *m, unsigned int tablesize)
72a23c97 388{
95612cfa
AD
389 /* Since tags are unique, we can hash the pointer itself. */
390 return ((size_t) m->tag) % tablesize;
72a23c97
AD
391}
392
393
394/*-------------------------------.
2f1afb73 395| Create the symbol hash table. |
72a23c97
AD
396`-------------------------------*/
397
398void
db8837cb 399symbols_new (void)
72a23c97 400{
db8837cb 401 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
72a23c97 402 NULL,
95612cfa
AD
403 (Hash_hasher) hash_symbol,
404 (Hash_comparator) hash_compare_symbol,
db8837cb 405 (Hash_data_freer) symbol_free);
40675e7c
DM
406}
407
408
1e9798d5
AD
409/*----------------------------------------------------------------.
410| Find the symbol named KEY, and return it. If it does not exist |
411| yet, create it. |
412`----------------------------------------------------------------*/
413
db8837cb 414symbol_t *
39f41916 415symbol_get (const char *key, location_t location)
40675e7c 416{
db8837cb
AD
417 symbol_t probe;
418 symbol_t *entry;
40675e7c 419
97650f4e 420 /* Keep the symbol in a printable form. */
95612cfa 421 key = struniq_new (quotearg_style (escape_quoting_style, key));
d7163c0a 422 *(char const **) &probe.tag = key;
db8837cb 423 entry = hash_lookup (symbol_table, &probe);
40675e7c 424
72a23c97 425 if (!entry)
40675e7c 426 {
72a23c97 427 /* First insertion in the hash. */
ee000ba4 428 entry = symbol_new (key, location);
db8837cb 429 hash_insert (symbol_table, entry);
40675e7c 430 }
72a23c97
AD
431 return entry;
432}
40675e7c 433
40675e7c 434
39f41916
AD
435/*------------------------------------------------------------------.
436| Generate a dummy nonterminal, whose name cannot conflict with the |
437| user's names. |
438`------------------------------------------------------------------*/
439
440symbol_t *
441dummy_symbol_get (location_t location)
442{
443 /* Incremented for each generated symbol. */
444 static int dummy_count = 0;
445 static char buf[256];
446
447 symbol_t *sym;
448
449 sprintf (buf, "@%d", ++dummy_count);
450 sym = symbol_get (buf, location);
451 sym->class = nterm_sym;
452 sym->number = nvars++;
453 return sym;
454}
455
456
72a23c97 457/*-------------------.
db8837cb 458| Free the symbols. |
72a23c97
AD
459`-------------------*/
460
461void
db8837cb 462symbols_free (void)
72a23c97 463{
db8837cb 464 hash_free (symbol_table);
536545f3 465 free (symbols);
40675e7c
DM
466}
467
468
72a23c97 469/*---------------------------------------------------------------.
db8837cb 470| Look for undefined symbols, report an error, and consider them |
72a23c97
AD
471| terminals. |
472`---------------------------------------------------------------*/
473
40675e7c 474void
db8837cb 475symbols_do (symbol_processor processor, void *processor_data)
40675e7c 476{
db8837cb 477 hash_do_for_each (symbol_table,
72a23c97
AD
478 (Hash_processor) processor,
479 processor_data);
40675e7c 480}
2f1afb73
AD
481
482
483/*--------------------------------------------------------------.
484| Check that all the symbols are defined. Report any undefined |
485| symbols and consider them nonterminals. |
486`--------------------------------------------------------------*/
487
488void
489symbols_check_defined (void)
490{
491 symbols_do (symbol_check_defined, NULL);
492}
493
494/*------------------------------------------------------------------.
495| Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
496| number. |
497`------------------------------------------------------------------*/
498
499static void
500symbols_token_translations_init (void)
501{
a3714bce 502 bool num_256_available_p = true;
2f1afb73
AD
503 int i;
504
505 /* Find the highest user token number, and whether 256, the POSIX
506 preferred user token number for the error token, is used. */
507 max_user_token_number = 0;
508 for (i = 0; i < ntokens; ++i)
509 {
510 symbol_t *this = symbols[i];
511 if (this->user_token_number != USER_NUMBER_UNDEFINED)
512 {
513 if (this->user_token_number > max_user_token_number)
514 max_user_token_number = this->user_token_number;
515 if (this->user_token_number == 256)
a3714bce 516 num_256_available_p = false;
2f1afb73
AD
517 }
518 }
519
520 /* If 256 is not used, assign it to error, to follow POSIX. */
521 if (num_256_available_p
522 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
523 errtoken->user_token_number = 256;
524
525 /* Set the missing user numbers. */
526 if (max_user_token_number < 256)
527 max_user_token_number = 256;
528
529 for (i = 0; i < ntokens; ++i)
530 {
531 symbol_t *this = symbols[i];
532 if (this->user_token_number == USER_NUMBER_UNDEFINED)
533 this->user_token_number = ++max_user_token_number;
534 if (this->user_token_number > max_user_token_number)
535 max_user_token_number = this->user_token_number;
536 }
537
538 token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1);
539
540 /* Initialize all entries for literal tokens to 2, the internal
88bce5a2
AD
541 token number for $undefined, which represents all invalid inputs.
542 */
2f1afb73
AD
543 for (i = 0; i < max_user_token_number + 1; i++)
544 token_translations[i] = undeftoken->number;
545 symbols_do (symbol_translation, NULL);
546}
547
548
549/*----------------------------------------------------------------.
550| Assign symbol numbers, and write definition of token names into |
551| FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
552`----------------------------------------------------------------*/
553
554void
555symbols_pack (void)
556{
557 symbols = XCALLOC (symbol_t *, nsyms);
558
559 symbols_do (symbol_check_alias_consistence, NULL);
560 symbols_do (symbol_pack, NULL);
561
562 symbols_token_translations_init ();
563
564 if (startsymbol->class == unknown_sym)
ee000ba4 565 fatal_at (startsymbol_location,
dafdc66f 566 _("the start symbol %s is undefined"),
97650f4e 567 startsymbol->tag);
2f1afb73 568 else if (startsymbol->class == token_sym)
ee000ba4 569 fatal_at (startsymbol_location,
dafdc66f 570 _("the start symbol %s is a token"),
97650f4e 571 startsymbol->tag);
2f1afb73 572}