c++: variants: fuse declarations and definitions

We used to create a short definition of yy::parser with all the
implementations of its member functions outside.  But yy::parser is no
longer short and simple to read.  Maintaining each function twice is
painful: a lot of redundancy but different indentation levels, output
which depends on whether we are in a header or not (see
d132c2d545), etc.

Let's simplify this and put the implementations into the class
definition itself.

Discussed in this monologue:
https://lists.gnu.org/archive/html/bison-patches/2018-12/msg00058.html.

* data/skeletons/c++.m4, data/skeletons/lalr1.cc,
* data/skeletons/variant.hh (b4_basic_symbol_constructor_define)
(_b4_token_constructor_declare, b4_token_constructor_declare)
Merge into...
(b4_basic_symbol_constructor_define, _b4_token_constructor_define)
(b4_token_constructor_define): these.
This commit is contained in:
Akim Demaille
2018-12-26 09:04:07 +01:00
parent 50285ff066
commit 5fb0d276b3
3 changed files with 82 additions and 154 deletions

View File

@@ -238,11 +238,11 @@ m4_define([b4_public_types_declare],
]]) ]])
# b4_symbol_type_declare # b4_symbol_type_define
# ---------------------- # ---------------------
# Define symbol_type, the external type for symbols used for symbol # Define symbol_type, the external type for symbols used for symbol
# constructors. # constructors.
m4_define([b4_symbol_type_declare], m4_define([b4_symbol_type_define],
[[ /// A complete symbol. [[ /// A complete symbol.
/// ///
/// Expects its Base type to provide access to the symbol type /// Expects its Base type to provide access to the symbol type
@@ -267,7 +267,7 @@ m4_define([b4_symbol_type_declare],
basic_symbol (const basic_symbol& that);]b4_variant_if([[ basic_symbol (const basic_symbol& that);]b4_variant_if([[
/// Constructor for valueless symbols, and symbols from each type. /// Constructor for valueless symbols, and symbols from each type.
]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[ ]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
/// Constructor for valueless symbols. /// Constructor for valueless symbols.
basic_symbol (typename Base::kind_type t]b4_locations_if([, basic_symbol (typename Base::kind_type t]b4_locations_if([,
YY_MOVE_REF (location_type) l])[); YY_MOVE_REF (location_type) l])[);
@@ -351,7 +351,7 @@ m4_define([b4_symbol_type_declare],
symbol_type () {}; symbol_type () {};
/// Constructor for valueless symbols, and symbols from each type. /// Constructor for valueless symbols, and symbols from each type.
]b4_type_foreach([_b4_token_constructor_declare])dnl ]b4_type_foreach([_b4_token_constructor_define])dnl
])[}; ])[};
]]) ]])
@@ -394,9 +394,7 @@ m4_define([b4_public_types_define],
[YY_MOVE (that.value)]) [YY_MOVE (that.value)])
])[} ])[}
]b4_variant_if([[ ]b4_variant_if([], [[
// Implementation of basic_symbol constructor for each type.
]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
/// Constructor for valueless symbols. /// Constructor for valueless symbols.
template <typename Base> template <typename Base>
]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join( ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
@@ -517,16 +515,13 @@ m4_define([b4_public_types_define],
return token_type (yytoken_number_[type]); return token_type (yytoken_number_[type]);
} }
]])[]dnl ]])[]dnl
])
b4_token_constructor_define])
# b4_token_constructor_declare
# b4_tolen_constructor_define # b4_tolen_constructor_define
# ---------------------------- # ----------------------------
# Declare/define symbol constructors for all the value types. # Define symbol constructors for all the value types.
# Use at class-level. Redefined in variant.hh. # Use at class-level. Redefined in variant.hh.
m4_define([b4_token_constructor_declare], [])
m4_define([b4_token_constructor_define], []) m4_define([b4_token_constructor_define], [])

View File

@@ -180,7 +180,7 @@ m4_define([b4_shared_declarations],
{ {
public: public:
]b4_public_types_declare[ ]b4_public_types_declare[
]b4_symbol_type_declare[ ]b4_symbol_type_define[
/// Build a parser object. /// Build a parser object.
]b4_parser_class_name[ (]b4_parse_param_decl[); ]b4_parser_class_name[ (]b4_parse_param_decl[);
virtual ~]b4_parser_class_name[ (); virtual ~]b4_parser_class_name[ ();
@@ -215,7 +215,7 @@ m4_define([b4_shared_declarations],
/// Report a syntax error. /// Report a syntax error.
void error (const syntax_error& err); void error (const syntax_error& err);
]b4_token_constructor_declare[ ]b4_token_constructor_define[
private: private:
/// This class is not copyable. /// This class is not copyable.

View File

@@ -87,7 +87,7 @@ m4_define([b4_variant_includes],
# b4_value_type_declare # b4_value_type_declare
# --------------------- # ---------------------
# Declare semantic_type. # Define semantic_type.
m4_define([b4_value_type_declare], m4_define([b4_value_type_declare],
[[ /// A buffer to store and retrieve objects. [[ /// A buffer to store and retrieve objects.
/// ///
@@ -345,49 +345,78 @@ m4_define([_b4_includes_tokens],
[m4_map([_b4_is_token], [$@])]) [m4_map([_b4_is_token], [$@])])
# _b4_token_maker_declare(SYMBOL-NUM) # _b4_token_maker_define(SYMBOL-NUM)
# ----------------------------------- # ----------------------------------
# Declare make_SYMBOL for SYMBOL-NUM. Use at class-level. # Declare make_SYMBOL for SYMBOL-NUM. Use at class-level.
m4_define([_b4_token_maker_declare], m4_define([_b4_token_maker_define],
[b4_token_visible_if([$1], [b4_token_visible_if([$1],
[#if 201103L <= YY_CPLUSPLUS [#if 201103L <= YY_CPLUSPLUS
static static
symbol_type symbol_type
make_[]_b4_symbol([$1], [id]) (b4_join( make_[]_b4_symbol([$1], [id]) (b4_join(
b4_symbol_if([$1], [has_type], b4_symbol_if([$1], [has_type],
[b4_symbol([$1], [type]) v]), [b4_symbol([$1], [type]) v]),
b4_locations_if([location_type l]))); b4_locations_if([location_type l])))
{
return symbol_type (b4_join([token::b4_symbol([$1], [id])],
b4_symbol_if([$1], [has_type], [std::move (v)]),
b4_locations_if([std::move (l)])));
}
#else #else
static static
symbol_type symbol_type
make_[]_b4_symbol([$1], [id]) (b4_join( make_[]_b4_symbol([$1], [id]) (b4_join(
b4_symbol_if([$1], [has_type], b4_symbol_if([$1], [has_type],
[const b4_symbol([$1], [type])& v]), [const b4_symbol([$1], [type])& v]),
b4_locations_if([const location_type& l]))); b4_locations_if([const location_type& l])))
{
return symbol_type (b4_join([token::b4_symbol([$1], [id])],
b4_symbol_if([$1], [has_type], [v]),
b4_locations_if([l])));
}
#endif #endif
])]) ])])
# _b4_token_constructor_declare(SYMBOL-NUM...) m4_define([_b4_type_clause],
# -------------------------------------------- [b4_symbol_if([$1], [is_token],
# Declare a unique make_symbol for all the SYMBOL-NUM (they [b4_symbol_if([$1], [has_id],
[tok == token::b4_symbol([$1], [id])],
[tok == b4_symbol([$1], [user_number])])])])
# _b4_token_constructor_define(SYMBOL-NUM...)
# -------------------------------------------
# Define a unique make_symbol for all the SYMBOL-NUM (they
# have the same type). Use at class-level. # have the same type). Use at class-level.
m4_define([_b4_token_constructor_declare], m4_define([_b4_token_constructor_define],
[m4_ifval(_b4_includes_tokens($@), [m4_ifval(_b4_includes_tokens($@),
[#if 201103L <= YY_CPLUSPLUS [[#if 201103L <= YY_CPLUSPLUS
symbol_type (b4_join( symbol_type (]b4_join(
[int tok], [int tok],
b4_symbol_if([$1], [has_type], b4_symbol_if([$1], [has_type],
[b4_symbol([$1], [type]) v]), [b4_symbol([$1], [type]) v]),
b4_locations_if([location_type l]))); b4_locations_if([location_type l]))[)
: super_type(]b4_join([token_type (tok)],
b4_symbol_if([$1], [has_type], [std::move (v)]),
b4_locations_if([std::move (l)]))[)
{
YYASSERT (]m4_join([ || ], m4_map_sep([_b4_type_clause], [, ], [$@]))[);
}
#else #else
symbol_type (b4_join( symbol_type (]b4_join(
[int tok], [int tok],
b4_symbol_if([$1], [has_type], b4_symbol_if([$1], [has_type],
[const b4_symbol([$1], [type])& v]), [const b4_symbol([$1], [type])& v]),
b4_locations_if([const location_type& l]))); b4_locations_if([const location_type& l]))[)
: super_type(]b4_join([token_type (tok)],
b4_symbol_if([$1], [has_type], [v]),
b4_locations_if([l]))[)
{
YYASSERT (]m4_join([ || ], m4_map_sep([_b4_type_clause], [, ], [$@]))[);
}
#endif #endif
])]) ]])])
# b4_token_constructor_declare # b4_token_constructor_declare
@@ -399,123 +428,28 @@ b4_symbol_foreach([_b4_token_maker_declare])])
# _b4_token_maker_define(SYMBOL-NUM)
# ----------------------------------
# Define make_SYMBOL for SYMBOL-NUM.
m4_define([_b4_token_maker_define],
[b4_token_visible_if([$1],
[#if 201103L <= YY_CPLUSPLUS
inline
b4_parser_class_name::symbol_type
b4_parser_class_name::make_[]_b4_symbol([$1], [id]) (b4_join(
b4_symbol_if([$1], [has_type],
[b4_symbol([$1], [type]) v]),
b4_locations_if([location_type l])))
{
return symbol_type (b4_join([token::b4_symbol([$1], [id])],
b4_symbol_if([$1], [has_type], [std::move (v)]),
b4_locations_if([std::move (l)])));
}
#else
inline
b4_parser_class_name::symbol_type
b4_parser_class_name::make_[]_b4_symbol([$1], [id]) (b4_join(
b4_symbol_if([$1], [has_type],
[const b4_symbol([$1], [type])& v]),
b4_locations_if([const location_type& l])))
{
return symbol_type (b4_join([token::b4_symbol([$1], [id])],
b4_symbol_if([$1], [has_type], [v]),
b4_locations_if([l])));
}
#endif
])])
# _b4_token_constructor_define(SYMBOL-NUM...)
# -------------------------------------------
# Declare a unique make_symbol for all the SYMBOL-NUM (they
# have the same type). Use at class-level.
m4_define([_b4_type_clause],
[b4_symbol_if([$1], [is_token],
[b4_symbol_if([$1], [has_id],
[tok == token::b4_symbol([$1], [id])],
[tok == b4_symbol([$1], [user_number])])])])
m4_define([_b4_token_constructor_define],
[m4_ifval(_b4_includes_tokens($@),
[[#if 201103L <= YY_CPLUSPLUS
inline
]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
[int tok],
b4_symbol_if([$1], [has_type],
[b4_symbol([$1], [type]) v]),
b4_locations_if([location_type l]))[)
: super_type(]b4_join([token_type (tok)],
b4_symbol_if([$1], [has_type], [std::move (v)]),
b4_locations_if([std::move (l)]))[)
{
YYASSERT (]m4_join([ || ], m4_map_sep([_b4_type_clause], [, ], [$@]))[);
}
#else
inline
]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
[int tok],
b4_symbol_if([$1], [has_type],
[const b4_symbol([$1], [type])& v]),
b4_locations_if([const location_type& l]))[)
: super_type(]b4_join([token_type (tok)],
b4_symbol_if([$1], [has_type], [v]),
b4_locations_if([l]))[)
{
YYASSERT (]m4_join([ || ], m4_map_sep([_b4_type_clause], [, ], [$@]))[);
}
#endif
]])])
# b4_basic_symbol_constructor_declare(SYMBOL-NUM)
# -----------------------------------------------
# Generate a constructor declaration for basic_symbol from given type.
m4_define([b4_basic_symbol_constructor_declare],
[[#if 201103L <= YY_CPLUSPLUS
basic_symbol (]b4_join(
[typename Base::kind_type t],
b4_symbol_if([$1], [has_type], [b4_symbol([$1], [type])&& v]),
b4_locations_if([location_type&& l]))[);
#else
basic_symbol (]b4_join(
[typename Base::kind_type t],
b4_symbol_if([$1], [has_type], [const b4_symbol([$1], [type])& v]),
b4_locations_if([const location_type& l]))[);
#endif
]])
# b4_basic_symbol_constructor_define(SYMBOL-NUM) # b4_basic_symbol_constructor_define(SYMBOL-NUM)
# ---------------------------------------------- # ----------------------------------------------
# Generate a constructor implementation for basic_symbol from given type. # Generate a constructor for basic_symbol from given type.
m4_define([b4_basic_symbol_constructor_define], m4_define([b4_basic_symbol_constructor_define],
[[#if 201103L <= YY_CPLUSPLUS [[#if 201103L <= YY_CPLUSPLUS
template <typename Base> basic_symbol (]b4_join(
]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
[typename Base::kind_type t], [typename Base::kind_type t],
b4_symbol_if([$1], [has_type], [b4_symbol([$1], [type])&& v]), b4_symbol_if([$1], [has_type], [b4_symbol([$1], [type])&& v]),
b4_locations_if([location_type&& l]))[) b4_locations_if([location_type&& l]))[)
: Base (t)]b4_symbol_if([$1], [has_type], [ : Base (t)]b4_symbol_if([$1], [has_type], [
, value (std::move (v))])[]b4_locations_if([ , value (std::move (v))])[]b4_locations_if([
, location (std::move (l))])[ , location (std::move (l))])[
{} {}
#else #else
template <typename Base> basic_symbol (]b4_join(
]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
[typename Base::kind_type t], [typename Base::kind_type t],
b4_symbol_if([$1], [has_type], [const b4_symbol([$1], [type])& v]), b4_symbol_if([$1], [has_type], [const b4_symbol([$1], [type])& v]),
b4_locations_if([const location_type& l]))[) b4_locations_if([const location_type& l]))[)
: Base (t)]b4_symbol_if([$1], [has_type], [ : Base (t)]b4_symbol_if([$1], [has_type], [
, value (v)])[]b4_locations_if([ , value (v)])[]b4_locations_if([
, location (l)])[ , location (l)])[
{} {}
#endif #endif
]]) ]])
@@ -525,5 +459,4 @@ m4_define([b4_basic_symbol_constructor_define],
# Define the overloaded versions of make_symbol for all the value types. # Define the overloaded versions of make_symbol for all the value types.
m4_define([b4_token_constructor_define], m4_define([b4_token_constructor_define],
[ // Implementation of make_symbol for each symbol type. [ // Implementation of make_symbol for each symbol type.
b4_type_foreach([_b4_token_constructor_define])
b4_symbol_foreach([_b4_token_maker_define])]) b4_symbol_foreach([_b4_token_maker_define])])