From 676560b05f945335ff9e091934f1aea3ff373ced Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 9 Oct 2017 14:44:40 -0700 Subject: Fix emit logic for `cbuffer` member with initializer (#205) Given an input declaration like: cbuffer C { int a = -1; } Slang was automatically generating a `packoffset` semantic to place the member manually, but was emitting it *after* the initializer of the original declaration: cbuffer C : register(b) { int a = -1 : packoffset(c0); } That syntax is invalid, of course, and we actually want: cbuffer C : register(b) { int a : packoffset(c0) = -1; } This wasn't spotted earlier because putting initializers on a `cbuffer` member is not commonly done, since it requires reading those values via the reflection API. Slang's reflection API currently provides no way to access default values like this, so they aren't of much use yet. Still, it is better to emit correct syntax even in cases like this one. --- source/slang/emit.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 938d54d36..8483caefc 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -3291,7 +3291,8 @@ struct EmitVisitor } // Shared emit logic for variable declarations (used for parameters, locals, globals, fields) - void EmitVarDeclCommon(DeclRef declRef) + + void emitVarDeclHead(DeclRef declRef) { EmitModifiers(declRef.getDecl()); @@ -3306,7 +3307,10 @@ struct EmitVisitor } EmitSemantics(declRef.getDecl()); + } + void emitVarDeclInit(DeclRef declRef) + { // TODO(tfoley): technically have to apply substitution here too... if (auto initExpr = declRef.getDecl()->initExpr) { @@ -3323,6 +3327,12 @@ struct EmitVisitor } } + void EmitVarDeclCommon(DeclRef declRef) + { + emitVarDeclHead(declRef); + emitVarDeclInit(declRef); + } + // Shared emit logic for variable declarations (used for parameters, locals, globals, fields) void EmitVarDeclCommon(RefPtr decl) { @@ -3532,7 +3542,7 @@ struct EmitVisitor { int fieldIndex = fieldCounter++; - EmitVarDeclCommon(field); + emitVarDeclHead(field); RefPtr fieldLayout = structTypeLayout->fields[fieldIndex]; SLANG_RELEASE_ASSERT(fieldLayout->varDecl.GetName() == field.GetName()); @@ -3540,6 +3550,8 @@ struct EmitVisitor // Emit explicit layout annotations for every field emitHLSLParameterBlockFieldLayoutSemantics(layout, fieldLayout); + emitVarDeclInit(field); + Emit(";\n"); } } @@ -3809,9 +3821,9 @@ struct EmitVisitor } } - EmitVarDeclCommon(decl); - + emitVarDeclHead(makeDeclRef(decl.Ptr())); emitHLSLRegisterSemantics(layout); + emitVarDeclInit(makeDeclRef(decl.Ptr())); Emit(";\n"); } -- cgit v1.2.3