]>
Commit | Line | Data |
---|---|---|
1 | /* Symbol table manager for Bison. | |
2 | ||
3 | Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007 Free | |
4 | Software Foundation, Inc. | |
5 | ||
6 | This file is part of Bison, the GNU Compiler Compiler. | |
7 | ||
8 | This program 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 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "system.h" | |
23 | ||
24 | #include <hash.h> | |
25 | #include <quotearg.h> | |
26 | ||
27 | #include "complain.h" | |
28 | #include "gram.h" | |
29 | #include "symtab.h" | |
30 | ||
31 | /*------------------------. | |
32 | | Distinguished symbols. | | |
33 | `------------------------*/ | |
34 | ||
35 | symbol *errtoken = NULL; | |
36 | symbol *undeftoken = NULL; | |
37 | symbol *endtoken = NULL; | |
38 | symbol *accept = NULL; | |
39 | symbol *startsymbol = NULL; | |
40 | location startsymbol_location; | |
41 | ||
42 | /*---------------------------------------. | |
43 | | Default %destructor's and %printer's. | | |
44 | `---------------------------------------*/ | |
45 | ||
46 | static code_props default_tagged_destructor = CODE_PROPS_NONE_INIT; | |
47 | static code_props default_tagless_destructor = CODE_PROPS_NONE_INIT; | |
48 | static code_props default_tagged_printer = CODE_PROPS_NONE_INIT; | |
49 | static code_props default_tagless_printer = CODE_PROPS_NONE_INIT; | |
50 | ||
51 | /*---------------------------------. | |
52 | | Create a new symbol, named TAG. | | |
53 | `---------------------------------*/ | |
54 | ||
55 | static symbol * | |
56 | symbol_new (uniqstr tag, location loc) | |
57 | { | |
58 | symbol *res = xmalloc (sizeof *res); | |
59 | ||
60 | uniqstr_assert (tag); | |
61 | res->tag = tag; | |
62 | res->location = loc; | |
63 | ||
64 | res->type_name = NULL; | |
65 | code_props_none_init (&res->destructor); | |
66 | code_props_none_init (&res->printer); | |
67 | ||
68 | res->number = NUMBER_UNDEFINED; | |
69 | res->prec = 0; | |
70 | res->assoc = undef_assoc; | |
71 | res->user_token_number = USER_NUMBER_UNDEFINED; | |
72 | ||
73 | res->alias = NULL; | |
74 | res->class = unknown_sym; | |
75 | res->declared = false; | |
76 | ||
77 | if (nsyms == SYMBOL_NUMBER_MAXIMUM) | |
78 | fatal (_("too many symbols in input grammar (limit is %d)"), | |
79 | SYMBOL_NUMBER_MAXIMUM); | |
80 | nsyms++; | |
81 | return res; | |
82 | } | |
83 | ||
84 | /*----------------------------------------. | |
85 | | Create a new semantic type, named TAG. | | |
86 | `----------------------------------------*/ | |
87 | ||
88 | static semantic_type * | |
89 | semantic_type_new (uniqstr tag) | |
90 | { | |
91 | semantic_type *res = xmalloc (sizeof *res); | |
92 | ||
93 | uniqstr_assert (tag); | |
94 | res->tag = tag; | |
95 | code_props_none_init (&res->destructor); | |
96 | code_props_none_init (&res->printer); | |
97 | ||
98 | return res; | |
99 | } | |
100 | ||
101 | ||
102 | /*-----------------. | |
103 | | Print a symbol. | | |
104 | `-----------------*/ | |
105 | ||
106 | #define SYMBOL_ATTR_PRINT(Attr) \ | |
107 | if (s->Attr) \ | |
108 | fprintf (f, " %s { %s }", #Attr, s->Attr) | |
109 | ||
110 | #define SYMBOL_CODE_PRINT(Attr) \ | |
111 | if (s->Attr.code) \ | |
112 | fprintf (f, " %s { %s }", #Attr, s->Attr.code) | |
113 | ||
114 | void | |
115 | symbol_print (symbol *s, FILE *f) | |
116 | { | |
117 | if (s) | |
118 | { | |
119 | fprintf (f, "\"%s\"", s->tag); | |
120 | SYMBOL_ATTR_PRINT (type_name); | |
121 | SYMBOL_CODE_PRINT (destructor); | |
122 | SYMBOL_CODE_PRINT (printer); | |
123 | } | |
124 | else | |
125 | fprintf (f, "<NULL>"); | |
126 | } | |
127 | ||
128 | #undef SYMBOL_ATTR_PRINT | |
129 | #undef SYMBOL_CODE_PRINT | |
130 | ||
131 | /*------------------------------------------------------------------. | |
132 | | Complain that S's WHAT is redeclared at SECOND, and was first set | | |
133 | | at FIRST. | | |
134 | `------------------------------------------------------------------*/ | |
135 | ||
136 | static void | |
137 | symbol_redeclaration (symbol *s, const char *what, location first, | |
138 | location second) | |
139 | { | |
140 | complain_at (second, _("%s redeclaration for %s"), what, s->tag); | |
141 | complain_at (first, _("previous declaration")); | |
142 | } | |
143 | ||
144 | static void | |
145 | semantic_type_redeclaration (semantic_type *s, const char *what, location first, | |
146 | location second) | |
147 | { | |
148 | complain_at (second, _("%s redeclaration for <%s>"), what, s->tag); | |
149 | complain_at (first, _("previous declaration")); | |
150 | } | |
151 | ||
152 | ||
153 | /*-----------------------------------------------------------------. | |
154 | | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 | | |
155 | | as TYPE_NAME. | | |
156 | `-----------------------------------------------------------------*/ | |
157 | ||
158 | void | |
159 | symbol_type_set (symbol *sym, uniqstr type_name, location loc) | |
160 | { | |
161 | if (type_name) | |
162 | { | |
163 | if (sym->type_name) | |
164 | symbol_redeclaration (sym, "%type", sym->type_location, loc); | |
165 | uniqstr_assert (type_name); | |
166 | sym->type_name = type_name; | |
167 | sym->type_location = loc; | |
168 | } | |
169 | } | |
170 | ||
171 | ||
172 | /*-----------------------------------------. | |
173 | | Set the DESTRUCTOR associated with SYM. | | |
174 | `-----------------------------------------*/ | |
175 | ||
176 | void | |
177 | symbol_destructor_set (symbol *sym, code_props const *destructor) | |
178 | { | |
179 | if (sym->destructor.code) | |
180 | symbol_redeclaration (sym, "%destructor", sym->destructor.location, | |
181 | destructor->location); | |
182 | sym->destructor = *destructor; | |
183 | } | |
184 | ||
185 | /*------------------------------------------. | |
186 | | Set the DESTRUCTOR associated with TYPE. | | |
187 | `------------------------------------------*/ | |
188 | ||
189 | void | |
190 | semantic_type_destructor_set (semantic_type *type, | |
191 | code_props const *destructor) | |
192 | { | |
193 | if (type->destructor.code) | |
194 | semantic_type_redeclaration (type, "%destructor", | |
195 | type->destructor.location, | |
196 | destructor->location); | |
197 | type->destructor = *destructor; | |
198 | } | |
199 | ||
200 | /*---------------------------------------. | |
201 | | Get the computed %destructor for SYM. | | |
202 | `---------------------------------------*/ | |
203 | ||
204 | code_props const * | |
205 | symbol_destructor_get (symbol const *sym) | |
206 | { | |
207 | /* Per-symbol %destructor. */ | |
208 | if (sym->destructor.code) | |
209 | return &sym->destructor; | |
210 | ||
211 | /* Per-type %destructor. */ | |
212 | if (sym->type_name) | |
213 | { | |
214 | code_props const *destructor = | |
215 | &semantic_type_get (sym->type_name)->destructor; | |
216 | if (destructor->code) | |
217 | return destructor; | |
218 | } | |
219 | ||
220 | /* Apply default %destructor's only to user-defined symbols. */ | |
221 | if (sym->tag[0] == '$' || sym == errtoken) | |
222 | return &code_props_none; | |
223 | ||
224 | if (sym->type_name) | |
225 | return &default_tagged_destructor; | |
226 | return &default_tagless_destructor; | |
227 | } | |
228 | ||
229 | /*--------------------------------------. | |
230 | | Set the PRINTER associated with SYM. | | |
231 | `--------------------------------------*/ | |
232 | ||
233 | void | |
234 | symbol_printer_set (symbol *sym, code_props const *printer) | |
235 | { | |
236 | if (sym->printer.code) | |
237 | symbol_redeclaration (sym, "%printer", | |
238 | sym->printer.location, printer->location); | |
239 | sym->printer = *printer; | |
240 | } | |
241 | ||
242 | /*---------------------------------------. | |
243 | | Set the PRINTER associated with TYPE. | | |
244 | `---------------------------------------*/ | |
245 | ||
246 | void | |
247 | semantic_type_printer_set (semantic_type *type, code_props const *printer) | |
248 | { | |
249 | if (type->printer.code) | |
250 | semantic_type_redeclaration (type, "%printer", | |
251 | type->printer.location, printer->location); | |
252 | type->printer = *printer; | |
253 | } | |
254 | ||
255 | /*------------------------------------. | |
256 | | Get the computed %printer for SYM. | | |
257 | `------------------------------------*/ | |
258 | ||
259 | code_props const * | |
260 | symbol_printer_get (symbol const *sym) | |
261 | { | |
262 | /* Per-symbol %printer. */ | |
263 | if (sym->printer.code) | |
264 | return &sym->printer; | |
265 | ||
266 | /* Per-type %printer. */ | |
267 | if (sym->type_name) | |
268 | { | |
269 | code_props const *printer = &semantic_type_get (sym->type_name)->printer; | |
270 | if (printer->code) | |
271 | return printer; | |
272 | } | |
273 | ||
274 | /* Apply the default %printer only to user-defined symbols. */ | |
275 | if (sym->tag[0] == '$' || sym == errtoken) | |
276 | return &code_props_none; | |
277 | ||
278 | if (sym->type_name) | |
279 | return &default_tagged_printer; | |
280 | return &default_tagless_printer; | |
281 | } | |
282 | ||
283 | /*-----------------------------------------------------------------. | |
284 | | Set the PRECEDENCE associated with SYM. Does nothing if invoked | | |
285 | | with UNDEF_ASSOC as ASSOC. | | |
286 | `-----------------------------------------------------------------*/ | |
287 | ||
288 | void | |
289 | symbol_precedence_set (symbol *sym, int prec, assoc a, location loc) | |
290 | { | |
291 | if (a != undef_assoc) | |
292 | { | |
293 | if (sym->prec != 0) | |
294 | symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location, | |
295 | loc); | |
296 | sym->prec = prec; | |
297 | sym->assoc = a; | |
298 | sym->prec_location = loc; | |
299 | } | |
300 | ||
301 | /* Only terminals have a precedence. */ | |
302 | symbol_class_set (sym, token_sym, loc, false); | |
303 | } | |
304 | ||
305 | ||
306 | /*------------------------------------. | |
307 | | Set the CLASS associated with SYM. | | |
308 | `------------------------------------*/ | |
309 | ||
310 | void | |
311 | symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring) | |
312 | { | |
313 | if (sym->class != unknown_sym && sym->class != class) | |
314 | { | |
315 | complain_at (loc, _("symbol %s redefined"), sym->tag); | |
316 | sym->declared = false; | |
317 | } | |
318 | ||
319 | if (class == nterm_sym && sym->class != nterm_sym) | |
320 | sym->number = nvars++; | |
321 | else if (class == token_sym && sym->number == NUMBER_UNDEFINED) | |
322 | sym->number = ntokens++; | |
323 | ||
324 | sym->class = class; | |
325 | ||
326 | if (declaring) | |
327 | { | |
328 | if (sym->declared) | |
329 | warn_at (loc, _("symbol %s redeclared"), sym->tag); | |
330 | sym->declared = true; | |
331 | } | |
332 | } | |
333 | ||
334 | ||
335 | /*------------------------------------------------. | |
336 | | Set the USER_TOKEN_NUMBER associated with SYM. | | |
337 | `------------------------------------------------*/ | |
338 | ||
339 | void | |
340 | symbol_user_token_number_set (symbol *sym, int user_token_number, location loc) | |
341 | { | |
342 | int *user_token_numberp; | |
343 | ||
344 | aver (sym->class == token_sym); | |
345 | ||
346 | if (sym->user_token_number != USER_NUMBER_ALIAS) | |
347 | user_token_numberp = &sym->user_token_number; | |
348 | else | |
349 | user_token_numberp = &sym->alias->user_token_number; | |
350 | if (*user_token_numberp != USER_NUMBER_UNDEFINED | |
351 | && *user_token_numberp != user_token_number) | |
352 | complain_at (loc, _("redefining user token number of %s"), sym->tag); | |
353 | ||
354 | *user_token_numberp = user_token_number; | |
355 | /* User defined $end token? */ | |
356 | if (user_token_number == 0) | |
357 | { | |
358 | endtoken = sym; | |
359 | endtoken->number = 0; | |
360 | /* It is always mapped to 0, so it was already counted in | |
361 | NTOKENS. */ | |
362 | --ntokens; | |
363 | } | |
364 | } | |
365 | ||
366 | ||
367 | /*----------------------------------------------------------. | |
368 | | If SYM is not defined, report an error, and consider it a | | |
369 | | nonterminal. | | |
370 | `----------------------------------------------------------*/ | |
371 | ||
372 | static inline bool | |
373 | symbol_check_defined (symbol *sym) | |
374 | { | |
375 | if (sym->class == unknown_sym) | |
376 | { | |
377 | complain_at | |
378 | (sym->location, | |
379 | _("symbol %s is used, but is not defined as a token and has no rules"), | |
380 | sym->tag); | |
381 | sym->class = nterm_sym; | |
382 | sym->number = nvars++; | |
383 | } | |
384 | ||
385 | return true; | |
386 | } | |
387 | ||
388 | static bool | |
389 | symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED) | |
390 | { | |
391 | return symbol_check_defined (sym); | |
392 | } | |
393 | ||
394 | ||
395 | /*------------------------------------------------------------------. | |
396 | | Declare the new symbol SYM. Make it an alias of SYMVAL, and type | | |
397 | | SYMVAL with SYM's type. | | |
398 | `------------------------------------------------------------------*/ | |
399 | ||
400 | void | |
401 | symbol_make_alias (symbol *sym, symbol *symval, location loc) | |
402 | { | |
403 | if (symval->alias) | |
404 | warn_at (loc, _("symbol `%s' used more than once as a literal string"), | |
405 | symval->tag); | |
406 | else if (sym->alias) | |
407 | warn_at (loc, _("symbol `%s' given more than one literal string"), | |
408 | sym->tag); | |
409 | else | |
410 | { | |
411 | symval->class = token_sym; | |
412 | symval->user_token_number = sym->user_token_number; | |
413 | sym->user_token_number = USER_NUMBER_ALIAS; | |
414 | symval->alias = sym; | |
415 | sym->alias = symval; | |
416 | symval->number = sym->number; | |
417 | symbol_type_set (symval, sym->type_name, loc); | |
418 | } | |
419 | } | |
420 | ||
421 | ||
422 | /*---------------------------------------------------------. | |
423 | | Check that THIS, and its alias, have same precedence and | | |
424 | | associativity. | | |
425 | `---------------------------------------------------------*/ | |
426 | ||
427 | static inline void | |
428 | symbol_check_alias_consistency (symbol *this) | |
429 | { | |
430 | symbol *alias = this; | |
431 | symbol *orig = this->alias; | |
432 | ||
433 | /* Check only those that _are_ the aliases. */ | |
434 | if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS)) | |
435 | return; | |
436 | ||
437 | if (orig->type_name != alias->type_name) | |
438 | { | |
439 | if (orig->type_name) | |
440 | symbol_type_set (alias, orig->type_name, orig->type_location); | |
441 | else | |
442 | symbol_type_set (orig, alias->type_name, alias->type_location); | |
443 | } | |
444 | ||
445 | ||
446 | if (orig->destructor.code || alias->destructor.code) | |
447 | { | |
448 | if (orig->destructor.code) | |
449 | symbol_destructor_set (alias, &orig->destructor); | |
450 | else | |
451 | symbol_destructor_set (orig, &alias->destructor); | |
452 | } | |
453 | ||
454 | if (orig->printer.code || alias->printer.code) | |
455 | { | |
456 | if (orig->printer.code) | |
457 | symbol_printer_set (alias, &orig->printer); | |
458 | else | |
459 | symbol_printer_set (orig, &alias->printer); | |
460 | } | |
461 | ||
462 | if (alias->prec || orig->prec) | |
463 | { | |
464 | if (orig->prec) | |
465 | symbol_precedence_set (alias, orig->prec, orig->assoc, | |
466 | orig->prec_location); | |
467 | else | |
468 | symbol_precedence_set (orig, alias->prec, alias->assoc, | |
469 | alias->prec_location); | |
470 | } | |
471 | } | |
472 | ||
473 | static bool | |
474 | symbol_check_alias_consistency_processor (void *this, | |
475 | void *null ATTRIBUTE_UNUSED) | |
476 | { | |
477 | symbol_check_alias_consistency (this); | |
478 | return true; | |
479 | } | |
480 | ||
481 | ||
482 | /*-------------------------------------------------------------------. | |
483 | | Assign a symbol number, and write the definition of the token name | | |
484 | | into FDEFINES. Put in SYMBOLS. | | |
485 | `-------------------------------------------------------------------*/ | |
486 | ||
487 | static inline bool | |
488 | symbol_pack (symbol *this) | |
489 | { | |
490 | if (this->class == nterm_sym) | |
491 | { | |
492 | this->number += ntokens; | |
493 | } | |
494 | else if (this->alias) | |
495 | { | |
496 | /* This symbol and its alias are a single token defn. | |
497 | Allocate a tokno, and assign to both check agreement of | |
498 | prec and assoc fields and make both the same */ | |
499 | if (this->number == NUMBER_UNDEFINED) | |
500 | { | |
501 | if (this == endtoken || this->alias == endtoken) | |
502 | this->number = this->alias->number = 0; | |
503 | else | |
504 | { | |
505 | aver (this->alias->number != NUMBER_UNDEFINED); | |
506 | this->number = this->alias->number; | |
507 | } | |
508 | } | |
509 | /* Do not do processing below for USER_NUMBER_ALIASes. */ | |
510 | if (this->user_token_number == USER_NUMBER_ALIAS) | |
511 | return true; | |
512 | } | |
513 | else /* this->class == token_sym */ | |
514 | aver (this->number != NUMBER_UNDEFINED); | |
515 | ||
516 | symbols[this->number] = this; | |
517 | return true; | |
518 | } | |
519 | ||
520 | static bool | |
521 | symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED) | |
522 | { | |
523 | return symbol_pack (this); | |
524 | } | |
525 | ||
526 | ||
527 | ||
528 | ||
529 | /*--------------------------------------------------. | |
530 | | Put THIS in TOKEN_TRANSLATIONS if it is a token. | | |
531 | `--------------------------------------------------*/ | |
532 | ||
533 | static inline bool | |
534 | symbol_translation (symbol *this) | |
535 | { | |
536 | /* Non-terminal? */ | |
537 | if (this->class == token_sym | |
538 | && this->user_token_number != USER_NUMBER_ALIAS) | |
539 | { | |
540 | /* A token which translation has already been set? */ | |
541 | if (token_translations[this->user_token_number] != undeftoken->number) | |
542 | complain_at (this->location, | |
543 | _("tokens %s and %s both assigned number %d"), | |
544 | symbols[token_translations[this->user_token_number]]->tag, | |
545 | this->tag, this->user_token_number); | |
546 | ||
547 | token_translations[this->user_token_number] = this->number; | |
548 | } | |
549 | ||
550 | return true; | |
551 | } | |
552 | ||
553 | static bool | |
554 | symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED) | |
555 | { | |
556 | return symbol_translation (this); | |
557 | } | |
558 | ||
559 | ||
560 | /*---------------------------------------. | |
561 | | Symbol and semantic type hash tables. | | |
562 | `---------------------------------------*/ | |
563 | ||
564 | /* Initial capacity of symbol and semantic type hash table. */ | |
565 | #define HT_INITIAL_CAPACITY 257 | |
566 | ||
567 | static struct hash_table *symbol_table = NULL; | |
568 | static struct hash_table *semantic_type_table = NULL; | |
569 | ||
570 | static inline bool | |
571 | hash_compare_symbol (const symbol *m1, const symbol *m2) | |
572 | { | |
573 | /* Since tags are unique, we can compare the pointers themselves. */ | |
574 | return UNIQSTR_EQ (m1->tag, m2->tag); | |
575 | } | |
576 | ||
577 | static inline bool | |
578 | hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2) | |
579 | { | |
580 | /* Since names are unique, we can compare the pointers themselves. */ | |
581 | return UNIQSTR_EQ (m1->tag, m2->tag); | |
582 | } | |
583 | ||
584 | static bool | |
585 | hash_symbol_comparator (void const *m1, void const *m2) | |
586 | { | |
587 | return hash_compare_symbol (m1, m2); | |
588 | } | |
589 | ||
590 | static bool | |
591 | hash_semantic_type_comparator (void const *m1, void const *m2) | |
592 | { | |
593 | return hash_compare_semantic_type (m1, m2); | |
594 | } | |
595 | ||
596 | static inline size_t | |
597 | hash_symbol (const symbol *m, size_t tablesize) | |
598 | { | |
599 | /* Since tags are unique, we can hash the pointer itself. */ | |
600 | return ((uintptr_t) m->tag) % tablesize; | |
601 | } | |
602 | ||
603 | static inline size_t | |
604 | hash_semantic_type (const semantic_type *m, size_t tablesize) | |
605 | { | |
606 | /* Since names are unique, we can hash the pointer itself. */ | |
607 | return ((uintptr_t) m->tag) % tablesize; | |
608 | } | |
609 | ||
610 | static size_t | |
611 | hash_symbol_hasher (void const *m, size_t tablesize) | |
612 | { | |
613 | return hash_symbol (m, tablesize); | |
614 | } | |
615 | ||
616 | static size_t | |
617 | hash_semantic_type_hasher (void const *m, size_t tablesize) | |
618 | { | |
619 | return hash_semantic_type (m, tablesize); | |
620 | } | |
621 | ||
622 | /*-------------------------------. | |
623 | | Create the symbol hash table. | | |
624 | `-------------------------------*/ | |
625 | ||
626 | void | |
627 | symbols_new (void) | |
628 | { | |
629 | symbol_table = hash_initialize (HT_INITIAL_CAPACITY, | |
630 | NULL, | |
631 | hash_symbol_hasher, | |
632 | hash_symbol_comparator, | |
633 | free); | |
634 | semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY, | |
635 | NULL, | |
636 | hash_semantic_type_hasher, | |
637 | hash_semantic_type_comparator, | |
638 | free); | |
639 | } | |
640 | ||
641 | ||
642 | /*----------------------------------------------------------------. | |
643 | | Find the symbol named KEY, and return it. If it does not exist | | |
644 | | yet, create it. | | |
645 | `----------------------------------------------------------------*/ | |
646 | ||
647 | symbol * | |
648 | symbol_from_uniqstr (const uniqstr key, location loc) | |
649 | { | |
650 | symbol probe; | |
651 | symbol *entry; | |
652 | ||
653 | probe.tag = key; | |
654 | entry = hash_lookup (symbol_table, &probe); | |
655 | ||
656 | if (!entry) | |
657 | { | |
658 | /* First insertion in the hash. */ | |
659 | entry = symbol_new (key, loc); | |
660 | hash_insert (symbol_table, entry); | |
661 | } | |
662 | return entry; | |
663 | } | |
664 | ||
665 | ||
666 | /*-----------------------------------------------------------------------. | |
667 | | Find the semantic type named KEY, and return it. If it does not exist | | |
668 | | yet, create it. | | |
669 | `-----------------------------------------------------------------------*/ | |
670 | ||
671 | semantic_type * | |
672 | semantic_type_from_uniqstr (const uniqstr key) | |
673 | { | |
674 | semantic_type probe; | |
675 | semantic_type *entry; | |
676 | ||
677 | probe.tag = key; | |
678 | entry = hash_lookup (semantic_type_table, &probe); | |
679 | ||
680 | if (!entry) | |
681 | { | |
682 | /* First insertion in the hash. */ | |
683 | entry = semantic_type_new (key); | |
684 | hash_insert (semantic_type_table, entry); | |
685 | } | |
686 | return entry; | |
687 | } | |
688 | ||
689 | ||
690 | /*----------------------------------------------------------------. | |
691 | | Find the symbol named KEY, and return it. If it does not exist | | |
692 | | yet, create it. | | |
693 | `----------------------------------------------------------------*/ | |
694 | ||
695 | symbol * | |
696 | symbol_get (const char *key, location loc) | |
697 | { | |
698 | return symbol_from_uniqstr (uniqstr_new (key), loc); | |
699 | } | |
700 | ||
701 | ||
702 | /*-----------------------------------------------------------------------. | |
703 | | Find the semantic type named KEY, and return it. If it does not exist | | |
704 | | yet, create it. | | |
705 | `-----------------------------------------------------------------------*/ | |
706 | ||
707 | semantic_type * | |
708 | semantic_type_get (const char *key) | |
709 | { | |
710 | return semantic_type_from_uniqstr (uniqstr_new (key)); | |
711 | } | |
712 | ||
713 | ||
714 | /*------------------------------------------------------------------. | |
715 | | Generate a dummy nonterminal, whose name cannot conflict with the | | |
716 | | user's names. | | |
717 | `------------------------------------------------------------------*/ | |
718 | ||
719 | symbol * | |
720 | dummy_symbol_get (location loc) | |
721 | { | |
722 | /* Incremented for each generated symbol. */ | |
723 | static int dummy_count = 0; | |
724 | static char buf[256]; | |
725 | ||
726 | symbol *sym; | |
727 | ||
728 | sprintf (buf, "$@%d", ++dummy_count); | |
729 | sym = symbol_get (buf, loc); | |
730 | sym->class = nterm_sym; | |
731 | sym->number = nvars++; | |
732 | return sym; | |
733 | } | |
734 | ||
735 | bool | |
736 | symbol_is_dummy (const symbol *sym) | |
737 | { | |
738 | return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@'); | |
739 | } | |
740 | ||
741 | /*-------------------. | |
742 | | Free the symbols. | | |
743 | `-------------------*/ | |
744 | ||
745 | void | |
746 | symbols_free (void) | |
747 | { | |
748 | hash_free (symbol_table); | |
749 | hash_free (semantic_type_table); | |
750 | free (symbols); | |
751 | } | |
752 | ||
753 | ||
754 | /*---------------------------------------------------------------. | |
755 | | Look for undefined symbols, report an error, and consider them | | |
756 | | terminals. | | |
757 | `---------------------------------------------------------------*/ | |
758 | ||
759 | static void | |
760 | symbols_do (Hash_processor processor, void *processor_data) | |
761 | { | |
762 | hash_do_for_each (symbol_table, processor, processor_data); | |
763 | } | |
764 | ||
765 | ||
766 | /*--------------------------------------------------------------. | |
767 | | Check that all the symbols are defined. Report any undefined | | |
768 | | symbols and consider them nonterminals. | | |
769 | `--------------------------------------------------------------*/ | |
770 | ||
771 | void | |
772 | symbols_check_defined (void) | |
773 | { | |
774 | symbols_do (symbol_check_defined_processor, NULL); | |
775 | } | |
776 | ||
777 | /*------------------------------------------------------------------. | |
778 | | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same | | |
779 | | number. | | |
780 | `------------------------------------------------------------------*/ | |
781 | ||
782 | static void | |
783 | symbols_token_translations_init (void) | |
784 | { | |
785 | bool num_256_available_p = true; | |
786 | int i; | |
787 | ||
788 | /* Find the highest user token number, and whether 256, the POSIX | |
789 | preferred user token number for the error token, is used. */ | |
790 | max_user_token_number = 0; | |
791 | for (i = 0; i < ntokens; ++i) | |
792 | { | |
793 | symbol *this = symbols[i]; | |
794 | if (this->user_token_number != USER_NUMBER_UNDEFINED) | |
795 | { | |
796 | if (this->user_token_number > max_user_token_number) | |
797 | max_user_token_number = this->user_token_number; | |
798 | if (this->user_token_number == 256) | |
799 | num_256_available_p = false; | |
800 | } | |
801 | } | |
802 | ||
803 | /* If 256 is not used, assign it to error, to follow POSIX. */ | |
804 | if (num_256_available_p | |
805 | && errtoken->user_token_number == USER_NUMBER_UNDEFINED) | |
806 | errtoken->user_token_number = 256; | |
807 | ||
808 | /* Set the missing user numbers. */ | |
809 | if (max_user_token_number < 256) | |
810 | max_user_token_number = 256; | |
811 | ||
812 | for (i = 0; i < ntokens; ++i) | |
813 | { | |
814 | symbol *this = symbols[i]; | |
815 | if (this->user_token_number == USER_NUMBER_UNDEFINED) | |
816 | this->user_token_number = ++max_user_token_number; | |
817 | if (this->user_token_number > max_user_token_number) | |
818 | max_user_token_number = this->user_token_number; | |
819 | } | |
820 | ||
821 | token_translations = xnmalloc (max_user_token_number + 1, | |
822 | sizeof *token_translations); | |
823 | ||
824 | /* Initialize all entries for literal tokens to 2, the internal | |
825 | token number for $undefined, which represents all invalid inputs. | |
826 | */ | |
827 | for (i = 0; i < max_user_token_number + 1; i++) | |
828 | token_translations[i] = undeftoken->number; | |
829 | symbols_do (symbol_translation_processor, NULL); | |
830 | } | |
831 | ||
832 | ||
833 | /*----------------------------------------------------------------. | |
834 | | Assign symbol numbers, and write definition of token names into | | |
835 | | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. | | |
836 | `----------------------------------------------------------------*/ | |
837 | ||
838 | void | |
839 | symbols_pack (void) | |
840 | { | |
841 | symbols_do (symbol_check_alias_consistency_processor, NULL); | |
842 | ||
843 | symbols = xcalloc (nsyms, sizeof *symbols); | |
844 | symbols_do (symbol_pack_processor, NULL); | |
845 | ||
846 | /* Aliases leave empty slots in symbols, so remove them. */ | |
847 | { | |
848 | int writei; | |
849 | int readi; | |
850 | int nsyms_old = nsyms; | |
851 | for (writei = 0, readi = 0; readi < nsyms_old; readi += 1) | |
852 | { | |
853 | if (symbols[readi] == NULL) | |
854 | { | |
855 | nsyms -= 1; | |
856 | ntokens -= 1; | |
857 | } | |
858 | else | |
859 | { | |
860 | symbols[writei] = symbols[readi]; | |
861 | symbols[writei]->number = writei; | |
862 | if (symbols[writei]->alias) | |
863 | symbols[writei]->alias->number = writei; | |
864 | writei += 1; | |
865 | } | |
866 | } | |
867 | } | |
868 | symbols = xnrealloc (symbols, nsyms, sizeof *symbols); | |
869 | ||
870 | symbols_token_translations_init (); | |
871 | ||
872 | if (startsymbol->class == unknown_sym) | |
873 | fatal_at (startsymbol_location, | |
874 | _("the start symbol %s is undefined"), | |
875 | startsymbol->tag); | |
876 | else if (startsymbol->class == token_sym) | |
877 | fatal_at (startsymbol_location, | |
878 | _("the start symbol %s is a token"), | |
879 | startsymbol->tag); | |
880 | } | |
881 | ||
882 | ||
883 | /*--------------------------------------------------. | |
884 | | Set default tagged/tagless %destructor/%printer. | | |
885 | `--------------------------------------------------*/ | |
886 | ||
887 | void | |
888 | default_tagged_destructor_set (code_props const *destructor) | |
889 | { | |
890 | if (default_tagged_destructor.code) | |
891 | { | |
892 | complain_at (destructor->location, | |
893 | _("redeclaration for default tagged %%destructor")); | |
894 | complain_at (default_tagged_destructor.location, | |
895 | _("previous declaration")); | |
896 | } | |
897 | default_tagged_destructor = *destructor; | |
898 | } | |
899 | ||
900 | void | |
901 | default_tagless_destructor_set (code_props const *destructor) | |
902 | { | |
903 | if (default_tagless_destructor.code) | |
904 | { | |
905 | complain_at (destructor->location, | |
906 | _("redeclaration for default tagless %%destructor")); | |
907 | complain_at (default_tagless_destructor.location, | |
908 | _("previous declaration")); | |
909 | } | |
910 | default_tagless_destructor = *destructor; | |
911 | } | |
912 | ||
913 | void | |
914 | default_tagged_printer_set (code_props const *printer) | |
915 | { | |
916 | if (default_tagged_printer.code) | |
917 | { | |
918 | complain_at (printer->location, | |
919 | _("redeclaration for default tagged %%printer")); | |
920 | complain_at (default_tagged_printer.location, | |
921 | _("previous declaration")); | |
922 | } | |
923 | default_tagged_printer = *printer; | |
924 | } | |
925 | ||
926 | void | |
927 | default_tagless_printer_set (code_props const *printer) | |
928 | { | |
929 | if (default_tagless_printer.code) | |
930 | { | |
931 | complain_at (printer->location, | |
932 | _("redeclaration for default tagless %%printer")); | |
933 | complain_at (default_tagless_printer.location, | |
934 | _("previous declaration")); | |
935 | } | |
936 | default_tagless_printer = *printer; | |
937 | } |