From 11748a75e66c2bd3fa7ef7635fd35363465f599c Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 20 Aug 2020 08:23:51 -0700 Subject: Initial support for a using construct (#1506) The basic idea is that if you have a namespace: namespace MyCoolNamespace { void f() { ... } ... } then you can bring the declarations from that namespace into scope with: using MyCoolNamespace; f(); The `using` construct is allowed in any scope where declarations are allowed. As an additional feature, the construct allows and then ignores the keyword `namespace` if it occurs right after `using`: using namespace MyCoolNamespace; Note that unlike in C++, `using` a namespace inside another namespace doesn't implicitly make the symbols available to clients of that namespace: namespace hidden { void secret() {...} ... } namespace api { using hidden; ... } api.secret(); // ERROR: `secret()` isn't a member of `api` The implementation of this feature was relatively simple, although it does leave out more advanced features that might be desirable in the future: * No support for `using MCN = MyCoolNamespace` sorts of tricks to define a short name * No support for `using` anything that isn't a namespace (e.g., to make the members of a type available without qualification) * No support for cases where multiple visible modules have a namespace of the same name (or dealing with overloaded namespaces in general) --- .../namespaces/using-namespace.slang | 47 ++++++++++++++++++++++ .../namespaces/using-namespace.slang.expected.txt | 4 ++ 2 files changed, 51 insertions(+) create mode 100644 tests/language-feature/namespaces/using-namespace.slang create mode 100644 tests/language-feature/namespaces/using-namespace.slang.expected.txt (limited to 'tests') diff --git a/tests/language-feature/namespaces/using-namespace.slang b/tests/language-feature/namespaces/using-namespace.slang new file mode 100644 index 000000000..b0b301929 --- /dev/null +++ b/tests/language-feature/namespaces/using-namespace.slang @@ -0,0 +1,47 @@ +// using-namespace.slang + +// Test that `using` can bring declarations from a namespace into scope + +//TEST(compute):COMPARE_COMPUTE: + +namespace X +{ + struct A { int a; } +} + +namespace Y +{ + // A `using` that brings one namespace into scope of declarations in another + // + using X; + + int b(A a) { return a.a; } +} + +// A `using` that brings declarations in a namespace into the global scope +// +using Y; + +int test(int value) +{ + // A `using` that brings declarations into a local scope + // (and also uses the optional `namespace` placeholder keyword) + // + using namespace X; + + A a = { value }; + return b(a); +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int inVal = tid; + int outVal = test(inVal); + outputBuffer[tid] = outVal; +} + diff --git a/tests/language-feature/namespaces/using-namespace.slang.expected.txt b/tests/language-feature/namespaces/using-namespace.slang.expected.txt new file mode 100644 index 000000000..bc856dafa --- /dev/null +++ b/tests/language-feature/namespaces/using-namespace.slang.expected.txt @@ -0,0 +1,4 @@ +0 +1 +2 +3 -- cgit v1.2.3