Skip to content

[Gardening] Strip TypeChecker of its ASTContext #28231

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 5 commits into from
Nov 13, 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
4 changes: 2 additions & 2 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ ConstraintSystem::SolverState::SolverState(

// If we're supposed to debug a specific constraint solver attempt,
// turn on debugging now.
ASTContext &ctx = CS.getTypeChecker().Context;
ASTContext &ctx = CS.getASTContext();
auto &tyOpts = ctx.TypeCheckerOpts;
OldDebugConstraintSolver = tyOpts.DebugConstraintSolver;
if (tyOpts.DebugConstraintSolverAttempt &&
Expand Down Expand Up @@ -409,7 +409,7 @@ ConstraintSystem::SolverState::~SolverState() {
}

// Restore debugging state.
TypeCheckerOptions &tyOpts = CS.getTypeChecker().Context.TypeCheckerOpts;
TypeCheckerOptions &tyOpts = CS.getASTContext().TypeCheckerOpts;
tyOpts.DebugConstraintSolver = OldDebugConstraintSolver;

// Write our local statistics back to the overall statistics.
Expand Down
17 changes: 9 additions & 8 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,7 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
TypeCheckExprOptions options,
ExprTypeCheckListener &listener,
ConstraintSystem *baseCS) {
auto &Context = dc->getASTContext();
FrontendStatsTracer StatsTracer(Context.Stats, "typecheck-expr", expr);
PrettyStackTraceExpr stackTrace(Context, "type-checking", expr);

Expand Down Expand Up @@ -3717,22 +3718,22 @@ void ConstraintSystem::print(raw_ostream &out) const {
out << "\nActive Constraints:\n";
for (auto &constraint : ActiveConstraints) {
out.indent(2);
constraint.print(out, &getTypeChecker().Context.SourceMgr);
constraint.print(out, &getASTContext().SourceMgr);
out << "\n";
}

out << "\nInactive Constraints:\n";
for (auto &constraint : InactiveConstraints) {
out.indent(2);
constraint.print(out, &getTypeChecker().Context.SourceMgr);
constraint.print(out, &getASTContext().SourceMgr);
out << "\n";
}

if (solverState && !solverState->hasRetiredConstraints()) {
out << "\nRetired Constraints:\n";
solverState->forEachRetired([&](Constraint &constraint) {
out.indent(2);
constraint.print(out, &getTypeChecker().Context.SourceMgr);
constraint.print(out, &getASTContext().SourceMgr);
out << "\n";
});
}
Expand Down Expand Up @@ -3786,7 +3787,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
out << "\nDisjunction choices:\n";
for (auto &choice : DisjunctionChoices) {
out.indent(2);
choice.first->dump(&getTypeChecker().Context.SourceMgr, out);
choice.first->dump(&getASTContext().SourceMgr, out);
out << " is #" << choice.second << "\n";
}
}
Expand All @@ -3795,7 +3796,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
out << "\nOpened types:\n";
for (const auto &opened : OpenedTypes) {
out.indent(2);
opened.first->dump(&getTypeChecker().Context.SourceMgr, out);
opened.first->dump(&getASTContext().SourceMgr, out);
out << " opens ";
interleave(opened.second.begin(), opened.second.end(),
[&](OpenedType opened) {
Expand All @@ -3814,7 +3815,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
out << "\nOpened existential types:\n";
for (const auto &openedExistential : OpenedExistentialTypes) {
out.indent(2);
openedExistential.first->dump(&getTypeChecker().Context.SourceMgr, out);
openedExistential.first->dump(&getASTContext().SourceMgr, out);
out << " opens to " << openedExistential.second->getString(PO);
out << "\n";
}
Expand All @@ -3823,7 +3824,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
if (!DefaultedConstraints.empty()) {
out << "\nDefaulted constraints: ";
interleave(DefaultedConstraints, [&](ConstraintLocator *locator) {
locator->dump(&getTypeChecker().Context.SourceMgr, out);
locator->dump(&getASTContext().SourceMgr, out);
}, [&] {
out << ", ";
});
Expand All @@ -3832,7 +3833,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
if (failedConstraint) {
out << "\nFailed constraint:\n";
out.indent(2);
failedConstraint->print(out, &getTypeChecker().Context.SourceMgr);
failedConstraint->print(out, &getASTContext().SourceMgr);
out << "\n";
}

Expand Down
7 changes: 1 addition & 6 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,12 @@ using namespace swift;
TypeChecker &TypeChecker::createForContext(ASTContext &ctx) {
assert(!ctx.getLegacyGlobalTypeChecker() &&
"Cannot install more than one instance of the global type checker!");
auto *TC = new TypeChecker(ctx);
auto *TC = new TypeChecker();
ctx.installGlobalTypeChecker(TC);
ctx.addCleanup([=](){ delete TC; });
return *ctx.getLegacyGlobalTypeChecker();
}

TypeChecker::TypeChecker(ASTContext &Ctx)
: Context(Ctx) {}

TypeChecker::~TypeChecker() {}

ProtocolDecl *TypeChecker::getProtocol(ASTContext &Context, SourceLoc loc,
KnownProtocolKind kind) {
auto protocol = Context.getProtocol(kind);
Expand Down
42 changes: 17 additions & 25 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,10 @@ class TypeChecker final {
std::vector<AbstractClosureExpr *> ClosuresWithUncomputedCaptures;

private:
ASTContext &Context;
TypeChecker() = default;
~TypeChecker() = default;

TypeChecker(ASTContext &Ctx);
friend class ASTContext;
friend class constraints::ConstraintSystem;
friend class TypeCheckFunctionBodyUntilRequest;

public:
/// Create a new type checker instance for the given ASTContext, if it
Expand All @@ -559,7 +557,6 @@ class TypeChecker final {
public:
TypeChecker(const TypeChecker&) = delete;
TypeChecker& operator=(const TypeChecker&) = delete;
~TypeChecker();

static Type getArraySliceType(SourceLoc loc, Type elementType);
static Type getDictionaryType(SourceLoc loc, Type keyType, Type valueType);
Expand Down Expand Up @@ -928,24 +925,17 @@ class TypeChecker final {
/// struct or class.
static void addImplicitConstructors(NominalTypeDecl *typeDecl);

/// \name Name lookup
///
/// Routines that perform name lookup.
///
/// During type checking, these routines should be used instead of
/// \c MemberLookup and \c UnqualifiedLookup, because these routines will
/// lazily introduce declarations and (FIXME: eventually) perform recursive
/// type-checking that the AST-level lookup routines don't.
///
/// @{
private:
Optional<Type> boolType;

public:
/// Fold the given sequence expression into an (unchecked) expression
/// tree.
static Expr *foldSequence(SequenceExpr *expr, DeclContext *dc);

private:
/// Given an pre-folded expression, find LHS from the expression if a binary
/// operator \c name appended to the expression.
static Expr *findLHS(DeclContext *DC, Expr *E, Identifier name);

public:
/// Type check the given expression.
///
/// \param expr The expression to type-check, which will be modified in
Expand Down Expand Up @@ -1338,6 +1328,12 @@ class TypeChecker final {
static Type deriveTypeWitness(DeclContext *DC, NominalTypeDecl *nominal,
AssociatedTypeDecl *assocType);

/// \name Name lookup
///
/// Routines that perform name lookup.
///
/// @{

/// Perform unqualified name lookup at the given source location
/// within a particular declaration context.
///
Expand Down Expand Up @@ -1374,10 +1370,6 @@ class TypeChecker final {
NameLookupOptions options
= defaultMemberLookupOptions);

/// Check whether the given declaration can be written as a
/// member of the given base type.
static bool isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl);

/// Look up a member type within the given type.
///
/// This routine looks for member types with the given name within the
Expand Down Expand Up @@ -1414,9 +1406,9 @@ class TypeChecker final {
Identifier name,
SourceLoc nameLoc);

/// Given an pre-folded expression, find LHS from the expression if a binary
/// operator \c name appended to the expression.
static Expr *findLHS(DeclContext *DC, Expr *E, Identifier name);
/// Check whether the given declaration can be written as a
/// member of the given base type.
static bool isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl);

/// @}

Expand Down