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