Shader: Merge AstScopedVisitor, AstValidator and TransformVisitor to SanitizeVisitor
This commit is contained in:
112
include/Nazara/Shader/Ast/SanitizeVisitor.hpp
Normal file
112
include/Nazara/Shader/Ast/SanitizeVisitor.hpp
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Shader generator"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
|
||||
#define NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderAstCloner.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API SanitizeVisitor final : AstCloner
|
||||
{
|
||||
public:
|
||||
inline SanitizeVisitor();
|
||||
SanitizeVisitor(const SanitizeVisitor&) = delete;
|
||||
SanitizeVisitor(SanitizeVisitor&&) = delete;
|
||||
~SanitizeVisitor() = default;
|
||||
|
||||
StatementPtr Sanitize(StatementPtr& statement, std::string* error = nullptr);
|
||||
|
||||
SanitizeVisitor& operator=(const SanitizeVisitor&) = delete;
|
||||
SanitizeVisitor& operator=(SanitizeVisitor&&) = delete;
|
||||
|
||||
private:
|
||||
struct Identifier;
|
||||
|
||||
const ExpressionType& CheckField(const ExpressionType& structType, const std::string* memberIdentifier, std::size_t remainingMembers, std::size_t* structIndices);
|
||||
|
||||
using AstCloner::CloneExpression;
|
||||
|
||||
ExpressionPtr Clone(AccessMemberIdentifierExpression& node) override;
|
||||
ExpressionPtr Clone(AssignExpression& node) override;
|
||||
ExpressionPtr Clone(BinaryExpression& node) override;
|
||||
ExpressionPtr Clone(CastExpression& node) override;
|
||||
ExpressionPtr Clone(ConditionalExpression& node) override;
|
||||
ExpressionPtr Clone(ConstantExpression& node) override;
|
||||
ExpressionPtr Clone(IdentifierExpression& node) override;
|
||||
ExpressionPtr Clone(IntrinsicExpression& node) override;
|
||||
ExpressionPtr Clone(SwizzleExpression& node) override;
|
||||
|
||||
StatementPtr Clone(BranchStatement& node) override;
|
||||
StatementPtr Clone(ConditionalStatement& node) override;
|
||||
StatementPtr Clone(DeclareExternalStatement& node) override;
|
||||
StatementPtr Clone(DeclareFunctionStatement& node) override;
|
||||
StatementPtr Clone(DeclareStructStatement& node) override;
|
||||
StatementPtr Clone(DeclareVariableStatement& node) override;
|
||||
StatementPtr Clone(ExpressionStatement& node) override;
|
||||
StatementPtr Clone(MultiStatement& node) override;
|
||||
|
||||
inline const Identifier* FindIdentifier(const std::string_view& identifierName) const;
|
||||
|
||||
Expression& MandatoryExpr(ExpressionPtr& node);
|
||||
Statement& MandatoryStatement(StatementPtr& node);
|
||||
void TypeMustMatch(ExpressionPtr& left, ExpressionPtr& right);
|
||||
void TypeMustMatch(const ExpressionType& left, const ExpressionType& right);
|
||||
|
||||
void PushScope();
|
||||
void PopScope();
|
||||
|
||||
inline std::size_t RegisterFunction(std::string name);
|
||||
inline std::size_t RegisterStruct(std::string name, StructDescription description);
|
||||
inline std::size_t RegisterVariable(std::string name, ExpressionType type);
|
||||
|
||||
std::size_t ResolveStruct(const ExpressionType& exprType);
|
||||
std::size_t ResolveStruct(const IdentifierType& identifierType);
|
||||
std::size_t ResolveStruct(const StructType& structType);
|
||||
std::size_t ResolveStruct(const UniformType& uniformType);
|
||||
ExpressionType ResolveType(const ExpressionType& exprType);
|
||||
|
||||
struct Alias
|
||||
{
|
||||
std::variant<ExpressionType> value;
|
||||
};
|
||||
|
||||
struct Struct
|
||||
{
|
||||
std::size_t structIndex;
|
||||
};
|
||||
|
||||
struct Variable
|
||||
{
|
||||
std::size_t varIndex;
|
||||
};
|
||||
|
||||
struct Identifier
|
||||
{
|
||||
std::string name;
|
||||
std::variant<Alias, Struct, Variable> value;
|
||||
};
|
||||
|
||||
std::size_t m_nextFuncIndex;
|
||||
std::vector<Identifier> m_identifiersInScope;
|
||||
std::vector<StructDescription> m_structs;
|
||||
std::vector<ExpressionType> m_variables;
|
||||
std::vector<std::size_t> m_scopeSizes;
|
||||
|
||||
struct Context;
|
||||
Context* m_context;
|
||||
};
|
||||
|
||||
inline StatementPtr Sanitize(StatementPtr& ast, std::string* error = nullptr);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/SanitizeVisitor.inl>
|
||||
|
||||
#endif
|
||||
@@ -2,18 +2,17 @@
|
||||
// This file is part of the "Nazara Engine - Shader generator"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/Ast/TransformVisitor.hpp>
|
||||
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
inline TransformVisitor::TransformVisitor() :
|
||||
m_nextFuncIndex(0),
|
||||
m_nextVarIndex(0)
|
||||
inline SanitizeVisitor::SanitizeVisitor() :
|
||||
m_nextFuncIndex(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline auto TransformVisitor::FindIdentifier(const std::string_view& identifierName) const -> const Identifier*
|
||||
inline auto SanitizeVisitor::FindIdentifier(const std::string_view& identifierName) const -> const Identifier*
|
||||
{
|
||||
auto it = std::find_if(m_identifiersInScope.rbegin(), m_identifiersInScope.rend(), [&](const Identifier& identifier) { return identifier.name == identifierName; });
|
||||
if (it == m_identifiersInScope.rend())
|
||||
@@ -22,14 +21,14 @@ namespace Nz::ShaderAst
|
||||
return &*it;
|
||||
}
|
||||
|
||||
inline std::size_t TransformVisitor::RegisterFunction(std::string name)
|
||||
inline std::size_t SanitizeVisitor::RegisterFunction(std::string name)
|
||||
{
|
||||
std::size_t funcIndex = m_nextFuncIndex++;
|
||||
return funcIndex;
|
||||
}
|
||||
|
||||
|
||||
inline std::size_t TransformVisitor::RegisterStruct(std::string name, StructDescription description)
|
||||
inline std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription description)
|
||||
{
|
||||
std::size_t structIndex = m_structs.size();
|
||||
m_structs.emplace_back(std::move(description));
|
||||
@@ -44,9 +43,10 @@ namespace Nz::ShaderAst
|
||||
return structIndex;
|
||||
}
|
||||
|
||||
inline std::size_t TransformVisitor::RegisterVariable(std::string name)
|
||||
inline std::size_t SanitizeVisitor::RegisterVariable(std::string name, ExpressionType type)
|
||||
{
|
||||
std::size_t varIndex = m_nextVarIndex++;
|
||||
std::size_t varIndex = m_variables.size();
|
||||
m_variables.emplace_back(std::move(type));
|
||||
|
||||
m_identifiersInScope.push_back({
|
||||
std::move(name),
|
||||
@@ -57,6 +57,12 @@ namespace Nz::ShaderAst
|
||||
|
||||
return varIndex;
|
||||
}
|
||||
|
||||
StatementPtr Sanitize(StatementPtr& ast, std::string* error)
|
||||
{
|
||||
SanitizeVisitor sanitizer;
|
||||
return sanitizer.Sanitize(ast, error);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
@@ -1,90 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Shader generator"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
|
||||
#define NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderAstCloner.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API TransformVisitor : AstCloner
|
||||
{
|
||||
public:
|
||||
inline TransformVisitor();
|
||||
TransformVisitor(const TransformVisitor&) = delete;
|
||||
TransformVisitor(TransformVisitor&&) = delete;
|
||||
~TransformVisitor() = default;
|
||||
|
||||
StatementPtr Transform(StatementPtr& statement);
|
||||
|
||||
TransformVisitor& operator=(const TransformVisitor&) = delete;
|
||||
TransformVisitor& operator=(TransformVisitor&&) = delete;
|
||||
|
||||
private:
|
||||
struct Identifier;
|
||||
|
||||
ExpressionPtr Clone(AccessMemberIdentifierExpression& node) override;
|
||||
ExpressionPtr Clone(CastExpression& node) override;
|
||||
ExpressionPtr Clone(IdentifierExpression& node) override;
|
||||
ExpressionPtr CloneExpression(ExpressionPtr& expr) override;
|
||||
|
||||
inline const Identifier* FindIdentifier(const std::string_view& identifierName) const;
|
||||
|
||||
void PushScope();
|
||||
void PopScope();
|
||||
|
||||
inline std::size_t RegisterFunction(std::string name);
|
||||
inline std::size_t RegisterStruct(std::string name, StructDescription description);
|
||||
inline std::size_t RegisterVariable(std::string name);
|
||||
|
||||
ExpressionType ResolveType(const ExpressionType& exprType);
|
||||
|
||||
using AstCloner::Visit;
|
||||
void Visit(BranchStatement& node) override;
|
||||
void Visit(ConditionalStatement& node) override;
|
||||
void Visit(DeclareExternalStatement& node) override;
|
||||
void Visit(DeclareFunctionStatement& node) override;
|
||||
void Visit(DeclareStructStatement& node) override;
|
||||
void Visit(DeclareVariableStatement& node) override;
|
||||
void Visit(MultiStatement& node) override;
|
||||
|
||||
struct Alias
|
||||
{
|
||||
std::variant<ExpressionType> value;
|
||||
};
|
||||
|
||||
struct Struct
|
||||
{
|
||||
std::size_t structIndex;
|
||||
};
|
||||
|
||||
struct Variable
|
||||
{
|
||||
std::size_t varIndex;
|
||||
};
|
||||
|
||||
struct Identifier
|
||||
{
|
||||
std::string name;
|
||||
std::variant<Alias, Struct, Variable> value;
|
||||
};
|
||||
|
||||
private:
|
||||
std::size_t m_nextFuncIndex;
|
||||
std::size_t m_nextVarIndex;
|
||||
std::vector<Identifier> m_identifiersInScope;
|
||||
std::vector<StructDescription> m_structs;
|
||||
std::vector<std::size_t> m_scopeSizes;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/TransformVisitor.inl>
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user