xref: /llvm-project/mlir/lib/Tools/mlir-lsp-server/MLIRServer.h (revision 0a81ace0047a2de93e71c82cdf0977fc989660df)
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 "mlir/Support/LLVM.h"
13 #include "llvm/Support/Error.h"
14 #include <memory>
15 #include <optional>
16 
17 namespace mlir {
18 class DialectRegistry;
19 
20 namespace lsp {
21 struct CodeAction;
22 struct CodeActionContext;
23 struct CompletionList;
24 struct Diagnostic;
25 struct DocumentSymbol;
26 struct Hover;
27 struct Location;
28 struct MLIRConvertBytecodeResult;
29 struct Position;
30 struct Range;
31 class URIForFile;
32 
33 /// This class implements all of the MLIR related functionality necessary for a
34 /// language server. This class allows for keeping the MLIR specific logic
35 /// separate from the logic that involves LSP server/client communication.
36 class MLIRServer {
37 public:
38   /// Construct a new server with the given dialect regitstry.
39   MLIRServer(DialectRegistry &registry);
40   ~MLIRServer();
41 
42   /// Add or update the document, with the provided `version`, at the given URI.
43   /// Any diagnostics emitted for this document should be added to
44   /// `diagnostics`.
45   void addOrUpdateDocument(const URIForFile &uri, StringRef contents,
46                            int64_t version,
47                            std::vector<Diagnostic> &diagnostics);
48 
49   /// Remove the document with the given uri. Returns the version of the removed
50   /// document, or std::nullopt if the uri did not have a corresponding document
51   /// within the server.
52   std::optional<int64_t> removeDocument(const URIForFile &uri);
53 
54   /// Return the locations of the object pointed at by the given position.
55   void getLocationsOf(const URIForFile &uri, const Position &defPos,
56                       std::vector<Location> &locations);
57 
58   /// Find all references of the object pointed at by the given position.
59   void findReferencesOf(const URIForFile &uri, const Position &pos,
60                         std::vector<Location> &references);
61 
62   /// Find a hover description for the given hover position, or std::nullopt if
63   /// one couldn't be found.
64   std::optional<Hover> findHover(const URIForFile &uri,
65                                  const Position &hoverPos);
66 
67   /// Find all of the document symbols within the given file.
68   void findDocumentSymbols(const URIForFile &uri,
69                            std::vector<DocumentSymbol> &symbols);
70 
71   /// Get the code completion list for the position within the given file.
72   CompletionList getCodeCompletion(const URIForFile &uri,
73                                    const Position &completePos);
74 
75   /// Get the set of code actions within the file.
76   void getCodeActions(const URIForFile &uri, const Range &pos,
77                       const CodeActionContext &context,
78                       std::vector<CodeAction> &actions);
79 
80   /// Convert the given bytecode file to the textual format.
81   llvm::Expected<MLIRConvertBytecodeResult>
82   convertFromBytecode(const URIForFile &uri);
83 
84   /// Convert the given textual file to the bytecode format.
85   llvm::Expected<MLIRConvertBytecodeResult>
86   convertToBytecode(const URIForFile &uri);
87 
88 private:
89   struct Impl;
90 
91   std::unique_ptr<Impl> impl;
92 };
93 
94 } // namespace lsp
95 } // namespace mlir
96 
97 #endif // LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
98