Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: src/ast/ast-expression-rewriter.cc

Issue 1565153002: Add a generic mechanism for expression rewriting (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/ast/ast.h"
6 #include "src/ast/ast-expression-rewriter.h"
7
8 namespace v8 {
9 namespace internal {
10
11 // ----------------------------------------------------------------------------
12 // Implementation of AstExpressionRewriter
13 // The AST is traversed but no actual rewriting takes place, unless the
14 // Visit methods are overriden in subclasses.
15
16 #define REWRITE_THIS(node) \
17 do { \
18 if (!RewriteExpression(node)) return; \
19 } while (false)
20 #define NOTHING() DCHECK_NULL(replacement_)
21
22
23 void AstExpressionRewriter::VisitDeclarations(
24 ZoneList<Declaration*>* declarations) {
25 for (int i = 0; i < declarations->length(); i++) {
26 AST_REWRITE(Declaration, declarations->at(i),
27 declarations->Set(i, replacement));
28 }
29 }
30
31
32 void AstExpressionRewriter::VisitStatements(ZoneList<Statement*>* statements) {
33 for (int i = 0; i < statements->length(); i++) {
34 AST_REWRITE(Statement, statements->at(i), statements->Set(i, replacement));
35 // Not stopping when a jump statement is found.
36 }
37 }
38
39
40 void AstExpressionRewriter::VisitExpressions(
41 ZoneList<Expression*>* expressions) {
42 for (int i = 0; i < expressions->length(); i++) {
43 // The variable statement visiting code may pass NULL expressions
44 // to this code. Maybe this should be handled by introducing an
45 // undefined expression or literal? Revisit this code if this
46 // changes
47 Expression* expression = expressions->at(i);
48 if (expression != nullptr) {
49 AST_REWRITE(Expression, expression, expressions->Set(i, replacement));
50 }
51 }
52 }
53
54
55 void AstExpressionRewriter::VisitVariableDeclaration(
56 VariableDeclaration* node) {
57 // Not visiting `proxy_`.
58 NOTHING();
59 }
60
61
62 void AstExpressionRewriter::VisitFunctionDeclaration(
63 FunctionDeclaration* node) {
64 // Not visiting `proxy_`.
65 AST_REWRITE(FunctionLiteral, node->fun(), node->set_fun(replacement));
66 }
67
68
69 void AstExpressionRewriter::VisitImportDeclaration(ImportDeclaration* node) {
70 // Not visiting `proxy_`.
71 NOTHING();
72 }
73
74
75 void AstExpressionRewriter::VisitExportDeclaration(ExportDeclaration* node) {
76 // Not visiting `proxy_`.
77 NOTHING();
78 }
79
80
81 void AstExpressionRewriter::VisitBlock(Block* node) {
82 VisitStatements(node->statements());
83 }
84
85
86 void AstExpressionRewriter::VisitExpressionStatement(
87 ExpressionStatement* node) {
88 AST_REWRITE(Expression, node->expression(),
89 node->set_expression(replacement));
90 }
91
92
93 void AstExpressionRewriter::VisitEmptyStatement(EmptyStatement* node) {
94 NOTHING();
95 }
96
97
98 void AstExpressionRewriter::VisitSloppyBlockFunctionStatement(
99 SloppyBlockFunctionStatement* node) {
100 AST_REWRITE(Statement, node->statement(), node->set_statement(replacement));
101 }
102
103
104 void AstExpressionRewriter::VisitIfStatement(IfStatement* node) {
105 AST_REWRITE(Expression, node->condition(), node->set_condition(replacement));
106 AST_REWRITE(Statement, node->then_statement(),
107 node->set_then_statement(replacement));
108 AST_REWRITE(Statement, node->else_statement(),
109 node->set_else_statement(replacement));
110 }
111
112
113 void AstExpressionRewriter::VisitContinueStatement(ContinueStatement* node) {
114 NOTHING();
115 }
116
117
118 void AstExpressionRewriter::VisitBreakStatement(BreakStatement* node) {
119 NOTHING();
120 }
121
122
123 void AstExpressionRewriter::VisitReturnStatement(ReturnStatement* node) {
124 AST_REWRITE(Expression, node->expression(),
125 node->set_expression(replacement));
126 }
127
128
129 void AstExpressionRewriter::VisitWithStatement(WithStatement* node) {
130 AST_REWRITE(Expression, node->expression(),
131 node->set_expression(replacement));
132 AST_REWRITE(Statement, node->statement(), node->set_statement(replacement));
133 }
134
135
136 void AstExpressionRewriter::VisitSwitchStatement(SwitchStatement* node) {
137 AST_REWRITE(Expression, node->tag(), node->set_tag(replacement));
138 ZoneList<CaseClause*>* clauses = node->cases();
139 for (int i = 0; i < clauses->length(); i++) {
140 AST_REWRITE(CaseClause, clauses->at(i), clauses->Set(i, replacement));
141 }
142 }
143
144
145 void AstExpressionRewriter::VisitDoWhileStatement(DoWhileStatement* node) {
146 AST_REWRITE(Expression, node->cond(), node->set_cond(replacement));
147 AST_REWRITE(Statement, node->body(), node->set_body(replacement));
148 }
149
150
151 void AstExpressionRewriter::VisitWhileStatement(WhileStatement* node) {
152 AST_REWRITE(Expression, node->cond(), node->set_cond(replacement));
153 AST_REWRITE(Statement, node->body(), node->set_body(replacement));
154 }
155
156
157 void AstExpressionRewriter::VisitForStatement(ForStatement* node) {
158 if (node->init() != nullptr) {
159 AST_REWRITE(Statement, node->init(), node->set_init(replacement));
160 }
161 if (node->cond() != nullptr) {
162 AST_REWRITE(Expression, node->cond(), node->set_cond(replacement));
163 }
164 if (node->next() != nullptr) {
165 AST_REWRITE(Statement, node->next(), node->set_next(replacement));
166 }
167 AST_REWRITE(Statement, node->body(), node->set_body(replacement));
168 }
169
170
171 void AstExpressionRewriter::VisitForInStatement(ForInStatement* node) {
172 AST_REWRITE(Expression, node->each(), node->set_each(replacement));
173 AST_REWRITE(Expression, node->subject(), node->set_subject(replacement));
174 AST_REWRITE(Statement, node->body(), node->set_body(replacement));
175 }
176
177
178 void AstExpressionRewriter::VisitForOfStatement(ForOfStatement* node) {
179 AST_REWRITE(Expression, node->each(), node->set_each(replacement));
180 AST_REWRITE(Expression, node->assign_iterator(),
181 node->set_assign_iterator(replacement));
182 AST_REWRITE(Expression, node->next_result(),
183 node->set_next_result(replacement));
184 AST_REWRITE(Expression, node->result_done(),
185 node->set_result_done(replacement));
186 AST_REWRITE(Expression, node->assign_each(),
187 node->set_assign_each(replacement));
188 AST_REWRITE(Expression, node->subject(), node->set_subject(replacement));
189 AST_REWRITE(Statement, node->body(), node->set_body(replacement));
190 }
191
192
193 void AstExpressionRewriter::VisitTryCatchStatement(TryCatchStatement* node) {
194 AST_REWRITE(Block, node->try_block(), node->set_try_block(replacement));
195 // Not visiting the variable.
196 AST_REWRITE(Block, node->catch_block(), node->set_catch_block(replacement));
197 }
198
199
200 void AstExpressionRewriter::VisitTryFinallyStatement(
201 TryFinallyStatement* node) {
202 AST_REWRITE(Block, node->try_block(), node->set_try_block(replacement));
203 AST_REWRITE(Block, node->finally_block(),
204 node->set_finally_block(replacement));
205 }
206
207
208 void AstExpressionRewriter::VisitDebuggerStatement(DebuggerStatement* node) {
209 NOTHING();
210 }
211
212
213 void AstExpressionRewriter::VisitFunctionLiteral(FunctionLiteral* node) {
214 REWRITE_THIS(node);
215 VisitDeclarations(node->scope()->declarations());
216 ZoneList<Statement*>* body = node->body();
217 if (body != nullptr) VisitStatements(body);
218 }
219
220
221 void AstExpressionRewriter::VisitClassLiteral(ClassLiteral* node) {
222 REWRITE_THIS(node);
223 // Not visiting `class_variable_proxy_`.
224 Expression* extends = node->extends();
225 if (extends != nullptr) {
226 AST_REWRITE(Expression, extends, node->set_extends(replacement));
227 }
228 AST_REWRITE(FunctionLiteral, node->constructor(),
229 node->set_constructor(replacement));
230 ZoneList<typename ClassLiteral::Property*>* properties = node->properties();
231 for (int i = 0; i < properties->length(); i++) {
232 VisitObjectLiteralProperty(properties->at(i));
233 }
234 }
235
236
237 void AstExpressionRewriter::VisitNativeFunctionLiteral(
238 NativeFunctionLiteral* node) {
239 REWRITE_THIS(node);
240 NOTHING();
241 }
242
243
244 void AstExpressionRewriter::VisitConditional(Conditional* node) {
245 REWRITE_THIS(node);
246 AST_REWRITE(Expression, node->condition(), node->set_condition(replacement));
247 AST_REWRITE(Expression, node->then_expression(),
248 node->set_then_expression(replacement));
249 AST_REWRITE(Expression, node->else_expression(),
250 node->set_else_expression(replacement));
251 }
252
253
254 void AstExpressionRewriter::VisitVariableProxy(VariableProxy* node) {
255 REWRITE_THIS(node);
256 NOTHING();
257 }
258
259
260 void AstExpressionRewriter::VisitLiteral(Literal* node) {
261 REWRITE_THIS(node);
262 NOTHING();
263 }
264
265
266 void AstExpressionRewriter::VisitRegExpLiteral(RegExpLiteral* node) {
267 REWRITE_THIS(node);
268 NOTHING();
269 }
270
271
272 void AstExpressionRewriter::VisitObjectLiteral(ObjectLiteral* node) {
273 REWRITE_THIS(node);
274 ZoneList<typename ObjectLiteral::Property*>* properties = node->properties();
275 for (int i = 0; i < properties->length(); i++) {
276 VisitObjectLiteralProperty(properties->at(i));
277 }
278 }
279
280
281 void AstExpressionRewriter::VisitObjectLiteralProperty(
282 ObjectLiteralProperty* property) {
283 if (property == nullptr) return;
284 AST_REWRITE(Expression, property->key(), property->set_key(replacement));
285 AST_REWRITE(Expression, property->value(), property->set_value(replacement));
286 }
287
288
289 void AstExpressionRewriter::VisitArrayLiteral(ArrayLiteral* node) {
290 REWRITE_THIS(node);
291 VisitExpressions(node->values());
292 }
293
294
295 void AstExpressionRewriter::VisitAssignment(Assignment* node) {
296 REWRITE_THIS(node);
297 AST_REWRITE(Expression, node->target(), node->set_target(replacement));
298 AST_REWRITE(Expression, node->value(), node->set_value(replacement));
299 }
300
301
302 void AstExpressionRewriter::VisitYield(Yield* node) {
303 REWRITE_THIS(node);
304 AST_REWRITE(Expression, node->generator_object(),
305 node->set_generator_object(replacement));
306 AST_REWRITE(Expression, node->expression(),
307 node->set_expression(replacement));
308 }
309
310
311 void AstExpressionRewriter::VisitThrow(Throw* node) {
312 REWRITE_THIS(node);
313 AST_REWRITE(Expression, node->exception(), node->set_exception(replacement));
314 }
315
316
317 void AstExpressionRewriter::VisitProperty(Property* node) {
318 REWRITE_THIS(node);
319 if (node == nullptr) return;
320 AST_REWRITE(Expression, node->obj(), node->set_obj(replacement));
321 AST_REWRITE(Expression, node->key(), node->set_key(replacement));
322 }
323
324
325 void AstExpressionRewriter::VisitCall(Call* node) {
326 REWRITE_THIS(node);
327 AST_REWRITE(Expression, node->expression(),
328 node->set_expression(replacement));
329 VisitExpressions(node->arguments());
330 }
331
332
333 void AstExpressionRewriter::VisitCallNew(CallNew* node) {
334 REWRITE_THIS(node);
335 AST_REWRITE(Expression, node->expression(),
336 node->set_expression(replacement));
337 VisitExpressions(node->arguments());
338 }
339
340
341 void AstExpressionRewriter::VisitCallRuntime(CallRuntime* node) {
342 REWRITE_THIS(node);
343 VisitExpressions(node->arguments());
344 }
345
346
347 void AstExpressionRewriter::VisitUnaryOperation(UnaryOperation* node) {
348 REWRITE_THIS(node);
349 AST_REWRITE(Expression, node->expression(),
350 node->set_expression(replacement));
351 }
352
353
354 void AstExpressionRewriter::VisitCountOperation(CountOperation* node) {
355 REWRITE_THIS(node);
356 AST_REWRITE(Expression, node->expression(),
357 node->set_expression(replacement));
358 }
359
360
361 void AstExpressionRewriter::VisitBinaryOperation(BinaryOperation* node) {
362 REWRITE_THIS(node);
363 AST_REWRITE(Expression, node->left(), node->set_left(replacement));
364 AST_REWRITE(Expression, node->right(), node->set_right(replacement));
365 }
366
367
368 void AstExpressionRewriter::VisitCompareOperation(CompareOperation* node) {
369 REWRITE_THIS(node);
370 AST_REWRITE(Expression, node->left(), node->set_left(replacement));
371 AST_REWRITE(Expression, node->right(), node->set_right(replacement));
372 }
373
374
375 void AstExpressionRewriter::VisitSpread(Spread* node) {
376 REWRITE_THIS(node);
377 AST_REWRITE(Expression, node->expression(),
378 node->set_expression(replacement));
379 }
380
381
382 void AstExpressionRewriter::VisitThisFunction(ThisFunction* node) {
383 REWRITE_THIS(node);
384 NOTHING();
385 }
386
387
388 void AstExpressionRewriter::VisitSuperPropertyReference(
389 SuperPropertyReference* node) {
390 REWRITE_THIS(node);
391 AST_REWRITE(VariableProxy, node->this_var(), node->set_this_var(replacement));
392 AST_REWRITE(Expression, node->home_object(),
393 node->set_home_object(replacement));
394 }
395
396
397 void AstExpressionRewriter::VisitSuperCallReference(SuperCallReference* node) {
398 REWRITE_THIS(node);
399 AST_REWRITE(VariableProxy, node->this_var(), node->set_this_var(replacement));
400 AST_REWRITE(VariableProxy, node->new_target_var(),
401 node->set_new_target_var(replacement));
402 AST_REWRITE(VariableProxy, node->this_function_var(),
403 node->set_this_function_var(replacement));
404 }
405
406
407 void AstExpressionRewriter::VisitCaseClause(CaseClause* node) {
408 if (!node->is_default()) {
409 AST_REWRITE(Expression, node->label(), node->set_label(replacement));
410 }
411 VisitStatements(node->statements());
412 }
413
414
415 void AstExpressionRewriter::VisitEmptyParentheses(EmptyParentheses* node) {
416 NOTHING();
417 }
418
419
420 void AstExpressionRewriter::VisitDoExpression(DoExpression* node) {
421 REWRITE_THIS(node);
422 AST_REWRITE(Block, node->block(), node->set_block(replacement));
423 AST_REWRITE(VariableProxy, node->result(), node->set_result(replacement));
424 }
425
426
427 void AstExpressionRewriter::VisitRewritableAssignmentExpression(
428 RewritableAssignmentExpression* node) {
429 REWRITE_THIS(node);
430 AST_REWRITE(Expression, node->expression(),
431 node->set_expression(replacement));
432 }
433
434
435 } // namespace internal
436 } // namespace v8
OLDNEW
« src/ast/ast.h ('K') | « src/ast/ast-expression-rewriter.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698