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