Skip to content

Commit 48fc69d

Browse files
committed
AST: Refactor GSB::EquivalenceClass::getTypeInContext() into GenericEnvironment::getOrCreateArchetypeFromInterfaceType()
1 parent 2f17416 commit 48fc69d

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/GenericParamKey.h"
2222
#include "swift/AST/GenericParamList.h"
2323
#include "swift/AST/GenericSignature.h"
24+
#include "swift/AST/GenericSignatureBuilder.h"
2425
#include "swift/Basic/Compiler.h"
2526
#include "swift/Basic/Debug.h"
2627
#include "llvm/ADT/ArrayRef.h"
@@ -85,6 +86,14 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
8586

8687
friend QueryInterfaceTypeSubstitutions;
8788

89+
Type getOrCreateArchetypeFromInterfaceType(
90+
GenericSignatureBuilder::EquivalenceClass *equivClass);
91+
92+
/// Retrieve the mapping for the given generic parameter, if present.
93+
///
94+
/// This is only useful when lazily populating a generic environment.
95+
Optional<Type> getMappingIfPresent(GenericParamKey key) const;
96+
8897
public:
8998
GenericSignature getGenericSignature() const {
9099
return Signature;
@@ -101,11 +110,6 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
101110
/// an archetype)
102111
void addMapping(GenericParamKey key, Type contextType);
103112

104-
/// Retrieve the mapping for the given generic parameter, if present.
105-
///
106-
/// This is only useful when lazily populating a generic environment.
107-
Optional<Type> getMappingIfPresent(GenericParamKey key) const;
108-
109113
/// Make vanilla new/delete illegal.
110114
void *operator new(size_t Bytes) = delete;
111115
void operator delete(void *Data) = delete;

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,6 @@ class GenericSignatureBuilder {
237237
Type getAnchor(GenericSignatureBuilder &builder,
238238
TypeArrayView<GenericTypeParamType> genericParams);
239239

240-
/// Retrieve (or build) the contextual type corresponding to
241-
/// this equivalence class within the given generic environment.
242-
Type getTypeInContext(GenericSignatureBuilder &builder,
243-
GenericEnvironment *genericEnv);
244-
245240
/// Dump a debugging representation of this equivalence class,
246241
void dump(llvm::raw_ostream &out,
247242
GenericSignatureBuilder *builder = nullptr) const;

lib/AST/GenericEnvironment.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,27 @@ Type TypeBase::mapTypeOutOfContext() {
119119
}
120120

121121
Type
122-
GenericSignatureBuilder::EquivalenceClass::getTypeInContext(
123-
GenericSignatureBuilder &builder, GenericEnvironment *genericEnv) {
124-
auto genericParams = genericEnv->getGenericParams();
122+
GenericEnvironment::getOrCreateArchetypeFromInterfaceType(
123+
GenericSignatureBuilder::EquivalenceClass *equivClass) {
124+
auto genericParams = getGenericParams();
125125

126-
// The anchor descr
127-
Type anchor = getAnchor(builder, genericParams);
126+
auto &builder = *getGenericSignatureBuilder();
127+
Type anchor = equivClass->getAnchor(builder, genericParams);
128128

129129
// If this equivalence class is mapped to a concrete type, produce that
130130
// type.
131-
if (concreteType) {
132-
if (recursiveConcreteType)
131+
if (equivClass->concreteType) {
132+
if (equivClass->recursiveConcreteType)
133133
return ErrorType::get(anchor);
134134

135135
// Prevent recursive substitution.
136-
this->recursiveConcreteType = true;
136+
equivClass->recursiveConcreteType = true;
137137
SWIFT_DEFER {
138-
this->recursiveConcreteType = false;
138+
equivClass->recursiveConcreteType = false;
139139
};
140140

141-
return genericEnv->mapTypeIntoContext(concreteType,
142-
builder.getLookupConformanceFn());
141+
return mapTypeIntoContext(equivClass->concreteType,
142+
builder.getLookupConformanceFn());
143143
}
144144

145145
// Local function to check whether we have a generic parameter that has
@@ -148,7 +148,7 @@ GenericSignatureBuilder::EquivalenceClass::getTypeInContext(
148148
auto genericParam = anchor->getAs<GenericTypeParamType>();
149149
if (!genericParam) return Type();
150150

151-
auto type = genericEnv->getMappingIfPresent(genericParam);
151+
auto type = getMappingIfPresent(genericParam);
152152
if (!type) return Type();
153153

154154
// We already have a mapping for this generic parameter in the generic
@@ -169,8 +169,8 @@ GenericSignatureBuilder::EquivalenceClass::getTypeInContext(
169169

170170
// Map the parent type into this context.
171171
parentArchetype =
172-
parentEquivClass->getTypeInContext(builder, genericEnv)
173-
->castTo<ArchetypeType>();
172+
getOrCreateArchetypeFromInterfaceType(parentEquivClass)
173+
->castTo<ArchetypeType>();
174174

175175
// If we already have a nested type with this name, return it.
176176
assocType = depMemTy->getAssocType();
@@ -186,17 +186,17 @@ GenericSignatureBuilder::EquivalenceClass::getTypeInContext(
186186
}
187187

188188
// Substitute into the superclass.
189-
Type superclass = this->recursiveSuperclassType ? Type() : this->superclass;
189+
Type superclass = (equivClass->recursiveSuperclassType
190+
? Type() : equivClass->superclass);
190191
if (superclass && superclass->hasTypeParameter()) {
191192
// Prevent recursive substitution.
192-
this->recursiveSuperclassType = true;
193+
equivClass->recursiveSuperclassType = true;
193194
SWIFT_DEFER {
194-
this->recursiveSuperclassType = false;
195+
equivClass->recursiveSuperclassType = false;
195196
};
196197

197-
superclass = genericEnv->mapTypeIntoContext(
198-
superclass,
199-
builder.getLookupConformanceFn());
198+
superclass = mapTypeIntoContext(superclass,
199+
builder.getLookupConformanceFn());
200200
if (superclass->is<ErrorType>())
201201
superclass = Type();
202202

@@ -210,10 +210,10 @@ GenericSignatureBuilder::EquivalenceClass::getTypeInContext(
210210

211211
// Collect the protocol conformances for the archetype.
212212
SmallVector<ProtocolDecl *, 4> protos;
213-
for (const auto &conforms : conformsTo) {
213+
for (const auto &conforms : equivClass->conformsTo) {
214214
auto proto = conforms.first;
215215

216-
if (!isConformanceSatisfiedBySuperclass(proto))
216+
if (!equivClass->isConformanceSatisfiedBySuperclass(proto))
217217
protos.push_back(proto);
218218
}
219219

@@ -223,18 +223,20 @@ GenericSignatureBuilder::EquivalenceClass::getTypeInContext(
223223
// Create a nested archetype.
224224
auto *depMemTy = anchor->castTo<DependentMemberType>();
225225
archetype = NestedArchetypeType::getNew(ctx, parentArchetype, depMemTy,
226-
protos, superclass, layout);
226+
protos, superclass,
227+
equivClass->layout);
227228

228229
// Register this archetype with its parent.
229230
parentArchetype->registerNestedType(assocType->getName(), archetype);
230231
} else {
231232
// Create a top-level archetype.
232233
auto genericParam = anchor->castTo<GenericTypeParamType>();
233-
archetype = PrimaryArchetypeType::getNew(ctx, genericEnv, genericParam,
234-
protos, superclass, layout);
234+
archetype = PrimaryArchetypeType::getNew(ctx, this, genericParam,
235+
protos, superclass,
236+
equivClass->layout);
235237

236238
// Register the archetype with the generic environment.
237-
genericEnv->addMapping(genericParam, archetype);
239+
addMapping(genericParam, archetype);
238240
}
239241

240242
return archetype;
@@ -263,7 +265,7 @@ void ArchetypeType::resolveNestedType(
263265
result = concrete;
264266
} else {
265267
auto *equivClass = resolved.getEquivalenceClass(builder);
266-
result = equivClass->getTypeInContext(builder, genericEnv);
268+
result = genericEnv->getOrCreateArchetypeFromInterfaceType(equivClass);
267269
}
268270

269271
assert(!nested.second ||
@@ -294,7 +296,7 @@ Type QueryInterfaceTypeSubstitutions::operator()(SubstitutableType *type) const{
294296
ArchetypeResolutionKind::CompleteWellFormed);
295297

296298
auto mutableSelf = const_cast<GenericEnvironment *>(self);
297-
contextType = equivClass->getTypeInContext(*builder, mutableSelf);
299+
contextType = mutableSelf->getOrCreateArchetypeFromInterfaceType(equivClass);
298300

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

0 commit comments

Comments
 (0)