mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
c++: revamp the support for variants
The current approach was too adhoc: the symbols were not sufficiently self-contained, in particular wrt memory management. The "new" guideline is the one that should have been followed from the start: let the symbols handle themslves, instead of leaving their users to it. It was justified by the will to avoid gratuitious moves and copies, but the current approach does not seem to be slower, yet it will probably be simpler to adjust to support move semantics from C++11. The documentation says that the %parse-param are available from the %destructor. In retrospect, that was a silly design decision, which we can break for variants, as its a new feature. It should be phased out for non-variants too. * data/variant.hh: A variant never knows if it stores something or not, it is up to its users to store this information. Yet, in parse.assert mode, make sure the empty/filled variants are properly used. (b4_symbol_constructor_define_): Don't call directly the symbol constructor, to save a useless temporary. * data/stack.hh (push): Steal the pushed value instead of duplicating it. This will simplify the callers of push, who handled this "move" approach themselves. * data/c++.m4 (basic_symbol): Let -1, as kind, denote the fact that a symbol is empty. This is needed for instance when shifting the lookahead: yyla is given as argument to "push", and its value is then moved on the stack. But then yyla must be declared "empty" so that its destructor won't be called. (basic_symbol::move): New. Move the responsibility of calling the destructor from yy_destroy to ~basic_symbol in the case of variants. * data/lalr1.cc (stack_symbol_type): Now a derived class from its previous value, so that we can add a constructor from a symbol_type. (by_state): State -1 means empty. (yypush_): Factor, by calling one overload from the other one, and using the new semantics of stack::push. No longer reclaim by hand the memory from rhs symbols, since now that we store objects with proper destructors, they will be reclaimed automatically. Conversely, be sure to delete yylhs. * tests/c++.at (C++ Variant-based Symbols): New "unit" test for symbols.
This commit is contained in:
60
data/c++.m4
60
data/c++.m4
@@ -166,6 +166,9 @@ m4_define([b4_public_types_declare],
|
||||
template <typename Base>
|
||||
struct basic_symbol : Base
|
||||
{
|
||||
/// Alias to Base.
|
||||
typedef Base super_type;
|
||||
|
||||
/// Default constructor.
|
||||
inline basic_symbol ();
|
||||
]b4_locations_if([
|
||||
@@ -184,6 +187,7 @@ m4_define([b4_public_types_declare],
|
||||
const semantic_type& v]b4_locations_if([,
|
||||
const location_type& l])[);
|
||||
|
||||
~basic_symbol ();
|
||||
/// Assignment operator.
|
||||
inline basic_symbol& operator= (const basic_symbol& other);
|
||||
|
||||
@@ -209,10 +213,17 @@ m4_define([b4_public_types_declare],
|
||||
/// Constructor.
|
||||
inline by_type (token_type t);
|
||||
|
||||
/// Steal the type of \a that.
|
||||
void move (by_type& that);
|
||||
|
||||
/// The symbol type.
|
||||
///
|
||||
/// -1 when this symbol is empty.
|
||||
int type;
|
||||
|
||||
/// The type (corresponding to \a type).
|
||||
///
|
||||
/// -1 when this symbol is empty.
|
||||
inline int type_get () const;
|
||||
|
||||
/// The token.
|
||||
@@ -275,39 +286,52 @@ m4_define([b4_public_types_define],
|
||||
[const semantic_type& v],
|
||||
b4_locations_if([const location_type& l]))[)
|
||||
: Base (t)
|
||||
, value ()]b4_locations_if([
|
||||
, value (]b4_variant_if([], [v])[)]b4_locations_if([
|
||||
, location (l)])[
|
||||
{
|
||||
// FIXME: The YYUSE macro is only available in the .cc skeleton files. It
|
||||
// is not available in .hh files, where this code is when using %defines.
|
||||
{]b4_variant_if([[
|
||||
(void) v;
|
||||
]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [copy],
|
||||
[v])],
|
||||
[value = v;])[
|
||||
}
|
||||
]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
|
||||
|
||||
template <typename Base>
|
||||
]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
|
||||
[typename Base::value_type t],
|
||||
b4_locations_if([const location_type& l]))[)
|
||||
: Base (t)]b4_locations_if([
|
||||
: Base (t)
|
||||
, value ()]b4_locations_if([
|
||||
, location (l)])[
|
||||
{}
|
||||
|
||||
template <typename Base>
|
||||
inline
|
||||
]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
|
||||
{]b4_variant_if([[
|
||||
// User destructor.
|
||||
int yytype = this->type_get ();
|
||||
switch (yytype)
|
||||
{
|
||||
]b4_symbol_foreach([b4_symbol_destructor])dnl
|
||||
[ default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Type destructor.
|
||||
]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
|
||||
}
|
||||
|
||||
template <typename Base>
|
||||
void
|
||||
]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
|
||||
{
|
||||
this->type = s.type_get ();]b4_locations_if([
|
||||
location = s.location;])[
|
||||
]b4_variant_if([b4_symbol_variant([s.type_get ()], [value], [move],
|
||||
super_type::move(s);
|
||||
]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
|
||||
[s.value])],
|
||||
[value = s.value;])[
|
||||
[value = s.value;])[]b4_locations_if([
|
||||
location = s.location;])[
|
||||
}
|
||||
|
||||
// by_type.
|
||||
]b4_parser_class_name[::by_type::by_type ()
|
||||
: type ()
|
||||
: type (-1)
|
||||
{}
|
||||
|
||||
]b4_parser_class_name[::by_type::by_type (const by_type& other)
|
||||
@@ -318,6 +342,14 @@ m4_define([b4_public_types_define],
|
||||
: type (yytranslate_ (t))
|
||||
{}
|
||||
|
||||
inline
|
||||
void
|
||||
]b4_parser_class_name[::by_type::move (by_type& that)
|
||||
{
|
||||
type = that.type;
|
||||
that.type = -1;
|
||||
}
|
||||
|
||||
int
|
||||
]b4_parser_class_name[::by_type::type_get () const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user