Shader/SanitizeVisitor: Fix variable shadowing
This commit is contained in:
parent
0fe7f6b899
commit
6ee3899400
|
|
@ -2271,7 +2271,21 @@ namespace Nz::ShaderAst
|
|||
TypeMustMatch(resolvedType, GetExpressionType(*node.initialExpression));
|
||||
}
|
||||
|
||||
if (m_context->options.makeVariableNameUnique && FindIdentifier(node.varName) != nullptr)
|
||||
node.varIndex = RegisterVariable(node.varName, resolvedType);
|
||||
node.varType = std::move(resolvedType);
|
||||
|
||||
if (m_context->options.makeVariableNameUnique)
|
||||
{
|
||||
// Since we are registered, FindIdentifier will find us
|
||||
auto IgnoreOurself = [varIndex = *node.varIndex](const Identifier& identifier)
|
||||
{
|
||||
if (identifier.type == Identifier::Type::Variable && identifier.index == varIndex)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
if (FindIdentifier(node.varName, IgnoreOurself) != nullptr)
|
||||
{
|
||||
// Try to make variable name unique by appending _X to its name (incrementing X until it's unique) to the variable name until by incrementing X
|
||||
unsigned int cloneIndex = 2;
|
||||
|
|
@ -2280,13 +2294,11 @@ namespace Nz::ShaderAst
|
|||
{
|
||||
candidateName = node.varName + "_" + std::to_string(cloneIndex++);
|
||||
}
|
||||
while (FindIdentifier(candidateName) != nullptr);
|
||||
while (FindIdentifier(candidateName, IgnoreOurself) != nullptr);
|
||||
|
||||
node.varName = std::move(candidateName);
|
||||
}
|
||||
|
||||
node.varIndex = RegisterVariable(node.varName, resolvedType);
|
||||
node.varType = std::move(resolvedType);
|
||||
}
|
||||
|
||||
SanitizeIdentifier(node.varName);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue