Skip to content

Commit e507e2a

Browse files
authored
[circt-verilog-lsp] Add support for package import indexing (#9023)
This commit adds support for looking up the definition of package imports
1 parent be4ffb6 commit e507e2a

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

lib/Tools/circt-verilog-lsp-server/VerilogServerImpl/VerilogServer.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "slang/diagnostics/DiagnosticClient.h"
2727
#include "slang/diagnostics/Diagnostics.h"
2828
#include "slang/driver/Driver.h"
29+
#include "slang/syntax/AllSyntax.h"
2930
#include "slang/syntax/SyntaxTree.h"
3031
#include "slang/text/SourceLocation.h"
3132
#include "slang/text/SourceManager.h"
@@ -557,6 +558,33 @@ struct VerilogIndexer : slang::ast::ASTVisitor<VerilogIndexer, true, true> {
557558
visitDefault(expr);
558559
}
559560

561+
void visit(const slang::ast::ExplicitImportSymbol &expr) {
562+
auto *def = expr.package();
563+
if (!def)
564+
return;
565+
566+
if (auto *syn = expr.getSyntax()) {
567+
if (auto *item = syn->as_if<slang::syntax::PackageImportItemSyntax>()) {
568+
insertSymbol(def, item->package.location(), /*isDefinition=*/false);
569+
}
570+
}
571+
visitDefault(expr);
572+
}
573+
574+
void visit(const slang::ast::WildcardImportSymbol &expr) {
575+
auto *def = expr.getPackage();
576+
if (!def)
577+
return;
578+
579+
if (auto *syn = expr.getSyntax()) {
580+
if (auto *item = syn->as_if<slang::syntax::PackageImportItemSyntax>()) {
581+
insertSymbol(def, item->package.location(), false);
582+
}
583+
}
584+
585+
visitDefault(expr);
586+
}
587+
560588
void visit(const slang::ast::VariableDeclStatement &expr) {
561589
insertSymbol(&expr.symbol, expr.sourceRange, /*isDefinition=*/true);
562590
visitDefault(expr);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: cd %S && circt-verilog-lsp-server -lit-test < %s | FileCheck %s
2+
// REQUIRES: slang
3+
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"verilog","capabilities":{},"trace":"off"}}
4+
// -----
5+
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{
6+
"uri":"test:///foo.sv",
7+
"languageId":"verilog",
8+
"version":1,
9+
"text":"package apack;\ntypedef logic test_t;\n endpackage\nmodule test \nimport apack::*;\nimport apack::test_t;\n(output out\n);ifdef#()i_dut(.out(out));\nendmodule"
10+
}}}
11+
// -----
12+
// Find definition of `apack`
13+
{"jsonrpc":"2.0","id":1,"method":"textDocument/definition","params":{
14+
"textDocument":{"uri":"test:///foo.sv"},
15+
"position":{"line":4,"character":10}
16+
}}
17+
// -----
18+
// CHECK: "id": 1,
19+
// CHECK-NEXT: "jsonrpc": "2.0",
20+
// CHECK-NEXT: "result": [
21+
// CHECK-NEXT: {
22+
// CHECK-NEXT: "range": {
23+
// CHECK-NEXT: "end": {
24+
// CHECK-NEXT: "character": 13,
25+
// CHECK-NEXT: "line": 0
26+
// CHECK-NEXT: },
27+
// CHECK-NEXT: "start": {
28+
// CHECK-NEXT: "character": 8,
29+
// CHECK-NEXT: "line": 0
30+
// CHECK-NEXT: }
31+
// CHECK-NEXT: },
32+
// CHECK-NEXT: "uri": "test:///foo.sv"
33+
// CHECK-NEXT: }
34+
// CHECK-NEXT: ]
35+
// CHECK-NEXT:}
36+
// Find definition of `apack`
37+
{"jsonrpc":"2.0","id":2,"method":"textDocument/definition","params":{
38+
"textDocument":{"uri":"test:///foo.sv"},
39+
"position":{"line":5,"character":10}
40+
}}
41+
// -----
42+
// CHECK: "id": 2,
43+
// CHECK-NEXT: "jsonrpc": "2.0",
44+
// CHECK-NEXT: "result": [
45+
// CHECK-NEXT: {
46+
// CHECK-NEXT: "range": {
47+
// CHECK-NEXT: "end": {
48+
// CHECK-NEXT: "character": 13,
49+
// CHECK-NEXT: "line": 0
50+
// CHECK-NEXT: },
51+
// CHECK-NEXT: "start": {
52+
// CHECK-NEXT: "character": 8,
53+
// CHECK-NEXT: "line": 0
54+
// CHECK-NEXT: }
55+
// CHECK-NEXT: },
56+
// CHECK-NEXT: "uri": "test:///foo.sv"
57+
// CHECK-NEXT: }
58+
// CHECK-NEXT: ]
59+
// CHECK-NEXT:}
60+
// -----
61+
// -----
62+
{"jsonrpc":"2.0","id":3,"method":"shutdown"}
63+
// -----
64+
{"jsonrpc":"2.0","method":"exit"}

0 commit comments

Comments
 (0)