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). --- tools/slang-reflection-test/main.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tools') diff --git a/tools/slang-reflection-test/main.cpp b/tools/slang-reflection-test/main.cpp index 7e5f268f3..872d2ff3a 100644 --- a/tools/slang-reflection-test/main.cpp +++ b/tools/slang-reflection-test/main.cpp @@ -372,6 +372,27 @@ static void emitReflectionTypeInfoJSON( } write(writer, "\""); } + + // TODO: We should really print the result type for all resource + // types, but current test output depends on the old behavior, so + // we only add result type output for structured buffers at first. + // + switch (shape & SLANG_RESOURCE_BASE_SHAPE_MASK) + { + default: + break; + + case SLANG_STRUCTURED_BUFFER: + if( auto resultType = type->getResourceResultType() ) + { + write(writer, ",\n"); + write(writer, "\"resultType\": "); + emitReflectionTypeJSON( + writer, + resultType); + } + break; + } } break; -- cgit v1.2.3