From: Jakub Dupak Date: Mon, 16 Oct 2023 13:17:33 +0000 (+0200) Subject: gccrs: hir: Lower labelled block X-Git-Tag: basepoints/gcc-15~2078 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a66df6197e84b6817d20c494bee7242bbfab0369;p=thirdparty%2Fgcc.git gccrs: hir: Lower labelled block gcc/rust/ChangeLog: * hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Call loop lowering and add it to constr. * hir/tree/rust-hir-expr.h (class LoopLabel): Move before BlockExpr and add to BlockExpr. --- diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index 308a2ec83a41..d730f29b13a8 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -95,6 +95,8 @@ ASTLowering::go () void ASTLoweringBlock::visit (AST::BlockExpr &expr) { + auto label = lower_loop_label (expr.get_label ()); + std::vector> block_stmts; bool block_did_terminate = false; @@ -143,8 +145,8 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr) = new HIR::BlockExpr (mapping, std::move (block_stmts), std::unique_ptr (tail_expr), tail_reachable, expr.get_inner_attrs (), - expr.get_outer_attrs (), expr.get_start_locus (), - expr.get_end_locus ()); + expr.get_outer_attrs (), label, + expr.get_start_locus (), expr.get_end_locus ()); terminated = block_did_terminate; } diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index 3bfadbf2dc01..6d26287dbe3f 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -28,6 +28,34 @@ namespace Rust { namespace HIR { +// Loop label expression HIR node used with break and continue expressions +// TODO: inline? +class LoopLabel /*: public Node*/ +{ + Lifetime label; // or type LIFETIME_OR_LABEL + + location_t locus; + + Analysis::NodeMapping mappings; + +public: + std::string as_string () const; + + LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label, + location_t locus) + : label (std::move (loop_label)), locus (locus), mappings (mapping) + {} + + // Returns whether the LoopLabel is in an error state. + bool is_error () const { return label.is_error (); } + + location_t get_locus () const { return locus; } + + Analysis::NodeMapping &get_mappings () { return mappings; } + + Lifetime &get_lifetime () { return label; } +}; + // HIR node for an expression with an accompanying block - abstract class ExprWithBlock : public Expr { @@ -2121,6 +2149,7 @@ public: std::vector > statements; std::unique_ptr expr; bool tail_reachable; + LoopLabel label; location_t start_locus; location_t end_locus; @@ -2140,19 +2169,19 @@ public: std::vector > block_statements, std::unique_ptr block_expr, bool tail_reachable, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - location_t start_locus, location_t end_locus) + LoopLabel label, location_t start_locus, location_t end_locus) : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), WithInnerAttrs (std::move (inner_attribs)), statements (std::move (block_statements)), expr (std::move (block_expr)), - tail_reachable (tail_reachable), start_locus (start_locus), - end_locus (end_locus) + tail_reachable (tail_reachable), label (std::move (label)), + start_locus (start_locus), end_locus (end_locus) {} // Copy constructor with clone BlockExpr (BlockExpr const &other) : ExprWithBlock (other), /*statements(other.statements),*/ - WithInnerAttrs (other.inner_attrs), start_locus (other.start_locus), - end_locus (other.end_locus) + WithInnerAttrs (other.inner_attrs), label (other.label), + start_locus (other.start_locus), end_locus (other.end_locus) { // guard to protect from null pointer dereference if (other.expr != nullptr) @@ -2211,6 +2240,9 @@ public: return ExprType::Block; } + bool has_label () const { return !label.is_error (); } + LoopLabel &get_label () { return label; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2835,34 +2867,6 @@ protected: } }; -// Loop label expression HIR node used with break and continue expressions -// TODO: inline? -class LoopLabel /*: public Node*/ -{ - Lifetime label; // or type LIFETIME_OR_LABEL - - location_t locus; - - Analysis::NodeMapping mappings; - -public: - std::string as_string () const; - - LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label, - location_t locus) - : label (std::move (loop_label)), locus (locus), mappings (mapping) - {} - - // Returns whether the LoopLabel is in an error state. - bool is_error () const { return label.is_error (); } - - location_t get_locus () const { return locus; } - - Analysis::NodeMapping &get_mappings () { return mappings; } - - Lifetime &get_lifetime () { return label; } -}; - // Base loop expression HIR node - aka LoopExpr class BaseLoopExpr : public ExprWithBlock {