Skip to content

Commit 4bf9025

Browse files
authored
Merge pull request #28152 from hamishknight/decontextualize
Make TypeChecker's Context and Diags fields private
2 parents 6792e0b + 4ef137b commit 4bf9025

File tree

6 files changed

+99
-42
lines changed

6 files changed

+99
-42
lines changed

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5125,9 +5125,8 @@ Expr *ExprRewriter::coerceImplicitlyUnwrappedOptionalToValue(Expr *expr, Type ob
51255125
if (optTy->is<LValueType>())
51265126
objTy = LValueType::get(objTy);
51275127

5128-
expr = new (cs.getTypeChecker().Context) ForceValueExpr(expr,
5129-
expr->getEndLoc(),
5130-
/* forcedIUO=*/ true);
5128+
expr = new (cs.getASTContext()) ForceValueExpr(expr, expr->getEndLoc(),
5129+
/* forcedIUO=*/ true);
51315130
cs.setType(expr, objTy);
51325131
expr->setImplicit();
51335132
return expr;

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ namespace {
909909
}
910910

911911
class PreCheckExpression : public ASTWalker {
912-
TypeChecker &TC;
912+
ASTContext &Ctx;
913913
DeclContext *DC;
914914

915915
Expr *ParentExpr;
@@ -1053,14 +1053,17 @@ namespace {
10531053
}
10541054

10551055
public:
1056-
PreCheckExpression(TypeChecker &tc, DeclContext *dc, Expr *parent)
1057-
: TC(tc), DC(dc), ParentExpr(parent) {}
1056+
PreCheckExpression(DeclContext *dc, Expr *parent)
1057+
: Ctx(dc->getASTContext()), DC(dc), ParentExpr(parent) {}
10581058

1059-
ASTContext &getASTContext() const { return TC.Context; }
1059+
ASTContext &getASTContext() const { return Ctx; }
10601060

10611061
bool walkToClosureExprPre(ClosureExpr *expr);
10621062

10631063
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
1064+
// FIXME: Remove TypeChecker dependencies below.
1065+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
1066+
10641067
// If this is a call, record the argument expression.
10651068
if (auto call = dyn_cast<ApplyExpr>(expr)) {
10661069
if (!isa<SelfApplyExpr>(expr)) {
@@ -1994,7 +1997,7 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19941997
return nullptr;
19951998

19961999
// Don't bother to convert deprecated selector syntax.
1997-
if (auto selectorTy = TC.Context.getSelectorType()) {
2000+
if (auto selectorTy = getASTContext().getSelectorType()) {
19982001
if (type->isEqual(selectorTy))
19992002
return nullptr;
20002003
}
@@ -2011,7 +2014,7 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
20112014
/// Pre-check the expression, validating any types that occur in the
20122015
/// expression and folding sequence expressions.
20132016
bool TypeChecker::preCheckExpression(Expr *&expr, DeclContext *dc) {
2014-
PreCheckExpression preCheck(*this, dc, expr);
2017+
PreCheckExpression preCheck(dc, expr);
20152018
// Perform the pre-check.
20162019
if (auto result = expr->walk(preCheck)) {
20172020
expr = result;
@@ -2444,7 +2447,7 @@ void TypeChecker::getPossibleTypesOfExpressionWithoutApplying(
24442447
static FunctionType *
24452448
getTypeOfCompletionOperatorImpl(TypeChecker &TC, DeclContext *DC, Expr *expr,
24462449
ConcreteDeclRef &referencedDecl) {
2447-
ASTContext &Context = TC.Context;
2450+
auto &Context = DC->getASTContext();
24482451

24492452
FrontendStatsTracer StatsTracer(Context.Stats,
24502453
"typecheck-completion-operator", expr);

lib/Sema/TypeCheckDecl.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,11 +2274,11 @@ static void checkDynamicSelfType(ValueDecl *decl, Type type) {
22742274
namespace {
22752275
class DeclChecker : public DeclVisitor<DeclChecker> {
22762276
public:
2277-
TypeChecker &TC;
2277+
ASTContext &Ctx;
22782278

2279-
explicit DeclChecker(TypeChecker &TC) : TC(TC) {}
2279+
explicit DeclChecker(ASTContext &ctx) : Ctx(ctx) {}
22802280

2281-
ASTContext &getASTContext() const { return TC.Context; }
2281+
ASTContext &getASTContext() const { return Ctx; }
22822282

22832283
void visit(Decl *decl) {
22842284
if (getASTContext().Stats)
@@ -2585,6 +2585,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25852585

25862586
checkAccessControl(PBD);
25872587

2588+
// FIXME: Remove TypeChecker dependency.
2589+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
2590+
25882591
// If the initializers in the PBD aren't checked yet, do so now.
25892592
for (auto i : range(PBD->getNumPatternEntries())) {
25902593
if (!PBD->isInitialized(i))
@@ -2657,6 +2660,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
26572660
(void) SD->getImplInfo();
26582661

26592662
TypeChecker::checkParameterAttributes(SD->getIndices());
2663+
2664+
// FIXME: Remove TypeChecker dependency.
2665+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
26602666
checkDefaultArguments(TC, SD->getIndices());
26612667

26622668
if (SD->getDeclContext()->getSelfClassDecl()) {
@@ -3213,6 +3219,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32133219

32143220

32153221
bool shouldSkipBodyTypechecking(const AbstractFunctionDecl *AFD) {
3222+
// FIXME: Remove TypeChecker dependency.
3223+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
3224+
32163225
// Make sure we're in the mode that's skipping function bodies.
32173226
if (!TC.canSkipNonInlinableBodies())
32183227
return false;
@@ -3256,6 +3265,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32563265
}
32573266
}
32583267

3268+
// FIXME: Remove TypeChecker dependencies below.
3269+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
32593270
if (requiresDefinition(FD) && !FD->hasBody()) {
32603271
// Complain if we should have a body.
32613272
FD->diagnose(diag::func_decl_without_brace);
@@ -3333,6 +3344,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
33333344

33343345
if (auto *PL = EED->getParameterList()) {
33353346
TypeChecker::checkParameterAttributes(PL);
3347+
3348+
// FIXME: Remove TypeChecker dependency.
3349+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
33363350
checkDefaultArguments(TC, PL);
33373351
}
33383352

@@ -3583,6 +3597,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
35833597

35843598
checkAccessControl(CD);
35853599

3600+
// FIXME: Remove TypeChecker dependencies below.
3601+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
35863602
if (requiresDefinition(CD) && !CD->hasBody()) {
35873603
// Complain if we should have a body.
35883604
CD->diagnose(diag::missing_initializer_def);
@@ -3607,6 +3623,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
36073623
} else if (shouldSkipBodyTypechecking(DD)) {
36083624
DD->setBodySkipped(DD->getBodySourceRange());
36093625
} else {
3626+
// FIXME: Remove TypeChecker dependency.
3627+
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
36103628
TC.definedFunctions.push_back(DD);
36113629
}
36123630
}
@@ -3658,7 +3676,7 @@ bool TypeChecker::isAvailabilitySafeForConformance(
36583676
}
36593677

36603678
void TypeChecker::typeCheckDecl(Decl *D) {
3661-
DeclChecker(*this).visit(D);
3679+
DeclChecker(D->getASTContext()).visit(D);
36623680
}
36633681

36643682
// Returns 'nullptr' if this is the setter's 'newValue' parameter;

lib/Sema/TypeCheckREPL.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,29 @@ using namespace swift;
3131

3232
namespace {
3333
struct REPLContext {
34-
TypeChecker &TC;
3534
ASTContext &Context;
3635
SourceFile &SF;
3736
SmallVector<ValueDecl *, 4> PrintDecls;
3837
SmallVector<ValueDecl *, 4> DebugPrintlnDecls;
3938

40-
REPLContext(TypeChecker &TC, SourceFile &SF)
41-
: TC(TC), Context(TC.Context), SF(SF) {}
39+
REPLContext(SourceFile &SF) : Context(SF.getASTContext()), SF(SF) {}
4240

4341
bool requirePrintDecls() {
4442
if (!PrintDecls.empty() && !DebugPrintlnDecls.empty())
4543
return false;
4644

45+
auto *stdlib = TypeChecker::getStdlibModule(&SF);
4746
{
4847
Identifier Id(Context.getIdentifier("_replPrintLiteralString"));
49-
auto lookup = TypeChecker::lookupUnqualified(TC.getStdlibModule(&SF), Id,
50-
SourceLoc());
48+
auto lookup = TypeChecker::lookupUnqualified(stdlib, Id, SourceLoc());
5149
if (!lookup)
5250
return true;
5351
for (auto result : lookup)
5452
PrintDecls.push_back(result.getValueDecl());
5553
}
5654
{
5755
Identifier Id(Context.getIdentifier("_replDebugPrintln"));
58-
auto lookup = TypeChecker::lookupUnqualified(TC.getStdlibModule(&SF), Id,
59-
SourceLoc());
56+
auto lookup = TypeChecker::lookupUnqualified(stdlib, Id, SourceLoc());
6057
if (!lookup)
6158
return true;
6259
for (auto result : lookup)
@@ -191,8 +188,8 @@ namespace {
191188
unsigned NextResponseVariableIndex = 0;
192189

193190
public:
194-
REPLChecker(TypeChecker &TC, SourceFile &SF, TopLevelContext &TLC)
195-
: REPLContext(TC, SF), TLC(TLC) {}
191+
REPLChecker(SourceFile &SF, TopLevelContext &TLC)
192+
: REPLContext(SF), TLC(TLC) {}
196193

197194
void processREPLTopLevelExpr(Expr *E);
198195
void processREPLTopLevelPatternBinding(PatternBindingDecl *PBD);
@@ -257,6 +254,9 @@ void REPLChecker::generatePrintOfExpression(StringRef NameStr, Expr *E) {
257254
// Typecheck the function.
258255
BraceStmt *Body = builder.createBodyStmt(Loc, EndLoc);
259256
CE->setBody(Body, false);
257+
258+
// FIXME: Remove TypeChecker dependency.
259+
auto &TC = *Context.getLegacyGlobalTypeChecker();
260260
TC.typeCheckClosureBody(CE);
261261
TC.ClosuresWithUncomputedCaptures.push_back(CE);
262262

@@ -437,7 +437,7 @@ void TypeChecker::processREPLTopLevel(SourceFile &SF, TopLevelContext &TLC,
437437
std::vector<Decl *> NewDecls(SF.Decls.begin()+FirstDecl, SF.Decls.end());
438438
SF.Decls.resize(FirstDecl);
439439

440-
REPLChecker RC(*this, SF, TLC);
440+
REPLChecker RC(SF, TLC);
441441

442442
// Loop over each of the new decls, processing them, adding them back to
443443
// the Decls list.

0 commit comments

Comments
 (0)