IDE+Evaluator: refactor resolveProtocolName to using the request evaluator, NFC

This commit is contained in:
Xi Ge
2019-07-22 15:35:09 -07:00
parent 344d9311a8
commit 1cb746b47c
8 changed files with 104 additions and 39 deletions

View File

@@ -13,6 +13,7 @@
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ASTDemangler.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
@@ -1144,3 +1145,28 @@ CollectOverriddenDeclsRequest::evaluate(Evaluator &evaluator,
return copyToContext(VD->getASTContext(), llvm::makeArrayRef(results));
}
//----------------------------------------------------------------------------//
// ResolveProtocolNameRequest
//----------------------------------------------------------------------------//
llvm::Expected<ProtocolDecl*>
ResolveProtocolNameRequest::evaluate(Evaluator &evaluator,
ProtocolNameOwner Input) const {
auto &ctx = Input.DC->getASTContext();
auto name = Input.Name;
// First try to solve by usr
ProtocolDecl *pd = dyn_cast_or_null<ProtocolDecl>(Demangle::
getTypeDeclForUSR(ctx, name));
if (!pd) {
// Second try to solve by mangled symbol name
pd = dyn_cast_or_null<ProtocolDecl>(Demangle::getTypeDeclForMangling(ctx, name));
}
if (!pd) {
// Thirdly try to solve by mangled type name
if (auto ty = Demangle::getTypeForMangling(ctx, name)) {
pd = dyn_cast_or_null<ProtocolDecl>(ty->getAnyGeneric());
}
}
return pd;
}