public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: Rename APIs around metavars and repetitions to improve clarity
@ 2022-07-22 13:34 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-07-22 13:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d3926e6a87abd10aebd8f41749c00efbfeef1936

commit d3926e6a87abd10aebd8f41749c00efbfeef1936
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Jul 20 15:07:58 2022 +0200

    macros: Rename APIs around metavars and repetitions to improve clarity

Diff:
---
 gcc/rust/expand/rust-macro-expand.cc         | 27 ++++++++++++++++-----------
 gcc/rust/expand/rust-macro-expand.h          | 15 +++++++--------
 gcc/rust/expand/rust-macro-substitute-ctx.cc |  2 +-
 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 f96c28834dd..8c6a3e53dad 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 51d7516f1c5..f6edbc8329f 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 1031cea483d..9592d2d2a9e 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);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-22 13:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-22 13:34 [gcc/devel/rust/master] macros: Rename APIs around metavars and repetitions to improve clarity Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).