Skip to content

Commit ccc75cd

Browse files
Reflection Fixes. (#6346)
* Fix 6317. * Fixes #6316. * Fix cmake preset. --------- Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 8406b56 commit ccc75cd

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

CMakePresets.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"generator": "Ninja Multi-Config",
1313
"binaryDir": "${sourceDir}/build",
1414
"cacheVariables": {
15-
"CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreaded$<$<CONFIG:Debug>:Debug>"
15+
"CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreaded$<$<CONFIG:Debug>:Debug>",
16+
"SLANG_ENABLE_IR_BREAK_ALLOC": "$<$<CONFIG:Debug>:TRUE>$<$<NOT:$<CONFIG:Debug>>:FALSE>"
1617
}
1718
},
1819
{

source/slang/slang-reflection-api.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1016,9 +1016,10 @@ SLANG_API SlangReflectionType* spReflection_FindTypeByName(
10161016
SubstitutionSet(genericDeclRef),
10171017
astBuilder,
10181018
genericDeclRef.getDecl()->inner);
1019-
return convert(DeclRefType::create(
1020-
astBuilder,
1021-
createDefaultSubstitutionsIfNeeded(astBuilder, nullptr, innerDeclRef)));
1019+
if (as<AggTypeDecl>(innerDeclRef.getDecl()) ||
1020+
as<SimpleTypeDecl>(innerDeclRef.getDecl()))
1021+
return convert(DeclRefType::create(astBuilder, innerDeclRef));
1022+
return nullptr;
10221023
}
10231024

10241025
if (as<ErrorType>(result))

source/slang/slang.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,16 @@ Expr* ComponentType::findDeclFromString(String const& name, DiagnosticSink* sink
27142714
return result;
27152715
}
27162716

2717+
bool isSimpleName(String const& name)
2718+
{
2719+
for (char c : name)
2720+
{
2721+
if (!CharUtil::isAlphaOrDigit(c) && c != '_' && c != '$')
2722+
return false;
2723+
}
2724+
return true;
2725+
}
2726+
27172727
Expr* ComponentType::findDeclFromStringInType(
27182728
Type* type,
27192729
String const& name,
@@ -2743,8 +2753,19 @@ Expr* ComponentType::findDeclFromStringInType(
27432753

27442754
SLANG_AST_BUILDER_RAII(linkage->getASTBuilder());
27452755

2746-
Expr* expr = linkage->parseTermString(name, scope);
2756+
Expr* expr = nullptr;
27472757

2758+
if (isSimpleName(name))
2759+
{
2760+
auto varExpr = astBuilder->create<VarExpr>();
2761+
varExpr->scope = scope;
2762+
varExpr->name = getLinkage()->getNamePool()->getName(name);
2763+
expr = varExpr;
2764+
}
2765+
else
2766+
{
2767+
expr = linkage->parseTermString(name, scope);
2768+
}
27482769
SemanticsContext context(linkage->getSemanticsForReflection());
27492770
context = context.allowStaticReferenceToNonStaticMember().withSink(sink);
27502771

tools/slang-unit-test/unit-test-function-reflection.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ SLANG_UNIT_TEST(functionReflection)
4242
int bar1(IFloat a, IFloat b) { return 0; }
4343
int bar2<T>(T a, float3 b) { return 0; }
4444
int bar3(float3 b) { return 0; }
45+
int bar4<T:IFloat>(T a){return 0;}
46+
47+
struct Foo { __init() {} }
4548
)";
4649

4750
auto moduleName = "moduleG" + String(Process::getId());
@@ -205,4 +208,12 @@ SLANG_UNIT_TEST(functionReflection)
205208
resolvedFunctionReflection = bar3Reflection->specializeWithArgTypes(1, argTypes);
206209
SLANG_CHECK(resolvedFunctionReflection != nullptr);
207210
SLANG_CHECK(resolvedFunctionReflection == bar3Reflection);
211+
212+
// GitHub issue #6317: bar2 is a function, not a type, so it should not be found.
213+
SLANG_CHECK(module->getLayout()->findTypeByName("bar4") == nullptr);
214+
215+
auto fooType = module->getLayout()->findTypeByName("Foo");
216+
SLANG_CHECK_ABORT(fooType != nullptr);
217+
auto ctor = module->getLayout()->findFunctionByNameInType(fooType, "$init");
218+
SLANG_CHECK(ctor != nullptr);
208219
}

0 commit comments

Comments
 (0)