]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: ast: More visitors to allow lang item paths in expressions
authorArthur Cohen <arthur.cohen@embecosm.com>
Wed, 22 Jan 2025 14:09:08 +0000 (14:09 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 24 Mar 2025 12:06:54 +0000 (13:06 +0100)
gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Adapt visitor to lang item
PathInExpressions.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
* expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise.
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
* hir/rust-ast-lower.cc (ASTLoweringExprWithBlock::visit): Likewise.
(ASTLowerPathInExpression::visit): Likewise.
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): Likewise.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise.

gcc/rust/ast/rust-ast-collector.cc
gcc/rust/ast/rust-ast-visitor.cc
gcc/rust/expand/rust-cfg-strip.cc
gcc/rust/expand/rust-expand-visitor.cc
gcc/rust/hir/rust-ast-lower.cc
gcc/rust/resolve/rust-ast-resolve-path.cc
gcc/rust/resolve/rust-early-name-resolver.cc

index 978de648a5833ac0e0ce776eeea0ce0c626a79f2..073fa728dcf1b2a6fbda872e61c27ecf961dc840 100644 (file)
 // You should have received a copy of the GNU General Public License
 // along with GCC; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
+
 #include "rust-ast-collector.h"
 #include "rust-ast.h"
 #include "rust-diagnostics.h"
 #include "rust-expr.h"
 #include "rust-item.h"
 #include "rust-keyword-values.h"
+#include "rust-path.h"
 #include "rust-system.h"
 #include "rust-token.h"
 
@@ -530,11 +532,23 @@ TokenCollector::visit (PathExprSegment &segment)
 void
 TokenCollector::visit (PathInExpression &path)
 {
-  if (path.opening_scope_resolution ())
+  if (path.is_lang_item ())
     {
-      push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
+      push (Rust::Token::make (TokenId::HASH, path.get_locus ()));
+      push (Rust::Token::make (TokenId::LEFT_SQUARE, path.get_locus ()));
+      push (Rust::Token::make_identifier (path.get_locus (), "lang"));
+      push (Rust::Token::make (TokenId::EQUAL, path.get_locus ()));
+      push (
+       Rust::Token::make_string (path.get_locus (),
+                                 LangItem::ToString (path.get_lang_item ())));
+      push (Rust::Token::make (TokenId::RIGHT_SQUARE, path.get_locus ()));
+
+      return;
     }
 
+  if (path.opening_scope_resolution ())
+    push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
+
   visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
 }
 
index e2e2d9dda37bf9ba52fef7f56730de9267c970c0..077b41b5cacf814818c643f2dcd9fc4c3d0a190b 100644 (file)
@@ -89,8 +89,10 @@ void
 DefaultASTVisitor::visit (AST::PathInExpression &path)
 {
   visit_outer_attrs (path);
-  for (auto &segment : path.get_segments ())
-    visit (segment);
+
+  if (!path.is_lang_item ())
+    for (auto &segment : path.get_segments ())
+      visit (segment);
 }
 
 void
index 4e6a8ac2b5fa183bdbe9097d3a697965130445d1..a8c3ca5bfc98df03b1bf6c870664e6bb8fe4c8f7 100644 (file)
@@ -19,6 +19,7 @@
 #include "rust-cfg-strip.h"
 #include "rust-ast-full.h"
 #include "rust-ast-visitor.h"
+#include "rust-path.h"
 #include "rust-session-manager.h"
 #include "rust-attribute-values.h"
 
@@ -434,10 +435,13 @@ CfgStrip::visit (AST::PathInExpression &path)
       return;
     }
 
-  for (auto &segment : path.get_segments ())
+  if (!path.is_lang_item ())
     {
-      if (segment.has_generic_args ())
-       maybe_strip_generic_args (segment.get_generic_args ());
+      for (auto &segment : path.get_segments ())
+       {
+         if (segment.has_generic_args ())
+           maybe_strip_generic_args (segment.get_generic_args ());
+       }
     }
 }
 
index 38399d0d74b2c53ec852e6317c2607aa92e49037..6e06fa516098a5613f7a8356e2604dfbea9ab225 100644 (file)
@@ -477,9 +477,10 @@ ExpandVisitor::visit (AST::MacroInvocation &macro_invoc)
 void
 ExpandVisitor::visit (AST::PathInExpression &path)
 {
-  for (auto &segment : path.get_segments ())
-    if (segment.has_generic_args ())
-      expand_generic_args (segment.get_generic_args ());
+  if (!path.is_lang_item ())
+    for (auto &segment : path.get_segments ())
+      if (segment.has_generic_args ())
+       expand_generic_args (segment.get_generic_args ());
 }
 
 void
index a94a8432273bf29084268753b873fc8ec6855dcd..ebdf9810b34fc6ee158bc61f2e5ff32e47969b53 100644 (file)
@@ -418,24 +418,7 @@ ASTLoweringExprWithBlock::visit (AST::WhileLoopExpr &expr)
 void
 ASTLoweringExprWithBlock::visit (AST::ForLoopExpr &expr)
 {
-  // TODO FIXME
-
-  // HIR::BlockExpr *loop_block
-  //   = ASTLoweringBlock::translate (expr.get_loop_block ().get (),
-  //   &terminated);
-  // HIR::LoopLabel loop_label = lower_loop_label (expr.get_loop_label ());
-  // HIR::Expr *iterator_expr
-  //   = ASTLoweringExpr::translate (expr.get_iterator_expr ().get (),
-  //                             &terminated);
-  // HIR::Pattern *loop_pattern
-  //   = ASTLoweringPattern::translate (expr.get_pattern ().get ());
-
-  // auto crate_num = mappings->get_current_crate ();
-  // Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
-  //                            mappings->get_next_hir_id (crate_num),
-  //                            UNKNOWN_LOCAL_DEFID);
-
-  gcc_unreachable ();
+  rust_unreachable ();
 }
 
 void
@@ -494,6 +477,18 @@ ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr)
 void
 ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
 {
+  auto crate_num = mappings.get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
+                                mappings.get_next_hir_id (crate_num),
+                                UNKNOWN_LOCAL_DEFID);
+
+  if (expr.is_lang_item ())
+    {
+      translated = new HIR::PathInExpression (mapping, expr.get_lang_item (),
+                                             expr.get_locus (), false);
+      return;
+    }
+
   std::vector<HIR::PathExprSegment> path_segments;
   auto &segments = expr.get_segments ();
   for (auto &s : segments)
@@ -504,10 +499,6 @@ ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
       HIR::PathExprSegment *lowered_seg = &path_segments.back ();
       mappings.insert_hir_path_expr_seg (lowered_seg);
     }
-  auto crate_num = mappings.get_current_crate ();
-  Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
-                                mappings.get_next_hir_id (crate_num),
-                                UNKNOWN_LOCAL_DEFID);
 
   translated = new HIR::PathInExpression (mapping, std::move (path_segments),
                                          expr.get_locus (),
index ea39fd4669061d88430dfee18cf6c703a15cf85f..256159bc5cc5bcf3375b61b461cc054978737027 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "rust-ast-resolve-path.h"
 #include "rust-ast-resolve-type.h"
+#include "rust-hir-map.h"
 #include "rust-path.h"
 
 namespace Rust {
@@ -49,6 +50,10 @@ ResolvePath::go (AST::SimplePath &expr)
 NodeId
 ResolvePath::resolve_path (AST::PathInExpression &expr)
 {
+  if (expr.is_lang_item ())
+    return Analysis::Mappings::get ().get_lang_item_node (
+      expr.get_lang_item ());
+
   NodeId resolved_node_id = UNKNOWN_NODEID;
   NodeId module_scope_id = resolver->peek_current_module_scope ();
   NodeId previous_resolved_node_id = module_scope_id;
index 34bac98534cc15b088536fc3e13cb630bd0bb4c3..42a4a17090f7826de4e01847b60218c5b924d691 100644 (file)
@@ -156,9 +156,10 @@ EarlyNameResolver::visit (AST::ConstGenericParam &)
 void
 EarlyNameResolver::visit (AST::PathInExpression &path)
 {
-  for (auto &segment : path.get_segments ())
-    if (segment.has_generic_args ())
-      resolve_generic_args (segment.get_generic_args ());
+  if (!path.is_lang_item ())
+    for (auto &segment : path.get_segments ())
+      if (segment.has_generic_args ())
+       resolve_generic_args (segment.get_generic_args ());
 }
 
 void