CIRCT 22.0.0git
Loading...
Searching...
No Matches
VerilogServer.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 implements the VerilogServer class, the top-level coordinator for
10// the CIRCT Verilog LSP server. It manages the lifetime and synchronization of
11// all open Verilog source files and provides the high-level entry points for
12// Language Server Protocol (LSP) requests.
13//
14// Responsibilities:
15// * Maintain a map of open documents (`VerilogTextFile` instances) indexed by
16// their URI filenames.
17// * Create, update, and remove documents in response to LSP notifications
18// such as `textDocument/didOpen`, `didChange`, and `didClose`.
19// * Route definition and reference queries to the appropriate
20// VerilogTextFile, which in turn delegates to its `VerilogDocument`.
21// * Ensure each document remains synchronized with its latest text version
22// and up-to-date semantic index.
23//
24// Internal structure:
25// - Uses an internal `Impl` struct (pImpl pattern) to encapsulate the
26// server’s file map and its shared `VerilogServerContext`.
27// - Each document is owned via a `std::unique_ptr<VerilogTextFile>`,
28// allowing clean removal and automatic teardown when the file is closed.
29//
30//===----------------------------------------------------------------------===//
31
32#include "llvm/Support/LSP/Protocol.h"
33
34#include "circt/Support/LLVM.h"
36
37#include "VerilogServer.h"
39#include "VerilogTextFile.h"
40
41#include <memory>
42#include <optional>
43
44using namespace llvm::lsp;
45
47 explicit Impl(const VerilogServerOptions &options) : context(options) {}
48
49 /// The files held by the server, mapped by their URI file name.
50 llvm::StringMap<std::unique_ptr<VerilogTextFile>> files;
51
53};
54
56 : impl(std::make_unique<Impl>(options)) {}
58
60 const URIForFile &uri, StringRef contents, int64_t version,
61 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
62
63 impl->files[uri.file()] = std::make_unique<VerilogTextFile>(
64 impl->context, uri, contents, version, diagnostics);
65}
66
68 const URIForFile &uri,
69 ArrayRef<llvm::lsp::TextDocumentContentChangeEvent> changes,
70 int64_t version, std::vector<llvm::lsp::Diagnostic> &diagnostics) {
71 // Check that we actually have a document for this uri.
72 auto it = impl->files.find(uri.file());
73 if (it == impl->files.end())
74 return;
75
76 // Try to update the document. If we fail, erase the file from the server. A
77 // failed updated generally means we've fallen out of sync somewhere.
78 if (failed(it->second->update(uri, version, changes, diagnostics)))
79 impl->files.erase(it);
80}
81
82std::optional<int64_t>
84 auto it = impl->files.find(uri.file());
85 if (it == impl->files.end())
86 return std::nullopt;
87
88 int64_t version = it->second->getVersion();
89 impl->files.erase(it);
90 return version;
91}
92
94 const URIForFile &uri, const Position &defPos,
95 std::vector<llvm::lsp::Location> &locations) {
96 auto fileIt = impl->files.find(uri.file());
97 if (fileIt != impl->files.end())
98 fileIt->second->getLocationsOf(uri, defPos, locations);
99}
100
102 const URIForFile &uri, const Position &pos,
103 std::vector<llvm::lsp::Location> &references) {
104 auto fileIt = impl->files.find(uri.file());
105 if (fileIt != impl->files.end())
106 fileIt->second->findReferencesOf(uri, pos, references);
107}
std::optional< int64_t > removeDocument(const URIForFile &uri)
Remove the document with the given uri.
void updateDocument(const URIForFile &uri, llvm::ArrayRef< TextDocumentContentChangeEvent > changes, int64_t version, std::vector< Diagnostic > &diagnostics)
Update the document, with the provided version, at the given URI.
VerilogServer(const circt::lsp::VerilogServerOptions &options)
void addDocument(const URIForFile &uri, llvm::StringRef contents, int64_t version, std::vector< Diagnostic > &diagnostics)
Add the document, with the provided version, at the given URI.
void getLocationsOf(const URIForFile &uri, const llvm::lsp::Position &defPos, std::vector< llvm::lsp::Location > &locations)
Return the locations of the object pointed at by the given position.
void findReferencesOf(const URIForFile &uri, const llvm::lsp::Position &pos, std::vector< llvm::lsp::Location > &references)
Find all references of the object pointed at by the given position.
llvm::lsp::URIForFile URIForFile
llvm::StringMap< std::unique_ptr< VerilogTextFile > > files
The files held by the server, mapped by their URI file name.
Impl(const VerilogServerOptions &options)