summaryrefslogtreecommitdiff
path: root/source/slang/syntax.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-02-02 11:58:54 -0500
committerTim Foley <tfoleyNV@users.noreply.github.com>2019-02-02 08:58:54 -0800
commit3726194fbe3da234eb30b6371e5b4ab1ea388f93 (patch)
tree815bb6162f76aeb2bd517126b802cc511cd0322a /source/slang/syntax.h
parent6f2c03430afdf963eed53c08d0b63342af722868 (diff)
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.
Diffstat (limited to 'source/slang/syntax.h')
-rw-r--r--source/slang/syntax.h45
1 files changed, 27 insertions, 18 deletions
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<typename T>
+ 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 <typename T>
+ bool is() const { return Slang::as<T>(decl) != nullptr; }
+
+ // "dynamic cast" to a more specific declaration reference type
+ template<typename T>
+ DeclRef<T> 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<typename U>
- DeclRef<U> as() const
- {
- DeclRef<U> result;
- result.decl = Slang::as<U>(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<typename U>
DeclRef<U> Substitute(DeclRef<U> declRef) const
{
@@ -556,7 +557,15 @@ namespace Slang
}
};
-
+ template<typename T>
+ DeclRef<T> DeclRefBase::as() const
+ {
+ DeclRef<T> result;
+ result.decl = Slang::as<T>(decl);
+ result.substitutions = substitutions;
+ return result;
+ }
+
template<typename T>
inline DeclRef<T> makeDeclRef(T* decl)
{
@@ -723,12 +732,12 @@ namespace Slang
RefPtr<Decl>* Adjust(RefPtr<Decl>* ptr, RefPtr<Decl>* end) const
{
- while (ptr != end)
+ for (; ptr != end; ptr++)
{
- DeclRef<Decl> declRef(ptr->Ptr(), substitutions);
- if (declRef.as<T>())
+ if (as<T>(*ptr))
+ {
return ptr;
- ptr++;
+ }
}
return end;
}
@@ -1062,7 +1071,7 @@ namespace Slang
RefPtr<Val> getVal()
{
SLANG_ASSERT(getFlavor() == Flavor::val);
- return m_obj.dynamicCast<Val>();
+ return m_obj.as<Val>();
}
RefPtr<WitnessTable> getWitnessTable();
@@ -1165,7 +1174,7 @@ namespace Slang
inline int GetVectorSize(VectorExpressionType* vecType)
{
- auto constantVal = vecType->elementCount.dynamicCast<ConstantIntVal>();
+ auto constantVal = as<ConstantIntVal>(vecType->elementCount);
if (constantVal)
return (int) constantVal->value;
// TODO: what to do in this case?