diff --git a/include/swift/Index/Index.h b/include/swift/Index/Index.h index 9bc94631090..2079d547fac 100644 --- a/include/swift/Index/Index.h +++ b/include/swift/Index/Index.h @@ -20,14 +20,6 @@ class ModuleDecl; class SourceFile; class DeclContext; -// Get decls that the given decl overrides, protocol requirements that it -// serves as a default implementation of, and optionally (as it is more -// expensive) protocol requirements it satisfies in a conforming class -std::vector -getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements = true, - bool Transitive = false); - - namespace index { void indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer); diff --git a/include/swift/Index/Utils.h b/include/swift/Index/Utils.h new file mode 100644 index 00000000000..0069489a07f --- /dev/null +++ b/include/swift/Index/Utils.h @@ -0,0 +1,41 @@ +//===--- Utils.h - Index utilities that are generally useful ----*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#ifndef SWIFT_INDEX_UTILS_H +#define SWIFT_INDEX_UTILS_H + +#include "swift/Basic/LLVM.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" + +namespace swift { +class ValueDecl; + +/// \brief Collect all the protocol requirements that a given declaration can +/// provide default implementations for. VD is a declaration in extension +/// declaration. Scratch is the buffer to collect those protocol +/// requirements. +/// +/// \returns the slice of Scratch +ArrayRef +canDeclProvideDefaultImplementationFor(ValueDecl* VD, + llvm::SmallVectorImpl &Scratch); + +/// \brief Get decls that the given decl overrides, protocol requirements that +/// it serves as a default implementation of, and optionally protocol +/// requirements it satisfies in a conforming class +std::vector +getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements = true, + bool Transitive = false); + +} // end namespace swift +#endif // SWIFT_INDEX_UTILS_H diff --git a/include/swift/Sema/IDETypeChecking.h b/include/swift/Sema/IDETypeChecking.h index f23017fecab..43f82e27847 100644 --- a/include/swift/Sema/IDETypeChecking.h +++ b/include/swift/Sema/IDETypeChecking.h @@ -49,15 +49,6 @@ namespace swift { void collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD, llvm::SmallDenseMap &DefaultMap); - /// \brief Collect all the protocol requirements that a given declaration can - /// provide default implementations for. VD is a declaration in extension - /// declaration. Scratch is the buffer to collect those protocol - /// requirements. - /// - /// \returns the slice of Scratch - ArrayRef canDeclProvideDefaultImplementationFor(ValueDecl* VD, - llvm::SmallVectorImpl &Scratch); - /// \brief Given an unresolved member E and its parent P, this function tries /// to infer the type of E. /// \returns true on success, false on error. diff --git a/lib/IDE/IDETypeChecking.cpp b/lib/IDE/IDETypeChecking.cpp index c5783e62427..765744ba99c 100644 --- a/lib/IDE/IDETypeChecking.cpp +++ b/lib/IDE/IDETypeChecking.cpp @@ -18,46 +18,6 @@ using namespace swift; -static Type getContextFreeInterfaceType(ValueDecl *VD) { - if (auto AFD = dyn_cast(VD)) { - return AFD->getMethodInterfaceType(); - } - return VD->getInterfaceType(); -} - -ArrayRef swift:: -canDeclProvideDefaultImplementationFor(ValueDecl* VD, - llvm::SmallVectorImpl &Scratch) { - - // Skip decls that don't have valid names. - if (!VD->getFullName()) - return {}; - - // Check if VD is from a protocol extension. - auto P = VD->getDeclContext()->getAsProtocolExtensionContext(); - if (!P) - return {}; - - // Look up all decls in the protocol's inheritance chain for the ones with - // the same name with VD. - ResolvedMemberResult LookupResult = - resolveValueMember(*P->getInnermostDeclContext(), - P->getDeclaredInterfaceType(), VD->getFullName()); - - auto VDType = getContextFreeInterfaceType(VD); - for (auto Mem : LookupResult.getMemberDecls(InterestedMemberKind::All)) { - if (isa(Mem->getDeclContext())) { - if (Mem->isProtocolRequirement() && - getContextFreeInterfaceType(Mem)->isEqual(VDType)) { - // We find a protocol requirement VD can provide default - // implementation for. - Scratch.push_back(Mem); - } - } - } - return Scratch; -} - void swift:: collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD, llvm::SmallDenseMap &DefaultMap) { diff --git a/lib/Index/Index.cpp b/lib/Index/Index.cpp index 64944b03ca3..5478fd6f651 100644 --- a/lib/Index/Index.cpp +++ b/lib/Index/Index.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "swift/Index/Index.h" +#include "swift/Index/Utils.h" #include "swift/AST/ASTContext.h" #include "swift/AST/Comment.h" @@ -1379,6 +1380,46 @@ void IndexSwiftASTWalker::getModuleHash(SourceFileOrModule Mod, OS << llvm::APInt(64, code).toString(36, /*Signed=*/false); } +static Type getContextFreeInterfaceType(ValueDecl *VD) { + if (auto AFD = dyn_cast(VD)) { + return AFD->getMethodInterfaceType(); + } + return VD->getInterfaceType(); +} + +ArrayRef swift:: +canDeclProvideDefaultImplementationFor(ValueDecl* VD, + llvm::SmallVectorImpl &Scratch) { + + // Skip decls that don't have valid names. + if (!VD->getFullName()) + return {}; + + // Check if VD is from a protocol extension. + auto P = VD->getDeclContext()->getAsProtocolExtensionContext(); + if (!P) + return {}; + + // Look up all decls in the protocol's inheritance chain for the ones with + // the same name with VD. + ResolvedMemberResult LookupResult = + resolveValueMember(*P->getInnermostDeclContext(), + P->getDeclaredInterfaceType(), VD->getFullName()); + + auto VDType = getContextFreeInterfaceType(VD); + for (auto Mem : LookupResult.getMemberDecls(InterestedMemberKind::All)) { + if (isa(Mem->getDeclContext())) { + if (Mem->isProtocolRequirement() && + getContextFreeInterfaceType(Mem)->isEqual(VDType)) { + // We find a protocol requirement VD can provide default + // implementation for. + Scratch.push_back(Mem); + } + } + } + return Scratch; +} + std::vector swift:: getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements, bool Transitive) { diff --git a/lib/Migrator/SyntacticMigratorPass.cpp b/lib/Migrator/SyntacticMigratorPass.cpp index d892a7c5196..f14bf37bc40 100644 --- a/lib/Migrator/SyntacticMigratorPass.cpp +++ b/lib/Migrator/SyntacticMigratorPass.cpp @@ -14,7 +14,7 @@ #include "swift/AST/ASTVisitor.h" #include "swift/Frontend/Frontend.h" #include "swift/IDE/Utils.h" -#include "swift/Index/Index.h" +#include "swift/Index/Utils.h" #include "swift/Migrator/EditorAdapter.h" #include "swift/Migrator/FixitApplyDiagnosticConsumer.h" #include "swift/Migrator/Migrator.h"