Skip to content

Commit 6cc22fe

Browse files
authored
Merge pull request #12357 from DougGregor/module-free-gsb
2 parents aac8746 + 2723946 commit 6cc22fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+269
-313
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -862,22 +862,20 @@ class ASTContext {
862862
/// generic signature builder for the given signature, if we don't already
863863
/// have one.
864864
void registerGenericSignatureBuilder(GenericSignature *sig,
865-
ModuleDecl &module,
866865
GenericSignatureBuilder &&builder);
867866
friend class GenericSignatureBuilder;
868867

869868
public:
870869
/// Retrieve or create the stored generic signature builder for the given
871870
/// canonical generic signature and module.
872-
GenericSignatureBuilder *getOrCreateGenericSignatureBuilder(CanGenericSignature sig,
873-
ModuleDecl *mod);
871+
GenericSignatureBuilder *getOrCreateGenericSignatureBuilder(
872+
CanGenericSignature sig);
874873

875874
/// Retrieve or create the canonical generic environment of a canonical
876875
/// generic signature builder.
877876
GenericEnvironment *getOrCreateCanonicalGenericEnvironment(
878877
GenericSignatureBuilder *builder,
879-
GenericSignature *sig,
880-
ModuleDecl &module);
878+
GenericSignature *sig);
881879

882880
/// Retrieve the inherited name set for the given class.
883881
const InheritedNameSet *getAllPropertyNames(ClassDecl *classDecl,

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "swift/AST/GenericParamKey.h"
2828
#include "swift/AST/IfConfigClause.h"
2929
#include "swift/AST/LayoutConstraint.h"
30-
#include "swift/AST/LazyResolver.h"
3130
#include "swift/AST/TypeAlignments.h"
3231
#include "swift/AST/TypeWalker.h"
3332
#include "swift/AST/Witness.h"
@@ -62,6 +61,7 @@ namespace swift {
6261
class GenericSignature;
6362
class GenericTypeParamDecl;
6463
class GenericTypeParamType;
64+
class LazyResolver;
6565
class ModuleDecl;
6666
class NameAliasType;
6767
class EnumCaseDecl;

include/swift/AST/GenericSignature.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
132132
ArrayRef<Requirement> requirements);
133133

134134
/// Retrieve the generic signature builder for the given generic signature.
135-
GenericSignatureBuilder *getGenericSignatureBuilder(ModuleDecl &mod);
135+
GenericSignatureBuilder *getGenericSignatureBuilder();
136136

137137
friend class ArchetypeType;
138138

@@ -256,54 +256,55 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
256256
/// Create a new generic environment that provides fresh contextual types
257257
/// (archetypes) that correspond to the interface types in this generic
258258
/// signature.
259-
GenericEnvironment *createGenericEnvironment(ModuleDecl &mod);
259+
GenericEnvironment *createGenericEnvironment();
260260

261261
/// Uniquing for the ASTContext.
262262
void Profile(llvm::FoldingSetNodeID &ID) {
263263
Profile(ID, getGenericParams(), getRequirements());
264264
}
265265

266266
/// Determine whether the given dependent type is required to be a class.
267-
bool requiresClass(Type type, ModuleDecl &mod);
267+
bool requiresClass(Type type);
268268

269269
/// Determine the superclass bound on the given dependent type.
270-
Type getSuperclassBound(Type type, ModuleDecl &mod);
270+
Type getSuperclassBound(Type type);
271271

272272
using ConformsToArray = SmallVector<ProtocolDecl *, 2>;
273273
/// Determine the set of protocols to which the given dependent type
274274
/// must conform.
275-
ConformsToArray getConformsTo(Type type, ModuleDecl &mod);
275+
ConformsToArray getConformsTo(Type type);
276276

277277
/// Determine whether the given dependent type conforms to this protocol.
278-
bool conformsToProtocol(Type type, ProtocolDecl *proto, ModuleDecl &mod);
278+
bool conformsToProtocol(Type type, ProtocolDecl *proto);
279279

280280
/// Determine whether the given dependent type is equal to a concrete type.
281-
bool isConcreteType(Type type, ModuleDecl &mod);
281+
bool isConcreteType(Type type);
282282

283283
/// Return the concrete type that the given dependent type is constrained to,
284284
/// or the null Type if it is not the subject of a concrete same-type
285285
/// constraint.
286-
Type getConcreteType(Type type, ModuleDecl &mod);
286+
Type getConcreteType(Type type);
287287

288288
/// Return the layout constraint that the given dependent type is constrained
289289
/// to, or the null LayoutConstraint if it is not the subject of layout
290290
/// constraint.
291-
LayoutConstraint getLayoutConstraint(Type type, ModuleDecl &mod);
291+
LayoutConstraint getLayoutConstraint(Type type);
292292

293293
/// Return whether two type parameters represent the same type under this
294294
/// generic signature.
295295
///
296296
/// The type parameters must be known to not be concrete within the context.
297-
bool areSameTypeParameterInContext(Type type1, Type type2, ModuleDecl &mod);
297+
bool areSameTypeParameterInContext(Type type1, Type type2);
298298

299299
/// Return the canonical version of the given type under this generic
300300
/// signature.
301-
CanType getCanonicalTypeInContext(Type type, ModuleDecl &mod);
302-
bool isCanonicalTypeInContext(Type type, ModuleDecl &mod);
301+
CanType getCanonicalTypeInContext(Type type);
302+
bool isCanonicalTypeInContext(Type type);
303303

304304
/// Return the canonical version of the given type under this generic
305305
/// signature.
306-
CanType getCanonicalTypeInContext(Type type, GenericSignatureBuilder &builder);
306+
CanType getCanonicalTypeInContext(Type type,
307+
GenericSignatureBuilder &builder);
307308
bool isCanonicalTypeInContext(Type type, GenericSignatureBuilder &builder);
308309

309310
/// Retrieve the conformance access path used to extract the conformance of
@@ -319,8 +320,7 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
319320
///
320321
/// \seealso ConformanceAccessPath
321322
ConformanceAccessPath getConformanceAccessPath(Type type,
322-
ProtocolDecl *protocol,
323-
ModuleDecl &mod);
323+
ProtocolDecl *protocol);
324324

325325
static void Profile(llvm::FoldingSetNodeID &ID,
326326
ArrayRef<GenericTypeParamType *> genericParams,

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -475,20 +475,40 @@ class GenericSignatureBuilder {
475475

476476
public:
477477
/// Construct a new generic signature builder.
478-
///
479-
/// \param lookupConformance Conformance-lookup routine that will be used
480-
/// to satisfy conformance requirements for concrete types.
481-
explicit GenericSignatureBuilder(ASTContext &ctx,
482-
std::function<GenericFunction> lookupConformance);
483-
478+
explicit GenericSignatureBuilder(ASTContext &ctx);
484479
GenericSignatureBuilder(GenericSignatureBuilder &&);
485480
~GenericSignatureBuilder();
486481

487482
/// Retrieve the AST context.
488483
ASTContext &getASTContext() const { return Context; }
489484

490-
/// Retrieve the conformance-lookup function used by this generic signature builder.
491-
std::function<GenericFunction> getLookupConformanceFn() const;
485+
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
486+
/// conformance in a generic signature builder.
487+
class LookUpConformanceInBuilder {
488+
GenericSignatureBuilder *builder;
489+
public:
490+
explicit LookUpConformanceInBuilder(GenericSignatureBuilder *builder)
491+
: builder(builder) {}
492+
493+
Optional<ProtocolConformanceRef>
494+
operator()(CanType dependentType,
495+
Type conformingReplacementType,
496+
ProtocolType *conformedProtocol) const {
497+
return builder->lookupConformance(dependentType,
498+
conformingReplacementType,
499+
conformedProtocol);
500+
}
501+
};
502+
503+
/// Retrieve a function that can perform conformance lookup for this
504+
/// builder.
505+
LookUpConformanceInBuilder getLookupConformanceFn();
506+
507+
/// Lookup a protocol conformance in a module-agnostic manner.
508+
Optional<ProtocolConformanceRef>
509+
lookupConformance(CanType dependentType, Type conformingReplacementType,
510+
ProtocolType *conformedProtocol);
511+
492512

493513
/// Retrieve the lazy resolver, if there is one.
494514
LazyResolver *getLazyResolver() const;
@@ -593,7 +613,6 @@ class GenericSignatureBuilder {
593613
/// After this point, one cannot introduce new requirements, and the
594614
/// generic signature builder no longer has valid state.
595615
GenericSignature *computeGenericSignature(
596-
ModuleDecl &module,
597616
SourceLoc loc,
598617
bool allowConcreteGenericParams = false,
599618
bool allowBuilderToMove = true) &&;

include/swift/AST/LazyResolver.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_AST_LAZYRESOLVER_H
1818
#define SWIFT_AST_LAZYRESOLVER_H
1919

20+
#include "swift/AST/ProtocolConformanceRef.h"
2021
#include "swift/AST/TypeLoc.h"
2122
#include "llvm/ADT/PointerEmbeddedInt.h"
2223

@@ -102,6 +103,10 @@ class LazyResolver {
102103
/// is usable for the given type.
103104
virtual bool isProtocolExtensionUsable(DeclContext *dc, Type type,
104105
ExtensionDecl *protocolExtension) = 0;
106+
107+
/// Mark the given conformance as "used" from the given declaration context.
108+
virtual void markConformanceUsed(ProtocolConformanceRef conformance,
109+
DeclContext *dc) = 0;
105110
};
106111

107112
/// An implementation of LazyResolver that delegates to another.
@@ -171,6 +176,11 @@ class DelegatingLazyResolver : public LazyResolver {
171176
ExtensionDecl *protocolExtension) override {
172177
return Principal.isProtocolExtensionUsable(dc, type, protocolExtension);
173178
}
179+
180+
void markConformanceUsed(ProtocolConformanceRef conformance,
181+
DeclContext *dc) override {
182+
return Principal.markConformanceUsed(conformance, dc);
183+
}
174184
};
175185

176186
class LazyMemberLoader;

include/swift/AST/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ class CanGenericSignature {
577577

578578
/// Retrieve the canonical generic environment associated with this
579579
/// generic signature.
580-
GenericEnvironment *getGenericEnvironment(ModuleDecl &module) const;
580+
GenericEnvironment *getGenericEnvironment() const;
581581

582582
GenericSignature *operator->() const {
583583
return Signature;

include/swift/AST/Types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
393393

394394
/// getCanonicalType - Stronger canonicalization which folds away equivalent
395395
/// associated types, or type parameters that have been made concrete.
396-
CanType getCanonicalType(GenericSignature *sig, ModuleDecl &mod);
396+
CanType getCanonicalType(GenericSignature *sig);
397397

398398
/// Reconstitute type sugar, e.g., for array types, dictionary
399399
/// types, optionals, etc.
@@ -3523,7 +3523,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
35233523
/// If this is a @convention(witness_method) function with a protocol
35243524
/// constrained self parameter, return the protocol constraint for
35253525
/// the Self type.
3526-
ProtocolDecl *getDefaultWitnessMethodProtocol(ModuleDecl &M) const;
3526+
ProtocolDecl *getDefaultWitnessMethodProtocol() const;
35273527

35283528
/// If this is a @convention(witness_method) function with a class
35293529
/// constrained self parameter, return the class constraint for the

include/swift/SIL/AbstractionPattern.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,14 @@ class AbstractionPattern {
705705

706706
/// Is this an interface type that is subject to a concrete
707707
/// same-type constraint?
708-
bool isConcreteType(ModuleDecl &module) const {
708+
bool isConcreteType() const {
709709
assert(isTypeParameter());
710710
return (getKind() != Kind::Opaque &&
711711
GenericSig != nullptr &&
712-
GenericSig->isConcreteType(getType(), module));
712+
GenericSig->isConcreteType(getType()));
713713
}
714714

715-
bool requiresClass(ModuleDecl &module) {
715+
bool requiresClass() {
716716
switch (getKind()) {
717717
case Kind::Opaque:
718718
return false;
@@ -725,7 +725,7 @@ class AbstractionPattern {
725725
isa<GenericTypeParamType>(type)) {
726726
assert(GenericSig &&
727727
"Dependent type in pattern without generic signature?");
728-
return GenericSig->requiresClass(type, module);
728+
return GenericSig->requiresClass(type);
729729
}
730730
return false;
731731
}

lib/AST/ASTContext.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
252252
llvm::DenseMap<const DeclContext *, LazyContextData *> LazyContexts;
253253

254254
/// Stored generic signature builders for canonical generic signatures.
255-
llvm::DenseMap<std::pair<GenericSignature *, ModuleDecl *>,
256-
std::unique_ptr<GenericSignatureBuilder>>
255+
llvm::DenseMap<GenericSignature *, std::unique_ptr<GenericSignatureBuilder>>
257256
GenericSignatureBuilders;
258257

259258
/// Canonical generic environments for canonical generic signatures.
@@ -1436,35 +1435,32 @@ void ASTContext::getVisibleTopLevelClangModules(
14361435

14371436
void ASTContext::registerGenericSignatureBuilder(
14381437
GenericSignature *sig,
1439-
ModuleDecl &module,
14401438
GenericSignatureBuilder &&builder) {
14411439
auto canSig = sig->getCanonicalSignature();
1442-
auto known = Impl.GenericSignatureBuilders.find({canSig, &module});
1440+
auto known = Impl.GenericSignatureBuilders.find(canSig);
14431441
if (known != Impl.GenericSignatureBuilders.end()) {
14441442
++NumRegisteredGenericSignatureBuildersAlready;
14451443
return;
14461444
}
14471445

14481446
++NumRegisteredGenericSignatureBuilders;
1449-
Impl.GenericSignatureBuilders[{canSig, &module}] =
1447+
Impl.GenericSignatureBuilders[canSig] =
14501448
llvm::make_unique<GenericSignatureBuilder>(std::move(builder));
14511449
}
14521450

14531451
GenericSignatureBuilder *ASTContext::getOrCreateGenericSignatureBuilder(
1454-
CanGenericSignature sig,
1455-
ModuleDecl *mod) {
1452+
CanGenericSignature sig) {
14561453
// Check whether we already have a generic signature builder for this
14571454
// signature and module.
1458-
auto known = Impl.GenericSignatureBuilders.find({sig, mod});
1455+
auto known = Impl.GenericSignatureBuilders.find(sig);
14591456
if (known != Impl.GenericSignatureBuilders.end())
14601457
return known->second.get();
14611458

14621459
// Create a new generic signature builder with the given signature.
1463-
auto builder =
1464-
new GenericSignatureBuilder(*this, LookUpConformanceInModule(mod));
1460+
auto builder = new GenericSignatureBuilder(*this);
14651461

14661462
// Store this generic signature builder (no generic environment yet).
1467-
Impl.GenericSignatureBuilders[{sig, mod}] =
1463+
Impl.GenericSignatureBuilders[sig] =
14681464
std::unique_ptr<GenericSignatureBuilder>(builder);
14691465

14701466
builder->addGenericSignature(sig);
@@ -1534,13 +1530,12 @@ GenericSignatureBuilder *ASTContext::getOrCreateGenericSignatureBuilder(
15341530

15351531
GenericEnvironment *ASTContext::getOrCreateCanonicalGenericEnvironment(
15361532
GenericSignatureBuilder *builder,
1537-
GenericSignature *sig,
1538-
ModuleDecl &module) {
1533+
GenericSignature *sig) {
15391534
auto known = Impl.CanonicalGenericEnvironments.find(builder);
15401535
if (known != Impl.CanonicalGenericEnvironments.end())
15411536
return known->second;
15421537

1543-
auto env = sig->createGenericEnvironment(module);
1538+
auto env = sig->createGenericEnvironment();
15441539
Impl.CanonicalGenericEnvironments[builder] = env;
15451540
return env;
15461541
}
@@ -3637,12 +3632,10 @@ GenericFunctionType::get(GenericSignature *sig,
36373632
// it's canonical. Unfortunately, isCanonicalTypeInContext can cause
36383633
// new GenericFunctionTypes to be created and thus invalidate our insertion
36393634
// point.
3640-
auto &moduleForCanonicality = *ctx.TheBuiltinModule;
36413635
bool isCanonical = sig->isCanonical()
36423636
&& isCanonicalFunctionInputType(input)
3643-
&& sig->isCanonicalTypeInContext(unwrapParenType(input),
3644-
moduleForCanonicality)
3645-
&& sig->isCanonicalTypeInContext(output, moduleForCanonicality);
3637+
&& sig->isCanonicalTypeInContext(unwrapParenType(input))
3638+
&& sig->isCanonicalTypeInContext(output);
36463639

36473640
if (auto result
36483641
= ctx.Impl.GenericFunctionTypes.FindNodeOrInsertPos(id, insertPos)) {
@@ -4725,7 +4718,7 @@ CanGenericSignature ASTContext::getExistentialSignature(CanType existential,
47254718

47264719
assert(existential.isExistentialType());
47274720

4728-
GenericSignatureBuilder builder(*this, LookUpConformanceInModule(mod));
4721+
GenericSignatureBuilder builder(*this);
47294722

47304723
auto genericParam = GenericTypeParamType::get(0, 0, *this);
47314724
builder.addGenericParameter(genericParam);
@@ -4736,7 +4729,7 @@ CanGenericSignature ASTContext::getExistentialSignature(CanType existential,
47364729
GenericSignatureBuilder::FloatingRequirementSource::forAbstract();
47374730
builder.addRequirement(requirement, source, nullptr);
47384731

4739-
CanGenericSignature genericSig(std::move(builder).computeGenericSignature(*mod, SourceLoc()));
4732+
CanGenericSignature genericSig(std::move(builder).computeGenericSignature(SourceLoc()));
47404733

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

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,8 +1680,8 @@ void ASTMangler::appendAssociatedTypeName(DependentMemberType *dmt) {
16801680
// dependent type, but can't yet. Shouldn't need this side channel.
16811681

16821682
appendIdentifier(assocTy->getName().str());
1683-
if (!OptimizeProtocolNames || !CurGenericSignature || !Mod
1684-
|| CurGenericSignature->getConformsTo(dmt->getBase(), *Mod).size() > 1) {
1683+
if (!OptimizeProtocolNames || !CurGenericSignature
1684+
|| CurGenericSignature->getConformsTo(dmt->getBase()).size() > 1) {
16851685
appendAnyGenericType(assocTy->getProtocol());
16861686
}
16871687
}

lib/AST/ASTScope.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Decl.h"
2121
#include "swift/AST/Expr.h"
2222
#include "swift/AST/Initializer.h"
23+
#include "swift/AST/LazyResolver.h"
2324
#include "swift/AST/Module.h"
2425
#include "swift/AST/ParameterList.h"
2526
#include "swift/AST/Pattern.h"

0 commit comments

Comments
 (0)