blob: 4b441b9ae41f520e670d98e6643f9264899a17a5 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdarg.h>
29
30#include "v8.h"
31
32#include "prettyprinter.h"
33#include "scopes.h"
34#include "platform.h"
35
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
39#ifdef DEBUG
40
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +000041PrettyPrinter::PrettyPrinter(Isolate* isolate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 output_ = NULL;
43 size_ = 0;
44 pos_ = 0;
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +000045 InitializeAstVisitor(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046}
47
48
49PrettyPrinter::~PrettyPrinter() {
50 DeleteArray(output_);
51}
52
53
54void PrettyPrinter::VisitBlock(Block* node) {
55 if (!node->is_initializer_block()) Print("{ ");
56 PrintStatements(node->statements());
57 if (node->statements()->length() > 0) Print(" ");
58 if (!node->is_initializer_block()) Print("}");
59}
60
61
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000062void PrettyPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063 Print("var ");
64 PrintLiteral(node->proxy()->name(), false);
ulan@chromium.org812308e2012-02-29 15:58:45 +000065 Print(";");
66}
67
68
69void PrettyPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
70 Print("function ");
71 PrintLiteral(node->proxy()->name(), false);
72 Print(" = ");
73 PrintFunctionLiteral(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074 Print(";");
75}
76
77
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000078void PrettyPrinter::VisitModuleDeclaration(ModuleDeclaration* node) {
79 Print("module ");
80 PrintLiteral(node->proxy()->name(), false);
81 Print(" = ");
82 Visit(node->module());
83 Print(";");
84}
85
86
ulan@chromium.org812308e2012-02-29 15:58:45 +000087void PrettyPrinter::VisitImportDeclaration(ImportDeclaration* node) {
88 Print("import ");
89 PrintLiteral(node->proxy()->name(), false);
90 Print(" from ");
91 Visit(node->module());
92 Print(";");
93}
94
95
96void PrettyPrinter::VisitExportDeclaration(ExportDeclaration* node) {
97 Print("export ");
98 PrintLiteral(node->proxy()->name(), false);
99 Print(";");
100}
101
102
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000103void PrettyPrinter::VisitModuleLiteral(ModuleLiteral* node) {
104 VisitBlock(node->body());
105}
106
107
108void PrettyPrinter::VisitModuleVariable(ModuleVariable* node) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000109 Visit(node->proxy());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000110}
111
112
113void PrettyPrinter::VisitModulePath(ModulePath* node) {
114 Visit(node->module());
115 Print(".");
116 PrintLiteral(node->name(), false);
117}
118
119
120void PrettyPrinter::VisitModuleUrl(ModuleUrl* node) {
121 Print("at ");
122 PrintLiteral(node->url(), true);
123}
124
125
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000126void PrettyPrinter::VisitModuleStatement(ModuleStatement* node) {
127 Print("module ");
128 PrintLiteral(node->proxy()->name(), false);
129 Print(" ");
130 Visit(node->body());
131}
132
133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) {
135 Visit(node->expression());
136 Print(";");
137}
138
139
140void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) {
141 Print(";");
142}
143
144
145void PrettyPrinter::VisitIfStatement(IfStatement* node) {
146 Print("if (");
147 Visit(node->condition());
148 Print(") ");
149 Visit(node->then_statement());
150 if (node->HasElseStatement()) {
151 Print(" else ");
152 Visit(node->else_statement());
153 }
154}
155
156
157void PrettyPrinter::VisitContinueStatement(ContinueStatement* node) {
158 Print("continue");
159 ZoneStringList* labels = node->target()->labels();
160 if (labels != NULL) {
161 Print(" ");
162 ASSERT(labels->length() > 0); // guaranteed to have at least one entry
163 PrintLiteral(labels->at(0), false); // any label from the list is fine
164 }
165 Print(";");
166}
167
168
169void PrettyPrinter::VisitBreakStatement(BreakStatement* node) {
170 Print("break");
171 ZoneStringList* labels = node->target()->labels();
172 if (labels != NULL) {
173 Print(" ");
174 ASSERT(labels->length() > 0); // guaranteed to have at least one entry
175 PrintLiteral(labels->at(0), false); // any label from the list is fine
176 }
177 Print(";");
178}
179
180
181void PrettyPrinter::VisitReturnStatement(ReturnStatement* node) {
182 Print("return ");
183 Visit(node->expression());
184 Print(";");
185}
186
187
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000188void PrettyPrinter::VisitWithStatement(WithStatement* node) {
189 Print("with (");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 Visit(node->expression());
191 Print(") ");
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000192 Visit(node->statement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193}
194
195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) {
197 PrintLabels(node->labels());
198 Print("switch (");
199 Visit(node->tag());
200 Print(") { ");
201 ZoneList<CaseClause*>* cases = node->cases();
202 for (int i = 0; i < cases->length(); i++)
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000203 Visit(cases->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 Print("}");
205}
206
207
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000208void PrettyPrinter::VisitCaseClause(CaseClause* clause) {
209 if (clause->is_default()) {
210 Print("default");
211 } else {
212 Print("case ");
213 Visit(clause->label());
214 }
215 Print(": ");
216 PrintStatements(clause->statements());
217 if (clause->statements()->length() > 0)
218 Print(" ");
219}
220
221
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000222void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 PrintLabels(node->labels());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000224 Print("do ");
225 Visit(node->body());
226 Print(" while (");
227 Visit(node->cond());
228 Print(");");
229}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000232void PrettyPrinter::VisitWhileStatement(WhileStatement* node) {
233 PrintLabels(node->labels());
234 Print("while (");
235 Visit(node->cond());
236 Print(") ");
237 Visit(node->body());
238}
239
240
241void PrettyPrinter::VisitForStatement(ForStatement* node) {
242 PrintLabels(node->labels());
243 Print("for (");
244 if (node->init() != NULL) {
245 Visit(node->init());
246 Print(" ");
247 } else {
248 Print("; ");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000250 if (node->cond() != NULL) Visit(node->cond());
251 Print("; ");
252 if (node->next() != NULL) {
253 Visit(node->next()); // prints extra ';', unfortunately
254 // to fix: should use Expression for next
255 }
256 Print(") ");
257 Visit(node->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258}
259
260
261void PrettyPrinter::VisitForInStatement(ForInStatement* node) {
262 PrintLabels(node->labels());
263 Print("for (");
264 Visit(node->each());
265 Print(" in ");
266 Visit(node->enumerable());
267 Print(") ");
268 Visit(node->body());
269}
270
271
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000272void PrettyPrinter::VisitForOfStatement(ForOfStatement* node) {
273 PrintLabels(node->labels());
274 Print("for (");
275 Visit(node->each());
276 Print(" of ");
277 Visit(node->iterable());
278 Print(") ");
279 Visit(node->body());
280}
281
282
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000283void PrettyPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 Print("try ");
285 Visit(node->try_block());
286 Print(" catch (");
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000287 const bool quote = false;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000288 PrintLiteral(node->variable()->name(), quote);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 Print(") ");
290 Visit(node->catch_block());
291}
292
293
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000294void PrettyPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 Print("try ");
296 Visit(node->try_block());
297 Print(" finally ");
298 Visit(node->finally_block());
299}
300
301
302void PrettyPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
303 Print("debugger ");
304}
305
306
307void PrettyPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
308 Print("(");
309 PrintFunctionLiteral(node);
310 Print(")");
311}
312
313
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +0000314void PrettyPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 Print("(");
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +0000316 PrintLiteral(node->name(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 Print(")");
318}
319
320
321void PrettyPrinter::VisitConditional(Conditional* node) {
322 Visit(node->condition());
323 Print(" ? ");
324 Visit(node->then_expression());
325 Print(" : ");
326 Visit(node->else_expression());
327}
328
329
330void PrettyPrinter::VisitLiteral(Literal* node) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000331 PrintLiteral(node->value(), true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332}
333
334
335void PrettyPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
336 Print(" RegExp(");
337 PrintLiteral(node->pattern(), false);
338 Print(",");
339 PrintLiteral(node->flags(), false);
340 Print(") ");
341}
342
343
344void PrettyPrinter::VisitObjectLiteral(ObjectLiteral* node) {
345 Print("{ ");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 for (int i = 0; i < node->properties()->length(); i++) {
347 if (i != 0) Print(",");
348 ObjectLiteral::Property* property = node->properties()->at(i);
349 Print(" ");
350 Visit(property->key());
351 Print(": ");
352 Visit(property->value());
353 }
354 Print(" }");
355}
356
357
358void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) {
359 Print("[ ");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 for (int i = 0; i < node->values()->length(); i++) {
361 if (i != 0) Print(",");
362 Visit(node->values()->at(i));
363 }
364 Print(" ]");
365}
366
367
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368void PrettyPrinter::VisitVariableProxy(VariableProxy* node) {
369 PrintLiteral(node->name(), false);
370}
371
372
373void PrettyPrinter::VisitAssignment(Assignment* node) {
374 Visit(node->target());
375 Print(" %s ", Token::String(node->op()));
376 Visit(node->value());
377}
378
379
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000380void PrettyPrinter::VisitYield(Yield* node) {
381 Print("yield ");
382 Visit(node->expression());
383}
384
385
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386void PrettyPrinter::VisitThrow(Throw* node) {
387 Print("throw ");
388 Visit(node->exception());
389}
390
391
392void PrettyPrinter::VisitProperty(Property* node) {
393 Expression* key = node->key();
394 Literal* literal = key->AsLiteral();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000395 if (literal != NULL && literal->value()->IsInternalizedString()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 Print("(");
397 Visit(node->obj());
398 Print(").");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000399 PrintLiteral(literal->value(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 } else {
401 Visit(node->obj());
402 Print("[");
403 Visit(key);
404 Print("]");
405 }
406}
407
408
409void PrettyPrinter::VisitCall(Call* node) {
410 Visit(node->expression());
411 PrintArguments(node->arguments());
412}
413
414
415void PrettyPrinter::VisitCallNew(CallNew* node) {
416 Print("new (");
417 Visit(node->expression());
418 Print(")");
419 PrintArguments(node->arguments());
420}
421
422
423void PrettyPrinter::VisitCallRuntime(CallRuntime* node) {
424 Print("%%");
425 PrintLiteral(node->name(), false);
426 PrintArguments(node->arguments());
427}
428
429
430void PrettyPrinter::VisitUnaryOperation(UnaryOperation* node) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000431 Token::Value op = node->op();
432 bool needsSpace =
433 op == Token::DELETE || op == Token::TYPEOF || op == Token::VOID;
434 Print("(%s%s", Token::String(op), needsSpace ? " " : "");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 Visit(node->expression());
436 Print(")");
437}
438
439
440void PrettyPrinter::VisitCountOperation(CountOperation* node) {
441 Print("(");
442 if (node->is_prefix()) Print("%s", Token::String(node->op()));
443 Visit(node->expression());
444 if (node->is_postfix()) Print("%s", Token::String(node->op()));
445 Print(")");
446}
447
448
449void PrettyPrinter::VisitBinaryOperation(BinaryOperation* node) {
450 Print("(");
451 Visit(node->left());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000452 Print(" %s ", Token::String(node->op()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 Visit(node->right());
454 Print(")");
455}
456
457
458void PrettyPrinter::VisitCompareOperation(CompareOperation* node) {
459 Print("(");
460 Visit(node->left());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000461 Print(" %s ", Token::String(node->op()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 Visit(node->right());
463 Print(")");
464}
465
466
467void PrettyPrinter::VisitThisFunction(ThisFunction* node) {
468 Print("<this-function>");
469}
470
471
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000472const char* PrettyPrinter::Print(AstNode* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 Init();
474 Visit(node);
475 return output_;
476}
477
478
479const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) {
480 Init();
481 ExpressionStatement* statement =
482 program->body()->at(0)->AsExpressionStatement();
483 Visit(statement->expression());
484 return output_;
485}
486
487
488const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) {
489 Init();
490 PrintStatements(program->body());
491 Print("\n");
492 return output_;
493}
494
495
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000496void PrettyPrinter::PrintOut(Isolate* isolate, AstNode* node) {
497 PrettyPrinter printer(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 PrintF("%s", printer.Print(node));
499}
500
501
502void PrettyPrinter::Init() {
503 if (size_ == 0) {
504 ASSERT(output_ == NULL);
505 const int initial_size = 256;
506 output_ = NewArray<char>(initial_size);
507 size_ = initial_size;
508 }
509 output_[0] = '\0';
510 pos_ = 0;
511}
512
513
514void PrettyPrinter::Print(const char* format, ...) {
515 for (;;) {
516 va_list arguments;
517 va_start(arguments, format);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000518 int n = OS::VSNPrintF(Vector<char>(output_, size_) + pos_,
519 format,
520 arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 va_end(arguments);
522
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000523 if (n >= 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 // there was enough space - we are done
525 pos_ += n;
526 return;
527 } else {
528 // there was not enough space - allocate more and try again
529 const int slack = 32;
530 int new_size = size_ + (size_ >> 1) + slack;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 char* new_output = NewArray<char>(new_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000532 OS::MemCopy(new_output, output_, pos_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 DeleteArray(output_);
534 output_ = new_output;
535 size_ = new_size;
536 }
537 }
538}
539
540
541void PrettyPrinter::PrintStatements(ZoneList<Statement*>* statements) {
ricow@chromium.org7ad65222011-12-19 12:13:11 +0000542 if (statements == NULL) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 for (int i = 0; i < statements->length(); i++) {
544 if (i != 0) Print(" ");
545 Visit(statements->at(i));
546 }
547}
548
549
550void PrettyPrinter::PrintLabels(ZoneStringList* labels) {
551 if (labels != NULL) {
552 for (int i = 0; i < labels->length(); i++) {
553 PrintLiteral(labels->at(i), false);
554 Print(": ");
555 }
556 }
557}
558
559
560void PrettyPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
561 Print("(");
562 for (int i = 0; i < arguments->length(); i++) {
563 if (i != 0) Print(", ");
564 Visit(arguments->at(i));
565 }
566 Print(")");
567}
568
569
570void PrettyPrinter::PrintLiteral(Handle<Object> value, bool quote) {
571 Object* object = *value;
572 if (object->IsString()) {
573 String* string = String::cast(object);
574 if (quote) Print("\"");
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000575 for (int i = 0; i < string->length(); i++) {
576 Print("%c", string->Get(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 }
578 if (quote) Print("\"");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 } else if (object->IsNull()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 Print("null");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000581 } else if (object->IsTrue()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 Print("true");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000583 } else if (object->IsFalse()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 Print("false");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000585 } else if (object->IsUndefined()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 Print("undefined");
587 } else if (object->IsNumber()) {
588 Print("%g", object->Number());
589 } else if (object->IsJSObject()) {
590 // regular expression
591 if (object->IsJSFunction()) {
592 Print("JS-Function");
593 } else if (object->IsJSArray()) {
594 Print("JS-array[%u]", JSArray::cast(object)->length());
595 } else if (object->IsJSObject()) {
596 Print("JS-Object");
597 } else {
598 Print("?UNKNOWN?");
599 }
600 } else if (object->IsFixedArray()) {
601 Print("FixedArray");
602 } else {
603 Print("<unknown literal %p>", object);
604 }
605}
606
607
608void PrettyPrinter::PrintParameters(Scope* scope) {
609 Print("(");
610 for (int i = 0; i < scope->num_parameters(); i++) {
611 if (i > 0) Print(", ");
612 PrintLiteral(scope->parameter(i)->name(), false);
613 }
614 Print(")");
615}
616
617
618void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
619 for (int i = 0; i < declarations->length(); i++) {
620 if (i > 0) Print(" ");
621 Visit(declarations->at(i));
622 }
623}
624
625
626void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) {
627 Print("function ");
628 PrintLiteral(function->name(), false);
629 PrintParameters(function->scope());
630 Print(" { ");
631 PrintDeclarations(function->scope()->declarations());
632 PrintStatements(function->body());
633 Print(" }");
634}
635
636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637//-----------------------------------------------------------------------------
638
639class IndentedScope BASE_EMBEDDED {
640 public:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000641 IndentedScope(AstPrinter* printer, const char* txt)
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000642 : ast_printer_(printer) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 ast_printer_->PrintIndented(txt);
644 ast_printer_->Print("\n");
645 ast_printer_->inc_indent();
646 }
647
648 virtual ~IndentedScope() {
649 ast_printer_->dec_indent();
650 }
651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000653 AstPrinter* ast_printer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654};
655
656
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657//-----------------------------------------------------------------------------
658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000660AstPrinter::AstPrinter(Isolate* isolate) : PrettyPrinter(isolate), indent_(0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661}
662
663
664AstPrinter::~AstPrinter() {
665 ASSERT(indent_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666}
667
668
669void AstPrinter::PrintIndented(const char* txt) {
670 for (int i = 0; i < indent_; i++) {
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000671 Print(". ");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 }
673 Print(txt);
674}
675
676
677void AstPrinter::PrintLiteralIndented(const char* info,
678 Handle<Object> value,
679 bool quote) {
680 PrintIndented(info);
681 Print(" ");
682 PrintLiteral(value, quote);
683 Print("\n");
684}
685
686
687void AstPrinter::PrintLiteralWithModeIndented(const char* info,
688 Variable* var,
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000689 Handle<Object> value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 if (var == NULL) {
691 PrintLiteralIndented(info, value, true);
692 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000693 EmbeddedVector<char, 256> buf;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000694 int pos = OS::SNPrintF(buf, "%s (mode = %s", info,
695 Variable::Mode2String(var->mode()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000696 OS::SNPrintF(buf + pos, ")");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000697 PrintLiteralIndented(buf.start(), value, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 }
699}
700
701
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000702void AstPrinter::PrintLabelsIndented(ZoneStringList* labels) {
703 if (labels == NULL || labels->length() == 0) return;
704 PrintIndented("LABELS ");
705 PrintLabels(labels);
706 Print("\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707}
708
709
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000710void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000711 IndentedScope indent(this, s);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 Visit(node);
713}
714
715
716const char* AstPrinter::PrintProgram(FunctionLiteral* program) {
717 Init();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000718 { IndentedScope indent(this, "FUNC");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 PrintLiteralIndented("NAME", program->name(), true);
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000720 PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 PrintParameters(program->scope());
722 PrintDeclarations(program->scope()->declarations());
723 PrintStatements(program->body());
724 }
725 return Output();
726}
727
728
729void AstPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
730 if (declarations->length() > 0) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000731 IndentedScope indent(this, "DECLS");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 for (int i = 0; i < declarations->length(); i++) {
733 Visit(declarations->at(i));
734 }
735 }
736}
737
738
739void AstPrinter::PrintParameters(Scope* scope) {
740 if (scope->num_parameters() > 0) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000741 IndentedScope indent(this, "PARAMS");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 for (int i = 0; i < scope->num_parameters(); i++) {
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000743 PrintLiteralWithModeIndented("VAR", scope->parameter(i),
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000744 scope->parameter(i)->name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 }
746 }
747}
748
749
750void AstPrinter::PrintStatements(ZoneList<Statement*>* statements) {
751 for (int i = 0; i < statements->length(); i++) {
752 Visit(statements->at(i));
753 }
754}
755
756
757void AstPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
758 for (int i = 0; i < arguments->length(); i++) {
759 Visit(arguments->at(i));
760 }
761}
762
763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764void AstPrinter::VisitBlock(Block* node) {
765 const char* block_txt = node->is_initializer_block() ? "BLOCK INIT" : "BLOCK";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000766 IndentedScope indent(this, block_txt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 PrintStatements(node->statements());
768}
769
770
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000771// TODO(svenpanne) Start with IndentedScope.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000772void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000773 PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()),
774 node->proxy()->var(),
775 node->proxy()->name());
776}
777
778
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000779// TODO(svenpanne) Start with IndentedScope.
ulan@chromium.org812308e2012-02-29 15:58:45 +0000780void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
781 PrintIndented("FUNCTION ");
782 PrintLiteral(node->proxy()->name(), true);
783 Print(" = function ");
784 PrintLiteral(node->fun()->name(), false);
785 Print("\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786}
787
788
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000789void AstPrinter::VisitModuleDeclaration(ModuleDeclaration* node) {
790 IndentedScope indent(this, "MODULE");
791 PrintLiteralIndented("NAME", node->proxy()->name(), true);
792 Visit(node->module());
793}
794
795
ulan@chromium.org812308e2012-02-29 15:58:45 +0000796void AstPrinter::VisitImportDeclaration(ImportDeclaration* node) {
797 IndentedScope indent(this, "IMPORT");
798 PrintLiteralIndented("NAME", node->proxy()->name(), true);
799 Visit(node->module());
800}
801
802
803void AstPrinter::VisitExportDeclaration(ExportDeclaration* node) {
804 IndentedScope indent(this, "EXPORT ");
805 PrintLiteral(node->proxy()->name(), true);
806}
807
808
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000809void AstPrinter::VisitModuleLiteral(ModuleLiteral* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000810 IndentedScope indent(this, "MODULE LITERAL");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000811 VisitBlock(node->body());
812}
813
814
815void AstPrinter::VisitModuleVariable(ModuleVariable* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000816 IndentedScope indent(this, "MODULE VARIABLE");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000817 Visit(node->proxy());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000818}
819
820
821void AstPrinter::VisitModulePath(ModulePath* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000822 IndentedScope indent(this, "MODULE PATH");
823 PrintIndentedVisit("MODULE PATH PARENT", node->module());
824 PrintLiteralIndented("NAME", node->name(), true);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000825}
826
827
828void AstPrinter::VisitModuleUrl(ModuleUrl* node) {
829 PrintLiteralIndented("URL", node->url(), true);
830}
831
832
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000833void AstPrinter::VisitModuleStatement(ModuleStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000834 IndentedScope indent(this, "MODULE STATEMENT");
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000835 PrintLiteralIndented("NAME", node->proxy()->name(), true);
836 PrintStatements(node->body()->statements());
837}
838
839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000841 IndentedScope indent(this, "EXPRESSION STATEMENT");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 Visit(node->expression());
843}
844
845
846void AstPrinter::VisitEmptyStatement(EmptyStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000847 IndentedScope indent(this, "EMPTY");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848}
849
850
851void AstPrinter::VisitIfStatement(IfStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000852 IndentedScope indent(this, "IF");
853 PrintIndentedVisit("CONDITION", node->condition());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 PrintIndentedVisit("THEN", node->then_statement());
855 if (node->HasElseStatement()) {
856 PrintIndentedVisit("ELSE", node->else_statement());
857 }
858}
859
860
861void AstPrinter::VisitContinueStatement(ContinueStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000862 IndentedScope indent(this, "CONTINUE");
863 PrintLabelsIndented(node->target()->labels());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864}
865
866
867void AstPrinter::VisitBreakStatement(BreakStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000868 IndentedScope indent(this, "BREAK");
869 PrintLabelsIndented(node->target()->labels());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870}
871
872
873void AstPrinter::VisitReturnStatement(ReturnStatement* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000874 IndentedScope indent(this, "RETURN");
875 Visit(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876}
877
878
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000879void AstPrinter::VisitWithStatement(WithStatement* node) {
880 IndentedScope indent(this, "WITH");
881 PrintIndentedVisit("OBJECT", node->expression());
882 PrintIndentedVisit("BODY", node->statement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883}
884
885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000887 IndentedScope indent(this, "SWITCH");
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000888 PrintLabelsIndented(node->labels());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 PrintIndentedVisit("TAG", node->tag());
890 for (int i = 0; i < node->cases()->length(); i++) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000891 Visit(node->cases()->at(i));
892 }
893}
894
895
896void AstPrinter::VisitCaseClause(CaseClause* clause) {
897 if (clause->is_default()) {
898 IndentedScope indent(this, "DEFAULT");
899 PrintStatements(clause->statements());
900 } else {
901 IndentedScope indent(this, "CASE");
902 Visit(clause->label());
903 PrintStatements(clause->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 }
905}
906
907
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000908void AstPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000909 IndentedScope indent(this, "DO");
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000910 PrintLabelsIndented(node->labels());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000911 PrintIndentedVisit("BODY", node->body());
912 PrintIndentedVisit("COND", node->cond());
913}
914
915
916void AstPrinter::VisitWhileStatement(WhileStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000917 IndentedScope indent(this, "WHILE");
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000918 PrintLabelsIndented(node->labels());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000919 PrintIndentedVisit("COND", node->cond());
920 PrintIndentedVisit("BODY", node->body());
921}
922
923
924void AstPrinter::VisitForStatement(ForStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000925 IndentedScope indent(this, "FOR");
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000926 PrintLabelsIndented(node->labels());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 if (node->init()) PrintIndentedVisit("INIT", node->init());
928 if (node->cond()) PrintIndentedVisit("COND", node->cond());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000929 PrintIndentedVisit("BODY", node->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 if (node->next()) PrintIndentedVisit("NEXT", node->next());
931}
932
933
934void AstPrinter::VisitForInStatement(ForInStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000935 IndentedScope indent(this, "FOR IN");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 PrintIndentedVisit("FOR", node->each());
937 PrintIndentedVisit("IN", node->enumerable());
938 PrintIndentedVisit("BODY", node->body());
939}
940
941
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000942void AstPrinter::VisitForOfStatement(ForOfStatement* node) {
943 IndentedScope indent(this, "FOR OF");
944 PrintIndentedVisit("FOR", node->each());
945 PrintIndentedVisit("OF", node->iterable());
946 PrintIndentedVisit("BODY", node->body());
947}
948
949
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000950void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000951 IndentedScope indent(this, "TRY CATCH");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 PrintIndentedVisit("TRY", node->try_block());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000953 PrintLiteralWithModeIndented("CATCHVAR",
954 node->variable(),
955 node->variable()->name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 PrintIndentedVisit("CATCH", node->catch_block());
957}
958
959
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000960void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000961 IndentedScope indent(this, "TRY FINALLY");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 PrintIndentedVisit("TRY", node->try_block());
963 PrintIndentedVisit("FINALLY", node->finally_block());
964}
965
966
967void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000968 IndentedScope indent(this, "DEBUGGER");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969}
970
971
972void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000973 IndentedScope indent(this, "FUNC LITERAL");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 PrintLiteralIndented("NAME", node->name(), false);
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000975 PrintLiteralIndented("INFERRED NAME", node->inferred_name(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 PrintParameters(node->scope());
977 // We don't want to see the function literal in this case: it
978 // will be printed via PrintProgram when the code for it is
979 // generated.
980 // PrintStatements(node->body());
981}
982
983
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +0000984void AstPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
985 IndentedScope indent(this, "NATIVE FUNC LITERAL");
986 PrintLiteralIndented("NAME", node->name(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987}
988
989
990void AstPrinter::VisitConditional(Conditional* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000991 IndentedScope indent(this, "CONDITIONAL");
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000992 PrintIndentedVisit("CONDITION", node->condition());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 PrintIndentedVisit("THEN", node->then_expression());
994 PrintIndentedVisit("ELSE", node->else_expression());
995}
996
997
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000998// TODO(svenpanne) Start with IndentedScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999void AstPrinter::VisitLiteral(Literal* node) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001000 PrintLiteralIndented("LITERAL", node->value(), true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001}
1002
1003
1004void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001005 IndentedScope indent(this, "REGEXP LITERAL");
ager@chromium.orge2902be2009-06-08 12:21:35 +00001006 PrintLiteralIndented("PATTERN", node->pattern(), false);
1007 PrintLiteralIndented("FLAGS", node->flags(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008}
1009
1010
1011void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001012 IndentedScope indent(this, "OBJ LITERAL");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 for (int i = 0; i < node->properties()->length(); i++) {
1014 const char* prop_kind = NULL;
1015 switch (node->properties()->at(i)->kind()) {
1016 case ObjectLiteral::Property::CONSTANT:
1017 prop_kind = "PROPERTY - CONSTANT";
1018 break;
1019 case ObjectLiteral::Property::COMPUTED:
1020 prop_kind = "PROPERTY - COMPUTED";
1021 break;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001022 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1023 prop_kind = "PROPERTY - MATERIALIZED_LITERAL";
1024 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 case ObjectLiteral::Property::PROTOTYPE:
1026 prop_kind = "PROPERTY - PROTOTYPE";
1027 break;
1028 case ObjectLiteral::Property::GETTER:
1029 prop_kind = "PROPERTY - GETTER";
1030 break;
1031 case ObjectLiteral::Property::SETTER:
1032 prop_kind = "PROPERTY - SETTER";
1033 break;
1034 default:
1035 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 }
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001037 IndentedScope prop(this, prop_kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 PrintIndentedVisit("KEY", node->properties()->at(i)->key());
1039 PrintIndentedVisit("VALUE", node->properties()->at(i)->value());
1040 }
1041}
1042
1043
1044void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001045 IndentedScope indent(this, "ARRAY LITERAL");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 if (node->values()->length() > 0) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001047 IndentedScope indent(this, "VALUES");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 for (int i = 0; i < node->values()->length(); i++) {
1049 Visit(node->values()->at(i));
1050 }
1051 }
1052}
1053
1054
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001055// TODO(svenpanne) Start with IndentedScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056void AstPrinter::VisitVariableProxy(VariableProxy* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 Variable* var = node->var();
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001058 EmbeddedVector<char, 128> buf;
1059 int pos = OS::SNPrintF(buf, "VAR PROXY");
1060 switch (var->location()) {
1061 case Variable::UNALLOCATED:
1062 break;
1063 case Variable::PARAMETER:
1064 OS::SNPrintF(buf + pos, " parameter[%d]", var->index());
1065 break;
1066 case Variable::LOCAL:
1067 OS::SNPrintF(buf + pos, " local[%d]", var->index());
1068 break;
1069 case Variable::CONTEXT:
1070 OS::SNPrintF(buf + pos, " context[%d]", var->index());
1071 break;
1072 case Variable::LOOKUP:
1073 OS::SNPrintF(buf + pos, " lookup");
1074 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001076 PrintLiteralWithModeIndented(buf.start(), var, node->name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077}
1078
1079
1080void AstPrinter::VisitAssignment(Assignment* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001081 IndentedScope indent(this, Token::Name(node->op()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 Visit(node->target());
1083 Visit(node->value());
1084}
1085
1086
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001087void AstPrinter::VisitYield(Yield* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001088 IndentedScope indent(this, "YIELD");
1089 Visit(node->expression());
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001090}
1091
1092
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093void AstPrinter::VisitThrow(Throw* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001094 IndentedScope indent(this, "THROW");
1095 Visit(node->exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096}
1097
1098
1099void AstPrinter::VisitProperty(Property* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001100 IndentedScope indent(this, "PROPERTY");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 Visit(node->obj());
1102 Literal* literal = node->key()->AsLiteral();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001103 if (literal != NULL && literal->value()->IsInternalizedString()) {
1104 PrintLiteralIndented("NAME", literal->value(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 } else {
1106 PrintIndentedVisit("KEY", node->key());
1107 }
1108}
1109
1110
1111void AstPrinter::VisitCall(Call* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001112 IndentedScope indent(this, "CALL");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 Visit(node->expression());
1114 PrintArguments(node->arguments());
1115}
1116
1117
1118void AstPrinter::VisitCallNew(CallNew* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001119 IndentedScope indent(this, "CALL NEW");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 Visit(node->expression());
1121 PrintArguments(node->arguments());
1122}
1123
1124
1125void AstPrinter::VisitCallRuntime(CallRuntime* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001126 IndentedScope indent(this, "CALL RUNTIME");
1127 PrintLiteralIndented("NAME", node->name(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 PrintArguments(node->arguments());
1129}
1130
1131
1132void AstPrinter::VisitUnaryOperation(UnaryOperation* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001133 IndentedScope indent(this, Token::Name(node->op()));
1134 Visit(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135}
1136
1137
1138void AstPrinter::VisitCountOperation(CountOperation* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001139 EmbeddedVector<char, 128> buf;
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001140 OS::SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"),
1141 Token::Name(node->op()));
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001142 IndentedScope indent(this, buf.start());
1143 Visit(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144}
1145
1146
1147void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001148 IndentedScope indent(this, Token::Name(node->op()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 Visit(node->left());
1150 Visit(node->right());
1151}
1152
1153
1154void AstPrinter::VisitCompareOperation(CompareOperation* node) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001155 IndentedScope indent(this, Token::Name(node->op()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 Visit(node->left());
1157 Visit(node->right());
1158}
1159
1160
1161void AstPrinter::VisitThisFunction(ThisFunction* node) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001162 IndentedScope indent(this, "THIS-FUNCTION");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163}
1164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165#endif // DEBUG
1166
1167} } // namespace v8::internal