Skip to content

[NFC] Drop More Type Checkers #28179

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 13 commits into from
Nov 11, 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
2 changes: 2 additions & 0 deletions include/swift/AST/ASTTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

SWIFT_TYPEID(AncestryFlags)
SWIFT_TYPEID(CtorInitializerKind)
SWIFT_TYPEID(FunctionBuilderClosurePreCheck)
SWIFT_TYPEID(GenericSignature)
SWIFT_TYPEID(ImplicitMemberAction)
SWIFT_TYPEID(ParamSpecifier)
Expand All @@ -28,6 +29,7 @@ SWIFT_TYPEID(Type)
SWIFT_TYPEID(TypePair)
SWIFT_TYPEID(TypeWitnessAndDecl)
SWIFT_TYPEID(Witness)
SWIFT_TYPEID_NAMED(ClosureExpr *, ClosureExpr)
SWIFT_TYPEID_NAMED(ConstructorDecl *, ConstructorDecl)
SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr)
SWIFT_TYPEID_NAMED(Decl *, Decl)
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/ASTTypeIDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ namespace swift {

class AbstractFunctionDecl;
class BraceStmt;
class ClosureExpr;
class ConstructorDecl;
class CustomAttr;
class Decl;
class EnumDecl;
enum class FunctionBuilderClosurePreCheck : uint8_t;
class GenericParamList;
class GenericSignature;
class GenericTypeParamType;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5407,6 +5407,9 @@ Expr *packSingleArgument(ASTContext &ctx, SourceLoc lParenLoc,
[](const Expr *E) -> Type {
return E->getType();
});

void simple_display(llvm::raw_ostream &out, const ClosureExpr *CE);

} // end namespace swift

#endif
31 changes: 31 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,36 @@ class ValueWitnessRequest
void cacheResult(Witness value) const;
};

enum class FunctionBuilderClosurePreCheck : uint8_t {
/// There were no problems pre-checking the closure.
Okay,

/// There was an error pre-checking the closure.
Error,

/// The closure has a return statement.
HasReturnStmt,
};

class PreCheckFunctionBuilderRequest
: public SimpleRequest<PreCheckFunctionBuilderRequest,
FunctionBuilderClosurePreCheck(ClosureExpr *),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<FunctionBuilderClosurePreCheck>
evaluate(Evaluator &evaluator, ClosureExpr *closure) const;

public:
// Separate caching.
bool isCached() const { return true; }
};

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand All @@ -1767,6 +1797,7 @@ AnyValue::Holder<GenericSignature>::equals(const HolderBase &other) const {
void simple_display(llvm::raw_ostream &out, Type value);
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);
void simple_display(llvm::raw_ostream &out, ImplicitMemberAction action);
void simple_display(llvm::raw_ostream &out, FunctionBuilderClosurePreCheck pck);

#define SWIFT_TYPEID_ZONE TypeChecker
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ SWIFT_REQUEST(TypeChecker, HasUserDefinedDesignatedInitRequest,
bool(NominalTypeDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasMemberwiseInitRequest,
bool(StructDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, PreCheckFunctionBuilderRequest,
FunctionBuilderClosurePreCheck(ClosureExpr *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
bool(NominalTypeDecl *, ImplicitMemberAction), Uncached,
NoLocationInfo)
Expand Down
13 changes: 13 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,19 @@ SourceLoc TapExpr::getEndLoc() const {
return SourceLoc();
}

void swift::simple_display(llvm::raw_ostream &out, const ClosureExpr *CE) {
if (!CE) {
out << "(null)";
return;
}

if (CE->hasSingleExpressionBody()) {
out << "single expression closure";
} else {
out << "closure";
}
}

// See swift/Basic/Statistic.h for declaration: this enables tracing Exprs, is
// defined here to avoid too much layering violation / circular linkage
// dependency.
Expand Down
19 changes: 19 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,3 +1113,22 @@ Optional<Witness> ValueWitnessRequest::getCachedResult() const {
void ValueWitnessRequest::cacheResult(Witness type) const {
// FIXME: Refactor this to be the thing that warms the cache.
}

//----------------------------------------------------------------------------//
// PreCheckFunctionBuilderRequest computation.
//----------------------------------------------------------------------------//

void swift::simple_display(llvm::raw_ostream &out,
FunctionBuilderClosurePreCheck value) {
switch (value) {
case FunctionBuilderClosurePreCheck::Okay:
out << "okay";
break;
case FunctionBuilderClosurePreCheck::HasReturnStmt:
out << "has return statement";
break;
case FunctionBuilderClosurePreCheck::Error:
out << "error";
break;
}
}
31 changes: 12 additions & 19 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/TypeCheckRequests.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <iterator>
Expand Down Expand Up @@ -516,7 +517,9 @@ ConstraintSystem::TypeMatchResult ConstraintSystem::applyFunctionBuilder(

// Pre-check the closure body: pre-check any expressions in it and look
// for return statements.
switch (TC.preCheckFunctionBuilderClosureBody(closure)) {
auto request = PreCheckFunctionBuilderRequest{closure};
switch (evaluateOrDefault(getASTContext().evaluator, request,
FunctionBuilderClosurePreCheck::Error)) {
case FunctionBuilderClosurePreCheck::Okay:
// If the pre-check was okay, apply the function-builder transform.
break;
Expand Down Expand Up @@ -582,7 +585,7 @@ ConstraintSystem::TypeMatchResult ConstraintSystem::applyFunctionBuilder(
// that CSGen might have.
//
// TODO: just build the AST the way we want it in the first place.
if (TC.preCheckExpression(singleExpr, closure))
if (ConstraintSystem::preCheckExpression(singleExpr, closure))
return getTypeMatchFailure(locator);

singleExpr = generateConstraints(singleExpr, closure);
Expand Down Expand Up @@ -617,13 +620,12 @@ namespace {

/// Pre-check all the expressions in the closure body.
class PreCheckFunctionBuilderClosure : public ASTWalker {
TypeChecker &TC;
ClosureExpr *Closure;
bool HasReturnStmt = false;
bool HasError = false;
public:
PreCheckFunctionBuilderClosure(TypeChecker &tc, ClosureExpr *closure)
: TC(tc), Closure(closure) {}
PreCheckFunctionBuilderClosure(ClosureExpr *closure)
: Closure(closure) {}

FunctionBuilderClosurePreCheck run() {
Stmt *oldBody = Closure->getBody();
Expand All @@ -649,7 +651,7 @@ class PreCheckFunctionBuilderClosure : public ASTWalker {
// Pre-check the expression. If this fails, abort the walk immediately.
// Otherwise, replace the expression with the result of pre-checking.
// In either case, don't recurse into the expression.
if (TC.preCheckExpression(E, /*DC*/ Closure)) {
if (ConstraintSystem::preCheckExpression(E, /*DC*/ Closure)) {
HasError = true;
return std::make_pair(false, nullptr);
}
Expand All @@ -671,21 +673,12 @@ class PreCheckFunctionBuilderClosure : public ASTWalker {

}

FunctionBuilderClosurePreCheck
TypeChecker::preCheckFunctionBuilderClosureBody(ClosureExpr *closure) {
llvm::Expected<FunctionBuilderClosurePreCheck>
PreCheckFunctionBuilderRequest::evaluate(Evaluator &eval,
ClosureExpr *closure) const {
// Single-expression closures should already have been pre-checked.
if (closure->hasSingleExpressionBody())
return FunctionBuilderClosurePreCheck::Okay;

// Check whether we've already done this analysis.
auto it = precheckedFunctionBuilderClosures.find(closure);
if (it != precheckedFunctionBuilderClosures.end())
return it->second;

auto result = PreCheckFunctionBuilderClosure(*this, closure).run();

// Cache the result.
precheckedFunctionBuilderClosures.insert(std::make_pair(closure, result));

return result;
return PreCheckFunctionBuilderClosure(closure).run();
}
34 changes: 16 additions & 18 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ namespace {
refExpr = declRefExpr;
}

auto resultTy = cs.getTypeChecker().typeCheckExpression(
auto resultTy = TypeChecker::typeCheckExpression(
refExpr, cs.DC, TypeLoc::withoutLoc(expectedFnType),
CTP_CannotFail);
if (!resultTy)
Expand Down Expand Up @@ -2818,7 +2818,7 @@ namespace {
//
// The result is that in Swift 5, 'try?' avoids producing nested optionals.

if (!cs.getTypeChecker().getLangOpts().isSwiftVersionAtLeast(5)) {
if (!cs.getASTContext().LangOpts.isSwiftVersionAtLeast(5)) {
// Nothing to do for Swift 4 and earlier!
return simplifyExprType(expr);
}
Expand Down Expand Up @@ -3124,7 +3124,7 @@ namespace {
auto castContextKind =
SuppressDiagnostics ? CheckedCastContextKind::None
: CheckedCastContextKind::IsExpr;
auto castKind = cs.getTypeChecker().typeCheckCheckedCast(
auto castKind = TypeChecker::typeCheckCheckedCast(
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
expr->getCastTypeLoc().getSourceRange());

Expand Down Expand Up @@ -3531,7 +3531,7 @@ namespace {

solution.setExprTypes(sub);

if (cs.getTypeChecker().convertToType(sub, toType, cs.DC))
if (TypeChecker::convertToType(sub, toType, cs.DC))
return nullptr;

cs.cacheExprTypes(sub);
Expand Down Expand Up @@ -3576,7 +3576,7 @@ namespace {
: CheckedCastContextKind::ForcedCast;

auto fromType = cs.getType(sub);
auto castKind = cs.getTypeChecker().typeCheckCheckedCast(
auto castKind = TypeChecker::typeCheckCheckedCast(
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
expr->getCastTypeLoc().getSourceRange());
switch (castKind) {
Expand Down Expand Up @@ -3657,7 +3657,7 @@ namespace {
: CheckedCastContextKind::ConditionalCast;

auto fromType = cs.getType(sub);
auto castKind = cs.getTypeChecker().typeCheckCheckedCast(
auto castKind = TypeChecker::typeCheckCheckedCast(
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
expr->getCastTypeLoc().getSourceRange());
switch (castKind) {
Expand Down Expand Up @@ -3875,7 +3875,7 @@ namespace {
Expr *callExpr = CallExpr::createImplicit(ctx, fnRef, { argExpr },
{ Identifier() });

auto resultTy = cs.getTypeChecker().typeCheckExpression(
auto resultTy = TypeChecker::typeCheckExpression(
callExpr, cs.DC, TypeLoc::withoutLoc(valueType), CTP_CannotFail);
assert(resultTy && "Conversion cannot fail!");
(void)resultTy;
Expand Down Expand Up @@ -4494,8 +4494,8 @@ namespace {
cs.cacheType(outerClosure);

// The inner closure at least will definitely have a capture.
cs.TC.ClosuresWithUncomputedCaptures.push_back(outerClosure);
cs.TC.ClosuresWithUncomputedCaptures.push_back(closure);
cs.getTypeChecker().ClosuresWithUncomputedCaptures.push_back(outerClosure);
cs.getTypeChecker().ClosuresWithUncomputedCaptures.push_back(closure);

// let outerApply = "\( outerClosure )( \(E) )"
auto outerApply = CallExpr::createImplicit(ctx, outerClosure, {E}, {});
Expand Down Expand Up @@ -4787,7 +4787,7 @@ getCallerDefaultArg(ConstraintSystem &cs, DeclContext *dc,
// Convert the literal to the appropriate type.
auto defArgType =
param->getInterfaceType().subst(owner.getSubstitutions());
auto resultTy = cs.getTypeChecker().typeCheckParameterDefault(
auto resultTy = TypeChecker::typeCheckParameterDefault(
init, dc, defArgType,
/*isAutoClosure=*/param->isAutoClosure(),
/*canFail=*/false);
Expand Down Expand Up @@ -5408,7 +5408,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
arg, closureType->getResult(),
locator.withPathElement(ConstraintLocator::AutoclosureResult));

convertedArg = cs.TC.buildAutoClosureExpr(dc, arg, closureType);
convertedArg = TypeChecker::buildAutoClosureExpr(dc, arg, closureType);
cs.cacheExprTypes(convertedArg);
} else {
convertedArg = coerceToType(
Expand Down Expand Up @@ -5676,12 +5676,10 @@ static Expr *buildElementConversion(ExprRewriter &rewriter,
ConstraintLocatorBuilder locator,
Expr *element) {
auto &cs = rewriter.getConstraintSystem();

auto &tc = rewriter.getConstraintSystem().getTypeChecker();
if (bridged &&
tc.typeCheckCheckedCast(srcType, destType,
CheckedCastContextKind::None, cs.DC,
SourceLoc(), nullptr, SourceRange())
TypeChecker::typeCheckCheckedCast(srcType, destType,
CheckedCastContextKind::None, cs.DC,
SourceLoc(), nullptr, SourceRange())
!= CheckedCastKind::Coercion) {
if (auto conversion =
rewriter.buildObjCBridgeExpr(element, destType, locator))
Expand Down Expand Up @@ -7443,14 +7441,14 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
if (!skipClosures) {
bool hadError = false;
for (auto *closure : walker.getClosuresToTypeCheck())
hadError |= getTypeChecker().typeCheckClosureBody(closure);
hadError |= TypeChecker::typeCheckClosureBody(closure);

// Tap expressions too; they should or should not be
// type-checked under the same conditions as closure bodies.
for (auto tuple : walker.getTapsToTypeCheck()) {
auto tap = std::get<0>(tuple);
auto tapDC = std::get<1>(tuple);
hadError |= getTypeChecker().typeCheckTapBody(tap, tapDC);
hadError |= TypeChecker::typeCheckTapBody(tap, tapDC);
}

// If any of them failed to type check, bail.
Expand Down
Loading