]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Resolve labels
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 31 Mar 2025 15:00:11 +0000 (17:00 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 8 Apr 2025 08:17:16 +0000 (10:17 +0200)
gcc/rust/ChangeLog:

* hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Lower label only if
it exists.
* hir/tree/rust-hir-expr.cc (BlockExpr::BlockExpr): Make loop label
optional.
(BaseLoopExpr::BaseLoopExpr): Likewise.
(LoopExpr::LoopExpr): Likewise.
(WhileLoopExpr::WhileLoopExpr): Likewise.
* hir/tree/rust-hir-expr.h: Use optional for lifetime and labels.
* hir/tree/rust-hir.cc (WhileLoopExpr::as_string): Use getter.
(WhileLetLoopExpr::as_string): Likewise.
(LoopExpr::as_string): Likewise.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Resolve labels.
* resolve/rust-late-name-resolver-2.0.h: Add visit function prototype
for loop labels.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/hir/rust-ast-lower.cc
gcc/rust/hir/tree/rust-hir-expr.cc
gcc/rust/hir/tree/rust-hir-expr.h
gcc/rust/hir/tree/rust-hir.cc
gcc/rust/resolve/rust-late-name-resolver-2.0.cc
gcc/rust/resolve/rust-late-name-resolver-2.0.h

index ebdf9810b34fc6ee158bc61f2e5ff32e47969b53..c14fccde0816c5078ba7ccd05e40a61eef4a0651 100644 (file)
@@ -97,7 +97,11 @@ ASTLowering::go ()
 void
 ASTLoweringBlock::visit (AST::BlockExpr &expr)
 {
-  auto label = lower_loop_label (expr.get_label ());
+  tl::optional<HIR::LoopLabel> label;
+  if (expr.has_label ())
+    label = lower_loop_label (expr.get_label ());
+  else
+    label = tl::nullopt;
 
   std::vector<std::unique_ptr<HIR::Stmt>> block_stmts;
   bool block_did_terminate = false;
index bb7ebfbc617bd7ae56b350f12aa57964f394e062..82a09e935887d613896133f4fba32f8b3a89a09b 100644 (file)
@@ -749,7 +749,7 @@ BlockExpr::BlockExpr (Analysis::NodeMapping mappings,
                      std::vector<std::unique_ptr<Stmt>> block_statements,
                      std::unique_ptr<Expr> block_expr, bool tail_reachable,
                      AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
-                     LoopLabel label, location_t start_locus,
+                     tl::optional<LoopLabel> label, location_t start_locus,
                      location_t end_locus)
   : ExprWithBlock (std::move (mappings), std::move (outer_attribs)),
     WithInnerAttrs (std::move (inner_attribs)),
@@ -985,7 +985,8 @@ UnsafeBlockExpr::operator= (UnsafeBlockExpr const &other)
 
 BaseLoopExpr::BaseLoopExpr (Analysis::NodeMapping mappings,
                            std::unique_ptr<BlockExpr> loop_block,
-                           location_t locus, LoopLabel loop_label,
+                           location_t locus,
+                           tl::optional<LoopLabel> loop_label,
                            AST::AttrVec outer_attribs)
   : ExprWithBlock (std::move (mappings), std::move (outer_attribs)),
     loop_label (std::move (loop_label)), loop_block (std::move (loop_block)),
@@ -1011,7 +1012,8 @@ BaseLoopExpr::operator= (BaseLoopExpr const &other)
 
 LoopExpr::LoopExpr (Analysis::NodeMapping mappings,
                    std::unique_ptr<BlockExpr> loop_block, location_t locus,
-                   LoopLabel loop_label, AST::AttrVec outer_attribs)
+                   tl::optional<LoopLabel> loop_label,
+                   AST::AttrVec outer_attribs)
   : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus,
                  std::move (loop_label), std::move (outer_attribs))
 {}
@@ -1019,7 +1021,8 @@ LoopExpr::LoopExpr (Analysis::NodeMapping mappings,
 WhileLoopExpr::WhileLoopExpr (Analysis::NodeMapping mappings,
                              std::unique_ptr<Expr> loop_condition,
                              std::unique_ptr<BlockExpr> loop_block,
-                             location_t locus, LoopLabel loop_label,
+                             location_t locus,
+                             tl::optional<LoopLabel> loop_label,
                              AST::AttrVec outer_attribs)
   : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus,
                  std::move (loop_label), std::move (outer_attribs)),
@@ -1046,7 +1049,8 @@ WhileLetLoopExpr::WhileLetLoopExpr (
   Analysis::NodeMapping mappings,
   std::vector<std::unique_ptr<Pattern>> match_arm_patterns,
   std::unique_ptr<Expr> condition, std::unique_ptr<BlockExpr> loop_block,
-  location_t locus, LoopLabel loop_label, AST::AttrVec outer_attribs)
+  location_t locus, tl::optional<LoopLabel> loop_label,
+  AST::AttrVec outer_attribs)
   : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus,
                  std::move (loop_label), std::move (outer_attribs)),
     match_arm_patterns (std::move (match_arm_patterns)),
index 3ff38b340655187f7775bbeba8d6a54e20a63a46..2d1059ab8cc5ec0bb109cf60293db173227d3681 100644 (file)
@@ -1717,7 +1717,7 @@ public:
   std::vector<std::unique_ptr<Stmt>> statements;
   std::unique_ptr<Expr> expr;
   bool tail_reachable;
-  LoopLabel label;
+  tl::optional<LoopLabel> label;
   location_t start_locus;
   location_t end_locus;
 
@@ -1737,7 +1737,8 @@ public:
             std::vector<std::unique_ptr<Stmt>> block_statements,
             std::unique_ptr<Expr> block_expr, bool tail_reachable,
             AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
-            LoopLabel label, location_t start_locus, location_t end_locus);
+            tl::optional<LoopLabel> label, location_t start_locus,
+            location_t end_locus);
 
   // Copy constructor with clone
   BlockExpr (BlockExpr const &other);
@@ -1776,8 +1777,8 @@ public:
     return ExprType::Block;
   }
 
-  bool has_label () const { return !label.is_error (); }
-  LoopLabel &get_label () { return label; }
+  bool has_label () const { return label.has_value (); }
+  LoopLabel &get_label () { return label.value (); }
 
 protected:
   /* Use covariance to implement clone function as returning this object rather
@@ -2295,7 +2296,7 @@ protected:
 class BaseLoopExpr : public ExprWithBlock
 {
 protected:
-  LoopLabel loop_label;
+  tl::optional<LoopLabel> loop_label;
   std::unique_ptr<BlockExpr> loop_block;
 
 private:
@@ -2305,7 +2306,7 @@ protected:
   // Constructor for BaseLoopExpr
   BaseLoopExpr (Analysis::NodeMapping mappings,
                std::unique_ptr<BlockExpr> loop_block, location_t locus,
-               LoopLabel loop_label,
+               tl::optional<LoopLabel> loop_label,
                AST::AttrVec outer_attribs = AST::AttrVec ());
 
   // Copy constructor for BaseLoopExpr with clone
@@ -2324,13 +2325,14 @@ protected:
   }
 
 public:
-  bool has_loop_label () const { return !loop_label.is_error (); }
+  bool has_loop_label () const { return loop_label.has_value (); }
 
   location_t get_locus () const override final { return locus; }
 
   HIR::BlockExpr &get_loop_block () { return *loop_block; };
 
-  LoopLabel &get_loop_label () { return loop_label; }
+  LoopLabel &get_loop_label () { return loop_label.value (); }
+  const LoopLabel &get_loop_label () const { return loop_label.value (); }
 };
 
 // 'Loop' expression (i.e. the infinite loop) HIR node
@@ -2342,7 +2344,8 @@ public:
   // Constructor for LoopExpr
   LoopExpr (Analysis::NodeMapping mappings,
            std::unique_ptr<BlockExpr> loop_block, location_t locus,
-           LoopLabel loop_label, AST::AttrVec outer_attribs = AST::AttrVec ());
+           tl::optional<LoopLabel> loop_label,
+           AST::AttrVec outer_attribs = AST::AttrVec ());
 
   void accept_vis (HIRFullVisitor &vis) override;
   void accept_vis (HIRExpressionVisitor &vis) override;
@@ -2372,7 +2375,7 @@ public:
   WhileLoopExpr (Analysis::NodeMapping mappings,
                 std::unique_ptr<Expr> loop_condition,
                 std::unique_ptr<BlockExpr> loop_block, location_t locus,
-                LoopLabel loop_label,
+                tl::optional<LoopLabel> loop_label,
                 AST::AttrVec outer_attribs = AST::AttrVec ());
 
   // Copy constructor with clone
@@ -2421,7 +2424,7 @@ public:
                    std::vector<std::unique_ptr<Pattern>> match_arm_patterns,
                    std::unique_ptr<Expr> condition,
                    std::unique_ptr<BlockExpr> loop_block, location_t locus,
-                   LoopLabel loop_label,
+                   tl::optional<LoopLabel> loop_label,
                    AST::AttrVec outer_attribs = AST::AttrVec ());
 
   // Copy constructor with clone
index 822eaffcb7c18d6fa9b17eddfb8a31dc52f4272d..771ae9c79629077582c7d1fcde8b209261c209b2 100644 (file)
@@ -1704,7 +1704,7 @@ WhileLoopExpr::as_string () const
     }
   else
     {
-      str += loop_label.as_string ();
+      str += get_loop_label ().as_string ();
     }
 
   str += "\n Conditional expr: " + condition->as_string ();
@@ -1726,7 +1726,7 @@ WhileLetLoopExpr::as_string () const
     }
   else
     {
-      str += loop_label.as_string ();
+      str += get_loop_label ().as_string ();
     }
 
   str += "\n Match arm patterns: ";
@@ -1761,7 +1761,7 @@ LoopExpr::as_string () const
     }
   else
     {
-      str += loop_label.as_string ();
+      str += get_loop_label ().as_string ();
     }
 
   str += "\n Loop block: " + loop_block->as_string ();
index d18e7e2a32417261265bc60af61c411e444cb49e..e0006fdea27935301471a78323409aea63d3249a 100644 (file)
@@ -212,6 +212,25 @@ Late::visit (AST::BreakExpr &expr)
   funny_error = false;
 }
 
+void
+Late::visit (AST::LoopLabel &label)
+{
+  // Shall we move this to visit(AST::Lifetime) or do we need to
+  // keep the context ?
+  auto lifetime = label.get_lifetime ();
+  if (auto resolved = ctx.labels.get (lifetime.as_string ()))
+    {
+      ctx.map_usage (Usage (lifetime.get_node_id ()),
+                    Definition (resolved->get_node_id ()));
+    }
+  else
+    {
+      ctx.labels.insert (Identifier (lifetime.as_string (),
+                                    lifetime.get_locus ()),
+                        lifetime.get_node_id ());
+    }
+}
+
 void
 Late::visit (AST::IdentifierExpr &expr)
 {
index ac376b5cb6f8261e60a45bb8697f5f64e1dc629d..e40141c9238ce69acfc535a274ffca69a2e99824 100644 (file)
@@ -48,6 +48,7 @@ public:
   void visit (AST::IdentifierExpr &) override;
   void visit (AST::StructExprFieldIdentifier &) override;
   void visit (AST::BreakExpr &) override;
+  void visit (AST::LoopLabel &) override;
   void visit (AST::PathInExpression &) override;
   void visit (AST::TypePath &) override;
   void visit (AST::Trait &) override;