Skip to content

Commit 81151da

Browse files
authored
Merge pull request swiftlang#19005 from nkcsgexi/request-usr-gen
IDE: refactor USR generation to request evaluator model.
2 parents 10468c6 + dd5f21b commit 81151da

File tree

6 files changed

+79
-18
lines changed

6 files changed

+79
-18
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,31 @@ class RequirementRequest :
297297
void cacheResult(Requirement value) const;
298298
};
299299

300+
/// Generate the USR for the given declaration.
301+
class USRGenerationRequest :
302+
public SimpleRequest<USRGenerationRequest,
303+
CacheKind::Cached,
304+
std::string,
305+
const ValueDecl*>
306+
{
307+
public:
308+
using SimpleRequest::SimpleRequest;
309+
310+
private:
311+
friend class SimpleRequest;
312+
313+
// Evaluation.
314+
llvm::Expected<std::string> evaluate(Evaluator &eval, const ValueDecl* d) const;
315+
316+
public:
317+
// Cycle handling
318+
void diagnoseCycle(DiagnosticEngine &diags) const;
319+
void noteCycleStep(DiagnosticEngine &diags) const;
320+
321+
// Caching
322+
bool isCached() const { return true; }
323+
};
324+
300325
/// The zone number for the type checker.
301326
#define SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE 10
302327

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ SWIFT_TYPEID(OverriddenDeclsRequest)
2121
SWIFT_TYPEID(IsObjCRequest)
2222
SWIFT_TYPEID(IsDynamicRequest)
2323
SWIFT_TYPEID(RequirementRequest)
24+
SWIFT_TYPEID(USRGenerationRequest)

include/swift/Basic/CTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SWIFT_TYPEID_NAMED(double, Double)
3232
SWIFT_TYPEID_NAMED(bool, Bool)
3333
SWIFT_TYPEID_NAMED(decltype(nullptr), NullPtr)
3434
SWIFT_TYPEID_NAMED(void, Void)
35+
SWIFT_TYPEID_NAMED(std::string, String)
3536

3637
// C++ standard library types.
3738
SWIFT_TYPEID_TEMPLATE1_NAMED(std::vector, Vector, typename T, T)

include/swift/Basic/SimpleDisplay.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace swift {
4949
HAS_TRIVIAL_DISPLAY(float);
5050
HAS_TRIVIAL_DISPLAY(double);
5151
HAS_TRIVIAL_DISPLAY(bool);
52+
HAS_TRIVIAL_DISPLAY(std::string);
5253

5354
#undef HAS_TRIVIAL_DISPLAY
5455

lib/AST/TypeCheckRequests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,19 @@ void RequirementRequest::cacheResult(Requirement value) const {
412412
break;
413413
}
414414
}
415+
416+
//----------------------------------------------------------------------------//
417+
// USR computation.
418+
//----------------------------------------------------------------------------//
419+
420+
void USRGenerationRequest::diagnoseCycle(DiagnosticEngine &diags) const {
421+
const auto &storage = getStorage();
422+
auto &d = std::get<0>(storage);
423+
diags.diagnose(d, diag::circular_reference);
424+
}
425+
426+
void USRGenerationRequest::noteCycleStep(DiagnosticEngine &diags) const {
427+
const auto &storage = getStorage();
428+
auto &d = std::get<0>(storage);
429+
diags.diagnose(d, diag::circular_reference);
430+
}

lib/AST/USRGeneration.cpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "swift/AST/USRGeneration.h"
1616
#include "swift/AST/ASTMangler.h"
1717
#include "swift/AST/SwiftNameTranslation.h"
18+
#include "swift/AST/TypeCheckRequests.h"
1819
#include "llvm/ADT/SmallString.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/Support/raw_ostream.h"
@@ -163,13 +164,14 @@ static bool shouldUseObjCUSR(const Decl *D) {
163164
return false;
164165
}
165166

166-
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
167+
llvm::Expected<std::string>
168+
swift::USRGenerationRequest::evaluate(Evaluator &evaluator, const ValueDecl* D) const {
167169
if (!D->hasName() && !isa<ParamDecl>(D) && !isa<AccessorDecl>(D))
168-
return true; // Ignore.
170+
return std::string(); // Ignore.
169171
if (D->getModuleContext()->isBuiltinModule())
170-
return true; // Ignore.
172+
return std::string(); // Ignore.
171173
if (isa<ModuleDecl>(D))
172-
return true; // Ignore.
174+
return std::string(); // Ignore.
173175

174176
auto interpretAsClangNode = [](const ValueDecl *D)->ClangNode {
175177
ClangNode ClangN = D->getClangNode();
@@ -200,13 +202,17 @@ bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
200202
return ClangN;
201203
};
202204

205+
llvm::SmallString<128> Buffer;
206+
llvm::raw_svector_ostream OS(Buffer);
207+
203208
if (ClangNode ClangN = interpretAsClangNode(D)) {
204-
llvm::SmallString<128> Buf;
205209
if (auto ClangD = ClangN.getAsDecl()) {
206-
bool Ignore = clang::index::generateUSRForDecl(ClangD, Buf);
207-
if (!Ignore)
208-
OS << Buf.str();
209-
return Ignore;
210+
bool Ignore = clang::index::generateUSRForDecl(ClangD, Buffer);
211+
if (!Ignore) {
212+
return Buffer.str();
213+
} else {
214+
return std::string();
215+
}
210216
}
211217

212218
auto &Importer = *D->getASTContext().getClangModuleLoader();
@@ -215,30 +221,41 @@ bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
215221
bool Ignore = clang::index::generateUSRForMacro(
216222
D->getBaseName().getIdentifier().str(),
217223
ClangMacroInfo->getDefinitionLoc(),
218-
Importer.getClangASTContext().getSourceManager(), Buf);
224+
Importer.getClangASTContext().getSourceManager(), Buffer);
219225
if (!Ignore)
220-
OS << Buf.str();
221-
return Ignore;
226+
return Buffer.str();
227+
else
228+
return std::string();
222229
}
223230

224231
if (shouldUseObjCUSR(D)) {
225-
return printObjCUSR(D, OS);
232+
if (printObjCUSR(D, OS)) {
233+
return std::string();
234+
} else {
235+
return OS.str();
236+
}
226237
}
227238

228239
if (!D->hasInterfaceType())
229-
return true;
240+
return std::string();
230241

231242
// Invalid code.
232243
if (D->getInterfaceType().findIf([](Type t) -> bool {
233244
return t->is<ModuleType>();
234245
}))
235-
return true;
246+
return std::string();
236247

237248
Mangle::ASTMangler NewMangler;
238-
std::string Mangled = NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
239-
240-
OS << Mangled;
249+
return NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
250+
}
241251

252+
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
253+
auto result = evaluateOrDefault(D->getASTContext().evaluator,
254+
USRGenerationRequest { D },
255+
std::string());
256+
if (result.empty())
257+
return true;
258+
OS << result;
242259
return false;
243260
}
244261

0 commit comments

Comments
 (0)