Skip to content

Commit

Permalink
Add hir lowering of closure expressions
Browse files Browse the repository at this point in the history
In the AST we have ClosureExprInner and ClosureExprInnerTyped the first
is the closure expression of the form:

    let closure_inferred  = |i| i + 1;

The second is of the form:

    let closure_annotated = |i: i32| -> i32 { i + 1 };

Both of these can be seguared into a single HIR::ClosureExpr with an
optional return type and parameter types.

Addresses #195
  • Loading branch information
philberty authored and dkm committed Jan 5, 2023
1 parent 8ee64ab commit 33457b9
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 207 deletions.
9 changes: 4 additions & 5 deletions gcc/rust/ast/rust-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2134,8 +2134,6 @@ struct ClosureParam
private:
std::vector<Attribute> outer_attrs;
std::unique_ptr<Pattern> pattern;

// bool has_type_given;
std::unique_ptr<Type> type;
Location locus;

Expand Down Expand Up @@ -2202,19 +2200,19 @@ struct ClosureParam
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }

// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Pattern> &get_pattern ()
{
rust_assert (pattern != nullptr);
return pattern;
}

// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (has_type_given ());
return type;
}

Location get_locus () const { return locus; }
};

// Base closure definition expression AST node - abstract
Expand Down Expand Up @@ -2248,6 +2246,8 @@ class ClosureExpr : public ExprWithoutBlock
{
outer_attrs = std::move (new_attrs);
}

bool get_has_move () const { return has_move; }
};

// Represents a non-type-specified closure expression AST node
Expand Down Expand Up @@ -2307,7 +2307,6 @@ class ClosureExprInner : public ClosureExpr
return closure_inner == nullptr;
}

// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Expr> &get_definition_expr ()
{
rust_assert (closure_inner != nullptr);
Expand Down
6 changes: 2 additions & 4 deletions gcc/rust/backend/rust-compile-block.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class CompileConditionalBlocks : public HIRCompileBase,
// Empty visit for unused Expression HIR nodes.
void visit (HIR::PathInExpression &) override {}
void visit (HIR::QualifiedPathInExpression &) override {}
void visit (HIR::ClosureExprInner &) override {}
void visit (HIR::ClosureExprInnerTyped &) override {}
void visit (HIR::ClosureExpr &) override {}
void visit (HIR::StructExprFieldIdentifier &) override {}
void visit (HIR::StructExprFieldIdentifierValue &) override {}
void visit (HIR::StructExprFieldIndexValue &) override {}
Expand Down Expand Up @@ -146,8 +145,7 @@ class CompileExprWithBlock : public HIRCompileBase,
// Empty visit for unused Expression HIR nodes.
void visit (HIR::PathInExpression &) override {}
void visit (HIR::QualifiedPathInExpression &) override {}
void visit (HIR::ClosureExprInner &) override {}
void visit (HIR::ClosureExprInnerTyped &) override {}
void visit (HIR::ClosureExpr &) override {}
void visit (HIR::StructExprFieldIdentifier &) override {}
void visit (HIR::StructExprFieldIdentifierValue &) override {}
void visit (HIR::StructExprFieldIndexValue &) override {}
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2803,5 +2803,11 @@ CompileExpr::visit (HIR::ArrayIndexExpr &expr)
expr.get_locus ());
}

void
CompileExpr::visit (HIR::ClosureExpr &expr)
{
gcc_unreachable ();
}

} // namespace Compile
} // namespace Rust
3 changes: 1 addition & 2 deletions gcc/rust/backend/rust-compile-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ class CompileExpr : private HIRCompileBase, protected HIR::HIRExpressionVisitor
void visit (HIR::RangeToExpr &expr) override;
void visit (HIR::RangeFullExpr &expr) override;
void visit (HIR::RangeFromToInclExpr &expr) override;
void visit (HIR::ClosureExpr &expr) override;

// TODO
void visit (HIR::ClosureExprInner &) override {}
void visit (HIR::ClosureExprInnerTyped &) override {}
void visit (HIR::ErrorPropagationExpr &) override {}
void visit (HIR::RangeToInclExpr &) override {}
void visit (HIR::ForLoopExpr &) override {}
Expand Down
8 changes: 1 addition & 7 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ PrivacyReporter::visit (HIR::FieldAccessExpr &expr)
}

void
PrivacyReporter::visit (HIR::ClosureExprInner &expr)
PrivacyReporter::visit (HIR::ClosureExpr &expr)
{
// Not handled yet
}
Expand All @@ -442,12 +442,6 @@ PrivacyReporter::visit (HIR::BlockExpr &expr)
last_expr->accept_vis (*this);
}

void
PrivacyReporter::visit (HIR::ClosureExprInnerTyped &expr)
{
// Not handled yet
}

void
PrivacyReporter::visit (HIR::ContinueExpr &expr)
{}
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ types

virtual void visit (HIR::QualifiedPathInExpression &expr);
virtual void visit (HIR::PathInExpression &expr);
virtual void visit (HIR::ClosureExprInnerTyped &);
virtual void visit (HIR::ClosureExprInner &expr);
virtual void visit (HIR::ClosureExpr &expr);
virtual void visit (HIR::StructExprStructFields &);
virtual void visit (HIR::StructExprStruct &);
virtual void visit (HIR::LiteralExpr &expr);
Expand Down
6 changes: 1 addition & 5 deletions gcc/rust/checks/errors/rust-const-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ ConstChecker::visit (FieldAccessExpr &expr)
}

void
ConstChecker::visit (ClosureExprInner &expr)
ConstChecker::visit (ClosureExpr &expr)
{}

void
Expand All @@ -395,10 +395,6 @@ ConstChecker::visit (BlockExpr &expr)
expr.get_final_expr ()->accept_vis (*this);
}

void
ConstChecker::visit (ClosureExprInnerTyped &expr)
{}

void
ConstChecker::visit (ContinueExpr &expr)
{}
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/checks/errors/rust-const-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ class ConstChecker : public HIRFullVisitor
virtual void visit (CallExpr &expr) override;
virtual void visit (MethodCallExpr &expr) override;
virtual void visit (FieldAccessExpr &expr) override;
virtual void visit (ClosureExprInner &expr) override;
virtual void visit (ClosureExpr &expr) override;
virtual void visit (BlockExpr &expr) override;
virtual void visit (ClosureExprInnerTyped &expr) override;
virtual void visit (ContinueExpr &expr) override;
virtual void visit (BreakExpr &expr) override;
virtual void visit (RangeFromToExpr &expr) override;
Expand Down
6 changes: 1 addition & 5 deletions gcc/rust/checks/errors/rust-unsafe-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ UnsafeChecker::visit (FieldAccessExpr &expr)
}

void
UnsafeChecker::visit (ClosureExprInner &expr)
UnsafeChecker::visit (ClosureExpr &expr)
{}

void
Expand All @@ -466,10 +466,6 @@ UnsafeChecker::visit (BlockExpr &expr)
expr.get_final_expr ()->accept_vis (*this);
}

void
UnsafeChecker::visit (ClosureExprInnerTyped &expr)
{}

void
UnsafeChecker::visit (ContinueExpr &expr)
{}
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/checks/errors/rust-unsafe-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ class UnsafeChecker : public HIRFullVisitor
virtual void visit (CallExpr &expr) override;
virtual void visit (MethodCallExpr &expr) override;
virtual void visit (FieldAccessExpr &expr) override;
virtual void visit (ClosureExprInner &expr) override;
virtual void visit (ClosureExpr &expr) override;
virtual void visit (BlockExpr &expr) override;
virtual void visit (ClosureExprInnerTyped &expr) override;
virtual void visit (ContinueExpr &expr) override;
virtual void visit (BreakExpr &expr) override;
virtual void visit (RangeFromToExpr &expr) override;
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/hir/rust-ast-lower-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ class ASTLoweringBase : public AST::ASTVisitor
HIR::Literal lower_literal (const AST::Literal &literal);

HIR::ExternBlock *lower_extern_block (AST::ExternBlock &extern_block);

HIR::ClosureParam lower_closure_param (AST::ClosureParam &param);
};

} // namespace HIR
Expand Down
53 changes: 53 additions & 0 deletions gcc/rust/hir/rust-ast-lower-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -753,5 +753,58 @@ ASTLoweringExpr::visit (AST::RangeFromToInclExpr &expr)
expr.get_locus ());
}

void
ASTLoweringExpr::visit (AST::ClosureExprInner &expr)
{
HIR::Expr *closure_expr
= ASTLoweringExpr::translate (expr.get_definition_expr ().get ());

std::vector<HIR::ClosureParam> closure_params;
for (auto &param : expr.get_params ())
{
HIR::ClosureParam p = lower_closure_param (param);
closure_params.push_back (std::move (p));
}

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

translated
= new HIR::ClosureExpr (mapping, std::move (closure_params),
nullptr /* closure_return_type */,
std::unique_ptr<HIR::Expr> (closure_expr),
expr.get_has_move (), expr.get_outer_attrs (),
expr.get_locus ());
}

void
ASTLoweringExpr::visit (AST::ClosureExprInnerTyped &expr)
{
HIR::Type *closure_return_type = nullptr;
HIR::Expr *closure_expr
= ASTLoweringExpr::translate (expr.get_definition_block ().get ());

std::vector<HIR::ClosureParam> closure_params;
for (auto &param : expr.get_params ())
{
HIR::ClosureParam p = lower_closure_param (param);
closure_params.push_back (std::move (p));
}

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

translated
= new HIR::ClosureExpr (mapping, std::move (closure_params),
std::unique_ptr<HIR::Type> (closure_return_type),
std::unique_ptr<HIR::Expr> (closure_expr),
expr.get_has_move (), expr.get_outer_attrs (),
expr.get_locus ());
}

} // namespace HIR
} // namespace Rust
2 changes: 2 additions & 0 deletions gcc/rust/hir/rust-ast-lower-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class ASTLoweringExpr : public ASTLoweringBase
void visit (AST::RangeToExpr &expr) override;
void visit (AST::RangeFullExpr &expr) override;
void visit (AST::RangeFromToInclExpr &expr) override;
void visit (AST::ClosureExprInner &expr) override;
void visit (AST::ClosureExprInnerTyped &expr) override;

private:
ASTLoweringExpr ();
Expand Down
23 changes: 23 additions & 0 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#include "rust-ast-lower-expr.h"
#include "rust-ast-lower-block.h"
#include "rust-ast-lower-type.h"
#include "rust-ast-lower-pattern.h"
#include "rust-ast-lower-struct-field-expr.h"

namespace Rust {
namespace HIR {
using HIR::ClosureParam;

Visibility
translate_visibility (const AST::Visibility &vis)
Expand Down Expand Up @@ -473,5 +476,25 @@ ASTLowerQualPathInExpression::visit (AST::QualifiedPathInExpression &expr)
expr.get_locus (),
expr.get_outer_attrs ());
}

ClosureParam
ASTLoweringBase::lower_closure_param (AST::ClosureParam &param)
{
HIR::Pattern *param_pattern
= ASTLoweringPattern::translate (param.get_pattern ().get ());

HIR::Type *param_type
= param.has_type_given ()
? ASTLoweringType::translate (param.get_type ().get ())
: nullptr;

return HIR::ClosureParam (std::unique_ptr<HIR::Pattern> (param_pattern),
param.get_locus (),
param.has_type_given ()
? std::unique_ptr<HIR::Type> (param_type)
: nullptr,
param.get_outer_attrs ());
}

} // namespace HIR
} // namespace Rust
6 changes: 2 additions & 4 deletions gcc/rust/hir/rust-hir-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void
Dump::visit (FieldAccessExpr &)
{}
void
Dump::visit (ClosureExprInner &)
Dump::visit (ClosureExpr &)
{}
void
Dump::visit (BlockExpr &block_expr)
Expand All @@ -212,9 +212,7 @@ Dump::visit (BlockExpr &block_expr)
stream << "]";
indent--;
}
void
Dump::visit (ClosureExprInnerTyped &)
{}

void
Dump::visit (ContinueExpr &)
{}
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/hir/rust-hir-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ class Dump : public HIRFullVisitor
virtual void visit (CallExpr &) override;
virtual void visit (MethodCallExpr &) override;
virtual void visit (FieldAccessExpr &) override;
virtual void visit (ClosureExprInner &) override;
virtual void visit (ClosureExpr &) override;
virtual void visit (BlockExpr &) override;
virtual void visit (ClosureExprInnerTyped &) override;
virtual void visit (ContinueExpr &) override;
virtual void visit (BreakExpr &) override;
virtual void visit (RangeFromToExpr &) override;
Expand Down
Loading

0 comments on commit 33457b9

Please sign in to comment.