From 3a5214b65b2a5efdbcf9bf6fb4d7603e9ee63234 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 18 Oct 2018 09:30:38 -0700 Subject: Add support for static methods in interfaces (#680) This change allows an interface to include `static` methods as requirements, so that types that conform to the interface will need to satisfy the requirement with a `static` method. The essence of the check is simple: when checking that a method satisfies a requirement, we enforce that both are `static` or both are non-`static`. Making that simple change and adding a test change broke a few other places in the compiler that this change tries to fix. The main fix is to handle cases where we might look up an "effectively static" member of a type through an instance, and to make sure that we replace the instance-based lookup with type-based lookup. There was already logic along these lines in `lower-to-ir.cpp`, so this change centralizes it in `check.cpp` where it seems to logically belong. --- tests/compute/interface-static-method.slang | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/compute/interface-static-method.slang (limited to 'tests/compute/interface-static-method.slang') diff --git a/tests/compute/interface-static-method.slang b/tests/compute/interface-static-method.slang new file mode 100644 index 000000000..4747c8a8a --- /dev/null +++ b/tests/compute/interface-static-method.slang @@ -0,0 +1,56 @@ +// interface-static-method.slang + +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute + +interface IHideout +{ + int getAnimalCount(); +} + +interface ISuperhero +{ + associatedtype Hideout : IHideout; + + static Hideout getHideout(); +} + +struct Batcave : IHideout +{ + int batCount; + + int getAnimalCount() { return batCount; } +} + +struct Batman : ISuperhero +{ + typedef Batcave Hideout; + + static Batcave getHideout() + { + Batcave batcave = { 100 }; + return batcave; + } +} + +int doIt() +{ + return T.getHideout().getAnimalCount(); +} + +int test(int val) +{ + return doIt(); +} + + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out +RWStructuredBuffer outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int tid = dispatchThreadID.x; + outputBuffer[tid] = test(tid); +} \ No newline at end of file -- cgit v1.2.3