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/ast/rust-expr.h | |
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/ast/rust-expr.h')
-rw-r--r-- | gcc/rust/ast/rust-expr.h | 76 |
1 files changed, 41 insertions, 35 deletions
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 { |