CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
TypeDenoter.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "ASTEnums.hpp"
11#include "Visitor/Visitor.hpp"
12
14
18
20
21#include <memory>
22
24
25#define DECLARATION_PTR(CLASS_NAME) \
26 struct CLASS_NAME; \
27 using CLASS_NAME##Ptr = SPtr<CLASS_NAME>
28
39
40#undef DECLARATION_PTR
41
42 // Vector space structure for BaseTypeDenoter, used as language extension for a stronger type system.
43 struct VectorSpace {
45
46 VectorSpace() = default;
49
50 // Returns a descriptive string of this vector space.
52
53 // Returns true if this vector space is specified, i.e. source and destination are non-empty.
54 [[nodiscard]] bool isSpecified() const;
55
56 // Returns true if this vector space is a change of basis, i.e. source and destination spaces are different.
57 [[nodiscard]] bool isChangeOfBasis() const;
58
59 // Returns true if this vector space can be assigned to the specified vector space, i.e. its destination space equals the specified source space.
60 [[nodiscard]] bool isAssignableTo(const VectorSpace& rhs) const;
61
62 // Sets the source and destination spaces to the specified identifier.
63 void set(const StringType& space);
64
65 // Sets the source and destination spaces to the specified identifiers.
67
68 // Returns the common vector-space from the specified expressions, or throws ASTRuntimeError on failure.
70
71 // Copies the vector space of 'srcTypeDen' into 'dstTypeDen' if both types are non-empty and instances of BaseTypeDenoter.
73
74 StringType src; // Source vector space name.
75 StringType dst; // Destination vector space name.
76 };
77
78 bool operator==(const VectorSpace& lhs, const VectorSpace& rhs);
79 bool operator!=(const VectorSpace& lhs, const VectorSpace& rhs);
80
81 // Type denoter base class.
82 struct TypeDenoter : std::enable_shared_from_this<TypeDenoter> {
83 // Type denoter class types.
84 enum class Types {
85 Void,
86 Null,
87 Base,
88 Buffer,
89 Sampler,
90 Struct,
91 Alias,
92 Array,
94 };
95
96 // Type denoter comparison flags.
97 enum : UInt32 {
98 // Ignore generic sub types in a buffer type denoter (for 'Equals' function).
100 };
101
102 virtual ~TypeDenoter();
103
104 // Returns the type (kind) of this type denoter.
105 virtual Types getType() const = 0;
106
107 // Returns a simple string representation of this type denoter (e.g. "scalar type").
108 virtual String toString() const = 0;
109
110 // Returns a copy of this type denoter.
111 virtual TypeDenoterPtr copy() const = 0;
112
113 // Returns true if this (aliased) type denoter is equal to the specified (aliased) type denoter (see GetAliased).
114 virtual bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const;
115
116 // Returns true if this type denoter can be casted to the specified target type denoter (special cases void and base types).
117 virtual bool isCastableTo(const TypeDenoter& targetType) const;
118
119 // Accumulates the vector size for this type denoter (with a 16 byte boundary), and returns true on success.
120 virtual bool accumulateAlignedVectorSize(UInt32& size, UInt32& padding, UInt32* offset = nullptr) const;
121
122 public: // Shortcuts
123 // Shortcut to check if this is a VoidTypeDenoter.
124 bool isVoid() const;
125
126 // Shortcut to check if this is a NullTypeDenoter.
127 bool isNull() const;
128
129 // Shortcut to check if this is a BaseTypeDenoter.
130 bool isBase() const;
131
132 // Shortcut to check if this is a BaseTypeDenoter of a scalar data type (see global function IsScalarType).
133 bool isScalar() const;
134
135 // Shortcut to check if this is a BaseTypeDenoter of a vector data type (see global function IsVectorType).
136 bool isVector() const;
137
138 // Shortcut to check if this is a BaseTypeDenoter of a matrix data type (see global function IsMatrixType).
139 bool isMatrix() const;
140
141 // Shortcut to check if this is a SamplerTypeDenoter.
142 bool isSampler() const;
143
144 // Shortcut to check if this is a BufferTypeDenoter.
145 bool isBuffer() const;
146
147 // Shortcut to check if this is a StructTypeDenoter.
148 bool isStruct() const;
149
150 // Shortcut to check if this is an AliasTypeDenoter.
151 bool isAlias() const;
152
153 // Shortcut to check if this is an ArrayTypeDenoter.
154 bool isArray() const;
155
156 // Shortcut to check if this is a FunctionTypeDenoter.
157 bool isFunction() const;
158
159 // Returns this type denoter as the specified sub class if this type denoter has the correct type. Otherwise, null is returned.
160 template<typename T> T* as() { return (getType() == T::classType ? static_cast<T*>(this) : nullptr); }
161
162 // Returns this constant type denoter as the specified sub class if this type denoter has the correct type. Otherwise, null is returned.
163 template<typename T> const T* as() const { return (getType() == T::classType ? static_cast<const T*>(this) : nullptr); }
164
165 public: // Type Derivation
170 virtual TypeDenoterPtr getSub(const Expression* expression = nullptr);
171
172 // Returns a sub type denoter for the identifier of the specified object expression.
173 virtual TypeDenoterPtr getSubObject(const String& ident, const AST* ast = nullptr);
174
175 // Returns a sub type denoter for the array indices of the specified array access expression.
176 virtual TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST* ast = nullptr);
177
178 // Returns either this type denoter or an aliased type.
179 virtual const TypeDenoter& getAliased() const;
180
181 public: // Type specific functions
182 // Returns the type identifier (if it has one), e.g. for structs and type aliases.
183 virtual String getIdent() const;
184
185 // Sets the identifier of this type denoter if the aliased type is anonymous.
186 virtual void setIdentIfAnonymous(const String& ident);
187
188 // Returns the number of array dimensions. By default 0.
189 virtual UInt32 numDimensions() const;
190
191 // Returns a pointer to the AST symbol reference or null if there is no such reference.
192 virtual AST* symbolRef() const;
193
194 // Returns either this type denoter (if 'arrayDimensions' is empty), or this type denoter as array with the specified dimension expressions.
195 virtual TypeDenoterPtr asArray(const Vector<ArrayDimensionPtr>& arrayDimensions);
196
197 // Returns the sub type denoter, or null if there is no sub type denoter.
199
200 public:
201 // Find the best suitable common type denoter for both left and right hand side type denoters.
203
204 // Find the best suitable common type denoter from the left and right hand side expressions, and throws an ASTRuntimeError on failure.
205 static TypeDenoterPtr findCommonTypeDenoterFrom(const ExpressionPtr& lhsExpression, const ExpressionPtr& rhsExpression, bool useMinDimension = false, const AST* ast = nullptr);
206
207 // Makes a boolean type denoter with the dimension of the specified type denoter.
209
210 // Returns true if the specified types have an implicit vector truncation (i.e. .
212 };
213
214 // Void type denoter.
215 struct VoidTypeDenoter : public TypeDenoter {
217
218 Types getType() const override;
219 String toString() const override;
220 TypeDenoterPtr copy() const override;
221
222 // Returns always false, since void type can not be casted to anything.
223 bool isCastableTo(const TypeDenoter& targetType) const override;
224 };
225
226 // Null type denoter.
227 struct NullTypeDenoter : public TypeDenoter {
229
230 Types getType() const override;
231 String toString() const override;
232 TypeDenoterPtr copy() const override;
233
234 bool isCastableTo(const TypeDenoter& targetType) const override;
235 };
236
237 // Base type denoter.
238 struct BaseTypeDenoter : public TypeDenoter {
240
241 BaseTypeDenoter() = default;
243
244 Types getType() const override;
245 String toString() const override;
246 TypeDenoterPtr copy() const override;
247
248 bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const override;
249 bool isCastableTo(const TypeDenoter& targetType) const override;
250 bool accumulateAlignedVectorSize(UInt32& size, UInt32& padding, UInt32* offset = nullptr) const override;
251
252 TypeDenoterPtr getSubObject(const String& ident, const AST* ast = nullptr) override;
253 TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST* ast = nullptr) override;
254
255 DataType dataType = DataType::Undefined; // Data type of this base type denoter. By default DataType::Undefined.
256 VectorSpace vectorSpace; // Vector space of this type denoter.
257 };
258
267
268 BufferTypeDenoter() = default;
271
272 Types getType() const override;
273 String toString() const override;
274 TypeDenoterPtr copy() const override;
275
276 bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const override;
277
278 TypeDenoterPtr getSubObject(const String& ident, const AST* ast = nullptr) override;
279 TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST* ast = nullptr) override;
280
281 AST* symbolRef() const override;
282
283 // Returns the generic type denoter of this buffer type.
285
286 // Always returns a valid generic type denoter. By default BaseTypeDenoter(Float4).
288
291 Int32 genericSize = 1; // Either number of samples in [1, 128) (for multi-sampled textures), or patch size. By default 1.
292
295 };
296
297 // Sampler type denoter.
316
317 // Struct type denoter.
320
321 StructTypeDenoter() = default;
324
325 Types getType() const override;
326 String toString() const override;
327 TypeDenoterPtr copy() const override;
328
329 void setIdentIfAnonymous(const String& ident) override;
330
331 bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const override;
332 bool isCastableTo(const TypeDenoter& targetType) const override;
333 bool accumulateAlignedVectorSize(UInt32& size, UInt32& padding, UInt32* offset = nullptr) const override;
334
335 String getIdent() const override;
336
337 AST* symbolRef() const override;
338
339 TypeDenoterPtr getSubObject(const String& ident, const AST* ast = nullptr) override;
340
342
343 String ident; // Type identifier
344
345 StructDeclaration* structDeclarationRef = nullptr; // Reference to the StructDecl AST node
346 };
347
348 // Alias type denoter.
351
352 AliasTypeDenoter() = default;
355
356 Types getType() const override;
357 String toString() const override;
358 TypeDenoterPtr copy() const override;
359
360 void setIdentIfAnonymous(const String& ident) override;
361
362 bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const override;
363 bool isCastableTo(const TypeDenoter& targetType) const override;
364 bool accumulateAlignedVectorSize(UInt32& size, UInt32& padding, UInt32* offset = nullptr) const override;
365
366 String getIdent() const override;
367
368 TypeDenoterPtr getSub(const Expression* expression = nullptr) override;
369 TypeDenoterPtr getSubObject(const String& ident, const AST* ast = nullptr) override;
370 TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST* ast = nullptr) override;
371
372 const TypeDenoter& getAliased() const override;
373
374 // Returns the type denoter of the aliased type, or throws an ASTRuntimeError on failure.
375 const TypeDenoterPtr& getAliasedTypeOrThrow(const AST* ast = nullptr) const;
376
377 UInt32 numDimensions() const override;
378
379 AST* symbolRef() const override;
380
381 String ident; // Type identifier
382
383 AliasDeclaration* aliasDeclarationRef = nullptr; // Reference to the AliasDecl AST node.
384 };
385
386 // Array type denoter.
389
390 ArrayTypeDenoter() = default;
391
394
396
397 Types getType() const override;
398
399 // Throws std::runtime_error if 'subTypeDenoter' is null.
400 String toString() const override;
401
402 TypeDenoterPtr copy() const override;
403
404 TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST* ast = nullptr) override;
405
406 bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const override;
407 bool isCastableTo(const TypeDenoter& targetType) const override;
408 bool accumulateAlignedVectorSize(UInt32& size, UInt32& padding, UInt32* offset = nullptr) const override;
409
410 // Returns the number if dimensions for this array plus its sub type (if it's also an array).
411 UInt32 numDimensions() const override;
412
413 AST* symbolRef() const override;
414
415 // Returns true if the dimensions of the specified array type denoter are equal to the dimension of this array type denoter.
416 bool equalsDimensions(const ArrayTypeDenoter& rhs) const;
417
418 // Returns a copy of this type denoter with the accumulated array dimensions.
420
421 // Returns the sub type denoter of this struct type.
423
424 // Inserts the specified sub array type denoter to this type denoter, with all its array dimension, and replaces the base type denoter.
426
427 // Returns the array dimension sizes.
429
430 // Returns the number of array elements, or 0 if a dynamic array dimension is contained.
432
433 TypeDenoterPtr subTypeDenoter; // Sub type denoter
435 };
436
437 // Function type denoter (currently only used for enhanced error reports).
440
444
445 Types getType() const override;
446 String toString() const override;
447 TypeDenoterPtr copy() const override;
448
449 bool equals(const TypeDenoter& rhs, const RawFlags& compareFlags = 0) const override;
450 bool isCastableTo(const TypeDenoter& targetType) const override;
451
452 String getIdent() const override;
453
454 String ident; // Type identifier
455
456 Vector<FunctionDeclaration*> funcDeclarationRefs; // Reference to all function candidates.
457 };
458
459} // namespace CeresEngine::ShaderCompiler
#define DECLARATION_PTR(CLASS_NAME)
Definition TypeDenoter.hpp:25
Basic string that uses framework's memory allocators.
Definition String.hpp:60
Definition Flags.hpp:235
Definition AST.hpp:33
SPtr< Expression > ExpressionPtr
Definition Visitor.hpp:26
bool operator!=(const Identifier &lhs, const Identifier &rhs)
Definition Identifier.hpp:64
ImageLayoutFormat
Definition ASTEnums.hpp:642
SPtr< BaseTypeDenoter > BaseTypeDenoterPtr
Definition TypeDenoter.hpp:32
bool operator==(const Identifier &lhs, const Identifier &rhs)
Definition Identifier.hpp:60
SPtr< TypeDenoter > TypeDenoterPtr
Definition TypeDenoter.hpp:29
DataType
Definition ASTEnums.hpp:159
BufferType
Definition ASTEnums.hpp:492
SamplerType
Definition ASTEnums.hpp:571
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::int32_t Int32
Definition DataTypes.hpp:21
CiBasicString< char > CiString
Narrow case-insensitive string used for handling narrow encoded text (either locale specific ANSI or ...
Definition String.hpp:252
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition TypeDenoter.hpp:349
TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST *ast=nullptr) override
bool isCastableTo(const TypeDenoter &targetType) const override
TypeDenoterPtr copy() const override
TypeDenoterPtr getSubObject(const String &ident, const AST *ast=nullptr) override
AliasTypeDenoter(AliasDeclaration *aliasDeclarationRef)
static const Types classType
Definition TypeDenoter.hpp:350
bool accumulateAlignedVectorSize(UInt32 &size, UInt32 &padding, UInt32 *offset=nullptr) const override
const TypeDenoter & getAliased() const override
TypeDenoterPtr getSub(const Expression *expression=nullptr) override
Returns a sub type denoter for the specified expression.
String ident
Definition TypeDenoter.hpp:381
void setIdentIfAnonymous(const String &ident) override
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
AliasDeclaration * aliasDeclarationRef
Definition TypeDenoter.hpp:383
const TypeDenoterPtr & getAliasedTypeOrThrow(const AST *ast=nullptr) const
Definition TypeDenoter.hpp:387
bool isCastableTo(const TypeDenoter &targetType) const override
ArrayTypeDenoter(const TypeDenoterPtr &subTypeDenoter, const Vector< ArrayDimensionPtr > &arrayDimensions)
TypeDenoterPtr asArray(const Vector< ArrayDimensionPtr > &subArrayDimensions) override
TypeDenoter * fetchSubTypeDenoter() const override
ArrayTypeDenoter(const TypeDenoterPtr &subTypeDenoter)
bool equalsDimensions(const ArrayTypeDenoter &rhs) const
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
void insertSubArray(const ArrayTypeDenoter &subArrayTypeDenoter)
bool accumulateAlignedVectorSize(UInt32 &size, UInt32 &padding, UInt32 *offset=nullptr) const override
TypeDenoterPtr copy() const override
TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST *ast=nullptr) override
ArrayTypeDenoter(const TypeDenoterPtr &subTypeDenoter, const Vector< ArrayDimensionPtr > &baseArrayDimensions, const Vector< ArrayDimensionPtr > &subArrayDimensions)
TypeDenoterPtr subTypeDenoter
Definition TypeDenoter.hpp:433
Vector< ArrayDimensionPtr > arrayDimensions
Definition TypeDenoter.hpp:434
static const Types classType
Definition TypeDenoter.hpp:388
Definition TypeDenoter.hpp:238
DataType dataType
Definition TypeDenoter.hpp:255
bool isCastableTo(const TypeDenoter &targetType) const override
TypeDenoterPtr copy() const override
TypeDenoterPtr getSubObject(const String &ident, const AST *ast=nullptr) override
VectorSpace vectorSpace
Definition TypeDenoter.hpp:256
static const Types classType
Definition TypeDenoter.hpp:239
bool accumulateAlignedVectorSize(UInt32 &size, UInt32 &padding, UInt32 *offset=nullptr) const override
TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST *ast=nullptr) override
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
Buffer type denoter with generic sub type and generic size.
Definition TypeDenoter.hpp:265
ImageLayoutFormat layoutFormat
Definition TypeDenoter.hpp:294
BufferDeclaration * bufferDeclarationRef
Definition TypeDenoter.hpp:293
TypeDenoterPtr copy() const override
BufferTypeDenoter(const BufferType bufferType)
TypeDenoterPtr genericTypeDenoter
Definition TypeDenoter.hpp:290
BufferTypeDenoter(BufferDeclaration *bufferDeclarationRef)
Int32 genericSize
Definition TypeDenoter.hpp:291
BufferType bufferType
Definition TypeDenoter.hpp:289
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST *ast=nullptr) override
TypeDenoter * fetchSubTypeDenoter() const override
static const Types classType
Definition TypeDenoter.hpp:266
TypeDenoterPtr getSubObject(const String &ident, const AST *ast=nullptr) override
Vector< FunctionDeclaration * > funcDeclarationRefs
Definition TypeDenoter.hpp:456
String ident
Definition TypeDenoter.hpp:454
static const Types classType
Definition TypeDenoter.hpp:439
TypeDenoterPtr copy() const override
bool isCastableTo(const TypeDenoter &targetType) const override
FunctionTypeDenoter(FunctionDeclaration *funcDeclarationRef)
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
FunctionTypeDenoter(const String &ident, const Vector< FunctionDeclaration * > &funcDeclarationRefs)
Definition TypeDenoter.hpp:227
bool isCastableTo(const TypeDenoter &targetType) const override
static const Types classType
Definition TypeDenoter.hpp:228
TypeDenoterPtr copy() const override
SamplerTypeDenoter(SamplerDeclaration *samplerDeclarationRef)
SamplerTypeDenoter(const SamplerType samplerType)
SamplerType samplerType
Definition TypeDenoter.hpp:313
SamplerDeclaration * samplerDeclarationRef
Definition TypeDenoter.hpp:314
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
TypeDenoterPtr copy() const override
static const Types classType
Definition TypeDenoter.hpp:299
void setIdentIfAnonymous(const String &ident) override
String ident
Definition TypeDenoter.hpp:343
bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const override
static const Types classType
Definition TypeDenoter.hpp:319
StructDeclaration * getStructDeclarationOrThrow(const AST *ast=nullptr) const
TypeDenoterPtr copy() const override
bool isCastableTo(const TypeDenoter &targetType) const override
TypeDenoterPtr getSubObject(const String &ident, const AST *ast=nullptr) override
StructTypeDenoter(StructDeclaration *structDeclarationRef)
bool accumulateAlignedVectorSize(UInt32 &size, UInt32 &padding, UInt32 *offset=nullptr) const override
StructDeclaration * structDeclarationRef
Definition TypeDenoter.hpp:345
Definition TypeDenoter.hpp:82
virtual TypeDenoterPtr asArray(const Vector< ArrayDimensionPtr > &arrayDimensions)
virtual bool accumulateAlignedVectorSize(UInt32 &size, UInt32 &padding, UInt32 *offset=nullptr) const
virtual TypeDenoterPtr copy() const =0
static TypeDenoterPtr findCommonTypeDenoter(const TypeDenoterPtr &lhsTypeDen, const TypeDenoterPtr &rhsTypeDen, bool useMinDimension=false)
virtual TypeDenoter * fetchSubTypeDenoter() const
const T * as() const
Definition TypeDenoter.hpp:163
static TypeDenoterPtr findCommonTypeDenoterFrom(const ExpressionPtr &lhsExpression, const ExpressionPtr &rhsExpression, bool useMinDimension=false, const AST *ast=nullptr)
static Int32 findVectorTruncation(const TypeDenoter &sourceTypeDen, const TypeDenoter &destTypeDen, int &sourceVecSize, int &destVecSize)
virtual String toString() const =0
@ IgnoreGenericSubType
Definition TypeDenoter.hpp:99
virtual bool isCastableTo(const TypeDenoter &targetType) const
static BaseTypeDenoterPtr makeBoolTypeWithDimensionOf(const TypeDenoter &typeDen)
virtual bool equals(const TypeDenoter &rhs, const RawFlags &compareFlags=0) const
virtual TypeDenoterPtr getSubObject(const String &ident, const AST *ast=nullptr)
Types
Definition TypeDenoter.hpp:84
virtual TypeDenoterPtr getSubArray(const std::size_t numArrayIndices, const AST *ast=nullptr)
virtual void setIdentIfAnonymous(const String &ident)
virtual TypeDenoterPtr getSub(const Expression *expression=nullptr)
Returns a sub type denoter for the specified expression.
T * as()
Definition TypeDenoter.hpp:160
virtual const TypeDenoter & getAliased() const
Definition TypeDenoter.hpp:43
void set(const StringType &space)
static void copy(TypeDenoter *dstTypeDen, const TypeDenoter *srcTypeDen)
StringType dst
Definition TypeDenoter.hpp:75
VectorSpace(const StringType &space)
static VectorSpace findCommonVectorSpace(const Vector< ExpressionPtr > &expressionList, bool ignoreUnspecified=false, const AST *ast=nullptr)
StringType src
Definition TypeDenoter.hpp:74
void set(const StringType &srcSpace, const StringType &dstSpace)
VectorSpace(const StringType &src, const StringType &dst)
bool isAssignableTo(const VectorSpace &rhs) const
Definition TypeDenoter.hpp:215
static const Types classType
Definition TypeDenoter.hpp:216
TypeDenoterPtr copy() const override
bool isCastableTo(const TypeDenoter &targetType) const override