MLIR  22.0.0git
MLIRServer.h
Go to the documentation of this file.
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"
15 #include "llvm/Support/Error.h"
16 #include <memory>
17 #include <optional>
18 
19 namespace mlir {
20 class DialectRegistry;
21 
22 namespace lsp {
23 using llvm::lsp::CodeAction;
24 using llvm::lsp::CodeActionContext;
25 using llvm::lsp::CompletionList;
26 using llvm::lsp::Diagnostic;
27 using llvm::lsp::DocumentSymbol;
28 using llvm::lsp::Hover;
29 using llvm::lsp::Location;
31 using llvm::lsp::Position;
32 using llvm::lsp::Range;
33 using 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.
38 class MLIRServer {
39 public:
40  /// Construct a new server with the given dialect registry function.
41  MLIRServer(DialectRegistryFn registry_fn);
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.
84  convertFromBytecode(const URIForFile &uri);
85 
86  /// Convert the given textual file to the bytecode format.
88  convertToBytecode(const URIForFile &uri);
89 
90 private:
91  struct Impl;
92 
93  std::unique_ptr<Impl> impl;
94 };
95 
96 } // namespace lsp
97 } // namespace mlir
98 
99 #endif // LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
This class implements all of the MLIR related functionality necessary for a language server.
Definition: MLIRServer.h:38
void addOrUpdateDocument(const URIForFile &uri, StringRef contents, int64_t version, std::vector< Diagnostic > &diagnostics)
Add or update the document, with the provided version, at the given URI.
std::optional< int64_t > removeDocument(const URIForFile &uri)
Remove the document with the given uri.
void findReferencesOf(const URIForFile &uri, const Position &pos, std::vector< Location > &references)
Find all references of the object pointed at by the given position.
void getLocationsOf(const URIForFile &uri, const Position &defPos, std::vector< Location > &locations)
Return the locations of the object pointed at by the given position.
std::optional< Hover > findHover(const URIForFile &uri, const Position &hoverPos)
Find a hover description for the given hover position, or std::nullopt if one couldn't be found.
llvm::Expected< MLIRConvertBytecodeResult > convertFromBytecode(const URIForFile &uri)
Convert the given bytecode file to the textual format.
llvm::Expected< MLIRConvertBytecodeResult > convertToBytecode(const URIForFile &uri)
Convert the given textual file to the bytecode format.
CompletionList getCodeCompletion(const URIForFile &uri, const Position &completePos)
Get the code completion list for the position within the given file.
void findDocumentSymbols(const URIForFile &uri, std::vector< DocumentSymbol > &symbols)
Find all of the document symbols within the given file.
MLIRServer(DialectRegistryFn registry_fn)
Construct a new server with the given dialect registry function.
void getCodeActions(const URIForFile &uri, const Range &pos, const CodeActionContext &context, std::vector< CodeAction > &actions)
Get the set of code actions within the file.
Include the generated interface declarations.
This class represents the result of converting between MLIR's bytecode and textual format.
Definition: Protocol.h:48