]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: ast: Parse labelled block
authorJakub Dupak <dev@jakubdupak.com>
Mon, 16 Oct 2023 12:42:49 +0000 (14:42 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:09:20 +0000 (19:09 +0100)
gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc (AstBuilder::block): Add label arg to constructor call.
* ast/rust-expr.h (class LoopLabel): Move before BlockExpr.
(class BlockExpr): Add LoopLabel member.
* expand/rust-derive-clone.cc (DeriveClone::clone_fn): Add label arg to constructor call.
* parse/rust-parse-impl.h (Parser::parse_block_expr): Add label parameter.
(Parser::parse_labelled_loop_expr): Add label arg to constructor call.
* parse/rust-parse.h: Add label arg to constructor call.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
gcc/rust/ast/rust-ast-builder.cc
gcc/rust/ast/rust-expr.h
gcc/rust/expand/rust-derive-clone.cc
gcc/rust/parse/rust-parse-impl.h
gcc/rust/parse/rust-parse.h

index b630cfa53920276bba25e1b01ea0c1da845b41bd..e2d3eea6c583ca839938f8a45cb9475afb8a0c55 100644 (file)
@@ -85,8 +85,9 @@ std::unique_ptr<Expr>
 AstBuilder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
                   std::unique_ptr<Expr> &&tail_expr)
 {
-  return std::unique_ptr<Expr> (
-    new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, loc, loc));
+  return std::unique_ptr<Expr> (new BlockExpr (std::move (stmts),
+                                              std::move (tail_expr), {}, {},
+                                              LoopLabel::error (), loc, loc));
 }
 
 std::unique_ptr<Stmt>
index 012b055782364dba567a2fba60af45f1c52c02e1..bd59f81b562f283aa35b60b32de7d1c2fabca6f9 100644 (file)
@@ -11,6 +11,36 @@ namespace AST {
  * "has_whatever" pairs with
  * optional types (std::optional or boost::optional)? */
 
+// Loop label expression AST node used with break and continue expressions
+// TODO: inline?
+class LoopLabel /*: public Node*/
+{
+  Lifetime label; // or type LIFETIME_OR_LABEL
+  location_t locus;
+
+  NodeId node_id;
+
+public:
+  std::string as_string () const;
+
+  LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION)
+    : label (std::move (loop_label)), locus (locus),
+      node_id (Analysis::Mappings::get ()->get_next_node_id ())
+  {}
+
+  // Returns whether the LoopLabel is in an error state.
+  bool is_error () const { return label.is_error (); }
+
+  // Creates an error state LoopLabel.
+  static LoopLabel error () { return LoopLabel (Lifetime::error ()); }
+
+  location_t get_locus () const { return locus; }
+
+  Lifetime &get_lifetime () { return label; }
+
+  NodeId get_node_id () const { return node_id; }
+};
+
 // AST node for an expression with an accompanying block - abstract
 class ExprWithBlock : public Expr
 {
@@ -2396,6 +2426,7 @@ class BlockExpr : public ExprWithBlock
   std::vector<Attribute> inner_attrs;
   std::vector<std::unique_ptr<Stmt> > statements;
   std::unique_ptr<Expr> expr;
+  LoopLabel label;
   location_t start_locus;
   location_t end_locus;
   bool marked_for_strip = false;
@@ -2412,19 +2443,21 @@ public:
   BlockExpr (std::vector<std::unique_ptr<Stmt> > block_statements,
             std::unique_ptr<Expr> block_expr,
             std::vector<Attribute> inner_attribs,
-            std::vector<Attribute> outer_attribs, location_t start_locus,
-            location_t end_locus)
+            std::vector<Attribute> outer_attribs, LoopLabel label,
+            location_t start_locus, location_t end_locus)
     : outer_attrs (std::move (outer_attribs)),
       inner_attrs (std::move (inner_attribs)),
       statements (std::move (block_statements)), expr (std::move (block_expr)),
-      start_locus (start_locus), end_locus (end_locus)
+      label (std::move (label)), start_locus (start_locus),
+      end_locus (end_locus)
   {}
 
   // Copy constructor with clone
   BlockExpr (BlockExpr const &other)
     : ExprWithBlock (other), outer_attrs (other.outer_attrs),
-      inner_attrs (other.inner_attrs), start_locus (other.start_locus),
-      end_locus (other.end_locus), marked_for_strip (other.marked_for_strip)
+      inner_attrs (other.inner_attrs), label (other.label),
+      start_locus (other.start_locus), end_locus (other.end_locus),
+      marked_for_strip (other.marked_for_strip)
   {
     // guard to protect from null pointer dereference
     if (other.expr != nullptr)
@@ -2524,6 +2557,9 @@ public:
     outer_attrs = std::move (new_attrs);
   }
 
+  bool has_label () { return !label.is_error (); }
+  LoopLabel &get_label () { return label; }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
    * than base */
@@ -3352,36 +3388,6 @@ protected:
   }
 };
 
-// Loop label expression AST node used with break and continue expressions
-// TODO: inline?
-class LoopLabel /*: public Node*/
-{
-  Lifetime label; // or type LIFETIME_OR_LABEL
-  location_t locus;
-
-  NodeId node_id;
-
-public:
-  std::string as_string () const;
-
-  LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION)
-    : label (std::move (loop_label)), locus (locus),
-      node_id (Analysis::Mappings::get ()->get_next_node_id ())
-  {}
-
-  // Returns whether the LoopLabel is in an error state.
-  bool is_error () const { return label.is_error (); }
-
-  // Creates an error state LoopLabel.
-  static LoopLabel error () { return LoopLabel (Lifetime::error ()); }
-
-  location_t get_locus () const { return locus; }
-
-  Lifetime &get_lifetime () { return label; }
-
-  NodeId get_node_id () const { return node_id; }
-};
-
 // Base loop expression AST node - aka LoopExpr
 class BaseLoopExpr : public ExprWithBlock
 {
index 964602b98b9e6a817f58b1e497872c04c2300bef..cac309946576fd00902364a0578901d49061af34 100644 (file)
@@ -46,7 +46,8 @@ std::unique_ptr<TraitImplItem>
 DeriveClone::clone_fn (std::unique_ptr<Expr> &&clone_expr)
 {
   auto block = std::unique_ptr<BlockExpr> (
-    new BlockExpr ({}, std::move (clone_expr), {}, {}, loc, loc));
+    new BlockExpr ({}, std::move (clone_expr), {}, {}, AST::LoopLabel::error (),
+                  loc, loc));
   auto big_self_type = builder.single_type_path ("Self");
 
   return std::unique_ptr<TraitImplItem> (
index 1e59913e88eafb0bab5981ebde5114212e7c8229..7844f7214ad2984c44339e09a614c0ca70f4ff10 100644 (file)
@@ -7359,6 +7359,7 @@ Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
 template <typename ManagedTokenSource>
 std::unique_ptr<AST::BlockExpr>
 Parser<ManagedTokenSource>::parse_block_expr (AST::AttrVec outer_attrs,
+                                             AST::LoopLabel label,
                                              location_t pratt_parsed_loc)
 {
   location_t locus = pratt_parsed_loc;
@@ -7425,8 +7426,8 @@ Parser<ManagedTokenSource>::parse_block_expr (AST::AttrVec outer_attrs,
 
   return std::unique_ptr<AST::BlockExpr> (
     new AST::BlockExpr (std::move (stmts), std::move (expr),
-                       std::move (inner_attrs), std::move (outer_attrs), locus,
-                       end_locus));
+                       std::move (inner_attrs), std::move (outer_attrs),
+                       std::move (label), locus, end_locus));
 }
 
 /* Parses a "grouped" expression (expression in parentheses), used to control
@@ -8367,7 +8368,7 @@ Parser<ManagedTokenSource>::parse_for_loop_expr (AST::AttrVec outer_attrs,
 
 // Parses a loop expression with label (any kind of loop - disambiguates).
 template <typename ManagedTokenSource>
-std::unique_ptr<AST::BaseLoopExpr>
+std::unique_ptr<AST::Expr>
 Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
                                                      AST::AttrVec outer_attrs)
 {
@@ -8419,6 +8420,8 @@ Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
          return parse_while_loop_expr (std::move (outer_attrs),
                                        std::move (label));
        }
+    case LEFT_CURLY:
+      return parse_block_expr (std::move (outer_attrs), std::move (label));
     default:
       // error
       add_error (Error (t->get_locus (),
@@ -12537,7 +12540,8 @@ Parser<ManagedTokenSource>::null_denotation_not_path (
       return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
     case LEFT_CURLY:
       // ok - this is an expression with block for once.
-      return parse_block_expr (std::move (outer_attrs), tok->get_locus ());
+      return parse_block_expr (std::move (outer_attrs),
+                              AST::LoopLabel::error (), tok->get_locus ());
     case IF:
       // if or if let, so more lookahead to find out
       if (lexer.peek_token ()->get_id () == LET)
index d5c1219b08007b205c2aefc3b5b9513dff70f9a5..6a69533b3f2b3eb2fc6e4e3db69438a27cdccb5d 100644 (file)
@@ -139,6 +139,7 @@ public:
 
   std::unique_ptr<AST::BlockExpr>
   parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
+                   AST::LoopLabel label = AST::LoopLabel::error (),
                    location_t pratt_parsed_loc = UNKNOWN_LOCATION);
 
   bool is_macro_rules_def (const_TokenPtr t);
@@ -590,9 +591,9 @@ private:
   AST::MatchArm parse_match_arm ();
   std::vector<std::unique_ptr<AST::Pattern> >
   parse_match_arm_patterns (TokenId end_token_id);
-  std::unique_ptr<AST::BaseLoopExpr>
-  parse_labelled_loop_expr (const_TokenPtr tok,
-                           AST::AttrVec outer_attrs = AST::AttrVec ());
+  std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
+                                                      AST::AttrVec outer_attrs
+                                                      = AST::AttrVec ());
   AST::LoopLabel parse_loop_label (const_TokenPtr tok);
   std::unique_ptr<AST::AsyncBlockExpr>
   parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());