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 7, 2022
1 parent af031b0 commit e125119
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
38 changes: 38 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,39 @@ class ASTLoweringIfBlock : public ASTLoweringBase
bool terminated;
};

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

public:
static HIR::IfLetExpr *translate (AST::IfLetExpr *expr, bool *terminated)
{
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);
}
*terminated = resolver.terminated;
return resolver.translated;
}

~ASTLoweringIfLetBlock () {}

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

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

HIR::IfLetExpr *translated;
bool terminated;
};

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

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

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)
{
bool ignored_terminated = false;
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 ());

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 e125119

Please sign in to comment.