1//===- MLIRServer.h - MLIR General Language Server --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
10#define LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
11
12#include "Protocol.h"
13#include "mlir/Support/LLVM.h"
14#include "mlir/Tools/mlir-lsp-server/MlirLspRegistryFunction.h"
15#include "llvm/Support/Error.h"
16#include <memory>
17#include <optional>
18
19namespace mlir {
20class DialectRegistry;
21
22namespace lsp {
23using llvm::lsp::CodeAction;
24using llvm::lsp::CodeActionContext;
25using llvm::lsp::CompletionList;
26using llvm::lsp::Diagnostic;
27using llvm::lsp::DocumentSymbol;
28using llvm::lsp::Hover;
29using llvm::lsp::Location;
30using llvm::lsp::MLIRConvertBytecodeResult;
31using llvm::lsp::Position;
32using llvm::lsp::Range;
33using llvm::lsp::URIForFile;
34
35/// This class implements all of the MLIR related functionality necessary for a
36/// language server. This class allows for keeping the MLIR specific logic
37/// separate from the logic that involves LSP server/client communication.
38class MLIRServer {
39public:
40 /// Construct a new server with the given dialect registry function.
41 MLIRServer(DialectRegistryFn registry_fn);
42 ~MLIRServer();
43
44 /// Add or update the document, with the provided `version`, at the given URI.
45 /// Any diagnostics emitted for this document should be added to
46 /// `diagnostics`.
47 void addOrUpdateDocument(const URIForFile &uri, StringRef contents,
48 int64_t version,
49 std::vector<Diagnostic> &diagnostics);
50
51 /// Remove the document with the given uri. Returns the version of the removed
52 /// document, or std::nullopt if the uri did not have a corresponding document
53 /// within the server.
54 std::optional<int64_t> removeDocument(const URIForFile &uri);
55
56 /// Return the locations of the object pointed at by the given position.
57 void getLocationsOf(const URIForFile &uri, const Position &defPos,
58 std::vector<Location> &locations);
59
60 /// Find all references of the object pointed at by the given position.
61 void findReferencesOf(const URIForFile &uri, const Position &pos,
62 std::vector<Location> &references);
63
64 /// Find a hover description for the given hover position, or std::nullopt if
65 /// one couldn't be found.
66 std::optional<Hover> findHover(const URIForFile &uri,
67 const Position &hoverPos);
68
69 /// Find all of the document symbols within the given file.
70 void findDocumentSymbols(const URIForFile &uri,
71 std::vector<DocumentSymbol> &symbols);
72
73 /// Get the code completion list for the position within the given file.
74 CompletionList getCodeCompletion(const URIForFile &uri,
75 const Position &completePos);
76
77 /// Get the set of code actions within the file.
78 void getCodeActions(const URIForFile &uri, const Range &pos,
79 const CodeActionContext &context,
80 std::vector<CodeAction> &actions);
81
82 /// Convert the given bytecode file to the textual format.
83 llvm::Expected<MLIRConvertBytecodeResult>
84 convertFromBytecode(const URIForFile &uri);
85
86 /// Convert the given textual file to the bytecode format.
87 llvm::Expected<MLIRConvertBytecodeResult>
88 convertToBytecode(const URIForFile &uri);
89
90 /// Set the workspace root for the server.
91 void setWorkspaceRoot(StringRef root);
92
93private:
94 struct Impl;
95
96 std::unique_ptr<Impl> impl;
97};
98
99} // namespace lsp
100} // namespace mlir
101
102#endif // LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
103