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