Skip to content

Make TypeChecker's Context and Diags fields private #28152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5125,9 +5125,8 @@ Expr *ExprRewriter::coerceImplicitlyUnwrappedOptionalToValue(Expr *expr, Type ob
if (optTy->is<LValueType>())
objTy = LValueType::get(objTy);

expr = new (cs.getTypeChecker().Context) ForceValueExpr(expr,
expr->getEndLoc(),
/* forcedIUO=*/ true);
expr = new (cs.getASTContext()) ForceValueExpr(expr, expr->getEndLoc(),
/* forcedIUO=*/ true);
cs.setType(expr, objTy);
expr->setImplicit();
return expr;
Expand Down
17 changes: 10 additions & 7 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ namespace {
}

class PreCheckExpression : public ASTWalker {
TypeChecker &TC;
ASTContext &Ctx;
DeclContext *DC;

Expr *ParentExpr;
Expand Down Expand Up @@ -1053,14 +1053,17 @@ namespace {
}

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

ASTContext &getASTContext() const { return TC.Context; }
ASTContext &getASTContext() const { return Ctx; }

bool walkToClosureExprPre(ClosureExpr *expr);

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
// FIXME: Remove TypeChecker dependencies below.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();

// If this is a call, record the argument expression.
if (auto call = dyn_cast<ApplyExpr>(expr)) {
if (!isa<SelfApplyExpr>(expr)) {
Expand Down Expand Up @@ -1994,7 +1997,7 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
return nullptr;

// Don't bother to convert deprecated selector syntax.
if (auto selectorTy = TC.Context.getSelectorType()) {
if (auto selectorTy = getASTContext().getSelectorType()) {
if (type->isEqual(selectorTy))
return nullptr;
}
Expand All @@ -2011,7 +2014,7 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
/// Pre-check the expression, validating any types that occur in the
/// expression and folding sequence expressions.
bool TypeChecker::preCheckExpression(Expr *&expr, DeclContext *dc) {
PreCheckExpression preCheck(*this, dc, expr);
PreCheckExpression preCheck(dc, expr);
// Perform the pre-check.
if (auto result = expr->walk(preCheck)) {
expr = result;
Expand Down Expand Up @@ -2444,7 +2447,7 @@ void TypeChecker::getPossibleTypesOfExpressionWithoutApplying(
static FunctionType *
getTypeOfCompletionOperatorImpl(TypeChecker &TC, DeclContext *DC, Expr *expr,
ConcreteDeclRef &referencedDecl) {
ASTContext &Context = TC.Context;
auto &Context = DC->getASTContext();

FrontendStatsTracer StatsTracer(Context.Stats,
"typecheck-completion-operator", expr);
Expand Down
26 changes: 22 additions & 4 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2274,11 +2274,11 @@ static void checkDynamicSelfType(ValueDecl *decl, Type type) {
namespace {
class DeclChecker : public DeclVisitor<DeclChecker> {
public:
TypeChecker &TC;
ASTContext &Ctx;

explicit DeclChecker(TypeChecker &TC) : TC(TC) {}
explicit DeclChecker(ASTContext &ctx) : Ctx(ctx) {}

ASTContext &getASTContext() const { return TC.Context; }
ASTContext &getASTContext() const { return Ctx; }

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

checkAccessControl(PBD);

// FIXME: Remove TypeChecker dependency.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();

// If the initializers in the PBD aren't checked yet, do so now.
for (auto i : range(PBD->getNumPatternEntries())) {
if (!PBD->isInitialized(i))
Expand Down Expand Up @@ -2657,6 +2660,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
(void) SD->getImplInfo();

TypeChecker::checkParameterAttributes(SD->getIndices());

// FIXME: Remove TypeChecker dependency.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
checkDefaultArguments(TC, SD->getIndices());

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


bool shouldSkipBodyTypechecking(const AbstractFunctionDecl *AFD) {
// FIXME: Remove TypeChecker dependency.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();

// Make sure we're in the mode that's skipping function bodies.
if (!TC.canSkipNonInlinableBodies())
return false;
Expand Down Expand Up @@ -3256,6 +3265,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}
}

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

if (auto *PL = EED->getParameterList()) {
TypeChecker::checkParameterAttributes(PL);

// FIXME: Remove TypeChecker dependency.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
checkDefaultArguments(TC, PL);
}

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

checkAccessControl(CD);

// FIXME: Remove TypeChecker dependencies below.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
if (requiresDefinition(CD) && !CD->hasBody()) {
// Complain if we should have a body.
CD->diagnose(diag::missing_initializer_def);
Expand All @@ -3607,6 +3623,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
} else if (shouldSkipBodyTypechecking(DD)) {
DD->setBodySkipped(DD->getBodySourceRange());
} else {
// FIXME: Remove TypeChecker dependency.
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
TC.definedFunctions.push_back(DD);
}
}
Expand Down Expand Up @@ -3658,7 +3676,7 @@ bool TypeChecker::isAvailabilitySafeForConformance(
}

void TypeChecker::typeCheckDecl(Decl *D) {
DeclChecker(*this).visit(D);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeCheckDecl can be static. That should remove some other type checker dependencies in the constraint system.

Copy link
Contributor Author

@hamishknight hamishknight Nov 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CodaFi Yup, I'm planning on sinking all of the getLegacyGlobalTypeChecker calls down to the places where we actually access the remaining type checker state. Thanks for the review!

DeclChecker(D->getASTContext()).visit(D);
}

// Returns 'nullptr' if this is the setter's 'newValue' parameter;
Expand Down
20 changes: 10 additions & 10 deletions lib/Sema/TypeCheckREPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,29 @@ using namespace swift;

namespace {
struct REPLContext {
TypeChecker &TC;
ASTContext &Context;
SourceFile &SF;
SmallVector<ValueDecl *, 4> PrintDecls;
SmallVector<ValueDecl *, 4> DebugPrintlnDecls;

REPLContext(TypeChecker &TC, SourceFile &SF)
: TC(TC), Context(TC.Context), SF(SF) {}
REPLContext(SourceFile &SF) : Context(SF.getASTContext()), SF(SF) {}

bool requirePrintDecls() {
if (!PrintDecls.empty() && !DebugPrintlnDecls.empty())
return false;

auto *stdlib = TypeChecker::getStdlibModule(&SF);
{
Identifier Id(Context.getIdentifier("_replPrintLiteralString"));
auto lookup = TypeChecker::lookupUnqualified(TC.getStdlibModule(&SF), Id,
SourceLoc());
auto lookup = TypeChecker::lookupUnqualified(stdlib, Id, SourceLoc());
if (!lookup)
return true;
for (auto result : lookup)
PrintDecls.push_back(result.getValueDecl());
}
{
Identifier Id(Context.getIdentifier("_replDebugPrintln"));
auto lookup = TypeChecker::lookupUnqualified(TC.getStdlibModule(&SF), Id,
SourceLoc());
auto lookup = TypeChecker::lookupUnqualified(stdlib, Id, SourceLoc());
if (!lookup)
return true;
for (auto result : lookup)
Expand Down Expand Up @@ -191,8 +188,8 @@ namespace {
unsigned NextResponseVariableIndex = 0;

public:
REPLChecker(TypeChecker &TC, SourceFile &SF, TopLevelContext &TLC)
: REPLContext(TC, SF), TLC(TLC) {}
REPLChecker(SourceFile &SF, TopLevelContext &TLC)
: REPLContext(SF), TLC(TLC) {}

void processREPLTopLevelExpr(Expr *E);
void processREPLTopLevelPatternBinding(PatternBindingDecl *PBD);
Expand Down Expand Up @@ -257,6 +254,9 @@ void REPLChecker::generatePrintOfExpression(StringRef NameStr, Expr *E) {
// Typecheck the function.
BraceStmt *Body = builder.createBodyStmt(Loc, EndLoc);
CE->setBody(Body, false);

// FIXME: Remove TypeChecker dependency.
auto &TC = *Context.getLegacyGlobalTypeChecker();
TC.typeCheckClosureBody(CE);
TC.ClosuresWithUncomputedCaptures.push_back(CE);

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

REPLChecker RC(*this, SF, TLC);
REPLChecker RC(SF, TLC);

// Loop over each of the new decls, processing them, adding them back to
// the Decls list.
Expand Down
Loading