From 3726194fbe3da234eb30b6371e5b4ab1ea388f93 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Sat, 2 Feb 2019 11:58:54 -0500 Subject: Feature/as refactor review (#821) * Replace dynamicCast with as where does not change behavior (ie not Type derived). Use free function where scoping is clear. * Replace uses of dynamicCast with as when there is no difference in behavior. * Remove the IsXXXX methods from Type. * Don't have separate smart pointer to store canonicalType on Type. * Simplify Slang.FilteredMemberRefList.Adjust, such does the cast directly. * Use free as where appropriate. * Use free function version of casts where appropriate. * Fix text in casting.md * Fix typos in decl-refs.md * Remove the uses of free function as on RefDecl. Add 'canAs' to RefDecl as a way to test if a cast is possible. Moved 'as' into RefDeclBase. * Use 'is' to test for as cast on smart pointers. Fix small scope issue. * * Cache stringType and enumTypeType on the Session * Make DeclRefType::Create return a RefPtr * Make casting of result use the *method* .as (cos using free function would mean objects being wrongly destroyed) * Make results from createInstance ref'd to avoid possible leaks. * Fix typo in template parameter for is on RefPtr. --- source/slang/syntax.h | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'source/slang/syntax.h') diff --git a/source/slang/syntax.h b/source/slang/syntax.h index 076d62d71..e354fff05 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -420,6 +420,10 @@ namespace Slang bool Equals(SubstitutionSet substSet) const; int GetHashCode() const; }; + + template + struct DeclRef; + // A reference to a declaration, which may include // substitutions for generic parameters. struct DeclRefBase @@ -461,6 +465,13 @@ namespace Slang // Apply substitutions to this declaration reference DeclRefBase SubstituteImpl(SubstitutionSet subst, int* ioDiff); + // Returns true if 'as' will return a valid cast + template + bool is() const { return Slang::as(decl) != nullptr; } + + // "dynamic cast" to a more specific declaration reference type + template + DeclRef as() const; // Check if this is an equivalent declaration reference to another bool Equals(DeclRefBase const& declRef) const; @@ -502,16 +513,6 @@ namespace Slang { } - // "dynamic cast" to a more specific declaration reference type - template - DeclRef as() const - { - DeclRef result; - result.decl = Slang::as(decl); - result.substitutions = substitutions; - return result; - } - T* getDecl() const { return (T*)decl; @@ -537,7 +538,7 @@ namespace Slang return DeclRefBase::Substitute(expr); } - // Apply substitutions to a type or ddeclaration + // Apply substitutions to a type or declaration template DeclRef Substitute(DeclRef declRef) const { @@ -556,7 +557,15 @@ namespace Slang } }; - + template + DeclRef DeclRefBase::as() const + { + DeclRef result; + result.decl = Slang::as(decl); + result.substitutions = substitutions; + return result; + } + template inline DeclRef makeDeclRef(T* decl) { @@ -723,12 +732,12 @@ namespace Slang RefPtr* Adjust(RefPtr* ptr, RefPtr* end) const { - while (ptr != end) + for (; ptr != end; ptr++) { - DeclRef declRef(ptr->Ptr(), substitutions); - if (declRef.as()) + if (as(*ptr)) + { return ptr; - ptr++; + } } return end; } @@ -1062,7 +1071,7 @@ namespace Slang RefPtr getVal() { SLANG_ASSERT(getFlavor() == Flavor::val); - return m_obj.dynamicCast(); + return m_obj.as(); } RefPtr getWitnessTable(); @@ -1165,7 +1174,7 @@ namespace Slang inline int GetVectorSize(VectorExpressionType* vecType) { - auto constantVal = vecType->elementCount.dynamicCast(); + auto constantVal = as(vecType->elementCount); if (constantVal) return (int) constantVal->value; // TODO: what to do in this case? -- cgit v1.2.3