Skip to content

Commit 33457b9

Browse files
philbertydkm
authored andcommitted
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
1 parent 8ee64ab commit 33457b9

23 files changed

+206
-207
lines changed

gcc/rust/ast/rust-expr.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -2134,8 +2134,6 @@ struct ClosureParam
21342134
private:
21352135
std::vector<Attribute> outer_attrs;
21362136
std::unique_ptr<Pattern> pattern;
2137-
2138-
// bool has_type_given;
21392137
std::unique_ptr<Type> type;
21402138
Location locus;
21412139

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

2205-
// TODO: is this better? Or is a "vis_block" better?
22062203
std::unique_ptr<Pattern> &get_pattern ()
22072204
{
22082205
rust_assert (pattern != nullptr);
22092206
return pattern;
22102207
}
22112208

2212-
// TODO: is this better? Or is a "vis_block" better?
22132209
std::unique_ptr<Type> &get_type ()
22142210
{
22152211
rust_assert (has_type_given ());
22162212
return type;
22172213
}
2214+
2215+
Location get_locus () const { return locus; }
22182216
};
22192217

22202218
// Base closure definition expression AST node - abstract
@@ -2248,6 +2246,8 @@ class ClosureExpr : public ExprWithoutBlock
22482246
{
22492247
outer_attrs = std::move (new_attrs);
22502248
}
2249+
2250+
bool get_has_move () const { return has_move; }
22512251
};
22522252

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

2310-
// TODO: is this better? Or is a "vis_block" better?
23112310
std::unique_ptr<Expr> &get_definition_expr ()
23122311
{
23132312
rust_assert (closure_inner != nullptr);

gcc/rust/backend/rust-compile-block.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class CompileConditionalBlocks : public HIRCompileBase,
5757
// Empty visit for unused Expression HIR nodes.
5858
void visit (HIR::PathInExpression &) override {}
5959
void visit (HIR::QualifiedPathInExpression &) override {}
60-
void visit (HIR::ClosureExprInner &) override {}
61-
void visit (HIR::ClosureExprInnerTyped &) override {}
60+
void visit (HIR::ClosureExpr &) override {}
6261
void visit (HIR::StructExprFieldIdentifier &) override {}
6362
void visit (HIR::StructExprFieldIdentifierValue &) override {}
6463
void visit (HIR::StructExprFieldIndexValue &) override {}
@@ -146,8 +145,7 @@ class CompileExprWithBlock : public HIRCompileBase,
146145
// Empty visit for unused Expression HIR nodes.
147146
void visit (HIR::PathInExpression &) override {}
148147
void visit (HIR::QualifiedPathInExpression &) override {}
149-
void visit (HIR::ClosureExprInner &) override {}
150-
void visit (HIR::ClosureExprInnerTyped &) override {}
148+
void visit (HIR::ClosureExpr &) override {}
151149
void visit (HIR::StructExprFieldIdentifier &) override {}
152150
void visit (HIR::StructExprFieldIdentifierValue &) override {}
153151
void visit (HIR::StructExprFieldIndexValue &) override {}

gcc/rust/backend/rust-compile-expr.cc

+6
Original file line numberDiff line numberDiff line change
@@ -2803,5 +2803,11 @@ CompileExpr::visit (HIR::ArrayIndexExpr &expr)
28032803
expr.get_locus ());
28042804
}
28052805

2806+
void
2807+
CompileExpr::visit (HIR::ClosureExpr &expr)
2808+
{
2809+
gcc_unreachable ();
2810+
}
2811+
28062812
} // namespace Compile
28072813
} // namespace Rust

gcc/rust/backend/rust-compile-expr.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ class CompileExpr : private HIRCompileBase, protected HIR::HIRExpressionVisitor
6767
void visit (HIR::RangeToExpr &expr) override;
6868
void visit (HIR::RangeFullExpr &expr) override;
6969
void visit (HIR::RangeFromToInclExpr &expr) override;
70+
void visit (HIR::ClosureExpr &expr) override;
7071

7172
// TODO
72-
void visit (HIR::ClosureExprInner &) override {}
73-
void visit (HIR::ClosureExprInnerTyped &) override {}
7473
void visit (HIR::ErrorPropagationExpr &) override {}
7574
void visit (HIR::RangeToInclExpr &) override {}
7675
void visit (HIR::ForLoopExpr &) override {}

gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ PrivacyReporter::visit (HIR::FieldAccessExpr &expr)
426426
}
427427

428428
void
429-
PrivacyReporter::visit (HIR::ClosureExprInner &expr)
429+
PrivacyReporter::visit (HIR::ClosureExpr &expr)
430430
{
431431
// Not handled yet
432432
}
@@ -442,12 +442,6 @@ PrivacyReporter::visit (HIR::BlockExpr &expr)
442442
last_expr->accept_vis (*this);
443443
}
444444

445-
void
446-
PrivacyReporter::visit (HIR::ClosureExprInnerTyped &expr)
447-
{
448-
// Not handled yet
449-
}
450-
451445
void
452446
PrivacyReporter::visit (HIR::ContinueExpr &expr)
453447
{}

gcc/rust/checks/errors/privacy/rust-privacy-reporter.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ types
8383

8484
virtual void visit (HIR::QualifiedPathInExpression &expr);
8585
virtual void visit (HIR::PathInExpression &expr);
86-
virtual void visit (HIR::ClosureExprInnerTyped &);
87-
virtual void visit (HIR::ClosureExprInner &expr);
86+
virtual void visit (HIR::ClosureExpr &expr);
8887
virtual void visit (HIR::StructExprStructFields &);
8988
virtual void visit (HIR::StructExprStruct &);
9089
virtual void visit (HIR::LiteralExpr &expr);

gcc/rust/checks/errors/rust-const-checker.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ ConstChecker::visit (FieldAccessExpr &expr)
382382
}
383383

384384
void
385-
ConstChecker::visit (ClosureExprInner &expr)
385+
ConstChecker::visit (ClosureExpr &expr)
386386
{}
387387

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

398-
void
399-
ConstChecker::visit (ClosureExprInnerTyped &expr)
400-
{}
401-
402398
void
403399
ConstChecker::visit (ContinueExpr &expr)
404400
{}

gcc/rust/checks/errors/rust-const-checker.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,8 @@ class ConstChecker : public HIRFullVisitor
111111
virtual void visit (CallExpr &expr) override;
112112
virtual void visit (MethodCallExpr &expr) override;
113113
virtual void visit (FieldAccessExpr &expr) override;
114-
virtual void visit (ClosureExprInner &expr) override;
114+
virtual void visit (ClosureExpr &expr) override;
115115
virtual void visit (BlockExpr &expr) override;
116-
virtual void visit (ClosureExprInnerTyped &expr) override;
117116
virtual void visit (ContinueExpr &expr) override;
118117
virtual void visit (BreakExpr &expr) override;
119118
virtual void visit (RangeFromToExpr &expr) override;

gcc/rust/checks/errors/rust-unsafe-checker.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ UnsafeChecker::visit (FieldAccessExpr &expr)
453453
}
454454

455455
void
456-
UnsafeChecker::visit (ClosureExprInner &expr)
456+
UnsafeChecker::visit (ClosureExpr &expr)
457457
{}
458458

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

469-
void
470-
UnsafeChecker::visit (ClosureExprInnerTyped &expr)
471-
{}
472-
473469
void
474470
UnsafeChecker::visit (ContinueExpr &expr)
475471
{}

gcc/rust/checks/errors/rust-unsafe-checker.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ class UnsafeChecker : public HIRFullVisitor
8888
virtual void visit (CallExpr &expr) override;
8989
virtual void visit (MethodCallExpr &expr) override;
9090
virtual void visit (FieldAccessExpr &expr) override;
91-
virtual void visit (ClosureExprInner &expr) override;
91+
virtual void visit (ClosureExpr &expr) override;
9292
virtual void visit (BlockExpr &expr) override;
93-
virtual void visit (ClosureExprInnerTyped &expr) override;
9493
virtual void visit (ContinueExpr &expr) override;
9594
virtual void visit (BreakExpr &expr) override;
9695
virtual void visit (RangeFromToExpr &expr) override;

gcc/rust/hir/rust-ast-lower-base.h

+2
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ class ASTLoweringBase : public AST::ASTVisitor
313313
HIR::Literal lower_literal (const AST::Literal &literal);
314314

315315
HIR::ExternBlock *lower_extern_block (AST::ExternBlock &extern_block);
316+
317+
HIR::ClosureParam lower_closure_param (AST::ClosureParam &param);
316318
};
317319

318320
} // namespace HIR

gcc/rust/hir/rust-ast-lower-expr.cc

+53
Original file line numberDiff line numberDiff line change
@@ -753,5 +753,58 @@ ASTLoweringExpr::visit (AST::RangeFromToInclExpr &expr)
753753
expr.get_locus ());
754754
}
755755

756+
void
757+
ASTLoweringExpr::visit (AST::ClosureExprInner &expr)
758+
{
759+
HIR::Expr *closure_expr
760+
= ASTLoweringExpr::translate (expr.get_definition_expr ().get ());
761+
762+
std::vector<HIR::ClosureParam> closure_params;
763+
for (auto &param : expr.get_params ())
764+
{
765+
HIR::ClosureParam p = lower_closure_param (param);
766+
closure_params.push_back (std::move (p));
767+
}
768+
769+
auto crate_num = mappings->get_current_crate ();
770+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
771+
mappings->get_next_hir_id (crate_num),
772+
mappings->get_next_localdef_id (crate_num));
773+
774+
translated
775+
= new HIR::ClosureExpr (mapping, std::move (closure_params),
776+
nullptr /* closure_return_type */,
777+
std::unique_ptr<HIR::Expr> (closure_expr),
778+
expr.get_has_move (), expr.get_outer_attrs (),
779+
expr.get_locus ());
780+
}
781+
782+
void
783+
ASTLoweringExpr::visit (AST::ClosureExprInnerTyped &expr)
784+
{
785+
HIR::Type *closure_return_type = nullptr;
786+
HIR::Expr *closure_expr
787+
= ASTLoweringExpr::translate (expr.get_definition_block ().get ());
788+
789+
std::vector<HIR::ClosureParam> closure_params;
790+
for (auto &param : expr.get_params ())
791+
{
792+
HIR::ClosureParam p = lower_closure_param (param);
793+
closure_params.push_back (std::move (p));
794+
}
795+
796+
auto crate_num = mappings->get_current_crate ();
797+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
798+
mappings->get_next_hir_id (crate_num),
799+
mappings->get_next_localdef_id (crate_num));
800+
801+
translated
802+
= new HIR::ClosureExpr (mapping, std::move (closure_params),
803+
std::unique_ptr<HIR::Type> (closure_return_type),
804+
std::unique_ptr<HIR::Expr> (closure_expr),
805+
expr.get_has_move (), expr.get_outer_attrs (),
806+
expr.get_locus ());
807+
}
808+
756809
} // namespace HIR
757810
} // namespace Rust

gcc/rust/hir/rust-ast-lower-expr.h

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class ASTLoweringExpr : public ASTLoweringBase
116116
void visit (AST::RangeToExpr &expr) override;
117117
void visit (AST::RangeFullExpr &expr) override;
118118
void visit (AST::RangeFromToInclExpr &expr) override;
119+
void visit (AST::ClosureExprInner &expr) override;
120+
void visit (AST::ClosureExprInnerTyped &expr) override;
119121

120122
private:
121123
ASTLoweringExpr ();

gcc/rust/hir/rust-ast-lower.cc

+23
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
#include "rust-ast-lower-expr.h"
2323
#include "rust-ast-lower-block.h"
2424
#include "rust-ast-lower-type.h"
25+
#include "rust-ast-lower-pattern.h"
26+
#include "rust-ast-lower-struct-field-expr.h"
2527

2628
namespace Rust {
2729
namespace HIR {
30+
using HIR::ClosureParam;
2831

2932
Visibility
3033
translate_visibility (const AST::Visibility &vis)
@@ -473,5 +476,25 @@ ASTLowerQualPathInExpression::visit (AST::QualifiedPathInExpression &expr)
473476
expr.get_locus (),
474477
expr.get_outer_attrs ());
475478
}
479+
480+
ClosureParam
481+
ASTLoweringBase::lower_closure_param (AST::ClosureParam &param)
482+
{
483+
HIR::Pattern *param_pattern
484+
= ASTLoweringPattern::translate (param.get_pattern ().get ());
485+
486+
HIR::Type *param_type
487+
= param.has_type_given ()
488+
? ASTLoweringType::translate (param.get_type ().get ())
489+
: nullptr;
490+
491+
return HIR::ClosureParam (std::unique_ptr<HIR::Pattern> (param_pattern),
492+
param.get_locus (),
493+
param.has_type_given ()
494+
? std::unique_ptr<HIR::Type> (param_type)
495+
: nullptr,
496+
param.get_outer_attrs ());
497+
}
498+
476499
} // namespace HIR
477500
} // namespace Rust

gcc/rust/hir/rust-hir-dump.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void
197197
Dump::visit (FieldAccessExpr &)
198198
{}
199199
void
200-
Dump::visit (ClosureExprInner &)
200+
Dump::visit (ClosureExpr &)
201201
{}
202202
void
203203
Dump::visit (BlockExpr &block_expr)
@@ -212,9 +212,7 @@ Dump::visit (BlockExpr &block_expr)
212212
stream << "]";
213213
indent--;
214214
}
215-
void
216-
Dump::visit (ClosureExprInnerTyped &)
217-
{}
215+
218216
void
219217
Dump::visit (ContinueExpr &)
220218
{}

gcc/rust/hir/rust-hir-dump.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ class Dump : public HIRFullVisitor
7878
virtual void visit (CallExpr &) override;
7979
virtual void visit (MethodCallExpr &) override;
8080
virtual void visit (FieldAccessExpr &) override;
81-
virtual void visit (ClosureExprInner &) override;
81+
virtual void visit (ClosureExpr &) override;
8282
virtual void visit (BlockExpr &) override;
83-
virtual void visit (ClosureExprInnerTyped &) override;
8483
virtual void visit (ContinueExpr &) override;
8584
virtual void visit (BreakExpr &) override;
8685
virtual void visit (RangeFromToExpr &) override;

0 commit comments

Comments
 (0)