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