Skip to content

Commit de4a36a

Browse files
committed
Fix unresolved alias not being registered as identifiers
Also check that identifiers are not conditional before taking them the conditional index should be working but we should be able to do better (using "conditional scope id")
1 parent 4422c2d commit de4a36a

File tree

5 files changed

+177
-28
lines changed

5 files changed

+177
-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: 38 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 currentConditionalIndex = 0;
217+
unsigned int nextConditionalIndex = 1;
217218
};
218219

219220
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
@@ -323,7 +324,12 @@ namespace nzsl::Ast
323324
const auto& env = *m_context->modules[moduleIndex].environment;
324325
identifierData = FindIdentifier(env, node.identifiers.front().identifier);
325326
if (identifierData)
327+
{
328+
if (m_context->options.partialSanitization && identifierData->conditionalIndex != m_context->currentConditionalIndex)
329+
return Cloner::Clone(node);
330+
326331
return HandleIdentifier(identifierData, node.identifiers.front().sourceLocation);
332+
}
327333
}
328334
}
329335

@@ -447,7 +453,7 @@ namespace nzsl::Ast
447453

448454
if (!fieldPtr)
449455
{
450-
if (s->isConditional)
456+
if (s->conditionIndex != m_context->currentConditionalIndex)
451457
return Cloner::Clone(node); //< unresolved
452458

453459
throw CompilerUnknownFieldError{ indexedExpr->sourceLocation, identifierEntry.identifier };
@@ -1150,6 +1156,9 @@ namespace nzsl::Ast
11501156
if (identifierData->category == IdentifierCategory::Unresolved)
11511157
return Cloner::Clone(node);
11521158

1159+
if (m_context->options.partialSanitization && identifierData->conditionalIndex != m_context->currentConditionalIndex)
1160+
return Cloner::Clone(node);
1161+
11531162
return HandleIdentifier(identifierData, node.sourceLocation);
11541163
}
11551164

@@ -1354,9 +1363,9 @@ namespace nzsl::Ast
13541363

13551364
if (!conditionValue.has_value())
13561365
{
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; });
13601369

13611370
// Unresolvable condition
13621371
auto condStatement = ShaderBuilder::ConditionalStatement(std::move(cloneCondition), Cloner::Clone(*node.statement));
@@ -1447,7 +1456,7 @@ namespace nzsl::Ast
14471456
std::uint64_t bindingKey = BuildBindingKey(bindingSet, bindingIndex + i);
14481457
if (auto it = m_context->usedBindingIndexes.find(bindingKey); it != m_context->usedBindingIndexes.end())
14491458
{
1450-
if (!it->second.isConditional || !usedBindingData.isConditional)
1459+
if (it->second.conditionalStatementIndex == m_context->currentConditionalIndex || usedBindingData.conditionalStatementIndex == m_context->currentConditionalIndex)
14511460
throw CompilerExtBindingAlreadyUsedError{ sourceLoc, bindingSet, bindingIndex };
14521461
}
14531462

@@ -1462,11 +1471,11 @@ namespace nzsl::Ast
14621471
auto& extVar = clone->externalVars[i];
14631472

14641473
Context::UsedExternalData usedBindingData;
1465-
usedBindingData.isConditional = m_context->inConditionalStatement;
1474+
usedBindingData.conditionalStatementIndex = m_context->currentConditionalIndex;
14661475

14671476
if (auto it = m_context->declaredExternalVar.find(extVar.name); it != m_context->declaredExternalVar.end())
14681477
{
1469-
if (!it->second.isConditional || !usedBindingData.isConditional)
1478+
if (it->second.conditionalStatementIndex == m_context->currentConditionalIndex || usedBindingData.conditionalStatementIndex == m_context->currentConditionalIndex)
14701479
throw CompilerExtAlreadyDeclaredError{ extVar.sourceLocation, extVar.name };
14711480
}
14721481

@@ -1586,7 +1595,7 @@ namespace nzsl::Ast
15861595
bindingIndex++;
15871596

15881597
Context::UsedExternalData usedBindingData;
1589-
usedBindingData.isConditional = m_context->inConditionalStatement;
1598+
usedBindingData.conditionalStatementIndex = m_context->currentConditionalIndex;
15901599

15911600
extVar.bindingIndex = bindingIndex;
15921601
RegisterBinding(arraySize, bindingSet, bindingIndex, usedBindingData, extVar.sourceLocation);
@@ -1912,7 +1921,7 @@ namespace nzsl::Ast
19121921
}
19131922
}
19141923

1915-
clone->description.isConditional = m_context->inConditionalStatement;
1924+
clone->description.conditionIndex = m_context->currentConditionalIndex;
19161925

19171926
clone->structIndex = RegisterStruct(clone->description.name, &clone->description, clone->structIndex, clone->sourceLocation);
19181927
SanitizeIdentifier(clone->description.name, IdentifierScope::Struct);
@@ -3547,7 +3556,7 @@ namespace nzsl::Ast
35473556
bool unresolved = false;
35483557
if (const IdentifierData* identifierData = FindIdentifier(name))
35493558
{
3550-
if (!m_context->inConditionalStatement || !identifierData->isConditional)
3559+
if (identifierData->conditionalIndex == m_context->currentConditionalIndex)
35513560
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
35523561
else
35533562
unresolved = true;
@@ -3571,7 +3580,7 @@ namespace nzsl::Ast
35713580
{
35723581
aliasIndex,
35733582
IdentifierCategory::Alias,
3574-
m_context->inConditionalStatement
3583+
m_context->currentConditionalIndex
35753584
}
35763585
});
35773586
}
@@ -3602,7 +3611,7 @@ namespace nzsl::Ast
36023611
{
36033612
constantIndex,
36043613
IdentifierCategory::Constant,
3605-
m_context->inConditionalStatement
3614+
m_context->currentConditionalIndex
36063615
}
36073616
});
36083617

@@ -3654,7 +3663,7 @@ namespace nzsl::Ast
36543663
{
36553664
functionIndex,
36563665
IdentifierCategory::Function,
3657-
m_context->inConditionalStatement
3666+
m_context->currentConditionalIndex
36583667
}
36593668
});
36603669

@@ -3673,7 +3682,7 @@ namespace nzsl::Ast
36733682
{
36743683
intrinsicIndex,
36753684
IdentifierCategory::Intrinsic,
3676-
m_context->inConditionalStatement
3685+
m_context->currentConditionalIndex
36773686
}
36783687
});
36793688

@@ -3692,7 +3701,7 @@ namespace nzsl::Ast
36923701
{
36933702
moduleIndex,
36943703
IdentifierCategory::Module,
3695-
m_context->inConditionalStatement
3704+
m_context->currentConditionalIndex
36963705
}
36973706
});
36983707

@@ -3706,7 +3715,7 @@ namespace nzsl::Ast
37063715
{
37073716
std::numeric_limits<std::size_t>::max(),
37083717
IdentifierCategory::ReservedName,
3709-
m_context->inConditionalStatement
3718+
m_context->currentConditionalIndex
37103719
}
37113720
});
37123721
}
@@ -3716,7 +3725,7 @@ namespace nzsl::Ast
37163725
bool unresolved = false;
37173726
if (const IdentifierData* identifierData = FindIdentifier(name))
37183727
{
3719-
if (!m_context->inConditionalStatement || !identifierData->isConditional)
3728+
if (identifierData->conditionalIndex == m_context->currentConditionalIndex)
37203729
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
37213730
else
37223731
unresolved = true;
@@ -3740,7 +3749,7 @@ namespace nzsl::Ast
37403749
{
37413750
structIndex,
37423751
IdentifierCategory::Struct,
3743-
m_context->inConditionalStatement
3752+
m_context->currentConditionalIndex
37443753
}
37453754
});
37463755
}
@@ -3771,7 +3780,7 @@ namespace nzsl::Ast
37713780
{
37723781
typeIndex,
37733782
IdentifierCategory::Type,
3774-
m_context->inConditionalStatement
3783+
m_context->currentConditionalIndex
37753784
}
37763785
});
37773786

@@ -3805,7 +3814,7 @@ namespace nzsl::Ast
38053814
{
38063815
typeIndex,
38073816
IdentifierCategory::Type,
3808-
m_context->inConditionalStatement
3817+
m_context->currentConditionalIndex
38093818
}
38103819
});
38113820

@@ -3819,7 +3828,7 @@ namespace nzsl::Ast
38193828
{
38203829
std::numeric_limits<std::size_t>::max(),
38213830
IdentifierCategory::Unresolved,
3822-
m_context->inConditionalStatement
3831+
m_context->currentConditionalIndex
38233832
}
38243833
});
38253834
}
@@ -3832,7 +3841,8 @@ namespace nzsl::Ast
38323841
// Allow variable shadowing
38333842
if (identifier->category != IdentifierCategory::Variable)
38343843
throw CompilerIdentifierAlreadyUsedError{ sourceLocation, name };
3835-
else if (identifier->isConditional && m_context->inConditionalStatement)
3844+
3845+
if (identifier->conditionalIndex != m_context->currentConditionalIndex)
38363846
unresolved = true; //< right variable isn't know from this point
38373847
}
38383848

@@ -3854,7 +3864,7 @@ namespace nzsl::Ast
38543864
{
38553865
varIndex,
38563866
IdentifierCategory::Variable,
3857-
m_context->inConditionalStatement
3867+
m_context->currentConditionalIndex
38583868
}
38593869
});
38603870
}
@@ -4102,7 +4112,10 @@ namespace nzsl::Ast
41024112

41034113
const ExpressionType* exprType = GetExpressionType(*node.expression);
41044114
if (!exprType)
4115+
{
4116+
RegisterUnresolved(node.name);
41054117
return ValidationResult::Unresolved;
4118+
}
41064119

41074120
const ExpressionType& resolvedType = ResolveAlias(*exprType);
41084121

0 commit comments

Comments
 (0)