HLSL: allow implicit array sizing.
In HLSL array sizes need not be provided explicitly in all circumstances.
For example, this is valid (note no number between the [ ]):
// no explicit array size
uniform float g_array[] = { 1, 2, 3, 4, 5 };
This PR does not attempt to validate most invalid cases.
A new test is added to verify the resulting linker objects.
diff --git a/hlsl/hlslGrammar.cpp b/hlsl/hlslGrammar.cpp
index 30d42f1..48698e5 100755
--- a/hlsl/hlslGrammar.cpp
+++ b/hlsl/hlslGrammar.cpp
@@ -1515,8 +1515,14 @@
// array_specifier
TArraySizes* arraySizes = nullptr;
acceptArraySpecifier(arraySizes);
- if (arraySizes)
+ if (arraySizes) {
+ if (arraySizes->isImplicit()) {
+ parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
+ return false;
+ }
+
type->newArraySizes(*arraySizes);
+ }
// post_decls
acceptPostDecls(type->getQualifier());
@@ -2601,6 +2607,7 @@
// array_specifier
// : LEFT_BRACKET integer_expression RGHT_BRACKET post_decls // optional
+// : LEFT_BRACKET RGHT_BRACKET post_decls // optional
//
void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
{
@@ -2610,21 +2617,25 @@
return;
TSourceLoc loc = token.loc;
- TIntermTyped* sizeExpr;
- if (! acceptAssignmentExpression(sizeExpr)) {
- expected("array-sizing expression");
- return;
- }
+ TIntermTyped* sizeExpr = nullptr;
+
+ // Array sizing expression is optional. If ommitted, array is implicitly sized.
+ const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
if (! acceptTokenClass(EHTokRightBracket)) {
expected("]");
return;
}
- TArraySize arraySize;
- parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
arraySizes = new TArraySizes;
- arraySizes->addInnerSize(arraySize);
+
+ if (hasArraySize) {
+ TArraySize arraySize;
+ parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
+ arraySizes->addInnerSize(arraySize);
+ } else {
+ arraySizes->addInnerSize(); // implicitly sized
+ }
}
// post_decls