Skip to content

Commit cc11fc6

Browse files
committed
[Type checker] Introduce a request for computing the type of a pattern.
Compute the "raw" type of a pattern via a request, caching the result of TypeChecker::typeCheckPattern(). Retain typeCheckPattern() as the general entrypoint for performing this computation so that other code doesn't need to change yet.
1 parent 033f9c7 commit cc11fc6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/GenericSignature.h"
2121
#include "swift/AST/Type.h"
2222
#include "swift/AST/Evaluator.h"
23+
#include "swift/AST/Pattern.h"
2324
#include "swift/AST/SimpleRequest.h"
2425
#include "swift/AST/TypeResolutionStage.h"
2526
#include "swift/Basic/AnyValue.h"
@@ -33,6 +34,7 @@ namespace swift {
3334
class AbstractStorageDecl;
3435
class AccessorDecl;
3536
enum class AccessorKind;
37+
class ContextualPattern;
3638
class DefaultArgumentExpr;
3739
class GenericParamList;
3840
class PrecedenceGroupDecl;
@@ -1991,6 +1993,32 @@ class HasDynamicMemberLookupAttributeRequest
19911993
}
19921994
};
19931995

1996+
/// Determines the type of a given pattern.
1997+
///
1998+
/// Note that this returns the "raw" pattern type, which can involve
1999+
/// unresolved types and unbound generic types where type inference is
2000+
/// allowed.
2001+
class PatternTypeRequest
2002+
: public SimpleRequest<PatternTypeRequest, Type(ContextualPattern),
2003+
CacheKind::Cached> {
2004+
public:
2005+
using SimpleRequest::SimpleRequest;
2006+
2007+
private:
2008+
friend SimpleRequest;
2009+
2010+
// Evaluation.
2011+
llvm::Expected<Type> evaluate(
2012+
Evaluator &evaluator, ContextualPattern pattern) const;
2013+
2014+
public:
2015+
bool isCached() const { return true; }
2016+
2017+
SourceLoc getNearestLoc() const {
2018+
return std::get<0>(getStorage()).getPattern()->getLoc();
2019+
}
2020+
};
2021+
19942022
// Allow AnyValue to compare two Type values, even though Type doesn't
19952023
// support ==.
19962024
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,6 @@ SWIFT_REQUEST(TypeChecker, TypeWitnessRequest,
216216
SWIFT_REQUEST(TypeChecker, ValueWitnessRequest,
217217
Witness(NormalProtocolConformance *, ValueDecl *),
218218
SeparatelyCached, NoLocationInfo)
219+
SWIFT_REQUEST(TypeChecker, PatternTypeRequest,
220+
Type(ContextualPattern),
221+
Cached, HasNearestLocation)

lib/Sema/TypeCheckPattern.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/SourceFile.h"
2525
#include "swift/AST/NameLookup.h"
2626
#include "swift/AST/ParameterList.h"
27+
#include "swift/AST/TypeCheckRequests.h"
2728
#include "llvm/Support/SaveAndRestore.h"
2829
#include <utility>
2930
using namespace swift;
@@ -704,6 +705,14 @@ static Type validateTypedPattern(TypeResolution resolution,
704705
}
705706

706707
Type TypeChecker::typeCheckPattern(ContextualPattern pattern) {
708+
DeclContext *dc = pattern.getDeclContext();
709+
ASTContext &ctx = dc->getASTContext();
710+
return evaluateOrDefault(
711+
ctx.evaluator, PatternTypeRequest{pattern}, ErrorType::get(ctx));
712+
}
713+
714+
llvm::Expected<Type> PatternTypeRequest::evaluate(
715+
Evaluator &evaluator, ContextualPattern pattern) const {
707716
Pattern *P = pattern.getPattern();
708717
DeclContext *dc = pattern.getDeclContext();
709718

0 commit comments

Comments
 (0)