From e5cc4660c634a0dd35a9813e03192d380f253332 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 29 Nov 2018 07:48:38 -0800 Subject: Fix uses of dynamic_cast on types in reflection API (#731) The `Type` infrastructure uses a class hierarchy, but blindly `dynamic_cast`ing to a desired case doesn't always give the expected result, because a `Type` could represent a `typedef` (a `NamedExpressionType`) that itself resolves to, e.g, a vector type (a `VectorExpressionType`). In that case a `dynamic_cast(someType)` would fail, even though the type logically represents a vector. The `Type::As()` method is designed to handle this case, by "looking through" simple `typedef`s to get at the real definition of a type. The fix in this case is to use `Type::As()` at various points in the reflection code (`reflection.cpp`) instead of `dynamic_cast`. This problem surfaced with a `StructuredBuffer` not reflecting correctly, because the element type (`float2`) is actually a `typedef` (for `vector`), so I've included a test case that stresses that case. Getting the right output in the test required tweaking the `slang-reflection-test` tool to produce additional output for resource types (currently narrowed down to only affect structured buffers to avoid large diffs in expected test outputs). --- source/slang/reflection.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'source/slang/reflection.cpp') diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp index 73b616377..44920fb9f 100644 --- a/source/slang/reflection.cpp +++ b/source/slang/reflection.cpp @@ -189,7 +189,7 @@ SLANG_API unsigned int spReflectionType_GetFieldCount(SlangReflectionType* inTyp // TODO: maybe filter based on kind - if(auto declRefType = dynamic_cast(type)) + if(auto declRefType = type->As()) { auto declRef = declRefType->declRef; if( auto structDeclRef = declRef.As()) @@ -208,7 +208,7 @@ SLANG_API SlangReflectionVariable* spReflectionType_GetFieldByIndex(SlangReflect // TODO: maybe filter based on kind - if(auto declRefType = dynamic_cast(type)) + if(auto declRefType = type->As()) { auto declRef = declRefType->declRef; if( auto structDeclRef = declRef.As()) @@ -226,11 +226,11 @@ SLANG_API size_t spReflectionType_GetElementCount(SlangReflectionType* inType) auto type = convert(inType); if(!type) return 0; - if(auto arrayType = dynamic_cast(type)) + if(auto arrayType = type->As()) { return arrayType->ArrayLength ? (size_t) GetIntVal(arrayType->ArrayLength) : 0; } - else if( auto vectorType = dynamic_cast(type)) + else if( auto vectorType = type->As()) { return (size_t) GetIntVal(vectorType->elementCount); } @@ -243,19 +243,19 @@ SLANG_API SlangReflectionType* spReflectionType_GetElementType(SlangReflectionTy auto type = convert(inType); if(!type) return nullptr; - if(auto arrayType = dynamic_cast(type)) + if(auto arrayType = type->As()) { return (SlangReflectionType*) arrayType->baseType.Ptr(); } - else if( auto constantBufferType = dynamic_cast(type)) + else if( auto constantBufferType = type->As()) { return convert(constantBufferType->elementType.Ptr()); } - else if( auto vectorType = dynamic_cast(type)) + else if( auto vectorType = type->As()) { return convert(vectorType->elementType.Ptr()); } - else if( auto matrixType = dynamic_cast(type)) + else if( auto matrixType = type->As()) { return convert(matrixType->getElementType()); } @@ -268,15 +268,15 @@ SLANG_API unsigned int spReflectionType_GetRowCount(SlangReflectionType* inType) auto type = convert(inType); if(!type) return 0; - if(auto matrixType = dynamic_cast(type)) + if(auto matrixType = type->As()) { return (unsigned int) GetIntVal(matrixType->getRowCount()); } - else if(auto vectorType = dynamic_cast(type)) + else if(auto vectorType = type->As()) { return 1; } - else if( auto basicType = dynamic_cast(type) ) + else if( auto basicType = type->As() ) { return 1; } @@ -289,15 +289,15 @@ SLANG_API unsigned int spReflectionType_GetColumnCount(SlangReflectionType* inTy auto type = convert(inType); if(!type) return 0; - if(auto matrixType = dynamic_cast(type)) + if(auto matrixType = type->As()) { return (unsigned int) GetIntVal(matrixType->getColumnCount()); } - else if(auto vectorType = dynamic_cast(type)) + else if(auto vectorType = type->As()) { return (unsigned int) GetIntVal(vectorType->elementCount); } - else if( auto basicType = dynamic_cast(type) ) + else if( auto basicType = type->As() ) { return 1; } @@ -310,16 +310,16 @@ SLANG_API SlangScalarType spReflectionType_GetScalarType(SlangReflectionType* in auto type = convert(inType); if(!type) return 0; - if(auto matrixType = dynamic_cast(type)) + if(auto matrixType = type->As()) { type = matrixType->getElementType(); } - else if(auto vectorType = dynamic_cast(type)) + else if(auto vectorType = type->As()) { type = vectorType->elementType.Ptr(); } - if(auto basicType = dynamic_cast(type)) + if(auto basicType = type->As()) { switch (basicType->baseType) { -- cgit v1.2.3