Move canDeclProvideDefaultImplementationFor() from libIDE into libIndex

This commit is contained in:
Nathan Hawes
2017-04-27 23:23:45 -07:00
parent 8ad6aa4e0d
commit 4ff254de97
6 changed files with 83 additions and 58 deletions

View File

@@ -20,14 +20,6 @@ class ModuleDecl;
class SourceFile; class SourceFile;
class DeclContext; 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<ValueDecl*>
getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements = true,
bool Transitive = false);
namespace index { namespace index {
void indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer); void indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer);

View File

@@ -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<ValueDecl*>
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
llvm::SmallVectorImpl<ValueDecl*> &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<ValueDecl*>
getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements = true,
bool Transitive = false);
} // end namespace swift
#endif // SWIFT_INDEX_UTILS_H

View File

@@ -49,15 +49,6 @@ namespace swift {
void collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD, void collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD,
llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &DefaultMap); llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &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<ValueDecl*> canDeclProvideDefaultImplementationFor(ValueDecl* VD,
llvm::SmallVectorImpl<ValueDecl*> &Scratch);
/// \brief Given an unresolved member E and its parent P, this function tries /// \brief Given an unresolved member E and its parent P, this function tries
/// to infer the type of E. /// to infer the type of E.
/// \returns true on success, false on error. /// \returns true on success, false on error.

View File

@@ -18,46 +18,6 @@
using namespace swift; using namespace swift;
static Type getContextFreeInterfaceType(ValueDecl *VD) {
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
return AFD->getMethodInterfaceType();
}
return VD->getInterfaceType();
}
ArrayRef<ValueDecl*> swift::
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
llvm::SmallVectorImpl<ValueDecl*> &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<ProtocolDecl>(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:: void swift::
collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD, collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD,
llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &DefaultMap) { llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &DefaultMap) {

View File

@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/Index/Index.h" #include "swift/Index/Index.h"
#include "swift/Index/Utils.h"
#include "swift/AST/ASTContext.h" #include "swift/AST/ASTContext.h"
#include "swift/AST/Comment.h" #include "swift/AST/Comment.h"
@@ -1379,6 +1380,46 @@ void IndexSwiftASTWalker::getModuleHash(SourceFileOrModule Mod,
OS << llvm::APInt(64, code).toString(36, /*Signed=*/false); OS << llvm::APInt(64, code).toString(36, /*Signed=*/false);
} }
static Type getContextFreeInterfaceType(ValueDecl *VD) {
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
return AFD->getMethodInterfaceType();
}
return VD->getInterfaceType();
}
ArrayRef<ValueDecl*> swift::
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
llvm::SmallVectorImpl<ValueDecl*> &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<ProtocolDecl>(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<ValueDecl*> swift:: std::vector<ValueDecl*> swift::
getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements, getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements,
bool Transitive) { bool Transitive) {

View File

@@ -14,7 +14,7 @@
#include "swift/AST/ASTVisitor.h" #include "swift/AST/ASTVisitor.h"
#include "swift/Frontend/Frontend.h" #include "swift/Frontend/Frontend.h"
#include "swift/IDE/Utils.h" #include "swift/IDE/Utils.h"
#include "swift/Index/Index.h" #include "swift/Index/Utils.h"
#include "swift/Migrator/EditorAdapter.h" #include "swift/Migrator/EditorAdapter.h"
#include "swift/Migrator/FixitApplyDiagnosticConsumer.h" #include "swift/Migrator/FixitApplyDiagnosticConsumer.h"
#include "swift/Migrator/Migrator.h" #include "swift/Migrator/Migrator.h"