Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attribute reflection. #5823

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions include/slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ public: \
typedef struct SlangReflectionVariableLayout SlangReflectionVariableLayout;
typedef struct SlangReflectionTypeParameter SlangReflectionTypeParameter;
typedef struct SlangReflectionUserAttribute SlangReflectionUserAttribute;
typedef SlangReflectionUserAttribute SlangReflectionAttribute;
typedef struct SlangReflectionFunction SlangReflectionFunction;
typedef struct SlangReflectionGeneric SlangReflectionGeneric;

Expand Down Expand Up @@ -2140,46 +2141,48 @@ union GenericArgReflection
bool boolVal;
};

struct UserAttribute
struct Attribute
{
char const* getName()
{
return spReflectionUserAttribute_GetName((SlangReflectionUserAttribute*)this);
return spReflectionUserAttribute_GetName((SlangReflectionAttribute*)this);
}
uint32_t getArgumentCount()
{
return (uint32_t)spReflectionUserAttribute_GetArgumentCount(
(SlangReflectionUserAttribute*)this);
(SlangReflectionAttribute*)this);
}
TypeReflection* getArgumentType(uint32_t index)
{
return (TypeReflection*)spReflectionUserAttribute_GetArgumentType(
(SlangReflectionUserAttribute*)this,
(SlangReflectionAttribute*)this,
index);
}
SlangResult getArgumentValueInt(uint32_t index, int* value)
{
return spReflectionUserAttribute_GetArgumentValueInt(
(SlangReflectionUserAttribute*)this,
(SlangReflectionAttribute*)this,
index,
value);
}
SlangResult getArgumentValueFloat(uint32_t index, float* value)
{
return spReflectionUserAttribute_GetArgumentValueFloat(
(SlangReflectionUserAttribute*)this,
(SlangReflectionAttribute*)this,
index,
value);
}
const char* getArgumentValueString(uint32_t index, size_t* outSize)
{
return spReflectionUserAttribute_GetArgumentValueString(
(SlangReflectionUserAttribute*)this,
(SlangReflectionAttribute*)this,
index,
outSize);
}
};

typedef Attribute UserAttribute;

struct TypeReflection
{
enum class Kind
Expand Down Expand Up @@ -2320,13 +2323,15 @@ struct TypeReflection
return (UserAttribute*)spReflectionType_GetUserAttribute((SlangReflectionType*)this, index);
}

UserAttribute* findUserAttributeByName(char const* name)
UserAttribute* findAttributeByName(char const* name)
{
return (UserAttribute*)spReflectionType_FindUserAttributeByName(
(SlangReflectionType*)this,
name);
}

UserAttribute* findUserAttributeByName(char const* name) { return findAttributeByName(name); }

TypeReflection* applySpecializations(GenericReflection* generic)
{
return (TypeReflection*)spReflectionType_applySpecializations(
Expand Down Expand Up @@ -2777,21 +2782,26 @@ struct VariableReflection
return spReflectionVariable_GetUserAttributeCount((SlangReflectionVariable*)this);
}

UserAttribute* getUserAttributeByIndex(unsigned int index)
Attribute* getUserAttributeByIndex(unsigned int index)
{
return (UserAttribute*)spReflectionVariable_GetUserAttribute(
(SlangReflectionVariable*)this,
index);
}

UserAttribute* findUserAttributeByName(SlangSession* globalSession, char const* name)
Attribute* findAttributeByName(SlangSession* globalSession, char const* name)
{
return (UserAttribute*)spReflectionVariable_FindUserAttributeByName(
(SlangReflectionVariable*)this,
globalSession,
name);
}

Attribute* findUserAttributeByName(SlangSession* globalSession, char const* name)
{
return findAttributeByName(globalSession, name);
}

bool hasDefaultValue()
{
return spReflectionVariable_HasDefaultValue((SlangReflectionVariable*)this);
Expand Down Expand Up @@ -2908,20 +2918,22 @@ struct FunctionReflection
{
return spReflectionFunction_GetUserAttributeCount((SlangReflectionFunction*)this);
}
UserAttribute* getUserAttributeByIndex(unsigned int index)
Attribute* getUserAttributeByIndex(unsigned int index)
{
return (UserAttribute*)spReflectionFunction_GetUserAttribute(
(SlangReflectionFunction*)this,
index);
return (
Attribute*)spReflectionFunction_GetUserAttribute((SlangReflectionFunction*)this, index);
}
UserAttribute* findUserAttributeByName(SlangSession* globalSession, char const* name)
Attribute* findAttributeByName(SlangSession* globalSession, char const* name)
{
return (UserAttribute*)spReflectionFunction_FindUserAttributeByName(
return (Attribute*)spReflectionFunction_FindUserAttributeByName(
(SlangReflectionFunction*)this,
globalSession,
name);
}

Attribute* findUserAttributeByName(SlangSession* globalSession, char const* name)
{
return findAttributeByName(globalSession, name);
}
Modifier* findModifier(Modifier::ID id)
{
return (Modifier*)spReflectionFunction_FindModifier(
Expand Down
15 changes: 6 additions & 9 deletions source/slang/slang-check-modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,18 +752,15 @@ Modifier* SemanticsVisitor::validateAttribute(
{
auto& arg = attr->args[paramIndex];
bool typeChecked = false;
if (auto basicType = as<BasicExpressionType>(paramDecl->getType()))
if (isValidCompileTimeConstantType(paramDecl->getType()))
{
if (basicType->getBaseType() == BaseType::Int)
if (auto cint = checkConstantIntVal(arg))
{
if (auto cint = checkConstantIntVal(arg))
{
for (Index ci = attr->intArgVals.getCount(); ci < paramIndex + 1; ci++)
attr->intArgVals.add(nullptr);
attr->intArgVals[(uint32_t)paramIndex] = cint;
}
typeChecked = true;
for (Index ci = attr->intArgVals.getCount(); ci < paramIndex + 1; ci++)
attr->intArgVals.add(nullptr);
attr->intArgVals[(uint32_t)paramIndex] = cint;
}
typeChecked = true;
}
if (!typeChecked)
{
Expand Down
10 changes: 6 additions & 4 deletions source/slang/slang-reflection-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ namespace Slang

// Conversion routines to help with strongly-typed reflection API

static inline UserDefinedAttribute* convert(SlangReflectionUserAttribute* attrib)
static inline Attribute* convert(SlangReflectionUserAttribute* attrib)
{
return (UserDefinedAttribute*)attrib;
return (Attribute*)attrib;
}
static inline SlangReflectionUserAttribute* convert(UserDefinedAttribute* attrib)
static inline SlangReflectionUserAttribute* convert(Attribute* attrib)
{
return (SlangReflectionUserAttribute*)attrib;
}
Expand Down Expand Up @@ -154,7 +154,9 @@ static SlangReflectionUserAttribute* findUserAttributeByName(
const char* name)
{
auto nameObj = session->tryGetNameObj(name);
for (auto x : decl->getModifiersOfType<UserDefinedAttribute>())
if (!nameObj)
return nullptr;
for (auto x : decl->getModifiersOfType<Attribute>())
{
if (x->keywordName == nameObj)
return (SlangReflectionUserAttribute*)(x);
Expand Down
2 changes: 1 addition & 1 deletion tools/gfx/d3d12/d3d12-shader-object-layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool ShaderObjectLayoutImpl::isBindingRangeRootParameter(
{
if (auto leafVariable = typeLayout->getBindingRangeLeafVariable(bindingRangeIndex))
{
if (leafVariable->findUserAttributeByName(globalSession, rootParameterAttributeName))
if (leafVariable->findAttributeByName(globalSession, rootParameterAttributeName))
{
isRootParameter = true;
}
Expand Down
79 changes: 79 additions & 0 deletions tools/slang-unit-test/unit-test-attribute-reflection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// unit-test-translation-unit-import.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;

// Test that the reflection API provides correct info about attributes.

SLANG_UNIT_TEST(attributeReflection)
{
const char* userSourceBody = R"(
public enum E
{
V0,
V1,
};

[__AttributeUsage(_AttributeTargets.Struct)]
public struct NormalTextureAttribute
{
public E Type;
};

[COM("042BE50B-CB01-4DBB-8367-3A9CDCBE2F49")]
interface IInterface { void f(); }

[NormalTexture(E.V1)]
struct TS {};
)";
String userSource = 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 reflection = module->getLayout();

auto interfaceType = reflection->findTypeByName("IInterface");
SLANG_CHECK(interfaceType != nullptr);

auto comAttribute = interfaceType->findAttributeByName("COM");
SLANG_CHECK(comAttribute != nullptr);

size_t size = 0;
auto guid = comAttribute->getArgumentValueString(0, &size);
UnownedStringSlice stringSlice = UnownedStringSlice(guid, size);
SLANG_CHECK(stringSlice == "\"042BE50B-CB01-4DBB-8367-3A9CDCBE2F49\"");

auto testType = reflection->findTypeByName("TS");
SLANG_CHECK(testType != nullptr);

auto normalTextureAttribute = testType->findAttributeByName("NormalTexture");
SLANG_CHECK(normalTextureAttribute != nullptr);

int value = 0;
normalTextureAttribute->getArgumentValueInt(0, &value);
SLANG_CHECK(value == 1);
}
2 changes: 1 addition & 1 deletion tools/slang-unit-test/unit-test-decl-tree-reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ SLANG_UNIT_TEST(declTreeReflection)
SLANG_CHECK(result == SLANG_OK);
SLANG_CHECK(val == 1024);
SLANG_CHECK(
funcReflection->findUserAttributeByName(globalSession.get(), "MyFuncProperty") ==
funcReflection->findAttributeByName(globalSession.get(), "MyFuncProperty") ==
userAttribute);
}

Expand Down
2 changes: 1 addition & 1 deletion tools/slang-unit-test/unit-test-function-reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SLANG_UNIT_TEST(functionReflection)
SLANG_CHECK(result == SLANG_OK);
SLANG_CHECK(val == 1024);
SLANG_CHECK(
funcReflection->findUserAttributeByName(globalSession.get(), "MyFuncProperty") ==
funcReflection->findAttributeByName(globalSession.get(), "MyFuncProperty") ==
userAttribute);

// Check overloaded method resolution
Expand Down
Loading