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