summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2022-04-05 17:53:41 -0400
committerGitHub <noreply@github.com>2022-04-05 17:53:41 -0400
commit464ecb6083f64f903df19b961e2af0075bdf3111 (patch)
tree6dbb83f694b175cf8bf71f937c467b1ca27a0f62
parent7f36c34daf633dcade3a3bab9cdcf14a34fed3cd (diff)
Fix issue with multiple namespace openings (#2176)
* #include an absolute path didn't work - because paths were taken to always be relative. * Added sample-grad-clamp-lod sample. * Fix handling multiple reopenings of namespaces.
-rw-r--r--source/slang/slang-ast-support-types.h3
-rw-r--r--source/slang/slang-parser.cpp25
-rw-r--r--tests/language-feature/namespaces/multiple-namespace.slang56
-rw-r--r--tests/language-feature/namespaces/multiple-namespace.slang.expected.txt4
4 files changed, 76 insertions, 12 deletions
diff --git a/source/slang/slang-ast-support-types.h b/source/slang/slang-ast-support-types.h
index e7c2287ff..ffe7cd553 100644
--- a/source/slang/slang-ast-support-types.h
+++ b/source/slang/slang-ast-support-types.h
@@ -310,6 +310,9 @@ namespace Slang
template<typename T>
bool hasModifier() { return findModifier<T>() != nullptr; }
+ /// True if has no modifiers
+ bool isEmpty() const { return first == nullptr; }
+
FilteredModifierList<Modifier>::Iterator begin() { return FilteredModifierList<Modifier>::Iterator(first); }
FilteredModifierList<Modifier>::Iterator end() { return FilteredModifierList<Modifier>::Iterator(nullptr); }
};
diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp
index 961691c9a..c08f5cf34 100644
--- a/source/slang/slang-parser.cpp
+++ b/source/slang/slang-parser.cpp
@@ -2928,8 +2928,7 @@ namespace Slang
// any non-null pointer we return to the AST).
//
NamespaceDecl* namespaceDecl = nullptr;
- NodeBase* result = nullptr;
- //
+
// In order to find out what case we are in, we start by looking
// for a namespace declaration of the same name in the parent
// declaration.
@@ -2985,14 +2984,6 @@ namespace Slang
{
namespaceDecl = parser->astBuilder->create<NamespaceDecl>();
namespaceDecl->nameAndLoc = nameAndLoc;
-
- // In the case where we are creating the first
- // declaration of the given namesapce, we need
- // to use it as the return value of the parsing
- // callback, so that it is appropriately added
- // to the parent declaration.
- //
- result = namespaceDecl;
}
}
@@ -3003,7 +2994,7 @@ namespace Slang
//
parseDeclBody(parser, namespaceDecl);
- return result;
+ return namespaceDecl;
}
static NodeBase* parseUsingDecl(Parser* parser, void* /*userData*/)
@@ -3590,6 +3581,16 @@ namespace Slang
ContainerDecl* containerDecl,
Modifiers modifiers)
{
+
+ // If this is a namespace and already added, we don't want to add to the parent
+ // Or add any modifiers
+ if (as<NamespaceDecl>(decl) && decl->parentDecl)
+ {
+ // Presumably we have no modifiers.
+ SLANG_ASSERT(modifiers.isEmpty());
+ return;
+ }
+
// Add any modifiers we parsed before the declaration to the list
// of modifiers on the declaration itself.
//
@@ -3601,9 +3602,9 @@ namespace Slang
declToModify = genericDecl->inner;
_addModifiers(declToModify, modifiers);
- // Make sure the decl is properly nested inside its lexical parent
if (containerDecl)
{
+ // Make sure the decl is properly nested inside its lexical parent
AddMember(containerDecl, decl);
}
}
diff --git a/tests/language-feature/namespaces/multiple-namespace.slang b/tests/language-feature/namespaces/multiple-namespace.slang
new file mode 100644
index 000000000..a687d5e79
--- /dev/null
+++ b/tests/language-feature/namespaces/multiple-namespace.slang
@@ -0,0 +1,56 @@
+// multiple-namespace.slang
+
+//TEST(compute):COMPARE_COMPUTE: -shaderobj
+
+// Multiple namespace open/closing
+
+namespace A
+{
+ struct Struct1
+ {
+ int a;
+ }
+}
+
+namespace A
+{
+ struct Struct2
+ {
+ int b;
+ }
+}
+
+namespace A
+{
+ struct Struct3
+ {
+ int c;
+ }
+}
+
+// A few more
+
+namespace A {}
+namespace A {}
+namespace A { namespace A {} }
+
+int test(int val)
+{
+ A.Struct1 s1 = {};
+ A::Struct2 s2 = { val };
+ A.Struct3 s3 = { val * val };
+
+ return s1.a + s2.b + s3.c;
+}
+
+//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/multiple-namespace.slang.expected.txt b/tests/language-feature/namespaces/multiple-namespace.slang.expected.txt
new file mode 100644
index 000000000..bfb7eefe7
--- /dev/null
+++ b/tests/language-feature/namespaces/multiple-namespace.slang.expected.txt
@@ -0,0 +1,4 @@
+0
+2
+6
+C