Skip to content

Commit 8915cf8

Browse files
committed
Sourcekit/Evaluator: refactor cursor-info resolver to using the evaluator model. NFC
This change adds a new IDE request ID zone and refactors the cursor-info resolver to use the request evaluator model.
1 parent b28b44b commit 8915cf8

File tree

13 files changed

+506
-276
lines changed

13 files changed

+506
-276
lines changed

include/swift/Basic/Statistics.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ FRONTEND_STATISTIC(Sema, NumUnloadedLazyIterableDeclContexts)
227227
#include "swift/AST/AccessTypeIDZone.def"
228228
#include "swift/AST/NameLookupTypeIDZone.def"
229229
#include "swift/AST/TypeCheckerTypeIDZone.def"
230+
#include "swift/IDE/IDERequestIDZone.def"
230231
#undef SWIFT_TYPEID
231232

232233
/// The next 10 statistics count 5 kinds of SIL entities present
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-------- IDERequestIDZone.def ------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
// This definition file describes the types in the IDE requests
14+
// TypeID zone, for use with the TypeID template.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
SWIFT_TYPEID(CursorInfoRequest)

include/swift/IDE/IDERequests.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===----- IDERequests.h - IDE functionality Requests -----------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
// This file defines IDE request using the evaluator model.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
#ifndef SWIFT_IDE_REQUESTS_H
17+
#define SWIFT_IDE_REQUESTS_H
18+
19+
#include "swift/AST/ASTTypeIDs.h"
20+
#include "swift/AST/Evaluator.h"
21+
#include "swift/AST/SimpleRequest.h"
22+
#include "swift/IDE/Utils.h"
23+
24+
namespace swift {
25+
26+
// Input for CursorInfoRequest.
27+
// Putting the source file and location together allows us to print the request
28+
// input well e.g. file.swift:3:4
29+
struct CursorInfoOwner {
30+
SourceFile *File;
31+
SourceLoc Loc;
32+
33+
CursorInfoOwner(SourceFile *File, SourceLoc Loc): File(File), Loc(Loc) { }
34+
35+
friend llvm::hash_code hash_value(const CursorInfoOwner &CI) {
36+
return hash_combine(hash_value(CI.File),
37+
hash_value(CI.Loc.getOpaquePointerValue()));
38+
}
39+
40+
friend bool operator==(const CursorInfoOwner &lhs, const CursorInfoOwner &rhs) {
41+
return lhs.File == rhs.File &&
42+
lhs.Loc.getOpaquePointerValue() == rhs.Loc.getOpaquePointerValue();
43+
}
44+
45+
friend bool operator!=(const CursorInfoOwner &lhs, const CursorInfoOwner &rhs) {
46+
return !(lhs == rhs);
47+
}
48+
bool isValid() const {
49+
return File && File->getBufferID() && Loc.isValid();
50+
}
51+
};
52+
53+
void simple_display(llvm::raw_ostream &out, const CursorInfoOwner &owner);
54+
55+
/// Resolve cursor info at a given location.
56+
class CursorInfoRequest:
57+
public SimpleRequest<CursorInfoRequest,
58+
ide::ResolvedCursorInfo(CursorInfoOwner),
59+
CacheKind::Uncached> // FIXME: cache these
60+
{
61+
public:
62+
using SimpleRequest::SimpleRequest;
63+
64+
private:
65+
friend SimpleRequest;
66+
67+
// Evaluation.
68+
llvm::Expected<ide::ResolvedCursorInfo> evaluate(Evaluator &evaluator,
69+
CursorInfoOwner CI) const;
70+
71+
public:
72+
// Caching
73+
bool isCached() const { return true; }
74+
// Source location
75+
SourceLoc getNearestLoc() const;
76+
};
77+
78+
/// The zone number for the IDE.
79+
#define SWIFT_IDE_REQUESTS_TYPEID_ZONE 137
80+
#define SWIFT_TYPEID_ZONE SWIFT_IDE_REQUESTS_TYPEID_ZONE
81+
#define SWIFT_TYPEID_HEADER "swift/IDE/IDERequestIDZone.def"
82+
#include "swift/Basic/DefineTypeIDZone.h"
83+
#undef SWIFT_TYPEID_ZONE
84+
#undef SWIFT_TYPEID_HEADER
85+
86+
// Set up reporting of evaluated requests.
87+
#define SWIFT_TYPEID(RequestType) \
88+
template<> \
89+
inline void reportEvaluatedRequest(UnifiedStatsReporter &stats, \
90+
const RequestType &request) { \
91+
++stats.getFrontendCounters().RequestType; \
92+
}
93+
#include "swift/IDE/IDERequestIDZone.def"
94+
#undef SWIFT_TYPEID
95+
96+
} // end namespace swift
97+
98+
#endif // SWIFT_IDE_REQUESTS_H

include/swift/IDE/Utils.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -196,45 +196,6 @@ struct ResolvedCursorInfo {
196196
bool isInvalid() const { return Kind == CursorInfoKind::Invalid; }
197197
};
198198

199-
class CursorInfoResolver : public SourceEntityWalker {
200-
SourceFile &SrcFile;
201-
SourceLoc LocToResolve;
202-
ResolvedCursorInfo CursorInfo;
203-
Type ContainerType;
204-
llvm::SmallVector<Expr*, 4> TrailingExprStack;
205-
206-
public:
207-
explicit CursorInfoResolver(SourceFile &SrcFile) :
208-
SrcFile(SrcFile), CursorInfo(&SrcFile) {}
209-
ResolvedCursorInfo resolve(SourceLoc Loc);
210-
SourceManager &getSourceMgr() const;
211-
private:
212-
bool walkToExprPre(Expr *E) override;
213-
bool walkToExprPost(Expr *E) override;
214-
bool walkToDeclPre(Decl *D, CharSourceRange Range) override;
215-
bool walkToDeclPost(Decl *D) override;
216-
bool walkToStmtPre(Stmt *S) override;
217-
bool walkToStmtPost(Stmt *S) override;
218-
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
219-
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef, Type T,
220-
ReferenceMetaData Data) override;
221-
bool visitCallArgName(Identifier Name, CharSourceRange Range,
222-
ValueDecl *D) override;
223-
bool visitDeclarationArgumentName(Identifier Name, SourceLoc StartLoc,
224-
ValueDecl *D) override;
225-
bool visitModuleReference(ModuleEntity Mod, CharSourceRange Range) override;
226-
bool rangeContainsLoc(SourceRange Range) const;
227-
bool rangeContainsLoc(CharSourceRange Range) const;
228-
bool isDone() const { return CursorInfo.isValid(); }
229-
bool tryResolve(ValueDecl *D, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
230-
SourceLoc Loc, bool IsRef, Type Ty = Type());
231-
bool tryResolve(ModuleEntity Mod, SourceLoc Loc);
232-
bool tryResolve(Stmt *St);
233-
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
234-
ReferenceMetaData Data,
235-
bool IsOpenBracket) override;
236-
};
237-
238199
struct UnresolvedLoc {
239200
SourceLoc Loc;
240201
bool ResolveArgLocs;

include/swift/Subsystems.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ namespace swift {
396396
/// ASTContext.
397397
void registerTypeCheckerRequestFunctions(Evaluator &evaluator);
398398

399+
/// Register IDE-level request functions with the evaluator.
400+
///
401+
/// The ASTContext will automatically call these upon construction.
402+
void registerIDERequestFunctions(Evaluator &evaluator);
403+
399404
} // end namespace swift
400405

401406
#endif // SWIFT_SUBSYSTEMS_H

lib/IDE/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ add_swift_host_library(swiftIDE STATIC
1414
IDETypeChecking.cpp
1515
APIDigesterData.cpp
1616
SourceEntityWalker.cpp
17-
TypeContextInfo.cpp)
17+
TypeContextInfo.cpp
18+
IDERequests.cpp)
1819
target_link_libraries(swiftIDE PRIVATE
1920
swiftAST
2021
swiftClangImporter

0 commit comments

Comments
 (0)