Skip to content

Commit c1adfed

Browse files
committed
Sema: TypeChecker::computeCaptures() and friends should be static
1 parent 369e31e commit c1adfed

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

lib/Sema/DebuggerTestingTransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class DebuggerTestingTransform : public ASTWalker {
242242

243243
// Captures have to be computed after the closure is type-checked. This
244244
// ensures that the type checker can infer <noescape> for captured values.
245-
TC.computeCaptures(Closure);
245+
TypeChecker::computeCaptures(Closure);
246246

247247
return {false, FinalExpr};
248248
}

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using namespace swift;
3232
namespace {
3333

3434
class FindCapturedVars : public ASTWalker {
35-
TypeChecker &TC;
35+
ASTContext &Context;
3636
SmallVector<CapturedValue, 4> Captures;
3737
llvm::SmallDenseMap<ValueDecl*, unsigned, 4> captureEntryNumber;
3838
SourceLoc GenericParamCaptureLoc;
@@ -44,12 +44,12 @@ class FindCapturedVars : public ASTWalker {
4444
bool NoEscape, ObjC;
4545

4646
public:
47-
FindCapturedVars(TypeChecker &tc,
47+
FindCapturedVars(ASTContext &Context,
4848
SourceLoc CaptureLoc,
4949
DeclContext *CurDC,
5050
bool NoEscape,
5151
bool ObjC)
52-
: TC(tc), CaptureLoc(CaptureLoc), CurDC(CurDC),
52+
: Context(Context), CaptureLoc(CaptureLoc), CurDC(CurDC),
5353
NoEscape(NoEscape), ObjC(ObjC) {}
5454

5555
CaptureInfo getCaptureInfo() const {
@@ -71,7 +71,7 @@ class FindCapturedVars : public ASTWalker {
7171
if (Captures.empty())
7272
result.setCaptures(None);
7373
else
74-
result.setCaptures(TC.Context.AllocateCopy(Captures));
74+
result.setCaptures(Context.AllocateCopy(Captures));
7575

7676
return result;
7777
}
@@ -219,7 +219,7 @@ class FindCapturedVars : public ASTWalker {
219219
// can safely ignore them.
220220
// FIXME(TapExpr): This is probably caused by the scoping
221221
// algorithm's ignorance of TapExpr. We should fix that.
222-
if (D->getBaseName() == D->getASTContext().Id_dollarInterpolation)
222+
if (D->getBaseName() == Context.Id_dollarInterpolation)
223223
return { false, DRE };
224224

225225
// Capture the generic parameters of the decl, unless it's a
@@ -273,14 +273,14 @@ class FindCapturedVars : public ASTWalker {
273273
// This is not supported since nominal types cannot capture values.
274274
if (auto NTD = dyn_cast<NominalTypeDecl>(TmpDC)) {
275275
if (DC->isLocalContext()) {
276-
TC.diagnose(DRE->getLoc(), diag::capture_across_type_decl,
277-
NTD->getDescriptiveKind(),
278-
D->getBaseName().getIdentifier());
276+
Context.Diags.diagnose(DRE->getLoc(), diag::capture_across_type_decl,
277+
NTD->getDescriptiveKind(),
278+
D->getBaseName().getIdentifier());
279279

280-
TC.diagnose(NTD->getLoc(), diag::kind_declared_here,
281-
DescriptiveDeclKind::Type);
280+
NTD->diagnose(diag::kind_declared_here,
281+
DescriptiveDeclKind::Type);
282282

283-
TC.diagnose(D, diag::decl_declared_here, D->getFullName());
283+
D->diagnose(diag::decl_declared_here, D->getFullName());
284284
return { false, DRE };
285285
}
286286
}
@@ -328,7 +328,7 @@ class FindCapturedVars : public ASTWalker {
328328
}
329329

330330
void propagateCaptures(AnyFunctionRef innerClosure, SourceLoc captureLoc) {
331-
TC.computeCaptures(innerClosure);
331+
TypeChecker::computeCaptures(innerClosure);
332332

333333
auto &captureInfo = innerClosure.getCaptureInfo();
334334

@@ -597,7 +597,8 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
597597
if (!AFR.getBody())
598598
return;
599599

600-
FindCapturedVars finder(*this,
600+
auto &Context = AFR.getAsDeclContext()->getASTContext();
601+
FindCapturedVars finder(Context,
601602
AFR.getLoc(),
602603
AFR.getAsDeclContext(),
603604
AFR.isKnownNoEscape(),
@@ -624,27 +625,29 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
624625
if (AFD && finder.getGenericParamCaptureLoc().isValid()) {
625626
if (auto Clas = AFD->getParent()->getSelfClassDecl()) {
626627
if (Clas->usesObjCGenericsModel()) {
627-
diagnose(AFD->getLoc(),
628-
diag::objc_generic_extension_using_type_parameter);
628+
AFD->diagnose(diag::objc_generic_extension_using_type_parameter);
629629

630630
// If it's possible, suggest adding @objc.
631631
Optional<ForeignErrorConvention> errorConvention;
632632
if (!AFD->isObjC() &&
633633
isRepresentableInObjC(AFD, ObjCReason::MemberOfObjCMembersClass,
634634
errorConvention)) {
635-
diagnose(AFD->getLoc(),
635+
AFD->diagnose(
636636
diag::objc_generic_extension_using_type_parameter_try_objc)
637637
.fixItInsert(AFD->getAttributeInsertionLoc(false), "@objc ");
638638
}
639639

640-
diagnose(finder.getGenericParamCaptureLoc(),
641-
diag::objc_generic_extension_using_type_parameter_here);
640+
Context.Diags.diagnose(
641+
finder.getGenericParamCaptureLoc(),
642+
diag::objc_generic_extension_using_type_parameter_here);
642643
}
643644
}
644645
}
645646
}
646647

647648
void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
649+
auto &ctx = typeDecl->getASTContext();
650+
648651
for (auto member : typeDecl->getMembers()) {
649652
// Ignore everything other than PBDs.
650653
auto *PBD = dyn_cast<PatternBindingDecl>(member);
@@ -659,16 +662,16 @@ void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
659662
if (init == nullptr)
660663
continue;
661664

662-
FindCapturedVars finder(*this,
665+
FindCapturedVars finder(ctx,
663666
init->getLoc(),
664667
PBD->getInitContext(i),
665668
/*NoEscape=*/false,
666669
/*ObjC=*/false);
667670
init->walk(finder);
668671

669672
if (finder.getDynamicSelfCaptureLoc().isValid()) {
670-
diagnose(finder.getDynamicSelfCaptureLoc(),
671-
diag::dynamic_self_stored_property_init);
673+
ctx.Diags.diagnose(finder.getDynamicSelfCaptureLoc(),
674+
diag::dynamic_self_stored_property_init);
672675
}
673676

674677
auto captures = finder.getCaptureInfo();

lib/Sema/TypeChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,12 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)
358358

359359
// Compute captures for functions and closures we visited.
360360
for (auto *closure : TC.ClosuresWithUncomputedCaptures) {
361-
TC.computeCaptures(closure);
361+
TypeChecker::computeCaptures(closure);
362362
}
363363
TC.ClosuresWithUncomputedCaptures.clear();
364364

365365
for (AbstractFunctionDecl *FD : reversed(TC.definedFunctions)) {
366-
TC.computeCaptures(FD);
366+
TypeChecker::computeCaptures(FD);
367367
}
368368

369369
// Check error-handling correctness for all the functions defined in

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,10 +1430,10 @@ class TypeChecker final : public LazyResolver {
14301430
bool typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt);
14311431

14321432
/// Compute the set of captures for the given function or closure.
1433-
void computeCaptures(AnyFunctionRef AFR);
1433+
static void computeCaptures(AnyFunctionRef AFR);
14341434

14351435
/// Check for invalid captures from stored property initializers.
1436-
void checkPatternBindingCaptures(NominalTypeDecl *typeDecl);
1436+
static void checkPatternBindingCaptures(NominalTypeDecl *typeDecl);
14371437

14381438
/// Change the context of closures in the given initializer
14391439
/// expression to the given context.

0 commit comments

Comments
 (0)