diff options
| author | Yong He <yonghe@outlook.com> | 2025-08-06 01:07:41 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-06 08:07:41 +0000 |
| commit | 68b0125226464cb3c9e9b7f50bfb53cda97723b4 (patch) | |
| tree | 5f0833c6d9aa759b2769f7f6ac9b3ca6ed9a10f0 /tools | |
| parent | 83675103a1a4fefde11b314aed26f4d37860efe7 (diff) | |
Add reflection api for overload candidate filtering. (#8066)
* Add reflection api for overload candidate filtering.
* Fix API.
* Fix.
* Update build.
* Update test.
* Update formatting.
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | tools/slang-unit-test/unit-test-function-lookup-resolution.cpp | 91 |
2 files changed, 94 insertions, 1 deletions
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 <stdio.h> +#include <stdlib.h> + +using namespace Slang; + +static String getTypeFullName(slang::TypeReflection* type) +{ + ComPtr<ISlangBlob> 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 : IBase> T { + public void method(int x) {} + } + )"; + + auto moduleName = "moduleG" + String(Process::getId()); + String userSource = "import " + moduleName + ";\n" + userSourceBody; + ComPtr<slang::IGlobalSession> 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<slang::ISession> session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr<slang::IBlob> 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<slang::FunctionReflection*> 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"); +} |
