glr2.cc: example: use objects (not pointers) to represent the AST

Currently we are using pointers.  The whole point of
glr2.cc (vs. glr.cc) is precisely to allow genuine C++ objects to be
semantic values.  Let's make that work.

* data/skeletons/glr2.cc (glr_state::glr_state): Be sure to initialize
yysval.
(glr_state): Add copy-ctor, assignment and dtor.
(glr_state::copyFrom): Be sure to initialize the destination if it was
not.
(glr_state::~glr_state): Destroy the semantic value.
* examples/c++/glr/ast.hh: Rewrite so that we use genuine objects,
rather than a traditional OOP hierarchy that requires to deal with
pointers.
With help from Bruno Belanyi <bruno.belanyi@epita.fr>.
* examples/c++/glr/c++-types.yy: Remove memory management.
Use true objects.
(main): Don't reach yydebug directly.

* examples/c++/glr/local.mk: We need C++11.
This commit is contained in:
Akim Demaille
2020-12-14 20:26:41 +01:00
parent 611348e67b
commit 0a82316e54
7 changed files with 177 additions and 89 deletions

View File

@@ -779,16 +779,15 @@ public:
{}
/// Build with a semantic value.
glr_state(state_num lrState, size_t posn, YYSTYPE sval]b4_locations_if([[, YYLTYPE loc]])[)
: yyresolved(true)
, yylrState(lrState)
, yyposn(posn)
, yypred(0)]b4_locations_if([[
, yyloc(loc)]])[]b4_parse_assert_if([[
glr_state (state_num lrState, size_t posn, YYSTYPE sval]b4_locations_if([[, YYLTYPE loc]])[)
: yyresolved (true)
, yylrState (lrState)
, yyposn (posn)
, yypred (0)
, yysval (sval)]b4_locations_if([[
, yyloc (loc)]])[]b4_parse_assert_if([[
, magic_ (MAGIC)]])[
{
semanticVal() = sval;
}
{}
/// Build with a semantic option.
glr_state(state_num lrState, size_t posn)
@@ -800,17 +799,43 @@ public:
, magic_ (MAGIC)]])[
{}
glr_state (const glr_state& other)]b4_parse_assert_if([[
: magic_ (MAGIC)]])[
{
// FIXME: Do it right.
copyFrom (other);
}
~glr_state ()
{]b4_parse_assert_if([[
check_ ();
magic_ = 0;]])[
if (yyresolved)
yysval.YYSTYPE::~semantic_type ();
}
glr_state& operator= (const glr_state& other)
{
copyFrom (other);
return *this;
}
void copyFrom (const glr_state& other)
{]b4_parse_assert_if([[
check_ ();
other.check_ ();]])[
*this = other;
if (!yyresolved && other.yyresolved)
new (&yysval) YYSTYPE;
yyresolved = other.yyresolved;
yylrState = other.yylrState;
yyposn = other.yyposn;
setPred(other.pred());
if (other.yyresolved) {
semanticVal() = other.semanticVal();
} else {
setFirstVal(other.firstVal());
}
}]b4_locations_if([[
yyloc = other.yyloc;]])[
}
/** Type tag for the semantic value. If true, yysval applies, otherwise
@@ -1691,7 +1716,7 @@ public:
#endif
yys.yyresolved = s->yyresolved;
if (s->yyresolved)
yys.semanticVal() = s->semanticVal();
new (&yys.semanticVal()) YYSTYPE(s->semanticVal());
else
/* The effect of using semanticVal or yyloc (in an immediate rule) is
* undefined. */
@@ -2633,7 +2658,7 @@ public:
{
yys.yyresolved = true;
YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
yys.semanticVal() = yysval;
new (&yys.semanticVal()) YYSTYPE(yysval);
YY_IGNORE_MAYBE_UNINITIALIZED_END
}
else