MLIR  22.0.0git
Protocol.cpp
Go to the documentation of this file.
1 //===--- Protocol.cpp - Language Server Protocol Implementation -----------===//
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 // This file contains the serialization code for the PDLL specific LSP structs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Protocol.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/JSON.h"
16 
17 using namespace mlir;
18 using namespace mlir::lsp;
19 
20 // Helper that doesn't treat `null` and absent fields as failures.
21 template <typename T>
22 static bool mapOptOrNull(const llvm::json::Value &params,
23  llvm::StringLiteral prop, T &out,
24  llvm::json::Path path) {
25  const llvm::json::Object *o = params.getAsObject();
26  assert(o);
27 
28  // Field is missing or null.
29  auto *v = o->get(prop);
30  if (!v || v->getAsNull())
31  return true;
32  return fromJSON(*v, out, path.field(prop));
33 }
34 
35 //===----------------------------------------------------------------------===//
36 // PDLLViewOutputParams
37 //===----------------------------------------------------------------------===//
38 
39 bool mlir::lsp::fromJSON(const llvm::json::Value &value,
40  PDLLViewOutputKind &result, llvm::json::Path path) {
41  if (std::optional<StringRef> str = value.getAsString()) {
42  if (*str == "ast") {
43  result = PDLLViewOutputKind::AST;
44  return true;
45  }
46  if (*str == "mlir") {
47  result = PDLLViewOutputKind::MLIR;
48  return true;
49  }
50  if (*str == "cpp") {
51  result = PDLLViewOutputKind::CPP;
52  return true;
53  }
54  }
55  return false;
56 }
57 
58 bool mlir::lsp::fromJSON(const llvm::json::Value &value,
59  PDLLViewOutputParams &result, llvm::json::Path path) {
60  llvm::json::ObjectMapper o(value, path);
61  return o && o.map("uri", result.uri) && o.map("kind", result.kind);
62 }
63 
64 //===----------------------------------------------------------------------===//
65 // PDLLViewOutputResult
66 //===----------------------------------------------------------------------===//
67 
68 llvm::json::Value mlir::lsp::toJSON(const PDLLViewOutputResult &value) {
69  return llvm::json::Object{{"output", value.output}};
70 }
static bool mapOptOrNull(const llvm::json::Value &params, llvm::StringLiteral prop, T &out, llvm::json::Path path)
Definition: Protocol.cpp:22
llvm::json::Value toJSON(const URIForFile &value)
Add support for JSON serialization.
Definition: Protocol.cpp:257
PDLLViewOutputKind
The type of output to view from PDLL.
Definition: Protocol.h:32
bool fromJSON(const llvm::json::Value &value, URIForFile &result, llvm::json::Path path)
Definition: Protocol.cpp:242
Include the generated interface declarations.
Represents the parameters used when viewing the output of a PDLL file.
Definition: Protocol.h:39
URIForFile uri
The URI of the document to view the output of.
Definition: Protocol.h:41
PDLLViewOutputKind kind
The kind of output to generate.
Definition: Protocol.h:44
Represents the result of viewing the output of a PDLL file.
Definition: Protocol.h:58
std::string output
The string representation of the output.
Definition: Protocol.h:60