yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

CopilotFix FunctionReflection getName() returning null for overloaded functions (#8113)c7c481614

master
3.0 KiB94 linesraw
1// unit-test-function-lookup-resolution.cpp
2
3#include "../../source/core/slang-io.h"
4#include "../../source/core/slang-process.h"
5#include "slang-com-ptr.h"
6#include "slang.h"
7#include "unit-test/slang-unit-test.h"
8
9#include <stdio.h>
10#include <stdlib.h>
11
12using namespace Slang;
13
14static String getTypeFullName(slang::TypeReflection* type)
15{
16    ComPtr<ISlangBlob> blob;
17    type->getFullName(blob.writeRef());
18    return String((const char*)blob->getBufferPointer());
19}
20
21// Test that the reflection API provides correctly resolved lookup results.
22
23SLANG_UNIT_TEST(functionLookupResolution)
24{
25    // Source for a module that contains an undecorated entrypoint.
26    const char* userSourceBody = R"(
27        public interface IBase
28        {
29            public void step(inout float f);
30            public void method(int x) {}
31        }
32
33        public struct Impl : IBase
34        {
35            public void step(inout float f)
36            {
37                f += 1.0f;
38            }
39            public override void method(int x) {}
40        }
41        public extension<T : IBase> T {
42            public void method(int x) {}
43        }
44        )";
45
46    auto moduleName = "moduleG" + String(Process::getId());
47    String userSource = "import " + moduleName + ";\n" + userSourceBody;
48    ComPtr<slang::IGlobalSession> globalSession;
49    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
50    slang::TargetDesc targetDesc = {};
51    targetDesc.format = SLANG_HLSL;
52    targetDesc.profile = globalSession->findProfile("sm_5_0");
53    slang::SessionDesc sessionDesc = {};
54    sessionDesc.targetCount = 1;
55    sessionDesc.targets = &targetDesc;
56    ComPtr<slang::ISession> session;
57    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
58
59    ComPtr<slang::IBlob> diagnosticBlob;
60    auto module = session->loadModuleFromSourceString(
61        "m",
62        "m.slang",
63        userSourceBody,
64        diagnosticBlob.writeRef());
65    SLANG_CHECK(module != nullptr);
66
67    auto layout = module->getLayout();
68    auto type = layout->findTypeByName("Impl");
69    SLANG_CHECK_ABORT(type != nullptr);
70
71    auto func = layout->findFunctionByNameInType(type, "step");
72    SLANG_CHECK_ABORT(func && !func->isOverloaded());
73
74
75    auto func1 = layout->findFunctionByNameInType(type, "method");
76    SLANG_CHECK_ABORT(func1->isOverloaded());
77    SLANG_CHECK(func1->getOverloadCount() == 3);
78    // Test that overloaded function containers return the correct name
79    SLANG_CHECK(func1->getName() != nullptr);
80    SLANG_CHECK(String(func1->getName()) == "method");
81    if (func1->isOverloaded())
82    {
83        List<slang::FunctionReflection*> candidates;
84        for (uint32_t i = 0; i < func1->getOverloadCount(); i++)
85        {
86            candidates.add(func1->getOverload(i));
87        }
88        func1 = layout->tryResolveOverloadedFunction(
89            (uint32_t)candidates.getCount(),
90            candidates.getBuffer());
91    }
92    SLANG_CHECK(!func1->isOverloaded());
93    SLANG_CHECK(String(func1->getName()) == "method");
94}