Skip to content

Commit fd2fe85

Browse files
authored
Merge pull request swiftlang#12959 from slavapestov/map-type-out-of-context-cleanup
Move mapTypeOutOfContext() from GenericEnvironment to TypeBase
2 parents 28c8fb8 + 510c412 commit fd2fe85

Some content is hidden

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

42 files changed

+112
-169
lines changed

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
323323
/// Map an interface type to a contextual type within this context.
324324
Type mapTypeIntoContext(Type type) const;
325325

326-
/// Map a type within this context to an interface type.
327-
Type mapTypeOutOfContext(Type type) const;
328-
329326
/// Returns this or the first local parent context, or nullptr if it is not
330327
/// contained in one.
331328
DeclContext *getLocalContext();

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
///

include/swift/SIL/SILFunction.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,6 @@ class SILFunction
648648
/// SILFunction.
649649
SILType mapTypeIntoContext(SILType type) const;
650650

651-
/// Map the given type, which is based on a contextual SILFunctionType and may
652-
/// therefore contain context archetypes, to an interface type.
653-
Type mapTypeOutOfContext(Type type) const;
654-
655651
/// Converts the given function definition to a declaration.
656652
void convertToDeclaration();
657653

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ void ASTMangler::appendClosureComponents(Type Ty, unsigned discriminator,
17801780
if (!Ty)
17811781
Ty = ErrorType::get(localContext->getASTContext());
17821782

1783-
Ty = parentContext->mapTypeOutOfContext(Ty);
1783+
Ty = Ty->mapTypeOutOfContext();
17841784
appendType(Ty->getCanonicalType());
17851785
appendOperator(isImplicit ? "fu" : "fU", Index(discriminator));
17861786
}
@@ -1978,9 +1978,7 @@ void ASTMangler::appendProtocolConformance(const ProtocolConformance *conformanc
19781978
appendProtocolName(conformance->getProtocol());
19791979
appendIdentifier(behaviorStorage->getBaseName().getIdentifier().str());
19801980
} else {
1981-
auto conformanceDC = conformance->getDeclContext();
1982-
auto conformingType =
1983-
conformanceDC->mapTypeOutOfContext(conformance->getType());
1981+
auto conformingType = conformance->getType()->mapTypeOutOfContext();
19841982
appendType(conformingType->getCanonicalType());
19851983
appendProtocolName(conformance->getProtocol());
19861984
appendModule(conformance->getDeclContext()->getParentModule());

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
487487
if (T->hasArchetype()) {
488488
// Get the interface type, since TypeLocs still have
489489
// contextual types in them.
490-
T = Current->getInnermostDeclContext()->mapTypeOutOfContext(T);
490+
T = T->mapTypeOutOfContext();
491491
}
492492

493493
auto *M = Current->getDeclContext()->getParentModule();

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,7 @@ void TypeAliasDecl::setUnderlyingType(Type underlying) {
25032503
// lldb creates global typealiases containing archetypes
25042504
// sometimes...
25052505
if (underlying->hasArchetype() && isGenericContext())
2506-
underlying = mapTypeOutOfContext(underlying);
2506+
underlying = underlying->mapTypeOutOfContext();
25072507
UnderlyingTy.setType(underlying);
25082508

25092509
// FIXME -- if we already have an interface type, we're changing the
@@ -5185,7 +5185,7 @@ bool EnumElementDecl::computeType() {
51855185

51865186
// The type of the enum element is either (T) -> T or (T) -> ArgType -> T.
51875187
if (auto inputTy = getArgumentTypeLoc().getType()) {
5188-
resultTy = FunctionType::get(ED->mapTypeOutOfContext(inputTy), resultTy);
5188+
resultTy = FunctionType::get(inputTy->mapTypeOutOfContext(), resultTy);
51895189
}
51905190

51915191
if (auto *genericSig = ED->getGenericSignatureOfContext())

lib/AST/DeclContext.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,6 @@ Type DeclContext::mapTypeIntoContext(Type type) const {
305305
getGenericEnvironmentOfContext(), type);
306306
}
307307

308-
Type DeclContext::mapTypeOutOfContext(Type type) const {
309-
return GenericEnvironment::mapTypeOutOfContext(
310-
getGenericEnvironmentOfContext(), type);
311-
}
312-
313308
DeclContext *DeclContext::getLocalContext() {
314309
if (isLocalContext())
315310
return this;

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/ClangImporter/ImportType.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ Type ClangImporter::Implementation::importMethodType(
21142114
bodyName,
21152115
swiftParamTy,
21162116
ImportedHeaderUnit);
2117-
paramInfo->setInterfaceType(dc->mapTypeOutOfContext(swiftParamTy));
2117+
paramInfo->setInterfaceType(swiftParamTy->mapTypeOutOfContext());
21182118

21192119
// Determine whether we have a default argument.
21202120
if (kind == SpecialMethodKind::Regular ||
@@ -2178,7 +2178,7 @@ Type ClangImporter::Implementation::importMethodType(
21782178

21792179
// Form the function type.
21802180
return FunctionType::get((*bodyParams)->getInterfaceType(SwiftContext),
2181-
dc->mapTypeOutOfContext(swiftResultTy), extInfo);
2181+
swiftResultTy->mapTypeOutOfContext(), extInfo);
21822182
}
21832183

21842184
Type ClangImporter::Implementation::importAccessorMethodType(
@@ -2218,7 +2218,7 @@ Type ClangImporter::Implementation::importAccessorMethodType(
22182218
Type resultTy;
22192219
if (isGetter) {
22202220
*params = ParameterList::createEmpty(SwiftContext);
2221-
resultTy = dc->mapTypeOutOfContext(propertyTy);
2221+
resultTy = propertyTy->mapTypeOutOfContext();
22222222
} else {
22232223
const clang::ParmVarDecl *param = clangDecl->parameters().front();
22242224
ImportedName fullBodyName = importFullName(param, CurrentVersion);
@@ -2233,7 +2233,7 @@ Type ClangImporter::Implementation::importAccessorMethodType(
22332233
argLabel, nameLoc, bodyName,
22342234
propertyTy,
22352235
/*dummy DC*/ImportedHeaderUnit);
2236-
paramInfo->setInterfaceType(dc->mapTypeOutOfContext(propertyTy));
2236+
paramInfo->setInterfaceType(propertyTy->mapTypeOutOfContext());
22372237

22382238
*params = ParameterList::create(SwiftContext, paramInfo);
22392239
resultTy = SwiftContext.getVoidDecl()->getDeclaredInterfaceType();

0 commit comments

Comments
 (0)