[gcc r14-7422] gccrs: ast: Add use declarations TokenStream visitors

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:33:23 GMT 2024


https://gcc.gnu.org/g:458fb22123e64260c60dffe0d9ba3421414e4a01

commit r14-7422-g458fb22123e64260c60dffe0d9ba3421414e4a01
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Thu Mar 23 16:46:51 2023 +0100

    gccrs: ast: Add use declarations TokenStream visitors
    
    Add UseDeclaration (and it's childrens) visitor implementation.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast-tokenstream.cc (TokenStream::visit): Add visitor.
            * ast/rust-item.h: Add missing getters.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast-tokenstream.cc | 77 ++++++++++++++++++++++++++++++++----
 gcc/rust/ast/rust-item.h             | 11 ++++++
 2 files changed, 80 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-tokenstream.cc b/gcc/rust/ast/rust-ast-tokenstream.cc
index f5d04450dbc..9fb9f931202 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.cc
+++ b/gcc/rust/ast/rust-ast-tokenstream.cc
@@ -1586,20 +1586,81 @@ TokenStream::visit (ExternCrate &crate)
 }
 
 void
-TokenStream::visit (UseTreeGlob &)
-{}
+TokenStream::visit (UseTreeGlob &use_tree)
+{
+  switch (use_tree.get_glob_type ())
+    {
+      case UseTreeGlob::PathType::PATH_PREFIXED: {
+	auto path = use_tree.get_path ();
+	visit (path);
+	tokens.push_back (Rust::Token::make (SCOPE_RESOLUTION, Location ()));
+      }
+      break;
+    case UseTreeGlob::PathType::NO_PATH:
+      tokens.push_back (Rust::Token::make (SCOPE_RESOLUTION, Location ()));
+      break;
+    case UseTreeGlob::PathType::GLOBAL:
+      break;
+    }
+  tokens.push_back (Rust::Token::make (ASTERISK, Location ()));
+}
 
 void
-TokenStream::visit (UseTreeList &)
-{}
+TokenStream::visit (UseTreeList &use_tree)
+{
+  switch (use_tree.get_path_type ())
+    {
+      case UseTreeList::PathType::PATH_PREFIXED: {
+	auto path = use_tree.get_path ();
+	visit (path);
+	tokens.push_back (Rust::Token::make (SCOPE_RESOLUTION, Location ()));
+      }
+      break;
+    case UseTreeList::PathType::NO_PATH:
+      tokens.push_back (Rust::Token::make (SCOPE_RESOLUTION, Location ()));
+      break;
+    case UseTreeList::PathType::GLOBAL:
+      break;
+    }
+
+  tokens.push_back (Rust::Token::make (LEFT_CURLY, Location ()));
+  if (use_tree.has_trees ())
+    {
+      visit_items_joined_by_separator (use_tree.get_trees (), COMMA);
+    }
+  tokens.push_back (Rust::Token::make (RIGHT_CURLY, Location ()));
+}
 
 void
-TokenStream::visit (UseTreeRebind &)
-{}
+TokenStream::visit (UseTreeRebind &use_tree)
+{
+  auto path = use_tree.get_path ();
+  visit (path);
+  switch (use_tree.get_new_bind_type ())
+    {
+      case UseTreeRebind::NewBindType::IDENTIFIER: {
+	tokens.push_back (Rust::Token::make (AS, Location ()));
+	auto id = use_tree.get_identifier ();
+	tokens.push_back (
+	  Rust::Token::make_identifier (use_tree.get_locus (), std::move (id)));
+      }
+      break;
+    case UseTreeRebind::NewBindType::WILDCARD:
+      tokens.push_back (Rust::Token::make (AS, Location ()));
+      tokens.push_back (Rust::Token::make (UNDERSCORE, use_tree.get_locus ()));
+      break;
+    case UseTreeRebind::NewBindType::NONE:
+      break;
+    }
+}
 
 void
-TokenStream::visit (UseDeclaration &)
-{}
+TokenStream::visit (UseDeclaration &decl)
+{
+  tokens.push_back (Rust::Token::make (USE, decl.get_locus ()));
+  visit (*decl.get_tree ());
+  tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+}
 
 void
 TokenStream::visit (Function &function)
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index b82b5fbcea3..e08a35234a8 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -1280,6 +1280,8 @@ public:
 
   void accept_vis (ASTVisitor &vis) override;
 
+  PathType get_glob_type () { return glob_type; }
+
   Kind get_kind () const override { return Glob; }
 
   SimplePath get_path () const
@@ -1367,6 +1369,8 @@ public:
 
   std::string as_string () const override;
 
+  PathType get_path_type () { return path_type; }
+
   void accept_vis (ASTVisitor &vis) override;
 
   Kind get_kind () const override { return List; }
@@ -1376,6 +1380,8 @@ public:
     return path;
   }
 
+  std::vector<std::unique_ptr<UseTree>> &get_trees () { return trees; }
+
   const std::vector<std::unique_ptr<UseTree>> &get_trees () const
   {
     return trees;
@@ -1424,6 +1430,8 @@ public:
 
   std::string as_string () const override;
 
+  NewBindType get_new_bind_type () { return bind_type; }
+
   void accept_vis (ASTVisitor &vis) override;
 
   Kind get_kind () const override { return Rebind; }
@@ -1497,6 +1505,9 @@ public:
   UseDeclaration &operator= (UseDeclaration &&other) = default;
 
   Location get_locus () const override final { return locus; }
+
+  std::unique_ptr<UseTree> &get_tree () { return use_tree; }
+
   const std::unique_ptr<UseTree> &get_tree () const { return use_tree; }
 
   void accept_vis (ASTVisitor &vis) override;


More information about the Gcc-cvs mailing list