Skip to content

Commit e2c652d

Browse files
committed
Add support for whole module import
1 parent 1559d89 commit e2c652d

28 files changed

+886
-211
lines changed

include/NZSL/Ast/AstSerializer.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace nzsl::Ast
3939
void Serialize(IdentifierExpression& node);
4040
void Serialize(IntrinsicExpression& node);
4141
void Serialize(IntrinsicFunctionExpression& node);
42+
void Serialize(ModuleExpression& node);
43+
void Serialize(NamedExternalBlockExpression& node);
4244
void Serialize(StructTypeExpression& node);
4345
void Serialize(SwizzleExpression& node);
4446
void Serialize(TypeExpression& node);

include/NZSL/Ast/Cloner.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ namespace nzsl::Ast
5555
virtual ExpressionPtr Clone(IdentifierExpression& node);
5656
virtual ExpressionPtr Clone(IntrinsicExpression& node);
5757
virtual ExpressionPtr Clone(IntrinsicFunctionExpression& node);
58+
virtual ExpressionPtr Clone(ModuleExpression& node);
59+
virtual ExpressionPtr Clone(NamedExternalBlockExpression& node);
5860
virtual ExpressionPtr Clone(StructTypeExpression& node);
5961
virtual ExpressionPtr Clone(SwizzleExpression& node);
6062
virtual ExpressionPtr Clone(TypeExpression& node);

include/NZSL/Ast/Compare.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ namespace nzsl::Ast
6060
inline bool Compare(const IdentifierExpression& lhs, const IdentifierExpression& rhs, const ComparisonParams& params = {});
6161
inline bool Compare(const IntrinsicExpression& lhs, const IntrinsicExpression& rhs, const ComparisonParams& params = {});
6262
inline bool Compare(const IntrinsicFunctionExpression& lhs, const IntrinsicFunctionExpression& rhs, const ComparisonParams& params = {});
63+
inline bool Compare(const ModuleExpression& lhs, const ModuleExpression& rhs, const ComparisonParams& params = {});
64+
inline bool Compare(const NamedExternalBlockExpression& lhs, const NamedExternalBlockExpression& rhs, const ComparisonParams& params = {});
6365
inline bool Compare(const StructTypeExpression& lhs, const StructTypeExpression& rhs, const ComparisonParams& params = {});
6466
inline bool Compare(const SwizzleExpression& lhs, const SwizzleExpression& rhs, const ComparisonParams& params = {});
6567
inline bool Compare(const TypeExpression& lhs, const TypeExpression& rhs, const ComparisonParams& params = {});

include/NZSL/Ast/Compare.inl

+20-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ namespace nzsl::Ast
263263

264264
if (!Compare(lhs.renamedIdentifierLoc, rhs.renamedIdentifierLoc, params))
265265
return false;
266-
266+
267267
return true;
268268
}
269269

@@ -503,6 +503,22 @@ namespace nzsl::Ast
503503
return true;
504504
}
505505

506+
inline bool Compare(const ModuleExpression& lhs, const ModuleExpression& rhs, const ComparisonParams& params)
507+
{
508+
if (!Compare(lhs.moduleId, rhs.moduleId, params))
509+
return false;
510+
511+
return true;
512+
}
513+
514+
inline bool Compare(const NamedExternalBlockExpression& lhs, const NamedExternalBlockExpression& rhs, const ComparisonParams& params)
515+
{
516+
if (!Compare(lhs.externalBlockId, rhs.externalBlockId, params))
517+
return false;
518+
519+
return true;
520+
}
521+
506522
inline bool Compare(const StructTypeExpression& lhs, const StructTypeExpression& rhs, const ComparisonParams& params)
507523
{
508524
if (!Compare(lhs.structTypeId, rhs.structTypeId, params))
@@ -758,6 +774,9 @@ namespace nzsl::Ast
758774
if (params.compareModuleName && !Compare(lhs.moduleName, rhs.moduleName, params))
759775
return false;
760776

777+
if (!Compare(lhs.moduleIdentifier, rhs.moduleIdentifier, params))
778+
return false;
779+
761780
if (!Compare(lhs.identifiers, rhs.identifiers, params))
762781
return false;
763782

include/NZSL/Ast/Enums.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ namespace nzsl::Ast
216216
IdentifierExpression = 13,
217217
IntrinsicExpression = 14,
218218
IntrinsicFunctionExpression = 15,
219+
ModuleExpression = 42,
220+
NamedExternalBlockExpression = 43,
219221
StructTypeExpression = 16,
220222
SwizzleExpression = 17,
221223
TypeExpression = 18,
@@ -245,7 +247,7 @@ namespace nzsl::Ast
245247
ScopedStatement = 38,
246248
WhileStatement = 39,
247249

248-
Max = ContinueStatement
250+
Max = NamedExternalBlockExpression
249251
};
250252

251253
enum class PrimitiveType

include/NZSL/Ast/ExpressionType.hpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
#include <NZSL/Ast/Enums.hpp>
1313
#include <NZSL/Ast/ExpressionValue.hpp>
1414
#include <NZSL/Lang/SourceLocation.hpp>
15-
#include <optional>
1615
#include <string>
1716
#include <variant>
1817
#include <vector>
1918

20-
#ifdef NAZARA_COMPILER_GCC
21-
#pragma GCC diagnostic push
22-
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
23-
#endif
19+
NAZARA_WARNING_PUSH()
20+
NAZARA_WARNING_GCC_DISABLE("-Wmaybe-uninitialized")
2421

2522
namespace nzsl
2623
{
@@ -137,6 +134,22 @@ namespace nzsl::Ast
137134
inline bool operator!=(const MethodType& rhs) const;
138135
};
139136

137+
struct ModuleType
138+
{
139+
std::size_t moduleIndex;
140+
141+
inline bool operator==(const ModuleType& rhs) const;
142+
inline bool operator!=(const ModuleType& rhs) const;
143+
};
144+
145+
struct NamedExternalBlockType
146+
{
147+
std::size_t namedExternalBlockIndex;
148+
149+
inline bool operator==(const NamedExternalBlockType& rhs) const;
150+
inline bool operator!=(const NamedExternalBlockType& rhs) const;
151+
};
152+
140153
struct NoType
141154
{
142155
inline bool operator==(const NoType& rhs) const;
@@ -216,7 +229,7 @@ namespace nzsl::Ast
216229
inline bool operator!=(const PushConstantType& rhs) const;
217230
};
218231

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

221234
struct ContainedType
222235
{
@@ -252,6 +265,8 @@ namespace nzsl::Ast
252265
inline bool IsIntrinsicFunctionType(const ExpressionType& type);
253266
inline bool IsMatrixType(const ExpressionType& type);
254267
inline bool IsMethodType(const ExpressionType& type);
268+
inline bool IsModuleType(const ExpressionType& type);
269+
inline bool IsNamedExternalBlockType(const ExpressionType& type);
255270
inline bool IsNoType(const ExpressionType& type);
256271
inline bool IsPrimitiveType(const ExpressionType& type);
257272
inline bool IsPushConstantType(const ExpressionType& type);
@@ -296,6 +311,8 @@ namespace nzsl::Ast
296311
struct Stringifier
297312
{
298313
std::function<std::string(std::size_t aliasIndex)> aliasStringifier;
314+
std::function<std::string(std::size_t moduleIndex)> moduleStringifier;
315+
std::function<std::string(std::size_t moduleIndex)> namedExternalBlockStringifier;
299316
std::function<std::string(std::size_t structIndex)> structStringifier;
300317
std::function<std::string(std::size_t typeIndex)> typeStringifier;
301318
};
@@ -308,6 +325,8 @@ namespace nzsl::Ast
308325
NZSL_API std::string ToString(const IntrinsicFunctionType& type, const Stringifier& stringifier = {});
309326
NZSL_API std::string ToString(const MatrixType& type, const Stringifier& stringifier = {});
310327
NZSL_API std::string ToString(const MethodType& type, const Stringifier& stringifier = {});
328+
NZSL_API std::string ToString(const ModuleType& type, const Stringifier& stringifier = {});
329+
NZSL_API std::string ToString(const NamedExternalBlockType& type, const Stringifier& stringifier = {});
311330
NZSL_API std::string ToString(NoType type, const Stringifier& stringifier = {});
312331
NZSL_API std::string ToString(PrimitiveType type, const Stringifier& stringifier = {});
313332
NZSL_API std::string ToString(const PushConstantType& type, const Stringifier& stringifier = {});
@@ -320,9 +339,7 @@ namespace nzsl::Ast
320339
NZSL_API std::string ToString(const VectorType& type, const Stringifier& stringifier = {});
321340
}
322341

323-
#ifdef NAZARA_COMPILER_GCC
324-
#pragma GCC diagnostic pop
325-
#endif
342+
NAZARA_WARNING_POP()
326343

327344
#include <NZSL/Ast/ExpressionType.inl>
328345

include/NZSL/Ast/ExpressionType.inl

+50
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ namespace nzsl::Ast
4444
}
4545

4646

47+
inline bool NamedExternalBlockType::operator==(const NamedExternalBlockType& rhs) const
48+
{
49+
return namedExternalBlockIndex == rhs.namedExternalBlockIndex;
50+
}
51+
52+
inline bool NamedExternalBlockType::operator!=(const NamedExternalBlockType& rhs) const
53+
{
54+
return !operator==(rhs);
55+
}
56+
57+
4758
inline bool FunctionType::operator==(const FunctionType& rhs) const
4859
{
4960
return funcIndex == rhs.funcIndex;
@@ -83,6 +94,17 @@ namespace nzsl::Ast
8394
}
8495

8596

97+
inline bool ModuleType::operator==(const ModuleType& rhs) const
98+
{
99+
return moduleIndex == rhs.moduleIndex;
100+
}
101+
102+
inline bool ModuleType::operator!=(const ModuleType& rhs) const
103+
{
104+
return !operator==(rhs);
105+
}
106+
107+
86108
inline bool NoType::operator==(const NoType& /*rhs*/) const
87109
{
88110
return true;
@@ -216,6 +238,16 @@ namespace nzsl::Ast
216238
return std::holds_alternative<MethodType>(type);
217239
}
218240

241+
inline bool IsModuleType(const ExpressionType& type)
242+
{
243+
return std::holds_alternative<ModuleType>(type);
244+
}
245+
246+
inline bool IsNamedExternalBlockType(const ExpressionType& type)
247+
{
248+
return std::holds_alternative<NamedExternalBlockType>(type);
249+
}
250+
219251
inline bool IsNoType(const ExpressionType& type)
220252
{
221253
return std::holds_alternative<NoType>(type);
@@ -388,6 +420,24 @@ namespace std
388420
}
389421
};
390422

423+
template<>
424+
struct hash<nzsl::Ast::ModuleType>
425+
{
426+
std::size_t operator()(const nzsl::Ast::ModuleType& moduleType) const
427+
{
428+
return Nz::HashCombine(moduleType.moduleIndex);
429+
}
430+
};
431+
432+
template<>
433+
struct hash<nzsl::Ast::NamedExternalBlockType>
434+
{
435+
std::size_t operator()(const nzsl::Ast::NamedExternalBlockType& namedExternalBlockType) const
436+
{
437+
return Nz::HashCombine(namedExternalBlockType.namedExternalBlockIndex);
438+
}
439+
};
440+
391441
template<>
392442
struct hash<nzsl::Ast::PushConstantType>
393443
{

include/NZSL/Ast/NodeList.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ NZSL_SHADERAST_EXPRESSION(Function)
4444
NZSL_SHADERAST_EXPRESSION(Identifier)
4545
NZSL_SHADERAST_EXPRESSION(Intrinsic)
4646
NZSL_SHADERAST_EXPRESSION(IntrinsicFunction)
47+
NZSL_SHADERAST_EXPRESSION(Module)
48+
NZSL_SHADERAST_EXPRESSION(NamedExternalBlock)
4749
NZSL_SHADERAST_EXPRESSION(StructType)
4850
NZSL_SHADERAST_EXPRESSION(Swizzle)
4951
NZSL_SHADERAST_EXPRESSION(Type)

include/NZSL/Ast/Nodes.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ namespace nzsl::Ast
223223
std::size_t intrinsicId;
224224
};
225225

226+
struct NZSL_API ModuleExpression : Expression
227+
{
228+
NodeType GetType() const override;
229+
void Visit(ExpressionVisitor& visitor) override;
230+
231+
std::size_t moduleId;
232+
};
233+
234+
struct NZSL_API NamedExternalBlockExpression : Expression
235+
{
236+
NodeType GetType() const override;
237+
void Visit(ExpressionVisitor& visitor) override;
238+
239+
std::size_t externalBlockId;
240+
};
241+
226242
struct NZSL_API StructTypeExpression : Expression
227243
{
228244
NodeType GetType() const override;
@@ -478,6 +494,7 @@ namespace nzsl::Ast
478494
SourceLocation renamedIdentifierLoc;
479495
};
480496

497+
std::string moduleIdentifier;
481498
std::string moduleName;
482499
std::vector<Identifier> identifiers;
483500
};

include/NZSL/Ast/RecursiveVisitor.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace nzsl::Ast
3535
void Visit(IdentifierExpression& node) override;
3636
void Visit(IntrinsicExpression& node) override;
3737
void Visit(IntrinsicFunctionExpression& node) override;
38+
void Visit(ModuleExpression& node) override;
39+
void Visit(NamedExternalBlockExpression& node) override;
3840
void Visit(StructTypeExpression& node) override;
3941
void Visit(SwizzleExpression& node) override;
4042
void Visit(TypeExpression& node) override;

include/NZSL/Ast/Utils.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace nzsl::Ast
4646
void Visit(IdentifierExpression& node) override;
4747
void Visit(IntrinsicExpression& node) override;
4848
void Visit(IntrinsicFunctionExpression& node) override;
49+
void Visit(ModuleExpression& node) override;
50+
void Visit(NamedExternalBlockExpression& node) override;
4951
void Visit(StructTypeExpression& node) override;
5052
void Visit(SwizzleExpression& node) override;
5153
void Visit(TypeExpression& node) override;

include/NZSL/GlslWriter.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ namespace nzsl
7777
void Append(const Ast::IntrinsicFunctionType& intrinsicFunctionType);
7878
void Append(const Ast::MatrixType& matrixType);
7979
void Append(const Ast::MethodType& methodType);
80+
void Append(const Ast::ModuleType& methodType);
8081
void Append(Ast::MemoryLayout layout);
82+
void Append(const Ast::NamedExternalBlockType& namedExternalBlockType);
8183
void Append(Ast::NoType);
8284
void Append(Ast::PrimitiveType type);
8385
void Append(const Ast::PushConstantType& pushConstantType);

include/NZSL/Lang/ErrorList.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ NZSL_SHADERLANG_PARSER_ERROR(AttributeUnexpectedParameterCount, "attribute {} ex
4242
NZSL_SHADERLANG_PARSER_ERROR(ExpectedToken, "expected token {}, got {}", TokenType, TokenType)
4343
NZSL_SHADERLANG_PARSER_ERROR(DuplicateIdentifier, "duplicate identifier")
4444
NZSL_SHADERLANG_PARSER_ERROR(DuplicateModule, "duplicate module")
45+
NZSL_SHADERLANG_PARSER_ERROR(ModuleImportInvalidIdentifier, "{} is not a valid identifier to import", std::string)
46+
NZSL_SHADERLANG_PARSER_ERROR(ModuleImportMultiple, "a module import can only be a single name")
4547
NZSL_SHADERLANG_PARSER_ERROR(InvalidVersion, "\"{}\" is not a valid version", std::string)
4648
NZSL_SHADERLANG_PARSER_ERROR(MissingAttribute, "missing attribute {}", Ast::AttributeType)
4749
NZSL_SHADERLANG_PARSER_ERROR(ModuleFeatureMultipleUnique, "module feature {} has already been specified", Ast::ModuleFeature)

include/NZSL/LangWriter.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <NZSL/Ast/ExpressionVisitorExcept.hpp>
1313
#include <NZSL/Ast/Module.hpp>
1414
#include <NZSL/Ast/StatementVisitorExcept.hpp>
15-
#include <set>
16-
#include <sstream>
1715
#include <string>
1816

1917
namespace nzsl
@@ -70,6 +68,8 @@ namespace nzsl
7068
void Append(const Ast::IntrinsicFunctionType& intrinsicFunctionType);
7169
void Append(const Ast::MatrixType& matrixType);
7270
void Append(const Ast::MethodType& methodType);
71+
void Append(const Ast::ModuleType& moduleType);
72+
void Append(const Ast::NamedExternalBlockType& namedExternalBlockType);
7373
void Append(Ast::NoType);
7474
void Append(Ast::PrimitiveType type);
7575
void Append(const Ast::PushConstantType& pushConstantType);
@@ -120,6 +120,7 @@ namespace nzsl
120120
void RegisterAlias(std::size_t aliasIndex, std::string aliasName);
121121
void RegisterConstant(std::size_t constantIndex, std::string constantName);
122122
void RegisterFunction(std::size_t funcIndex, std::string functionName);
123+
void RegisterModule(std::size_t moduleIndex, std::string moduleName);
123124
void RegisterStruct(std::size_t structIndex, std::string structName);
124125
void RegisterVariable(std::size_t varIndex, std::string varName);
125126

@@ -142,6 +143,8 @@ namespace nzsl
142143
void Visit(Ast::FunctionExpression& node) override;
143144
void Visit(Ast::IdentifierExpression& node) override;
144145
void Visit(Ast::IntrinsicExpression& node) override;
146+
void Visit(Ast::ModuleExpression& node) override;
147+
void Visit(Ast::NamedExternalBlockExpression& node) override;
145148
void Visit(Ast::StructTypeExpression& node) override;
146149
void Visit(Ast::SwizzleExpression& node) override;
147150
void Visit(Ast::VariableValueExpression& node) override;

include/NZSL/Parser.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ namespace nzsl
9595
Ast::ExpressionPtr ParseStringExpression();
9696

9797
const std::string& ParseIdentifierAsName(SourceLocation* sourceLocation);
98-
std::string ParseModuleName();
98+
std::string ParseModuleName(SourceLocation* sourceLocation);
9999
Ast::ExpressionPtr ParseType();
100100

101101
const std::string& ExtractStringAttribute(Attribute&& attribute);

0 commit comments

Comments
 (0)