OLD | NEW |
| (Empty) |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/v8.h" | |
6 | |
7 #include "src/ast-expression-visitor.h" | |
8 | |
9 #include "src/ast.h" | |
10 #include "src/codegen.h" | |
11 #include "src/scopes.h" | |
12 | |
13 namespace v8 { | |
14 namespace internal { | |
15 | |
16 | |
17 #define RECURSE(call) \ | |
18 do { \ | |
19 DCHECK(!HasStackOverflow()); \ | |
20 call; \ | |
21 if (HasStackOverflow()) return; \ | |
22 } while (false) | |
23 | |
24 | |
25 #define RECURSE_EXPRESSION(call) \ | |
26 do { \ | |
27 DCHECK(!HasStackOverflow()); \ | |
28 ++depth_; \ | |
29 call; \ | |
30 --depth_; \ | |
31 if (HasStackOverflow()) return; \ | |
32 } while (false) | |
33 | |
34 | |
35 AstExpressionVisitor::AstExpressionVisitor(Isolate* isolate, Expression* root) | |
36 : root_(root), depth_(0) { | |
37 InitializeAstVisitor(isolate); | |
38 } | |
39 | |
40 | |
41 AstExpressionVisitor::AstExpressionVisitor(uintptr_t stack_limit, | |
42 Expression* root) | |
43 : root_(root), depth_(0) { | |
44 InitializeAstVisitor(stack_limit); | |
45 } | |
46 | |
47 | |
48 void AstExpressionVisitor::Run() { RECURSE(Visit(root_)); } | |
49 | |
50 | |
51 void AstExpressionVisitor::VisitVariableDeclaration(VariableDeclaration* decl) { | |
52 } | |
53 | |
54 | |
55 void AstExpressionVisitor::VisitFunctionDeclaration(FunctionDeclaration* decl) { | |
56 RECURSE(Visit(decl->fun())); | |
57 } | |
58 | |
59 | |
60 void AstExpressionVisitor::VisitImportDeclaration(ImportDeclaration* decl) {} | |
61 | |
62 | |
63 void AstExpressionVisitor::VisitExportDeclaration(ExportDeclaration* decl) {} | |
64 | |
65 | |
66 void AstExpressionVisitor::VisitStatements(ZoneList<Statement*>* stmts) { | |
67 for (int i = 0; i < stmts->length(); ++i) { | |
68 Statement* stmt = stmts->at(i); | |
69 RECURSE(Visit(stmt)); | |
70 if (stmt->IsJump()) break; | |
71 } | |
72 } | |
73 | |
74 | |
75 void AstExpressionVisitor::VisitBlock(Block* stmt) { | |
76 RECURSE(VisitStatements(stmt->statements())); | |
77 } | |
78 | |
79 | |
80 void AstExpressionVisitor::VisitExpressionStatement(ExpressionStatement* stmt) { | |
81 RECURSE(Visit(stmt->expression())); | |
82 } | |
83 | |
84 | |
85 void AstExpressionVisitor::VisitEmptyStatement(EmptyStatement* stmt) {} | |
86 | |
87 | |
88 void AstExpressionVisitor::VisitSloppyBlockFunctionStatement( | |
89 SloppyBlockFunctionStatement* stmt) { | |
90 RECURSE(Visit(stmt->statement())); | |
91 } | |
92 | |
93 | |
94 void AstExpressionVisitor::VisitIfStatement(IfStatement* stmt) { | |
95 RECURSE(Visit(stmt->condition())); | |
96 RECURSE(Visit(stmt->then_statement())); | |
97 RECURSE(Visit(stmt->else_statement())); | |
98 } | |
99 | |
100 | |
101 void AstExpressionVisitor::VisitContinueStatement(ContinueStatement* stmt) {} | |
102 | |
103 | |
104 void AstExpressionVisitor::VisitBreakStatement(BreakStatement* stmt) {} | |
105 | |
106 | |
107 void AstExpressionVisitor::VisitReturnStatement(ReturnStatement* stmt) { | |
108 RECURSE(Visit(stmt->expression())); | |
109 } | |
110 | |
111 | |
112 void AstExpressionVisitor::VisitWithStatement(WithStatement* stmt) { | |
113 RECURSE(stmt->expression()); | |
114 RECURSE(stmt->statement()); | |
115 } | |
116 | |
117 | |
118 void AstExpressionVisitor::VisitSwitchStatement(SwitchStatement* stmt) { | |
119 RECURSE(Visit(stmt->tag())); | |
120 | |
121 ZoneList<CaseClause*>* clauses = stmt->cases(); | |
122 | |
123 for (int i = 0; i < clauses->length(); ++i) { | |
124 CaseClause* clause = clauses->at(i); | |
125 if (!clause->is_default()) { | |
126 Expression* label = clause->label(); | |
127 RECURSE(Visit(label)); | |
128 } | |
129 ZoneList<Statement*>* stmts = clause->statements(); | |
130 RECURSE(VisitStatements(stmts)); | |
131 } | |
132 } | |
133 | |
134 | |
135 void AstExpressionVisitor::VisitCaseClause(CaseClause* clause) { | |
136 UNREACHABLE(); | |
137 } | |
138 | |
139 | |
140 void AstExpressionVisitor::VisitDoWhileStatement(DoWhileStatement* stmt) { | |
141 RECURSE(Visit(stmt->body())); | |
142 RECURSE(Visit(stmt->cond())); | |
143 } | |
144 | |
145 | |
146 void AstExpressionVisitor::VisitWhileStatement(WhileStatement* stmt) { | |
147 RECURSE(Visit(stmt->cond())); | |
148 RECURSE(Visit(stmt->body())); | |
149 } | |
150 | |
151 | |
152 void AstExpressionVisitor::VisitForStatement(ForStatement* stmt) { | |
153 if (stmt->init() != NULL) { | |
154 RECURSE(Visit(stmt->init())); | |
155 } | |
156 if (stmt->cond() != NULL) { | |
157 RECURSE(Visit(stmt->cond())); | |
158 } | |
159 if (stmt->next() != NULL) { | |
160 RECURSE(Visit(stmt->next())); | |
161 } | |
162 RECURSE(Visit(stmt->body())); | |
163 } | |
164 | |
165 | |
166 void AstExpressionVisitor::VisitForInStatement(ForInStatement* stmt) { | |
167 RECURSE(Visit(stmt->enumerable())); | |
168 RECURSE(Visit(stmt->body())); | |
169 } | |
170 | |
171 | |
172 void AstExpressionVisitor::VisitForOfStatement(ForOfStatement* stmt) { | |
173 RECURSE(Visit(stmt->iterable())); | |
174 RECURSE(Visit(stmt->body())); | |
175 } | |
176 | |
177 | |
178 void AstExpressionVisitor::VisitTryCatchStatement(TryCatchStatement* stmt) { | |
179 RECURSE(Visit(stmt->try_block())); | |
180 RECURSE(Visit(stmt->catch_block())); | |
181 } | |
182 | |
183 | |
184 void AstExpressionVisitor::VisitTryFinallyStatement(TryFinallyStatement* stmt) { | |
185 RECURSE(Visit(stmt->try_block())); | |
186 RECURSE(Visit(stmt->finally_block())); | |
187 } | |
188 | |
189 | |
190 void AstExpressionVisitor::VisitDebuggerStatement(DebuggerStatement* stmt) {} | |
191 | |
192 | |
193 void AstExpressionVisitor::VisitFunctionLiteral(FunctionLiteral* expr) { | |
194 Scope* scope = expr->scope(); | |
195 VisitExpression(expr); | |
196 RECURSE_EXPRESSION(VisitDeclarations(scope->declarations())); | |
197 RECURSE_EXPRESSION(VisitStatements(expr->body())); | |
198 } | |
199 | |
200 | |
201 void AstExpressionVisitor::VisitNativeFunctionLiteral( | |
202 NativeFunctionLiteral* expr) {} | |
203 | |
204 | |
205 void AstExpressionVisitor::VisitDoExpression(DoExpression* expr) { | |
206 RECURSE(VisitBlock(expr->block())); | |
207 RECURSE(VisitVariableProxy(expr->result())); | |
208 } | |
209 | |
210 | |
211 void AstExpressionVisitor::VisitConditional(Conditional* expr) { | |
212 VisitExpression(expr); | |
213 RECURSE_EXPRESSION(Visit(expr->condition())); | |
214 RECURSE_EXPRESSION(Visit(expr->then_expression())); | |
215 RECURSE_EXPRESSION(Visit(expr->else_expression())); | |
216 } | |
217 | |
218 | |
219 void AstExpressionVisitor::VisitVariableProxy(VariableProxy* expr) { | |
220 VisitExpression(expr); | |
221 } | |
222 | |
223 | |
224 void AstExpressionVisitor::VisitLiteral(Literal* expr) { | |
225 VisitExpression(expr); | |
226 } | |
227 | |
228 | |
229 void AstExpressionVisitor::VisitRegExpLiteral(RegExpLiteral* expr) { | |
230 VisitExpression(expr); | |
231 } | |
232 | |
233 | |
234 void AstExpressionVisitor::VisitObjectLiteral(ObjectLiteral* expr) { | |
235 VisitExpression(expr); | |
236 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); | |
237 for (int i = 0; i < props->length(); ++i) { | |
238 ObjectLiteralProperty* prop = props->at(i); | |
239 if (!prop->key()->IsLiteral()) { | |
240 RECURSE_EXPRESSION(Visit(prop->key())); | |
241 } | |
242 RECURSE_EXPRESSION(Visit(prop->value())); | |
243 } | |
244 } | |
245 | |
246 | |
247 void AstExpressionVisitor::VisitArrayLiteral(ArrayLiteral* expr) { | |
248 VisitExpression(expr); | |
249 ZoneList<Expression*>* values = expr->values(); | |
250 for (int i = 0; i < values->length(); ++i) { | |
251 Expression* value = values->at(i); | |
252 RECURSE_EXPRESSION(Visit(value)); | |
253 } | |
254 } | |
255 | |
256 | |
257 void AstExpressionVisitor::VisitAssignment(Assignment* expr) { | |
258 VisitExpression(expr); | |
259 RECURSE_EXPRESSION(Visit(expr->target())); | |
260 RECURSE_EXPRESSION(Visit(expr->value())); | |
261 } | |
262 | |
263 | |
264 void AstExpressionVisitor::VisitYield(Yield* expr) { | |
265 VisitExpression(expr); | |
266 RECURSE_EXPRESSION(Visit(expr->generator_object())); | |
267 RECURSE_EXPRESSION(Visit(expr->expression())); | |
268 } | |
269 | |
270 | |
271 void AstExpressionVisitor::VisitThrow(Throw* expr) { | |
272 VisitExpression(expr); | |
273 RECURSE_EXPRESSION(Visit(expr->exception())); | |
274 } | |
275 | |
276 | |
277 void AstExpressionVisitor::VisitProperty(Property* expr) { | |
278 VisitExpression(expr); | |
279 RECURSE_EXPRESSION(Visit(expr->obj())); | |
280 RECURSE_EXPRESSION(Visit(expr->key())); | |
281 } | |
282 | |
283 | |
284 void AstExpressionVisitor::VisitCall(Call* expr) { | |
285 VisitExpression(expr); | |
286 RECURSE_EXPRESSION(Visit(expr->expression())); | |
287 ZoneList<Expression*>* args = expr->arguments(); | |
288 for (int i = 0; i < args->length(); ++i) { | |
289 Expression* arg = args->at(i); | |
290 RECURSE_EXPRESSION(Visit(arg)); | |
291 } | |
292 } | |
293 | |
294 | |
295 void AstExpressionVisitor::VisitCallNew(CallNew* expr) { | |
296 VisitExpression(expr); | |
297 RECURSE_EXPRESSION(Visit(expr->expression())); | |
298 ZoneList<Expression*>* args = expr->arguments(); | |
299 for (int i = 0; i < args->length(); ++i) { | |
300 Expression* arg = args->at(i); | |
301 RECURSE_EXPRESSION(Visit(arg)); | |
302 } | |
303 } | |
304 | |
305 | |
306 void AstExpressionVisitor::VisitCallRuntime(CallRuntime* expr) { | |
307 VisitExpression(expr); | |
308 ZoneList<Expression*>* args = expr->arguments(); | |
309 for (int i = 0; i < args->length(); ++i) { | |
310 Expression* arg = args->at(i); | |
311 RECURSE_EXPRESSION(Visit(arg)); | |
312 } | |
313 } | |
314 | |
315 | |
316 void AstExpressionVisitor::VisitUnaryOperation(UnaryOperation* expr) { | |
317 VisitExpression(expr); | |
318 RECURSE_EXPRESSION(Visit(expr->expression())); | |
319 } | |
320 | |
321 | |
322 void AstExpressionVisitor::VisitCountOperation(CountOperation* expr) { | |
323 VisitExpression(expr); | |
324 RECURSE_EXPRESSION(Visit(expr->expression())); | |
325 } | |
326 | |
327 | |
328 void AstExpressionVisitor::VisitBinaryOperation(BinaryOperation* expr) { | |
329 VisitExpression(expr); | |
330 RECURSE_EXPRESSION(Visit(expr->left())); | |
331 RECURSE_EXPRESSION(Visit(expr->right())); | |
332 } | |
333 | |
334 | |
335 void AstExpressionVisitor::VisitCompareOperation(CompareOperation* expr) { | |
336 VisitExpression(expr); | |
337 RECURSE_EXPRESSION(Visit(expr->left())); | |
338 RECURSE_EXPRESSION(Visit(expr->right())); | |
339 } | |
340 | |
341 | |
342 void AstExpressionVisitor::VisitThisFunction(ThisFunction* expr) { | |
343 VisitExpression(expr); | |
344 } | |
345 | |
346 | |
347 void AstExpressionVisitor::VisitDeclarations(ZoneList<Declaration*>* decls) { | |
348 for (int i = 0; i < decls->length(); ++i) { | |
349 Declaration* decl = decls->at(i); | |
350 RECURSE(Visit(decl)); | |
351 } | |
352 } | |
353 | |
354 | |
355 void AstExpressionVisitor::VisitClassLiteral(ClassLiteral* expr) { | |
356 VisitExpression(expr); | |
357 if (expr->extends() != nullptr) { | |
358 RECURSE_EXPRESSION(Visit(expr->extends())); | |
359 } | |
360 RECURSE_EXPRESSION(Visit(expr->constructor())); | |
361 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); | |
362 for (int i = 0; i < props->length(); ++i) { | |
363 ObjectLiteralProperty* prop = props->at(i); | |
364 if (!prop->key()->IsLiteral()) { | |
365 RECURSE_EXPRESSION(Visit(prop->key())); | |
366 } | |
367 RECURSE_EXPRESSION(Visit(prop->value())); | |
368 } | |
369 } | |
370 | |
371 | |
372 void AstExpressionVisitor::VisitSpread(Spread* expr) { | |
373 VisitExpression(expr); | |
374 RECURSE_EXPRESSION(Visit(expr->expression())); | |
375 } | |
376 | |
377 | |
378 void AstExpressionVisitor::VisitEmptyParentheses(EmptyParentheses* expr) {} | |
379 | |
380 | |
381 void AstExpressionVisitor::VisitSuperPropertyReference( | |
382 SuperPropertyReference* expr) { | |
383 VisitExpression(expr); | |
384 RECURSE_EXPRESSION(VisitVariableProxy(expr->this_var())); | |
385 RECURSE_EXPRESSION(Visit(expr->home_object())); | |
386 } | |
387 | |
388 | |
389 void AstExpressionVisitor::VisitSuperCallReference(SuperCallReference* expr) { | |
390 VisitExpression(expr); | |
391 RECURSE_EXPRESSION(VisitVariableProxy(expr->this_var())); | |
392 RECURSE_EXPRESSION(VisitVariableProxy(expr->new_target_var())); | |
393 RECURSE_EXPRESSION(VisitVariableProxy(expr->this_function_var())); | |
394 } | |
395 | |
396 | |
397 } // namespace internal | |
398 } // namespace v8 | |
OLD | NEW |