Shader: Add types to error messages (and remove ID)

This commit is contained in:
SirLynix
2022-04-01 13:36:24 +02:00
committed by Jérôme Leclercq
parent 16cf75440b
commit 7c640f5c00
18 changed files with 501 additions and 320 deletions

View File

@@ -61,14 +61,6 @@ namespace Nz::ShaderAst
inline bool operator!=(const FunctionType& rhs) const;
};
struct IdentifierType
{
std::string name;
inline bool operator==(const IdentifierType& rhs) const;
inline bool operator!=(const IdentifierType& rhs) const;
};
struct IntrinsicFunctionType
{
IntrinsicType intrinsic;
@@ -151,7 +143,7 @@ namespace Nz::ShaderAst
inline bool operator!=(const VectorType& rhs) const;
};
using ExpressionType = std::variant<NoType, AliasType, ArrayType, FunctionType, IdentifierType, IntrinsicFunctionType, PrimitiveType, MatrixType, MethodType, SamplerType, StructType, Type, UniformType, VectorType>;
using ExpressionType = std::variant<NoType, AliasType, ArrayType, FunctionType, IntrinsicFunctionType, MatrixType, MethodType, PrimitiveType, SamplerType, StructType, Type, UniformType, VectorType>;
struct ContainedType
{
@@ -178,7 +170,6 @@ namespace Nz::ShaderAst
inline bool IsAliasType(const ExpressionType& type);
inline bool IsArrayType(const ExpressionType& type);
inline bool IsFunctionType(const ExpressionType& type);
inline bool IsIdentifierType(const ExpressionType& type);
inline bool IsIntrinsicFunctionType(const ExpressionType& type);
inline bool IsMatrixType(const ExpressionType& type);
inline bool IsMethodType(const ExpressionType& type);
@@ -189,6 +180,28 @@ namespace Nz::ShaderAst
inline bool IsTypeExpression(const ExpressionType& type);
inline bool IsUniformType(const ExpressionType& type);
inline bool IsVectorType(const ExpressionType& type);
struct Stringifier
{
std::function<std::string(std::size_t aliasIndex)> aliasStringifier;
std::function<std::string(std::size_t structIndex)> structStringifier;
std::function<std::string(std::size_t typeIndex)> typeStringifier;
};
std::string ToString(const AliasType& type, const Stringifier& stringifier = {});
std::string ToString(const ArrayType& type, const Stringifier& stringifier = {});
std::string ToString(const ExpressionType& type, const Stringifier& stringifier = {});
std::string ToString(const FunctionType& type, const Stringifier& stringifier = {});
std::string ToString(const IntrinsicFunctionType& type, const Stringifier& stringifier = {});
std::string ToString(const MatrixType& type, const Stringifier& stringifier = {});
std::string ToString(const MethodType& type, const Stringifier& stringifier = {});
std::string ToString(NoType type, const Stringifier& stringifier = {});
std::string ToString(PrimitiveType type, const Stringifier& stringifier = {});
std::string ToString(const SamplerType& type, const Stringifier& stringifier = {});
std::string ToString(const StructType& type, const Stringifier& stringifier = {});
std::string ToString(const Type& type, const Stringifier& stringifier = {});
std::string ToString(const UniformType& type, const Stringifier& stringifier = {});
std::string ToString(const VectorType& type, const Stringifier& stringifier = {});
}
#include <Nazara/Shader/Ast/ExpressionType.inl>

View File

@@ -30,17 +30,6 @@ namespace Nz::ShaderAst
}
inline bool IdentifierType::operator==(const IdentifierType& rhs) const
{
return name == rhs.name;
}
inline bool IdentifierType::operator!=(const IdentifierType& rhs) const
{
return !operator==(rhs);
}
inline bool IntrinsicFunctionType::operator==(const IntrinsicFunctionType& rhs) const
{
return intrinsic == rhs.intrinsic;
@@ -150,11 +139,6 @@ namespace Nz::ShaderAst
return std::holds_alternative<FunctionType>(type);
}
inline bool IsIdentifierType(const ExpressionType& type)
{
return std::holds_alternative<IdentifierType>(type);
}
inline bool IsIntrinsicFunctionType(const ExpressionType& type)
{
return std::holds_alternative<IntrinsicFunctionType>(type);

View File

@@ -66,6 +66,7 @@ namespace Nz::ShaderAst
struct Identifier;
struct IdentifierData;
template<typename T> struct IdentifierList;
struct NamedPartialType;
struct Scope;
using AstCloner::CloneExpression;
@@ -130,7 +131,7 @@ namespace Nz::ShaderAst
void RegisterBuiltin();
std::size_t RegisterAlias(std::string name, std::optional<IdentifierData> aliasData, std::optional<std::size_t> index, const ShaderLang::SourceLocation& sourceLocation);
std::size_t RegisterAlias(std::string name, std::optional<Identifier> aliasData, std::optional<std::size_t> index, const ShaderLang::SourceLocation& sourceLocation);
std::size_t RegisterConstant(std::string name, std::optional<ConstantValue> value, std::optional<std::size_t> index, const ShaderLang::SourceLocation& sourceLocation);
std::size_t RegisterFunction(std::string name, std::optional<FunctionData> funcData, std::optional<std::size_t> index, const ShaderLang::SourceLocation& sourceLocation);
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type);
@@ -141,11 +142,10 @@ namespace Nz::ShaderAst
void RegisterUnresolved(std::string name);
std::size_t RegisterVariable(std::string name, std::optional<ExpressionType> type, std::optional<std::size_t> index, const ShaderLang::SourceLocation& sourceLocation);
const IdentifierData* ResolveAliasIdentifier(const IdentifierData* identifier, const ShaderLang::SourceLocation& sourceLocation) const;
const Identifier* ResolveAliasIdentifier(const Identifier* identifier, const ShaderLang::SourceLocation& sourceLocation) const;
void ResolveFunctions();
std::size_t ResolveStruct(const AliasType& aliasType, const ShaderLang::SourceLocation& sourceLocation);
std::size_t ResolveStruct(const ExpressionType& exprType, const ShaderLang::SourceLocation& sourceLocation);
std::size_t ResolveStruct(const IdentifierType& identifierType, const ShaderLang::SourceLocation& sourceLocation);
std::size_t ResolveStruct(const StructType& structType, const ShaderLang::SourceLocation& sourceLocation);
std::size_t ResolveStruct(const UniformType& uniformType, const ShaderLang::SourceLocation& sourceLocation);
ExpressionType ResolveType(const ExpressionType& exprType, bool resolveAlias, const ShaderLang::SourceLocation& sourceLocation);
@@ -154,6 +154,11 @@ namespace Nz::ShaderAst
void SanitizeIdentifier(std::string& identifier);
MultiStatementPtr SanitizeInternal(MultiStatement& rootNode, std::string* error);
std::string ToString(const ExpressionType& exprType, const ShaderLang::SourceLocation& sourceLocation) const;
std::string ToString(const NamedPartialType& partialType, const ShaderLang::SourceLocation& sourceLocation) const;
template<typename... Args> std::string ToString(const std::variant<Args...>& value, const ShaderLang::SourceLocation& sourceLocation) const;
void TypeMustMatch(const ExpressionType& left, const ExpressionType& right, const ShaderLang::SourceLocation& sourceLocation) const;
ValidationResult TypeMustMatch(const ExpressionPtr& left, const ExpressionPtr& right, const ShaderLang::SourceLocation& sourceLocation);
ValidationResult Validate(DeclareAliasStatement& node);
@@ -174,13 +179,11 @@ namespace Nz::ShaderAst
template<std::size_t N> ValidationResult ValidateIntrinsicParamCount(IntrinsicExpression& node);
ValidationResult ValidateIntrinsicParamMatchingType(IntrinsicExpression& node);
template<std::size_t N, typename F> ValidationResult ValidateIntrinsicParameter(IntrinsicExpression& node, F&& func);
template<std::size_t N, typename F> ValidationResult ValidateIntrinsicParameterType(IntrinsicExpression& node, F&& func);
template<std::size_t N, typename F> ValidationResult ValidateIntrinsicParameterType(IntrinsicExpression& node, F&& func, const char* typeStr);
static Expression& MandatoryExpr(const ExpressionPtr& node, const ShaderLang::SourceLocation& sourceLocation);
static Statement& MandatoryStatement(const StatementPtr& node, const ShaderLang::SourceLocation& sourceLocation);
static void TypeMustMatch(const ExpressionType& left, const ExpressionType& right, const ShaderLang::SourceLocation& sourceLocation);
static StatementPtr Unscope(StatementPtr node);
static UInt32 ToSwizzleIndex(char c, const ShaderLang::SourceLocation& sourceLocation);
@@ -220,7 +223,7 @@ namespace Nz::ShaderAst
struct Identifier
{
std::string name;
IdentifierData data;
IdentifierData target;
};
struct Context;