Revert "[Type checker] Introduce directional path consistency algorithm"

This commit is contained in:
Mishal Shah
2016-08-24 17:53:12 -07:00
committed by GitHub
parent f683d84a55
commit a84704f4b5
6 changed files with 43 additions and 552 deletions

View File

@@ -27,7 +27,6 @@
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OptionSet.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/Types.h"
#include "swift/AST/TypeCheckerDebugConsumer.h"
@@ -1016,40 +1015,6 @@ public:
DictionaryElementTypeVariables;
private:
/// \brief Describe the candidate expression for partial solving.
/// This class used used by shrink & solve methods which apply
/// variation of directional path consistency algorithm in attempt
/// to reduce scopes of the overload sets (disjunctions) in the system.
class Candidate {
Expr *E;
TypeChecker &TC;
DeclContext *DC;
TypeLoc CT;
ContextualTypePurpose CTP;
public:
Candidate(ConstraintSystem &cs, Expr *expr)
: E(expr),
TC(cs.TC),
DC(cs.DC),
CT(cs.getContextualTypeLoc()),
CTP(cs.getContextualTypePurpose())
{}
/// \brief Try to solve this candidate sub-expression
/// and re-write it's OSR domains afterwards.
///
/// \returs true on solver failure, false otherwise.
bool solve();
/// \brief Apply solutions found by solver as reduced OSR sets for
/// for current and all of it's sub-expressions.
///
/// \param solutions The solutions found by running solver on the
/// this candidate expression.
void applySolutions(llvm::SmallVectorImpl<Solution> &solutions) const;
};
/// \brief Describes the current solver state.
struct SolverState {
SolverState(ConstraintSystem &cs);
@@ -2030,36 +1995,7 @@ private:
/// \returns true if an error occurred, false otherwise.
bool solveSimplified(SmallVectorImpl<Solution> &solutions,
FreeTypeVariableBinding allowFreeTypeVariables);
/// \brief Find reduced domains of disjunction constraints for given
/// expression, this is achived to solving individual sub-expressions
/// and combining resolving types. Such algorithm is called directional
/// path consistency because it goes from children to parents for all
/// related sub-expressions taking union of their domains.
///
/// \param expr The expression to find reductions for.
void shrink(Expr *expr);
public:
/// \brief Solve the system of constraints generated from provided expression.
///
/// \param expr The expression to generate constraints from.
/// \param convertType The expected type of the expression.
/// \param listener The callback to check solving progress.
/// \param solutions The set of solutions to the system of constraints.
/// \param allowFreeTypeVariables How to bind free type variables in
/// the solution.
///
/// \returns Error is an error occured, Solved is system is consistent
/// and solutions were found, Unsolved otherwise.
SolutionKind solve(Expr *&expr,
Type convertType,
ExprTypeCheckListener *listener,
SmallVectorImpl<Solution> &solutions,
FreeTypeVariableBinding allowFreeTypeVariables
= FreeTypeVariableBinding::Disallow);
/// \brief Solve the system of constraints.
///
/// \param solutions The set of solutions to this system of constraints.
@@ -2336,90 +2272,6 @@ TypeVariableType *TypeVariableType::getNew(const ASTContext &C, unsigned ID,
/// underlying forced downcast expression.
ForcedCheckedCastExpr *findForcedDowncast(ASTContext &ctx, Expr *expr);
/// ExprCleaner - This class is used by shrink to ensure that in
/// no situation will an expr node be left with a dangling type variable stuck
/// to it. Often type checking will create new AST nodes and replace old ones
/// (e.g. by turning an UnresolvedDotExpr into a MemberRefExpr). These nodes
/// might be left with pointers into the temporary constraint system through
/// their type variables, and we don't want pointers into the original AST to
/// dereference these now-dangling types.
class ExprCleaner {
llvm::SmallDenseMap<Expr *, Type> Exprs;
llvm::SmallDenseMap<TypeLoc *, Type> TypeLocs;
llvm::SmallDenseMap<Pattern *, Type> Patterns;
public:
ExprCleaner(Expr *E) {
struct ExprCleanserImpl : public ASTWalker {
ExprCleaner *TS;
ExprCleanserImpl(ExprCleaner *TS) : TS(TS) {}
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
TS->Exprs.insert({ expr, expr->getType() });
return { true, expr };
}
bool walkToTypeLocPre(TypeLoc &TL) override {
TS->TypeLocs.insert({ &TL, TL.getType() });
return true;
}
std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override {
TS->Patterns.insert({ P, P->getType() });
return { true, P };
}
// Don't walk into statements. This handles the BraceStmt in
// non-single-expr closures, so we don't walk into their body.
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
return { false, S };
}
};
E->walk(ExprCleanserImpl(this));
}
~ExprCleaner() {
// Check each of the expression nodes to verify that there are no type
// variables hanging out. If so, just nuke the type.
for (auto E : Exprs) {
E.getFirst()->setType(E.getSecond());
}
for (auto TL : TypeLocs) {
TL.getFirst()->setType(TL.getSecond(), false);
}
for (auto P : Patterns) {
P.getFirst()->setType(P.getSecond());
}
}
};
/**
* Count the number of overload sets present
* in the expression and all of the children.
*/
class OverloadSetCounter : public ASTWalker {
unsigned &NumOverloads;
public:
OverloadSetCounter(unsigned &overloads)
: NumOverloads(overloads)
{}
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
if (auto applyExpr = dyn_cast<ApplyExpr>(expr)) {
// If we've found function application and it's
// function is an overload set, count it.
if (isa<OverloadSetRefExpr>(applyExpr->getFn()))
++NumOverloads;
}
// Always recur into the children.
return { true, expr };
}
};
} // end namespace swift
#endif // LLVM_SWIFT_SEMA_CONSTRAINT_SYSTEM_H