Skip to content

Commit 4ff254d

Browse files
author
Nathan Hawes
committed
Move canDeclProvideDefaultImplementationFor() from libIDE into libIndex
1 parent 8ad6aa4 commit 4ff254d

File tree

6 files changed

+83
-58
lines changed

6 files changed

+83
-58
lines changed

include/swift/Index/Index.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ class ModuleDecl;
2020
class SourceFile;
2121
class DeclContext;
2222

23-
// Get decls that the given decl overrides, protocol requirements that it
24-
// serves as a default implementation of, and optionally (as it is more
25-
// expensive) protocol requirements it satisfies in a conforming class
26-
std::vector<ValueDecl*>
27-
getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements = true,
28-
bool Transitive = false);
29-
30-
3123
namespace index {
3224

3325
void indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer);

include/swift/Index/Utils.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- Utils.h - Index utilities that are generally useful ----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_INDEX_UTILS_H
14+
#define SWIFT_INDEX_UTILS_H
15+
16+
#include "swift/Basic/LLVM.h"
17+
#include "llvm/ADT/ArrayRef.h"
18+
#include "llvm/ADT/SmallVector.h"
19+
20+
namespace swift {
21+
class ValueDecl;
22+
23+
/// \brief Collect all the protocol requirements that a given declaration can
24+
/// provide default implementations for. VD is a declaration in extension
25+
/// declaration. Scratch is the buffer to collect those protocol
26+
/// requirements.
27+
///
28+
/// \returns the slice of Scratch
29+
ArrayRef<ValueDecl*>
30+
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
31+
llvm::SmallVectorImpl<ValueDecl*> &Scratch);
32+
33+
/// \brief Get decls that the given decl overrides, protocol requirements that
34+
/// it serves as a default implementation of, and optionally protocol
35+
/// requirements it satisfies in a conforming class
36+
std::vector<ValueDecl*>
37+
getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements = true,
38+
bool Transitive = false);
39+
40+
} // end namespace swift
41+
#endif // SWIFT_INDEX_UTILS_H

include/swift/Sema/IDETypeChecking.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@ namespace swift {
4949
void collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD,
5050
llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &DefaultMap);
5151

52-
/// \brief Collect all the protocol requirements that a given declaration can
53-
/// provide default implementations for. VD is a declaration in extension
54-
/// declaration. Scratch is the buffer to collect those protocol
55-
/// requirements.
56-
///
57-
/// \returns the slice of Scratch
58-
ArrayRef<ValueDecl*> canDeclProvideDefaultImplementationFor(ValueDecl* VD,
59-
llvm::SmallVectorImpl<ValueDecl*> &Scratch);
60-
6152
/// \brief Given an unresolved member E and its parent P, this function tries
6253
/// to infer the type of E.
6354
/// \returns true on success, false on error.

lib/IDE/IDETypeChecking.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,6 @@
1818

1919
using namespace swift;
2020

21-
static Type getContextFreeInterfaceType(ValueDecl *VD) {
22-
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
23-
return AFD->getMethodInterfaceType();
24-
}
25-
return VD->getInterfaceType();
26-
}
27-
28-
ArrayRef<ValueDecl*> swift::
29-
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
30-
llvm::SmallVectorImpl<ValueDecl*> &Scratch) {
31-
32-
// Skip decls that don't have valid names.
33-
if (!VD->getFullName())
34-
return {};
35-
36-
// Check if VD is from a protocol extension.
37-
auto P = VD->getDeclContext()->getAsProtocolExtensionContext();
38-
if (!P)
39-
return {};
40-
41-
// Look up all decls in the protocol's inheritance chain for the ones with
42-
// the same name with VD.
43-
ResolvedMemberResult LookupResult =
44-
resolveValueMember(*P->getInnermostDeclContext(),
45-
P->getDeclaredInterfaceType(), VD->getFullName());
46-
47-
auto VDType = getContextFreeInterfaceType(VD);
48-
for (auto Mem : LookupResult.getMemberDecls(InterestedMemberKind::All)) {
49-
if (isa<ProtocolDecl>(Mem->getDeclContext())) {
50-
if (Mem->isProtocolRequirement() &&
51-
getContextFreeInterfaceType(Mem)->isEqual(VDType)) {
52-
// We find a protocol requirement VD can provide default
53-
// implementation for.
54-
Scratch.push_back(Mem);
55-
}
56-
}
57-
}
58-
return Scratch;
59-
}
60-
6121
void swift::
6222
collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD,
6323
llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &DefaultMap) {

lib/Index/Index.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Index/Index.h"
14+
#include "swift/Index/Utils.h"
1415

1516
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/Comment.h"
@@ -1379,6 +1380,46 @@ void IndexSwiftASTWalker::getModuleHash(SourceFileOrModule Mod,
13791380
OS << llvm::APInt(64, code).toString(36, /*Signed=*/false);
13801381
}
13811382

1383+
static Type getContextFreeInterfaceType(ValueDecl *VD) {
1384+
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
1385+
return AFD->getMethodInterfaceType();
1386+
}
1387+
return VD->getInterfaceType();
1388+
}
1389+
1390+
ArrayRef<ValueDecl*> swift::
1391+
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
1392+
llvm::SmallVectorImpl<ValueDecl*> &Scratch) {
1393+
1394+
// Skip decls that don't have valid names.
1395+
if (!VD->getFullName())
1396+
return {};
1397+
1398+
// Check if VD is from a protocol extension.
1399+
auto P = VD->getDeclContext()->getAsProtocolExtensionContext();
1400+
if (!P)
1401+
return {};
1402+
1403+
// Look up all decls in the protocol's inheritance chain for the ones with
1404+
// the same name with VD.
1405+
ResolvedMemberResult LookupResult =
1406+
resolveValueMember(*P->getInnermostDeclContext(),
1407+
P->getDeclaredInterfaceType(), VD->getFullName());
1408+
1409+
auto VDType = getContextFreeInterfaceType(VD);
1410+
for (auto Mem : LookupResult.getMemberDecls(InterestedMemberKind::All)) {
1411+
if (isa<ProtocolDecl>(Mem->getDeclContext())) {
1412+
if (Mem->isProtocolRequirement() &&
1413+
getContextFreeInterfaceType(Mem)->isEqual(VDType)) {
1414+
// We find a protocol requirement VD can provide default
1415+
// implementation for.
1416+
Scratch.push_back(Mem);
1417+
}
1418+
}
1419+
}
1420+
return Scratch;
1421+
}
1422+
13821423
std::vector<ValueDecl*> swift::
13831424
getOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements,
13841425
bool Transitive) {

lib/Migrator/SyntacticMigratorPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "swift/AST/ASTVisitor.h"
1515
#include "swift/Frontend/Frontend.h"
1616
#include "swift/IDE/Utils.h"
17-
#include "swift/Index/Index.h"
17+
#include "swift/Index/Utils.h"
1818
#include "swift/Migrator/EditorAdapter.h"
1919
#include "swift/Migrator/FixitApplyDiagnosticConsumer.h"
2020
#include "swift/Migrator/Migrator.h"

0 commit comments

Comments
 (0)