-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstPass.h
215 lines (200 loc) · 8.38 KB
/
AstPass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
#include "Ast.h"
#include "Logger.h"
#include <vector>
#include <type_traits>
#include <memory>
namespace lwscript
{
class AstPass;
template <typename T>
concept IsChildOfAstPass = !std::is_same_v<T, void> &&
!std::is_abstract_v<T> &&
std::is_base_of_v<AstPass, T>;
class LWSCRIPT_API AstPass
{
public:
AstPass() noexcept = default;
virtual ~AstPass() = default;
Stmt *Execute(Stmt *stmt)
{
return ExecuteStmt(stmt);
}
protected:
Stmt *ExecuteStmt(Stmt *stmt)
{
switch (stmt->kind)
{
case AstKind::VAR:
return ExecuteVarStmt((VarStmt *)stmt);
case AstKind::EXPR:
return ExecuteExprStmt((ExprStmt *)stmt);
case AstKind::RETURN:
return ExecuteReturnStmt((ReturnStmt *)stmt);
case AstKind::IF:
return ExecuteIfStmt((IfStmt *)stmt);
case AstKind::SCOPE:
return ExecuteScopeStmt((ScopeStmt *)stmt);
case AstKind::WHILE:
return ExecuteWhileStmt((WhileStmt *)stmt);
case AstKind::BREAK:
return ExecuteBreakStmt((BreakStmt *)stmt);
case AstKind::CONTINUE:
return ExecuteContinueStmt((ContinueStmt *)stmt);
case AstKind::ENUM:
return ExecuteEnumStmt((EnumStmt *)stmt);
case AstKind::FUNCTION:
return ExecuteFunctionStmt((FunctionStmt *)stmt);
case AstKind::CLASS:
return ExecuteClassStmt((ClassStmt *)stmt);
case AstKind::MODULE:
return ExecuteModuleStmt((ModuleStmt *)stmt);
case AstKind::ASTSTMTS:
return ExecuteAstStmts((AstStmts *)stmt);
default:
return stmt;
}
}
virtual Stmt *ExecuteAstStmts(AstStmts *stmt) { return stmt; }
virtual Stmt *ExecuteVarStmt(VarStmt *stmt) { return stmt; }
virtual Stmt *ExecuteExprStmt(ExprStmt *stmt) { return stmt; }
virtual Stmt *ExecuteReturnStmt(ReturnStmt *stmt) { return stmt; }
virtual Stmt *ExecuteIfStmt(IfStmt *stmt) { return stmt; }
virtual Stmt *ExecuteScopeStmt(ScopeStmt *stmt) { return stmt; }
virtual Stmt *ExecuteWhileStmt(WhileStmt *stmt) { return stmt; }
virtual Stmt *ExecuteEnumStmt(EnumStmt *stmt) { return stmt; }
virtual Stmt *ExecuteFunctionStmt(FunctionStmt *stmt) { return stmt; }
virtual Stmt *ExecuteClassStmt(ClassStmt *stmt) { return stmt; }
virtual Stmt *ExecuteBreakStmt(BreakStmt *stmt) { return stmt; }
virtual Stmt *ExecuteContinueStmt(ContinueStmt *stmt) { return stmt; }
virtual Stmt *ExecuteModuleStmt(ModuleStmt *stmt) { return stmt; }
virtual Expr *ExecuteExpr(Expr *expr)
{
switch (expr->kind)
{
case AstKind::LITERAL:
return ExecuteLiteralExpr((LiteralExpr *)expr);
case AstKind::IDENTIFIER:
return ExecuteIdentifierExpr((IdentifierExpr *)expr);
case AstKind::VAR_DESC:
return ExecuteVarArgExpr((VarArgExpr *)expr);
case AstKind::GROUP:
return ExecuteGroupExpr((GroupExpr *)expr);
case AstKind::ARRAY:
return ExecuteArrayExpr((ArrayExpr *)expr);
case AstKind::DICT:
return ExecuteDictExpr((DictExpr *)expr);
case AstKind::PREFIX:
return ExecutePrefixExpr((PrefixExpr *)expr);
case AstKind::INFIX:
return ExecuteInfixExpr((InfixExpr *)expr);
case AstKind::POSTFIX:
return ExecutePostfixExpr((PostfixExpr *)expr);
case AstKind::CONDITION:
return ExecuteConditionExpr((ConditionExpr *)expr);
case AstKind::REF:
return ExecuteRefExpr((RefExpr *)expr);
case AstKind::LAMBDA:
return ExecuteLambdaExpr((LambdaExpr *)expr);
case AstKind::DOT:
return ExecuteDotExpr((DotExpr *)expr);
case AstKind::CALL:
return ExecuteCallExpr((CallExpr *)expr);
case AstKind::NEW:
return ExecuteNewExpr((NewExpr *)expr);
case AstKind::THIS:
return ExecuteThisExpr((ThisExpr *)expr);
case AstKind::BASE:
return ExecuteBaseExpr((BaseExpr *)expr);
case AstKind::COMPOUND:
return ExecuteCompoundExpr((CompoundExpr *)expr);
case AstKind::STRUCT:
return ExecuteStructExpr((StructExpr *)expr);
case AstKind::VAR_ARG:
return ExecuteVarArgExpr((VarArgExpr *)expr);
case AstKind::FACTORIAL:
return ExecuteFactorialExpr((FactorialExpr *)expr);
case AstKind::APPREGATE:
return ExecuteAppregateExpr((AppregateExpr *)expr);
default:
return expr;
}
}
virtual Expr *ExecuteLiteralExpr(LiteralExpr *expr) { return expr; }
virtual Expr *ExecuteIdentifierExpr(IdentifierExpr *expr) { return expr; }
virtual Expr *ExecuteGroupExpr(GroupExpr *expr) { return expr; }
virtual Expr *ExecuteArrayExpr(ArrayExpr *expr) { return expr; }
virtual Expr *ExecuteAppregateExpr(AppregateExpr *expr) { return expr; }
virtual Expr *ExecuteDictExpr(DictExpr *expr) { return expr; }
virtual Expr *ExecutePrefixExpr(PrefixExpr *expr) { return expr; }
virtual Expr *ExecuteInfixExpr(InfixExpr *expr) { return expr; }
virtual Expr *ExecutePostfixExpr(PostfixExpr *expr) { return expr; }
virtual Expr *ExecuteConditionExpr(ConditionExpr *expr) { return expr; }
virtual Expr *ExecuteIndexExpr(IndexExpr *expr) { return expr; }
virtual Expr *ExecuteRefExpr(RefExpr *expr) { return expr; }
virtual Expr *ExecuteStructExpr(StructExpr *expr) { return expr; }
virtual Expr *ExecuteVarArgExpr(VarArgExpr *expr) { return expr; }
virtual Expr *ExecuteLambdaExpr(LambdaExpr *expr) { return expr; }
virtual Expr *ExecuteCompoundExpr(CompoundExpr *expr) { return expr; }
virtual Expr *ExecuteDotExpr(DotExpr *expr) { return expr; }
virtual Expr *ExecuteCallExpr(CallExpr *expr) { return expr; }
virtual Expr *ExecuteNewExpr(NewExpr *expr) { return expr; }
virtual Expr *ExecuteThisExpr(ThisExpr *expr) { return expr; }
virtual Expr *ExecuteBaseExpr(BaseExpr *expr) { return expr; }
virtual Expr *ExecuteFactorialExpr(FactorialExpr *expr) { return expr; }
virtual Expr *ExecuteVarDescExpr(VarDescExpr *expr) { return expr; }
protected:
template <typename T>
requires IsChildOfAstPass<T>
void RequirePass();
private:
friend class AstPassManager;
AstPassManager *mOwner;
};
class AstPassManager
{
public:
constexpr AstPassManager() noexcept = default;
constexpr ~AstPassManager() noexcept = default;
template <typename T, typename... Args>
requires IsChildOfAstPass<T>
AstPassManager *Add(Args &&...params) noexcept
{
for (size_t pos = 0; pos < mPasses.size(); ++pos)
{
if (dynamic_cast<T *>(mPasses[pos].get())) // ignore already exists pass
return this;
}
std::unique_ptr<T> pass = std::make_unique<T>(std::forward<Args>(params)...);
pass->mOwner = this;
mPasses.emplace_back(std::move(pass));
return this;
}
void Execute(Stmt *stmt)
{
for (auto &pass : mPasses)
pass->Execute(stmt);
}
private:
std::vector<std::unique_ptr<AstPass>> mPasses;
friend class AstPass;
template <typename T>
requires IsChildOfAstPass<T>
bool HasPass()
{
for (int32_t pos; pos < mPasses.size(); ++pos)
{
if (dynamic_cast<T *>(mPasses[pos].get()))
return true;
}
return false;
}
};
template <typename T>
requires IsChildOfAstPass<T>
inline void AstPass::RequirePass()
{
if (!mOwner->HasPass<T>())
mOwner->Add<T>();
}
}