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

@ -36,7 +36,7 @@ namespace Nz::ShaderLang
#include <Nazara/Shader/ShaderLangErrorList.hpp> #include <Nazara/Shader/ShaderLangErrorList.hpp>
}; };
class Error : public std::exception class NAZARA_SHADER_API Error : public std::exception
{ {
public: public:
inline Error(SourceLocation sourceLocation, ErrorCategory errorCategory, ErrorType errorType) noexcept; inline Error(SourceLocation sourceLocation, ErrorCategory errorCategory, ErrorType errorType) noexcept;

View File

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

View File

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

View File

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

View File

@ -184,6 +184,7 @@ if is_plat("windows") then
add_cxxflags("/bigobj", "/permissive-", "/Zc:__cplusplus", "/Zc:externConstexpr", "/Zc:inline", "/Zc:lambda", "/Zc:preprocessor", "/Zc:referenceBinding", "/Zc:strictStrings", "/Zc:throwingNew") add_cxxflags("/bigobj", "/permissive-", "/Zc:__cplusplus", "/Zc:externConstexpr", "/Zc:inline", "/Zc:lambda", "/Zc:preprocessor", "/Zc:referenceBinding", "/Zc:strictStrings", "/Zc:throwingNew")
add_cxflags("/w44062") -- Enable warning: switch case not handled add_cxflags("/w44062") -- Enable warning: switch case not handled
add_cxflags("/wd4251") -- Disable warning: class needs to have dll-interface to be used by clients of class blah blah blah add_cxflags("/wd4251") -- Disable warning: class needs to have dll-interface to be used by clients of class blah blah blah
add_cxflags("/wd4275") -- Disable warning: DLL-interface class 'class_1' used as base for DLL-interface blah
elseif is_plat("mingw") then elseif is_plat("mingw") then
add_cxflags("-Og", "-Wa,-mbig-obj") add_cxflags("-Og", "-Wa,-mbig-obj")
add_ldflags("-Wa,-mbig-obj") add_ldflags("-Wa,-mbig-obj")