Skip to content

Commit

Permalink
verilog: CST: add getters for NetVariableAssignment
Browse files Browse the repository at this point in the history
  • Loading branch information
IEncinas10 committed Nov 21, 2024
1 parent 8743a3c commit 791ffa0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
13 changes: 13 additions & 0 deletions verilog/CST/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,17 @@ const verible::SyntaxTreeNode *GetAssignModifyLhs(
return verible::GetSubtreeAsNode(assign_modify,
NodeEnum::kAssignModifyStatement, 0);
}

const verible::SyntaxTreeNode *GetNetVariableAssignmentLhs(
const verible::SyntaxTreeNode &assignment) {
return verible::GetSubtreeAsNode(assignment, NodeEnum::kNetVariableAssignment,
0);
}

const verible::SyntaxTreeLeaf *GetNetVariableAssignmentOperator(
const verible::SyntaxTreeNode &assignment) {
return verible::GetSubtreeAsLeaf(assignment, NodeEnum::kNetVariableAssignment,
1);
}

} // namespace verilog
10 changes: 10 additions & 0 deletions verilog/CST/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ const verible::SyntaxTreeNode *GetAssignModifyRhs(
const verible::SyntaxTreeNode *GetAssignModifyLhs(
const verible::SyntaxTreeNode &assign_modify);

// Return the left hand side (Lhs) from a NetVariableAssignment
// Example: get 'x' from 'x = y'
const verible::SyntaxTreeNode *GetNetVariableAssignmentLhs(
const verible::SyntaxTreeNode &assignment);

// Return the operator from a NetVariableAssignment
// Example: get '=' from 'x = y'
const verible::SyntaxTreeLeaf *GetNetVariableAssignmentOperator(
const verible::SyntaxTreeNode &assignment);

} // namespace verilog

#endif // VERIBLE_VERILOG_CST_STATEMENT_H_
93 changes: 93 additions & 0 deletions verilog/CST/statement_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1745,5 +1745,98 @@ TEST(GetAssignModifyRhs, Various) {
}
}

TEST(GetNetVariableAssignmentLhs, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m;\nendmodule\n"},
{"module m;\n"
"reg k;\n"
"always_comb begin\n",
{kTag, "k"},
" = 1;\nend\n",
"endmodule\n"},
{"module m;\n"
"reg k;\n"
"always_comb begin\n",
"k &= 1;\nend\n"
"endmodule\n"},
{"module m;\n"
"reg k;\n"
"always_comb begin\n"
"k |= 1;\nend\n"
"endmodule\n"},
{"module m;\n"
"reg k;\n"
"always_ff begin\n"
"k <= 1;\nend\n"
"endmodule\n"},
};
for (const auto &test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView &text_structure) {
const auto &root = text_structure.SyntaxTree();
const auto &net_var_assignments = SearchSyntaxTree(
*ABSL_DIE_IF_NULL(root), NodekNetVariableAssignment());

std::vector<TreeSearchMatch> left_hand_sides;
for (const auto &assignment : net_var_assignments) {
const auto *lhs = GetNetVariableAssignmentLhs(
verible::SymbolCastToNode(*assignment.match));
left_hand_sides.emplace_back(
TreeSearchMatch{lhs, {/* ignored context */}});
}
return left_hand_sides;
});
}
}

TEST(GetNetVariableAssignmentOperator, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m;\nendmodule\n"},
{"module m;\n"
"reg k;\n"
"always_comb begin\n",
"k ",
{kTag, "="},
" 1;\nend\n",
"endmodule\n"},
{"module m;\n"
"reg k;\n"
"always_comb begin\n",
"k &= 1;\nend\n"
"endmodule\n"},
{"module m;\n"
"reg k;\n"
"always_comb begin\n"
"k |= 1;\nend\n"
"endmodule\n"},
{"module m;\n"
"reg k;\n"
"always_ff begin\n"
"k <= 1;\nend\n"
"endmodule\n"},
};
for (const auto &test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView &text_structure) {
const auto &root = text_structure.SyntaxTree();
const auto &net_var_assignments = SearchSyntaxTree(
*ABSL_DIE_IF_NULL(root), NodekNetVariableAssignment());

std::vector<TreeSearchMatch> left_hand_sides;
for (const auto &assignment : net_var_assignments) {
const auto *lhs = GetNetVariableAssignmentOperator(
verible::SymbolCastToNode(*assignment.match));
left_hand_sides.emplace_back(
TreeSearchMatch{lhs, {/* ignored context */}});
}
return left_hand_sides;
});
}
}

} // namespace
} // namespace verilog

0 comments on commit 791ffa0

Please sign in to comment.