From 3b1d27f78720bf4cf3857f31a4d26226e2581fb1 Mon Sep 17 00:00:00 2001 From: Jakub Dupak Date: Mon, 16 Oct 2023 14:42:49 +0200 Subject: [PATCH] gccrs: ast: Parse labelled block 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 --- gcc/rust/ast/rust-ast-builder.cc | 5 +- gcc/rust/ast/rust-expr.h | 76 +++++++++++++++------------- gcc/rust/expand/rust-derive-clone.cc | 3 +- gcc/rust/parse/rust-parse-impl.h | 12 +++-- gcc/rust/parse/rust-parse.h | 7 +-- 5 files changed, 58 insertions(+), 45 deletions(-) diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index b630cfa53920..e2d3eea6c583 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -85,8 +85,9 @@ std::unique_ptr AstBuilder::block (std::vector> &&stmts, std::unique_ptr &&tail_expr) { - return std::unique_ptr ( - new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, loc, loc)); + return std::unique_ptr (new BlockExpr (std::move (stmts), + std::move (tail_expr), {}, {}, + LoopLabel::error (), loc, loc)); } std::unique_ptr diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 012b05578236..bd59f81b562f 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -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 inner_attrs; std::vector > statements; std::unique_ptr expr; + LoopLabel label; location_t start_locus; location_t end_locus; bool marked_for_strip = false; @@ -2412,19 +2443,21 @@ public: BlockExpr (std::vector > block_statements, std::unique_ptr block_expr, std::vector inner_attribs, - std::vector outer_attribs, location_t start_locus, - location_t end_locus) + std::vector 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 { diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc index 964602b98b9e..cac309946576 100644 --- a/gcc/rust/expand/rust-derive-clone.cc +++ b/gcc/rust/expand/rust-derive-clone.cc @@ -46,7 +46,8 @@ std::unique_ptr DeriveClone::clone_fn (std::unique_ptr &&clone_expr) { auto block = std::unique_ptr ( - 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 ( diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 1e59913e88ea..7844f7214ad2 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -7359,6 +7359,7 @@ Parser::parse_expr_stmt (AST::AttrVec outer_attrs, template std::unique_ptr Parser::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::parse_block_expr (AST::AttrVec outer_attrs, return std::unique_ptr ( 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::parse_for_loop_expr (AST::AttrVec outer_attrs, // Parses a loop expression with label (any kind of loop - disambiguates). template -std::unique_ptr +std::unique_ptr Parser::parse_labelled_loop_expr (const_TokenPtr tok, AST::AttrVec outer_attrs) { @@ -8419,6 +8420,8 @@ Parser::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::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) diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index d5c1219b0800..6a69533b3f2b 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -139,6 +139,7 @@ public: std::unique_ptr 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 > parse_match_arm_patterns (TokenId end_token_id); - std::unique_ptr - parse_labelled_loop_expr (const_TokenPtr tok, - AST::AttrVec outer_attrs = AST::AttrVec ()); + std::unique_ptr parse_labelled_loop_expr (const_TokenPtr tok, + AST::AttrVec outer_attrs + = AST::AttrVec ()); AST::LoopLabel parse_loop_label (const_TokenPtr tok); std::unique_ptr parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ()); -- 2.47.2