diff options
author | Jakub Dupak <dev@jakubdupak.com> | 2023-10-16 14:42:49 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 19:09:20 +0100 |
commit | 3b1d27f78720bf4cf3857f31a4d26226e2581fb1 (patch) | |
tree | 73ddf9a35bf03586194d48e18d2edc6e2e61f63a /gcc/rust | |
parent | 9e7e3ea6380d4c8fe01740ce5278e84d2124fe93 (diff) | |
download | gcc-3b1d27f78720bf4cf3857f31a4d26226e2581fb1.zip gcc-3b1d27f78720bf4cf3857f31a4d26226e2581fb1.tar.gz gcc-3b1d27f78720bf4cf3857f31a4d26226e2581fb1.tar.bz2 |
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 <dev@jakubdupak.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r-- | gcc/rust/ast/rust-ast-builder.cc | 5 | ||||
-rw-r--r-- | gcc/rust/ast/rust-expr.h | 76 | ||||
-rw-r--r-- | gcc/rust/expand/rust-derive-clone.cc | 3 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 12 | ||||
-rw-r--r-- | 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 b630cfa..e2d3eea 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -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> diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 012b055..bd59f81 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<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 { diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc index 964602b..cac3099 100644 --- a/gcc/rust/expand/rust-derive-clone.cc +++ b/gcc/rust/expand/rust-derive-clone.cc @@ -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> ( diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 1e59913..7844f72 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -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) diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index d5c1219..6a69533 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -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 ()); |