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 const llvm::lsp::URIForFile &uri, llvm::StringRef contents,
42 std::vector<llvm::lsp::Diagnostic> &diagnostics);
45
46 const llvm::lsp::URIForFile &getURI() const { return uri; }
47
48 const slang::SourceManager &getSlangSourceManager() const {
49 return driver.sourceManager;
50 }
51
52 // Return LSP location from slang location.
53 llvm::lsp::Location getLspLocation(slang::SourceLocation loc) const;
54 llvm::lsp::Location getLspLocation(slang::SourceRange range) const;
55
56 slang::BufferID getMainBufferID() const { return mainBufferId; }
57
58 //===--------------------------------------------------------------------===//
59 // Definitions and References
60 //===--------------------------------------------------------------------===//
61
62 void getLocationsOf(const llvm::lsp::URIForFile &uri,
63 const llvm::lsp::Position &defPos,
64 std::vector<llvm::lsp::Location> &locations);
65
66 void findReferencesOf(const llvm::lsp::URIForFile &uri,
67 const llvm::lsp::Position &pos,
68 std::vector<llvm::lsp::Location> &references);
69
70 std::optional<uint32_t> lspPositionToOffset(const llvm::lsp::Position &pos);
71 const char *getPointerFor(const llvm::lsp::Position &pos);
72
73private:
74 std::optional<std::pair<slang::BufferID, llvm::SmallString<128>>>
75 getOrOpenFile(llvm::StringRef filePath);
76
78
79 slang::BufferID mainBufferId;
80
81 // A map from a file name to the corresponding buffer ID in the LLVM
82 // source manager.
83 llvm::StringMap<std::pair<slang::BufferID, llvm::SmallString<128>>>
85
86 // The compilation result.
87 llvm::FailureOr<std::unique_ptr<slang::ast::Compilation>> compilation;
88
89 // The slang driver.
90 slang::driver::Driver driver;
91
92 /// The index of the parsed module.
93 std::unique_ptr<circt::lsp::VerilogIndex> index;
94
95 /// The precomputed line offsets for faster lookups
96 std::vector<uint32_t> lineOffsets;
97 void computeLineOffsets(std::string_view text);
98
99 // The URI of the document.
100 llvm::lsp::URIForFile uri;
101};
102
103} // namespace lsp
104} // namespace circt
105
106#endif
std::unique_ptr< circt::lsp::VerilogIndex > index
The index of the parsed module.
VerilogDocument(VerilogServerContext &globalContext, const llvm::lsp::URIForFile &uri, llvm::StringRef contents, std::vector< llvm::lsp::Diagnostic > &diagnostics)
const char * getPointerFor(const llvm::lsp::Position &pos)
slang::driver::Driver driver
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.