Skip to content

Commit

Permalink
WIP fixup desugaring
Browse files Browse the repository at this point in the history
  • Loading branch information
dkm committed Sep 21, 2024
1 parent 67028a9 commit 95e61f1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/hir/rust-ast-lower-block.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ASTLoweringIfLetBlock : public ASTLoweringBase
private:
ASTLoweringIfLetBlock () : ASTLoweringBase (), translated (nullptr) {}

void desugar_iflet (AST::IfLetExpr &, HIR::Expr **,
void desugar_iflet (AST::IfLetExpr &, HIR::Expr **, HIR::Expr *,
std::vector<HIR::MatchCase> &);

HIR::MatchExpr *translated;
Expand Down
75 changes: 56 additions & 19 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqElse &expr)
void
ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr,
HIR::Expr **branch_value,
HIR::Expr *kase_else_expr,
std::vector<HIR::MatchCase> &match_arms)
{
HIR::Expr *kase_expr;
Expand Down Expand Up @@ -245,6 +246,30 @@ ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr,
HIR::MatchCase kase (std::move (mapping), std::move (arm),
std::unique_ptr<HIR::Expr> (kase_expr));
match_arms.push_back (std::move (kase));

// The default match arm
std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard;
Analysis::NodeMapping mapping_default (crate_num, expr.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

std::unique_ptr<HIR::WildcardPattern> wc
= std::unique_ptr<HIR::WildcardPattern> (
new HIR::WildcardPattern (mapping_default, expr.get_locus ()));

match_arm_patterns_wildcard.push_back (std::move (wc));

HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard),
expr.get_locus (), nullptr, {});




HIR::MatchCase kase_else (std::move (mapping_default),
std::move (arm_default),
std::unique_ptr<HIR::Expr> (kase_else_expr));
match_arms.push_back (std::move (kase_else));

}

void
Expand All @@ -265,13 +290,24 @@ ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
HIR::Expr *branch_value;

std::vector<HIR::MatchCase> match_arms;
desugar_iflet (expr, &branch_value, match_arms);

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);

// new HIR::BlockExpr (mapping, std::move (block_stmts),
// std::unique_ptr<HIR::ExprWithoutBlock> (tail_expr),
// tail_reachable, expr.get_inner_attrs (),
// expr.get_outer_attrs (), label,
// expr.get_start_locus (), expr.get_end_locus ());

HIR::TupleExpr * unit = new HIR::TupleExpr(mapping, {}, {}, {}, expr.get_locus());
// HIR::BlockExpr empty_block = new HIR::BlockExpr (mapping, {}, nullptr,
// false, {}, LoopLabel::error()/* label */
// {}, expr.get_locus(), /* FIXME this is not OK */expr.get_locus());

desugar_iflet (expr, &branch_value, unit, match_arms);

translated
= new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
std::move (match_arms), {}, {}, expr.get_locus ());
Expand All @@ -297,34 +333,35 @@ ASTLoweringIfLetBlock::visit (AST::IfLetExprConseqElse &expr)
HIR::Expr *branch_value;
std::vector<HIR::MatchCase> match_arms;

desugar_iflet (expr, &branch_value, match_arms);
HIR::Expr *kase_else_expr
= ASTLoweringExpr::translate (expr.get_else_block ());

desugar_iflet (expr, &branch_value, kase_else_expr, match_arms);

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);

std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard;
Analysis::NodeMapping mapping_default (crate_num, expr.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);
// std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard;
// Analysis::NodeMapping mapping_default (crate_num, expr.get_node_id (),
// mappings.get_next_hir_id (crate_num),
// UNKNOWN_LOCAL_DEFID);

std::unique_ptr<HIR::WildcardPattern> wc
= std::unique_ptr<HIR::WildcardPattern> (
new HIR::WildcardPattern (mapping_default, expr.get_locus ()));
// std::unique_ptr<HIR::WildcardPattern> wc
// = std::unique_ptr<HIR::WildcardPattern> (
// new HIR::WildcardPattern (mapping_default, expr.get_locus ()));

match_arm_patterns_wildcard.push_back (std::move (wc));
// match_arm_patterns_wildcard.push_back (std::move (wc));

HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard),
expr.get_locus (), nullptr, {});
// HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard),
// expr.get_locus (), nullptr, {});

HIR::Expr *kase_else_expr
= ASTLoweringExpr::translate (expr.get_else_block ());

HIR::MatchCase kase_else (std::move (mapping_default),
std::move (arm_default),
std::unique_ptr<HIR::Expr> (kase_else_expr));
match_arms.push_back (std::move (kase_else));
// HIR::MatchCase kase_else (std::move (mapping_default),
// std::move (arm_default),
// std::unique_ptr<HIR::Expr> (kase_else_expr));
// match_arms.push_back (std::move (kase_else));

translated
= new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace HIR {
// TODO: inline?
class LoopLabel /*: public Node*/
{
Lifetime label; // or type LIFETIME_OR_LABEL
Lifetime label; // of type LIFETIME_OR_LABEL

location_t locus;

Expand Down

0 comments on commit 95e61f1

Please sign in to comment.