Skip to content

Commit

Permalink
Lower IfLet expressions.
Browse files Browse the repository at this point in the history
Addresses Rust-GCC#1177.
  • Loading branch information
antego committed May 9, 2022
1 parent af031b0 commit 245215b
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 245215b

Please sign in to comment.