Skip to content

Commit

Permalink
Change how DeclRef::toText works (#5592)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
saipraveenb25 authored Nov 21, 2024
1 parent 3d36e0c commit e9caf5d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
77 changes: 76 additions & 1 deletion source/slang/slang-ast-decl-ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,82 @@ DeclRefBase* DeclRefBase::getBase()
}
void DeclRefBase::toText(StringBuilder& out)
{
SLANG_AST_NODE_VIRTUAL_CALL(DeclRefBase, toText, (out));
if (auto lookupDeclRef = as<LookupDeclRef>(this))
{
lookupDeclRef->_toTextOverride(out);
return;
}

if (as<GenericTypeParamDecl>(this->getDecl()))
{
SLANG_ASSERT(as<DirectDeclRef>(this));
out << this->getDecl()->getName()->text;
return;
}
else if (as<GenericValueParamDecl>(this->getDecl()))
{
SLANG_ASSERT(as<DirectDeclRef>(this));
out << this->getDecl()->getName()->text;
return;
}

SubstitutionSet substSet(this);

List<Decl*> decls;
for (auto dd = getDecl(); dd; dd = dd->parentDecl)
{
// Skip the top-level decl.
if (as<ModuleDecl>(dd))
continue;

// Skip base decls in generic containers. We will handle them when we handle the generic
// decl.
//
if (dd->parentDecl && as<GenericDecl>(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<GenericDecl>(decl))
{
if (auto genericAppDeclRef = substSet.findGenericAppDeclRef(genericDecl))
{
Index paramCount = 0;
for (auto member : genericDecl->members)
if (as<GenericTypeParamDeclBase>(member) ||
as<GenericValueParamDecl>(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
Expand Down
6 changes: 3 additions & 3 deletions tests/diagnostics/mismatching-types.slang.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>.NonGenericInner', got 'GenericOuter<float>.NonGenericInner'
a.ng = b.ng;
^~
tests/diagnostics/mismatching-types.slang(61): error 30019: expected an expression of type 'GenericInner<int>', got 'int'
tests/diagnostics/mismatching-types.slang(61): error 30019: expected an expression of type 'NonGenericOuter.GenericInner<int>', got 'int'
c.i = 0;
^
tests/diagnostics/mismatching-types.slang(63): error 30019: expected an expression of type 'GenericInner<int>', got 'GenericInner<float>'
tests/diagnostics/mismatching-types.slang(63): error 30019: expected an expression of type 'NonGenericOuter.GenericInner<int>', got 'NonGenericOuter.GenericInner<float>'
c.i = c.f;
^
tests/diagnostics/mismatching-types.slang(65): error 30019: expected an expression of type 'GenericInner<int>.ReallyNested', got 'int'
tests/diagnostics/mismatching-types.slang(65): error 30019: expected an expression of type 'NonGenericOuter.GenericInner<int>.ReallyNested', got 'int'
c.i.n = 0;
^
tests/diagnostics/mismatching-types.slang(74): error 30019: expected an expression of type 'Texture1D<int>', got 'Texture1D<float>'
Expand Down
7 changes: 6 additions & 1 deletion tools/slang-unit-test/unit-test-decl-tree-reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ SLANG_UNIT_TEST(declTreeReflection)
{
auto type = compositeProgram->getLayout()->findTypeByName("MyGenericType<half>");
SLANG_CHECK(type != nullptr);
// SLANG_CHECK(type->getKind() == slang::DeclReflection::Kind::Struct);
SLANG_CHECK(getTypeFullName(type) == "MyGenericType<half>");
auto funcReflection = compositeProgram->getLayout()->findFunctionByNameInType(type, "g");
SLANG_CHECK(funcReflection != nullptr);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e9caf5d

Please sign in to comment.