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 --- external/vulkan | 2 +- source/slang/slang-ast-decl-ref.cpp | 77 +++++++++++++++++++++- tests/diagnostics/mismatching-types.slang.expected | 6 +- .../unit-test-decl-tree-reflection.cpp | 7 +- 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/external/vulkan b/external/vulkan index 577baa050..f864bc6df 160000 --- a/external/vulkan +++ b/external/vulkan @@ -1 +1 @@ -Subproject commit 577baa05033cf1d9236b3d078ca4b3269ed87a2b +Subproject commit f864bc6dfe6229a399566e979c16795386d0f308 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 diff --git a/tests/diagnostics/mismatching-types.slang.expected b/tests/diagnostics/mismatching-types.slang.expected index 3bbab624d..65a6df32e 100644 --- a/tests/diagnostics/mismatching-types.slang.expected +++ b/tests/diagnostics/mismatching-types.slang.expected @@ -15,13 +15,13 @@ tests/diagnostics/mismatching-types.slang(57): error 30019: expected an expressi tests/diagnostics/mismatching-types.slang(59): error 30019: expected an expression of type 'GenericOuter.NonGenericInner', got 'GenericOuter.NonGenericInner' a.ng = b.ng; ^~ -tests/diagnostics/mismatching-types.slang(61): error 30019: expected an expression of type 'GenericInner', got 'int' +tests/diagnostics/mismatching-types.slang(61): error 30019: expected an expression of type 'NonGenericOuter.GenericInner', got 'int' c.i = 0; ^ -tests/diagnostics/mismatching-types.slang(63): error 30019: expected an expression of type 'GenericInner', got 'GenericInner' +tests/diagnostics/mismatching-types.slang(63): error 30019: expected an expression of type 'NonGenericOuter.GenericInner', got 'NonGenericOuter.GenericInner' c.i = c.f; ^ -tests/diagnostics/mismatching-types.slang(65): error 30019: expected an expression of type 'GenericInner.ReallyNested', got 'int' +tests/diagnostics/mismatching-types.slang(65): error 30019: expected an expression of type 'NonGenericOuter.GenericInner.ReallyNested', got 'int' c.i.n = 0; ^ tests/diagnostics/mismatching-types.slang(74): error 30019: expected an expression of type 'Texture1D', got 'Texture1D' diff --git a/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp b/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp index c8bd9cd02..2ceb9981b 100644 --- a/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp +++ b/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp @@ -242,7 +242,6 @@ SLANG_UNIT_TEST(declTreeReflection) { auto type = compositeProgram->getLayout()->findTypeByName("MyGenericType"); SLANG_CHECK(type != nullptr); - // SLANG_CHECK(type->getKind() == slang::DeclReflection::Kind::Struct); SLANG_CHECK(getTypeFullName(type) == "MyGenericType"); auto funcReflection = compositeProgram->getLayout()->findFunctionByNameInType(type, "g"); SLANG_CHECK(funcReflection != nullptr); @@ -485,6 +484,12 @@ SLANG_UNIT_TEST(declTreeReflection) SLANG_CHECK(getTypeFullName(specializedMethodWithFloat->getReturnType()) == "float"); } + // Check getTypeFullName() on nested objects. + { + auto structType = compositeProgram->getLayout()->findTypeByName("MyNamespace::MyStruct"); + SLANG_CHECK(getTypeFullName(structType) == "MyNamespace.MyStruct"); + } + // Check iterators { unsigned int count = 0; -- cgit v1.2.3