summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-syntax.cpp
diff options
context:
space:
mode:
authorAlexey Panteleev <alpanteleev@nvidia.com>2022-04-01 10:56:02 -0700
committerGitHub <noreply@github.com>2022-04-01 10:56:02 -0700
commit2ddd252db192ab4376994d34cb9be862f97b5449 (patch)
treeca828032001de1b465e84a6c76b80177a4036649 /source/slang/slang-syntax.cpp
parent255fd5873f65a6b01d5385c277d55612dc3cc587 (diff)
Improved type printing (#2172)
Improved the type printing function to include the generic substitutions and parent types. Added a test for it, mismatching-types.slang
Diffstat (limited to 'source/slang/slang-syntax.cpp')
-rw-r--r--source/slang/slang-syntax.cpp64
1 files changed, 58 insertions, 6 deletions
diff --git a/source/slang/slang-syntax.cpp b/source/slang/slang-syntax.cpp
index 6b2140056..b9a5b8cd5 100644
--- a/source/slang/slang-syntax.cpp
+++ b/source/slang/slang-syntax.cpp
@@ -1132,17 +1132,69 @@ Index getFilterCountImpl(const ReflectClassInfo& clsInfo, MemberFilterStyle filt
return builder;
}
- void DeclRefBase::toText(StringBuilder& out) const
+ // Prints a partially qualified type name with generic substitutions.
+ static void _printNestedDecl(const Substitutions* substitutions, Decl* decl, StringBuilder& out)
{
- if (decl)
+ // If there is a parent scope for the declaration, print it first.
+ // Exclude top-level namespaces like `tu0` or `core`.
+ if (decl->parentDecl && !Slang::as<ModuleDecl>(decl->parentDecl))
{
- auto name = decl->getName();
- if (name)
+ auto parentGeneric = Slang::as<GenericDecl>(decl->parentDecl);
+
+ // Exclude function or operator names.
+ // Avoids excessively verbose messages like `func<T>(func::T)`
+ if (!(parentGeneric && Slang::as<CallableDecl>(parentGeneric->inner)))
{
- // TODO: need to print out substitutions too!
- out << name->text;
+ _printNestedDecl(substitutions, decl->parentDecl, out);
+
+ // If the parent is a generic for this type, skip *this* type.
+ // Avoids duplicate types like `MyType<T>::MyType`
+ if (parentGeneric && parentGeneric->inner == decl)
+ return;
+
+ out << ".";
}
}
+
+ // Print this type's name.
+ auto name = decl->getName();
+ if (name)
+ {
+ out << name->text;
+ }
+
+ // Look for generic substitutions on this type.
+ for (const Substitutions* subst = substitutions; subst; subst = subst->outer)
+ {
+ auto genericSubstitution = Slang::as<GenericSubstitution>(subst);
+ if (!genericSubstitution)
+ continue;
+
+ // If the substitution is for this type, print it.
+ if (genericSubstitution->genericDecl == decl)
+ {
+ out << "<";
+ bool isFirst = true;
+ for (const auto& it : genericSubstitution->args)
+ {
+ if (!isFirst)
+ out << ", ";
+ isFirst = false;
+ it->toText(out);
+ }
+ out << ">";
+
+ break;
+ }
+ }
+ }
+
+ void DeclRefBase::toText(StringBuilder& out) const
+ {
+ if (decl)
+ {
+ _printNestedDecl(substitutions, decl, out);
+ }
}
bool SubstitutionSet::equals(const SubstitutionSet& substSet) const