Shader: Error fixes

This commit is contained in:
SirLynix
2022-04-06 09:06:02 +02:00
parent 8784ec9b47
commit 447cdfddc9
5 changed files with 35 additions and 27 deletions

View File

@@ -45,7 +45,7 @@ namespace Nz::ShaderAst
{
if (index < availableIndices.GetSize())
{
if (!availableIndices.Test(index))
if (!availableIndices.Test(index) && !preregisteredIndices.UnboundedTest(index))
throw ShaderLang::AstAlreadyUsedIndexPreregisterError{ sourceLocation, index };
}
else if (index >= availableIndices.GetSize())
@@ -1258,16 +1258,16 @@ namespace Nz::ShaderAst
{
if (member.cond.HasValue())
{
ComputeExprValue(member.cond, node.sourceLocation);
ComputeExprValue(member.cond, member.sourceLocation);
if (member.cond.IsResultingValue() && !member.cond.GetResultingValue())
continue;
}
if (member.builtin.HasValue())
ComputeExprValue(member.builtin, node.sourceLocation);
ComputeExprValue(member.builtin, member.sourceLocation);
if (member.locationIndex.HasValue())
ComputeExprValue(member.locationIndex, node.sourceLocation);
ComputeExprValue(member.locationIndex, member.sourceLocation);
if (member.builtin.HasValue() && member.locationIndex.HasValue())
throw ShaderLang::CompilerStructFieldBuiltinLocationError{ member.sourceLocation };
@@ -2097,7 +2097,9 @@ namespace Nz::ShaderAst
if (attribute.IsExpression())
{
std::optional<ConstantValue> value = ComputeConstantValue(*attribute.GetExpression());
auto& expr = *attribute.GetExpression();
std::optional<ConstantValue> value = ComputeConstantValue(expr);
if (!value)
return ValidationResult::Unresolved;
@@ -2109,13 +2111,13 @@ namespace Nz::ShaderAst
if (std::holds_alternative<Int32>(*value) && std::is_same_v<T, UInt32>)
attribute = static_cast<UInt32>(std::get<Int32>(*value));
else
throw ShaderLang::CompilerAttributeUnexpectedTypeError{ sourceLocation };
throw ShaderLang::CompilerAttributeUnexpectedTypeError{ expr.sourceLocation };
}
else
attribute = std::get<T>(*value);
}
else
throw ShaderLang::CompilerAttributeUnexpectedExpressionError{ sourceLocation };
throw ShaderLang::CompilerAttributeUnexpectedExpressionError{ expr.sourceLocation };
}
return ValidationResult::Validated;
@@ -2129,6 +2131,8 @@ namespace Nz::ShaderAst
if (attribute.IsExpression())
{
auto& expr = *attribute.GetExpression();
std::optional<ConstantValue> value = ComputeConstantValue(*attribute.GetExpression());
if (!value)
{
@@ -2144,13 +2148,13 @@ namespace Nz::ShaderAst
if (std::holds_alternative<Int32>(*value) && std::is_same_v<T, UInt32>)
targetAttribute = static_cast<UInt32>(std::get<Int32>(*value));
else
throw ShaderLang::CompilerAttributeUnexpectedTypeError{ sourceLocation };
throw ShaderLang::CompilerAttributeUnexpectedTypeError{ expr.sourceLocation };
}
else
targetAttribute = std::get<T>(*value);
}
else
throw ShaderLang::CompilerAttributeUnexpectedExpressionError{ sourceLocation };
throw ShaderLang::CompilerAttributeUnexpectedExpressionError{ expr.sourceLocation };
}
else
{

View File

@@ -81,9 +81,15 @@ namespace Nz::ShaderLang
};
UInt32 currentLine = 1;
std::size_t lastLineFeed = 0;
std::size_t lineStartPos = 0;
std::vector<Token> tokens;
auto HandleNewLine = [&]
{
currentLine++;
lineStartPos = currentPos + 1;
};
std::shared_ptr<const std::string> currentFile;
if (!filePath.empty())
currentFile = std::make_shared<std::string>(filePath);
@@ -93,7 +99,7 @@ namespace Nz::ShaderLang
char c = Peek(0);
Token token;
token.location.startColumn = SafeCast<UInt32>(currentPos - lastLineFeed) + 1;
token.location.startColumn = SafeCast<UInt32>(currentPos - lineStartPos) + 1;
token.location.startLine = currentLine;
token.location.file = currentFile;
@@ -113,11 +119,8 @@ namespace Nz::ShaderLang
break; //< Ignore blank spaces
case '\n':
{
currentLine++;
lastLineFeed = currentPos;
HandleNewLine();
break;
}
case '-':
{
@@ -170,10 +173,7 @@ namespace Nz::ShaderLang
}
}
else if (next == '\n')
{
currentLine++;
lastLineFeed = currentPos + 1;
}
HandleNewLine();
}
while (next != -1);
}
@@ -225,7 +225,7 @@ namespace Nz::ShaderLang
currentPos++;
}
token.location.endColumn = SafeCast<UInt32>(currentPos - lastLineFeed) + 1;
token.location.endColumn = SafeCast<UInt32>(currentPos - lineStartPos) + 1;
token.location.endLine = currentLine;
if (floatingPoint)
@@ -417,7 +417,7 @@ namespace Nz::ShaderLang
case '\0':
case '\n':
case '\r':
token.location.endColumn = SafeCast<UInt32>(currentPos - lastLineFeed) + 1;
token.location.endColumn = SafeCast<UInt32>(currentPos - lineStartPos) + 1;
token.location.endLine = currentLine;
throw LexerUnfinishedStringError{ token.location };
@@ -433,7 +433,7 @@ namespace Nz::ShaderLang
case '"': character = '"'; break;
case '\\': character = '\\'; break;
default:
token.location.endColumn = SafeCast<UInt32>(currentPos - lastLineFeed) + 1;
token.location.endColumn = SafeCast<UInt32>(currentPos - lineStartPos) + 1;
token.location.endLine = currentLine;
throw LexerUnrecognizedCharError{ token.location };
}
@@ -476,7 +476,7 @@ namespace Nz::ShaderLang
}
else
{
token.location.endColumn = SafeCast<UInt32>(currentPos - lastLineFeed) + 1;
token.location.endColumn = SafeCast<UInt32>(currentPos - lineStartPos) + 1;
token.location.endLine = currentLine;
throw LexerUnrecognizedTokenError{ token.location };
}
@@ -485,7 +485,7 @@ namespace Nz::ShaderLang
if (tokenType)
{
token.location.endColumn = SafeCast<UInt32>(currentPos - lastLineFeed) + 1;
token.location.endColumn = SafeCast<UInt32>(currentPos - lineStartPos) + 1;
token.location.endLine = currentLine;
token.type = *tokenType;

View File

@@ -1195,8 +1195,10 @@ namespace Nz::ShaderLang
// Function call
SourceLocation closingLocation;
auto parameters = ParseExpressionList(TokenType::ClosingParenthesis, &closingLocation);
const SourceLocation& lhsLoc = lhs->sourceLocation;
lhs = ShaderBuilder::CallFunction(std::move(lhs), std::move(parameters));
lhs->sourceLocation = SourceLocation::BuildFromTo(token.location, closingLocation);
lhs->sourceLocation = SourceLocation::BuildFromTo(lhsLoc, closingLocation);
continue;
}
@@ -1208,8 +1210,9 @@ namespace Nz::ShaderLang
SourceLocation closingLocation;
auto parameters = ParseExpressionList(TokenType::ClosingSquareBracket, &closingLocation);
const SourceLocation& lhsLoc = lhs->sourceLocation;
lhs = ShaderBuilder::AccessIndex(std::move(lhs), std::move(parameters));
lhs->sourceLocation = SourceLocation::BuildFromTo(token.location, closingLocation);
lhs->sourceLocation = SourceLocation::BuildFromTo(lhsLoc, closingLocation);
continue;
}