Skip to content

Commit

Permalink
[AST] Introduce DeclContext::mapType(Into|OutOf)Context()
Browse files Browse the repository at this point in the history
Use them to eliminate some more instances of getSelfTypeInContext().
  • Loading branch information
DougGregor committed Dec 2, 2016
1 parent 7370b6c commit bcde656
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
8 changes: 7 additions & 1 deletion include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,13 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
/// \brief Retrieve the innermost archetypes of this context or any
/// of its parents.
GenericEnvironment *getGenericEnvironmentOfContext() const;


/// Map an interface type to a contextual type within this context.
Type mapTypeIntoContext(Type type) const;

/// Map a type within this context to an interface type.
Type mapTypeOutOfContext(Type type) const;

/// Returns this or the first local parent context, or nullptr if it is not
/// contained in one.
DeclContext *getLocalContext();
Expand Down
14 changes: 14 additions & 0 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ GenericEnvironment *DeclContext::getGenericEnvironmentOfContext() const {
}
}

Type DeclContext::mapTypeIntoContext(Type type) const {
if (auto genericEnv = getGenericEnvironmentOfContext())
return genericEnv->mapTypeIntoContext(getParentModule(), type);

return type;
}

Type DeclContext::mapTypeOutOfContext(Type type) const {
if (auto genericEnv = getGenericEnvironmentOfContext())
return genericEnv->mapTypeOutOfContext(getParentModule(), type);

return type;
}

DeclContext *DeclContext::getLocalContext() {
if (isLocalContext())
return this;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,8 +1789,8 @@ void swift::maybeAddAccessorsToVariable(VarDecl *var, TypeChecker &TC) {
Type behaviorSelf;
Type behaviorInterfaceSelf;
if (dc->isTypeContext()) {
behaviorSelf = dc->getSelfTypeInContext();
behaviorInterfaceSelf = dc->getSelfInterfaceType();
behaviorSelf = dc->mapTypeIntoContext(behaviorInterfaceSelf);
assert(behaviorSelf && "type context doesn't have self type?!");
if (var->isStatic())
behaviorSelf = MetatypeType::get(behaviorSelf);
Expand Down
6 changes: 4 additions & 2 deletions tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,10 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
} else if (auto *VD = dyn_cast<ValueDecl>(D)) {
auto *DC = VD->getDeclContext();
Type selfTy;
if (DC->isTypeContext())
selfTy = DC->getSelfTypeInContext();
if (DC->isTypeContext()) {
selfTy = DC->getSelfInterfaceType();
selfTy = VD->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
}
bool Failed =
passCursorInfoForDecl(VD, MainModule, selfTy, Type(),
/*isRef=*/false, BufferID, Lang, CompInvok,
Expand Down

0 comments on commit bcde656

Please sign in to comment.