CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Converter.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
11
13
16
19
21
26 class Converter : public VisitorTracker {
27 private:
28 // Helper class to handle code injection during traversal.
30 private:
33 std::size_t mIdx = 0;
34
35 public:
38
41
42 // Returns the next statement to visit, or null if there is no more statement.
44
47
48 private:
50
51 void insertStatementAt(const StatementPtr& statement, std::size_t pos);
52 };
53
54 // Symbol table to determine which variables must be renamed (scope rules are different between HLSL and GLSL).
56
57 Program* mProgram = nullptr;
59
60 // Stack with information of the current 'self' parameter of a member function.
62
65
69
70 public:
71 // Converts the specified AST for the target language.
73
74 protected:
75 virtual void convertASTPrimary(Program& program, const ShaderInput& inputDesc, const ShaderOutput& outputDesc) = 0;
76
77 protected: // Symbol table functions
78 // Opens a new scope in the smybol table.
79 void openScope();
80
81 // Closes the current scope in the symbol table.
82 void closeScope();
83
84 // Registers the AST node in the current scope with the specified identifier.
85 void register_(const String& ident);
86
87 // Returns the symbol with the specified identifer which is in the deepest scope, or null if there is no such symbol.
88 [[nodiscard]] bool fetch(const String& ident) const;
89
90 // Returns the symbol with the specified identifer which is in the current scope, or null if there is no such symbol.
91 [[nodiscard]] bool fetchFromCurrentScope(const String& ident) const;
92
93 protected: // Self parameter
96
98
99 protected: // Name mangling
100 // Renames the specified variable declaration with the name mangling temporary-prefix.
102
103 // Renames the specified identifier to "_{ObfuscationCounter}".
105
106 // Renames the identifier of the specified declaration object.
108
109 // Renames the identifiers of the specified input/output variable declarations.
110 void renameIdentOfInOutVarDeclarations(const Vector<VarDeclaration*>& varDeclarations, bool input, bool useSemanticOnly = false);
111
112 // Labels the specified anonymous structure.
114
115 protected: // Code injection
116 // Visits the specified statements, and allows insertion of further statements (i.e. replace the single statement by a code block statement).
117 void visitScopedStatement(StatementPtr& statement, void* args = nullptr);
118
119 // Visits the specified list of statements, and allows insertion of further statements.
121
122 // Inserts the specified statement before the current statement.
124
125 // Inserts the specified statement after the current statement.
127
128 // Moves all structure declaration within the specified statement list into the respective upper scope.
130
131 protected: // Misc
132 // Returns true if the variable is a global input/output variable declaration.
133 bool isGlobalInOutVarDeclaration(const VarDeclaration* varDeclaration) const;
134
135 // Returns true if the specified type denoter is a sampler state type.
136 [[nodiscard]] bool isSamplerStateTypeDenoter(const TypeDenoterPtr& typeDenoter) const;
137
138 // Removes all statements that are marked as dead code.
140
141 // Returns an identifier for a new temporary variable.
143
144 // Returns the program AST root node.
145 [[nodiscard]] inline Program* getProgram() const { return mProgram; }
146
147 // Returns the name mangling settings.
148 [[nodiscard]] inline const NameMangling& getNameMangling() const { return mNameMangling; }
149
150 private:
152
154 };
155} // namespace CeresEngine::ShaderCompiler
Vector< StatementPtr > * mStatementList
Definition Converter.hpp:32
StatementScopeHandler & operator=(const StatementScopeHandler &)=default
StatementScopeHandler(const StatementScopeHandler &)=default
StatementScopeHandler(Vector< StatementPtr > &statements)
StatementPtr * mStatement
Definition Converter.hpp:31
void insertStatementAt(const StatementPtr &statement, std::size_t pos)
void insertStatementBefore(const StatementPtr &statement)
void insertStatementAfter(const StatementPtr &statement)
GLSL AST converter.
Definition Converter.hpp:26
UInt32 mObfuscationCounter
Definition Converter.hpp:67
void removeDeadCode(Vector< StatementPtr > &statements)
bool fetch(const String &ident) const
void visitScopedStatementsFromHandler(const StatementScopeHandler &handler, void *args)
bool isGlobalInOutVarDeclaration(const VarDeclaration *varDeclaration) const
void renameIdentOfInOutVarDeclarations(const Vector< VarDeclaration * > &varDeclarations, bool input, bool useSemanticOnly=false)
void labelAnonymousDeclaration(Declaration *declarationObj)
bool fetchFromCurrentScope(const String &ident) const
virtual void convertASTPrimary(Program &program, const ShaderInput &inputDesc, const ShaderOutput &outputDesc)=0
void insertStatementAfter(const StatementPtr &statement, bool globalScope=false)
void renameIdentOf(Declaration *obj)
Program * mProgram
Definition Converter.hpp:57
Program * getProgram() const
Definition Converter.hpp:145
const NameMangling & getNameMangling() const
Definition Converter.hpp:148
bool isSamplerStateTypeDenoter(const TypeDenoterPtr &typeDenoter) const
void visitScopedStatement(StatementPtr &statement, void *args=nullptr)
void renameIdentObfuscated(Identifier &ident)
void pushSelfParameter(VarDeclaration *parameter)
UInt32 mAnonymCounter
Definition Converter.hpp:66
StatementScopeHandler * mStatementScopeHandlerGlobalRef
Definition Converter.hpp:64
Vector< VarDeclaration * > mSelfParamStack
Definition Converter.hpp:61
UInt32 mTempVarCounter
Definition Converter.hpp:68
void insertStatementBefore(const StatementPtr &statement, bool globalScope=false)
StatementScopeHandler & getActiveStatementScopeHandler()
void moveNestedStructDeclarations(Vector< StatementPtr > &localStatements, bool globalScope=false)
void visitScopedStatementList(Vector< StatementPtr > &statementList, void *args=nullptr)
void renameIdent(Identifier &ident)
SymbolTable< bool > mSymTable
Definition Converter.hpp:55
void register_(const String &ident)
NameMangling mNameMangling
Definition Converter.hpp:58
Stack< StatementScopeHandler > mStatementScopeHandlerStack
Definition Converter.hpp:63
bool convertAST(Program &program, const ShaderInput &inputDesc, const ShaderOutput &outputDesc)
VarDeclaration * getActiveSelfParameter() const
Class to manage identifiers that can be renamed (maybe several times), to keep track of the original ...
Definition Identifier.hpp:18
Definition SymbolTable.hpp:43
Definition VisitorTracker.hpp:18
Definition AST.hpp:33
SPtr< Statement > StatementPtr
Definition Visitor.hpp:25
SPtr< TypeDenoter > TypeDenoterPtr
Definition TypeDenoter.hpp:29
std::stack< T, Container > Stack
The Stack class is a container adapter that gives the programmer the functionality of a stack - speci...
Definition Stack.hpp:18
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
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
Name mangling descriptor structure for shader input/output variables (also referred to as "varyings")...
Definition ShaderCompiler.hpp:185
Shader input descriptor structure.
Definition ShaderCompiler.hpp:223
Shader output descriptor structure.
Definition ShaderCompiler.hpp:293