]> git.saurik.com Git - bison.git/blame_incremental - src/symtab.c
Regenerate.
[bison.git] / src / symtab.c
... / ...
CommitLineData
1/* Symbol table manager for Bison.
2
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23#include <config.h>
24#include "system.h"
25
26#include <hash.h>
27#include <quotearg.h>
28
29#include "complain.h"
30#include "gram.h"
31#include "symtab.h"
32
33/*------------------------.
34| Distinguished symbols. |
35`------------------------*/
36
37symbol *errtoken = NULL;
38symbol *undeftoken = NULL;
39symbol *endtoken = NULL;
40symbol *accept = NULL;
41symbol *startsymbol = NULL;
42location startsymbol_location;
43
44/*---------------------------------.
45| Create a new symbol, named TAG. |
46`---------------------------------*/
47
48static symbol *
49symbol_new (uniqstr tag, location loc)
50{
51 symbol *res = xmalloc (sizeof *res);
52
53 uniqstr_assert (tag);
54 res->tag = tag;
55 res->location = loc;
56
57 res->type_name = NULL;
58 res->destructor = NULL;
59 res->printer = NULL;
60
61 res->number = NUMBER_UNDEFINED;
62 res->prec = 0;
63 res->assoc = undef_assoc;
64 res->user_token_number = USER_NUMBER_UNDEFINED;
65
66 res->alias = NULL;
67 res->class = unknown_sym;
68
69 if (nsyms == SYMBOL_NUMBER_MAXIMUM)
70 fatal (_("too many symbols in input grammar (limit is %d)"),
71 SYMBOL_NUMBER_MAXIMUM);
72 nsyms++;
73 return res;
74}
75
76
77/*-----------------.
78| Print a symbol. |
79`-----------------*/
80
81#define SYMBOL_ATTR_PRINT(Attr) \
82 if (s->Attr) \
83 fprintf (f, " %s { %s }", #Attr, s->Attr)
84
85void
86symbol_print (symbol *s, FILE *f)
87{
88 if (s)
89 {
90 fprintf (f, "\"%s\"", s->tag);
91 SYMBOL_ATTR_PRINT (type_name);
92 SYMBOL_ATTR_PRINT (destructor);
93 SYMBOL_ATTR_PRINT (printer);
94 }
95 else
96 fprintf (f, "<NULL>");
97}
98
99#undef SYMBOL_ATTR_PRINT
100
101/*------------------------------------------------------------------.
102| Complain that S's WHAT is redeclared at SECOND, and was first set |
103| at FIRST. |
104`------------------------------------------------------------------*/
105
106static void
107redeclaration (symbol* s, const char *what, location first, location second)
108{
109 complain_at (second, _("%s redeclaration for %s"), what, s->tag);
110 complain_at (first, _("first declaration"));
111}
112
113
114/*-----------------------------------------------------------------.
115| Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
116| as TYPE_NAME. |
117`-----------------------------------------------------------------*/
118
119void
120symbol_type_set (symbol *sym, uniqstr type_name, location loc)
121{
122 if (type_name)
123 {
124 if (sym->type_name)
125 redeclaration (sym, "%type", sym->type_location, loc);
126 uniqstr_assert (type_name);
127 sym->type_name = type_name;
128 sym->type_location = loc;
129 }
130}
131
132
133/*------------------------------------------------------------------.
134| Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
135`------------------------------------------------------------------*/
136
137void
138symbol_destructor_set (symbol *sym, const char *destructor, location loc)
139{
140 if (destructor)
141 {
142 if (sym->destructor)
143 redeclaration (sym, "%destructor", sym->destructor_location, loc);
144 sym->destructor = destructor;
145 sym->destructor_location = loc;
146 }
147}
148
149
150/*---------------------------------------------------------------.
151| Set the PRINTER associated with SYM. Do nothing if passed 0. |
152`---------------------------------------------------------------*/
153
154void
155symbol_printer_set (symbol *sym, const char *printer, location loc)
156{
157 if (printer)
158 {
159 if (sym->printer)
160 redeclaration (sym, "%printer", sym->destructor_location, loc);
161 sym->printer = printer;
162 sym->printer_location = loc;
163 }
164}
165
166
167/*-----------------------------------------------------------------.
168| Set the PRECEDENCE associated with SYM. Does nothing if invoked |
169| with UNDEF_ASSOC as ASSOC. |
170`-----------------------------------------------------------------*/
171
172void
173symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
174{
175 if (a != undef_assoc)
176 {
177 if (sym->prec != 0)
178 redeclaration (sym, assoc_to_string (a), sym->prec_location, loc);
179 sym->prec = prec;
180 sym->assoc = a;
181 sym->prec_location = loc;
182 }
183
184 /* Only terminals have a precedence. */
185 symbol_class_set (sym, token_sym, loc);
186}
187
188
189/*------------------------------------.
190| Set the CLASS associated with SYM. |
191`------------------------------------*/
192
193void
194symbol_class_set (symbol *sym, symbol_class class, location loc)
195{
196 if (sym->class != unknown_sym && sym->class != class)
197 complain_at (loc, _("symbol %s redefined"), sym->tag);
198
199 if (class == nterm_sym && sym->class != nterm_sym)
200 sym->number = nvars++;
201 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
202 sym->number = ntokens++;
203
204 sym->class = class;
205}
206
207
208/*------------------------------------------------.
209| Set the USER_TOKEN_NUMBER associated with SYM. |
210`------------------------------------------------*/
211
212void
213symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
214{
215 if (sym->class != token_sym)
216 abort ();
217
218 if (sym->user_token_number != USER_NUMBER_UNDEFINED
219 && sym->user_token_number != user_token_number)
220 complain_at (loc, _("redefining user token number of %s"), sym->tag);
221
222 sym->user_token_number = user_token_number;
223 /* User defined $end token? */
224 if (user_token_number == 0)
225 {
226 endtoken = sym;
227 endtoken->number = 0;
228 /* It is always mapped to 0, so it was already counted in
229 NTOKENS. */
230 --ntokens;
231 }
232}
233
234
235/*----------------------------------------------------------.
236| If SYM is not defined, report an error, and consider it a |
237| nonterminal. |
238`----------------------------------------------------------*/
239
240static inline bool
241symbol_check_defined (symbol *sym)
242{
243 if (sym->class == unknown_sym)
244 {
245 complain_at
246 (sym->location,
247 _("symbol %s is used, but is not defined as a token and has no rules"),
248 sym->tag);
249 sym->class = nterm_sym;
250 sym->number = nvars++;
251 }
252
253 return true;
254}
255
256static bool
257symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
258{
259 return symbol_check_defined (sym);
260}
261
262
263/*------------------------------------------------------------------.
264| Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
265| SYMVAL with SYM's type. |
266`------------------------------------------------------------------*/
267
268void
269symbol_make_alias (symbol *sym, symbol *symval, location loc)
270{
271 if (symval->alias)
272 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
273 symval->tag);
274 else if (sym->alias)
275 warn_at (loc, _("symbol `%s' given more than one literal string"),
276 sym->tag);
277 else
278 {
279 symval->class = token_sym;
280 symval->user_token_number = sym->user_token_number;
281 sym->user_token_number = USER_NUMBER_ALIAS;
282 symval->alias = sym;
283 sym->alias = symval;
284 /* sym and symval combined are only one symbol. */
285 nsyms--;
286 ntokens--;
287 if (ntokens != sym->number && ntokens != symval->number)
288 abort ();
289 sym->number = symval->number =
290 (symval->number < sym->number) ? symval->number : sym->number;
291 symbol_type_set (symval, sym->type_name, loc);
292 }
293}
294
295
296/*---------------------------------------------------------.
297| Check that THIS, and its alias, have same precedence and |
298| associativity. |
299`---------------------------------------------------------*/
300
301static inline void
302symbol_check_alias_consistency (symbol *this)
303{
304 symbol *alias = this;
305 symbol *orig = this->alias;
306
307 /* Check only those that _are_ the aliases. */
308 if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS))
309 return;
310
311 if (orig->type_name != alias->type_name)
312 {
313 if (orig->type_name)
314 symbol_type_set (alias, orig->type_name, orig->type_location);
315 else
316 symbol_type_set (orig, alias->type_name, alias->type_location);
317 }
318
319
320 if (orig->destructor || alias->destructor)
321 {
322 if (orig->destructor)
323 symbol_destructor_set (alias, orig->destructor,
324 orig->destructor_location);
325 else
326 symbol_destructor_set (orig, alias->destructor,
327 alias->destructor_location);
328 }
329
330 if (orig->printer || alias->printer)
331 {
332 if (orig->printer)
333 symbol_printer_set (alias, orig->printer, orig->printer_location);
334 else
335 symbol_printer_set (orig, alias->printer, alias->printer_location);
336 }
337
338 if (alias->prec || orig->prec)
339 {
340 if (orig->prec)
341 symbol_precedence_set (alias, orig->prec, orig->assoc,
342 orig->prec_location);
343 else
344 symbol_precedence_set (orig, alias->prec, alias->assoc,
345 alias->prec_location);
346 }
347}
348
349static bool
350symbol_check_alias_consistency_processor (void *this,
351 void *null ATTRIBUTE_UNUSED)
352{
353 symbol_check_alias_consistency (this);
354 return true;
355}
356
357
358/*-------------------------------------------------------------------.
359| Assign a symbol number, and write the definition of the token name |
360| into FDEFINES. Put in SYMBOLS. |
361`-------------------------------------------------------------------*/
362
363static inline bool
364symbol_pack (symbol *this)
365{
366 if (this->class == nterm_sym)
367 {
368 this->number += ntokens;
369 }
370 else if (this->alias)
371 {
372 /* This symbol and its alias are a single token defn.
373 Allocate a tokno, and assign to both check agreement of
374 prec and assoc fields and make both the same */
375 if (this->number == NUMBER_UNDEFINED)
376 {
377 if (this == endtoken || this->alias == endtoken)
378 this->number = this->alias->number = 0;
379 else
380 {
381 if (this->alias->number == NUMBER_UNDEFINED)
382 abort ();
383 this->number = this->alias->number;
384 }
385 }
386 /* Do not do processing below for USER_NUMBER_ALIASes. */
387 if (this->user_token_number == USER_NUMBER_ALIAS)
388 return true;
389 }
390 else /* this->class == token_sym */
391 {
392 if (this->number == NUMBER_UNDEFINED)
393 abort ();
394 }
395
396 symbols[this->number] = this;
397 return true;
398}
399
400static bool
401symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
402{
403 return symbol_pack (this);
404}
405
406
407
408
409/*--------------------------------------------------.
410| Put THIS in TOKEN_TRANSLATIONS if it is a token. |
411`--------------------------------------------------*/
412
413static inline bool
414symbol_translation (symbol *this)
415{
416 /* Non-terminal? */
417 if (this->class == token_sym
418 && this->user_token_number != USER_NUMBER_ALIAS)
419 {
420 /* A token which translation has already been set? */
421 if (token_translations[this->user_token_number] != undeftoken->number)
422 complain_at (this->location,
423 _("tokens %s and %s both assigned number %d"),
424 symbols[token_translations[this->user_token_number]]->tag,
425 this->tag, this->user_token_number);
426
427 token_translations[this->user_token_number] = this->number;
428 }
429
430 return true;
431}
432
433static bool
434symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
435{
436 return symbol_translation (this);
437}
438
439
440/*----------------------.
441| A symbol hash table. |
442`----------------------*/
443
444/* Initial capacity of symbols hash table. */
445#define HT_INITIAL_CAPACITY 257
446
447static struct hash_table *symbol_table = NULL;
448
449static inline bool
450hash_compare_symbol (const symbol *m1, const symbol *m2)
451{
452 /* Since tags are unique, we can compare the pointers themselves. */
453 return UNIQSTR_EQ (m1->tag, m2->tag);
454}
455
456static bool
457hash_symbol_comparator (void const *m1, void const *m2)
458{
459 return hash_compare_symbol (m1, m2);
460}
461
462static inline size_t
463hash_symbol (const symbol *m, size_t tablesize)
464{
465 /* Since tags are unique, we can hash the pointer itself. */
466 return ((uintptr_t) m->tag) % tablesize;
467}
468
469static size_t
470hash_symbol_hasher (void const *m, size_t tablesize)
471{
472 return hash_symbol (m, tablesize);
473}
474
475
476/*-------------------------------.
477| Create the symbol hash table. |
478`-------------------------------*/
479
480void
481symbols_new (void)
482{
483 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
484 NULL,
485 hash_symbol_hasher,
486 hash_symbol_comparator,
487 free);
488}
489
490
491/*----------------------------------------------------------------.
492| Find the symbol named KEY, and return it. If it does not exist |
493| yet, create it. |
494`----------------------------------------------------------------*/
495
496symbol *
497symbol_get (const char *key, location loc)
498{
499 symbol probe;
500 symbol *entry;
501
502 key = uniqstr_new (key);
503 probe.tag = key;
504 entry = hash_lookup (symbol_table, &probe);
505
506 if (!entry)
507 {
508 /* First insertion in the hash. */
509 entry = symbol_new (key, loc);
510 hash_insert (symbol_table, entry);
511 }
512 return entry;
513}
514
515
516/*------------------------------------------------------------------.
517| Generate a dummy nonterminal, whose name cannot conflict with the |
518| user's names. |
519`------------------------------------------------------------------*/
520
521symbol *
522dummy_symbol_get (location loc)
523{
524 /* Incremented for each generated symbol. */
525 static int dummy_count = 0;
526 static char buf[256];
527
528 symbol *sym;
529
530 sprintf (buf, "@%d", ++dummy_count);
531 sym = symbol_get (buf, loc);
532 sym->class = nterm_sym;
533 sym->number = nvars++;
534 return sym;
535}
536
537
538/*-------------------.
539| Free the symbols. |
540`-------------------*/
541
542void
543symbols_free (void)
544{
545 hash_free (symbol_table);
546 free (symbols);
547}
548
549
550/*---------------------------------------------------------------.
551| Look for undefined symbols, report an error, and consider them |
552| terminals. |
553`---------------------------------------------------------------*/
554
555static void
556symbols_do (Hash_processor processor, void *processor_data)
557{
558 hash_do_for_each (symbol_table, processor, processor_data);
559}
560
561
562/*--------------------------------------------------------------.
563| Check that all the symbols are defined. Report any undefined |
564| symbols and consider them nonterminals. |
565`--------------------------------------------------------------*/
566
567void
568symbols_check_defined (void)
569{
570 symbols_do (symbol_check_defined_processor, NULL);
571}
572
573/*------------------------------------------------------------------.
574| Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
575| number. |
576`------------------------------------------------------------------*/
577
578static void
579symbols_token_translations_init (void)
580{
581 bool num_256_available_p = true;
582 int i;
583
584 /* Find the highest user token number, and whether 256, the POSIX
585 preferred user token number for the error token, is used. */
586 max_user_token_number = 0;
587 for (i = 0; i < ntokens; ++i)
588 {
589 symbol *this = symbols[i];
590 if (this->user_token_number != USER_NUMBER_UNDEFINED)
591 {
592 if (this->user_token_number > max_user_token_number)
593 max_user_token_number = this->user_token_number;
594 if (this->user_token_number == 256)
595 num_256_available_p = false;
596 }
597 }
598
599 /* If 256 is not used, assign it to error, to follow POSIX. */
600 if (num_256_available_p
601 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
602 errtoken->user_token_number = 256;
603
604 /* Set the missing user numbers. */
605 if (max_user_token_number < 256)
606 max_user_token_number = 256;
607
608 for (i = 0; i < ntokens; ++i)
609 {
610 symbol *this = symbols[i];
611 if (this->user_token_number == USER_NUMBER_UNDEFINED)
612 this->user_token_number = ++max_user_token_number;
613 if (this->user_token_number > max_user_token_number)
614 max_user_token_number = this->user_token_number;
615 }
616
617 token_translations = xnmalloc (max_user_token_number + 1,
618 sizeof *token_translations);
619
620 /* Initialize all entries for literal tokens to 2, the internal
621 token number for $undefined, which represents all invalid inputs.
622 */
623 for (i = 0; i < max_user_token_number + 1; i++)
624 token_translations[i] = undeftoken->number;
625 symbols_do (symbol_translation_processor, NULL);
626}
627
628
629/*----------------------------------------------------------------.
630| Assign symbol numbers, and write definition of token names into |
631| FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
632`----------------------------------------------------------------*/
633
634void
635symbols_pack (void)
636{
637 symbols = xcalloc (nsyms, sizeof *symbols);
638
639 symbols_do (symbol_check_alias_consistency_processor, NULL);
640 symbols_do (symbol_pack_processor, NULL);
641
642 symbols_token_translations_init ();
643
644 if (startsymbol->class == unknown_sym)
645 fatal_at (startsymbol_location,
646 _("the start symbol %s is undefined"),
647 startsymbol->tag);
648 else if (startsymbol->class == token_sym)
649 fatal_at (startsymbol_location,
650 _("the start symbol %s is a token"),
651 startsymbol->tag);
652}