]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: ast: Add use declarations TokenStream visitors
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 23 Mar 2023 15:46:51 +0000 (16:46 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:41 +0000 (18:28 +0100)
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>
gcc/rust/ast/rust-ast-tokenstream.cc
gcc/rust/ast/rust-item.h

index f5d04450dbc694ddceea4f91a04d681a9968631f..9fb9f931202451b9846477ce7c318a033181212b 100644 (file)
@@ -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)
index b82b5fbcea317e282aa63c5017b369befb0e7b03..e08a35234a85f1a7bf9236f4796d37e58ae147ee 100644 (file)
@@ -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;