CIRCT 22.0.0git
Loading...
Searching...
No Matches
VerilogTextFile.h
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// VerilogTextFile.h
10//
11// This header declares VerilogTextFile, a lightweight LSP-facing wrapper
12// around VerilogDocument that represents an open, editable Verilog source
13// buffer in the CIRCT Verilog LSP server.
14//
15// VerilogTextFile owns the current text contents and version (as tracked by
16// the LSP client) and rebuilds its VerilogDocument whenever the file is
17// opened or updated. It also forwards language queries (e.g. “go to
18// definition”, “find references”) to the underlying VerilogDocument, which
19// performs Slang-based parsing, indexing, and location translation.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LIB_CIRCT_TOOLS_CIRCT_VERILOG_LSP_SERVER_VERILOGTEXTFILE_H_
24#define LIB_CIRCT_TOOLS_CIRCT_VERILOG_LSP_SERVER_VERILOGTEXTFILE_H_
25
26#include "llvm/Support/LSP/Protocol.h"
27
28#include "VerilogDocument.h"
29
30namespace circt {
31namespace lsp {
32
33struct VerilogServerContext;
34
35/// This class represents a text file containing one or more Verilog
36/// documents.
38public:
39 /// Initialize a new VerilogTextFile and its VerilogDocument.
40 /// Thread-safe; acquires contentMutex and docMutex.
42 const llvm::lsp::URIForFile &uri,
43 llvm::StringRef fileContents, int64_t version,
44 std::vector<llvm::lsp::Diagnostic> &diagnostics);
45
46 /// Return the current version of this text file.
47 /// Thread-safe.
48 int64_t getVersion() const { return version; }
49
50 /// Update the file to the new version using the provided set of content
51 /// changes. Returns failure if the update was unsuccessful.
52 /// Thread-safe; acquires contentMutex and docMutex.
53 llvm::LogicalResult
54 update(const llvm::lsp::URIForFile &uri, int64_t newVersion,
55 llvm::ArrayRef<llvm::lsp::TextDocumentContentChangeEvent> changes,
56 std::vector<llvm::lsp::Diagnostic> &diagnostics);
57
58 /// Return position of definition of an object pointed to by pos.
59 /// Thread-safe; acquires docMutex.
60 void getLocationsOf(const llvm::lsp::URIForFile &uri,
61 llvm::lsp::Position defPos,
62 std::vector<llvm::lsp::Location> &locations);
63
64 /// Return all references to an object pointed to by pos.
65 /// Thread-safe; acquires docMutex.
66 void findReferencesOf(const llvm::lsp::URIForFile &uri,
67 llvm::lsp::Position pos,
68 std::vector<llvm::lsp::Location> &references);
69
70 /// Return document for read access.
71 /// Thread-safe; acquires docMutex.
72 std::shared_ptr<VerilogDocument> getDocument();
73
74 /// Override document after update.
75 /// Thread-safe; acquires docMutex.
76 void setDocument(std::shared_ptr<VerilogDocument> newDoc);
77
78private:
79 /// Initialize the text file from the given file contents.
80 /// NOT thread-safe. ONLY call with contentMutex acquired!
81 /// Acquires docMutex.
82 void initialize(const llvm::lsp::URIForFile &uri, int64_t newVersion,
83 std::vector<llvm::lsp::Diagnostic> &diagnostics);
84
86
88
89 /// The full string contents of the file.
90 std::string contents;
91
92 /// The project-scale driver
93 std::unique_ptr<slang::driver::Driver> projectDriver;
94 std::vector<std::string> projectIncludeDirectories;
95 /// A mutex to control updates of contents.
96 /// Acquire BEFORE docMutex.
97 std::shared_mutex contentMutex;
98
99 /// The version of this file.
100 int64_t version = 0;
101
102 /// The chunks of this file. The order of these chunks is the order in which
103 /// they appear in the text file.
104 std::shared_ptr<circt::lsp::VerilogDocument> document;
105
106 /// A mutex to control updates of document;
107 /// Acquire AFTER contentMutex.
108 std::shared_mutex docMutex;
109};
110
111} // namespace lsp
112} // namespace circt
113
114#endif
This class represents a text file containing one or more Verilog documents.
void initialize(const llvm::lsp::URIForFile &uri, int64_t newVersion, std::vector< llvm::lsp::Diagnostic > &diagnostics)
Initialize the text file from the given file contents.
int64_t getVersion() const
Return the current version of this text file.
std::unique_ptr< slang::driver::Driver > projectDriver
The project-scale driver.
std::shared_ptr< VerilogDocument > getDocument()
Return document for read access.
VerilogServerContext & context
void getLocationsOf(const llvm::lsp::URIForFile &uri, llvm::lsp::Position defPos, std::vector< llvm::lsp::Location > &locations)
Return position of definition of an object pointed to by pos.
void findReferencesOf(const llvm::lsp::URIForFile &uri, llvm::lsp::Position pos, std::vector< llvm::lsp::Location > &references)
Return all references to an object pointed to by pos.
std::string contents
The full string contents of the file.
int64_t version
The version of this file.
llvm::LogicalResult update(const llvm::lsp::URIForFile &uri, int64_t newVersion, llvm::ArrayRef< llvm::lsp::TextDocumentContentChangeEvent > changes, std::vector< llvm::lsp::Diagnostic > &diagnostics)
Update the file to the new version using the provided set of content changes.
std::shared_mutex contentMutex
A mutex to control updates of contents.
std::shared_mutex docMutex
A mutex to control updates of document; Acquire AFTER contentMutex.
std::vector< std::string > projectIncludeDirectories
std::shared_ptr< circt::lsp::VerilogDocument > document
The chunks of this file.
void setDocument(std::shared_ptr< VerilogDocument > newDoc)
Override document after update.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.