Skip to content

Commit

Permalink
Allow identifier-like symbols for the import alias name.
Browse files Browse the repository at this point in the history
Fixes crash in verilog::MakeDPIImport() with signature like:
  Check failed: E(leaf.get().token_enum) == expected_token_enum (293 vs. 292)

PiperOrigin-RevId: 303818326
  • Loading branch information
fangism authored and hzeller committed Mar 30, 2020
1 parent 862813b commit 2cad9a0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions verilog/CST/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ cc_library(
":verilog_nonterminals",
":verilog_treebuilder_utils",
"//common/analysis:syntax_tree_search",
"//common/text:concrete_syntax_leaf",
"//common/text:concrete_syntax_tree",
"//common/text:symbol",
"//common/text:tree_utils",
"//verilog/parser:verilog_token_classifications",
"//verilog/parser:verilog_token_enum",
],
)
Expand Down
25 changes: 24 additions & 1 deletion verilog/CST/DPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
#include <vector>

#include "common/analysis/syntax_tree_search.h"
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/symbol.h"
#include "common/text/tree_utils.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/CST/verilog_treebuilder_utils.h"
#include "verilog/parser/verilog_token_classifications.h"
#include "verilog/parser/verilog_token_enum.h"

namespace verilog {
Expand All @@ -38,7 +40,10 @@ verible::SymbolPtr MakeDPIImport(T0&& keyword, T1&& spec, T2&& property,
T3&& id, T4&& equals, T5&& proto, T6&& semi) {
verible::CheckSymbolAsLeaf(*keyword, verilog_tokentype::TK_import);
verible::CheckSymbolAsLeaf(*spec, verilog_tokentype::TK_StringLiteral);
verible::CheckOptionalSymbolAsLeaf(id, verilog_tokentype::SymbolIdentifier);
if (id != nullptr) {
CHECK(IsIdentifierLike(
verilog_tokentype(verible::SymbolCastToLeaf(*id).get().token_enum)));
}
verible::CheckOptionalSymbolAsLeaf(equals, '=');
CHECK(verible::SymbolCastToNode(*proto).MatchesTagAnyOf(
{NodeEnum::kFunctionPrototype, NodeEnum::kTaskPrototype}));
Expand All @@ -50,6 +55,24 @@ verible::SymbolPtr MakeDPIImport(T0&& keyword, T1&& spec, T2&& property,
verible::ExtendNode(std::forward<T5>(proto), std::forward<T6>(semi)));
}

// Partial specialization provided as a workaround to passing nullptr
// in positions 3 and 4 (optional symbols). Compiler is not guaranteed
// to deduce to that some paths are not reachble/applicable.
template <typename T0, typename T1, typename T2, typename T3, typename T4>
verible::SymbolPtr MakeDPIImport(T0&& keyword, T1&& spec, T2&& property,
nullptr_t id, nullptr_t equals, T3&& proto,
T4&& semi) {
verible::CheckSymbolAsLeaf(*keyword, verilog_tokentype::TK_import);
verible::CheckSymbolAsLeaf(*spec, verilog_tokentype::TK_StringLiteral);
CHECK(verible::SymbolCastToNode(*proto).MatchesTagAnyOf(
{NodeEnum::kFunctionPrototype, NodeEnum::kTaskPrototype}));
ExpectString(semi, ";");
return verible::MakeTaggedNode(
NodeEnum::kDPIImportItem, std::forward<T0>(keyword),
std::forward<T1>(spec), std::forward<T2>(property), id, equals,
verible::ExtendNode(std::forward<T3>(proto), std::forward<T4>(semi)));
}

// Find all DPI imports.
std::vector<verible::TreeSearchMatch> FindAllDPIImports(const verible::Symbol&);

Expand Down
10 changes: 10 additions & 0 deletions verilog/parser/verilog_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,16 @@ static const char* kModuleTests[] = {
"import pkg::ident;\n"
"export \"name\" task rabbit;\n"
"endmodule",
"import \"DPI\" function bar;\n",
"import \"DPI-C\" function bar;\n",
"import \"DPI-C\" foo = function bar;\n",
"import \"DPI-C\" foo = function void bar;\n",
"import \"DPI-C\" foo = function void bar();\n",
"import \"DPI-C\" foo = function void bar(input chandle ch);\n",
// using escaped identifier as alias:
"import \"DPI-C\" \\foo = function void bar(input chandle ch);\n",
"import \"DPI-C\" `foo = function void bar(input chandle ch);\n", // macro
"import \"DPI-C\" foo = function longint unsigned bar(input chandle ch);\n",
"module zoobar ();\n"
"import \"DPI-C\" function void fizzbuzz (\n"
" input in val, output int nothing);\n"
Expand Down

0 comments on commit 2cad9a0

Please sign in to comment.