[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
c++: variants: fuse declarations and definitions
From: |
Akim Demaille |
Subject: |
c++: variants: fuse declarations and definitions |
Date: |
Wed, 26 Dec 2018 20:53:48 +0100 |
commit 5fb0d276b3fe2e736fce1e002d7a08a3d1a86944
Author: Akim Demaille <address@hidden>
Date: Wed Dec 26 09:04:07 2018 +0100
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
d132c2d5455135f63a7497c38358eadd6e3a6af8), 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.
diff --git a/data/skeletons/c++.m4 b/data/skeletons/c++.m4
index e0f4dacb..3dca87c2 100644
--- a/data/skeletons/c++.m4
+++ b/data/skeletons/c++.m4
@@ -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
# constructors.
-m4_define([b4_symbol_type_declare],
+m4_define([b4_symbol_type_define],
[[ /// A complete symbol.
///
/// 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([[
/// 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.
basic_symbol (typename Base::kind_type t]b4_locations_if([,
YY_MOVE_REF (location_type) l])[);
@@ -351,7 +351,7 @@ m4_define([b4_symbol_type_declare],
symbol_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)])
])[}
-]b4_variant_if([[
- // Implementation of basic_symbol constructor for each type.
-]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
+]b4_variant_if([], [[
/// Constructor for valueless symbols.
template <typename Base>
]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]);
}
]])[]dnl
-
-b4_token_constructor_define])
+])
-# b4_token_constructor_declare
# 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.
-m4_define([b4_token_constructor_declare], [])
m4_define([b4_token_constructor_define], [])
diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc
index 977b4b23..2d82172e 100644
--- a/data/skeletons/lalr1.cc
+++ b/data/skeletons/lalr1.cc
@@ -180,7 +180,7 @@ m4_define([b4_shared_declarations],
{
public:
]b4_public_types_declare[
-]b4_symbol_type_declare[
+]b4_symbol_type_define[
/// Build a parser object.
]b4_parser_class_name[ (]b4_parse_param_decl[);
virtual ~]b4_parser_class_name[ ();
@@ -215,7 +215,7 @@ m4_define([b4_shared_declarations],
/// Report a syntax error.
void error (const syntax_error& err);
-]b4_token_constructor_declare[
+]b4_token_constructor_define[
private:
/// This class is not copyable.
diff --git a/data/skeletons/variant.hh b/data/skeletons/variant.hh
index c613f597..6c681e6e 100644
--- a/data/skeletons/variant.hh
+++ b/data/skeletons/variant.hh
@@ -87,7 +87,7 @@ m4_define([b4_variant_includes],
# b4_value_type_declare
# ---------------------
-# Declare semantic_type.
+# Define semantic_type.
m4_define([b4_value_type_declare],
[[ /// A buffer to store and retrieve objects.
///
@@ -345,177 +345,111 @@ m4_define([_b4_includes_tokens],
[m4_map([_b4_is_token], address@hidden)])
-# _b4_token_maker_declare(SYMBOL-NUM)
-# -----------------------------------
-# Declare make_SYMBOL for SYMBOL-NUM. Use at class-level.
-m4_define([_b4_token_maker_declare],
-[b4_token_visible_if([$1],
-[#if 201103L <= YY_CPLUSPLUS
- static
- symbol_type
- make_[]_b4_symbol([$1], [id]) (b4_join(
- b4_symbol_if([$1], [has_type],
- [b4_symbol([$1], [type]) v]),
- b4_locations_if([location_type l])));
-#else
- static
- symbol_type
- 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])));
-#endif
-])])
-
-
-# _b4_token_constructor_declare(SYMBOL-NUM...)
-# --------------------------------------------
-# Declare a unique make_symbol for all the SYMBOL-NUM (they
-# have the same type). Use at class-level.
-m4_define([_b4_token_constructor_declare],
-[m4_ifval(_b4_includes_tokens($@),
-[#if 201103L <= YY_CPLUSPLUS
- symbol_type (b4_join(
- [int tok],
- b4_symbol_if([$1], [has_type],
- [b4_symbol([$1], [type]) v]),
- b4_locations_if([location_type l])));
-#else
- 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])));
-#endif
-])])
-
-
-# b4_token_constructor_declare
-# ----------------------------
-# Declare token constructors. Use at class-level.
-m4_define([b4_token_constructor_declare],
-[ // Symbol constructors declarations.
-b4_symbol_foreach([_b4_token_maker_declare])])
-
-
-
# _b4_token_maker_define(SYMBOL-NUM)
# ----------------------------------
-# Define make_SYMBOL for SYMBOL-NUM.
+# Declare make_SYMBOL for SYMBOL-NUM. Use at class-level.
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)])));
- }
+ static
+ symbol_type
+ 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])));
- }
+ static
+ symbol_type
+ 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])])])])
+
+# _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.
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], [, ],
address@hidden))[);
- }
+ 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], [, ],
address@hidden))[);
+ }
#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], [, ],
address@hidden))[);
- }
+ 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], [, ],
address@hidden))[);
+ }
#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_token_constructor_declare
+# ----------------------------
+# Declare token constructors. Use at class-level.
+m4_define([b4_token_constructor_declare],
+[ // Symbol constructors declarations.
+b4_symbol_foreach([_b4_token_maker_declare])])
+
# 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],
[[#if 201103L <= YY_CPLUSPLUS
- template <typename Base>
- ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
+ 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]))[)
- : Base (t)]b4_symbol_if([$1], [has_type], [
- , value (std::move (v))])[]b4_locations_if([
- , location (std::move (l))])[
- {}
+ : Base (t)]b4_symbol_if([$1], [has_type], [
+ , value (std::move (v))])[]b4_locations_if([
+ , location (std::move (l))])[
+ {}
#else
- template <typename Base>
- ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
+ 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]))[)
- : Base (t)]b4_symbol_if([$1], [has_type], [
- , value (v)])[]b4_locations_if([
- , location (l)])[
- {}
+ : Base (t)]b4_symbol_if([$1], [has_type], [
+ , value (v)])[]b4_locations_if([
+ , location (l)])[
+ {}
#endif
]])
@@ -525,5 +459,4 @@ m4_define([b4_basic_symbol_constructor_define],
# Define the overloaded versions of make_symbol for all the value types.
m4_define([b4_token_constructor_define],
[ // Implementation of make_symbol for each symbol type.
-b4_type_foreach([_b4_token_constructor_define])
b4_symbol_foreach([_b4_token_maker_define])])
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- c++: variants: fuse declarations and definitions,
Akim Demaille <=