CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
HLSLParser.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 "HLSLScanner.hpp"
11
13
15
17
19
21
22 // Syntax parser class for the shading language HLSL.
23 class HLSLParser : public SLParser {
24 public:
25 HLSLParser(Log* log = nullptr);
26
28 bool rowMajorAlignment = false, bool enableWarnings = false);
29
30 private:
32
33 // Returns true if the current token is a data type.
34 [[nodiscard]] bool isDataType() const;
35
36 // Returns true if the current token is a base data type (i.e. scalar, vector, matrix, or string type denoter).
37 [[nodiscard]] bool isBaseDataType() const;
38
39 // Returns true if the current token is a literal.
40 [[nodiscard]] bool isLiteral() const;
41
42 // Returns true if the current token is part of an arithmetic unary expression, i.e. either '-' or '+'.
44
45 // Returns true if the current token is a modifier of a type specifier.
46 [[nodiscard]] bool isModifier() const;
47
48 // Converts the specified expression to a type name expression if it is a left-hand-side of a cast expression.
50
51 // Overrides the token accept function to process all directives before the actual parsing.
52 TokenPtr acceptIt() override;
53
54 // Processes the specified directive (only '#line'-directive are allowed after pre-processing).
55 void processDirective(const String& ident);
58
59 private: // Symbol table
60 // Opens a new scope of the type name symbol table.
61 void openScope();
62
63 // Closes the current scope of the type name symbol table.
64 void closeScope();
65
66 // Registers the specified identifier as type name, to detect cast expressions.
67 void registerTypeName(const String& ident);
68
69 // Returns true if the specified identifier is a valid type name within the current scope.
70 [[nodiscard]] bool isRegisteredTypeName(const String& ident) const;
71
72 // Makes a new alias declaration statement and registers it's identifier in the symbol table.
74
75 // Generates all pre defined type aliases (e.g. 'typedef Int32 DWORD').
77
78 // Creates a new var-decl statement with the current matrix pack alignment type modifier.
80
81 private: // Parsing
83
89 VarDeclarationPtr parseVarDeclaration(VarDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr) override;
90
96
97 BufferDeclarationPtr parseBufferDeclaration(BufferDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
98 SamplerDeclarationPtr parseSamplerDeclaration(SamplerDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
99 StructDeclarationPtr parseStructDeclaration(bool parseStructtoken = true, const TokenPtr& identifierToken = nullptr);
102 const TokenPtr& identifierToken = nullptr);
104
110 BasicDeclarationStatementPtr parseFunctionDeclarationStatement(const TypeSpecifierPtr& returnType = nullptr, const TokenPtr& identifierToken = nullptr);
112 BufferDeclarationStatementPtr parseBufferDeclarationStatement(const BufferTypeDenoterPtr& typeDenoter = nullptr, const TokenPtr& identifierToken = nullptr);
113 SamplerDeclarationStatementPtr parseSamplerDeclarationStatement(const SamplerTypeDenoterPtr& typeDenoter = nullptr, const TokenPtr& identifierToken = nullptr);
116
121
135 CallExpressionPtr parseCallExpressionWithPrefixOpt(const ExpressionPtr& prefixExpression = nullptr, bool isStatic = false, const TokenPtr& identifierToken = nullptr);
137
142 Vector<BufferDeclarationPtr> parseBufferDeclarationList(BufferDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
143 Vector<SamplerDeclarationPtr> parseSamplerDeclarationList(SamplerDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
146
147 String parseIdentWithNamespaceOpt(ObjectExpressionPtr& namespaceExpression, TokenPtr identifierToken = nullptr, SourceArea* area = nullptr);
148
149 TypeDenoterPtr parseTypeDenoter(bool allowVoidType = true, StructDeclarationPtr* structDeclaration = nullptr);
161
164
167
177
179
180 bool parseModifiers(TypeSpecifier* typeSpecifier, bool allowPrimitiveType = false);
181
182 private:
184
185 // Symbol table for type name (i.e. structure and typedef identifiers) to detect cast expression, which are not context free.
187
188 // True, if semantics are parsed for D3D10+ shader.
190
191 // True, if Cg keyword extensions are enabled.
192 bool mEnableCgKeywords = false;
193
194 // True, if matrix packing is globally set to row major.
195 bool mRowMajorAlignment = false;
196 };
197
198} // namespace CeresEngine::ShaderCompiler
Definition HLSLParser.hpp:23
BufferTypeDenoterPtr parseBufferTypeDenoter()
AliasDeclarationStatementPtr parseAliasDeclarationStatement()
Vector< StatementPtr > parseGlobalStatementList()
AssignExpressionPtr parseAssignExpression(const ExpressionPtr &expression)
ProgramPtr parseProgram(const SourceCodePtr &source)
TypeDenoterPtr parseTypeDenoterPrimary(StructDeclarationPtr *structDeclaration=nullptr)
Vector< RegisterPtr > parseRegisterList(bool parseFirstColon=true)
Vector< VarDeclarationStatementPtr > parseAnnotationList()
BaseTypeDenoterPtr parseBaseTypeDenoter()
StructDeclarationPtr parseStructDeclaration(bool parseStructtoken=true, const TokenPtr &identifierToken=nullptr)
void generatePreDefinedTypeAliases(Program &ast)
void parseVarDeclarationSemantic(VarDeclaration &varDeclaration, bool allowPackOffset=true)
CallExpressionPtr parseCallExpression(const ObjectExpressionPtr &objectExpression=nullptr, const TypeDenoterPtr &typeDenoter=nullptr)
CallExpressionPtr parseCallExpressionWithPrefixOpt(const ExpressionPtr &prefixExpression=nullptr, bool isStatic=false, const TokenPtr &identifierToken=nullptr)
ProgramPtr parseSource(const SourceCodePtr &source, const NameMangling &nameMangling, const InputShaderVersion versionIn, bool rowMajorAlignment=false, bool enableWarnings=false)
bool mRowMajorAlignment
Definition HLSLParser.hpp:195
ExpressionPtr parseTypeSpecifierOrCallExpression()
BaseTypeDenoterPtr parseBaseMatrixTypeDenoter()
BasicDeclarationStatementPtr parseUniformBufferDeclarationStatement()
CallExpressionPtr parseCallExpressionAsTypeCtor(const TypeDenoterPtr &typeDenoter)
SamplerDeclarationStatementPtr parseSamplerDeclarationStatement(const SamplerTypeDenoterPtr &typeDenoter=nullptr, const TokenPtr &identifierToken=nullptr)
TypeSpecifierPtr parseTypeSpecifier(bool parseVoidType=false)
VoidTypeDenoterPtr ParseVoidTypeDenoter()
ExpressionPtr parseExpressionWithSuffixOpt(ExpressionPtr expression)
StatementPtr parseStatement(bool allowAttributes=true)
ObjectExpressionPtr parseObjectExpression(const ExpressionPtr &expression=nullptr)
AliasDeclarationStatementPtr makeAndRegisterBuildinAlias(const DataType dataType, const String &ident)
VarDeclarationPtr parseVarDeclaration(VarDeclarationStatement *declarationStatementRef, const TokenPtr &identifierToken=nullptr) override
RegisterPtr parseRegister(bool parseColon=true)
TypeDenoterPtr parseTypeDenoter(bool allowVoidType=true, StructDeclarationPtr *structDeclaration=nullptr)
void registerTypeName(const String &ident)
Vector< SamplerValuePtr > parseSamplerValueList()
Vector< AliasDeclarationPtr > parseAliasDeclarationList(TypeDenoterPtr typeDenoter)
VarDeclarationStatementPtr parseParameter() override
BaseTypeDenoterPtr parseBaseVectorTypeDenoter()
BasicDeclarationStatementPtr parseFunctionDeclarationStatement(const TypeSpecifierPtr &returnType=nullptr, const TokenPtr &identifierToken=nullptr)
Vector< BufferDeclarationPtr > parseBufferDeclarationList(BufferDeclarationStatement *declarationStatementRef, const TokenPtr &identifierToken=nullptr)
TypeDenoterPtr parseTypeDenoterWithStructDeclarationOpt(StructDeclarationPtr &structDeclaration, bool allowVoidType=true)
DataType parseDataType(const String &keyword)
StatementPtr parseGlobalStatementWithSamplerTypeDenoter()
StatementPtr parseGlobalStatementWithBufferTypeDenoter()
UniformBufferType parseUniformBufferType()
StructTypeDenoterPtr parseStructTypeDenoterWithStructDeclarationOpt(StructDeclarationPtr &structDeclaration)
StatementPtr parseGlobalStatementWithTypeSpecifier()
FunctionDeclarationPtr parseFunctionDeclaration(BasicDeclarationStatement *declarationStatementRef, const TypeSpecifierPtr &returnType=nullptr, const TokenPtr &identifierToken=nullptr)
CodeBlockPtr parseCodeBlock() override
StatementPtr parseLocalStatement() override
IndexedSemantic parseSemantic(bool parseColon=true)
BufferDeclarationPtr parseBufferDeclaration(BufferDeclarationStatement *declarationStatementRef, const TokenPtr &identifierToken=nullptr)
SamplerTypeDenoterPtr parseSamplerTypeDenoter()
bool mUseD3D10Semantics
Definition HLSLParser.hpp:189
TypeSpecifierPtr makeTypeSpecifierWithPackAlignment()
bool mEnableCgKeywords
Definition HLSLParser.hpp:192
Vector< AttributePtr > parseAttributeList()
SwitchCasePtr parseSwitchCase() override
String parseIdentWithNamespaceOpt(ObjectExpressionPtr &namespaceExpression, TokenPtr identifierToken=nullptr, SourceArea *area=nullptr)
SamplerDeclarationPtr parseSamplerDeclaration(SamplerDeclarationStatement *declarationStatementRef, const TokenPtr &identifierToken=nullptr)
UniformBufferDeclarationPtr parseUniformBufferDeclaration()
Vector< SamplerDeclarationPtr > parseSamplerDeclarationList(SamplerDeclarationStatement *declarationStatementRef, const TokenPtr &identifierToken=nullptr)
ExpressionPtr parsePrimaryExpression() override
TypeSpecifierPtr makeTypeSpecifierIfLhsOfCastExpression(const ExpressionPtr &expression)
TypeNameSymbolTable mTypeNameSymbolTable
Definition HLSLParser.hpp:186
bool isRegisteredTypeName(const String &ident) const
TypeSpecifierExpressionPtr parseTypeSpecifierExpression()
void parseFunctionDeclarationSemantic(FunctionDeclaration &funcDeclaration)
LiteralExpressionPtr parseLiteralExpression()
PackOffsetPtr parsePackOffset(bool parseColon=true)
PostUnaryExpressionPtr parsePostUnaryExpression(const ExpressionPtr &expression)
VarDeclarationStatementPtr parseVarDeclarationStatement()
AliasDeclarationPtr parseAliasDeclaration(TypeDenoterPtr typeDenoter)
AliasTypeDenoterPtr parseAliasTypeDenoter(String ident="")
UnaryExpressionPtr parseUnaryExpression()
void processDirective(const String &ident)
BufferDeclarationStatementPtr parseBufferDeclarationStatement(const BufferTypeDenoterPtr &typeDenoter=nullptr, const TokenPtr &identifierToken=nullptr)
StatementPtr parseForLoopInitializer() override
StructTypeDenoterPtr parseStructTypeDenoter()
bool parseModifiers(TypeSpecifier *typeSpecifier, bool allowPrimitiveType=false)
ExpressionPtr parseObjectOrCallExpression(const ExpressionPtr &expression=nullptr)
Log base class.
Definition Log.hpp:19
Definition SLParser.hpp:28
Definition SourceArea.hpp:20
Definition AST.hpp:33
SPtr< TypeSpecifier > TypeSpecifierPtr
Definition Visitor.hpp:37
SPtr< LiteralExpression > LiteralExpressionPtr
Definition Visitor.hpp:68
SPtr< SamplerDeclarationStatement > SamplerDeclarationStatementPtr
Definition Visitor.hpp:48
SPtr< AliasDeclarationStatement > AliasDeclarationStatementPtr
Definition Visitor.hpp:50
TypeModifier
Definition ASTEnums.hpp:471
SPtr< Expression > ExpressionPtr
Definition Visitor.hpp:26
SPtr< ObjectExpression > ObjectExpressionPtr
Definition Visitor.hpp:76
SPtr< PackOffset > PackOffsetPtr
Definition Visitor.hpp:35
SPtr< AliasTypeDenoter > AliasTypeDenoterPtr
Definition TypeDenoter.hpp:36
InputShaderVersion
Input shader version enumeration.
Definition Targets.hpp:40
SPtr< AliasDeclaration > AliasDeclarationPtr
Definition Visitor.hpp:43
SPtr< SourceCode > SourceCodePtr
Definition SourceCode.hpp:66
SPtr< TypeSpecifierExpression > TypeSpecifierExpressionPtr
Definition Visitor.hpp:69
SPtr< BufferDeclarationStatement > BufferDeclarationStatementPtr
Definition Visitor.hpp:47
SPtr< BaseTypeDenoter > BaseTypeDenoterPtr
Definition TypeDenoter.hpp:32
SPtr< StructDeclaration > StructDeclarationPtr
Definition Visitor.hpp:42
SPtr< VarDeclaration > VarDeclarationPtr
Definition Visitor.hpp:39
InterpModifier
Definition ASTEnums.hpp:453
SPtr< PostUnaryExpression > PostUnaryExpressionPtr
Definition Visitor.hpp:73
SPtr< SamplerDeclaration > SamplerDeclarationPtr
Definition Visitor.hpp:41
SPtr< BufferTypeDenoter > BufferTypeDenoterPtr
Definition TypeDenoter.hpp:33
SPtr< CodeBlock > CodeBlockPtr
Definition Visitor.hpp:30
SPtr< FunctionDeclaration > FunctionDeclarationPtr
Definition Visitor.hpp:44
SPtr< Statement > StatementPtr
Definition Visitor.hpp:25
SPtr< CallExpression > CallExpressionPtr
Definition Visitor.hpp:74
StorageClass
Definition ASTEnums.hpp:441
SPtr< TypeDenoter > TypeDenoterPtr
Definition TypeDenoter.hpp:29
UniformBufferType
Definition ASTEnums.hpp:482
SPtr< BufferDeclaration > BufferDeclarationPtr
Definition Visitor.hpp:40
SPtr< SwitchCase > SwitchCasePtr
Definition Visitor.hpp:32
DataType
Definition ASTEnums.hpp:159
SPtr< SamplerTypeDenoter > SamplerTypeDenoterPtr
Definition TypeDenoter.hpp:34
SPtr< VoidTypeDenoter > VoidTypeDenoterPtr
Definition TypeDenoter.hpp:30
SPtr< AssignExpression > AssignExpressionPtr
Definition Visitor.hpp:77
BufferType
Definition ASTEnums.hpp:492
SPtr< StructTypeDenoter > StructTypeDenoterPtr
Definition TypeDenoter.hpp:35
SPtr< VarDeclarationStatement > VarDeclarationStatementPtr
Definition Visitor.hpp:49
SPtr< UnaryExpression > UnaryExpressionPtr
Definition Visitor.hpp:72
SPtr< UniformBufferDeclaration > UniformBufferDeclarationPtr
Definition Visitor.hpp:45
SPtr< Token > TokenPtr
Definition Token.hpp:174
SPtr< Attribute > AttributePtr
Definition Visitor.hpp:31
PrimitiveType
Definition ASTEnums.hpp:425
SPtr< SamplerValue > SamplerValuePtr
Definition Visitor.hpp:33
SPtr< BasicDeclarationStatement > BasicDeclarationStatementPtr
Definition Visitor.hpp:51
SPtr< Register > RegisterPtr
Definition Visitor.hpp:34
SPtr< Program > ProgramPtr
Definition Visitor.hpp:29
SamplerType
Definition ASTEnums.hpp:571
SPtr< Scanner > ScannerPtr
Definition Scanner.hpp:147
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Name mangling descriptor structure for shader input/output variables (also referred to as "varyings")...
Definition ShaderCompiler.hpp:185