summaryrefslogtreecommitdiffstats
path: root/tests/language-feature/namespaces
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-04-02 08:52:42 -0700
committerGitHub <noreply@github.com>2020-04-02 08:52:42 -0700
commit487d4a4f406c9dd9803ecdca02467d09ee1ecf4a (patch)
treec1a64a26620de90636f16fa0fdb117f3a3c4f92e /tests/language-feature/namespaces
parent5e73e984022c9ec8e901ccffc94d3cd5f374642a (diff)
Add basic support for namespaces (#1304)
This change adds logic for parsing `namespace` declarations, referencing them, and looking up their members. * The parser changes are a bit subtle, because that is where we deal with the issue of "re-opening" a namespace. We kludge things a bit by re-using an existing `NamespaceDecl` in the same parent if one is available, and thereby ensure that all the members in the same namespace can see on another. * In order to allow namespaces to be referenced by name they need to have a type so that a `DeclRefExpr` to them can be formed. For this purpose we introduce `NamespaceType` which is the (singleton) type of a reference to a given namespace. * The new `NamespaceType` case is detected in the `MemberExpr` checking logic and routed to the same logic that `StaticMemberExpr` uses, and the static lookup logic was extended with support for looking up in a namespace (a thin wrapper around one of the existing worker routines in `slang-lookup.cpp`. * I made `NamespaceDecl` have a shared base class with `ModuleDecl` in the hopes that this would allow us to allow references to modules by name in the future. That hasn't been tested as part of this change. * I cleaned up a bunch of logic around `ModuleDecl` holding a `Scope` pointer that was being used for some of the more ad hoc lookup routines in the public API. Those have been switched over to something that is a bit more sensible given the language rules and that doesn't rely on keeping state sititng around on the `ModuleDecl`. * I added a test case to make sure the new funcitonality works, which includes re-opening a namespace, and it also tests both `.` and `::` operations for lookup in a namespace. * The main missing feature here is the ability to do something like C++ `using`. It would probably be cleanest if we used `import` for this, since we already have that syntax (and having both `import` and `using` seems like a recipe for confusion). Most of the infrastructure is present to support `import`ing one namespace into another (in a way that wouldn't automatically pollute the namespace for clients), but some careful thought needs to be put into how import of namespaces vs. modules should work.
Diffstat (limited to 'tests/language-feature/namespaces')
-rw-r--r--tests/language-feature/namespaces/simple-namespace.slang65
-rw-r--r--tests/language-feature/namespaces/simple-namespace.slang.expected.txt4
2 files changed, 69 insertions, 0 deletions
diff --git a/tests/language-feature/namespaces/simple-namespace.slang b/tests/language-feature/namespaces/simple-namespace.slang
new file mode 100644
index 000000000..01f40545a
--- /dev/null
+++ b/tests/language-feature/namespaces/simple-namespace.slang
@@ -0,0 +1,65 @@
+// simple-namespace.slang
+
+//TEST(compute):COMPARE_COMPUTE:
+
+// Test that simple `namespace` declarations work as expected
+
+namespace A
+{
+ struct X
+ {
+ int val;
+
+ int getVal()
+ {
+ return val;
+ }
+ }
+}
+
+namespace B
+{
+ struct X
+ {
+ int head;
+ int tail;
+
+ int getHead() { return head; }
+ int getTail() { return tail; }
+ }
+
+ X makeX(int h, int t)
+ {
+ X result = { h, t };
+ return result;
+ }
+}
+
+namespace A
+{
+ X makeX(int v)
+ {
+ X result = { v };
+ return result;
+ }
+}
+
+int test(int val)
+{
+ A.X a = A::makeX(val);
+ B::X b = B.makeX(val*16, val*256);
+
+ return a.getVal() + b.getHead() + b.getTail();
+}
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> 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/simple-namespace.slang.expected.txt b/tests/language-feature/namespaces/simple-namespace.slang.expected.txt
new file mode 100644
index 000000000..fb43a3716
--- /dev/null
+++ b/tests/language-feature/namespaces/simple-namespace.slang.expected.txt
@@ -0,0 +1,4 @@
+0
+111
+222
+333