Skip to content

Commit

Permalink
Refactor expression hir lowering into cc file
Browse files Browse the repository at this point in the history
Add hir lowering of closure expressions

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 CohenArthur committed Jan 31, 2023
1 parent ae35bc3 commit 843c2ea
Show file tree
Hide file tree
Showing 24 changed files with 1,009 additions and 906 deletions.
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ GRS_OBJS = \
rust/rust-ast-lower-base.o \
rust/rust-ast-lower-pattern.o \
rust/rust-ast-lower-item.o \
rust/rust-ast-lower-expr.o \
rust/rust-early-name-resolver.o \
rust/rust-name-resolver.o \
rust/rust-ast-resolve.o \
Expand Down
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
Loading

0 comments on commit 843c2ea

Please sign in to comment.