CIRCT 22.0.0git
Loading...
Searching...
No Matches
VerilogDocument.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// VerilogDocument.h
10//
11// This header declares the VerilogDocument class, which models a single open
12// Verilog/SystemVerilog source file within the CIRCT Verilog LSP server. It
13// owns a Slang driver and compilation unit for that file, provides diagnostic
14// and indexing functionality, and exposes utilities for translating between
15// LSP and Slang locations.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LIB_CIRCT_TOOLS_CIRCT_VERILOG_LSP_SERVER_VERILOGDOCUMENT_H_
20#define LIB_CIRCT_TOOLS_CIRCT_VERILOG_LSP_SERVER_VERILOGDOCUMENT_H_
21
22#include "slang/ast/Compilation.h"
23#include "slang/driver/Driver.h"
24#include "slang/text/SourceManager.h"
25
26#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/Support/LSP/Protocol.h"
30
31#include "VerilogIndex.h"
32
33namespace circt {
34namespace lsp {
35
36struct VerilogServerContext;
37
39public:
41 VerilogServerContext &globalContext, const llvm::lsp::URIForFile &uri,
42 llvm::StringRef contents, std::vector<llvm::lsp::Diagnostic> &diagnostics,
43 const slang::driver::Driver *projectDriver = nullptr,
44 const std::vector<std::string> &projectIncludeDirectories = {});
47
48 const llvm::lsp::URIForFile &getURI() const { return uri; }
49
50 const slang::SourceManager &getSlangSourceManager() const {
51 return driver.sourceManager;
52 }
53
54 // Return LSP location from slang location.
55 llvm::lsp::Location getLspLocation(slang::SourceLocation loc) const;
56 llvm::lsp::Location getLspLocation(slang::SourceRange range) const;
57
58 slang::BufferID getMainBufferID() const { return mainBufferId; }
59
60 //===--------------------------------------------------------------------===//
61 // Definitions and References
62 //===--------------------------------------------------------------------===//
63
64 void getLocationsOf(const llvm::lsp::URIForFile &uri,
65 const llvm::lsp::Position &defPos,
66 std::vector<llvm::lsp::Location> &locations);
67
68 void findReferencesOf(const llvm::lsp::URIForFile &uri,
69 const llvm::lsp::Position &pos,
70 std::vector<llvm::lsp::Location> &references);
71
72 std::optional<uint32_t> lspPositionToOffset(const llvm::lsp::Position &pos);
73 const char *getPointerFor(const llvm::lsp::Position &pos);
74
75private:
76 std::optional<std::pair<slang::BufferID, llvm::SmallString<128>>>
77 getOrOpenFile(llvm::StringRef filePath);
78
80
81 slang::BufferID mainBufferId;
82
83 // A map from a file name to the corresponding buffer ID in the LLVM
84 // source manager.
85 llvm::StringMap<std::pair<slang::BufferID, llvm::SmallString<128>>>
87
88 // The compilation result.
89 llvm::FailureOr<std::unique_ptr<slang::ast::Compilation>> compilation;
90
91 // The slang driver.
92 slang::driver::Driver driver;
93
94 /// The index of the parsed module.
95 std::unique_ptr<circt::lsp::VerilogIndex> index;
96
97 /// The precomputed line offsets for faster lookups
98 std::vector<uint32_t> lineOffsets;
99 void computeLineOffsets(std::string_view text);
100
101 // The URI of the document.
102 llvm::lsp::URIForFile uri;
103};
104
105} // namespace lsp
106} // namespace circt
107
108#endif
std::unique_ptr< circt::lsp::VerilogIndex > index
The index of the parsed module.
const char * getPointerFor(const llvm::lsp::Position &pos)
slang::driver::Driver driver
VerilogDocument(VerilogServerContext &globalContext, const llvm::lsp::URIForFile &uri, llvm::StringRef contents, std::vector< llvm::lsp::Diagnostic > &diagnostics, const slang::driver::Driver *projectDriver=nullptr, const std::vector< std::string > &projectIncludeDirectories={})
VerilogServerContext & globalContext
llvm::lsp::URIForFile uri
VerilogDocument(const VerilogDocument &)=delete
void getLocationsOf(const llvm::lsp::URIForFile &uri, const llvm::lsp::Position &defPos, std::vector< llvm::lsp::Location > &locations)
void computeLineOffsets(std::string_view text)
Build a vector of line start offsets (0-based).
std::optional< std::pair< slang::BufferID, llvm::SmallString< 128 > > > getOrOpenFile(llvm::StringRef filePath)
void findReferencesOf(const llvm::lsp::URIForFile &uri, const llvm::lsp::Position &pos, std::vector< llvm::lsp::Location > &references)
VerilogDocument & operator=(const VerilogDocument &)=delete
std::optional< uint32_t > lspPositionToOffset(const llvm::lsp::Position &pos)
const slang::SourceManager & getSlangSourceManager() const
llvm::StringMap< std::pair< slang::BufferID, llvm::SmallString< 128 > > > filePathMap
slang::BufferID getMainBufferID() const
const llvm::lsp::URIForFile & getURI() const
std::vector< uint32_t > lineOffsets
The precomputed line offsets for faster lookups.
llvm::FailureOr< std::unique_ptr< slang::ast::Compilation > > compilation
llvm::lsp::Location getLspLocation(slang::SourceLocation loc) const
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.