Skip to content

Commit d417281

Browse files
authored
Merge pull request #12097 from DougGregor/gsb-nested-type-lookup
[GSB] Centralize, clean up, and cache nested type name lookup
2 parents adac80f + 4b43cbe commit d417281

17 files changed

+288
-312
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ class GenericSignatureBuilder {
215215
/// a superclass requirement.
216216
bool isConformanceSatisfiedBySuperclass(ProtocolDecl *proto) const;
217217

218+
/// Lookup a nested type with the given name within this equivalence
219+
/// class.
220+
///
221+
/// \param otherConcreteTypes If non-null, will be filled in the all of the
222+
/// concrete types we found (other than the result) with the same name.
223+
TypeDecl *lookupNestedType(
224+
Identifier name,
225+
SmallVectorImpl<TypeDecl *> *otherConcreteTypes = nullptr);
226+
218227
/// Dump a debugging representation of this equivalence class.
219228
void dump(llvm::raw_ostream &out) const;
220229

@@ -232,6 +241,17 @@ class GenericSignatureBuilder {
232241
/// anchor was cached.
233242
unsigned numMembers;
234243
} archetypeAnchorCache;
244+
245+
/// Describes a cached nested type.
246+
struct CachedNestedType {
247+
unsigned numConformancesPresent;
248+
CanType superclassPresent;
249+
llvm::TinyPtrVector<TypeDecl *> types;
250+
};
251+
252+
/// Cached nested-type information, which contains the best declaration
253+
/// for a given name.
254+
llvm::SmallDenseMap<Identifier, CachedNestedType> nestedTypeNameCache;
235255
};
236256

237257
friend class RequirementSource;
@@ -522,9 +542,6 @@ class GenericSignatureBuilder {
522542
/// \brief Add all of a generic signature's parameters and requirements.
523543
void addGenericSignature(GenericSignature *sig);
524544

525-
/// \brief Build the generic signature.
526-
GenericSignature *getGenericSignature();
527-
528545
/// Infer requirements from the given type, recursively.
529546
///
530547
/// This routine infers requirements from a type that occurs within the
@@ -558,11 +575,13 @@ class GenericSignatureBuilder {
558575
/// \brief Finalize the set of requirements and compute the generic
559576
/// signature.
560577
///
561-
/// After this point, one cannot introduce new requirements.
578+
/// After this point, one cannot introduce new requirements, and the
579+
/// generic signature builder no longer has valid state.
562580
GenericSignature *computeGenericSignature(
563581
SourceLoc loc,
564-
bool allowConcreteGenericParams = false);
582+
bool allowConcreteGenericParams = false) &&;
565583

584+
private:
566585
/// Finalize the set of requirements, performing any remaining checking
567586
/// required before generating archetypes.
568587
///
@@ -572,6 +591,7 @@ class GenericSignatureBuilder {
572591
ArrayRef<GenericTypeParamType *> genericParams,
573592
bool allowConcreteGenericParams=false);
574593

594+
public:
575595
/// Process any delayed requirements that can be handled now.
576596
void processDelayedRequirements();
577597

@@ -1490,6 +1510,12 @@ class GenericSignatureBuilder::PotentialArchetype {
14901510
return parentOrBuilder.dyn_cast<PotentialArchetype *>();
14911511
}
14921512

1513+
/// Retrieve the type declaration to which this nested type was resolved.
1514+
TypeDecl *getResolvedType() const {
1515+
assert(getParent() && "Not an associated type");
1516+
return identifier.assocTypeOrConcrete;
1517+
}
1518+
14931519
/// Retrieve the associated type to which this potential archetype
14941520
/// has been resolved.
14951521
AssociatedTypeDecl *getResolvedAssociatedType() const {
@@ -1632,13 +1658,8 @@ class GenericSignatureBuilder::PotentialArchetype {
16321658
ArchetypeResolutionKind kind,
16331659
GenericSignatureBuilder &builder);
16341660

1635-
/// \brief Retrieve (or create) a nested type with a known associated type.
1636-
PotentialArchetype *getNestedType(AssociatedTypeDecl *assocType,
1637-
GenericSignatureBuilder &builder);
1638-
1639-
/// \brief Retrieve (or create) a nested type with a known concrete type
1640-
/// declaration.
1641-
PotentialArchetype *getNestedType(TypeDecl *concreteDecl,
1661+
/// \brief Retrieve (or create) a nested type with a known type.
1662+
PotentialArchetype *getNestedType(TypeDecl *type,
16421663
GenericSignatureBuilder &builder);
16431664

16441665
/// \brief Retrieve (or create) a nested type that is the current best
@@ -1658,8 +1679,8 @@ class GenericSignatureBuilder::PotentialArchetype {
16581679
/// type or typealias of the given protocol, unless the \c kind implies that
16591680
/// a potential archetype should not be created if it's missing.
16601681
PotentialArchetype *updateNestedTypeForConformance(
1661-
PointerUnion<AssociatedTypeDecl *, TypeDecl *> type,
1662-
ArchetypeResolutionKind kind);
1682+
TypeDecl *type,
1683+
ArchetypeResolutionKind kind);
16631684

16641685
/// Update the named nested type when we know this type conforms to the given
16651686
/// protocol.

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4591,7 +4591,7 @@ CanGenericSignature ASTContext::getExistentialSignature(CanType existential,
45914591
GenericSignatureBuilder::FloatingRequirementSource::forAbstract();
45924592
builder.addRequirement(requirement, source, nullptr);
45934593

4594-
CanGenericSignature genericSig(builder.computeGenericSignature(SourceLoc()));
4594+
CanGenericSignature genericSig(std::move(builder).computeGenericSignature(SourceLoc()));
45954595

45964596
auto result = Impl.ExistentialSignatures.insert(
45974597
std::make_pair(existential, genericSig));

lib/AST/Builtins.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ namespace {
491491
Builder.addGenericParameter(gp);
492492
}
493493

494-
auto GenericSig = Builder.computeGenericSignature(SourceLoc());
494+
auto GenericSig =
495+
std::move(Builder).computeGenericSignature(SourceLoc());
495496
GenericEnv = GenericSig->createGenericEnvironment(*ctx.TheBuiltinModule);
496497
}
497498

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3419,7 +3419,8 @@ void ProtocolDecl::computeRequirementSignature() {
34193419
nullptr);
34203420

34213421
// Compute and record the signature.
3422-
auto requirementSig = builder.computeGenericSignature(SourceLoc());
3422+
auto requirementSig =
3423+
std::move(builder).computeGenericSignature(SourceLoc());
34233424
RequirementSignature = requirementSig->getRequirements().data();
34243425
assert(RequirementSignature != nullptr);
34253426
NumRequirementsInSignature = requirementSig->getRequirements().size();

0 commit comments

Comments
 (0)