Skip to content

Commit

Permalink
Add support for whole module import
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 30, 2024
1 parent 1559d89 commit e2c652d
Show file tree
Hide file tree
Showing 28 changed files with 886 additions and 211 deletions.
2 changes: 2 additions & 0 deletions include/NZSL/Ast/AstSerializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace nzsl::Ast
void Serialize(IdentifierExpression& node);
void Serialize(IntrinsicExpression& node);
void Serialize(IntrinsicFunctionExpression& node);
void Serialize(ModuleExpression& node);
void Serialize(NamedExternalBlockExpression& node);
void Serialize(StructTypeExpression& node);
void Serialize(SwizzleExpression& node);
void Serialize(TypeExpression& node);
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/Ast/Cloner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace nzsl::Ast
virtual ExpressionPtr Clone(IdentifierExpression& node);
virtual ExpressionPtr Clone(IntrinsicExpression& node);
virtual ExpressionPtr Clone(IntrinsicFunctionExpression& node);
virtual ExpressionPtr Clone(ModuleExpression& node);
virtual ExpressionPtr Clone(NamedExternalBlockExpression& node);
virtual ExpressionPtr Clone(StructTypeExpression& node);
virtual ExpressionPtr Clone(SwizzleExpression& node);
virtual ExpressionPtr Clone(TypeExpression& node);
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/Ast/Compare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace nzsl::Ast
inline bool Compare(const IdentifierExpression& lhs, const IdentifierExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const IntrinsicExpression& lhs, const IntrinsicExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const IntrinsicFunctionExpression& lhs, const IntrinsicFunctionExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const ModuleExpression& lhs, const ModuleExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const NamedExternalBlockExpression& lhs, const NamedExternalBlockExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const StructTypeExpression& lhs, const StructTypeExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const SwizzleExpression& lhs, const SwizzleExpression& rhs, const ComparisonParams& params = {});
inline bool Compare(const TypeExpression& lhs, const TypeExpression& rhs, const ComparisonParams& params = {});
Expand Down
21 changes: 20 additions & 1 deletion include/NZSL/Ast/Compare.inl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ namespace nzsl::Ast

if (!Compare(lhs.renamedIdentifierLoc, rhs.renamedIdentifierLoc, params))
return false;

return true;
}

Expand Down Expand Up @@ -503,6 +503,22 @@ namespace nzsl::Ast
return true;
}

inline bool Compare(const ModuleExpression& lhs, const ModuleExpression& rhs, const ComparisonParams& params)
{
if (!Compare(lhs.moduleId, rhs.moduleId, params))
return false;

return true;
}

inline bool Compare(const NamedExternalBlockExpression& lhs, const NamedExternalBlockExpression& rhs, const ComparisonParams& params)
{
if (!Compare(lhs.externalBlockId, rhs.externalBlockId, params))
return false;

return true;
}

inline bool Compare(const StructTypeExpression& lhs, const StructTypeExpression& rhs, const ComparisonParams& params)
{
if (!Compare(lhs.structTypeId, rhs.structTypeId, params))
Expand Down Expand Up @@ -758,6 +774,9 @@ namespace nzsl::Ast
if (params.compareModuleName && !Compare(lhs.moduleName, rhs.moduleName, params))
return false;

if (!Compare(lhs.moduleIdentifier, rhs.moduleIdentifier, params))
return false;

if (!Compare(lhs.identifiers, rhs.identifiers, params))
return false;

Expand Down
4 changes: 3 additions & 1 deletion include/NZSL/Ast/Enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ namespace nzsl::Ast
IdentifierExpression = 13,
IntrinsicExpression = 14,
IntrinsicFunctionExpression = 15,
ModuleExpression = 42,
NamedExternalBlockExpression = 43,
StructTypeExpression = 16,
SwizzleExpression = 17,
TypeExpression = 18,
Expand Down Expand Up @@ -245,7 +247,7 @@ namespace nzsl::Ast
ScopedStatement = 38,
WhileStatement = 39,

Max = ContinueStatement
Max = NamedExternalBlockExpression
};

enum class PrimitiveType
Expand Down
35 changes: 26 additions & 9 deletions include/NZSL/Ast/ExpressionType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
#include <NZSL/Ast/Enums.hpp>
#include <NZSL/Ast/ExpressionValue.hpp>
#include <NZSL/Lang/SourceLocation.hpp>
#include <optional>
#include <string>
#include <variant>
#include <vector>

#ifdef NAZARA_COMPILER_GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
NAZARA_WARNING_PUSH()
NAZARA_WARNING_GCC_DISABLE("-Wmaybe-uninitialized")

namespace nzsl
{
Expand Down Expand Up @@ -137,6 +134,22 @@ namespace nzsl::Ast
inline bool operator!=(const MethodType& rhs) const;
};

struct ModuleType
{
std::size_t moduleIndex;

inline bool operator==(const ModuleType& rhs) const;
inline bool operator!=(const ModuleType& rhs) const;
};

struct NamedExternalBlockType
{
std::size_t namedExternalBlockIndex;

inline bool operator==(const NamedExternalBlockType& rhs) const;
inline bool operator!=(const NamedExternalBlockType& rhs) const;
};

struct NoType
{
inline bool operator==(const NoType& rhs) const;
Expand Down Expand Up @@ -216,7 +229,7 @@ namespace nzsl::Ast
inline bool operator!=(const PushConstantType& rhs) const;
};

using ExpressionType = std::variant<NoType, AliasType, ArrayType, DynArrayType, FunctionType, IntrinsicFunctionType, MatrixType, MethodType, PrimitiveType, PushConstantType, SamplerType, StorageType, StructType, TextureType, Type, UniformType, VectorType>;
using ExpressionType = std::variant<NoType, AliasType, ArrayType, DynArrayType, FunctionType, IntrinsicFunctionType, MatrixType, MethodType, ModuleType, NamedExternalBlockType, PrimitiveType, PushConstantType, SamplerType, StorageType, StructType, TextureType, Type, UniformType, VectorType>;

struct ContainedType
{
Expand Down Expand Up @@ -252,6 +265,8 @@ namespace nzsl::Ast
inline bool IsIntrinsicFunctionType(const ExpressionType& type);
inline bool IsMatrixType(const ExpressionType& type);
inline bool IsMethodType(const ExpressionType& type);
inline bool IsModuleType(const ExpressionType& type);
inline bool IsNamedExternalBlockType(const ExpressionType& type);
inline bool IsNoType(const ExpressionType& type);
inline bool IsPrimitiveType(const ExpressionType& type);
inline bool IsPushConstantType(const ExpressionType& type);
Expand Down Expand Up @@ -296,6 +311,8 @@ namespace nzsl::Ast
struct Stringifier
{
std::function<std::string(std::size_t aliasIndex)> aliasStringifier;
std::function<std::string(std::size_t moduleIndex)> moduleStringifier;
std::function<std::string(std::size_t moduleIndex)> namedExternalBlockStringifier;
std::function<std::string(std::size_t structIndex)> structStringifier;
std::function<std::string(std::size_t typeIndex)> typeStringifier;
};
Expand All @@ -308,6 +325,8 @@ namespace nzsl::Ast
NZSL_API std::string ToString(const IntrinsicFunctionType& type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(const MatrixType& type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(const MethodType& type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(const ModuleType& type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(const NamedExternalBlockType& type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(NoType type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(PrimitiveType type, const Stringifier& stringifier = {});
NZSL_API std::string ToString(const PushConstantType& type, const Stringifier& stringifier = {});
Expand All @@ -320,9 +339,7 @@ namespace nzsl::Ast
NZSL_API std::string ToString(const VectorType& type, const Stringifier& stringifier = {});
}

#ifdef NAZARA_COMPILER_GCC
#pragma GCC diagnostic pop
#endif
NAZARA_WARNING_POP()

#include <NZSL/Ast/ExpressionType.inl>

Expand Down
50 changes: 50 additions & 0 deletions include/NZSL/Ast/ExpressionType.inl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ namespace nzsl::Ast
}


inline bool NamedExternalBlockType::operator==(const NamedExternalBlockType& rhs) const
{
return namedExternalBlockIndex == rhs.namedExternalBlockIndex;
}

inline bool NamedExternalBlockType::operator!=(const NamedExternalBlockType& rhs) const
{
return !operator==(rhs);
}


inline bool FunctionType::operator==(const FunctionType& rhs) const
{
return funcIndex == rhs.funcIndex;
Expand Down Expand Up @@ -83,6 +94,17 @@ namespace nzsl::Ast
}


inline bool ModuleType::operator==(const ModuleType& rhs) const
{
return moduleIndex == rhs.moduleIndex;
}

inline bool ModuleType::operator!=(const ModuleType& rhs) const
{
return !operator==(rhs);
}


inline bool NoType::operator==(const NoType& /*rhs*/) const
{
return true;
Expand Down Expand Up @@ -216,6 +238,16 @@ namespace nzsl::Ast
return std::holds_alternative<MethodType>(type);
}

inline bool IsModuleType(const ExpressionType& type)
{
return std::holds_alternative<ModuleType>(type);
}

inline bool IsNamedExternalBlockType(const ExpressionType& type)
{
return std::holds_alternative<NamedExternalBlockType>(type);
}

inline bool IsNoType(const ExpressionType& type)
{
return std::holds_alternative<NoType>(type);
Expand Down Expand Up @@ -388,6 +420,24 @@ namespace std
}
};

template<>
struct hash<nzsl::Ast::ModuleType>
{
std::size_t operator()(const nzsl::Ast::ModuleType& moduleType) const
{
return Nz::HashCombine(moduleType.moduleIndex);
}
};

template<>
struct hash<nzsl::Ast::NamedExternalBlockType>
{
std::size_t operator()(const nzsl::Ast::NamedExternalBlockType& namedExternalBlockType) const
{
return Nz::HashCombine(namedExternalBlockType.namedExternalBlockIndex);
}
};

template<>
struct hash<nzsl::Ast::PushConstantType>
{
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/Ast/NodeList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ NZSL_SHADERAST_EXPRESSION(Function)
NZSL_SHADERAST_EXPRESSION(Identifier)
NZSL_SHADERAST_EXPRESSION(Intrinsic)
NZSL_SHADERAST_EXPRESSION(IntrinsicFunction)
NZSL_SHADERAST_EXPRESSION(Module)
NZSL_SHADERAST_EXPRESSION(NamedExternalBlock)
NZSL_SHADERAST_EXPRESSION(StructType)
NZSL_SHADERAST_EXPRESSION(Swizzle)
NZSL_SHADERAST_EXPRESSION(Type)
Expand Down
17 changes: 17 additions & 0 deletions include/NZSL/Ast/Nodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ namespace nzsl::Ast
std::size_t intrinsicId;
};

struct NZSL_API ModuleExpression : Expression
{
NodeType GetType() const override;
void Visit(ExpressionVisitor& visitor) override;

std::size_t moduleId;
};

struct NZSL_API NamedExternalBlockExpression : Expression
{
NodeType GetType() const override;
void Visit(ExpressionVisitor& visitor) override;

std::size_t externalBlockId;
};

struct NZSL_API StructTypeExpression : Expression
{
NodeType GetType() const override;
Expand Down Expand Up @@ -478,6 +494,7 @@ namespace nzsl::Ast
SourceLocation renamedIdentifierLoc;
};

std::string moduleIdentifier;
std::string moduleName;
std::vector<Identifier> identifiers;
};
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/Ast/RecursiveVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace nzsl::Ast
void Visit(IdentifierExpression& node) override;
void Visit(IntrinsicExpression& node) override;
void Visit(IntrinsicFunctionExpression& node) override;
void Visit(ModuleExpression& node) override;
void Visit(NamedExternalBlockExpression& node) override;
void Visit(StructTypeExpression& node) override;
void Visit(SwizzleExpression& node) override;
void Visit(TypeExpression& node) override;
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/Ast/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace nzsl::Ast
void Visit(IdentifierExpression& node) override;
void Visit(IntrinsicExpression& node) override;
void Visit(IntrinsicFunctionExpression& node) override;
void Visit(ModuleExpression& node) override;
void Visit(NamedExternalBlockExpression& node) override;
void Visit(StructTypeExpression& node) override;
void Visit(SwizzleExpression& node) override;
void Visit(TypeExpression& node) override;
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/GlslWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ namespace nzsl
void Append(const Ast::IntrinsicFunctionType& intrinsicFunctionType);
void Append(const Ast::MatrixType& matrixType);
void Append(const Ast::MethodType& methodType);
void Append(const Ast::ModuleType& methodType);
void Append(Ast::MemoryLayout layout);
void Append(const Ast::NamedExternalBlockType& namedExternalBlockType);
void Append(Ast::NoType);
void Append(Ast::PrimitiveType type);
void Append(const Ast::PushConstantType& pushConstantType);
Expand Down
2 changes: 2 additions & 0 deletions include/NZSL/Lang/ErrorList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ NZSL_SHADERLANG_PARSER_ERROR(AttributeUnexpectedParameterCount, "attribute {} ex
NZSL_SHADERLANG_PARSER_ERROR(ExpectedToken, "expected token {}, got {}", TokenType, TokenType)
NZSL_SHADERLANG_PARSER_ERROR(DuplicateIdentifier, "duplicate identifier")
NZSL_SHADERLANG_PARSER_ERROR(DuplicateModule, "duplicate module")
NZSL_SHADERLANG_PARSER_ERROR(ModuleImportInvalidIdentifier, "{} is not a valid identifier to import", std::string)
NZSL_SHADERLANG_PARSER_ERROR(ModuleImportMultiple, "a module import can only be a single name")
NZSL_SHADERLANG_PARSER_ERROR(InvalidVersion, "\"{}\" is not a valid version", std::string)
NZSL_SHADERLANG_PARSER_ERROR(MissingAttribute, "missing attribute {}", Ast::AttributeType)
NZSL_SHADERLANG_PARSER_ERROR(ModuleFeatureMultipleUnique, "module feature {} has already been specified", Ast::ModuleFeature)
Expand Down
7 changes: 5 additions & 2 deletions include/NZSL/LangWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <NZSL/Ast/ExpressionVisitorExcept.hpp>
#include <NZSL/Ast/Module.hpp>
#include <NZSL/Ast/StatementVisitorExcept.hpp>
#include <set>
#include <sstream>
#include <string>

namespace nzsl
Expand Down Expand Up @@ -70,6 +68,8 @@ namespace nzsl
void Append(const Ast::IntrinsicFunctionType& intrinsicFunctionType);
void Append(const Ast::MatrixType& matrixType);
void Append(const Ast::MethodType& methodType);
void Append(const Ast::ModuleType& moduleType);
void Append(const Ast::NamedExternalBlockType& namedExternalBlockType);
void Append(Ast::NoType);
void Append(Ast::PrimitiveType type);
void Append(const Ast::PushConstantType& pushConstantType);
Expand Down Expand Up @@ -120,6 +120,7 @@ namespace nzsl
void RegisterAlias(std::size_t aliasIndex, std::string aliasName);
void RegisterConstant(std::size_t constantIndex, std::string constantName);
void RegisterFunction(std::size_t funcIndex, std::string functionName);
void RegisterModule(std::size_t moduleIndex, std::string moduleName);
void RegisterStruct(std::size_t structIndex, std::string structName);
void RegisterVariable(std::size_t varIndex, std::string varName);

Expand All @@ -142,6 +143,8 @@ namespace nzsl
void Visit(Ast::FunctionExpression& node) override;
void Visit(Ast::IdentifierExpression& node) override;
void Visit(Ast::IntrinsicExpression& node) override;
void Visit(Ast::ModuleExpression& node) override;
void Visit(Ast::NamedExternalBlockExpression& node) override;
void Visit(Ast::StructTypeExpression& node) override;
void Visit(Ast::SwizzleExpression& node) override;
void Visit(Ast::VariableValueExpression& node) override;
Expand Down
2 changes: 1 addition & 1 deletion include/NZSL/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace nzsl
Ast::ExpressionPtr ParseStringExpression();

const std::string& ParseIdentifierAsName(SourceLocation* sourceLocation);
std::string ParseModuleName();
std::string ParseModuleName(SourceLocation* sourceLocation);
Ast::ExpressionPtr ParseType();

const std::string& ExtractStringAttribute(Attribute&& attribute);
Expand Down
Loading

0 comments on commit e2c652d

Please sign in to comment.