12using namespace ImportVerilog;
17 :
public slang::ast::ASTVisitor<InstBodyVisitor,
21 InstBodyVisitor(
Context &context,
const slang::ast::Symbol &outermostModule)
22 : context(context), outermostModule(outermostModule) {}
24 void handle(
const slang::ast::InstanceSymbol &instNode) {
28 void handle(
const slang::ast::HierarchicalValueExpression &expr) {
29 auto builder = context.builder;
30 auto *currentInstBody =
31 expr.symbol.getParentScope()->getContainingInstance();
32 auto *outermostInstBody =
33 outermostModule.as_if<slang::ast::InstanceBodySymbol>();
37 if (currentInstBody == outermostInstBody)
40 auto hierName = builder.getStringAttr(expr.symbol.name);
41 const slang::ast::InstanceBodySymbol *parentInstBody =
nullptr;
44 std::function<void(
const slang::ast::InstanceBodySymbol *,
bool)>
45 collectHierarchicalPaths = [&](
auto sym,
bool isUpward) {
48 if (!context.sameHierPaths.contains(hierName) ||
49 !context.hierPaths.contains(sym)) {
50 context.hierPaths[sym].push_back(
53 isUpward ? slang::ast::ArgumentDirection::Out
54 : slang::ast::ArgumentDirection::In,
56 context.sameHierPaths.insert(hierName);
62 sym->parentInstance->getParentScope()->getContainingInstance();
68 if (parentInstBody && parentInstBody != outermostInstBody) {
70 builder.getStringAttr(sym->parentInstance->name +
71 llvm::Twine(
".") + hierName.getValue());
72 collectHierarchicalPaths(parentInstBody, isUpward);
75 if (parentInstBody && parentInstBody != currentInstBody)
76 collectHierarchicalPaths(parentInstBody, isUpward);
81 auto *tempInstBody = currentInstBody;
82 while (tempInstBody) {
83 tempInstBody = tempInstBody->parentInstance->getParentScope()
84 ->getContainingInstance();
85 if (tempInstBody == outermostInstBody) {
86 collectHierarchicalPaths(currentInstBody,
true);
91 hierName = builder.getStringAttr(currentInstBody->parentInstance->name +
92 llvm::Twine(
".") + hierName.getValue());
93 collectHierarchicalPaths(outermostInstBody,
false);
97 const slang::ast::Symbol &outermostModule;
102void Context::traverseInstanceBody(
const slang::ast::Symbol &symbol) {
103 if (
auto *instBodySymbol = symbol.as_if<slang::ast::InstanceBodySymbol>())
104 for (
auto &member : instBodySymbol->members()) {
105 auto &outermostModule = member.getParentScope()->asSymbol();
106 InstBodyVisitor visitor(*
this, outermostModule);
107 member.visit(visitor);
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
void traverseInstanceBody(const slang::ast::Symbol &symbol)
Hierarchical path information.