]> git.saurik.com Git - bison.git/blame - src/symtab.c
gnulib: update
[bison.git] / src / symtab.c
CommitLineData
0fb1efaf
PE
1/* Symbol table manager for Bison.
2
3209eb1c 3 Copyright (C) 1984, 1989, 2000-2002, 2004-2015 Free Software
575619af 4 Foundation, Inc.
40675e7c 5
95e36146 6 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 7
f16b0819 8 This program is free software: you can redistribute it and/or modify
95e36146 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
40675e7c 12
f16b0819 13 This program is distributed in the hope that it will be useful,
95e36146
AD
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.
40675e7c 17
95e36146 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
40675e7c 20
2cec9080 21#include <config.h>
40675e7c 22#include "system.h"
17ee7397
PE
23
24#include <hash.h>
17ee7397 25
2f1afb73 26#include "complain.h"
40675e7c 27#include "gram.h"
17ee7397 28#include "symtab.h"
40675e7c 29
83b60c97
JD
30/*-------------------------------------------------------------------.
31| Symbols sorted by tag. Allocated by the first invocation of |
32| symbols_do, after which no more symbols should be created. |
33`-------------------------------------------------------------------*/
34
35static symbol **symbols_sorted = NULL;
9641b918 36static symbol **semantic_types_sorted = NULL;
83b60c97 37
2f1afb73
AD
38/*------------------------.
39| Distinguished symbols. |
40`------------------------*/
41
17ee7397
PE
42symbol *errtoken = NULL;
43symbol *undeftoken = NULL;
44symbol *endtoken = NULL;
45symbol *accept = NULL;
46symbol *startsymbol = NULL;
47location startsymbol_location;
2f1afb73 48
284bc49c
VT
49/*---------------------------.
50| Precedence relation graph. |
51`---------------------------*/
52
53static symgraph **prec_nodes;
ec5479ce 54
e8f7155d
VT
55/*-----------------------------------.
56| Store which associativity is used. |
57`-----------------------------------*/
58
59bool *used_assoc = NULL;
60
72a23c97
AD
61/*---------------------------------.
62| Create a new symbol, named TAG. |
63`---------------------------------*/
1e9798d5 64
17ee7397
PE
65static symbol *
66symbol_new (uniqstr tag, location loc)
1e9798d5 67{
da2a7671 68 symbol *res = xmalloc (sizeof *res);
17ee7397 69 uniqstr_assert (tag);
4f646c37
AD
70
71 /* If the tag is not a string (starts with a double quote), check
72 that it is valid for Yacc. */
84526bf3 73 if (tag[0] != '\"' && tag[0] != '\'' && strchr (tag, '-'))
bb8e56ff
TR
74 complain (&loc, Wyacc,
75 _("POSIX Yacc forbids dashes in symbol names: %s"), tag);
4f646c37 76
95612cfa 77 res->tag = tag;
17ee7397 78 res->location = loc;
24c0aad7 79
1e9798d5 80 res->type_name = NULL;
c5ae8e85
AD
81 {
82 int i;
83 for (i = 0; i < CODE_PROPS_SIZE; ++i)
84 code_props_none_init (&res->props[i]);
85 }
24c0aad7 86
5fbb0954 87 res->number = NUMBER_UNDEFINED;
1e9798d5 88 res->prec = 0;
a945ec39 89 res->assoc = undef_assoc;
b87f8b21 90 res->user_token_number = USER_NUMBER_UNDEFINED;
260008e5 91
1e9798d5
AD
92 res->alias = NULL;
93 res->class = unknown_sym;
3b0b682f 94 res->status = undeclared;
1e9798d5 95
3239db74 96 if (nsyms == SYMBOL_NUMBER_MAXIMUM)
bb8e56ff 97 complain (NULL, fatal, _("too many symbols in input grammar (limit is %d)"),
6fb8b256 98 SYMBOL_NUMBER_MAXIMUM);
6d0ef4ec 99 nsyms++;
1e9798d5
AD
100 return res;
101}
102
26eb4f0b
AD
103/* If needed, swap first and second so that first has the earliest
104 location (according to location_cmp).
105
106 Many symbol features (e.g., user token numbers) are not assigned
107 during the parsing, but in a second step, via a traversal of the
108 symbol table sorted on tag.
109
110 However, error messages make more sense if we keep the first
111 declaration first.
112*/
113
114static
115void symbols_sort (symbol **first, symbol **second)
116{
117 if (0 < location_cmp ((*first)->location, (*second)->location))
118 {
119 symbol* tmp = *first;
120 *first = *second;
121 *second = tmp;
122 }
123}
124
461f1516
AD
125/* Likewise, for locations. */
126
127static
128void locations_sort (location *first, location *second)
129{
130 if (0 < location_cmp (*first, *second))
131 {
132 location tmp = *first;
133 *first = *second;
134 *second = tmp;
135 }
136}
137
6a0655d9 138char const *
71da68b3
VS
139code_props_type_string (code_props_type kind)
140{
141 switch (kind)
142 {
143 case destructor:
144 return "%destructor";
145 case printer:
146 return "%printer";
147 }
a2c525b3 148 abort ();
71da68b3
VS
149}
150
b2a0b7ca
JD
151/*----------------------------------------.
152| Create a new semantic type, named TAG. |
153`----------------------------------------*/
154
155static semantic_type *
9641b918 156semantic_type_new (uniqstr tag, const location *loc)
b2a0b7ca
JD
157{
158 semantic_type *res = xmalloc (sizeof *res);
159
160 uniqstr_assert (tag);
161 res->tag = tag;
e96b1b2c
TR
162 res->location = loc ? *loc : empty_location;
163 res->status = undeclared;
c5ae8e85
AD
164 {
165 int i;
166 for (i = 0; i < CODE_PROPS_SIZE; ++i)
167 code_props_none_init (&res->props[i]);
168 }
b2a0b7ca
JD
169
170 return res;
171}
172
40675e7c 173
867a3e00
AD
174/*-----------------.
175| Print a symbol. |
176`-----------------*/
177
e9690142
JD
178#define SYMBOL_ATTR_PRINT(Attr) \
179 if (s->Attr) \
ab703f2c 180 fprintf (f, " %s { %s }", #Attr, s->Attr)
867a3e00 181
71da68b3
VS
182#define SYMBOL_CODE_PRINT(Attr) \
183 if (s->props[Attr].code) \
184 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
95021767 185
867a3e00 186void
c5289832 187symbol_print (symbol const *s, FILE *f)
867a3e00 188{
affac613
AD
189 if (s)
190 {
848bcc28 191 fputs (s->tag, f);
affac613 192 SYMBOL_ATTR_PRINT (type_name);
95021767
JD
193 SYMBOL_CODE_PRINT (destructor);
194 SYMBOL_CODE_PRINT (printer);
affac613
AD
195 }
196 else
848bcc28 197 fputs ("<NULL>", f);
867a3e00
AD
198}
199
200#undef SYMBOL_ATTR_PRINT
95021767 201#undef SYMBOL_CODE_PRINT
867a3e00 202
aea10ef4
AD
203
204/*----------------------------------.
205| Whether S is a valid identifier. |
206`----------------------------------*/
207
208static bool
209is_identifier (uniqstr s)
210{
211 static char const alphanum[26 + 26 + 1 + 10] =
212 "abcdefghijklmnopqrstuvwxyz"
213 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
214 "_"
215 "0123456789";
216 if (!s || ! memchr (alphanum, *s, sizeof alphanum - 10))
217 return false;
218 for (++s; *s; ++s)
219 if (! memchr (alphanum, *s, sizeof alphanum))
220 return false;
221 return true;
222}
223
224
225/*-----------------------------------------------.
226| Get the identifier associated to this symbol. |
227`-----------------------------------------------*/
228uniqstr
229symbol_id_get (symbol const *sym)
230{
dfaa4860 231 aver (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS);
aea10ef4
AD
232 if (sym->alias)
233 sym = sym->alias;
234 return is_identifier (sym->tag) ? sym->tag : 0;
235}
236
237
df09ef2e
AD
238/*------------------------------------------------------------------.
239| Complain that S's WHAT is redeclared at SECOND, and was first set |
240| at FIRST. |
241`------------------------------------------------------------------*/
242
243static void
b2a0b7ca
JD
244symbol_redeclaration (symbol *s, const char *what, location first,
245 location second)
df09ef2e 246{
cbaea010 247 unsigned i = 0;
461f1516 248 locations_sort (&first, &second);
b999409e
TR
249 complain_indent (&second, complaint, &i,
250 _("%s redeclaration for %s"), what, s->tag);
cbaea010 251 i += SUB_INDENT;
b999409e
TR
252 complain_indent (&first, complaint, &i,
253 _("previous declaration"));
df09ef2e
AD
254}
255
b2a0b7ca
JD
256static void
257semantic_type_redeclaration (semantic_type *s, const char *what, location first,
258 location second)
259{
cbaea010 260 unsigned i = 0;
461f1516 261 locations_sort (&first, &second);
b999409e
TR
262 complain_indent (&second, complaint, &i,
263 _("%s redeclaration for <%s>"), what, s->tag);
cbaea010 264 i += SUB_INDENT;
b999409e
TR
265 complain_indent (&first, complaint, &i,
266 _("previous declaration"));
b2a0b7ca
JD
267}
268
df09ef2e 269
12cf133f 270
17ee7397
PE
271/*-----------------------------------------------------------------.
272| Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
273| as TYPE_NAME. |
274`-----------------------------------------------------------------*/
3ae2b51f
AD
275
276void
17ee7397 277symbol_type_set (symbol *sym, uniqstr type_name, location loc)
3ae2b51f 278{
e9955c83
AD
279 if (type_name)
280 {
17ee7397 281 if (sym->type_name)
e9690142 282 symbol_redeclaration (sym, "%type", sym->type_location, loc);
f249e803
AD
283 else
284 {
285 uniqstr_assert (type_name);
286 sym->type_name = type_name;
287 sym->type_location = loc;
288 }
e9955c83 289 }
3ae2b51f
AD
290}
291
71da68b3
VS
292/*--------------------------------------------------------.
293| Set the DESTRUCTOR or PRINTER associated with the SYM. |
294`--------------------------------------------------------*/
366eea36
AD
295
296void
71da68b3
VS
297symbol_code_props_set (symbol *sym, code_props_type kind,
298 code_props const *code)
366eea36 299{
71da68b3
VS
300 if (sym->props[kind].code)
301 symbol_redeclaration (sym, code_props_type_string (kind),
302 sym->props[kind].location,
303 code->location);
f249e803
AD
304 else
305 sym->props[kind] = *code;
366eea36
AD
306}
307
71da68b3
VS
308/*-----------------------------------------------------.
309| Set the DESTRUCTOR or PRINTER associated with TYPE. |
310`-----------------------------------------------------*/
b2a0b7ca
JD
311
312void
71da68b3
VS
313semantic_type_code_props_set (semantic_type *type,
314 code_props_type kind,
315 code_props const *code)
b2a0b7ca 316{
71da68b3
VS
317 if (type->props[kind].code)
318 semantic_type_redeclaration (type, code_props_type_string (kind),
319 type->props[kind].location,
320 code->location);
f249e803
AD
321 else
322 type->props[kind] = *code;
b2a0b7ca
JD
323}
324
71da68b3
VS
325/*---------------------------------------------------.
326| Get the computed %destructor or %printer for SYM. |
327`---------------------------------------------------*/
ec5479ce 328
70946cff
AD
329code_props *
330symbol_code_props_get (symbol *sym, code_props_type kind)
ec5479ce 331{
71da68b3
VS
332 /* Per-symbol code props. */
333 if (sym->props[kind].code)
334 return &sym->props[kind];
9350499c 335
71da68b3 336 /* Per-type code props. */
b2a0b7ca
JD
337 if (sym->type_name)
338 {
70946cff 339 code_props *code =
9641b918 340 &semantic_type_get (sym->type_name, NULL)->props[kind];
71da68b3
VS
341 if (code->code)
342 return code;
b2a0b7ca
JD
343 }
344
71da68b3 345 /* Apply default code props's only to user-defined symbols. */
9534d2be
AD
346 if (sym->tag[0] != '$' && sym != errtoken)
347 {
348 code_props *code =
349 &semantic_type_get (sym->type_name ? "*" : "", NULL)->props[kind];
350 if (code->code)
351 return code;
352 }
353 return &code_props_none;
ec5479ce
JD
354}
355
17ee7397
PE
356/*-----------------------------------------------------------------.
357| Set the PRECEDENCE associated with SYM. Does nothing if invoked |
358| with UNDEF_ASSOC as ASSOC. |
359`-----------------------------------------------------------------*/
3ae2b51f
AD
360
361void
17ee7397 362symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
3ae2b51f 363{
17ee7397 364 if (a != undef_assoc)
e9955c83 365 {
d8ce7031 366 if (sym->prec)
e9690142 367 symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location,
b2a0b7ca 368 loc);
d8ce7031
AD
369 else
370 {
371 sym->prec = prec;
372 sym->assoc = a;
373 sym->prec_location = loc;
374 }
e9955c83
AD
375 }
376
377 /* Only terminals have a precedence. */
073f9288 378 symbol_class_set (sym, token_sym, loc, false);
3ae2b51f
AD
379}
380
381
17ee7397
PE
382/*------------------------------------.
383| Set the CLASS associated with SYM. |
384`------------------------------------*/
44536b35
AD
385
386void
073f9288 387symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
44536b35 388{
3b0b682f 389 bool warned = false;
17ee7397 390 if (sym->class != unknown_sym && sym->class != class)
073f9288 391 {
bb8e56ff 392 complain (&loc, complaint, _("symbol %s redefined"), sym->tag);
c5ae8e85 393 /* Don't report both "redefined" and "redeclared". */
3b0b682f 394 warned = true;
073f9288 395 }
44536b35 396
17ee7397
PE
397 if (class == nterm_sym && sym->class != nterm_sym)
398 sym->number = nvars++;
6d0ef4ec 399 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
17ee7397 400 sym->number = ntokens++;
44536b35 401
17ee7397 402 sym->class = class;
073f9288
PE
403
404 if (declaring)
405 {
3b0b682f 406 if (sym->status == declared && !warned)
bb8e56ff 407 complain (&loc, Wother, _("symbol %s redeclared"), sym->tag);
f249e803
AD
408 else
409 sym->status = declared;
073f9288 410 }
44536b35
AD
411}
412
413
17ee7397
PE
414/*------------------------------------------------.
415| Set the USER_TOKEN_NUMBER associated with SYM. |
416`------------------------------------------------*/
44536b35
AD
417
418void
17ee7397 419symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
44536b35 420{
1f6b3679
JD
421 int *user_token_numberp;
422
dfaa4860 423 if (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
1f6b3679
JD
424 user_token_numberp = &sym->user_token_number;
425 else
426 user_token_numberp = &sym->alias->user_token_number;
427 if (*user_token_numberp != USER_NUMBER_UNDEFINED
428 && *user_token_numberp != user_token_number)
bb8e56ff
TR
429 complain (&loc, complaint, _("redefining user token number of %s"),
430 sym->tag);
44536b35 431
1f6b3679 432 *user_token_numberp = user_token_number;
88bce5a2 433 /* User defined $end token? */
44536b35
AD
434 if (user_token_number == 0)
435 {
17ee7397 436 endtoken = sym;
44536b35 437 /* It is always mapped to 0, so it was already counted in
e9690142 438 NTOKENS. */
1f36f544
JD
439 if (endtoken->number != NUMBER_UNDEFINED)
440 --ntokens;
441 endtoken->number = 0;
44536b35
AD
442 }
443}
444
445
17ee7397
PE
446/*----------------------------------------------------------.
447| If SYM is not defined, report an error, and consider it a |
448| nonterminal. |
449`----------------------------------------------------------*/
2f1afb73 450
0fb1efaf 451static inline bool
17ee7397 452symbol_check_defined (symbol *sym)
2f1afb73 453{
17ee7397 454 if (sym->class == unknown_sym)
2f1afb73 455 {
31557b9e 456 assert (sym->status != declared);
bb8e56ff
TR
457 complain (&sym->location,
458 sym->status == needed ? complaint : Wother,
459 _("symbol %s is used, but is not defined as a token"
460 " and has no rules"),
461 sym->tag);
17ee7397
PE
462 sym->class = nterm_sym;
463 sym->number = nvars++;
2f1afb73
AD
464 }
465
c5ae8e85
AD
466 {
467 int i;
468 for (i = 0; i < 2; ++i)
469 symbol_code_props_get (sym, i)->is_used = true;
470 }
ea9a35c6 471
9641b918
VS
472 /* Set the semantic type status associated to the current symbol to
473 'declared' so that we could check semantic types unnecessary uses. */
474 if (sym->type_name)
475 {
476 semantic_type *sem_type = semantic_type_get (sym->type_name, NULL);
477 if (sem_type)
478 sem_type->status = declared;
479 }
480
481 return true;
482}
483
484static inline bool
485semantic_type_check_defined (semantic_type *sem_type)
486{
c5ae8e85 487 /* <*> and <> do not have to be "declared". */
9534d2be
AD
488 if (sem_type->status == declared
489 || !*sem_type->tag
c0ef22ab 490 || STREQ (sem_type->tag, "*"))
ea9a35c6 491 {
c5ae8e85
AD
492 int i;
493 for (i = 0; i < 2; ++i)
ea9a35c6
VS
494 if (sem_type->props[i].kind != CODE_PROPS_NONE
495 && ! sem_type->props[i].is_used)
bb8e56ff
TR
496 complain (&sem_type->location, Wother,
497 _("useless %s for type <%s>"),
498 code_props_type_string (i), sem_type->tag);
ea9a35c6
VS
499 }
500 else
bb8e56ff
TR
501 complain (&sem_type->location, Wother,
502 _("type <%s> is used, but is not associated to any symbol"),
503 sem_type->tag);
9641b918 504
a3714bce 505 return true;
2f1afb73
AD
506}
507
0fb1efaf
PE
508static bool
509symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
510{
511 return symbol_check_defined (sym);
512}
513
9641b918
VS
514static bool
515semantic_type_check_defined_processor (void *sem_type,
516 void *null ATTRIBUTE_UNUSED)
517{
518 return semantic_type_check_defined (sem_type);
519}
520
2f1afb73 521
2f1afb73 522void
dfaa4860 523symbol_make_alias (symbol *sym, symbol *str, location loc)
2f1afb73 524{
dfaa4860 525 if (str->alias)
bb8e56ff 526 complain (&loc, Wother,
6fb8b256 527 _("symbol %s used more than once as a literal string"), str->tag);
17ee7397 528 else if (sym->alias)
bb8e56ff 529 complain (&loc, Wother,
6fb8b256 530 _("symbol %s given more than one literal string"), sym->tag);
2f1afb73
AD
531 else
532 {
dfaa4860
JD
533 str->class = token_sym;
534 str->user_token_number = sym->user_token_number;
535 sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
536 str->alias = sym;
537 sym->alias = str;
538 str->number = sym->number;
539 symbol_type_set (str, sym->type_name, loc);
2f1afb73
AD
540 }
541}
542
543
544/*---------------------------------------------------------.
545| Check that THIS, and its alias, have same precedence and |
546| associativity. |
547`---------------------------------------------------------*/
548
df09ef2e 549static inline void
0fb1efaf 550symbol_check_alias_consistency (symbol *this)
2f1afb73 551{
dfaa4860 552 symbol *sym = this;
b143f404 553 symbol *str = this->alias;
df09ef2e 554
dfaa4860
JD
555 /* Check only the symbol in the symbol-string pair. */
556 if (!(this->alias
557 && this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
df09ef2e
AD
558 return;
559
dfaa4860 560 if (str->type_name != sym->type_name)
2f1afb73 561 {
dfaa4860 562 if (str->type_name)
e9690142 563 symbol_type_set (sym, str->type_name, str->type_location);
df09ef2e 564 else
e9690142 565 symbol_type_set (str, sym->type_name, sym->type_location);
df09ef2e 566 }
2f1afb73 567
df09ef2e 568
c5ae8e85
AD
569 {
570 int i;
571 for (i = 0; i < CODE_PROPS_SIZE; ++i)
572 if (str->props[i].code)
573 symbol_code_props_set (sym, i, &str->props[i]);
574 else if (sym->props[i].code)
575 symbol_code_props_set (str, i, &sym->props[i]);
576 }
df09ef2e 577
dfaa4860 578 if (sym->prec || str->prec)
df09ef2e 579 {
dfaa4860 580 if (str->prec)
e9690142
JD
581 symbol_precedence_set (sym, str->prec, str->assoc,
582 str->prec_location);
df09ef2e 583 else
e9690142
JD
584 symbol_precedence_set (str, sym->prec, sym->assoc,
585 sym->prec_location);
2f1afb73 586 }
2f1afb73
AD
587}
588
0fb1efaf
PE
589static bool
590symbol_check_alias_consistency_processor (void *this,
e9690142 591 void *null ATTRIBUTE_UNUSED)
0fb1efaf 592{
df09ef2e
AD
593 symbol_check_alias_consistency (this);
594 return true;
0fb1efaf
PE
595}
596
2f1afb73
AD
597
598/*-------------------------------------------------------------------.
599| Assign a symbol number, and write the definition of the token name |
600| into FDEFINES. Put in SYMBOLS. |
601`-------------------------------------------------------------------*/
602
0fb1efaf 603static inline bool
17ee7397 604symbol_pack (symbol *this)
2f1afb73 605{
f74d6d25 606 aver (this->number != NUMBER_UNDEFINED);
2f1afb73 607 if (this->class == nterm_sym)
f74d6d25
JD
608 this->number += ntokens;
609 else if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
610 return true;
2f1afb73
AD
611
612 symbols[this->number] = this;
a3714bce 613 return true;
2f1afb73
AD
614}
615
0fb1efaf
PE
616static bool
617symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
618{
619 return symbol_pack (this);
620}
621
2f1afb73 622
12cf133f
AD
623static void
624user_token_number_redeclaration (int num, symbol *first, symbol *second)
625{
cbaea010 626 unsigned i = 0;
26eb4f0b 627 symbols_sort (&first, &second);
b999409e
TR
628 complain_indent (&second->location, complaint, &i,
629 _("user token number %d redeclaration for %s"),
630 num, second->tag);
b506d9bf 631 i += SUB_INDENT;
b999409e
TR
632 complain_indent (&first->location, complaint, &i,
633 _("previous declaration for %s"),
634 first->tag);
12cf133f 635}
2f1afb73
AD
636
637/*--------------------------------------------------.
638| Put THIS in TOKEN_TRANSLATIONS if it is a token. |
639`--------------------------------------------------*/
640
0fb1efaf 641static inline bool
17ee7397 642symbol_translation (symbol *this)
2f1afb73
AD
643{
644 /* Non-terminal? */
645 if (this->class == token_sym
dfaa4860 646 && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
2f1afb73
AD
647 {
648 /* A token which translation has already been set? */
649 if (token_translations[this->user_token_number] != undeftoken->number)
e9690142 650 user_token_number_redeclaration
12cf133f
AD
651 (this->user_token_number,
652 symbols[token_translations[this->user_token_number]],
653 this);
f249e803
AD
654 else
655 token_translations[this->user_token_number] = this->number;
2f1afb73 656 }
9553083c 657
a3714bce 658 return true;
2f1afb73
AD
659}
660
0fb1efaf
PE
661static bool
662symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
663{
664 return symbol_translation (this);
665}
666
72a23c97 667
b2a0b7ca
JD
668/*---------------------------------------.
669| Symbol and semantic type hash tables. |
670`---------------------------------------*/
40675e7c 671
b2a0b7ca 672/* Initial capacity of symbol and semantic type hash table. */
72a23c97
AD
673#define HT_INITIAL_CAPACITY 257
674
db8837cb 675static struct hash_table *symbol_table = NULL;
b2a0b7ca 676static struct hash_table *semantic_type_table = NULL;
72a23c97 677
0fb1efaf 678static inline bool
17ee7397 679hash_compare_symbol (const symbol *m1, const symbol *m2)
72a23c97 680{
95612cfa 681 /* Since tags are unique, we can compare the pointers themselves. */
17ee7397 682 return UNIQSTR_EQ (m1->tag, m2->tag);
72a23c97
AD
683}
684
b2a0b7ca
JD
685static inline bool
686hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
687{
688 /* Since names are unique, we can compare the pointers themselves. */
689 return UNIQSTR_EQ (m1->tag, m2->tag);
690}
691
0fb1efaf
PE
692static bool
693hash_symbol_comparator (void const *m1, void const *m2)
694{
695 return hash_compare_symbol (m1, m2);
696}
697
b2a0b7ca
JD
698static bool
699hash_semantic_type_comparator (void const *m1, void const *m2)
700{
701 return hash_compare_semantic_type (m1, m2);
702}
703
233a88ad
PE
704static inline size_t
705hash_symbol (const symbol *m, size_t tablesize)
72a23c97 706{
95612cfa 707 /* Since tags are unique, we can hash the pointer itself. */
0fb1efaf
PE
708 return ((uintptr_t) m->tag) % tablesize;
709}
710
b2a0b7ca
JD
711static inline size_t
712hash_semantic_type (const semantic_type *m, size_t tablesize)
713{
714 /* Since names are unique, we can hash the pointer itself. */
715 return ((uintptr_t) m->tag) % tablesize;
716}
717
233a88ad
PE
718static size_t
719hash_symbol_hasher (void const *m, size_t tablesize)
0fb1efaf
PE
720{
721 return hash_symbol (m, tablesize);
72a23c97
AD
722}
723
b2a0b7ca
JD
724static size_t
725hash_semantic_type_hasher (void const *m, size_t tablesize)
726{
727 return hash_semantic_type (m, tablesize);
728}
72a23c97
AD
729
730/*-------------------------------.
2f1afb73 731| Create the symbol hash table. |
72a23c97
AD
732`-------------------------------*/
733
734void
db8837cb 735symbols_new (void)
72a23c97 736{
db8837cb 737 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
e9690142
JD
738 NULL,
739 hash_symbol_hasher,
740 hash_symbol_comparator,
741 free);
b2a0b7ca 742 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
e9690142
JD
743 NULL,
744 hash_semantic_type_hasher,
745 hash_semantic_type_comparator,
746 free);
40675e7c
DM
747}
748
749
1e9798d5
AD
750/*----------------------------------------------------------------.
751| Find the symbol named KEY, and return it. If it does not exist |
752| yet, create it. |
753`----------------------------------------------------------------*/
754
17ee7397 755symbol *
203b9274 756symbol_from_uniqstr (const uniqstr key, location loc)
40675e7c 757{
17ee7397
PE
758 symbol probe;
759 symbol *entry;
40675e7c 760
0fb1efaf 761 probe.tag = key;
db8837cb 762 entry = hash_lookup (symbol_table, &probe);
40675e7c 763
72a23c97 764 if (!entry)
40675e7c 765 {
72a23c97 766 /* First insertion in the hash. */
83b60c97 767 aver (!symbols_sorted);
17ee7397 768 entry = symbol_new (key, loc);
92d7a23a
AD
769 if (!hash_insert (symbol_table, entry))
770 xalloc_die ();
40675e7c 771 }
72a23c97
AD
772 return entry;
773}
40675e7c 774
40675e7c 775
b2a0b7ca
JD
776/*-----------------------------------------------------------------------.
777| Find the semantic type named KEY, and return it. If it does not exist |
778| yet, create it. |
779`-----------------------------------------------------------------------*/
780
781semantic_type *
9641b918 782semantic_type_from_uniqstr (const uniqstr key, const location *loc)
b2a0b7ca
JD
783{
784 semantic_type probe;
785 semantic_type *entry;
786
787 probe.tag = key;
788 entry = hash_lookup (semantic_type_table, &probe);
789
790 if (!entry)
791 {
792 /* First insertion in the hash. */
9641b918 793 entry = semantic_type_new (key, loc);
92d7a23a
AD
794 if (!hash_insert (semantic_type_table, entry))
795 xalloc_die ();
b2a0b7ca
JD
796 }
797 return entry;
798}
799
800
203b9274
AD
801/*----------------------------------------------------------------.
802| Find the symbol named KEY, and return it. If it does not exist |
803| yet, create it. |
804`----------------------------------------------------------------*/
805
806symbol *
807symbol_get (const char *key, location loc)
808{
809 return symbol_from_uniqstr (uniqstr_new (key), loc);
810}
811
812
b2a0b7ca
JD
813/*-----------------------------------------------------------------------.
814| Find the semantic type named KEY, and return it. If it does not exist |
815| yet, create it. |
816`-----------------------------------------------------------------------*/
817
818semantic_type *
9641b918 819semantic_type_get (const char *key, const location *loc)
b2a0b7ca 820{
9641b918 821 return semantic_type_from_uniqstr (uniqstr_new (key), loc);
b2a0b7ca
JD
822}
823
824
39f41916
AD
825/*------------------------------------------------------------------.
826| Generate a dummy nonterminal, whose name cannot conflict with the |
827| user's names. |
828`------------------------------------------------------------------*/
829
17ee7397
PE
830symbol *
831dummy_symbol_get (location loc)
39f41916
AD
832{
833 /* Incremented for each generated symbol. */
834 static int dummy_count = 0;
835 static char buf[256];
836
17ee7397 837 symbol *sym;
39f41916 838
f91b1629 839 sprintf (buf, "$@%d", ++dummy_count);
17ee7397 840 sym = symbol_get (buf, loc);
39f41916
AD
841 sym->class = nterm_sym;
842 sym->number = nvars++;
843 return sym;
844}
845
4d7370cb
JD
846bool
847symbol_is_dummy (const symbol *sym)
848{
f91b1629 849 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
4d7370cb 850}
39f41916 851
72a23c97 852/*-------------------.
db8837cb 853| Free the symbols. |
72a23c97
AD
854`-------------------*/
855
856void
db8837cb 857symbols_free (void)
72a23c97 858{
db8837cb 859 hash_free (symbol_table);
b2a0b7ca 860 hash_free (semantic_type_table);
536545f3 861 free (symbols);
83b60c97 862 free (symbols_sorted);
ae9c90ba 863 free (semantic_types_sorted);
40675e7c
DM
864}
865
866
72a23c97 867/*---------------------------------------------------------------.
db8837cb 868| Look for undefined symbols, report an error, and consider them |
72a23c97
AD
869| terminals. |
870`---------------------------------------------------------------*/
871
83b60c97
JD
872static int
873symbols_cmp (symbol const *a, symbol const *b)
874{
875 return strcmp (a->tag, b->tag);
876}
877
878static int
879symbols_cmp_qsort (void const *a, void const *b)
880{
881 return symbols_cmp (*(symbol * const *)a, *(symbol * const *)b);
882}
883
0fb1efaf 884static void
dfe292c6 885symbols_do (Hash_processor processor, void *processor_data,
ae9c90ba 886 struct hash_table *table, symbol ***sorted)
40675e7c 887{
dfe292c6 888 size_t count = hash_get_n_entries (table);
ae9c90ba 889 if (!*sorted)
83b60c97 890 {
ae9c90ba
TR
891 *sorted = xnmalloc (count, sizeof **sorted);
892 hash_get_entries (table, (void**)*sorted, count);
893 qsort (*sorted, count, sizeof **sorted, symbols_cmp_qsort);
83b60c97
JD
894 }
895 {
896 size_t i;
897 for (i = 0; i < count; ++i)
ae9c90ba 898 processor ((*sorted)[i], processor_data);
83b60c97 899 }
40675e7c 900}
2f1afb73 901
2f1afb73
AD
902/*--------------------------------------------------------------.
903| Check that all the symbols are defined. Report any undefined |
904| symbols and consider them nonterminals. |
905`--------------------------------------------------------------*/
906
907void
908symbols_check_defined (void)
909{
dfe292c6 910 symbols_do (symbol_check_defined_processor, NULL,
ae9c90ba 911 symbol_table, &symbols_sorted);
9641b918 912 symbols_do (semantic_type_check_defined_processor, NULL,
ae9c90ba 913 semantic_type_table, &semantic_types_sorted);
2f1afb73
AD
914}
915
916/*------------------------------------------------------------------.
917| Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
918| number. |
919`------------------------------------------------------------------*/
920
921static void
922symbols_token_translations_init (void)
923{
a3714bce 924 bool num_256_available_p = true;
2f1afb73
AD
925 int i;
926
927 /* Find the highest user token number, and whether 256, the POSIX
928 preferred user token number for the error token, is used. */
929 max_user_token_number = 0;
930 for (i = 0; i < ntokens; ++i)
931 {
17ee7397 932 symbol *this = symbols[i];
2f1afb73 933 if (this->user_token_number != USER_NUMBER_UNDEFINED)
e9690142
JD
934 {
935 if (this->user_token_number > max_user_token_number)
936 max_user_token_number = this->user_token_number;
937 if (this->user_token_number == 256)
938 num_256_available_p = false;
939 }
2f1afb73
AD
940 }
941
942 /* If 256 is not used, assign it to error, to follow POSIX. */
943 if (num_256_available_p
944 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
945 errtoken->user_token_number = 256;
946
947 /* Set the missing user numbers. */
948 if (max_user_token_number < 256)
949 max_user_token_number = 256;
950
951 for (i = 0; i < ntokens; ++i)
952 {
17ee7397 953 symbol *this = symbols[i];
2f1afb73 954 if (this->user_token_number == USER_NUMBER_UNDEFINED)
e9690142 955 this->user_token_number = ++max_user_token_number;
2f1afb73 956 if (this->user_token_number > max_user_token_number)
e9690142 957 max_user_token_number = this->user_token_number;
2f1afb73
AD
958 }
959
da2a7671 960 token_translations = xnmalloc (max_user_token_number + 1,
e9690142 961 sizeof *token_translations);
2f1afb73 962
b3a28fd4
AD
963 /* Initialize all entries for literal tokens to the internal token
964 number for $undefined, which represents all invalid inputs. */
2f1afb73
AD
965 for (i = 0; i < max_user_token_number + 1; i++)
966 token_translations[i] = undeftoken->number;
dfe292c6 967 symbols_do (symbol_translation_processor, NULL,
ae9c90ba 968 symbol_table, &symbols_sorted);
2f1afb73
AD
969}
970
971
972/*----------------------------------------------------------------.
973| Assign symbol numbers, and write definition of token names into |
974| FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
975`----------------------------------------------------------------*/
976
977void
978symbols_pack (void)
979{
dfe292c6 980 symbols_do (symbol_check_alias_consistency_processor, NULL,
ae9c90ba 981 symbol_table, &symbols_sorted);
6d0ef4ec
JD
982
983 symbols = xcalloc (nsyms, sizeof *symbols);
ae9c90ba 984 symbols_do (symbol_pack_processor, NULL, symbol_table, &symbols_sorted);
2f1afb73 985
6d0ef4ec
JD
986 /* Aliases leave empty slots in symbols, so remove them. */
987 {
988 int writei;
989 int readi;
990 int nsyms_old = nsyms;
991 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
992 {
993 if (symbols[readi] == NULL)
994 {
995 nsyms -= 1;
996 ntokens -= 1;
997 }
998 else
999 {
1000 symbols[writei] = symbols[readi];
1001 symbols[writei]->number = writei;
1002 if (symbols[writei]->alias)
1003 symbols[writei]->alias->number = writei;
1004 writei += 1;
1005 }
1006 }
1007 }
1008 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
1009
2f1afb73
AD
1010 symbols_token_translations_init ();
1011
1012 if (startsymbol->class == unknown_sym)
bb8e56ff
TR
1013 complain (&startsymbol_location, fatal,
1014 _("the start symbol %s is undefined"),
1015 startsymbol->tag);
2f1afb73 1016 else if (startsymbol->class == token_sym)
bb8e56ff
TR
1017 complain (&startsymbol_location, fatal,
1018 _("the start symbol %s is a token"),
1019 startsymbol->tag);
2f1afb73 1020}
284bc49c
VT
1021
1022/*---------------------------------.
1023| Initialize relation graph nodes. |
1024`---------------------------------*/
1025
1026static void
1027init_prec_nodes (void)
1028{
1029 int i;
1030 prec_nodes = xcalloc (nsyms, sizeof *prec_nodes);
1031 for (i = 0; i < nsyms; ++i)
1032 {
1033 prec_nodes[i] = xmalloc (sizeof *prec_nodes[i]);
1034 symgraph *s = prec_nodes[i];
1035 s->id = i;
1036 s->succ = 0;
1037 s->pred = 0;
1038 }
1039}
1040
1041/*----------------.
1042| Create a link. |
1043`----------------*/
1044
1045static symgraphlink *
1046symgraphlink_new (graphid id, symgraphlink *next)
1047{
1048 symgraphlink *l = xmalloc (sizeof *l);
1049 l->id = id;
1050 l->next = next;
1051 return l;
1052}
1053
1054
1055/*------------------------------------------------------------------.
1056| Register the second symbol of the precedence relation, and return |
1057| whether this relation is new. Use only in register_precedence. |
1058`------------------------------------------------------------------*/
1059
1060static bool
1061register_precedence_second_symbol (symgraphlink **first, graphid sym)
1062{
1063 if (!*first || sym < (*first)->id)
1064 *first = symgraphlink_new (sym, *first);
1065 else
1066 {
1067 symgraphlink *slist = *first;
1068
1069 while (slist->next && slist->next->id <= sym)
1070 slist = slist->next;
1071
1072 if (slist->id == sym)
1073 /* Relation already present. */
1074 return false;
1075
1076 slist->next = symgraphlink_new (sym, slist->next);
1077 }
1078 return true;
1079}
1080
1081/*------------------------------------------------------------------.
1082| Register a new relation between symbols as used. The first symbol |
1083| has a greater precedence than the second one. |
1084`------------------------------------------------------------------*/
1085
1086void
1087register_precedence (graphid first, graphid snd)
1088{
1089 if (!prec_nodes)
1090 init_prec_nodes ();
1091 register_precedence_second_symbol (&(prec_nodes[first]->succ), snd);
1092 register_precedence_second_symbol (&(prec_nodes[snd]->pred), first);
1093}
1094
1095
f038e56c
TR
1096/*---------------------------------------.
1097| Deep clear a linked / adjacency list). |
1098`---------------------------------------*/
1099
1100static void
1101linkedlist_free (symgraphlink *node)
1102{
1103 if (node)
1104 {
1105 while (node->next)
1106 {
1107 symgraphlink *tmp = node->next;
1108 free (node);
1109 node = tmp;
1110 }
1111 free (node);
1112 }
1113}
1114
1115/*----------------------------------------------.
1116| Clear and destroy association tracking table. |
1117`----------------------------------------------*/
1118
1119static void
1120assoc_free (void)
1121{
1122 int i;
1123 for (i = 0; i < nsyms; ++i)
1124 {
1125 linkedlist_free (prec_nodes[i]->pred);
1126 linkedlist_free (prec_nodes[i]->succ);
1127 free (prec_nodes[i]);
1128 }
1129 free (prec_nodes);
1130}
1131
e8f7155d
VT
1132/*---------------------------------------.
1133| Initialize association tracking table. |
1134`---------------------------------------*/
1135
1136static void
1137init_assoc (void)
1138{
1139 graphid i;
c0ef22ab 1140 used_assoc = xcalloc (nsyms, sizeof *used_assoc);
e8f7155d
VT
1141 for (i = 0; i < nsyms; ++i)
1142 used_assoc[i] = false;
1143}
1144
1145/*------------------------------------------------------------------.
1146| Test if the associativity for the symbols is defined and useless. |
1147`------------------------------------------------------------------*/
1148
1149static inline bool
1150is_assoc_useless (symbol *s)
1151{
1152 return s
1153 && s->assoc != undef_assoc
1154 && s->assoc != precedence_assoc
1155 && !used_assoc[s->number];
1156}
1157
1158/*-------------------------------.
1159| Register a used associativity. |
1160`-------------------------------*/
1161
1162void
1163register_assoc (graphid i, graphid j)
1164{
1165 if (!used_assoc)
1166 init_assoc ();
1167 used_assoc[i] = true;
1168 used_assoc[j] = true;
1169}
1170
cc2235ac
VT
1171/*--------------------------------------------------.
1172| Print a warning for unused precedence relations. |
1173`--------------------------------------------------*/
e8f7155d
VT
1174
1175void
cc2235ac 1176print_precedence_warnings (void)
e8f7155d 1177{
cc2235ac
VT
1178 int i;
1179 if (!prec_nodes)
1180 init_prec_nodes ();
e8f7155d
VT
1181 if (!used_assoc)
1182 init_assoc ();
1183 for (i = 0; i < nsyms; ++i)
1184 {
1185 symbol *s = symbols[i];
cc2235ac
VT
1186 if (s
1187 && s->prec != 0
1188 && !prec_nodes[i]->pred
1189 && !prec_nodes[i]->succ)
1190 {
1191 if (is_assoc_useless (s))
d8ce7031 1192 complain (&s->prec_location, Wprecedence,
cc2235ac
VT
1193 _("useless precedence and associativity for %s"), s->tag);
1194 else if (s->assoc == precedence_assoc)
d8ce7031 1195 complain (&s->prec_location, Wprecedence,
cc2235ac
VT
1196 _("useless precedence for %s"), s->tag);
1197 }
1198 else if (is_assoc_useless (s))
d8ce7031 1199 complain (&s->prec_location, Wprecedence,
cc2235ac 1200 _("useless associativity for %s, use %%precedence"), s->tag);
e8f7155d 1201 }
f038e56c
TR
1202 free (used_assoc);
1203 assoc_free ();
e8f7155d 1204}