CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Analyzer.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
14
18
22
24
26
27 struct StructTypeDenoter;
28
29 // Context analyzer base class.
30 class Analyzer : protected VisitorTracker {
31 private:
34
36
38
39 public:
40 Analyzer(Log* log = nullptr);
41
43
44 protected:
46
47 virtual void decorateASTPrimary(Program& program, const ShaderInput& inputDesc, const ShaderOutput& outputDesc) = 0;
48
49 protected: // Report and error handling
50 void submitReport(bool isError, const String& msg, const AST* ast = nullptr, const Vector<const AST*>& astAppendices = {});
51
52 void error(const String& msg, const AST* ast = nullptr, const Vector<const AST*>& astAppendices = {});
53 void errorUndeclaredIdent(const String& ident, const AST* ast = nullptr);
54 void errorUndeclaredIdent(const String& ident, const String& contextName, const AST* ast = nullptr);
55 void errorUndeclaredIdent(const String& ident, const String& contextName, const String& similarIdent, const AST* ast = nullptr);
56 void errorInternal(const String& msg, const AST* ast = nullptr);
57
58 void warning(const String& msg, const AST* ast = nullptr);
60
61 // Returns the report handler.
63
64 // Returns true if the specified warnings flags are enabled.
65 [[nodiscard]] bool isWarningEnabled(UInt32 flags) const;
66
67 protected: // Symbol table functions
68 // Opens a new scope in the smybol table.
69 void openScope();
70
71 // Closes the current scope in the symbol table.
72 void closeScope();
73
74 // Registers the AST node in the current scope with the specified identifier.
75 void register_(const String& ident, AST* ast);
76
77 // Tries to fetch an AST node with the specified identifier from the symbol table and reports an error on failure.
78 AST* fetch(const String& ident, const AST* ast = nullptr);
79
80 // Tries to fetch an AST node with the specified identifier from the current scope of the symbol table and returns null on failure.
82
83 // Tries to fetch a declaration node with the specified identifier from the symbol table and reports an error on failure.
84 Declaration* fetchDeclaration(const String& ident, const AST* ast = nullptr);
85
86 // Tries to fetch a 'StructDecl' or 'AliasDecl' with the specified identifier from the symbol table and reports an error on failure.
87 Declaration* fetchType(const String& ident, const AST* ast = nullptr);
88
89 // Tries to fetch a 'VarDecl' with the specified identifier from the symbol table and reports an error on failure.
90 VarDeclaration* fetchVarDeclaration(const String& ident, const AST* ast = nullptr);
91
92 // Tries to fetch a 'FunctionDecl' with the specified identifier and arguments from the symbol table and reports an error on failure.
93 FunctionDeclaration* fetchFunctionDeclaration(const String& ident, const Vector<ExpressionPtr>& args, const AST* ast = nullptr);
94
95 // Tries to fetch a 'FunctionDecl' with the specified identifier from the symbol table and reports an error on failure (used for patch-constant-function).
96 FunctionDeclaration* fetchFunctionDeclaration(const String& ident, const AST* ast = nullptr);
97
98 // Tries to fetch a 'VarDecl' with the specified identifier from the structure type denoter and reports an error on failure.
100
102 const AST* ast = nullptr);
103
106
107 // Tries to find a type compatible structure declaration within the current scope.
109
110 //TODO: maybe replace this by "VisitorTracker::insideGlobalScope"
111 // Returns true if the visitor is currently inside the global scope (i.e. out of any function declaration).
113
114 protected: // Analyzer functions
115 void analyzeTypeDenoter(TypeDenoterPtr& typeDenoter, const AST* ast);
118 void analyzeAliasTypeDenoter(TypeDenoterPtr& typeDenoter, const AST* ast);
119
122
125
127
128 // Validates the implicit type cast from the source type denoter to the destination type denoter.
131
133
134 protected: // Const-expression evaluation
135 // Evaluates the specified constant expression.
137
138 // Evaluates the specified constant object expression or throws the expression if it's not constant.
140
141 // Tries to evaluate the specified constant expression, or returns the default value on failure.
142 Variant evaluateOrDefault(Expression& expression, const Variant& defaultValue = {});
143
144 // Evaluates the specified constant integer expression.
146
147 // Evaluates the specified constant floating-point expression.
149
150 private:
152
153 // Tries to find a similar identifier in the following order: symbol table, structure (if enabled).
154 String fetchSimilarIdent(const String& ident, StructDeclaration* structDeclaration = nullptr) const;
155
156 // Callback for the symbol table when a symbol is realsed from its scope.
158 };
159
160} // namespace CeresEngine::ShaderCompiler
Definition Flags.hpp:235
Definition Analyzer.hpp:30
Variant evaluateOrDefault(Expression &expression, const Variant &defaultValue={})
ReportHandler & getReportHandler()
Definition Analyzer.hpp:62
StructDeclaration * findCompatibleStructDeclaration(const StructDeclaration &rhs)
AST * fetch(const String &ident, const AST *ast=nullptr)
void error(const String &msg, const AST *ast=nullptr, const Vector< const AST * > &astAppendices={})
Variant evaluateConstExpression(Expression &expression)
TypeDenoterPtr getTypeDenoterFrom(TypedAST *ast, const TypeDenoter *expectedTypeDenoter=nullptr)
Int32 evaluateConstExpressionInt(Expression &expression)
void analyzeBufferTypeDenoter(BufferTypeDenoter &bufferTypeDen, const AST *ast)
ASTSymbolTable::OnOverrideProc OnOverrideProc
Definition Analyzer.hpp:45
void validateTypeCast(const TypeDenoter &sourceTypeDen, const TypeDenoter &destTypeDen, const String &contextDesc, const AST *ast=nullptr)
VarDeclaration * fetchVarDeclarationFromStruct(const StructTypeDenoter &structTypeDenoter, const String &ident, const AST *ast=nullptr)
void onReleaseSymbol(const ASTSymbolOverloadPtr &symbol)
void analyzeAliasTypeDenoter(TypeDenoterPtr &typeDenoter, const AST *ast)
FunctionDeclaration * fetchFunctionDeclaration(const String &ident, const AST *ast=nullptr)
ReportHandler mReportHandler
Definition Analyzer.hpp:32
void errorUndeclaredIdent(const String &ident, const AST *ast=nullptr)
void analyzeTypeDenoter(TypeDenoterPtr &typeDenoter, const AST *ast)
StructDeclaration * fetchStructDeclarationFromIdent(const String &ident, const AST *ast=nullptr)
bool decorateAST(Program &program, const ShaderInput &inputDesc, const ShaderOutput &outputDesc)
float evaluateConstExpressionFloat(Expression &expression)
bool isWarningEnabled(UInt32 flags) const
void analyzeFunctionControlPath(FunctionDeclaration &funcDeclaration)
void errorUndeclaredIdent(const String &ident, const String &contextName, const String &similarIdent, const AST *ast=nullptr)
String fetchSimilarIdent(const String &ident, StructDeclaration *structDeclaration=nullptr) const
void errorUndeclaredIdent(const String &ident, const String &contextName, const AST *ast=nullptr)
void analyzeConditionalExpression(Expression *expression)
void submitReport(bool isError, const String &msg, const AST *ast=nullptr, const Vector< const AST * > &astAppendices={})
Declaration * fetchDeclaration(const String &ident, const AST *ast=nullptr)
AST * fetchFromCurrentScopeOrNull(const String &ident) const
RawFlags mWarnings
Definition Analyzer.hpp:37
FunctionDeclaration * fetchFunctionDeclaration(const String &ident, const Vector< ExpressionPtr > &args, const AST *ast=nullptr)
void warning(const String &msg, const AST *ast=nullptr)
void analyzeFunctionEndOfScopes(const FunctionDeclaration &funcDeclaration)
void analyzeStructTypeDenoter(StructTypeDenoter &structTypeDen, const AST *ast)
void errorInternal(const String &msg, const AST *ast=nullptr)
Variant evaluateConstExpressionObject(const ObjectExpression &expression)
void warningOnNullStatement(const StatementPtr &ast, const String &statementTypeName)
void analyzeTypeSpecifierForParameter(const TypeSpecifier *typeSpecifier)
StructDeclaration * fetchStructDeclarationFromTypeDenoter(const TypeDenoter &typeDenoter)
FunctionDeclaration * fetchFunctionDeclarationFromStruct(const StructTypeDenoter &structTypeDenoter, const String &ident, const Vector< ExpressionPtr > &args, const AST *ast=nullptr)
bool collectArgumentTypeDenoters(const Vector< ExpressionPtr > &args, Vector< TypeDenoterPtr > &argTypeDens)
Declaration * fetchType(const String &ident, const AST *ast=nullptr)
void analyzeTypeSpecifier(TypeSpecifier *typeSpecifier)
ASTSymbolOverloadTable mSymTable
Definition Analyzer.hpp:35
void validateTypeCastFrom(TypedAST *sourceAST, TypedAST *destAST, const String &contextDesc)
VarDeclaration * fetchVarDeclaration(const String &ident, const AST *ast=nullptr)
void register_(const String &ident, AST *ast)
virtual void decorateASTPrimary(Program &program, const ShaderInput &inputDesc, const ShaderOutput &outputDesc)=0
SourceCode * mSourceCode
Definition Analyzer.hpp:33
Log base class.
Definition Log.hpp:19
Definition ReportHandler.hpp:31
Definition SourceCode.hpp:24
UniqueFunction< bool(SymbolType &prevSymbol) const > OnOverrideProc
Definition SymbolTable.hpp:46
Definition Variant.hpp:20
Definition VisitorTracker.hpp:18
Definition AST.hpp:33
SPtr< Statement > StatementPtr
Definition Visitor.hpp:25
SPtr< TypeDenoter > TypeDenoterPtr
Definition TypeDenoter.hpp:29
SPtr< ASTSymbolOverload > ASTSymbolOverloadPtr
Definition SymbolTable.hpp:259
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
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
Buffer type denoter with generic sub type and generic size.
Definition TypeDenoter.hpp:265
Shader input descriptor structure.
Definition ShaderCompiler.hpp:223
Shader output descriptor structure.
Definition ShaderCompiler.hpp:293
Definition TypeDenoter.hpp:82