Skip to content

Commit 323e52e

Browse files
committed
AST: Lazy construct GenericSignatureBuilder in a GenericEnvironment
1 parent 6a11410 commit 323e52e

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,12 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
7676
/// generic signature.
7777
ArrayRef<Type> getContextTypes() const;
7878

79-
GenericEnvironment(GenericSignature signature,
80-
GenericSignatureBuilder *builder);
79+
explicit GenericEnvironment(GenericSignature signature);
8180

8281
friend ArchetypeType;
8382
friend GenericSignatureBuilder;
8483

85-
GenericSignatureBuilder *getGenericSignatureBuilder() const { return Builder; }
84+
GenericSignatureBuilder *getGenericSignatureBuilder() const;
8685

8786
friend QueryInterfaceTypeSubstitutions;
8887

@@ -96,8 +95,7 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
9695
/// Create a new, "incomplete" generic environment that will be populated
9796
/// by calls to \c addMapping().
9897
static
99-
GenericEnvironment *getIncomplete(GenericSignature signature,
100-
GenericSignatureBuilder *builder);
98+
GenericEnvironment *getIncomplete(GenericSignature signature);
10199

102100
/// Add a mapping of a generic parameter to a specific type (which may be
103101
/// an archetype)

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4165,8 +4165,7 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
41654165

41664166
// Create a generic environment and bind the opaque archetype to the
41674167
// opaque interface type from the decl's signature.
4168-
auto *builder = signature->getGenericSignatureBuilder();
4169-
auto *env = GenericEnvironment::getIncomplete(signature, builder);
4168+
auto *env = GenericEnvironment::getIncomplete(signature);
41704169
env->addMapping(GenericParamKey(opaqueInterfaceTy), newOpaque);
41714170
newOpaque->Environment = env;
41724171

@@ -4243,8 +4242,7 @@ GenericEnvironment *OpenedArchetypeType::getGenericEnvironment() const {
42434242
auto &ctx = thisType->getASTContext();
42444243
// Create a generic environment to represent the opened type.
42454244
auto signature = ctx.getOpenedArchetypeSignature(Opened);
4246-
auto *builder = signature->getGenericSignatureBuilder();
4247-
auto *env = GenericEnvironment::getIncomplete(signature, builder);
4245+
auto *env = GenericEnvironment::getIncomplete(signature);
42484246
env->addMapping(signature->getGenericParams()[0], thisType);
42494247
Environment = env;
42504248

@@ -4399,15 +4397,14 @@ GenericSignature::get(TypeArrayView<GenericTypeParamType> params,
43994397
}
44004398

44014399
GenericEnvironment *GenericEnvironment::getIncomplete(
4402-
GenericSignature signature,
4403-
GenericSignatureBuilder *builder) {
4400+
GenericSignature signature) {
44044401
auto &ctx = signature->getASTContext();
44054402

44064403
// Allocate and construct the new environment.
44074404
unsigned numGenericParams = signature->getGenericParams().size();
44084405
size_t bytes = totalSizeToAlloc<Type>(numGenericParams);
44094406
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));
4410-
return new (mem) GenericEnvironment(signature, builder);
4407+
return new (mem) GenericEnvironment(signature);
44114408
}
44124409

44134410
void DeclName::CompoundDeclName::Profile(llvm::FoldingSetNodeID &id,

lib/AST/GenericEnvironment.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,24 @@ GenericEnvironment::getGenericParams() const {
4646
return Signature->getGenericParams();
4747
}
4848

49-
GenericEnvironment::GenericEnvironment(GenericSignature signature,
50-
GenericSignatureBuilder *builder)
51-
: Signature(signature), Builder(builder)
49+
GenericEnvironment::GenericEnvironment(GenericSignature signature)
50+
: Signature(signature)
5251
{
5352
// Clear out the memory that holds the context types.
5453
std::uninitialized_fill(getContextTypes().begin(), getContextTypes().end(),
5554
Type());
5655
}
5756

57+
GenericSignatureBuilder *GenericEnvironment::getGenericSignatureBuilder() const {
58+
if (Builder)
59+
return Builder;
60+
61+
const_cast<GenericEnvironment *>(this)->Builder
62+
= Signature->getGenericSignatureBuilder();
63+
64+
return Builder;
65+
}
66+
5867
void GenericEnvironment::addMapping(GenericParamKey key,
5968
Type contextType) {
6069
// Find the index into the parallel arrays of generic parameters and
@@ -122,15 +131,14 @@ Type QueryInterfaceTypeSubstitutions::operator()(SubstitutableType *type) const{
122131
// If the context type isn't already known, lazily create it.
123132
Type contextType = self->getContextTypes()[index];
124133
if (!contextType) {
125-
assert(self->Builder &&"Missing generic signature builder for lazy query");
134+
auto *builder = self->getGenericSignatureBuilder();
126135
auto equivClass =
127-
self->Builder->resolveEquivalenceClass(
136+
builder->resolveEquivalenceClass(
128137
type,
129138
ArchetypeResolutionKind::CompleteWellFormed);
130139

131140
auto mutableSelf = const_cast<GenericEnvironment *>(self);
132-
contextType = equivClass->getTypeInContext(*mutableSelf->Builder,
133-
mutableSelf);
141+
contextType = equivClass->getTypeInContext(*builder, mutableSelf);
134142

135143
// FIXME: Redundant mapping from key -> index.
136144
if (self->getContextTypes()[index].isNull())

lib/AST/GenericSignature.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,8 @@ CanGenericSignature GenericSignatureImpl::getCanonicalSignature() const {
247247

248248
GenericEnvironment *GenericSignatureImpl::getGenericEnvironment() const {
249249
if (GenericEnv == nullptr) {
250-
auto *builder = getGenericSignatureBuilder();
251250
const auto impl = const_cast<GenericSignatureImpl *>(this);
252-
impl->GenericEnv = GenericEnvironment::getIncomplete(this, builder);
251+
impl->GenericEnv = GenericEnvironment::getIncomplete(this);
253252
}
254253

255254
return GenericEnv;

0 commit comments

Comments
 (0)