Skip to content

Commit 7e85463

Browse files
authored
Merge pull request #38382 from slavapestov/encapsulate-lookup-nested-type
Introduce GenericSignature::lookupNestedType() to further hide GenericSignatureBuilder
2 parents b7c8b77 + f5f2b0f commit 7e85463

File tree

8 files changed

+53
-74
lines changed

8 files changed

+53
-74
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
389389
ConformanceAccessPath getConformanceAccessPath(Type type,
390390
ProtocolDecl *protocol) const;
391391

392+
/// Lookup a nested type with the given name within this type parameter.
393+
TypeDecl *lookupNestedType(Type type, Identifier name) const;
394+
392395
/// Get the ordinal of a generic parameter in this generic signature.
393396
///
394397
/// For example, if you have a generic signature for a nested context like:

lib/AST/GenericSignature.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,21 @@ GenericSignatureImpl::getConformanceAccessPath(Type type,
795795
type, protocol, this);
796796
}
797797

798+
TypeDecl *
799+
GenericSignatureImpl::lookupNestedType(Type type, Identifier name) const {
800+
assert(type->isTypeParameter());
801+
802+
auto *builder = getGenericSignatureBuilder();
803+
auto equivClass =
804+
builder->resolveEquivalenceClass(
805+
type,
806+
ArchetypeResolutionKind::CompleteWellFormed);
807+
if (!equivClass)
808+
return nullptr;
809+
810+
return equivClass->lookupNestedType(*builder, name);
811+
}
812+
798813
unsigned GenericParamKey::findIndexIn(
799814
TypeArrayView<GenericTypeParamType> genericParams) const {
800815
// For depth 0, we have random access. We perform the extra checking so that

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "swift/AST/DiagnosticEngine.h"
2424
#include "swift/AST/DiagnosticsClangImporter.h"
2525
#include "swift/AST/ExistentialLayout.h"
26-
#include "swift/AST/GenericEnvironment.h"
27-
#include "swift/AST/GenericSignatureBuilder.h"
26+
#include "swift/AST/GenericParamList.h"
27+
#include "swift/AST/GenericSignature.h"
2828
#include "swift/AST/Module.h"
2929
#include "swift/AST/NameLookup.h"
3030
#include "swift/AST/ParameterList.h"

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "swift/AST/DiagnosticsSIL.h"
2424
#include "swift/AST/ForeignInfo.h"
2525
#include "swift/AST/GenericEnvironment.h"
26-
#include "swift/AST/GenericSignatureBuilder.h"
2726
#include "swift/AST/Module.h"
2827
#include "swift/AST/ModuleLoader.h"
2928
#include "swift/AST/ProtocolConformance.h"

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "swift/AST/Expr.h"
3636
#include "swift/AST/ForeignErrorConvention.h"
3737
#include "swift/AST/GenericEnvironment.h"
38-
#include "swift/AST/GenericSignatureBuilder.h"
3938
#include "swift/AST/Initializer.h"
4039
#include "swift/AST/NameLookup.h"
4140
#include "swift/AST/NameLookupRequests.h"

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "swift/AST/DiagnosticsSema.h"
2020
#include "swift/AST/ExistentialLayout.h"
2121
#include "swift/AST/GenericEnvironment.h"
22-
#include "swift/AST/GenericSignatureBuilder.h"
2322
#include "swift/AST/ProtocolConformance.h"
2423
#include "swift/AST/ParameterList.h"
2524
#include "swift/AST/TypeCheckRequests.h"

lib/Sema/TypeCheckType.cpp

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "swift/AST/ExistentialLayout.h"
3030
#include "swift/AST/ForeignErrorConvention.h"
3131
#include "swift/AST/GenericEnvironment.h"
32-
#include "swift/AST/GenericSignatureBuilder.h"
3332
#include "swift/AST/NameLookup.h"
3433
#include "swift/AST/ParameterList.h"
3534
#include "swift/AST/PrettyStackTrace.h"
@@ -75,8 +74,7 @@ TypeResolution::forInterface(DeclContext *dc, TypeResolutionOptions options,
7574
HandlePlaceholderTypeReprFn placeholderHandler) {
7675
TypeResolution result(dc, TypeResolutionStage::Interface, options,
7776
unboundTyOpener, placeholderHandler);
78-
result.complete.genericSig = dc->getGenericSignatureOfContext();
79-
result.complete.builder = nullptr;
77+
result.genericSig = dc->getGenericSignatureOfContext();
8078
return result;
8179
}
8280

@@ -102,32 +100,22 @@ TypeResolution::forContextual(DeclContext *dc, GenericEnvironment *genericEnv,
102100
TypeResolution TypeResolution::withOptions(TypeResolutionOptions opts) const {
103101
TypeResolution result(dc, stage, opts, unboundTyOpener, placeholderHandler);
104102
result.genericEnv = genericEnv;
105-
result.complete = complete;
103+
result.genericSig = genericSig;
106104
return result;
107105
}
108106

109107
ASTContext &TypeResolution::getASTContext() const {
110108
return dc->getASTContext();
111109
}
112110

113-
GenericSignatureBuilder *TypeResolution::getGenericSignatureBuilder() const {
114-
assert(stage == TypeResolutionStage::Interface);
115-
if (!complete.builder) {
116-
auto genericSig = getGenericSignature();
117-
complete.builder = genericSig->getGenericSignatureBuilder();
118-
}
119-
120-
return complete.builder;
121-
}
122-
123111
GenericSignature TypeResolution::getGenericSignature() const {
124112
switch (stage) {
125113
case TypeResolutionStage::Contextual:
126114
return dc->getGenericSignatureOfContext();
127115

128116
case TypeResolutionStage::Interface:
129-
if (complete.genericSig)
130-
return complete.genericSig;
117+
if (genericSig)
118+
return genericSig;
131119

132120
return dc->getGenericSignatureOfContext();
133121

@@ -197,22 +185,13 @@ Type TypeResolution::resolveDependentMemberType(
197185
if (!genericSig)
198186
return ErrorType::get(baseTy);
199187

200-
auto builder = getGenericSignatureBuilder();
201-
auto baseEquivClass =
202-
builder->resolveEquivalenceClass(
203-
baseTy,
204-
ArchetypeResolutionKind::CompleteWellFormed);
205-
if (!baseEquivClass)
206-
return ErrorType::get(baseTy);
207-
208-
ASTContext &ctx = baseTy->getASTContext();
209-
210188
// Look for a nested type with the given name.
211-
if (auto nestedType =
212-
baseEquivClass->lookupNestedType(*builder, refIdentifier)) {
189+
if (auto nestedType = genericSig->lookupNestedType(baseTy, refIdentifier)) {
213190
// Record the type we found.
214191
ref->setValue(nestedType, nullptr);
215192
} else {
193+
ASTContext &ctx = DC->getASTContext();
194+
216195
// Resolve the base to a potential archetype.
217196
// Perform typo correction.
218197
TypoCorrectionResults corrections(ref->getNameRef(), ref->getNameLoc());
@@ -258,13 +237,6 @@ Type TypeResolution::resolveDependentMemberType(
258237
return DependentMemberType::get(baseTy, assocType);
259238
}
260239

261-
// Otherwise, the nested type comes from a concrete type,
262-
// or it's a typealias declared in protocol or protocol extension.
263-
// Substitute the base type into it.
264-
265-
// Make sure that base type didn't get replaced along the way.
266-
assert(baseTy->isTypeParameter());
267-
268240
// There are two situations possible here:
269241
//
270242
// 1. Member comes from the protocol, which means that it has been
@@ -280,9 +252,12 @@ Type TypeResolution::resolveDependentMemberType(
280252
// end up using incorrect generic signature while attempting to form
281253
// a substituted type for the member we found.
282254
if (!concrete->getDeclContext()->getSelfProtocolDecl()) {
283-
baseTy = baseEquivClass->concreteType ? baseEquivClass->concreteType
284-
: baseEquivClass->superclass;
285-
assert(baseTy);
255+
if (auto concreteTy = genericSig->getConcreteType(baseTy))
256+
baseTy = concreteTy;
257+
else {
258+
baseTy = genericSig->getSuperclassBound(baseTy);
259+
assert(baseTy);
260+
}
286261
}
287262

288263
return TypeChecker::substMemberTypeWithBase(DC->getParentModule(), concrete,
@@ -305,16 +280,12 @@ Type TypeResolution::resolveSelfAssociatedType(Type baseTy,
305280
}
306281

307282
assert(stage == TypeResolutionStage::Interface);
308-
auto builder = getGenericSignatureBuilder();
309-
auto baseEquivClass =
310-
builder->resolveEquivalenceClass(
311-
baseTy,
312-
ArchetypeResolutionKind::CompleteWellFormed);
313-
if (!baseEquivClass)
283+
auto genericSig = getGenericSignature();
284+
if (!genericSig)
314285
return ErrorType::get(baseTy);
315286

316287
// Look for a nested type with the given name.
317-
auto nestedType = baseEquivClass->lookupNestedType(*builder, name);
288+
auto nestedType = genericSig->lookupNestedType(baseTy, name);
318289
assert(nestedType);
319290

320291
// If the nested type has been resolved to an associated type, use it.
@@ -327,9 +298,12 @@ Type TypeResolution::resolveSelfAssociatedType(Type baseTy,
327298
// extension.
328299
//
329300
// Get the superclass of the 'Self' type parameter.
330-
baseTy = (baseEquivClass->concreteType
331-
? baseEquivClass->concreteType
332-
: baseEquivClass->superclass);
301+
if (auto concreteTy = genericSig->getConcreteType(baseTy))
302+
baseTy = concreteTy;
303+
else {
304+
baseTy = genericSig->getSuperclassBound(baseTy);
305+
assert(baseTy);
306+
}
333307
assert(baseTy);
334308
}
335309

@@ -925,12 +899,13 @@ Type TypeResolution::applyUnboundGenericArguments(
925899
}
926900

927901
skipRequirementsCheck |= parentTy->hasTypeVariable();
928-
} else if (auto genericEnv =
929-
decl->getDeclContext()->getGenericEnvironmentOfContext()) {
930-
auto genericSig = genericEnv->getGenericSignature();
902+
} else if (auto genericSig =
903+
decl->getDeclContext()->getGenericSignatureOfContext()) {
931904
for (auto gp : genericSig->getGenericParams()) {
932905
subs[gp->getCanonicalType()->castTo<GenericTypeParamType>()] =
933-
(usesArchetypes() ? genericEnv->mapTypeIntoContext(gp) : gp);
906+
(usesArchetypes()
907+
? genericSig->getGenericEnvironment()->mapTypeIntoContext(gp)
908+
: gp);
934909
}
935910
}
936911

lib/Sema/TypeCheckType.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class TypeRepr;
2828
class ComponentIdentTypeRepr;
2929
class GenericEnvironment;
3030
class GenericSignature;
31-
class GenericSignatureBuilder;
3231

3332
/// Flags that describe the context of type checking a pattern or
3433
/// type.
@@ -297,30 +296,20 @@ class TypeResolution {
297296
HandlePlaceholderTypeReprFn placeholderHandler;
298297

299298
private:
300-
union {
301-
/// The generic environment used to map to archetypes.
302-
GenericEnvironment *genericEnv;
299+
/// The generic environment used to map to archetypes.
300+
GenericEnvironment *genericEnv;
303301

304-
/// The generic signature
305-
struct {
306-
/// The generic signature to use for type resolution.
307-
GenericSignature genericSig;
308-
309-
/// The generic signature builder that will answer queries about
310-
/// generic types.
311-
mutable GenericSignatureBuilder *builder;
312-
} complete;
313-
};
302+
/// The generic signature to use for type resolution.
303+
GenericSignature genericSig;
314304

315305
TypeResolution(DeclContext *dc, TypeResolutionStage stage,
316306
TypeResolutionOptions options,
317307
OpenUnboundGenericTypeFn unboundTyOpener,
318308
HandlePlaceholderTypeReprFn placeholderHandler)
319309
: dc(dc), stage(stage), options(options),
320310
unboundTyOpener(unboundTyOpener),
321-
placeholderHandler(placeholderHandler) {}
322-
323-
GenericSignatureBuilder *getGenericSignatureBuilder() const;
311+
placeholderHandler(placeholderHandler),
312+
genericEnv(nullptr) {}
324313

325314
/// Retrieves the generic signature for the context, or NULL if there is
326315
/// no generic signature to resolve types.

0 commit comments

Comments
 (0)