Skip to content

Commit 00d3a39

Browse files
authored
Merge pull request swiftlang#26191 from nkcsgexi/range-info-request
IDE+Evaluator: refactor range info resolver to an IDE evaluator request. NFC
2 parents 8880aae + 8f1a9b4 commit 00d3a39

File tree

9 files changed

+850
-724
lines changed

9 files changed

+850
-724
lines changed

include/swift/IDE/IDERequestIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717
SWIFT_TYPEID(CursorInfoRequest)
18+
SWIFT_TYPEID(RangeInfoRequest)

include/swift/IDE/IDERequests.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "swift/IDE/IDETypeIDs.h"
2424

2525
namespace swift {
26+
//----------------------------------------------------------------------------//
27+
// Cusor info
28+
//----------------------------------------------------------------------------//
2629

2730
// Input for CursorInfoRequest.
2831
// Putting the source file and location together allows us to print the request
@@ -76,6 +79,67 @@ class CursorInfoRequest:
7679
SourceLoc getNearestLoc() const;
7780
};
7881

82+
//----------------------------------------------------------------------------//
83+
// Range info
84+
//----------------------------------------------------------------------------//
85+
86+
// Input for RangeInfoRequest.
87+
// Putting the source file and location together allows us to print the request
88+
// input well e.g. file.swift:3:4
89+
struct RangeInfoOwner {
90+
SourceFile *File;
91+
SourceLoc StartLoc;
92+
SourceLoc EndLoc;
93+
94+
RangeInfoOwner(SourceFile *File, SourceLoc StartLoc, SourceLoc EndLoc):
95+
File(File), StartLoc(StartLoc), EndLoc(EndLoc) {}
96+
RangeInfoOwner(SourceFile *File, unsigned Offset, unsigned Length);
97+
98+
friend llvm::hash_code hash_value(const RangeInfoOwner &CI) {
99+
return hash_combine(hash_value(CI.File),
100+
hash_value(CI.StartLoc.getOpaquePointerValue()),
101+
hash_value(CI.EndLoc.getOpaquePointerValue()));
102+
}
103+
104+
friend bool operator==(const RangeInfoOwner &lhs, const RangeInfoOwner &rhs) {
105+
return lhs.File == rhs.File && lhs.StartLoc == rhs.StartLoc &&
106+
lhs.EndLoc == rhs.EndLoc;
107+
}
108+
109+
friend bool operator!=(const RangeInfoOwner &lhs, const RangeInfoOwner &rhs) {
110+
return !(lhs == rhs);
111+
}
112+
113+
bool isValid() const {
114+
return File && File->getBufferID() && StartLoc.isValid() && EndLoc.isValid();
115+
}
116+
};
117+
118+
void simple_display(llvm::raw_ostream &out, const RangeInfoOwner &owner);
119+
120+
/// Resolve cursor info at a given location.
121+
class RangeInfoRequest:
122+
public SimpleRequest<RangeInfoRequest,
123+
ide::ResolvedRangeInfo(RangeInfoOwner),
124+
CacheKind::Cached>
125+
{
126+
public:
127+
using SimpleRequest::SimpleRequest;
128+
129+
private:
130+
friend SimpleRequest;
131+
132+
// Evaluation.
133+
llvm::Expected<ide::ResolvedRangeInfo> evaluate(Evaluator &evaluator,
134+
RangeInfoOwner CI) const;
135+
136+
public:
137+
// Caching
138+
bool isCached() const { return true; }
139+
// Source location
140+
SourceLoc getNearestLoc() const;
141+
};
142+
79143
/// The zone number for the IDE.
80144
#define SWIFT_IDE_REQUESTS_TYPEID_ZONE 137
81145
#define SWIFT_TYPEID_ZONE SWIFT_IDE_REQUESTS_TYPEID_ZONE

include/swift/IDE/IDETypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717
SWIFT_TYPEID_NAMED(ide::ResolvedCursorInfo, ResolvedCursorInfo)
18+
SWIFT_TYPEID_NAMED(ide::ResolvedRangeInfo, ResolvedRangeInfo)

include/swift/IDE/Utils.h

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -368,32 +368,26 @@ struct ResolvedRangeInfo {
368368
TokensInRange, nullptr, /*Commom Expr Parent*/nullptr,
369369
/*Single entry*/true, /*unhandled error*/false,
370370
OrphanKind::None, {}, {}, {}) {}
371-
void print(llvm::raw_ostream &OS);
371+
ResolvedRangeInfo(): ResolvedRangeInfo(ArrayRef<Token>()) {}
372+
void print(llvm::raw_ostream &OS) const;
372373
ExitState exit() const { return ExitInfo.Exit; }
373374
Type getType() const { return ExitInfo.ReturnType; }
374375

376+
friend bool operator==(const ResolvedRangeInfo &lhs,
377+
const ResolvedRangeInfo &rhs) {
378+
if (lhs.TokensInRange.size() != rhs.TokensInRange.size())
379+
return false;
380+
if (lhs.TokensInRange.empty())
381+
return true;
382+
return lhs.TokensInRange.front().getLoc() ==
383+
rhs.TokensInRange.front().getLoc();
384+
}
385+
375386
private:
376387
static CharSourceRange calculateContentRange(ArrayRef<Token> Tokens);
377388
};
378389

379-
class RangeResolver : public SourceEntityWalker {
380-
struct Implementation;
381-
std::unique_ptr<Implementation> Impl;
382-
bool walkToExprPre(Expr *E) override;
383-
bool walkToExprPost(Expr *E) override;
384-
bool walkToStmtPre(Stmt *S) override;
385-
bool walkToStmtPost(Stmt *S) override;
386-
bool walkToDeclPre(Decl *D, CharSourceRange Range) override;
387-
bool walkToDeclPost(Decl *D) override;
388-
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
389-
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef, Type T,
390-
ReferenceMetaData Data) override;
391-
public:
392-
RangeResolver(SourceFile &File, SourceLoc Start, SourceLoc End);
393-
RangeResolver(SourceFile &File, unsigned Offset, unsigned Length);
394-
~RangeResolver();
395-
ResolvedRangeInfo resolve();
396-
};
390+
void simple_display(llvm::raw_ostream &out, const ResolvedRangeInfo &info);
397391

398392
/// This provides a utility to view a printed name by parsing the components
399393
/// of that name. The components include a base name and an array of argument

0 commit comments

Comments
 (0)