X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/76290368d48b584808a120dfbd415be93aef04f6..4654b0c0a8a355e7a8fcdf51b1a5eb6c485126f1:/src/symtab.c
diff --git a/src/symtab.c b/src/symtab.c
index 81a938ce..b65bdd48 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -1,24 +1,22 @@
/* Symbol table manager for Bison.
- Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006 Free
- Software Foundation, Inc.
+ Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008
+ Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
- Bison is free software; you can redistribute it and/or modify
+ This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- Bison is distributed in the hope that it will be useful,
+ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with Bison; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA. */
+ along with this program. If not, see . */
#include
#include "system.h"
@@ -109,9 +107,9 @@ semantic_type_new (uniqstr tag)
if (s->Attr) \
fprintf (f, " %s { %s }", #Attr, s->Attr)
-#define SYMBOL_CODE_PRINT(Attr) \
- if (code_props_code_get (s->Attr)) \
- fprintf (f, " %s { %s }", #Attr, code_props_code_get(s->Attr))
+#define SYMBOL_CODE_PRINT(Attr) \
+ if (s->Attr.code) \
+ fprintf (f, " %s { %s }", #Attr, s->Attr.code)
void
symbol_print (symbol *s, FILE *f)
@@ -130,6 +128,41 @@ symbol_print (symbol *s, FILE *f)
#undef SYMBOL_ATTR_PRINT
#undef SYMBOL_CODE_PRINT
+
+/*----------------------------------.
+| Whether S is a valid identifier. |
+`----------------------------------*/
+
+static bool
+is_identifier (uniqstr s)
+{
+ static char const alphanum[26 + 26 + 1 + 10] =
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "_"
+ "0123456789";
+ if (!s || ! memchr (alphanum, *s, sizeof alphanum - 10))
+ return false;
+ for (++s; *s; ++s)
+ if (! memchr (alphanum, *s, sizeof alphanum))
+ return false;
+ return true;
+}
+
+
+/*-----------------------------------------------.
+| Get the identifier associated to this symbol. |
+`-----------------------------------------------*/
+uniqstr
+symbol_id_get (symbol const *sym)
+{
+ aver (sym->user_token_number != USER_NUMBER_ALIAS);
+ if (sym->alias)
+ sym = sym->alias;
+ return is_identifier (sym->tag) ? sym->tag : 0;
+}
+
+
/*------------------------------------------------------------------.
| Complain that S's WHAT is redeclared at SECOND, and was first set |
| at FIRST. |
@@ -170,19 +203,35 @@ symbol_type_set (symbol *sym, uniqstr type_name, location loc)
}
}
+/*-----------------------------------.
+| Get the CLASS associated with SYM. |
+`-----------------------------------*/
+
+const char *
+symbol_class_get_string (symbol *sym)
+{
+ if (sym->class)
+ {
+ if (sym->class == token_sym)
+ return "terminal";
+ else if (sym->class == nterm_sym)
+ return "nonterminal";
+ }
+ return "unknown";
+}
+
/*-----------------------------------------.
| Set the DESTRUCTOR associated with SYM. |
`-----------------------------------------*/
void
-symbol_destructor_set (symbol *sym, code_props destructor)
+symbol_destructor_set (symbol *sym, code_props const *destructor)
{
- if (code_props_code_get (sym->destructor))
- symbol_redeclaration (sym, "%destructor",
- code_props_location_get (sym->destructor),
- code_props_location_get (destructor));
- sym->destructor = destructor;
+ if (sym->destructor.code)
+ symbol_redeclaration (sym, "%destructor", sym->destructor.location,
+ destructor->location);
+ sym->destructor = *destructor;
}
/*------------------------------------------.
@@ -190,41 +239,43 @@ symbol_destructor_set (symbol *sym, code_props destructor)
`------------------------------------------*/
void
-semantic_type_destructor_set (semantic_type *type, code_props destructor)
+semantic_type_destructor_set (semantic_type *type,
+ code_props const *destructor)
{
- if (code_props_code_get (type->destructor))
+ if (type->destructor.code)
semantic_type_redeclaration (type, "%destructor",
- code_props_location_get (type->destructor),
- code_props_location_get (destructor));
- type->destructor = destructor;
+ type->destructor.location,
+ destructor->location);
+ type->destructor = *destructor;
}
/*---------------------------------------.
| Get the computed %destructor for SYM. |
`---------------------------------------*/
-code_props
-symbol_destructor_get (symbol *sym)
+code_props const *
+symbol_destructor_get (symbol const *sym)
{
/* Per-symbol %destructor. */
- if (code_props_code_get (sym->destructor))
- return sym->destructor;
+ if (sym->destructor.code)
+ return &sym->destructor;
/* Per-type %destructor. */
if (sym->type_name)
{
- code_props destructor = semantic_type_get (sym->type_name)->destructor;
- if (code_props_code_get (destructor))
+ code_props const *destructor =
+ &semantic_type_get (sym->type_name)->destructor;
+ if (destructor->code)
return destructor;
}
/* Apply default %destructor's only to user-defined symbols. */
if (sym->tag[0] == '$' || sym == errtoken)
- return code_props_none;
+ return &code_props_none;
if (sym->type_name)
- return default_tagged_destructor;
- return default_tagless_destructor;
+ return &default_tagged_destructor;
+ return &default_tagless_destructor;
}
/*--------------------------------------.
@@ -232,13 +283,12 @@ symbol_destructor_get (symbol *sym)
`--------------------------------------*/
void
-symbol_printer_set (symbol *sym, code_props printer)
+symbol_printer_set (symbol *sym, code_props const *printer)
{
- if (code_props_code_get (sym->printer))
+ if (sym->printer.code)
symbol_redeclaration (sym, "%printer",
- code_props_location_get (sym->printer),
- code_props_location_get (printer));
- sym->printer = printer;
+ sym->printer.location, printer->location);
+ sym->printer = *printer;
}
/*---------------------------------------.
@@ -246,41 +296,40 @@ symbol_printer_set (symbol *sym, code_props printer)
`---------------------------------------*/
void
-semantic_type_printer_set (semantic_type *type, code_props printer)
+semantic_type_printer_set (semantic_type *type, code_props const *printer)
{
- if (code_props_code_get (type->printer))
+ if (type->printer.code)
semantic_type_redeclaration (type, "%printer",
- code_props_location_get (type->printer),
- code_props_location_get (printer));
- type->printer = printer;
+ type->printer.location, printer->location);
+ type->printer = *printer;
}
/*------------------------------------.
| Get the computed %printer for SYM. |
`------------------------------------*/
-code_props
-symbol_printer_get (symbol *sym)
+code_props const *
+symbol_printer_get (symbol const *sym)
{
/* Per-symbol %printer. */
- if (code_props_code_get (sym->printer))
- return sym->printer;
+ if (sym->printer.code)
+ return &sym->printer;
/* Per-type %printer. */
if (sym->type_name)
{
- code_props printer = semantic_type_get (sym->type_name)->printer;
- if (code_props_code_get (printer))
+ code_props const *printer = &semantic_type_get (sym->type_name)->printer;
+ if (printer->code)
return printer;
}
/* Apply the default %printer only to user-defined symbols. */
if (sym->tag[0] == '$' || sym == errtoken)
- return code_props_none;
+ return &code_props_none;
if (sym->type_name)
- return default_tagged_printer;
- return default_tagless_printer;
+ return &default_tagged_printer;
+ return &default_tagless_printer;
}
/*-----------------------------------------------------------------.
@@ -344,8 +393,6 @@ symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
{
int *user_token_numberp;
- aver (sym->class == token_sym);
-
if (sym->user_token_number != USER_NUMBER_ALIAS)
user_token_numberp = &sym->user_token_number;
else
@@ -446,22 +493,20 @@ symbol_check_alias_consistency (symbol *this)
}
- if (code_props_code_get (orig->destructor)
- || code_props_code_get (alias->destructor))
+ if (orig->destructor.code || alias->destructor.code)
{
- if (code_props_code_get (orig->destructor))
- symbol_destructor_set (alias, orig->destructor);
+ if (orig->destructor.code)
+ symbol_destructor_set (alias, &orig->destructor);
else
- symbol_destructor_set (orig, alias->destructor);
+ symbol_destructor_set (orig, &alias->destructor);
}
- if (code_props_code_get (orig->printer)
- || code_props_code_get (alias->printer))
+ if (orig->printer.code || alias->printer.code)
{
- if (code_props_code_get (orig->printer))
- symbol_printer_set (alias, orig->printer);
+ if (orig->printer.code)
+ symbol_printer_set (alias, &orig->printer);
else
- symbol_printer_set (orig, alias->printer);
+ symbol_printer_set (orig, &alias->printer);
}
if (alias->prec || orig->prec)
@@ -826,9 +871,8 @@ symbols_token_translations_init (void)
token_translations = xnmalloc (max_user_token_number + 1,
sizeof *token_translations);
- /* Initialize all entries for literal tokens to 2, the internal
- token number for $undefined, which represents all invalid inputs.
- */
+ /* Initialize all entries for literal tokens to the internal token
+ number for $undefined, which represents all invalid inputs. */
for (i = 0; i < max_user_token_number + 1; i++)
token_translations[i] = undeftoken->number;
symbols_do (symbol_translation_processor, NULL);
@@ -890,54 +934,53 @@ symbols_pack (void)
`--------------------------------------------------*/
void
-default_tagged_destructor_set (code_props destructor)
+default_tagged_destructor_set (code_props const *destructor)
{
- if (code_props_code_get (default_tagged_destructor))
+ if (default_tagged_destructor.code)
{
- complain_at (code_props_location_get (destructor),
+ complain_at (destructor->location,
_("redeclaration for default tagged %%destructor"));
- complain_at (code_props_location_get (default_tagged_destructor),
+ complain_at (default_tagged_destructor.location,
_("previous declaration"));
}
- default_tagged_destructor = destructor;
+ default_tagged_destructor = *destructor;
}
void
-default_tagless_destructor_set (code_props destructor)
+default_tagless_destructor_set (code_props const *destructor)
{
- if (code_props_code_get (default_tagless_destructor))
+ if (default_tagless_destructor.code)
{
- complain_at (code_props_location_get (destructor),
+ complain_at (destructor->location,
_("redeclaration for default tagless %%destructor"));
- complain_at (code_props_location_get (default_tagless_destructor),
+ complain_at (default_tagless_destructor.location,
_("previous declaration"));
}
- default_tagless_destructor = destructor;
+ default_tagless_destructor = *destructor;
}
void
-default_tagged_printer_set (code_props printer)
+default_tagged_printer_set (code_props const *printer)
{
- if (code_props_code_get (default_tagged_printer))
+ if (default_tagged_printer.code)
{
- complain_at (code_props_location_get (printer),
+ complain_at (printer->location,
_("redeclaration for default tagged %%printer"));
- complain_at (code_props_location_get (default_tagged_printer),
+ complain_at (default_tagged_printer.location,
_("previous declaration"));
}
- default_tagged_printer = printer;
+ default_tagged_printer = *printer;
}
void
-default_tagless_printer_set (code_props printer)
+default_tagless_printer_set (code_props const *printer)
{
- if (code_props_code_get (default_tagless_printer))
+ if (default_tagless_printer.code)
{
- complain_at (code_props_location_get (printer),
+ complain_at (printer->location,
_("redeclaration for default tagless %%printer"));
- complain_at (code_props_location_get (default_tagless_printer),
+ complain_at (default_tagless_printer.location,
_("previous declaration"));
}
- default_tagless_printer = printer;
+ default_tagless_printer = *printer;
}
-