public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macromatch: Add location to abstract MacroMatch class
@ 2022-06-08 12:06 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:06 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:3ac17160c8033931f3bb04f234968f9b56ea3c64

commit 3ac17160c8033931f3bb04f234968f9b56ea3c64
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Feb 16 13:50:12 2022 +0100

    macromatch: Add location to abstract MacroMatch class

Diff:
---
 gcc/rust/ast/rust-ast.h          |  2 ++
 gcc/rust/ast/rust-macro.h        | 51 +++++++++++++++++++++++++---------------
 gcc/rust/parse/rust-parse-impl.h | 14 ++++++-----
 3 files changed, 42 insertions(+), 25 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 0c22c37903f..ba973f11ed2 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -111,6 +111,7 @@ public:
   virtual ~MacroMatch () {}
 
   virtual std::string as_string () const = 0;
+  virtual Location get_match_locus () const = 0;
 
   // Unique pointer custom clone function
   std::unique_ptr<MacroMatch> clone_macro_match () const
@@ -217,6 +218,7 @@ public:
   }
 
   std::string as_string () const override;
+  Location get_match_locus () const override { return tok_ref->get_locus (); };
 
   void accept_vis (ASTVisitor &vis) override;
 
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index 6ea7de821ad..2d59b1852db 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -87,24 +87,24 @@ class MacroMatchFragment : public MacroMatch
 {
   Identifier ident;
   MacroFragSpec frag_spec;
-
-  // TODO: should store location information?
+  Location locus;
 
 public:
-  MacroMatchFragment (Identifier ident, MacroFragSpec frag_spec)
-    : ident (std::move (ident)), frag_spec (frag_spec)
+  MacroMatchFragment (Identifier ident, MacroFragSpec frag_spec, Location locus)
+    : ident (std::move (ident)), frag_spec (frag_spec), locus (locus)
   {}
 
   // Returns whether macro match fragment is in an error state.
   bool is_error () const { return frag_spec == INVALID; }
 
   // Creates an error state macro match fragment.
-  static MacroMatchFragment create_error ()
+  static MacroMatchFragment create_error (Location locus)
   {
-    return MacroMatchFragment (std::string (""), INVALID);
+    return MacroMatchFragment (std::string (""), INVALID, locus);
   }
 
   std::string as_string () const override;
+  Location get_match_locus () const override { return locus; };
 
   void accept_vis (ASTVisitor &vis) override;
 
@@ -137,20 +137,22 @@ private:
   typedef Token MacroRepSep;
   // any token except delimiters and repetition operators
   std::unique_ptr<MacroRepSep> sep;
-
-  // TODO: should store location information?
+  Location locus;
 
 public:
   // Returns whether macro match repetition has separator token.
   bool has_sep () const { return sep != nullptr; }
 
   MacroMatchRepetition (std::vector<std::unique_ptr<MacroMatch> > matches,
-			MacroRepOp op, std::unique_ptr<MacroRepSep> sep)
-    : matches (std::move (matches)), op (op), sep (std::move (sep))
+			MacroRepOp op, std::unique_ptr<MacroRepSep> sep,
+			Location locus)
+    : matches (std::move (matches)), op (op), sep (std::move (sep)),
+      locus (locus)
   {}
 
   // Copy constructor with clone
-  MacroMatchRepetition (MacroMatchRepetition const &other) : op (other.op)
+  MacroMatchRepetition (MacroMatchRepetition const &other)
+    : op (other.op), locus (other.locus)
   {
     // guard to protect from null pointer dereference
     if (other.sep != nullptr)
@@ -165,6 +167,7 @@ public:
   MacroMatchRepetition &operator= (MacroMatchRepetition const &other)
   {
     op = other.op;
+    locus = other.locus;
 
     // guard to protect from null pointer dereference
     if (other.sep != nullptr)
@@ -184,6 +187,7 @@ public:
   MacroMatchRepetition &operator= (MacroMatchRepetition &&other) = default;
 
   std::string as_string () const override;
+  Location get_match_locus () const override { return locus; };
 
   void accept_vis (ASTVisitor &vis) override;
 
@@ -201,20 +205,22 @@ class MacroMatcher : public MacroMatch
 {
   DelimType delim_type;
   std::vector<std::unique_ptr<MacroMatch> > matches;
+  Location locus;
 
   // TODO: think of way to mark invalid that doesn't take up more space
   bool is_invalid;
 
-  // TODO: should store location information?
-
 public:
   MacroMatcher (DelimType delim_type,
-		std::vector<std::unique_ptr<MacroMatch> > matches)
-    : delim_type (delim_type), matches (std::move (matches)), is_invalid (false)
+		std::vector<std::unique_ptr<MacroMatch> > matches,
+		Location locus)
+    : delim_type (delim_type), matches (std::move (matches)), locus (locus),
+      is_invalid (false)
   {}
 
   // copy constructor with vector clone
-  MacroMatcher (MacroMatcher const &other) : delim_type (other.delim_type)
+  MacroMatcher (MacroMatcher const &other)
+    : delim_type (other.delim_type), locus (other.locus)
   {
     matches.reserve (other.matches.size ());
     for (const auto &e : other.matches)
@@ -225,6 +231,7 @@ public:
   MacroMatcher &operator= (MacroMatcher const &other)
   {
     delim_type = other.delim_type;
+    locus = other.locus;
 
     matches.reserve (other.matches.size ());
     for (const auto &e : other.matches)
@@ -238,10 +245,14 @@ public:
   MacroMatcher &operator= (MacroMatcher &&other) = default;
 
   // Creates an error state macro matcher.
-  static MacroMatcher create_error () { return MacroMatcher (true); }
+  static MacroMatcher create_error (Location locus)
+  {
+    return MacroMatcher (true, locus);
+  }
 
   // Returns whether MacroMatcher is in an error state.
   bool is_error () const { return is_invalid; }
+  Location get_match_locus () const override { return locus; }
 
   std::string as_string () const override;
 
@@ -256,7 +267,8 @@ protected:
   }
 
   // constructor only used to create error matcher
-  MacroMatcher (bool is_invalid) : delim_type (PARENS), is_invalid (is_invalid)
+  MacroMatcher (bool is_invalid, Location locus)
+    : delim_type (PARENS), locus (locus), is_invalid (is_invalid)
   {}
 };
 
@@ -296,7 +308,8 @@ public:
   // Creates an error state macro rule.
   static MacroRule create_error ()
   {
-    return MacroRule (MacroMatcher::create_error (),
+    // FIXME: Once #928 is merged, give location to MacroMatcher
+    return MacroRule (MacroMatcher::create_error (Location ()),
 		      MacroTranscriber (DelimTokenTree::create_empty ()));
   }
 
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 6d393b0bc75..b500c871795 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -1709,6 +1709,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
 
   // Map tokens to DelimType
   const_TokenPtr t = lexer.peek_token ();
+  Location locus = t->get_locus ();
   switch (t->get_id ())
     {
     case LEFT_PAREN:
@@ -1726,7 +1727,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
 	"unexpected token %qs - expecting delimiters (for a macro matcher)",
 	t->get_token_description ()));
 
-      return AST::MacroMatcher::create_error ();
+      return AST::MacroMatcher::create_error (t->get_locus ());
     }
   lexer.skip_token ();
 
@@ -1747,7 +1748,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
 	    t->get_token_description ());
 	  add_error (std::move (error));
 
-	  return AST::MacroMatcher::create_error ();
+	  return AST::MacroMatcher::create_error (t->get_locus ());
 	}
 
       matches.push_back (std::move (match));
@@ -1765,7 +1766,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
       // tokens match opening delimiter, so skip.
       lexer.skip_token ();
 
-      return AST::MacroMatcher (delim_type, std::move (matches));
+      return AST::MacroMatcher (delim_type, std::move (matches), locus);
     }
   else
     {
@@ -1781,7 +1782,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
 
       /* return error macro matcher despite possibly parsing mostly correct one?
        * TODO is this the best idea? */
-      return AST::MacroMatcher::create_error ();
+      return AST::MacroMatcher::create_error (t->get_locus ());
     }
 }
 
@@ -1857,6 +1858,7 @@ template <typename ManagedTokenSource>
 std::unique_ptr<AST::MacroMatchFragment>
 Parser<ManagedTokenSource>::parse_macro_match_fragment ()
 {
+  Location fragment_locus = lexer.peek_token ()->get_locus ();
   skip_token (DOLLAR_SIGN);
 
   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
@@ -1893,7 +1895,7 @@ Parser<ManagedTokenSource>::parse_macro_match_fragment ()
     }
 
   return std::unique_ptr<AST::MacroMatchFragment> (
-    new AST::MacroMatchFragment (std::move (ident), frag));
+    new AST::MacroMatchFragment (std::move (ident), frag, fragment_locus));
 }
 
 // Parses a repetition macro match.
@@ -2002,7 +2004,7 @@ Parser<ManagedTokenSource>::parse_macro_match_repetition ()
 
   return std::unique_ptr<AST::MacroMatchRepetition> (
     new AST::MacroMatchRepetition (std::move (matches), op,
-				   std::move (separator)));
+				   std::move (separator), t->get_locus ()));
 }
 
 /* Parses a visibility syntactical production (i.e. creating a non-default


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

only message in thread, other threads:[~2022-06-08 12:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:06 [gcc/devel/rust/master] macromatch: Add location to abstract MacroMatch class 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).