@@ -185,7 +185,7 @@ namespace nzsl::Ast
185
185
186
186
struct UsedExternalData
187
187
{
188
- bool isConditional ;
188
+ unsigned int conditionalStatementIndex ;
189
189
};
190
190
191
191
static constexpr std::size_t ModuleIdSentinel = std::numeric_limits<std::size_t >::max();
@@ -212,8 +212,9 @@ namespace nzsl::Ast
212
212
Options options;
213
213
FunctionData* currentFunction = nullptr ;
214
214
bool allowUnknownIdentifiers = false ;
215
- bool inConditionalStatement = false ;
216
215
bool inLoop = false ;
216
+ unsigned int currentConditionalIndex = 0 ;
217
+ unsigned int nextConditionalIndex = 1 ;
217
218
};
218
219
219
220
ModulePtr SanitizeVisitor::Sanitize (const Module& module , const Options& options, std::string* error)
@@ -323,7 +324,12 @@ namespace nzsl::Ast
323
324
const auto & env = *m_context->modules [moduleIndex].environment ;
324
325
identifierData = FindIdentifier (env, node.identifiers .front ().identifier );
325
326
if (identifierData)
327
+ {
328
+ if (m_context->options .partialSanitization && identifierData->conditionalIndex != m_context->currentConditionalIndex )
329
+ return Cloner::Clone (node);
330
+
326
331
return HandleIdentifier (identifierData, node.identifiers .front ().sourceLocation );
332
+ }
327
333
}
328
334
}
329
335
@@ -447,7 +453,7 @@ namespace nzsl::Ast
447
453
448
454
if (!fieldPtr)
449
455
{
450
- if (s->isConditional )
456
+ if (s->conditionIndex != m_context-> currentConditionalIndex )
451
457
return Cloner::Clone (node); // < unresolved
452
458
453
459
throw CompilerUnknownFieldError{ indexedExpr->sourceLocation , identifierEntry.identifier };
@@ -1150,6 +1156,9 @@ namespace nzsl::Ast
1150
1156
if (identifierData->category == IdentifierCategory::Unresolved)
1151
1157
return Cloner::Clone (node);
1152
1158
1159
+ if (m_context->options .partialSanitization && identifierData->conditionalIndex != m_context->currentConditionalIndex )
1160
+ return Cloner::Clone (node);
1161
+
1153
1162
return HandleIdentifier (identifierData, node.sourceLocation );
1154
1163
}
1155
1164
@@ -1354,9 +1363,9 @@ namespace nzsl::Ast
1354
1363
1355
1364
if (!conditionValue.has_value ())
1356
1365
{
1357
- bool wasInConditionalStatement = m_context->inConditionalStatement ;
1358
- m_context->inConditionalStatement = true ;
1359
- Nz::CallOnExit restoreCond ([=] { m_context->inConditionalStatement = wasInConditionalStatement ; });
1366
+ unsigned int prevCondStatementIndex = m_context->currentConditionalIndex ;
1367
+ m_context->currentConditionalIndex = m_context-> nextConditionalIndex ++ ;
1368
+ Nz::CallOnExit restoreCond ([=] { m_context->currentConditionalIndex = prevCondStatementIndex ; });
1360
1369
1361
1370
// Unresolvable condition
1362
1371
auto condStatement = ShaderBuilder::ConditionalStatement (std::move (cloneCondition), Cloner::Clone (*node.statement ));
@@ -1447,7 +1456,7 @@ namespace nzsl::Ast
1447
1456
std::uint64_t bindingKey = BuildBindingKey (bindingSet, bindingIndex + i);
1448
1457
if (auto it = m_context->usedBindingIndexes .find (bindingKey); it != m_context->usedBindingIndexes .end ())
1449
1458
{
1450
- if (! it->second .isConditional || ! usedBindingData.isConditional )
1459
+ if (it->second .conditionalStatementIndex == m_context-> currentConditionalIndex || usedBindingData.conditionalStatementIndex == m_context-> currentConditionalIndex )
1451
1460
throw CompilerExtBindingAlreadyUsedError{ sourceLoc, bindingSet, bindingIndex };
1452
1461
}
1453
1462
@@ -1462,11 +1471,11 @@ namespace nzsl::Ast
1462
1471
auto & extVar = clone->externalVars [i];
1463
1472
1464
1473
Context::UsedExternalData usedBindingData;
1465
- usedBindingData.isConditional = m_context->inConditionalStatement ;
1474
+ usedBindingData.conditionalStatementIndex = m_context->currentConditionalIndex ;
1466
1475
1467
1476
if (auto it = m_context->declaredExternalVar .find (extVar.name ); it != m_context->declaredExternalVar .end ())
1468
1477
{
1469
- if (! it->second .isConditional || ! usedBindingData.isConditional )
1478
+ if (it->second .conditionalStatementIndex == m_context-> currentConditionalIndex || usedBindingData.conditionalStatementIndex == m_context-> currentConditionalIndex )
1470
1479
throw CompilerExtAlreadyDeclaredError{ extVar.sourceLocation , extVar.name };
1471
1480
}
1472
1481
@@ -1586,7 +1595,7 @@ namespace nzsl::Ast
1586
1595
bindingIndex++;
1587
1596
1588
1597
Context::UsedExternalData usedBindingData;
1589
- usedBindingData.isConditional = m_context->inConditionalStatement ;
1598
+ usedBindingData.conditionalStatementIndex = m_context->currentConditionalIndex ;
1590
1599
1591
1600
extVar.bindingIndex = bindingIndex;
1592
1601
RegisterBinding (arraySize, bindingSet, bindingIndex, usedBindingData, extVar.sourceLocation );
@@ -1912,7 +1921,7 @@ namespace nzsl::Ast
1912
1921
}
1913
1922
}
1914
1923
1915
- clone->description .isConditional = m_context->inConditionalStatement ;
1924
+ clone->description .conditionIndex = m_context->currentConditionalIndex ;
1916
1925
1917
1926
clone->structIndex = RegisterStruct (clone->description .name , &clone->description , clone->structIndex , clone->sourceLocation );
1918
1927
SanitizeIdentifier (clone->description .name , IdentifierScope::Struct);
@@ -3547,7 +3556,7 @@ namespace nzsl::Ast
3547
3556
bool unresolved = false ;
3548
3557
if (const IdentifierData* identifierData = FindIdentifier (name))
3549
3558
{
3550
- if (!m_context-> inConditionalStatement || !identifierData-> isConditional )
3559
+ if (identifierData-> conditionalIndex == m_context-> currentConditionalIndex )
3551
3560
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
3552
3561
else
3553
3562
unresolved = true ;
@@ -3571,7 +3580,7 @@ namespace nzsl::Ast
3571
3580
{
3572
3581
aliasIndex,
3573
3582
IdentifierCategory::Alias,
3574
- m_context->inConditionalStatement
3583
+ m_context->currentConditionalIndex
3575
3584
}
3576
3585
});
3577
3586
}
@@ -3602,7 +3611,7 @@ namespace nzsl::Ast
3602
3611
{
3603
3612
constantIndex,
3604
3613
IdentifierCategory::Constant,
3605
- m_context->inConditionalStatement
3614
+ m_context->currentConditionalIndex
3606
3615
}
3607
3616
});
3608
3617
@@ -3654,7 +3663,7 @@ namespace nzsl::Ast
3654
3663
{
3655
3664
functionIndex,
3656
3665
IdentifierCategory::Function,
3657
- m_context->inConditionalStatement
3666
+ m_context->currentConditionalIndex
3658
3667
}
3659
3668
});
3660
3669
@@ -3673,7 +3682,7 @@ namespace nzsl::Ast
3673
3682
{
3674
3683
intrinsicIndex,
3675
3684
IdentifierCategory::Intrinsic,
3676
- m_context->inConditionalStatement
3685
+ m_context->currentConditionalIndex
3677
3686
}
3678
3687
});
3679
3688
@@ -3692,7 +3701,7 @@ namespace nzsl::Ast
3692
3701
{
3693
3702
moduleIndex,
3694
3703
IdentifierCategory::Module,
3695
- m_context->inConditionalStatement
3704
+ m_context->currentConditionalIndex
3696
3705
}
3697
3706
});
3698
3707
@@ -3706,7 +3715,7 @@ namespace nzsl::Ast
3706
3715
{
3707
3716
std::numeric_limits<std::size_t >::max (),
3708
3717
IdentifierCategory::ReservedName,
3709
- m_context->inConditionalStatement
3718
+ m_context->currentConditionalIndex
3710
3719
}
3711
3720
});
3712
3721
}
@@ -3716,7 +3725,7 @@ namespace nzsl::Ast
3716
3725
bool unresolved = false ;
3717
3726
if (const IdentifierData* identifierData = FindIdentifier (name))
3718
3727
{
3719
- if (!m_context-> inConditionalStatement || !identifierData-> isConditional )
3728
+ if (identifierData-> conditionalIndex == m_context-> currentConditionalIndex )
3720
3729
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
3721
3730
else
3722
3731
unresolved = true ;
@@ -3740,7 +3749,7 @@ namespace nzsl::Ast
3740
3749
{
3741
3750
structIndex,
3742
3751
IdentifierCategory::Struct,
3743
- m_context->inConditionalStatement
3752
+ m_context->currentConditionalIndex
3744
3753
}
3745
3754
});
3746
3755
}
@@ -3771,7 +3780,7 @@ namespace nzsl::Ast
3771
3780
{
3772
3781
typeIndex,
3773
3782
IdentifierCategory::Type,
3774
- m_context->inConditionalStatement
3783
+ m_context->currentConditionalIndex
3775
3784
}
3776
3785
});
3777
3786
@@ -3805,7 +3814,7 @@ namespace nzsl::Ast
3805
3814
{
3806
3815
typeIndex,
3807
3816
IdentifierCategory::Type,
3808
- m_context->inConditionalStatement
3817
+ m_context->currentConditionalIndex
3809
3818
}
3810
3819
});
3811
3820
@@ -3819,7 +3828,7 @@ namespace nzsl::Ast
3819
3828
{
3820
3829
std::numeric_limits<std::size_t >::max (),
3821
3830
IdentifierCategory::Unresolved,
3822
- m_context->inConditionalStatement
3831
+ m_context->currentConditionalIndex
3823
3832
}
3824
3833
});
3825
3834
}
@@ -3832,7 +3841,8 @@ namespace nzsl::Ast
3832
3841
// Allow variable shadowing
3833
3842
if (identifier->category != IdentifierCategory::Variable)
3834
3843
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
3835
- else if (identifier->isConditional && m_context->inConditionalStatement )
3844
+
3845
+ if (identifier->conditionalIndex != m_context->currentConditionalIndex )
3836
3846
unresolved = true ; // < right variable isn't know from this point
3837
3847
}
3838
3848
@@ -3854,7 +3864,7 @@ namespace nzsl::Ast
3854
3864
{
3855
3865
varIndex,
3856
3866
IdentifierCategory::Variable,
3857
- m_context->inConditionalStatement
3867
+ m_context->currentConditionalIndex
3858
3868
}
3859
3869
});
3860
3870
}
@@ -4102,7 +4112,10 @@ namespace nzsl::Ast
4102
4112
4103
4113
const ExpressionType* exprType = GetExpressionType (*node.expression );
4104
4114
if (!exprType)
4115
+ {
4116
+ RegisterUnresolved (node.name );
4105
4117
return ValidationResult::Unresolved;
4118
+ }
4106
4119
4107
4120
const ExpressionType& resolvedType = ResolveAlias (*exprType);
4108
4121
0 commit comments