From 2ddd252db192ab4376994d34cb9be862f97b5449 Mon Sep 17 00:00:00 2001 From: Alexey Panteleev Date: Fri, 1 Apr 2022 10:56:02 -0700 Subject: 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 --- source/slang/slang-syntax.cpp | 64 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) (limited to 'source/slang/slang-syntax.cpp') 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(decl->parentDecl)) { - auto name = decl->getName(); - if (name) + auto parentGeneric = Slang::as(decl->parentDecl); + + // Exclude function or operator names. + // Avoids excessively verbose messages like `func(func::T)` + if (!(parentGeneric && Slang::as(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::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(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 -- cgit v1.2.3