Skip to content

Commit bcde656

Browse files
committed
[AST] Introduce DeclContext::mapType(Into|OutOf)Context()
Use them to eliminate some more instances of getSelfTypeInContext().
1 parent 7370b6c commit bcde656

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

include/swift/AST/DeclContext.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,13 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
320320
/// \brief Retrieve the innermost archetypes of this context or any
321321
/// of its parents.
322322
GenericEnvironment *getGenericEnvironmentOfContext() const;
323-
323+
324+
/// Map an interface type to a contextual type within this context.
325+
Type mapTypeIntoContext(Type type) const;
326+
327+
/// Map a type within this context to an interface type.
328+
Type mapTypeOutOfContext(Type type) const;
329+
324330
/// Returns this or the first local parent context, or nullptr if it is not
325331
/// contained in one.
326332
DeclContext *getLocalContext();

lib/AST/DeclContext.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ GenericEnvironment *DeclContext::getGenericEnvironmentOfContext() const {
301301
}
302302
}
303303

304+
Type DeclContext::mapTypeIntoContext(Type type) const {
305+
if (auto genericEnv = getGenericEnvironmentOfContext())
306+
return genericEnv->mapTypeIntoContext(getParentModule(), type);
307+
308+
return type;
309+
}
310+
311+
Type DeclContext::mapTypeOutOfContext(Type type) const {
312+
if (auto genericEnv = getGenericEnvironmentOfContext())
313+
return genericEnv->mapTypeOutOfContext(getParentModule(), type);
314+
315+
return type;
316+
}
317+
304318
DeclContext *DeclContext::getLocalContext() {
305319
if (isLocalContext())
306320
return this;

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,8 +1789,8 @@ void swift::maybeAddAccessorsToVariable(VarDecl *var, TypeChecker &TC) {
17891789
Type behaviorSelf;
17901790
Type behaviorInterfaceSelf;
17911791
if (dc->isTypeContext()) {
1792-
behaviorSelf = dc->getSelfTypeInContext();
17931792
behaviorInterfaceSelf = dc->getSelfInterfaceType();
1793+
behaviorSelf = dc->mapTypeIntoContext(behaviorInterfaceSelf);
17941794
assert(behaviorSelf && "type context doesn't have self type?!");
17951795
if (var->isStatic())
17961796
behaviorSelf = MetatypeType::get(behaviorSelf);

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,8 +1260,10 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
12601260
} else if (auto *VD = dyn_cast<ValueDecl>(D)) {
12611261
auto *DC = VD->getDeclContext();
12621262
Type selfTy;
1263-
if (DC->isTypeContext())
1264-
selfTy = DC->getSelfTypeInContext();
1263+
if (DC->isTypeContext()) {
1264+
selfTy = DC->getSelfInterfaceType();
1265+
selfTy = VD->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
1266+
}
12651267
bool Failed =
12661268
passCursorInfoForDecl(VD, MainModule, selfTy, Type(),
12671269
/*isRef=*/false, BufferID, Lang, CompInvok,

0 commit comments

Comments
 (0)