From 956ede9be7d7b798ce719a7ac6a3be3f472f6db7 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 5 Jun 2020 19:38:43 -0700 Subject: Filter lookup results from interfaces in `visitMemberExpr`. Fixes #1377 --- source/slang/slang-check-expr.cpp | 30 +++++++++++- tests/compute/assoctype-lookup.slang | 58 +++++++++++++++++++++++ tests/compute/assoctype-lookup.slang.expected.txt | 4 ++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/compute/assoctype-lookup.slang create mode 100644 tests/compute/assoctype-lookup.slang.expected.txt diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 307ec6316..7adda8c77 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -1826,10 +1826,38 @@ namespace Slang // expr->baseExpression = maybeOpenExistential(expr->baseExpression); - // TODO: Handle the case of an overloaded base expression + // Handle the case of an overloaded base expression // here, in case we can use the name of the member to // disambiguate which of the candidates is meant, or if // we can return an overloaded result. + if (auto overloadedExpr = as(expr->baseExpression)) + { + if (overloadedExpr->base) + { + // If a member (dynamic or static) lookup result contains both the actual definition + // and the interface definition obtained from inheritance, we want to filter out + // the interface definitions. + LookupResult filteredLookupResult; + for (auto lookupResult : overloadedExpr->lookupResult2) + { + bool shouldRemove = false; + if (lookupResult.declRef.getParent().as()) + shouldRemove = true; + if (!shouldRemove) + { + filteredLookupResult.items.add(lookupResult); + } + } + if (filteredLookupResult.items.getCount() == 1) + filteredLookupResult.item = filteredLookupResult.items.getFirst(); + expr->baseExpression = createLookupResultExpr( + expr->name, + filteredLookupResult, + overloadedExpr->base, + overloadedExpr->loc); + } + // TODO: handle other cases of OverloadedExpr that need filtering. + } auto & baseType = expr->baseExpression->type; diff --git a/tests/compute/assoctype-lookup.slang b/tests/compute/assoctype-lookup.slang new file mode 100644 index 000000000..1dccc8e12 --- /dev/null +++ b/tests/compute/assoctype-lookup.slang @@ -0,0 +1,58 @@ +// assoctype-nested.slang + +// Confirm that an associated type can be correctly looked up. + +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -cpu +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute + +interface IBoneWeightSet +{ + associatedtype PackedType; +}; + +struct StandardBoneWeightSet : IBoneWeightSet +{ + struct PackedType + { + uint boneIds : BONEIDS; + uint boneWeights : BONEWEIGHTS; + }; + PackedType field; +}; + +interface IVertexFormat +{ + associatedtype BoneWeightSet : IBoneWeightSet; +}; + +struct VertexFormat : IVertexFormat +{ + typedef TBoneWeightSet BoneWeightSet; + TBoneWeightSet.PackedType weightSet; +}; + +struct OutputType +{ + VertexFormat.BoneWeightSet.PackedType field; +}; + +int test(int val) +{ + OutputType rs; + rs.field.boneIds = 256; + return rs.field.boneIds; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=gOutputBuffer +RWStructuredBuffer gOutputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int inputVal = tid; + int outputVal = test(inputVal); + gOutputBuffer[tid] = outputVal; +} \ No newline at end of file diff --git a/tests/compute/assoctype-lookup.slang.expected.txt b/tests/compute/assoctype-lookup.slang.expected.txt new file mode 100644 index 000000000..54248c25a --- /dev/null +++ b/tests/compute/assoctype-lookup.slang.expected.txt @@ -0,0 +1,4 @@ +100 +100 +100 +100 \ No newline at end of file -- cgit v1.2.3