Skip to content

Commit 1aaf5fc

Browse files
authored
Merge pull request swiftlang#26155 from nkcsgexi/request-cursor-info
Sourcekit+Evaluator: refactor cursor-info resolver to using the evaluator model. NFC
2 parents c76925c + f2a30bb commit 1aaf5fc

File tree

15 files changed

+573
-276
lines changed

15 files changed

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

include/swift/IDE/IDETypeIDZone.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===---------- IDETypeIDZone.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 type
14+
// TypeID zone, for use with the TypeID template.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
SWIFT_TYPEID_NAMED(ide::ResolvedCursorInfo, ResolvedCursorInfo)

include/swift/IDE/IDETypeIDs.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===--- IDETypeIDs.h - IDE Type Ids ----------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 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 TypeID support for IDE types.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_IDE_IDETYPEIDS_H
18+
#define SWIFT_IDE_IDETYPEIDS_H
19+
20+
#include "swift/Basic/TypeID.h"
21+
namespace swift {
22+
23+
#define SWIFT_IDE_TYPEID_ZONE 136
24+
#define SWIFT_TYPEID_ZONE SWIFT_IDE_TYPEID_ZONE
25+
#define SWIFT_TYPEID_HEADER "swift/IDE/IDETypeIDZone.def"
26+
#include "swift/Basic/DefineTypeIDZone.h"
27+
28+
} // end namespace swift
29+
30+
#endif // SWIFT_IDE_IDETYPEIDS_H

include/swift/IDE/Utils.h

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ struct ResolvedCursorInfo {
163163

164164
ResolvedCursorInfo() = default;
165165
ResolvedCursorInfo(SourceFile *SF) : SF(SF) {}
166-
166+
167+
friend bool operator==(const ResolvedCursorInfo &lhs,
168+
const ResolvedCursorInfo &rhs) {
169+
return lhs.SF == rhs.SF &&
170+
lhs.Loc.getOpaquePointerValue() == rhs.Loc.getOpaquePointerValue();
171+
}
172+
167173
void setValueRef(ValueDecl *ValueD,
168174
TypeDecl *CtorTyRef,
169175
ExtensionDecl *ExtTyRef,
@@ -196,44 +202,7 @@ struct ResolvedCursorInfo {
196202
bool isInvalid() const { return Kind == CursorInfoKind::Invalid; }
197203
};
198204

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-
};
205+
void simple_display(llvm::raw_ostream &out, const ResolvedCursorInfo &info);
237206

238207
struct UnresolvedLoc {
239208
SourceLoc Loc;

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)