From 11c547d1e94fa620f527c3590174e6e25ab21883 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 31 Jan 2019 10:14:26 -0500 Subject: Feature/as refactor (#817) * Made dynamicCast a free function. * Replace As with as or dynamicCast depending on if it is a type. * Fix problem with using non smart pointer cast. * Removed legacy asXXXX methods. * Remove As from Type. * Removed As from Qual type -> made coercable into Type*, such that can just use free 'as'. * Remove left over QualType::As() impl. * Remove As from SyntaxNodeBase. * Made as for instructions implemented by dynamicCast. * Replace As on DeclRef. Use the global as<> to do the cast. * Add const safe versions of dynamicCast and as for IRInst --- source/slang/syntax-base-defs.h | 56 +++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) (limited to 'source/slang/syntax-base-defs.h') diff --git a/source/slang/syntax-base-defs.h b/source/slang/syntax-base-defs.h index 81f03d43f..7f49bb607 100644 --- a/source/slang/syntax-base-defs.h +++ b/source/slang/syntax-base-defs.h @@ -16,17 +16,6 @@ END_SYNTAX_CLASS() ABSTRACT_SYNTAX_CLASS(SyntaxNodeBase, NodeBase) // The primary source location associated with this AST node FIELD(SourceLoc, loc) - - RAW( - // Allow dynamic casting with a convenient syntax - template - T* As() - { - SLANG_ASSERT(this); - return dynamic_cast(this); - } - ) - END_SYNTAX_CLASS() // Base class for compile-time values (most often a type). @@ -60,6 +49,15 @@ ABSTRACT_SYNTAX_CLASS(Val, NodeBase) ) END_SYNTAX_CLASS() +RAW( + class Type; + + template + SLANG_FORCE_INLINE T* as(Type* obj); + template + SLANG_FORCE_INLINE const T* as(const Type* obj); + ) + // A type, representing a classifier for some term in the AST. // // Types can include "sugar" in that they may refer to a @@ -68,7 +66,7 @@ END_SYNTAX_CLASS() // // In order to operation on types, though, we often want // to look past any sugar, and operate on an underlying -// "canonical" type. The reprsentation caches a pointer to +// "canonical" type. The representation caches a pointer to // a canonical type on every type, so we can easily // operate on the raw representation when needed. ABSTRACT_SYNTAX_CLASS(Type, Val) @@ -85,31 +83,12 @@ public: bool Equals(Type * type); bool Equals(RefPtr type); - bool IsVectorType() { return As() != nullptr; } - bool IsArray() { return As() != nullptr; } - - template - T* As() - { - return dynamic_cast(GetCanonicalType()); - } - - // Convenience/legacy wrappers for `As<>` - ArithmeticExpressionType * AsArithmeticType() { return As(); } - BasicExpressionType * AsBasicType() { return As(); } - VectorExpressionType * AsVectorType() { return As(); } - MatrixExpressionType * AsMatrixType() { return As(); } - ArrayExpressionType * AsArrayType() { return As(); } - - DeclRefType* AsDeclRefType() { return As(); } - - NamedExpressionType* AsNamedType(); - bool IsTextureOrSampler(); - bool IsTexture() { return As() != nullptr; } - bool IsSampler() { return As() != nullptr; } + bool IsTexture() { return as(this) != nullptr; } + bool IsSampler() { return as(this) != nullptr; } bool IsStruct(); - bool IsClass(); + //bool IsClass(); + Type* GetCanonicalType(); virtual RefPtr SubstituteImpl(SubstitutionSet subst, int* ioDiff) override; @@ -125,7 +104,12 @@ protected: Session* session = nullptr; ) END_SYNTAX_CLASS() - +RAW( + template + SLANG_FORCE_INLINE T* as(Type* obj) { return obj ? dynamicCast(obj->GetCanonicalType()) : nullptr; } + template + SLANG_FORCE_INLINE const T* as(const Type* obj) { return obj ? dynamicCast(const_cast(obj)->GetCanonicalType()) : nullptr; } +) // A substitution represents a binding of certain // type-level variables to concrete argument values -- cgit v1.2.3