Skip to content

Commit

Permalink
Allow shader params with empty brace inits:
Browse files Browse the repository at this point in the history
    type myarray[] = {},
    mystruct S = {}

Un-lengthed arrays, if they aren't connected or given a concrete
instance value, still default to length 1 (we don't allow 0-length
arrays, that's a whole other can of worms), but this is now treated as
equivalent to `= { default_value }` (0 for int or float, "" for string).

And it works for structs, too, just means to initialize all the elements
to is obvious (0, "") default values.

It's syntactic sugar, but it removes some clutter for people who want to
have shader parameters that are arrays of undetermined length (and thus
are probably expected to have a runtime-assigned instance value or be
conntected to an output of an earlier layer within the shader group),
removes the requirement for spelling out an initializing value that
won't be used anyway.
  • Loading branch information
lgritz committed Jan 14, 2019
1 parent a32d056 commit d78df9a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/liboslcomp/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ ASTNode::codegen_initlist (ref init, TypeSpec type, Symbol *sym)
break;
}
}
if (all_const) {
if (all_const && init) {
std::vector<char> arrayvals (type.simpletype().size());
for (int i = 0; init; init = init->next(), ++i) {
ASTliteral *lit = (ASTliteral *)init.get();
Expand Down
10 changes: 10 additions & 0 deletions src/liboslcomp/oslgram.y
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,16 @@ compound_initializer
$$ = new ASTcompound_initializer (oslcompiler, $2);
$$->sourceline (@1.first_line);
}
| '{' '}'
{
if (!oslcompiler->declaring_shader_formals())
oslcompiler->error (oslcompiler->filename(),
oslcompiler->lineno(),
"Empty compound initializers '{ }' "
"only allowed for shader parameters.");
$$ = new ASTcompound_initializer (oslcompiler, nullptr);
$$->sourceline (@1.first_line);
}
;

init_expression_list
Expand Down
8 changes: 6 additions & 2 deletions src/liboslcomp/typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ ASTvariable_declaration::typecheck (TypeSpec expected)
ASSERT (! init->nextptr() &&
"compound_initializer should be the only initializer");
init = ((ASTcompound_initializer *)init.get())->initlist();
if (!init)
return m_typespec;
}

// Warning to catch confusing comma operator in variable initializers.
Expand Down Expand Up @@ -812,6 +814,8 @@ class ASTcompound_initializer::TypeAdjuster {
// Adjust the type for every element of an array
void
typecheck_array(ASTcompound_initializer* init, TypeSpec expected) {
if (! init->initlist())
return; // early out for empty initializer { }
ASSERT (expected.is_array());
// Every element of the array is the same type
TypeSpec elemtype = expected.elementtype();
Expand Down Expand Up @@ -936,8 +940,8 @@ class ASTcompound_initializer::TypeAdjuster {

TypeSpec type() const {
// If succeeded, root type is at end of m_adjust
return (m_success || m_debug_successful) ? std::get<1>(m_adjust.back())
: TypeSpec(TypeDesc::NONE);
return (m_success || m_debug_successful) && m_adjust.size()
? std::get<1>(m_adjust.back()) : TypeSpec(TypeDesc::NONE);
}

bool success() const { return m_success; }
Expand Down
6 changes: 5 additions & 1 deletion testsuite/oslc-err-initlist-return/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ test.osl:14: warning: Function 'struct bcolor badreturn ()' redefined in the sam
Previous definitions:
test.osl:13
test.osl:14: error: Cannot return a 'struct acolor' from 'struct bcolor badreturn()'
test.osl:17: error: Syntax error: syntax error
test.osl:17: error: Empty compound initializers '{ }' only allowed for shader parameters.
test.osl:17: warning: Function 'struct bcolor badreturn ()' redefined in the same scope
Previous definitions:
test.osl:14
test.osl:13
FAILED test.osl
4 changes: 4 additions & 0 deletions testsuite/struct/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ a == { 3.14159, 42, [0 1 2], foobar }
constructed x == { 6.28319, 21, [1 2 3], constructed }

struct param: aparam == { 1, 0, [3 4 5], foo! }
Default init eparam == { 0 0 0, 0 }

after c=a, c == { 3.14159, 42, [0 1 2], foobar }

Expand Down Expand Up @@ -32,6 +33,7 @@ a == { 3.14159, 42, [0 1 2], foobar }
constructed x == { 6.28319, 21, [1 2 3], constructed }

struct param: aparam == { 1, 0, [3 4 5], foo! }
Default init eparam == { 0 0 0, 0 }

after c=a, c == { 3.14159, 42, [0 1 2], foobar }

Expand Down Expand Up @@ -59,6 +61,7 @@ a == { 3.14159, 42, [0 1 2], foobar }
constructed x == { 6.28319, 21, [1 2 3], constructed }

struct param: aparam == { 1, 0, [3 4 5], foo! }
Default init eparam == { 0 0 0, 0 }

after c=a, c == { 3.14159, 42, [0 1 2], foobar }

Expand Down Expand Up @@ -86,6 +89,7 @@ a == { 3.14159, 42, [0 1 2], foobar }
constructed x == { 6.28319, 21, [1 2 3], constructed }

struct param: aparam == { 1, 0, [3 4 5], foo! }
Default init eparam == { 0 0 0, 0 }

after c=a, c == { 3.14159, 42, [0 1 2], foobar }

Expand Down
6 changes: 5 additions & 1 deletion testsuite/struct/test.osl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void func_with_struct_output_param (output Astruct ap)

shader
test (
Astruct aparam = { 1.0, 0, point(3,4,5), "foo!" }
Astruct aparam = { 1.0, 0, point(3,4,5), "foo!" },
Bstruct eparam = {}
)
{
printf ("test struct\n");
Expand Down Expand Up @@ -65,6 +66,9 @@ test (
printf ("struct param: aparam == { %g, %i, [%g], %s }\n",
aparam.f, aparam.i, aparam.p, aparam.s);

// Test that empty default initializer for eparam
printf ("Default init eparam == { %g, %g }\n", eparam.v, eparam.i);

Bstruct b;

// Structure assignment
Expand Down
2 changes: 2 additions & 0 deletions testsuite/vararray-default/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ a array length 3
b array length 2
[0] = (9 9 9)
[1] = (1 2.3 4)
c array length 1
[0] = (0 0 0)

5 changes: 4 additions & 1 deletion testsuite/vararray-default/test.osl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ void print_array_contents (string name, vector v[])
printf (" [%d] = (%g)\n", i, v[i]);
}

shader test (float a[] = {0,1,2.3}, vector b[] = {9, vector(1,2.3,4)})
shader test (float a[] = {0,1,2.3},
vector b[] = {9, vector(1,2.3,4)},
vector c[] = {})
{
print_array_contents ("a", a);
print_array_contents ("b", b);
print_array_contents ("c", c);
}

0 comments on commit d78df9a

Please sign in to comment.