#include "MLIRServer.h"
#include "Protocol.h"
#include "mlir/AsmParser/AsmParser.h"
#include "mlir/AsmParser/AsmParserState.h"
#include "mlir/AsmParser/CodeComplete.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/FunctionInterfaces.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Support/ToolUtilities.h"
#include "mlir/Tools/lsp-server-support/Logging.h"
#include "mlir/Tools/lsp-server-support/SourceMgrUtils.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Base64.h"
#include "llvm/Support/SourceMgr.h"
#include <optional>
usingnamespacemlir;
static SMRange convertTokenLocToRange(SMLoc loc) { … }
static std::optional<lsp::Location> getLocationFromLoc(StringRef uriScheme,
FileLineColLoc loc) { … }
static std::optional<lsp::Location>
getLocationFromLoc(llvm::SourceMgr &sourceMgr, Location loc,
StringRef uriScheme, const lsp::URIForFile *uri = nullptr) { … }
static void collectLocationsFromLoc(Location loc,
std::vector<lsp::Location> &locations,
const lsp::URIForFile &uri) { … }
static bool contains(SMRange range, SMLoc loc) { … }
static bool isDefOrUse(const AsmParserState::SMDefinition &def, SMLoc loc,
SMRange *overlappedRange = nullptr) { … }
static std::optional<unsigned> getResultNumberFromLoc(SMLoc loc) { … }
static std::optional<StringRef> getTextFromRange(SMRange range) { … }
static unsigned getBlockNumber(Block *block) { … }
static void printDefBlockName(raw_ostream &os, Block *block, SMRange loc = { … }
static void printDefBlockName(raw_ostream &os,
const AsmParserState::BlockDefinition &def) { … }
static lsp::Diagnostic getLspDiagnoticFromDiag(llvm::SourceMgr &sourceMgr,
Diagnostic &diag,
const lsp::URIForFile &uri) { … }
namespace {
struct MLIRDocument { … };
}
MLIRDocument::MLIRDocument(MLIRContext &context, const lsp::URIForFile &uri,
StringRef contents,
std::vector<lsp::Diagnostic> &diagnostics) { … }
void MLIRDocument::getLocationsOf(const lsp::URIForFile &uri,
const lsp::Position &defPos,
std::vector<lsp::Location> &locations) { … }
void MLIRDocument::findReferencesOf(const lsp::URIForFile &uri,
const lsp::Position &pos,
std::vector<lsp::Location> &references) { … }
std::optional<lsp::Hover>
MLIRDocument::findHover(const lsp::URIForFile &uri,
const lsp::Position &hoverPos) { … }
std::optional<lsp::Hover> MLIRDocument::buildHoverForOperation(
SMRange hoverRange, const AsmParserState::OperationDefinition &op) { … }
lsp::Hover MLIRDocument::buildHoverForOperationResult(SMRange hoverRange,
Operation *op,
unsigned resultStart,
unsigned resultEnd,
SMLoc posLoc) { … }
lsp::Hover
MLIRDocument::buildHoverForBlock(SMRange hoverRange,
const AsmParserState::BlockDefinition &block) { … }
lsp::Hover MLIRDocument::buildHoverForBlockArgument(
SMRange hoverRange, BlockArgument arg,
const AsmParserState::BlockDefinition &block) { … }
lsp::Hover MLIRDocument::buildHoverForAttributeAlias(
SMRange hoverRange, const AsmParserState::AttributeAliasDefinition &attr) { … }
lsp::Hover MLIRDocument::buildHoverForTypeAlias(
SMRange hoverRange, const AsmParserState::TypeAliasDefinition &type) { … }
void MLIRDocument::findDocumentSymbols(
std::vector<lsp::DocumentSymbol> &symbols) { … }
void MLIRDocument::findDocumentSymbols(
Operation *op, std::vector<lsp::DocumentSymbol> &symbols) { … }
namespace {
class LSPCodeCompleteContext : public AsmParserCodeCompleteContext { … };
}
lsp::CompletionList
MLIRDocument::getCodeCompletion(const lsp::URIForFile &uri,
const lsp::Position &completePos,
const DialectRegistry ®istry) { … }
void MLIRDocument::getCodeActionForDiagnostic(
const lsp::URIForFile &uri, lsp::Position &pos, StringRef severity,
StringRef message, std::vector<lsp::TextEdit> &edits) { … }
llvm::Expected<lsp::MLIRConvertBytecodeResult>
MLIRDocument::convertToBytecode() { … }
namespace {
struct MLIRTextFileChunk { … };
}
namespace {
class MLIRTextFile { … };
}
MLIRTextFile::MLIRTextFile(const lsp::URIForFile &uri, StringRef fileContents,
int64_t version, DialectRegistry ®istry,
std::vector<lsp::Diagnostic> &diagnostics)
: … { … }
void MLIRTextFile::getLocationsOf(const lsp::URIForFile &uri,
lsp::Position defPos,
std::vector<lsp::Location> &locations) { … }
void MLIRTextFile::findReferencesOf(const lsp::URIForFile &uri,
lsp::Position pos,
std::vector<lsp::Location> &references) { … }
std::optional<lsp::Hover> MLIRTextFile::findHover(const lsp::URIForFile &uri,
lsp::Position hoverPos) { … }
void MLIRTextFile::findDocumentSymbols(
std::vector<lsp::DocumentSymbol> &symbols) { … }
lsp::CompletionList MLIRTextFile::getCodeCompletion(const lsp::URIForFile &uri,
lsp::Position completePos) { … }
void MLIRTextFile::getCodeActions(const lsp::URIForFile &uri,
const lsp::Range &pos,
const lsp::CodeActionContext &context,
std::vector<lsp::CodeAction> &actions) { … }
llvm::Expected<lsp::MLIRConvertBytecodeResult>
MLIRTextFile::convertToBytecode() { … }
MLIRTextFileChunk &MLIRTextFile::getChunkFor(lsp::Position &pos) { … }
struct lsp::MLIRServer::Impl { … };
lsp::MLIRServer::MLIRServer(DialectRegistry ®istry)
: … { … }
lsp::MLIRServer::~MLIRServer() = default;
void lsp::MLIRServer::addOrUpdateDocument(
const URIForFile &uri, StringRef contents, int64_t version,
std::vector<Diagnostic> &diagnostics) { … }
std::optional<int64_t> lsp::MLIRServer::removeDocument(const URIForFile &uri) { … }
void lsp::MLIRServer::getLocationsOf(const URIForFile &uri,
const Position &defPos,
std::vector<Location> &locations) { … }
void lsp::MLIRServer::findReferencesOf(const URIForFile &uri,
const Position &pos,
std::vector<Location> &references) { … }
std::optional<lsp::Hover> lsp::MLIRServer::findHover(const URIForFile &uri,
const Position &hoverPos) { … }
void lsp::MLIRServer::findDocumentSymbols(
const URIForFile &uri, std::vector<DocumentSymbol> &symbols) { … }
lsp::CompletionList
lsp::MLIRServer::getCodeCompletion(const URIForFile &uri,
const Position &completePos) { … }
void lsp::MLIRServer::getCodeActions(const URIForFile &uri, const Range &pos,
const CodeActionContext &context,
std::vector<CodeAction> &actions) { … }
llvm::Expected<lsp::MLIRConvertBytecodeResult>
lsp::MLIRServer::convertFromBytecode(const URIForFile &uri) { … }
llvm::Expected<lsp::MLIRConvertBytecodeResult>
lsp::MLIRServer::convertToBytecode(const URIForFile &uri) { … }