Skip to content

Commit 8263f10

Browse files
committed
t
1 parent 4422c2d commit 8263f10

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

include/NZSL/Ast/ExpressionType.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ namespace nzsl::Ast
241241
std::string name;
242242
std::string tag;
243243
std::vector<StructMember> members;
244-
bool isConditional = false;
244+
unsigned int conditionIndex = 0;
245245
};
246246

247247
inline bool IsAliasType(const ExpressionType& type);

include/NZSL/Ast/SanitizeVisitor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ namespace nzsl::Ast
227227
{
228228
std::size_t index;
229229
IdentifierCategory category;
230-
bool isConditional = false;
230+
unsigned int conditionalIndex = 0;
231231
};
232232

233233
struct Identifier

src/NZSL/Ast/SanitizeVisitor.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ namespace nzsl::Ast
185185

186186
struct UsedExternalData
187187
{
188-
bool isConditional;
188+
unsigned int conditionalStatementIndex;
189189
};
190190

191191
static constexpr std::size_t ModuleIdSentinel = std::numeric_limits<std::size_t>::max();
@@ -212,8 +212,9 @@ namespace nzsl::Ast
212212
Options options;
213213
FunctionData* currentFunction = nullptr;
214214
bool allowUnknownIdentifiers = false;
215-
bool inConditionalStatement = false;
216215
bool inLoop = false;
216+
unsigned int conditionalStatementIndex = 0;
217+
unsigned int nextConditionalIndex = 1;
217218
};
218219

219220
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
@@ -447,7 +448,7 @@ namespace nzsl::Ast
447448

448449
if (!fieldPtr)
449450
{
450-
if (s->isConditional)
451+
if (s->conditionIndex != m_context->conditionalStatementIndex)
451452
return Cloner::Clone(node); //< unresolved
452453

453454
throw CompilerUnknownFieldError{ indexedExpr->sourceLocation, identifierEntry.identifier };
@@ -1354,9 +1355,9 @@ namespace nzsl::Ast
13541355

13551356
if (!conditionValue.has_value())
13561357
{
1357-
bool wasInConditionalStatement = m_context->inConditionalStatement;
1358-
m_context->inConditionalStatement = true;
1359-
Nz::CallOnExit restoreCond([=] { m_context->inConditionalStatement = wasInConditionalStatement; });
1358+
unsigned int prevCondStatementIndex = m_context->conditionalStatementIndex;
1359+
m_context->conditionalStatementIndex = m_context->nextConditionalIndex++;
1360+
Nz::CallOnExit restoreCond([=] { m_context->conditionalStatementIndex = prevCondStatementIndex; });
13601361

13611362
// Unresolvable condition
13621363
auto condStatement = ShaderBuilder::ConditionalStatement(std::move(cloneCondition), Cloner::Clone(*node.statement));
@@ -1447,7 +1448,7 @@ namespace nzsl::Ast
14471448
std::uint64_t bindingKey = BuildBindingKey(bindingSet, bindingIndex + i);
14481449
if (auto it = m_context->usedBindingIndexes.find(bindingKey); it != m_context->usedBindingIndexes.end())
14491450
{
1450-
if (!it->second.isConditional || !usedBindingData.isConditional)
1451+
if (it->second.conditionalStatementIndex == m_context->conditionalStatementIndex || usedBindingData.conditionalStatementIndex == m_context->conditionalStatementIndex)
14511452
throw CompilerExtBindingAlreadyUsedError{ sourceLoc, bindingSet, bindingIndex };
14521453
}
14531454

@@ -1462,11 +1463,11 @@ namespace nzsl::Ast
14621463
auto& extVar = clone->externalVars[i];
14631464

14641465
Context::UsedExternalData usedBindingData;
1465-
usedBindingData.isConditional = m_context->inConditionalStatement;
1466+
usedBindingData.conditionalStatementIndex = m_context->conditionalStatementIndex;
14661467

14671468
if (auto it = m_context->declaredExternalVar.find(extVar.name); it != m_context->declaredExternalVar.end())
14681469
{
1469-
if (!it->second.isConditional || !usedBindingData.isConditional)
1470+
if (it->second.conditionalStatementIndex == m_context->conditionalStatementIndex || usedBindingData.conditionalStatementIndex == m_context->conditionalStatementIndex)
14701471
throw CompilerExtAlreadyDeclaredError{ extVar.sourceLocation, extVar.name };
14711472
}
14721473

@@ -1586,7 +1587,7 @@ namespace nzsl::Ast
15861587
bindingIndex++;
15871588

15881589
Context::UsedExternalData usedBindingData;
1589-
usedBindingData.isConditional = m_context->inConditionalStatement;
1590+
usedBindingData.conditionalStatementIndex = m_context->conditionalStatementIndex;
15901591

15911592
extVar.bindingIndex = bindingIndex;
15921593
RegisterBinding(arraySize, bindingSet, bindingIndex, usedBindingData, extVar.sourceLocation);
@@ -1912,7 +1913,7 @@ namespace nzsl::Ast
19121913
}
19131914
}
19141915

1915-
clone->description.isConditional = m_context->inConditionalStatement;
1916+
clone->description.conditionIndex = m_context->conditionalStatementIndex;
19161917

19171918
clone->structIndex = RegisterStruct(clone->description.name, &clone->description, clone->structIndex, clone->sourceLocation);
19181919
SanitizeIdentifier(clone->description.name, IdentifierScope::Struct);
@@ -3547,7 +3548,7 @@ namespace nzsl::Ast
35473548
bool unresolved = false;
35483549
if (const IdentifierData* identifierData = FindIdentifier(name))
35493550
{
3550-
if (!m_context->inConditionalStatement || !identifierData->isConditional)
3551+
if (identifierData->conditionalIndex == m_context->conditionalStatementIndex)
35513552
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
35523553
else
35533554
unresolved = true;
@@ -3571,7 +3572,7 @@ namespace nzsl::Ast
35713572
{
35723573
aliasIndex,
35733574
IdentifierCategory::Alias,
3574-
m_context->inConditionalStatement
3575+
m_context->conditionalStatementIndex
35753576
}
35763577
});
35773578
}
@@ -3602,7 +3603,7 @@ namespace nzsl::Ast
36023603
{
36033604
constantIndex,
36043605
IdentifierCategory::Constant,
3605-
m_context->inConditionalStatement
3606+
m_context->conditionalStatementIndex
36063607
}
36073608
});
36083609

@@ -3654,7 +3655,7 @@ namespace nzsl::Ast
36543655
{
36553656
functionIndex,
36563657
IdentifierCategory::Function,
3657-
m_context->inConditionalStatement
3658+
m_context->conditionalStatementIndex
36583659
}
36593660
});
36603661

@@ -3673,7 +3674,7 @@ namespace nzsl::Ast
36733674
{
36743675
intrinsicIndex,
36753676
IdentifierCategory::Intrinsic,
3676-
m_context->inConditionalStatement
3677+
m_context->conditionalStatementIndex
36773678
}
36783679
});
36793680

@@ -3692,7 +3693,7 @@ namespace nzsl::Ast
36923693
{
36933694
moduleIndex,
36943695
IdentifierCategory::Module,
3695-
m_context->inConditionalStatement
3696+
m_context->conditionalStatementIndex
36963697
}
36973698
});
36983699

@@ -3706,7 +3707,7 @@ namespace nzsl::Ast
37063707
{
37073708
std::numeric_limits<std::size_t>::max(),
37083709
IdentifierCategory::ReservedName,
3709-
m_context->inConditionalStatement
3710+
m_context->conditionalStatementIndex
37103711
}
37113712
});
37123713
}
@@ -3716,7 +3717,7 @@ namespace nzsl::Ast
37163717
bool unresolved = false;
37173718
if (const IdentifierData* identifierData = FindIdentifier(name))
37183719
{
3719-
if (!m_context->inConditionalStatement || !identifierData->isConditional)
3720+
if (identifierData->conditionalIndex == m_context->conditionalStatementIndex)
37203721
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
37213722
else
37223723
unresolved = true;
@@ -3740,7 +3741,7 @@ namespace nzsl::Ast
37403741
{
37413742
structIndex,
37423743
IdentifierCategory::Struct,
3743-
m_context->inConditionalStatement
3744+
m_context->conditionalStatementIndex
37443745
}
37453746
});
37463747
}
@@ -3771,7 +3772,7 @@ namespace nzsl::Ast
37713772
{
37723773
typeIndex,
37733774
IdentifierCategory::Type,
3774-
m_context->inConditionalStatement
3775+
m_context->conditionalStatementIndex
37753776
}
37763777
});
37773778

@@ -3805,7 +3806,7 @@ namespace nzsl::Ast
38053806
{
38063807
typeIndex,
38073808
IdentifierCategory::Type,
3808-
m_context->inConditionalStatement
3809+
m_context->conditionalStatementIndex
38093810
}
38103811
});
38113812

@@ -3819,7 +3820,7 @@ namespace nzsl::Ast
38193820
{
38203821
std::numeric_limits<std::size_t>::max(),
38213822
IdentifierCategory::Unresolved,
3822-
m_context->inConditionalStatement
3823+
m_context->conditionalStatementIndex
38233824
}
38243825
});
38253826
}
@@ -3832,7 +3833,8 @@ namespace nzsl::Ast
38323833
// Allow variable shadowing
38333834
if (identifier->category != IdentifierCategory::Variable)
38343835
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
3835-
else if (identifier->isConditional && m_context->inConditionalStatement)
3836+
3837+
if (identifier->conditionalIndex != m_context->conditionalStatementIndex)
38363838
unresolved = true; //< right variable isn't know from this point
38373839
}
38383840

@@ -3854,7 +3856,7 @@ namespace nzsl::Ast
38543856
{
38553857
varIndex,
38563858
IdentifierCategory::Variable,
3857-
m_context->inConditionalStatement
3859+
m_context->conditionalStatementIndex
38583860
}
38593861
});
38603862
}

tests/src/Tests/ShaderUtils.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ void ExpectGLSL(nzsl::ShaderStageType stageType, const nzsl::Ast::Module& shader
294294

295295
void ExpectGLSL(const nzsl::Ast::Module& shaderModule, std::string_view expectedOutput, const nzsl::ShaderWriter::States& options, const nzsl::GlslWriter::Environment& env, bool testShaderCompilation)
296296
{
297-
298297
// Retrieve entry-point to get shader type
299298
std::optional<nzsl::ShaderStageType> entryShaderStage;
300299

0 commit comments

Comments
 (0)