diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-03-23 16:46:51 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-03-30 16:48:27 +0200 |
commit | 3d9dbf5c004b4ab2206b86c68c2c824229aeee34 (patch) | |
tree | 3e1ad8d53e6675a59051cf51230f9e9a9486691f | |
parent | c34e05d01cfb3ffd5f2adb51eb6ab4d40aa3330f (diff) | |
download | gcc-3d9dbf5c004b4ab2206b86c68c2c824229aeee34.zip gcc-3d9dbf5c004b4ab2206b86c68c2c824229aeee34.tar.gz gcc-3d9dbf5c004b4ab2206b86c68c2c824229aeee34.tar.bz2 |
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>
-rw-r--r-- | gcc/rust/ast/rust-ast-tokenstream.cc | 77 | ||||
-rw-r--r-- | 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 f5d0445..9fb9f93 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 56cc13d..49e340e 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; |