Skip to content

Commit

Permalink
Merge #1218
Browse files Browse the repository at this point in the history
1218: Lower IfLet expressions r=CohenArthur a=antego

This PR addresses #1177. This change implements the "hir-lowering" part of the plan outlined here #1177 (comment).

Co-authored-by: antego <[email protected]>
  • Loading branch information
bors[bot] and antego authored May 10, 2022
2 parents ba3f981 + 245215b commit 03c21a0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
34 changes: 34 additions & 0 deletions gcc/rust/hir/rust-ast-lower-block.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ class ASTLoweringIfBlock : public ASTLoweringBase
bool terminated;
};

class ASTLoweringIfLetBlock : public ASTLoweringBase
{
using Rust::HIR::ASTLoweringBase::visit;

public:
static HIR::IfLetExpr *translate (AST::IfLetExpr *expr)
{
ASTLoweringIfLetBlock resolver;
expr->accept_vis (resolver);
if (resolver.translated != nullptr)
{
resolver.mappings->insert_hir_expr (
resolver.translated->get_mappings ().get_crate_num (),
resolver.translated->get_mappings ().get_hirid (),
resolver.translated);
}
return resolver.translated;
}

~ASTLoweringIfLetBlock () {}

void visit (AST::IfLetExpr &expr) override;

private:
ASTLoweringIfLetBlock () : ASTLoweringBase (), translated (nullptr) {}

HIR::IfLetExpr *translated;
};

class ASTLoweringExprWithBlock : public ASTLoweringBase
{
using Rust::HIR::ASTLoweringBase::visit;
Expand Down Expand Up @@ -159,6 +188,11 @@ class ASTLoweringExprWithBlock : public ASTLoweringBase
translated = ASTLoweringIfBlock::translate (&expr, &terminated);
}

void visit (AST::IfLetExpr &expr) override
{
translated = ASTLoweringIfLetBlock::translate (&expr);
}

void visit (AST::BlockExpr &expr) override
{
translated = ASTLoweringBlock::translate (&expr, &terminated);
Expand Down
28 changes: 28 additions & 0 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqIf &expr)
expr.get_locus ());
}

void
ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
{
std::vector<std::unique_ptr<HIR::Pattern> > patterns;
for (auto &pattern : expr.get_patterns ())
{
HIR::Pattern *ptrn = ASTLoweringPattern::translate (pattern.get ());
patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
}
HIR::Expr *value_ptr
= ASTLoweringExpr::translate (expr.get_value_expr ().get ());

bool ignored_terminated = false;
HIR::BlockExpr *block
= ASTLoweringBlock::translate (expr.get_if_block ().get (),
&ignored_terminated);

auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

translated = new HIR::IfLetExpr (mapping, std::move (patterns),
std::unique_ptr<HIR::Expr> (value_ptr),
std::unique_ptr<HIR::BlockExpr> (block),
expr.get_locus ());
}

// rust-ast-lower-struct-field-expr.h

void
Expand Down

0 comments on commit 03c21a0

Please sign in to comment.