Skip to content

Commit 5c6c236

Browse files
committed
[Evaluator] Use the evaluator directly from AST entry points.
Rather than call through the LazyResolver (when available) to satisfy queries that may require type checking, call into the evaluator directly: it will make use of the type checker (via the lazy resolver) when necessary. This change should allow us to use the request-evaluator’s cache for state rather than mutable AST state, as well as the ability to see requests that were evaluated after type checking.
1 parent 9e95a93 commit 5c6c236

File tree

8 files changed

+16
-216
lines changed

8 files changed

+16
-216
lines changed

include/swift/AST/LazyResolver.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ class LazyResolver {
7171
/// Resolve the "is Objective-C" bit for the given declaration.
7272
virtual void resolveIsObjC(ValueDecl *VD) = 0;
7373

74-
/// Retrieve the superclass of the given class.
75-
virtual Type getSuperclass(const ClassDecl *classDecl) = 0;
76-
77-
/// Retrieve the superclass of the given protocol.
78-
virtual Type getSuperclass(const ProtocolDecl *protocolDecl) = 0;
79-
80-
/// Resolve the raw type of the given enum.
81-
virtual Type getRawType(EnumDecl *enumDecl) = 0;
82-
83-
/// Get a specific inherited type from the given declaration.
84-
virtual Type getInheritedType(
85-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
86-
unsigned index) = 0;
87-
8874
/// Resolve the trailing where clause of the given protocol in-place.
8975
virtual void resolveTrailingWhereClause(ProtocolDecl *proto) = 0;
9076

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class EnumRawTypeRequest :
125125
#define SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE 10
126126

127127
#define SWIFT_TYPEID_ZONE SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE
128-
#define SWIFT_TYPEID_HEADER "swift/Sema/TypeCheckerTypeIDZone.def"
128+
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
129129
#include "swift/Basic/DefineTypeIDZone.h"
130130

131131
// Set up reporting of evaluated requests.
@@ -135,7 +135,7 @@ inline void reportEvaluatedRequest(UnifiedStatsReporter &stats, \
135135
const RequestType &request) { \
136136
++stats.getFrontendCounters().RequestType; \
137137
}
138-
#include "swift/Sema/TypeCheckerTypeIDZone.def"
138+
#include "swift/AST/TypeCheckerTypeIDZone.def"
139139
#undef SWIFT_TYPEID
140140

141141
} // end namespace swift

lib/AST/Decl.cpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/AST/ProtocolConformance.h"
3636
#include "swift/AST/ResilienceExpansion.h"
3737
#include "swift/AST/Stmt.h"
38+
#include "swift/AST/TypeCheckRequests.h"
3839
#include "swift/AST/TypeLoc.h"
3940
#include "swift/AST/SwiftNameTranslation.h"
4041
#include "clang/Lex/MacroInfo.h"
@@ -959,12 +960,8 @@ ExtensionDecl::takeConformanceLoaderSlow() {
959960

960961
Type ExtensionDecl::getInheritedType(unsigned index) const {
961962
ASTContext &ctx = getASTContext();
962-
if (auto lazyResolver = ctx.getLazyResolver()) {
963-
return lazyResolver->getInheritedType(const_cast<ExtensionDecl *>(this),
964-
index);
965-
}
966-
967-
return getInherited()[index].getType();
963+
return ctx.evaluator(InheritedTypeRequest{const_cast<ExtensionDecl *>(this),
964+
index});
968965
}
969966

970967
bool ExtensionDecl::isConstrainedExtension() const {
@@ -2329,12 +2326,8 @@ void ValueDecl::copyFormalAccessFrom(const ValueDecl *source,
23292326

23302327
Type TypeDecl::getInheritedType(unsigned index) const {
23312328
ASTContext &ctx = getASTContext();
2332-
if (auto lazyResolver = ctx.getLazyResolver()) {
2333-
return lazyResolver->getInheritedType(const_cast<TypeDecl *>(this),
2334-
index);
2335-
}
2336-
2337-
return getInherited()[index].getType();
2329+
return ctx.evaluator(InheritedTypeRequest{const_cast<TypeDecl *>(this),
2330+
index});
23382331
}
23392332

23402333
Type TypeDecl::getDeclaredInterfaceType() const {
@@ -2829,11 +2822,7 @@ EnumDecl::EnumDecl(SourceLoc EnumLoc,
28292822

28302823
Type EnumDecl::getRawType() const {
28312824
ASTContext &ctx = getASTContext();
2832-
if (auto lazyResolver = ctx.getLazyResolver()) {
2833-
return lazyResolver->getRawType(const_cast<EnumDecl *>(this));
2834-
}
2835-
2836-
return LazySemanticInfo.RawType.getPointer();
2825+
return ctx.evaluator(EnumRawTypeRequest{const_cast<EnumDecl *>(this)});
28372826
}
28382827

28392828
StructDecl::StructDecl(SourceLoc StructLoc, Identifier Name, SourceLoc NameLoc,
@@ -3286,11 +3275,7 @@ ProtocolDecl::getAssociatedTypeMembers() const {
32863275

32873276
Type ProtocolDecl::getSuperclass() const {
32883277
ASTContext &ctx = getASTContext();
3289-
if (auto lazyResolver = ctx.getLazyResolver()) {
3290-
return lazyResolver->getSuperclass(this);
3291-
}
3292-
3293-
return LazySemanticInfo.Superclass.getPointer();
3278+
return ctx.evaluator(SuperclassTypeRequest{const_cast<ProtocolDecl *>(this)});
32943279
}
32953280

32963281
ClassDecl *ProtocolDecl::getSuperclassDecl() const {
@@ -5698,11 +5683,7 @@ Type TypeBase::getSwiftNewtypeUnderlyingType() {
56985683

56995684
Type ClassDecl::getSuperclass() const {
57005685
ASTContext &ctx = getASTContext();
5701-
if (auto lazyResolver = ctx.getLazyResolver()) {
5702-
return lazyResolver->getSuperclass(this);
5703-
}
5704-
5705-
return LazySemanticInfo.Superclass.getPointer();
5686+
return ctx.evaluator(SuperclassTypeRequest{const_cast<ClassDecl *>(this)});
57065687
}
57075688

57085689
ClassDecl *ClassDecl::getSuperclassDecl() const {

lib/AST/TypeCheckRequests.cpp

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
#include "GenericTypeResolver.h"
13-
#include "TypeChecker.h"
14-
#include "swift/Sema/TypeCheckRequests.h"
12+
#include "swift/AST/TypeCheckRequests.h"
1513
#include "swift/AST/Decl.h"
16-
#include "swift/AST/ExistentialLayout.h"
14+
#include "swift/AST/DiagnosticsCommon.h"
1715
#include "swift/AST/TypeLoc.h"
1816
#include "swift/AST/Types.h"
19-
#include "swift/Subsystems.h"
2017

2118
using namespace swift;
2219

2320
namespace swift {
24-
// Implement the type checker type zone (zone 10).
21+
// Implement the type checker type zone (zone 10).
2522
#define SWIFT_TYPEID_ZONE 10
26-
#define SWIFT_TYPEID_HEADER "swift/Sema/TypeCheckerTypeIDZone.def"
23+
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
2724
#include "swift/Basic/ImplementTypeIDZone.h"
2825

2926
}
@@ -54,71 +51,6 @@ TypeLoc &InheritedTypeRequest::getTypeLoc(
5451
return decl.get<ExtensionDecl *>()->getInherited()[index];
5552
}
5653

57-
Type InheritedTypeRequest::evaluate(
58-
Evaluator &evaluator,
59-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
60-
unsigned index) const {
61-
// Figure out how to resolve types.
62-
TypeResolutionOptions options;
63-
DeclContext *dc;
64-
if (auto typeDecl = decl.dyn_cast<TypeDecl *>()) {
65-
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
66-
dc = nominal;
67-
options |= TypeResolutionFlags::GenericSignature;
68-
options |= TypeResolutionFlags::InheritanceClause;
69-
options |= TypeResolutionFlags::AllowUnavailableProtocol;
70-
} else {
71-
dc = typeDecl->getDeclContext();
72-
73-
if (isa<GenericTypeParamDecl>(typeDecl)) {
74-
// For generic parameters, we want name lookup to look at just the
75-
// signature of the enclosing entity.
76-
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {
77-
dc = nominal;
78-
options |= TypeResolutionFlags::GenericSignature;
79-
} else if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
80-
dc = ext;
81-
options |= TypeResolutionFlags::GenericSignature;
82-
} else if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
83-
dc = func;
84-
options |= TypeResolutionFlags::GenericSignature;
85-
} else if (!dc->isModuleScopeContext()) {
86-
// Skip the generic parameter's context entirely.
87-
dc = dc->getParent();
88-
}
89-
}
90-
}
91-
} else {
92-
auto ext = decl.get<ExtensionDecl *>();
93-
dc = ext;
94-
options |= TypeResolutionFlags::GenericSignature;
95-
options |= TypeResolutionFlags::InheritanceClause;
96-
options |= TypeResolutionFlags::AllowUnavailableProtocol;
97-
}
98-
99-
ProtocolRequirementTypeResolver protoResolver;
100-
GenericTypeToArchetypeResolver archetypeResolver(dc);
101-
GenericTypeResolver *resolver;
102-
if (isa<ProtocolDecl>(dc)) {
103-
resolver = &protoResolver;
104-
} else {
105-
resolver = &archetypeResolver;
106-
}
107-
108-
// FIXME: Hack for calls through here when we have no type checker.
109-
auto lazyResolver = dc->getASTContext().getLazyResolver();
110-
if (!lazyResolver) return ErrorType::get(dc->getASTContext());
111-
112-
TypeChecker &tc = *static_cast<TypeChecker *>(lazyResolver);
113-
TypeLoc &typeLoc = getTypeLoc(decl, index);
114-
115-
Type inheritedType =
116-
tc.resolveType(typeLoc.getTypeRepr(), dc, options, resolver);
117-
if (inheritedType && !isa<ProtocolDecl>(dc))
118-
inheritedType = inheritedType->mapTypeOutOfContext();
119-
return inheritedType ? inheritedType : ErrorType::get(tc.Context);
120-
}
121-
12254
void InheritedTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
12355
const auto &storage = getStorage();
12456
auto &typeLoc = getTypeLoc(std::get<0>(storage), std::get<1>(storage));
@@ -149,40 +81,6 @@ void InheritedTypeRequest::cacheResult(Type value) const {
14981
//----------------------------------------------------------------------------//
15082
// Superclass computation.
15183
//----------------------------------------------------------------------------//
152-
Type SuperclassTypeRequest::evaluate(Evaluator &evaluator,
153-
NominalTypeDecl *nominalDecl) const {
154-
assert(isa<ClassDecl>(nominalDecl) || isa<ProtocolDecl>(nominalDecl));
155-
156-
for (unsigned int idx : indices(nominalDecl->getInherited())) {
157-
Type inheritedType = evaluator(InheritedTypeRequest{nominalDecl, idx});
158-
if (!inheritedType) continue;
159-
160-
// If we found a class, return it.
161-
if (inheritedType->getClassOrBoundGenericClass()) {
162-
if (inheritedType->hasArchetype())
163-
return inheritedType->mapTypeOutOfContext();
164-
165-
return inheritedType;
166-
}
167-
168-
// If we found an existential with a superclass bound, return it.
169-
if (inheritedType->isExistentialType()) {
170-
if (auto superclassType =
171-
inheritedType->getExistentialLayout().superclass) {
172-
if (superclassType->getClassOrBoundGenericClass()) {
173-
if (superclassType->hasArchetype())
174-
return superclassType->mapTypeOutOfContext();
175-
176-
return superclassType;
177-
}
178-
}
179-
}
180-
}
181-
182-
// No superclass.
183-
return Type();
184-
}
185-
18684
void SuperclassTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
18785
// FIXME: Improve this diagnostic.
18886
auto nominalDecl = std::get<0>(getStorage());
@@ -223,26 +121,6 @@ void SuperclassTypeRequest::cacheResult(Type value) const {
223121
//----------------------------------------------------------------------------//
224122
// Enum raw type computation.
225123
//----------------------------------------------------------------------------//
226-
Type EnumRawTypeRequest::evaluate(Evaluator &evaluator,
227-
EnumDecl *enumDecl) const {
228-
for (unsigned int idx : indices(enumDecl->getInherited())) {
229-
Type inheritedType = evaluator(InheritedTypeRequest{enumDecl, idx});
230-
if (!inheritedType) continue;
231-
232-
// Skip existential types.
233-
if (inheritedType->isExistentialType()) continue;
234-
235-
// We found a raw type; return it.
236-
if (inheritedType->hasArchetype())
237-
return inheritedType->mapTypeOutOfContext();
238-
239-
return inheritedType;
240-
}
241-
242-
// No raw type.
243-
return Type();
244-
}
245-
246124
void EnumRawTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
247125
// FIXME: Improve this diagnostic.
248126
auto enumDecl = std::get<0>(getStorage());
@@ -267,16 +145,3 @@ void EnumRawTypeRequest::cacheResult(Type value) const {
267145
auto enumDecl = std::get<0>(getStorage());
268146
enumDecl->LazySemanticInfo.RawType.setPointerAndInt(value, true);
269147
}
270-
271-
// Define request evaluation functions for each of the type checker requests.
272-
static AbstractRequestFunction *typeCheckerRequestFunctions[] = {
273-
#define SWIFT_TYPEID(Name) \
274-
reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest),
275-
#include "swift/Sema/TypeCheckerTypeIDZone.def"
276-
#undef SWIFT_TYPEID
277-
};
278-
279-
void swift::registerTypeCheckerRequestFunctions(Evaluator &evaluator) {
280-
evaluator.registerRequestFunctions(SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE,
281-
typeCheckerRequestFunctions);
282-
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -233,26 +233,6 @@ static void addImplicitConformances(
233233
/// Check that the declaration attributes are ok.
234234
static void validateAttributes(TypeChecker &TC, Decl *D);
235235

236-
Type TypeChecker::getSuperclass(const ClassDecl *classDecl) {
237-
return Context.evaluator(
238-
SuperclassTypeRequest(const_cast<ClassDecl *>(classDecl)));
239-
}
240-
241-
Type TypeChecker::getSuperclass(const ProtocolDecl *protocolDecl) {
242-
return Context.evaluator(
243-
SuperclassTypeRequest(const_cast<ProtocolDecl *>(protocolDecl)));
244-
}
245-
246-
Type TypeChecker::getRawType(EnumDecl *enumDecl) {
247-
return Context.evaluator(EnumRawTypeRequest(enumDecl));
248-
}
249-
250-
Type TypeChecker::getInheritedType(
251-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
252-
unsigned index) {
253-
return Context.evaluator(InheritedTypeRequest(decl, index));
254-
}
255-
256236
void TypeChecker::resolveTrailingWhereClause(ProtocolDecl *proto) {
257237
ProtocolRequirementTypeResolver resolver;
258238
validateWhereClauses(proto, &resolver);

lib/Sema/TypeCheckRequestFunctions.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,3 @@ void swift::registerTypeCheckerRequestFunctions(Evaluator &evaluator) {
151151
evaluator.registerRequestFunctions(SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE,
152152
typeCheckerRequestFunctions);
153153
}
154-

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,15 +1544,6 @@ class TypeChecker final : public LazyResolver {
15441544
GenericRequirementsCheckListener *listener = nullptr,
15451545
SubstOptions options = None);
15461546

1547-
/// Get the superclass of the given class.
1548-
Type getSuperclass(const ClassDecl *classDecl) override;
1549-
1550-
/// Get the superclass of the given protocol.
1551-
Type getSuperclass(const ProtocolDecl *protocolDecl) override;
1552-
1553-
/// Get the raw type of the given enum.
1554-
Type getRawType(EnumDecl *enumDecl) override;
1555-
15561547
/// Resolve the inherited protocols of a given protocol.
15571548
void resolveInheritedProtocols(ProtocolDecl *protocol);
15581549

@@ -1561,10 +1552,6 @@ class TypeChecker final : public LazyResolver {
15611552
void validateWhereClauses(ProtocolDecl *protocol,
15621553
GenericTypeResolver *resolver);
15631554

1564-
/// Get a specific inherited type from the given declaration.
1565-
Type getInheritedType(llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
1566-
unsigned index) override;
1567-
15681555
void resolveTrailingWhereClause(ProtocolDecl *proto) override;
15691556

15701557
/// Check the inheritance clause of the given declaration.

unittests/AST/TestContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "TestContext.h"
1414
#include "swift/AST/Module.h"
1515
#include "swift/Strings.h"
16+
#include "swift/Subsystems.h"
1617

1718
using namespace swift;
1819
using namespace swift::unittest;
@@ -34,6 +35,7 @@ static void declareOptionalType(ASTContext &ctx, SourceFile *fileForLookups,
3435

3536
TestContext::TestContext(ShouldDeclareOptionalTypes optionals)
3637
: Ctx(*ASTContext::get(LangOpts, SearchPathOpts, SourceMgr, Diags)) {
38+
registerTypeCheckerRequestFunctions(Ctx.evaluator);
3739
auto stdlibID = Ctx.getIdentifier(STDLIB_NAME);
3840
auto *module = ModuleDecl::create(stdlibID, Ctx);
3941
Ctx.LoadedModules[stdlibID] = module;

0 commit comments

Comments
 (0)