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-literal-reindexer.h"
6 
7 #include "src/ast/ast.h"
8 #include "src/ast/scopes.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 
VisitVariableDeclaration(VariableDeclaration * node)14 void AstLiteralReindexer::VisitVariableDeclaration(VariableDeclaration* node) {
15   VisitVariableProxy(node->proxy());
16 }
17 
18 
VisitEmptyStatement(EmptyStatement * node)19 void AstLiteralReindexer::VisitEmptyStatement(EmptyStatement* node) {}
20 
21 
VisitSloppyBlockFunctionStatement(SloppyBlockFunctionStatement * node)22 void AstLiteralReindexer::VisitSloppyBlockFunctionStatement(
23     SloppyBlockFunctionStatement* node) {
24   Visit(node->statement());
25 }
26 
27 
VisitContinueStatement(ContinueStatement * node)28 void AstLiteralReindexer::VisitContinueStatement(ContinueStatement* node) {}
29 
30 
VisitBreakStatement(BreakStatement * node)31 void AstLiteralReindexer::VisitBreakStatement(BreakStatement* node) {}
32 
33 
VisitDebuggerStatement(DebuggerStatement * node)34 void AstLiteralReindexer::VisitDebuggerStatement(DebuggerStatement* node) {}
35 
36 
VisitNativeFunctionLiteral(NativeFunctionLiteral * node)37 void AstLiteralReindexer::VisitNativeFunctionLiteral(
38     NativeFunctionLiteral* node) {}
39 
40 
VisitDoExpression(DoExpression * node)41 void AstLiteralReindexer::VisitDoExpression(DoExpression* node) {
42   Visit(node->block());
43   Visit(node->result());
44 }
45 
46 
VisitLiteral(Literal * node)47 void AstLiteralReindexer::VisitLiteral(Literal* node) {}
48 
49 
VisitRegExpLiteral(RegExpLiteral * node)50 void AstLiteralReindexer::VisitRegExpLiteral(RegExpLiteral* node) {
51   UpdateIndex(node);
52 }
53 
54 
VisitVariableProxy(VariableProxy * node)55 void AstLiteralReindexer::VisitVariableProxy(VariableProxy* node) {}
56 
57 
VisitThisFunction(ThisFunction * node)58 void AstLiteralReindexer::VisitThisFunction(ThisFunction* node) {}
59 
60 
VisitSuperPropertyReference(SuperPropertyReference * node)61 void AstLiteralReindexer::VisitSuperPropertyReference(
62     SuperPropertyReference* node) {
63   Visit(node->this_var());
64   Visit(node->home_object());
65 }
66 
67 
VisitSuperCallReference(SuperCallReference * node)68 void AstLiteralReindexer::VisitSuperCallReference(SuperCallReference* node) {
69   Visit(node->this_var());
70   Visit(node->new_target_var());
71   Visit(node->this_function_var());
72 }
73 
74 
VisitRewritableExpression(RewritableExpression * node)75 void AstLiteralReindexer::VisitRewritableExpression(
76     RewritableExpression* node) {
77   Visit(node->expression());
78 }
79 
80 
VisitExpressionStatement(ExpressionStatement * node)81 void AstLiteralReindexer::VisitExpressionStatement(ExpressionStatement* node) {
82   Visit(node->expression());
83 }
84 
85 
VisitReturnStatement(ReturnStatement * node)86 void AstLiteralReindexer::VisitReturnStatement(ReturnStatement* node) {
87   Visit(node->expression());
88 }
89 
90 
VisitYield(Yield * node)91 void AstLiteralReindexer::VisitYield(Yield* node) {
92   Visit(node->generator_object());
93   Visit(node->expression());
94 }
95 
96 
VisitThrow(Throw * node)97 void AstLiteralReindexer::VisitThrow(Throw* node) { Visit(node->exception()); }
98 
99 
VisitUnaryOperation(UnaryOperation * node)100 void AstLiteralReindexer::VisitUnaryOperation(UnaryOperation* node) {
101   Visit(node->expression());
102 }
103 
104 
VisitCountOperation(CountOperation * node)105 void AstLiteralReindexer::VisitCountOperation(CountOperation* node) {
106   Visit(node->expression());
107 }
108 
109 
VisitBlock(Block * node)110 void AstLiteralReindexer::VisitBlock(Block* node) {
111   VisitStatements(node->statements());
112 }
113 
114 
VisitFunctionDeclaration(FunctionDeclaration * node)115 void AstLiteralReindexer::VisitFunctionDeclaration(FunctionDeclaration* node) {
116   VisitVariableProxy(node->proxy());
117   VisitFunctionLiteral(node->fun());
118 }
119 
120 
VisitCallRuntime(CallRuntime * node)121 void AstLiteralReindexer::VisitCallRuntime(CallRuntime* node) {
122   VisitArguments(node->arguments());
123 }
124 
125 
VisitWithStatement(WithStatement * node)126 void AstLiteralReindexer::VisitWithStatement(WithStatement* node) {
127   Visit(node->expression());
128   Visit(node->statement());
129 }
130 
131 
VisitDoWhileStatement(DoWhileStatement * node)132 void AstLiteralReindexer::VisitDoWhileStatement(DoWhileStatement* node) {
133   Visit(node->body());
134   Visit(node->cond());
135 }
136 
137 
VisitWhileStatement(WhileStatement * node)138 void AstLiteralReindexer::VisitWhileStatement(WhileStatement* node) {
139   Visit(node->cond());
140   Visit(node->body());
141 }
142 
143 
VisitTryCatchStatement(TryCatchStatement * node)144 void AstLiteralReindexer::VisitTryCatchStatement(TryCatchStatement* node) {
145   Visit(node->try_block());
146   Visit(node->catch_block());
147 }
148 
149 
VisitTryFinallyStatement(TryFinallyStatement * node)150 void AstLiteralReindexer::VisitTryFinallyStatement(TryFinallyStatement* node) {
151   Visit(node->try_block());
152   Visit(node->finally_block());
153 }
154 
155 
VisitProperty(Property * node)156 void AstLiteralReindexer::VisitProperty(Property* node) {
157   Visit(node->key());
158   Visit(node->obj());
159 }
160 
161 
VisitAssignment(Assignment * node)162 void AstLiteralReindexer::VisitAssignment(Assignment* node) {
163   Visit(node->target());
164   Visit(node->value());
165 }
166 
167 
VisitBinaryOperation(BinaryOperation * node)168 void AstLiteralReindexer::VisitBinaryOperation(BinaryOperation* node) {
169   Visit(node->left());
170   Visit(node->right());
171 }
172 
173 
VisitCompareOperation(CompareOperation * node)174 void AstLiteralReindexer::VisitCompareOperation(CompareOperation* node) {
175   Visit(node->left());
176   Visit(node->right());
177 }
178 
179 
VisitSpread(Spread * node)180 void AstLiteralReindexer::VisitSpread(Spread* node) {
181   // This is reachable because ParserBase::ParseArrowFunctionLiteral calls
182   // ReindexLiterals before calling RewriteDestructuringAssignments.
183   Visit(node->expression());
184 }
185 
186 
VisitEmptyParentheses(EmptyParentheses * node)187 void AstLiteralReindexer::VisitEmptyParentheses(EmptyParentheses* node) {}
188 
189 
VisitForInStatement(ForInStatement * node)190 void AstLiteralReindexer::VisitForInStatement(ForInStatement* node) {
191   Visit(node->each());
192   Visit(node->enumerable());
193   Visit(node->body());
194 }
195 
196 
VisitForOfStatement(ForOfStatement * node)197 void AstLiteralReindexer::VisitForOfStatement(ForOfStatement* node) {
198   Visit(node->assign_iterator());
199   Visit(node->next_result());
200   Visit(node->result_done());
201   Visit(node->assign_each());
202   Visit(node->body());
203 }
204 
205 
VisitConditional(Conditional * node)206 void AstLiteralReindexer::VisitConditional(Conditional* node) {
207   Visit(node->condition());
208   Visit(node->then_expression());
209   Visit(node->else_expression());
210 }
211 
212 
VisitIfStatement(IfStatement * node)213 void AstLiteralReindexer::VisitIfStatement(IfStatement* node) {
214   Visit(node->condition());
215   Visit(node->then_statement());
216   if (node->HasElseStatement()) {
217     Visit(node->else_statement());
218   }
219 }
220 
221 
VisitSwitchStatement(SwitchStatement * node)222 void AstLiteralReindexer::VisitSwitchStatement(SwitchStatement* node) {
223   Visit(node->tag());
224   ZoneList<CaseClause*>* cases = node->cases();
225   for (int i = 0; i < cases->length(); i++) {
226     VisitCaseClause(cases->at(i));
227   }
228 }
229 
230 
VisitCaseClause(CaseClause * node)231 void AstLiteralReindexer::VisitCaseClause(CaseClause* node) {
232   if (!node->is_default()) Visit(node->label());
233   VisitStatements(node->statements());
234 }
235 
236 
VisitForStatement(ForStatement * node)237 void AstLiteralReindexer::VisitForStatement(ForStatement* node) {
238   if (node->init() != NULL) Visit(node->init());
239   if (node->cond() != NULL) Visit(node->cond());
240   if (node->next() != NULL) Visit(node->next());
241   Visit(node->body());
242 }
243 
244 
VisitClassLiteral(ClassLiteral * node)245 void AstLiteralReindexer::VisitClassLiteral(ClassLiteral* node) {
246   if (node->extends()) Visit(node->extends());
247   if (node->constructor()) Visit(node->constructor());
248   if (node->class_variable_proxy()) {
249     VisitVariableProxy(node->class_variable_proxy());
250   }
251   for (int i = 0; i < node->properties()->length(); i++) {
252     VisitLiteralProperty(node->properties()->at(i));
253   }
254 }
255 
VisitObjectLiteral(ObjectLiteral * node)256 void AstLiteralReindexer::VisitObjectLiteral(ObjectLiteral* node) {
257   UpdateIndex(node);
258   for (int i = 0; i < node->properties()->length(); i++) {
259     VisitLiteralProperty(node->properties()->at(i));
260   }
261 }
262 
VisitLiteralProperty(LiteralProperty * node)263 void AstLiteralReindexer::VisitLiteralProperty(LiteralProperty* node) {
264   Visit(node->key());
265   Visit(node->value());
266 }
267 
268 
VisitArrayLiteral(ArrayLiteral * node)269 void AstLiteralReindexer::VisitArrayLiteral(ArrayLiteral* node) {
270   UpdateIndex(node);
271   for (int i = 0; i < node->values()->length(); i++) {
272     Visit(node->values()->at(i));
273   }
274 }
275 
276 
VisitCall(Call * node)277 void AstLiteralReindexer::VisitCall(Call* node) {
278   Visit(node->expression());
279   VisitArguments(node->arguments());
280 }
281 
282 
VisitCallNew(CallNew * node)283 void AstLiteralReindexer::VisitCallNew(CallNew* node) {
284   Visit(node->expression());
285   VisitArguments(node->arguments());
286 }
287 
288 
VisitStatements(ZoneList<Statement * > * statements)289 void AstLiteralReindexer::VisitStatements(ZoneList<Statement*>* statements) {
290   if (statements == NULL) return;
291   for (int i = 0; i < statements->length(); i++) {
292     Visit(statements->at(i));
293   }
294 }
295 
296 
VisitDeclarations(ZoneList<Declaration * > * declarations)297 void AstLiteralReindexer::VisitDeclarations(
298     ZoneList<Declaration*>* declarations) {
299   for (int i = 0; i < declarations->length(); i++) {
300     Visit(declarations->at(i));
301   }
302 }
303 
304 
VisitArguments(ZoneList<Expression * > * arguments)305 void AstLiteralReindexer::VisitArguments(ZoneList<Expression*>* arguments) {
306   for (int i = 0; i < arguments->length(); i++) {
307     Visit(arguments->at(i));
308   }
309 }
310 
311 
VisitFunctionLiteral(FunctionLiteral * node)312 void AstLiteralReindexer::VisitFunctionLiteral(FunctionLiteral* node) {
313   // We don't recurse into the declarations or body of the function literal:
314 }
315 
Reindex(Expression * pattern)316 void AstLiteralReindexer::Reindex(Expression* pattern) { Visit(pattern); }
317 }  // namespace internal
318 }  // namespace v8
319