Skip to content

Commit 2608be7

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-next
2 parents 5f63524 + be609e8 commit 2608be7

File tree

8 files changed

+183
-64
lines changed

8 files changed

+183
-64
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717
SWIFT_TYPEID_NAMED(NominalTypeDecl *, NominalTypeDecl)
1818
SWIFT_TYPEID_NAMED(VarDecl *, VarDecl)
19+
SWIFT_TYPEID_NAMED(ValueDecl *, ValueDecl)
1920
SWIFT_TYPEID_NAMED(Decl *, Decl)
2021
SWIFT_TYPEID(Type)
2122
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfo)

include/swift/IDE/IDERequestIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
//===----------------------------------------------------------------------===//
1717
SWIFT_TYPEID(CursorInfoRequest)
1818
SWIFT_TYPEID(RangeInfoRequest)
19+
SWIFT_TYPEID(ProvideDefaultImplForRequest)
20+
SWIFT_TYPEID(CollectOverriddenDeclsRequest)

include/swift/IDE/IDERequests.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,95 @@ class RangeInfoRequest:
140140
SourceLoc getNearestLoc() const;
141141
};
142142

143+
//----------------------------------------------------------------------------//
144+
// ProvideDefaultImplForRequest
145+
//----------------------------------------------------------------------------//
146+
/// Collect all the protocol requirements that a given declaration can
147+
/// provide default implementations for. Input is a declaration in extension
148+
/// declaration. The result is an array of requirements.
149+
class ProvideDefaultImplForRequest:
150+
public SimpleRequest<ProvideDefaultImplForRequest,
151+
ArrayRef<ValueDecl*>(ValueDecl*),
152+
CacheKind::Cached>
153+
{
154+
public:
155+
using SimpleRequest::SimpleRequest;
156+
157+
private:
158+
friend SimpleRequest;
159+
160+
// Evaluation.
161+
llvm::Expected<ArrayRef<ValueDecl*>> evaluate(Evaluator &evaluator,
162+
ValueDecl* VD) const;
163+
164+
public:
165+
// Caching
166+
bool isCached() const { return true; }
167+
// Source location
168+
SourceLoc getNearestLoc() const { return SourceLoc(); };
169+
};
170+
171+
//----------------------------------------------------------------------------//
172+
// CollectOverriddenDeclsRequest
173+
//----------------------------------------------------------------------------//
174+
struct OverridenDeclsOwner {
175+
ValueDecl *VD;
176+
bool IncludeProtocolRequirements;
177+
bool Transitive;
178+
179+
OverridenDeclsOwner(ValueDecl *VD, bool IncludeProtocolRequirements,
180+
bool Transitive): VD(VD),
181+
IncludeProtocolRequirements(IncludeProtocolRequirements),
182+
Transitive(Transitive) {}
183+
184+
friend llvm::hash_code hash_value(const OverridenDeclsOwner &CI) {
185+
return hash_combine(hash_value(CI.VD),
186+
hash_value(CI.IncludeProtocolRequirements),
187+
hash_value(CI.Transitive));
188+
}
189+
190+
friend bool operator==(const OverridenDeclsOwner &lhs,
191+
const OverridenDeclsOwner &rhs) {
192+
return lhs.VD == rhs.VD &&
193+
lhs.IncludeProtocolRequirements == rhs.IncludeProtocolRequirements &&
194+
lhs.Transitive == rhs.Transitive;
195+
}
196+
197+
friend bool operator!=(const OverridenDeclsOwner &lhs,
198+
const OverridenDeclsOwner &rhs) {
199+
return !(lhs == rhs);
200+
}
201+
202+
friend void simple_display(llvm::raw_ostream &out,
203+
const OverridenDeclsOwner &owner) {
204+
simple_display(out, owner.VD);
205+
}
206+
};
207+
208+
/// Get decls that the given decl overrides, protocol requirements that
209+
/// it serves as a default implementation of, and optionally protocol
210+
/// requirements it satisfies in a conforming class
211+
class CollectOverriddenDeclsRequest:
212+
public SimpleRequest<CollectOverriddenDeclsRequest,
213+
ArrayRef<ValueDecl*>(OverridenDeclsOwner),
214+
CacheKind::Cached> {
215+
public:
216+
using SimpleRequest::SimpleRequest;
217+
218+
private:
219+
friend SimpleRequest;
220+
221+
// Evaluation.
222+
llvm::Expected<ArrayRef<ValueDecl*>> evaluate(Evaluator &evaluator,
223+
OverridenDeclsOwner Owner) const;
224+
225+
public:
226+
// Caching
227+
bool isCached() const { return true; }
228+
// Source location
229+
SourceLoc getNearestLoc() const { return SourceLoc(); };
230+
};
231+
143232
/// The zone number for the IDE.
144233
#define SWIFT_IDE_REQUESTS_TYPEID_ZONE 137
145234
#define SWIFT_TYPEID_ZONE SWIFT_IDE_REQUESTS_TYPEID_ZONE

include/swift/Sema/IDETypeChecking.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,12 @@ namespace swift {
248248
///
249249
/// \returns the slice of Scratch
250250
ArrayRef<ValueDecl*>
251-
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
252-
llvm::SmallVectorImpl<ValueDecl*> &Scratch);
251+
canDeclProvideDefaultImplementationFor(ValueDecl* VD);
253252

254253
/// Get decls that the given decl overrides, protocol requirements that
255254
/// it serves as a default implementation of, and optionally protocol
256255
/// requirements it satisfies in a conforming class
257-
std::vector<ValueDecl*>
256+
ArrayRef<ValueDecl*>
258257
collectAllOverriddenDecls(ValueDecl *VD,
259258
bool IncludeProtocolRequirements = true,
260259
bool Transitive = false);

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
189189
Diagnostics));
190190
registerTypeCheckerRequestFunctions(Context->evaluator);
191191

192+
// Migrator and indexing need some IDE requests.
193+
if (Invocation.getMigratorOptions().shouldRunMigrator() ||
194+
!Invocation.getFrontendOptions().IndexStorePath.empty()) {
195+
registerIDERequestFunctions(Context->evaluator);
196+
}
192197
if (setUpModuleLoaders())
193198
return true;
194199

lib/IDE/IDERequests.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
1919
#include "swift/IDE/CommentConversion.h"
2020
#include "swift/IDE/Utils.h"
21+
#include "swift/Sema/IDETypeChecking.h"
2122
#include "swift/Markup/XMLUtils.h"
2223
#include "swift/Subsystems.h"
2324
#include "swift/IDE/IDERequests.h"
@@ -1073,3 +1074,72 @@ void
10731074
swift::ide::simple_display(llvm::raw_ostream &out, const ResolvedRangeInfo &info) {
10741075
info.print(out);
10751076
}
1077+
1078+
//----------------------------------------------------------------------------//
1079+
// ProvideDefaultImplForRequest
1080+
//----------------------------------------------------------------------------//
1081+
static Type getContextFreeInterfaceType(ValueDecl *VD) {
1082+
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
1083+
return AFD->getMethodInterfaceType();
1084+
}
1085+
return VD->getInterfaceType();
1086+
}
1087+
1088+
llvm::Expected<ArrayRef<ValueDecl*>>
1089+
ProvideDefaultImplForRequest::evaluate(Evaluator &eval, ValueDecl* VD) const {
1090+
// Skip decls that don't have valid names.
1091+
if (!VD->getFullName())
1092+
return ArrayRef<ValueDecl*>();
1093+
1094+
// Check if VD is from a protocol extension.
1095+
auto P = VD->getDeclContext()->getExtendedProtocolDecl();
1096+
if (!P)
1097+
return ArrayRef<ValueDecl*>();
1098+
SmallVector<ValueDecl*, 8> Results;
1099+
// Look up all decls in the protocol's inheritance chain for the ones with
1100+
// the same name with VD.
1101+
ResolvedMemberResult LookupResult =
1102+
resolveValueMember(*P->getInnermostDeclContext(),
1103+
P->getDeclaredInterfaceType(), VD->getFullName());
1104+
1105+
auto VDType = getContextFreeInterfaceType(VD);
1106+
for (auto Mem : LookupResult.getMemberDecls(InterestedMemberKind::All)) {
1107+
if (isa<ProtocolDecl>(Mem->getDeclContext())) {
1108+
if (Mem->isProtocolRequirement() &&
1109+
getContextFreeInterfaceType(Mem)->isEqual(VDType)) {
1110+
// We find a protocol requirement VD can provide default
1111+
// implementation for.
1112+
Results.push_back(Mem);
1113+
}
1114+
}
1115+
}
1116+
return copyToContext(VD->getASTContext(), llvm::makeArrayRef(Results));
1117+
}
1118+
1119+
//----------------------------------------------------------------------------//
1120+
// CollectOverriddenDeclsRequest
1121+
//----------------------------------------------------------------------------//
1122+
llvm::Expected<ArrayRef<ValueDecl*>>
1123+
CollectOverriddenDeclsRequest::evaluate(Evaluator &evaluator,
1124+
OverridenDeclsOwner Owner) const {
1125+
std::vector<ValueDecl*> results;
1126+
auto *VD = Owner.VD;
1127+
if (auto Overridden = VD->getOverriddenDecl()) {
1128+
results.push_back(Overridden);
1129+
while (Owner.Transitive && (Overridden = Overridden->getOverriddenDecl()))
1130+
results.push_back(Overridden);
1131+
}
1132+
1133+
for (auto Req : evaluateOrDefault(evaluator, ProvideDefaultImplForRequest(VD),
1134+
ArrayRef<ValueDecl*>())) {
1135+
results.push_back(Req);
1136+
}
1137+
1138+
if (Owner.IncludeProtocolRequirements) {
1139+
for (auto Satisfied : VD->getSatisfiedProtocolRequirements()) {
1140+
results.push_back(Satisfied);
1141+
}
1142+
}
1143+
1144+
return copyToContext(VD->getASTContext(), llvm::makeArrayRef(results));
1145+
}

lib/IDE/IDETypeChecking.cpp

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/ProtocolConformance.h"
2626
#include "swift/Sema/IDETypeChecking.h"
2727
#include "swift/IDE/SourceEntityWalker.h"
28+
#include "swift/IDE/IDERequests.h"
2829
#include "swift/Parse/Lexer.h"
2930

3031
using namespace swift;
@@ -741,68 +742,17 @@ swift::collectExpressionType(SourceFile &SF,
741742
return Scratch;
742743
}
743744

744-
static Type getContextFreeInterfaceType(ValueDecl *VD) {
745-
if (auto AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
746-
return AFD->getMethodInterfaceType();
747-
}
748-
return VD->getInterfaceType();
749-
}
750-
751745
ArrayRef<ValueDecl*> swift::
752-
canDeclProvideDefaultImplementationFor(ValueDecl* VD,
753-
llvm::SmallVectorImpl<ValueDecl*> &Scratch) {
754-
755-
// Skip decls that don't have valid names.
756-
if (!VD->getFullName())
757-
return {};
758-
759-
// Check if VD is from a protocol extension.
760-
auto P = VD->getDeclContext()->getExtendedProtocolDecl();
761-
if (!P)
762-
return {};
763-
764-
// Look up all decls in the protocol's inheritance chain for the ones with
765-
// the same name with VD.
766-
ResolvedMemberResult LookupResult =
767-
resolveValueMember(*P->getInnermostDeclContext(),
768-
P->getDeclaredInterfaceType(), VD->getFullName());
769-
770-
auto VDType = getContextFreeInterfaceType(VD);
771-
for (auto Mem : LookupResult.getMemberDecls(InterestedMemberKind::All)) {
772-
if (isa<ProtocolDecl>(Mem->getDeclContext())) {
773-
if (Mem->isProtocolRequirement() &&
774-
getContextFreeInterfaceType(Mem)->isEqual(VDType)) {
775-
// We find a protocol requirement VD can provide default
776-
// implementation for.
777-
Scratch.push_back(Mem);
778-
}
779-
}
780-
}
781-
return Scratch;
746+
canDeclProvideDefaultImplementationFor(ValueDecl* VD) {
747+
return evaluateOrDefault(VD->getASTContext().evaluator,
748+
ProvideDefaultImplForRequest(VD),
749+
ArrayRef<ValueDecl*>());
782750
}
783751

784-
std::vector<ValueDecl*> swift::
752+
ArrayRef<ValueDecl*> swift::
785753
collectAllOverriddenDecls(ValueDecl *VD, bool IncludeProtocolRequirements,
786-
bool Transitive) {
787-
std::vector<ValueDecl*> results;
788-
789-
if (auto Overridden = VD->getOverriddenDecl()) {
790-
results.push_back(Overridden);
791-
while (Transitive && (Overridden = Overridden->getOverriddenDecl()))
792-
results.push_back(Overridden);
793-
}
794-
795-
// Collect the protocol requirements this decl is a default impl for
796-
llvm::SmallVector<ValueDecl*, 2> Buffer;
797-
for (auto Req : canDeclProvideDefaultImplementationFor(VD, Buffer)) {
798-
results.push_back(Req);
799-
}
800-
801-
if (IncludeProtocolRequirements) {
802-
for (auto Satisfied : VD->getSatisfiedProtocolRequirements()) {
803-
results.push_back(Satisfied);
804-
}
805-
}
806-
807-
return results;
754+
bool Transitive) {
755+
return evaluateOrDefault(VD->getASTContext().evaluator,
756+
CollectOverriddenDeclsRequest(OverridenDeclsOwner(VD,
757+
IncludeProtocolRequirements, Transitive)), ArrayRef<ValueDecl*>());
808758
}

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
274274
if (IsModuleIndexing) {
275275
if (CI.setup(Invocation))
276276
return;
277+
// Indexing needs IDE requests
278+
registerIDERequestFunctions(CI.getASTContext().evaluator);
277279
bool IsClangModule = (FileExt == ".pcm");
278280
if (IsClangModule) {
279281
IdxConsumer.failed("Clang module files are not supported");
@@ -292,7 +294,8 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
292294

293295
if (CI.setup(Invocation))
294296
return;
295-
297+
// Indexing needs IDE requests
298+
registerIDERequestFunctions(CI.getASTContext().evaluator);
296299
trace::TracedOperation TracedOp(trace::OperationKind::IndexSource);
297300
if (TracedOp.enabled()) {
298301
trace::SwiftInvocation SwiftArgs;

0 commit comments

Comments
 (0)