From 68b0125226464cb3c9e9b7f50bfb53cda97723b4 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 6 Aug 2025 01:07:41 -0700 Subject: Add reflection api for overload candidate filtering. (#8066) * Add reflection api for overload candidate filtering. * Fix API. * Fix. * Update build. * Update test. * Update formatting. --- tools/CMakeLists.txt | 4 +- .../unit-test-function-lookup-resolution.cpp | 91 ++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tools/slang-unit-test/unit-test-function-lookup-resolution.cpp (limited to 'tools') diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 00bd93c49..7b28e960f 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -377,7 +377,9 @@ if(SLANG_ENABLE_TESTS) slang-unit-test MODULE EXCLUDE_FROM_ALL - EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL + EXTRA_COMPILE_DEFINITIONS_PRIVATE + SLANG_SHARED_LIBRARY_TOOL + SLANG_NO_DEPRECATION USE_FEWER_WARNINGS LINK_WITH_PRIVATE core compiler-core unit-test slang Threads::Threads OUTPUT_NAME slang-unit-test-tool diff --git a/tools/slang-unit-test/unit-test-function-lookup-resolution.cpp b/tools/slang-unit-test/unit-test-function-lookup-resolution.cpp new file mode 100644 index 000000000..539c9ac48 --- /dev/null +++ b/tools/slang-unit-test/unit-test-function-lookup-resolution.cpp @@ -0,0 +1,91 @@ +// unit-test-function-lookup-resolution.cpp + +#include "../../source/core/slang-io.h" +#include "../../source/core/slang-process.h" +#include "slang-com-ptr.h" +#include "slang.h" +#include "unit-test/slang-unit-test.h" + +#include +#include + +using namespace Slang; + +static String getTypeFullName(slang::TypeReflection* type) +{ + ComPtr blob; + type->getFullName(blob.writeRef()); + return String((const char*)blob->getBufferPointer()); +} + +// Test that the reflection API provides correctly resolved lookup results. + +SLANG_UNIT_TEST(functionLookupResolution) +{ + // Source for a module that contains an undecorated entrypoint. + const char* userSourceBody = R"( + public interface IBase + { + public void step(inout float f); + public void method(int x) {} + } + + public struct Impl : IBase + { + public void step(inout float f) + { + f += 1.0f; + } + public override void method(int x) {} + } + public extension T { + public void method(int x) {} + } + )"; + + auto moduleName = "moduleG" + String(Process::getId()); + String userSource = "import " + moduleName + ";\n" + userSourceBody; + ComPtr globalSession; + SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); + slang::TargetDesc targetDesc = {}; + targetDesc.format = SLANG_HLSL; + targetDesc.profile = globalSession->findProfile("sm_5_0"); + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + sessionDesc.targets = &targetDesc; + ComPtr session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr diagnosticBlob; + auto module = session->loadModuleFromSourceString( + "m", + "m.slang", + userSourceBody, + diagnosticBlob.writeRef()); + SLANG_CHECK(module != nullptr); + + auto layout = module->getLayout(); + auto type = layout->findTypeByName("Impl"); + SLANG_CHECK_ABORT(type != nullptr); + + auto func = layout->findFunctionByNameInType(type, "step"); + SLANG_CHECK_ABORT(func && !func->isOverloaded()); + + + auto func1 = layout->findFunctionByNameInType(type, "method"); + SLANG_CHECK_ABORT(func1->isOverloaded()); + SLANG_CHECK(func1->getOverloadCount() == 3); + if (func1->isOverloaded()) + { + List candidates; + for (uint32_t i = 0; i < func1->getOverloadCount(); i++) + { + candidates.add(func1->getOverload(i)); + } + func1 = layout->tryResolveOverloadedFunction( + (uint32_t)candidates.getCount(), + candidates.getBuffer()); + } + SLANG_CHECK(!func1->isOverloaded()); + SLANG_CHECK(String(func1->getName()) == "method"); +} -- cgit v1.2.3