public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Improve Fragment API
@ 2022-10-22 10:47 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-10-22 10:47 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:89490980726d298311107a452bdebeb43a2ff7e6

commit 89490980726d298311107a452bdebeb43a2ff7e6
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Oct 19 14:53:43 2022 +0200

    ast: Improve Fragment API

Diff:
---
 gcc/rust/ast/rust-ast-fragment.cc        | 31 +++++++++++++++++--------------
 gcc/rust/ast/rust-ast-fragment.h         | 18 ++++++++++++++++--
 gcc/rust/expand/rust-attribute-visitor.h |  2 +-
 gcc/rust/expand/rust-macro-builtins.cc   | 18 +++++++++---------
 gcc/rust/expand/rust-macro-expand.cc     |  6 +++---
 gcc/rust/expand/rust-macro-expand.h      |  2 +-
 6 files changed, 47 insertions(+), 30 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-fragment.cc b/gcc/rust/ast/rust-ast-fragment.cc
index 1a2dd99835a..c4916093fc6 100644
--- a/gcc/rust/ast/rust-ast-fragment.cc
+++ b/gcc/rust/ast/rust-ast-fragment.cc
@@ -21,22 +21,13 @@
 namespace Rust {
 namespace AST {
 
-Fragment::Fragment (std::vector<SingleASTNode> nodes, bool fragment_is_error)
-  : kind (fragment_is_error ? FragmentKind::Error : FragmentKind::Complete),
-    nodes (std::move (nodes))
-{
-  if (fragment_is_error)
-    rust_assert (nodes.empty ());
-}
+Fragment::Fragment (FragmentKind kind, std::vector<SingleASTNode> nodes)
+  : kind (kind), nodes (std::move (nodes))
+{}
 
 Fragment::Fragment (Fragment const &other) : kind (other.get_kind ())
 {
-  nodes.clear ();
-  nodes.reserve (other.nodes.size ());
-  for (auto &n : other.nodes)
-    {
-      nodes.push_back (n);
-    }
+  *this = other;
 }
 
 Fragment &
@@ -56,7 +47,19 @@ Fragment::operator= (Fragment const &other)
 Fragment
 Fragment::create_error ()
 {
-  return Fragment ({}, true);
+  return Fragment (FragmentKind::Error, {});
+}
+
+Fragment
+Fragment::complete (std::vector<AST::SingleASTNode> nodes)
+{
+  return Fragment (FragmentKind::Complete, std::move (nodes));
+}
+
+Fragment
+Fragment::unexpanded ()
+{
+  return Fragment (FragmentKind::Unexpanded, {});
 }
 
 std::vector<SingleASTNode> &
diff --git a/gcc/rust/ast/rust-ast-fragment.h b/gcc/rust/ast/rust-ast-fragment.h
index ee6ab0d8433..3ef4ba16dce 100644
--- a/gcc/rust/ast/rust-ast-fragment.h
+++ b/gcc/rust/ast/rust-ast-fragment.h
@@ -57,11 +57,23 @@ enum class FragmentKind
 class Fragment
 {
 public:
-  Fragment (std::vector<SingleASTNode> nodes, bool fragment_is_error = false);
   Fragment (Fragment const &other);
+  Fragment &operator= (Fragment const &other);
+
+  /**
+   * Create an error fragment
+   */
   static Fragment create_error ();
 
-  Fragment &operator= (Fragment const &other);
+  /**
+   * Create a complete AST fragment
+   */
+  static Fragment complete (std::vector<AST::SingleASTNode> nodes);
+
+  /**
+   * Create a fragment which contains unexpanded nodes
+   */
+  static Fragment unexpanded ();
 
   FragmentKind get_kind () const;
   std::vector<SingleASTNode> &get_nodes ();
@@ -78,6 +90,8 @@ public:
   void accept_vis (ASTVisitor &vis);
 
 private:
+  Fragment (FragmentKind kind, std::vector<SingleASTNode> nodes);
+
   FragmentKind kind;
 
   /**
diff --git a/gcc/rust/expand/rust-attribute-visitor.h b/gcc/rust/expand/rust-attribute-visitor.h
index 27aee0a2b03..c2e25dff4cb 100644
--- a/gcc/rust/expand/rust-attribute-visitor.h
+++ b/gcc/rust/expand/rust-attribute-visitor.h
@@ -60,7 +60,7 @@ public:
   {
     auto fragment = expander.take_expanded_fragment (*this);
     unsigned int original_depth = expander.expansion_depth;
-    auto final_fragment = AST::Fragment ({}, true);
+    auto final_fragment = AST::Fragment::create_error ();
 
     while (fragment.should_expand ())
       {
diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc
index 2e55037247a..14322d0db0a 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -279,7 +279,7 @@ MacroBuiltin::file (Location invoc_locus, AST::MacroInvocData &invoc)
     = Session::get_instance ().linemap->location_file (invoc_locus);
   auto file_str = AST::SingleASTNode (make_string (invoc_locus, current_file));
 
-  return AST::Fragment ({file_str});
+  return AST::Fragment::complete ({file_str});
 }
 
 AST::Fragment
@@ -292,7 +292,7 @@ MacroBuiltin::column (Location invoc_locus, AST::MacroInvocData &invoc)
     new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT,
 			  PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus)));
 
-  return AST::Fragment ({column_no});
+  return AST::Fragment::complete ({column_no});
 }
 
 /* Expand builtin macro include_bytes!("filename"), which includes the contents
@@ -335,7 +335,7 @@ MacroBuiltin::include_bytes (Location invoc_locus, AST::MacroInvocData &invoc)
     new AST::BorrowExpr (std::move (array), false, false, {}, invoc_locus));
 
   auto node = AST::SingleASTNode (std::move (borrow));
-  return AST::Fragment ({node});
+  return AST::Fragment::complete ({node});
 }
 
 /* Expand builtin macro include_str!("filename"), which includes the contents
@@ -362,7 +362,7 @@ MacroBuiltin::include_str (Location invoc_locus, AST::MacroInvocData &invoc)
   std::string str ((const char *) &bytes[0], bytes.size ());
 
   auto node = AST::SingleASTNode (make_string (invoc_locus, str));
-  return AST::Fragment ({node});
+  return AST::Fragment::complete ({node});
 }
 
 /* Expand builtin macro compile_error!("error"), which forces a compile error
@@ -430,7 +430,7 @@ MacroBuiltin::concat (Location invoc_locus, AST::MacroInvocData &invoc)
     return AST::Fragment::create_error ();
 
   auto node = AST::SingleASTNode (make_string (invoc_locus, str));
-  return AST::Fragment ({node});
+  return AST::Fragment::complete ({node});
 }
 
 /* Expand builtin macro env!(), which inspects an environment variable at
@@ -491,7 +491,7 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc)
     }
 
   auto node = AST::SingleASTNode (make_string (invoc_locus, env_value));
-  return AST::Fragment ({node});
+  return AST::Fragment::complete ({node});
 }
 
 AST::Fragment
@@ -527,7 +527,7 @@ MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc)
     new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL,
 			  PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus)));
 
-  return AST::Fragment ({literal_exp});
+  return AST::Fragment::complete ({literal_exp});
 }
 
 /* Expand builtin macro include!(), which includes a source file at the current
@@ -584,7 +584,7 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc)
       nodes.push_back (node);
     }
 
-  return AST::Fragment (nodes);
+  return AST::Fragment::complete (nodes);
 }
 
 AST::Fragment
@@ -597,7 +597,7 @@ MacroBuiltin::line (Location invoc_locus, AST::MacroInvocData &invoc)
     new AST::LiteralExpr (std::to_string (current_line), AST::Literal::INT,
 			  PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus)));
 
-  return AST::Fragment ({line_no});
+  return AST::Fragment::complete ({line_no});
 }
 
 } // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 3b852875463..589443453d6 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -729,7 +729,7 @@ parse_many (Parser<MacroInvocLexer> &parser, TokenId &delimiter,
       nodes.emplace_back (std::move (node));
     }
 
-  return AST::Fragment (std::move (nodes));
+  return AST::Fragment::complete (std::move (nodes));
 }
 
 /**
@@ -840,7 +840,7 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
 {
   auto expr = parser.parse_expr ();
 
-  return AST::Fragment ({std::move (expr)});
+  return AST::Fragment::complete ({std::move (expr)});
 }
 
 /**
@@ -855,7 +855,7 @@ transcribe_type (Parser<MacroInvocLexer> &parser)
   for (auto err : parser.get_errors ())
     err.emit_error ();
 
-  return AST::Fragment ({std::move (type)});
+  return AST::Fragment::complete ({std::move (type)});
 }
 
 static AST::Fragment
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index 3453058663c..6253a4ee000 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -344,7 +344,7 @@ struct MacroExpander
 	    auto new_nodes = expanded_fragment.get_nodes ();
 	    std::move (new_nodes.begin (), new_nodes.end (),
 		       std::back_inserter (accumulator));
-	    expanded_fragment = AST::Fragment (accumulator);
+	    expanded_fragment = AST::Fragment::complete (accumulator);
 	  }
 	expansion_depth--;
       }

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

only message in thread, other threads:[~2022-10-22 10:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 10:47 [gcc/devel/rust/master] ast: Improve Fragment API 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).