72 lines · cpp
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-exception6//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 ¶ms,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// PDLLViewOutputParams38//===----------------------------------------------------------------------===//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") {44 result = PDLLViewOutputKind::AST;45 return true;46 }47 if (*str == "mlir") {48 result = PDLLViewOutputKind::MLIR;49 return true;50 }51 if (*str == "cpp") {52 result = PDLLViewOutputKind::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// PDLLViewOutputResult67//===----------------------------------------------------------------------===//68 69llvm::json::Value mlir::lsp::toJSON(const PDLLViewOutputResult &value) {70 return llvm::json::Object{{"output", value.output}};71}72