aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-07-20 15:07:58 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2022-07-20 15:08:51 +0200
commitd3926e6a87abd10aebd8f41749c00efbfeef1936 (patch)
tree0bd73e8d3dcecde441893b2b035b8a8658d3e307
parent09855d5c0eb14a9b976a98ae4ec9ab012eebaa51 (diff)
downloadgcc-d3926e6a87abd10aebd8f41749c00efbfeef1936.zip
gcc-d3926e6a87abd10aebd8f41749c00efbfeef1936.tar.gz
gcc-d3926e6a87abd10aebd8f41749c00efbfeef1936.tar.bz2
macros: Rename APIs around metavars and repetitions to improve clarity
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc27
-rw-r--r--gcc/rust/expand/rust-macro-expand.h15
-rw-r--r--gcc/rust/expand/rust-macro-substitute-ctx.cc2
3 files changed, 24 insertions, 20 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index f96c288..8c6a3e5 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -83,15 +83,14 @@ MacroExpander::expand_decl_macro (Location invoc_locus,
if (did_match_rule)
{
- // FIXME: ARTHUR: Comment
- // Debugging
- for (auto &kv : matched_fragments)
- rust_debug ("[fragment]: %s (%ld - %s)", kv.first.c_str (),
- kv.second.get_fragments ().size (),
- kv.second.get_kind ()
- == MatchedFragmentContainer::Kind::Repetition
- ? "repetition"
- : "metavar");
+ // // Debugging
+ // for (auto &kv : matched_fragments)
+ // rust_debug ("[fragment]: %s (%ld - %s)", kv.first.c_str (),
+ // kv.second.get_fragments ().size (),
+ // kv.second.get_kind ()
+ // == MatchedFragmentContainer::Kind::Repetition
+ // ? "repetition"
+ // : "metavar");
matched_rule = &rule;
break;
@@ -521,7 +520,7 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
// matched fragment get the offset in the token stream
size_t offs_end = source.get_offs ();
- sub_stack.insert_fragment (
+ sub_stack.insert_metavar (
MatchedFragment (fragment->get_ident (), offs_begin, offs_end));
}
break;
@@ -626,7 +625,13 @@ MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
// matched fragment get the offset in the token stream
size_t offs_end = source.get_offs ();
- // FIXME: ARTHUR: Here we want to append?
+
+ // The main difference with match_matcher happens here: Instead
+ // of inserting a new fragment, we append to one. If that
+ // fragment does not exist, then the operation is similar to
+ // `insert_fragment` with the difference that we are not
+ // creating a metavariable, but a repetition of one, which is
+ // really different.
sub_stack.append_fragment (
MatchedFragment (fragment->get_ident (), offs_begin,
offs_end));
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index 51d7516..f6edbc8 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -101,7 +101,7 @@ public:
/**
* Create a valid fragment matched one time
*/
- static MatchedFragmentContainer one (MatchedFragment fragment)
+ static MatchedFragmentContainer metavar (MatchedFragment fragment)
{
return MatchedFragmentContainer ({fragment}, Kind::MetaVar);
}
@@ -111,9 +111,9 @@ public:
*/
void add_fragment (MatchedFragment fragment)
{
- fragments.emplace_back (fragment);
+ rust_assert (!is_single_fragment ());
- kind = Kind::Repetition;
+ fragments.emplace_back (fragment);
}
size_t get_match_amount () const { return fragments.size (); }
@@ -125,7 +125,6 @@ public:
bool is_single_fragment () const
{
- // FIXME: Is that valid?
return get_match_amount () == 1 && kind == Kind::MetaVar;
}
@@ -167,16 +166,16 @@ public:
}
/**
- * Insert a new matched fragment into the current substitution map
+ * Insert a new matched metavar into the current substitution map
*/
- void insert_fragment (MatchedFragment fragment)
+ void insert_metavar (MatchedFragment fragment)
{
auto &current_map = stack.back ();
auto it = current_map.find (fragment.fragment_ident);
if (it == current_map.end ())
- current_map.insert (
- {fragment.fragment_ident, MatchedFragmentContainer::one (fragment)});
+ current_map.insert ({fragment.fragment_ident,
+ MatchedFragmentContainer::metavar (fragment)});
else
gcc_unreachable ();
}
diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc b/gcc/rust/expand/rust-macro-substitute-ctx.cc
index 1031cea..9592d2d 100644
--- a/gcc/rust/expand/rust-macro-substitute-ctx.cc
+++ b/gcc/rust/expand/rust-macro-substitute-ctx.cc
@@ -155,7 +155,7 @@ SubstituteCtx::substitute_repetition (
sub_fragment = kv_match.second.get_fragments ()[i];
sub_map.insert (
- {kv_match.first, MatchedFragmentContainer::one (sub_fragment)});
+ {kv_match.first, MatchedFragmentContainer::metavar (sub_fragment)});
}
auto substitute_context = SubstituteCtx (input, new_macro, sub_map);