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