Skip to content

Commit f07cde7

Browse files
committed
AST: Move mapTypeOutOfContext() from GenericEnvironment to TypeBase
It doesn't actually *use* the generic environment.
1 parent 28c8fb8 commit f07cde7

18 files changed

+65
-91
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class SILType;
3737

3838
/// Describes the mapping between archetypes and interface types for the
3939
/// generic parameters of a DeclContext.
40+
///
41+
/// The most frequently used method here is mapTypeIntoContext(), which
42+
/// maps an interface type to a type written in terms of the generic
43+
/// environment's archetypes; to go in the other direction, use
44+
/// TypeBase::mapTypeOutOfContext().
45+
///
4046
class alignas(1 << DeclAlignInBits) GenericEnvironment final
4147
: private llvm::TrailingObjects<GenericEnvironment, Type> {
4248
GenericSignature *Signature = nullptr;
@@ -126,13 +132,6 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
126132
static Type mapTypeIntoContext(GenericEnvironment *genericEnv,
127133
Type type);
128134

129-
/// Map a contextual type to an interface type.
130-
static Type mapTypeOutOfContext(GenericEnvironment *genericEnv,
131-
Type type);
132-
133-
/// Map a contextual type to an interface type.
134-
Type mapTypeOutOfContext(Type type) const;
135-
136135
/// Map an interface type to a contextual type.
137136
Type mapTypeIntoContext(Type type) const;
138137

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
524524
/// underlying type.
525525
Type eraseDynamicSelfType();
526526

527+
/// Map a contextual type to an interface type.
528+
Type mapTypeOutOfContext();
529+
527530
/// \brief Compute and return the set of type variables that occur within this
528531
/// type.
529532
///

lib/AST/DeclContext.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ Type DeclContext::mapTypeIntoContext(Type type) const {
306306
}
307307

308308
Type DeclContext::mapTypeOutOfContext(Type type) const {
309-
return GenericEnvironment::mapTypeOutOfContext(
310-
getGenericEnvironmentOfContext(), type);
309+
return type->mapTypeOutOfContext();
311310
}
312311

313312
DeclContext *DeclContext::getLocalContext() {

lib/AST/GenericEnvironment.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,13 @@ Type GenericEnvironment::mapTypeIntoContext(GenericEnvironment *env,
120120
return env->mapTypeIntoContext(type);
121121
}
122122

123-
Type
124-
GenericEnvironment::mapTypeOutOfContext(GenericEnvironment *env,
125-
Type type) {
126-
assert(!type->hasTypeParameter() && "already have an interface type");
127-
128-
if (!env)
129-
return type.substDependentTypesWithErrorTypes();
130-
131-
return env->mapTypeOutOfContext(type);
132-
}
133-
134-
Type GenericEnvironment::mapTypeOutOfContext(Type type) const {
135-
type = type.subst([&](SubstitutableType *t) -> Type {
136-
return cast<ArchetypeType>(t)->getInterfaceType();
137-
},
138-
MakeAbstractConformanceForGenericType(),
139-
SubstFlags::AllowLoweredTypes);
140-
assert(!type->hasArchetype() && "not fully substituted");
141-
return type;
123+
Type TypeBase::mapTypeOutOfContext() {
124+
assert(!hasTypeParameter() && "already have an interface type");
125+
return Type(this).subst([&](SubstitutableType *t) -> Type {
126+
return cast<ArchetypeType>(t)->getInterfaceType();
127+
},
128+
MakeAbstractConformanceForGenericType(),
129+
SubstFlags::AllowLoweredTypes);
142130
}
143131

144132
Type GenericEnvironment::QueryInterfaceTypeSubstitutions::operator()(

lib/IRGen/GenHeap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ class FixedBoxTypeInfoBase : public BoxTypeInfo {
15301530
auto boxedInterfaceType = boxedType;
15311531
if (env) {
15321532
boxedInterfaceType = SILType::getPrimitiveType(
1533-
env->mapTypeOutOfContext(boxedType.getSwiftRValueType())
1533+
boxedType.getSwiftRValueType()->mapTypeOutOfContext()
15341534
->getCanonicalType(),
15351535
boxedType.getCategory());
15361536
}

lib/IRGen/GenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ ArchetypeType *TypeConverter::getExemplarArchetype(ArchetypeType *t) {
13581358

13591359
// Map the archetype out of its own generic environment and into the
13601360
// canonical generic environment.
1361-
auto interfaceType = genericEnv->mapTypeOutOfContext(t);
1361+
auto interfaceType = t->getInterfaceType();
13621362
auto exemplar = canGenericEnv->mapTypeIntoContext(interfaceType)
13631363
->castTo<ArchetypeType>();
13641364
assert(isExemplarArchetype(exemplar));

lib/ParseSIL/ParseSIL.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ void SILParser::convertRequirements(SILFunction *F,
842842
Ty.getTypeRepr()->walk(PerformLookup);
843843
performTypeLocChecking(Ty, /* IsSIL */ false);
844844
assert(Ty.getType());
845-
return GenericEnv->mapTypeOutOfContext(Ty.getType()->getCanonicalType());
845+
return Ty.getType()->mapTypeOutOfContext();
846846
};
847847

848848
for (auto &Req : From) {
@@ -1049,9 +1049,12 @@ bool SILParser::parseASTType(CanType &result, GenericEnvironment *env) {
10491049
TypeLoc loc = parsedType.get();
10501050
if (performTypeLocChecking(loc, /*IsSILType=*/ false, env))
10511051
return true;
1052-
result = loc.getType()->getCanonicalType();
1052+
10531053
if (env)
1054-
result = env->mapTypeOutOfContext(result)->getCanonicalType();
1054+
result = loc.getType()->mapTypeOutOfContext()->getCanonicalType();
1055+
else
1056+
result = loc.getType()->getCanonicalType();
1057+
10551058
// Invoke the callback on the parsed type.
10561059
ParsedTypeCallback(loc.getType());
10571060
return false;
@@ -2615,8 +2618,7 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
26152618

26162619
if (patternEnv)
26172620
loweredTy = SILType::getPrimitiveType(
2618-
patternEnv
2619-
->mapTypeOutOfContext(loweredTy.getSwiftRValueType())
2621+
loweredTy.getSwiftRValueType()->mapTypeOutOfContext()
26202622
->getCanonicalType(),
26212623
loweredTy.getCategory());
26222624

lib/SIL/SILFunction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ SILType GenericEnvironment::mapTypeIntoContext(SILModule &M,
205205
}
206206

207207
Type SILFunction::mapTypeOutOfContext(Type type) const {
208-
return GenericEnvironment::mapTypeOutOfContext(
209-
getGenericEnvironment(), type);
208+
return type->mapTypeOutOfContext();
210209
}
211210

212211
bool SILFunction::isNoReturnFunction() const {

lib/SIL/SILFunctionType.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,8 @@ static CanSILFunctionType getSILFunctionType(
881881
for (auto capture : loweredCaptures.getCaptures()) {
882882
if (capture.isDynamicSelfMetadata()) {
883883
ParameterConvention convention = ParameterConvention::Direct_Unowned;
884-
auto dynamicSelfInterfaceType = GenericEnvironment::mapTypeOutOfContext(
885-
function->getGenericEnvironment(),
886-
loweredCaptures.getDynamicSelfType());
884+
auto dynamicSelfInterfaceType =
885+
loweredCaptures.getDynamicSelfType()->mapTypeOutOfContext();
887886

888887
auto selfMetatype = MetatypeType::get(
889888
dynamicSelfInterfaceType,

lib/SIL/TypeLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,7 @@ TypeConverter::getContextBoxTypeForCapture(ValueDecl *captured,
25022502
auto homeSig = captured->getDeclContext()
25032503
->getGenericSignatureOfContext();
25042504
loweredInterfaceType =
2505-
env->mapTypeOutOfContext(loweredInterfaceType)
2505+
loweredInterfaceType->mapTypeOutOfContext()
25062506
->getCanonicalType(homeSig);
25072507
}
25082508

0 commit comments

Comments
 (0)