From ccc75cdd9508a4e19efa22e7c911cc2013f514fa Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 13 Feb 2025 03:29:36 -0800 Subject: Reflection Fixes. (#6346) * Fix 6317. * Fixes #6316. * Fix cmake preset. --------- Co-authored-by: Ellie Hermaszewska --- CMakePresets.json | 3 ++- source/slang/slang-reflection-api.cpp | 7 ++++--- source/slang/slang.cpp | 23 +++++++++++++++++++++- .../unit-test-function-reflection.cpp | 11 +++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index b0fa9b8bf..86ba402b2 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -12,7 +12,8 @@ "generator": "Ninja Multi-Config", "binaryDir": "${sourceDir}/build", "cacheVariables": { - "CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreaded$<$:Debug>" + "CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreaded$<$:Debug>", + "SLANG_ENABLE_IR_BREAK_ALLOC": "$<$:TRUE>$<$>:FALSE>" } }, { diff --git a/source/slang/slang-reflection-api.cpp b/source/slang/slang-reflection-api.cpp index d1adfedc0..52047a751 100644 --- a/source/slang/slang-reflection-api.cpp +++ b/source/slang/slang-reflection-api.cpp @@ -1016,9 +1016,10 @@ SLANG_API SlangReflectionType* spReflection_FindTypeByName( SubstitutionSet(genericDeclRef), astBuilder, genericDeclRef.getDecl()->inner); - return convert(DeclRefType::create( - astBuilder, - createDefaultSubstitutionsIfNeeded(astBuilder, nullptr, innerDeclRef))); + if (as(innerDeclRef.getDecl()) || + as(innerDeclRef.getDecl())) + return convert(DeclRefType::create(astBuilder, innerDeclRef)); + return nullptr; } if (as(result)) diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index ee0aa4c14..c316974f1 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -2714,6 +2714,16 @@ Expr* ComponentType::findDeclFromString(String const& name, DiagnosticSink* sink return result; } +bool isSimpleName(String const& name) +{ + for (char c : name) + { + if (!CharUtil::isAlphaOrDigit(c) && c != '_' && c != '$') + return false; + } + return true; +} + Expr* ComponentType::findDeclFromStringInType( Type* type, String const& name, @@ -2743,8 +2753,19 @@ Expr* ComponentType::findDeclFromStringInType( SLANG_AST_BUILDER_RAII(linkage->getASTBuilder()); - Expr* expr = linkage->parseTermString(name, scope); + Expr* expr = nullptr; + if (isSimpleName(name)) + { + auto varExpr = astBuilder->create(); + varExpr->scope = scope; + varExpr->name = getLinkage()->getNamePool()->getName(name); + expr = varExpr; + } + else + { + expr = linkage->parseTermString(name, scope); + } SemanticsContext context(linkage->getSemanticsForReflection()); context = context.allowStaticReferenceToNonStaticMember().withSink(sink); diff --git a/tools/slang-unit-test/unit-test-function-reflection.cpp b/tools/slang-unit-test/unit-test-function-reflection.cpp index 3ce6ab7a5..2f57d4151 100644 --- a/tools/slang-unit-test/unit-test-function-reflection.cpp +++ b/tools/slang-unit-test/unit-test-function-reflection.cpp @@ -42,6 +42,9 @@ SLANG_UNIT_TEST(functionReflection) int bar1(IFloat a, IFloat b) { return 0; } int bar2(T a, float3 b) { return 0; } int bar3(float3 b) { return 0; } + int bar4(T a){return 0;} + + struct Foo { __init() {} } )"; auto moduleName = "moduleG" + String(Process::getId()); @@ -205,4 +208,12 @@ SLANG_UNIT_TEST(functionReflection) resolvedFunctionReflection = bar3Reflection->specializeWithArgTypes(1, argTypes); SLANG_CHECK(resolvedFunctionReflection != nullptr); SLANG_CHECK(resolvedFunctionReflection == bar3Reflection); + + // GitHub issue #6317: bar2 is a function, not a type, so it should not be found. + SLANG_CHECK(module->getLayout()->findTypeByName("bar4") == nullptr); + + auto fooType = module->getLayout()->findTypeByName("Foo"); + SLANG_CHECK_ABORT(fooType != nullptr); + auto ctor = module->getLayout()->findFunctionByNameInType(fooType, "$init"); + SLANG_CHECK(ctor != nullptr); } -- cgit v1.2.3