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