From e9caf5de9c0ae137c31c32ea27bc17d7735689a3 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:51:59 -0500 Subject: Change how DeclRef::toText works (#5592) * Change how DeclRef::toText works We now ignore the decl-ref heirarchy since that only includes nodes with specialization info & simply walk up the tree of decls, while emitting any specializations present in the decl-ref. * Update some tests. Add cases for direct refs to generic params & Lookup decl refs --- source/slang/slang-ast-decl-ref.cpp | 77 ++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/slang/slang-ast-decl-ref.cpp b/source/slang/slang-ast-decl-ref.cpp index b2cc99ae4..29ab7f86e 100644 --- a/source/slang/slang-ast-decl-ref.cpp +++ b/source/slang/slang-ast-decl-ref.cpp @@ -312,7 +312,82 @@ DeclRefBase* DeclRefBase::getBase() } void DeclRefBase::toText(StringBuilder& out) { - SLANG_AST_NODE_VIRTUAL_CALL(DeclRefBase, toText, (out)); + if (auto lookupDeclRef = as(this)) + { + lookupDeclRef->_toTextOverride(out); + return; + } + + if (as(this->getDecl())) + { + SLANG_ASSERT(as(this)); + out << this->getDecl()->getName()->text; + return; + } + else if (as(this->getDecl())) + { + SLANG_ASSERT(as(this)); + out << this->getDecl()->getName()->text; + return; + } + + SubstitutionSet substSet(this); + + List decls; + for (auto dd = getDecl(); dd; dd = dd->parentDecl) + { + // Skip the top-level decl. + if (as(dd)) + continue; + + // Skip base decls in generic containers. We will handle them when we handle the generic + // decl. + // + if (dd->parentDecl && as(dd->parentDecl)) + continue; + + decls.add(dd); + } + + decls.reverse(); + + bool first = true; + for (auto decl : decls) + { + if (!first) + out << "."; + first = false; + + if (auto name = decl->getName()) + { + out << name->text; + + // If there are any specializations for this decl, emit them here: + if (auto genericDecl = as(decl)) + { + if (auto genericAppDeclRef = substSet.findGenericAppDeclRef(genericDecl)) + { + Index paramCount = 0; + for (auto member : genericDecl->members) + if (as(member) || + as(member)) + paramCount++; + out << "<"; + auto args = genericAppDeclRef->getArgs(); + Index argCount = args.getCount(); + for (Index aa = 0; aa < Math::Min(paramCount, argCount); ++aa) + { + if (aa != 0) + out << ", "; + args[aa]->toText(out); + } + out << ">"; + } + } + + // TODO: What do we do about extensions? + } + } } Name* DeclRefBase::getName() const -- cgit v1.2.3