Shader: Add SourceLocation members

TODO: Fill from Parser and use them for error throwing in SanitizeVisitor
This commit is contained in:
SirLynix
2022-03-27 14:06:58 +02:00
committed by Jérôme Leclercq
parent b8bf19f8cd
commit 960ab64d98
15 changed files with 329 additions and 137 deletions

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <vector>
@@ -23,12 +24,15 @@ namespace Nz::ShaderAst
template<typename T> bool Compare(const T& lhs, const T& rhs);
template<typename T, std::size_t S> bool Compare(const std::array<T, S>& lhs, const std::array<T, S>& rhs);
template<typename T> bool Compare(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs);
template<typename T> bool Compare(const std::vector<T>& lhs, const std::vector<T>& rhs);
template<typename T> bool Compare(const std::unique_ptr<T>& lhs, const std::unique_ptr<T>& rhs);
template<typename T> bool Compare(const ExpressionValue<T>& lhs, const ExpressionValue<T>& rhs);
inline bool Compare(const AccessIdentifierExpression::Identifier& lhs, const AccessIdentifierExpression::Identifier& rhs);
inline bool Compare(const BranchStatement::ConditionalStatement& lhs, const BranchStatement::ConditionalStatement& rhs);
inline bool Compare(const DeclareExternalStatement::ExternalVar& lhs, const DeclareExternalStatement::ExternalVar& rhs);
inline bool Compare(const DeclareFunctionStatement::Parameter& lhs, const DeclareFunctionStatement::Parameter& rhs);
inline bool Compare(const ShaderLang::SourceLocation& lhs, const ShaderLang::SourceLocation& rhs);
inline bool Compare(const StructDescription& lhs, const StructDescription& rhs);
inline bool Compare(const StructDescription::StructMember& lhs, const StructDescription::StructMember& rhs);

View File

@@ -13,6 +13,9 @@ namespace Nz::ShaderAst
if (lhs.GetType() != rhs.GetType())
return false;
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
return false;
switch (lhs.GetType())
{
case NodeType::None: break;
@@ -70,6 +73,9 @@ namespace Nz::ShaderAst
if (lhs.GetType() != rhs.GetType())
return false;
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
return false;
switch (lhs.GetType())
{
case NodeType::None: break;
@@ -101,6 +107,17 @@ namespace Nz::ShaderAst
return true;
}
template<typename T>
bool Compare(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs)
{
if (lhs == nullptr)
return rhs == nullptr;
else if (rhs == nullptr)
return false;
return Compare(*lhs, *rhs);
}
template<typename T>
bool Compare(const std::vector<T>& lhs, const std::vector<T>& rhs)
{
@@ -153,6 +170,17 @@ namespace Nz::ShaderAst
return true;
}
bool Compare(const AccessIdentifierExpression::Identifier& lhs, const AccessIdentifierExpression::Identifier& rhs)
{
if (!Compare(lhs.identifier, rhs.identifier))
return false;
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
return false;
return true;
}
inline bool Compare(const BranchStatement::ConditionalStatement& lhs, const BranchStatement::ConditionalStatement& rhs)
{
if (!Compare(lhs.condition, rhs.condition))
@@ -178,6 +206,9 @@ namespace Nz::ShaderAst
if (!Compare(lhs.type, rhs.type))
return false;
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
return false;
return true;
}
@@ -189,6 +220,29 @@ namespace Nz::ShaderAst
if (!Compare(lhs.type, rhs.type))
return false;
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
return false;
return true;
}
bool Compare(const ShaderLang::SourceLocation& lhs, const ShaderLang::SourceLocation& rhs)
{
if (!Compare(lhs.endColumn, rhs.endColumn))
return false;
if (!Compare(lhs.endLine, rhs.endLine))
return false;
if (!Compare(lhs.startColumn, rhs.startColumn))
return false;
if (!Compare(lhs.startLine, rhs.startLine))
return false;
if (!Compare(lhs.file, rhs.file))
return false;
return true;
}
@@ -223,6 +277,9 @@ namespace Nz::ShaderAst
if (!Compare(lhs.type, rhs.type))
return false;
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
return false;
return true;
}

View File

@@ -11,6 +11,7 @@
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/ByteStream.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
namespace Nz::ShaderAst
@@ -43,7 +44,6 @@ namespace Nz::ShaderAst
void Serialize(TypeExpression& node);
void Serialize(VariableValueExpression& node);
void Serialize(UnaryExpression& node);
void SerializeExpressionCommon(Expression& expr);
void Serialize(BranchStatement& node);
void Serialize(ConditionalStatement& node);
@@ -65,6 +65,9 @@ namespace Nz::ShaderAst
void Serialize(ScopedStatement& node);
void Serialize(WhileStatement& node);
void SerializeExpressionCommon(Expression& expr);
void SerializeNodeCommon(ShaderAst::Node& node);
protected:
template<typename T> void Container(T& container);
template<typename T> void Enum(T& enumVal);
@@ -75,11 +78,14 @@ namespace Nz::ShaderAst
virtual bool IsWriting() const = 0;
virtual void SerializeModule(ModulePtr& module) = 0;
virtual void Node(ExpressionPtr& node) = 0;
virtual void Node(StatementPtr& node) = 0;
virtual void SerializeModule(ModulePtr& module) = 0;
virtual void SharedString(std::shared_ptr<const std::string>& val) = 0;
inline void SourceLoc(ShaderLang::SourceLocation& sourceLoc);
virtual void Type(ExpressionType& type) = 0;
virtual void Value(bool& val) = 0;
@@ -110,11 +116,11 @@ namespace Nz::ShaderAst
private:
using AstSerializerBase::Serialize;
void SerializeModule(ModulePtr& module) override;
bool IsWriting() const override;
void Node(ExpressionPtr& node) override;
void Node(StatementPtr& node) override;
void SerializeModule(ModulePtr& module) override;
void SharedString(std::shared_ptr<const std::string>& val) override;
void Type(ExpressionType& type) override;
void Value(bool& val) override;
void Value(float& val) override;
@@ -131,6 +137,7 @@ namespace Nz::ShaderAst
void Value(UInt32& val) override;
void Value(UInt64& val) override;
std::unordered_map<std::string, UInt32> m_stringIndices;
ByteStream& m_stream;
};
@@ -145,11 +152,11 @@ namespace Nz::ShaderAst
private:
using AstSerializerBase::Serialize;
void SerializeModule(ModulePtr& module) override;
bool IsWriting() const override;
void Node(ExpressionPtr& node) override;
void Node(StatementPtr& node) override;
void SerializeModule(ModulePtr& module) override;
void SharedString(std::shared_ptr<const std::string>& val) override;
void Type(ExpressionType& type) override;
void Value(bool& val) override;
void Value(float& val) override;
@@ -166,6 +173,7 @@ namespace Nz::ShaderAst
void Value(UInt32& val) override;
void Value(UInt64& val) override;
std::vector<std::shared_ptr<const std::string>> m_strings;
ByteStream& m_stream;
};

View File

@@ -166,6 +166,15 @@ namespace Nz::ShaderAst
}
}
inline void AstSerializerBase::SourceLoc(ShaderLang::SourceLocation& sourceLoc)
{
SharedString(sourceLoc.file);
Value(sourceLoc.endColumn);
Value(sourceLoc.endLine);
Value(sourceLoc.startColumn);
Value(sourceLoc.startLine);
}
inline void AstSerializerBase::SizeT(std::size_t& val)
{
bool isWriting = IsWriting();

View File

@@ -8,6 +8,7 @@
#define NAZARA_SHADER_AST_EXPRESSIONTYPE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <Nazara/Shader/Ast/Enums.hpp>
#include <Nazara/Utility/Enums.hpp>
@@ -166,6 +167,7 @@ namespace Nz::ShaderAst
ExpressionValue<UInt32> locationIndex;
ExpressionValue<ExpressionType> type;
std::string name;
ShaderLang::SourceLocation sourceLocation;
};
ExpressionValue<StructLayout> layout;

View File

@@ -12,6 +12,7 @@
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <Nazara/Shader/Ast/ConstantValue.hpp>
#include <Nazara/Shader/Ast/Enums.hpp>
@@ -41,6 +42,8 @@ namespace Nz::ShaderAst
Node& operator=(const Node&) = delete;
Node& operator=(Node&&) noexcept = default;
ShaderLang::SourceLocation sourceLocation;
};
// Expressions
@@ -69,7 +72,13 @@ namespace Nz::ShaderAst
NodeType GetType() const override;
void Visit(AstExpressionVisitor& visitor) override;
std::vector<std::string> identifiers;
struct Identifier
{
std::string identifier;
ShaderLang::SourceLocation sourceLocation;
};
std::vector<Identifier> identifiers;
ExpressionPtr expr;
};
@@ -317,6 +326,7 @@ namespace Nz::ShaderAst
ExpressionValue<UInt32> bindingIndex;
ExpressionValue<UInt32> bindingSet;
ExpressionValue<ExpressionType> type;
ShaderLang::SourceLocation sourceLocation;
};
std::vector<ExternalVar> externalVars;
@@ -330,9 +340,10 @@ namespace Nz::ShaderAst
struct Parameter
{
ExpressionValue<ExpressionType> type;
std::optional<std::size_t> varIndex;
std::string name;
ExpressionValue<ExpressionType> type;
ShaderLang::SourceLocation sourceLocation;
};
std::optional<std::size_t> funcIndex;

View File

@@ -12,7 +12,12 @@ namespace Nz::ShaderBuilder
{
auto accessMemberNode = std::make_unique<ShaderAst::AccessIdentifierExpression>();
accessMemberNode->expr = std::move(expr);
accessMemberNode->identifiers = std::move(memberIdentifiers);
accessMemberNode->identifiers.reserve(memberIdentifiers.size());
for (std::string& identifier : memberIdentifiers)
{
auto& identifierEntry = accessMemberNode->identifiers.emplace_back();
identifierEntry.identifier = std::move(identifier);
}
return accessMemberNode;
}

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderLangParser.hpp>
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
#include <Nazara/Shader/Ast/Enums.hpp>
#include <exception>
#include <memory>
@@ -18,22 +19,6 @@
namespace Nz::ShaderLang
{
struct SourceLocation
{
inline SourceLocation();
inline SourceLocation(unsigned int line, unsigned int column, std::shared_ptr<const std::string> file);
inline SourceLocation(unsigned int line, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
inline SourceLocation(unsigned int startLine, unsigned int endLine, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
inline bool IsValid() const;
std::shared_ptr<const std::string> file; //< Since the same file will be used for every node, prevent holding X time the same path
unsigned int endColumn;
unsigned int endLine;
unsigned int startColumn;
unsigned int startLine;
};
enum class ErrorCategory
{
Ast,

View File

@@ -7,46 +7,6 @@
namespace Nz::ShaderLang
{
inline SourceLocation::SourceLocation() :
endColumn(0),
endLine(0),
startColumn(0),
startLine(0)
{
}
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int Column, std::shared_ptr<const std::string> File) :
file(std::move(File)),
endColumn(Column),
endLine(Line),
startColumn(Column),
startLine(Line)
{
}
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
file(std::move(File)),
endColumn(EndColumn),
endLine(Line),
startColumn(StartColumn),
startLine(Line)
{
}
inline SourceLocation::SourceLocation(unsigned int StartLine, unsigned int EndLine, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
file(std::move(File)),
endColumn(EndColumn),
endLine(EndLine),
startColumn(StartColumn),
startLine(StartLine)
{
}
inline bool SourceLocation::IsValid() const
{
return startLine != 0 && endLine != 0 && endColumn != 0 && startColumn != 0;
}
inline Error::Error(SourceLocation sourceLocation, ErrorCategory errorCategory, unsigned int errorType) noexcept :
m_errorCategory(errorCategory),
m_sourceLocation(std::move(sourceLocation)),

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Shader module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADER_SHADERLANGSOURCELOCATION_HPP
#define NAZARA_SHADER_SHADERLANGSOURCELOCATION_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <memory>
#include <string>
namespace Nz::ShaderLang
{
struct SourceLocation
{
inline SourceLocation();
inline SourceLocation(unsigned int line, unsigned int column, std::shared_ptr<const std::string> file);
inline SourceLocation(unsigned int line, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
inline SourceLocation(unsigned int startLine, unsigned int endLine, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
inline bool IsValid() const;
std::shared_ptr<const std::string> file; //< Since the same file will be used for every node, prevent holding X time the same path
UInt32 endColumn;
UInt32 endLine;
UInt32 startColumn;
UInt32 startLine;
};
}
#include <Nazara/Shader/ShaderLangSourceLocation.inl>
#endif // NAZARA_SHADER_SHADERLANGSOURCELOCATION_HPP

View File

@@ -0,0 +1,51 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Shader module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderLang
{
inline SourceLocation::SourceLocation() :
endColumn(0),
endLine(0),
startColumn(0),
startLine(0)
{
}
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int Column, std::shared_ptr<const std::string> File) :
file(std::move(File)),
endColumn(Column),
endLine(Line),
startColumn(Column),
startLine(Line)
{
}
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
file(std::move(File)),
endColumn(EndColumn),
endLine(Line),
startColumn(StartColumn),
startLine(Line)
{
}
inline SourceLocation::SourceLocation(unsigned int StartLine, unsigned int EndLine, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
file(std::move(File)),
endColumn(EndColumn),
endLine(EndLine),
startColumn(StartColumn),
startLine(StartLine)
{
}
inline bool SourceLocation::IsValid() const
{
return startLine != 0 && endLine != 0 && endColumn != 0 && startColumn != 0;
}
}
#include <Nazara/Shader/DebugOff.hpp>