1 // Copyright 2012 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.
9 #include "prettyprinter.h"
18 PrettyPrinter::PrettyPrinter(Zone* zone) {
22 InitializeAstVisitor(zone);
26 PrettyPrinter::~PrettyPrinter() {
31 void PrettyPrinter::VisitBlock(Block* node) {
32 if (!node->is_initializer_block()) Print("{ ");
33 PrintStatements(node->statements());
34 if (node->statements()->length() > 0) Print(" ");
35 if (!node->is_initializer_block()) Print("}");
39 void PrettyPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
41 PrintLiteral(node->proxy()->name(), false);
46 void PrettyPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
48 PrintLiteral(node->proxy()->name(), false);
50 PrintFunctionLiteral(node->fun());
55 void PrettyPrinter::VisitModuleDeclaration(ModuleDeclaration* node) {
57 PrintLiteral(node->proxy()->name(), false);
59 Visit(node->module());
64 void PrettyPrinter::VisitImportDeclaration(ImportDeclaration* node) {
66 PrintLiteral(node->proxy()->name(), false);
68 Visit(node->module());
73 void PrettyPrinter::VisitExportDeclaration(ExportDeclaration* node) {
75 PrintLiteral(node->proxy()->name(), false);
80 void PrettyPrinter::VisitModuleLiteral(ModuleLiteral* node) {
81 VisitBlock(node->body());
85 void PrettyPrinter::VisitModuleVariable(ModuleVariable* node) {
90 void PrettyPrinter::VisitModulePath(ModulePath* node) {
91 Visit(node->module());
93 PrintLiteral(node->name(), false);
97 void PrettyPrinter::VisitModuleUrl(ModuleUrl* node) {
99 PrintLiteral(node->url(), true);
103 void PrettyPrinter::VisitModuleStatement(ModuleStatement* node) {
105 PrintLiteral(node->proxy()->name(), false);
111 void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) {
112 Visit(node->expression());
117 void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) {
122 void PrettyPrinter::VisitIfStatement(IfStatement* node) {
124 Visit(node->condition());
126 Visit(node->then_statement());
127 if (node->HasElseStatement()) {
129 Visit(node->else_statement());
134 void PrettyPrinter::VisitContinueStatement(ContinueStatement* node) {
136 ZoneStringList* labels = node->target()->labels();
137 if (labels != NULL) {
139 ASSERT(labels->length() > 0); // guaranteed to have at least one entry
140 PrintLiteral(labels->at(0), false); // any label from the list is fine
146 void PrettyPrinter::VisitBreakStatement(BreakStatement* node) {
148 ZoneStringList* labels = node->target()->labels();
149 if (labels != NULL) {
151 ASSERT(labels->length() > 0); // guaranteed to have at least one entry
152 PrintLiteral(labels->at(0), false); // any label from the list is fine
158 void PrettyPrinter::VisitReturnStatement(ReturnStatement* node) {
160 Visit(node->expression());
165 void PrettyPrinter::VisitWithStatement(WithStatement* node) {
167 Visit(node->expression());
169 Visit(node->statement());
173 void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) {
174 PrintLabels(node->labels());
178 ZoneList<CaseClause*>* cases = node->cases();
179 for (int i = 0; i < cases->length(); i++)
185 void PrettyPrinter::VisitCaseClause(CaseClause* clause) {
186 if (clause->is_default()) {
190 Visit(clause->label());
193 PrintStatements(clause->statements());
194 if (clause->statements()->length() > 0)
199 void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
200 PrintLabels(node->labels());
209 void PrettyPrinter::VisitWhileStatement(WhileStatement* node) {
210 PrintLabels(node->labels());
218 void PrettyPrinter::VisitForStatement(ForStatement* node) {
219 PrintLabels(node->labels());
221 if (node->init() != NULL) {
227 if (node->cond() != NULL) Visit(node->cond());
229 if (node->next() != NULL) {
230 Visit(node->next()); // prints extra ';', unfortunately
231 // to fix: should use Expression for next
238 void PrettyPrinter::VisitForInStatement(ForInStatement* node) {
239 PrintLabels(node->labels());
243 Visit(node->enumerable());
249 void PrettyPrinter::VisitForOfStatement(ForOfStatement* node) {
250 PrintLabels(node->labels());
254 Visit(node->iterable());
260 void PrettyPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
262 Visit(node->try_block());
264 const bool quote = false;
265 PrintLiteral(node->variable()->name(), quote);
267 Visit(node->catch_block());
271 void PrettyPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
273 Visit(node->try_block());
275 Visit(node->finally_block());
279 void PrettyPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
284 void PrettyPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
286 PrintFunctionLiteral(node);
291 void PrettyPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
293 PrintLiteral(node->name(), false);
298 void PrettyPrinter::VisitConditional(Conditional* node) {
299 Visit(node->condition());
301 Visit(node->then_expression());
303 Visit(node->else_expression());
307 void PrettyPrinter::VisitLiteral(Literal* node) {
308 PrintLiteral(node->value(), true);
312 void PrettyPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
314 PrintLiteral(node->pattern(), false);
316 PrintLiteral(node->flags(), false);
321 void PrettyPrinter::VisitObjectLiteral(ObjectLiteral* node) {
323 for (int i = 0; i < node->properties()->length(); i++) {
324 if (i != 0) Print(",");
325 ObjectLiteral::Property* property = node->properties()->at(i);
327 Visit(property->key());
329 Visit(property->value());
335 void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) {
337 for (int i = 0; i < node->values()->length(); i++) {
338 if (i != 0) Print(",");
339 Visit(node->values()->at(i));
345 void PrettyPrinter::VisitVariableProxy(VariableProxy* node) {
346 PrintLiteral(node->name(), false);
350 void PrettyPrinter::VisitAssignment(Assignment* node) {
351 Visit(node->target());
352 Print(" %s ", Token::String(node->op()));
353 Visit(node->value());
357 void PrettyPrinter::VisitYield(Yield* node) {
359 Visit(node->expression());
363 void PrettyPrinter::VisitThrow(Throw* node) {
365 Visit(node->exception());
369 void PrettyPrinter::VisitProperty(Property* node) {
370 Expression* key = node->key();
371 Literal* literal = key->AsLiteral();
372 if (literal != NULL && literal->value()->IsInternalizedString()) {
376 PrintLiteral(literal->value(), false);
386 void PrettyPrinter::VisitCall(Call* node) {
387 Visit(node->expression());
388 PrintArguments(node->arguments());
392 void PrettyPrinter::VisitCallNew(CallNew* node) {
394 Visit(node->expression());
396 PrintArguments(node->arguments());
400 void PrettyPrinter::VisitCallRuntime(CallRuntime* node) {
402 PrintLiteral(node->name(), false);
403 PrintArguments(node->arguments());
407 void PrettyPrinter::VisitUnaryOperation(UnaryOperation* node) {
408 Token::Value op = node->op();
410 op == Token::DELETE || op == Token::TYPEOF || op == Token::VOID;
411 Print("(%s%s", Token::String(op), needsSpace ? " " : "");
412 Visit(node->expression());
417 void PrettyPrinter::VisitCountOperation(CountOperation* node) {
419 if (node->is_prefix()) Print("%s", Token::String(node->op()));
420 Visit(node->expression());
421 if (node->is_postfix()) Print("%s", Token::String(node->op()));
426 void PrettyPrinter::VisitBinaryOperation(BinaryOperation* node) {
429 Print(" %s ", Token::String(node->op()));
430 Visit(node->right());
435 void PrettyPrinter::VisitCompareOperation(CompareOperation* node) {
438 Print(" %s ", Token::String(node->op()));
439 Visit(node->right());
444 void PrettyPrinter::VisitThisFunction(ThisFunction* node) {
445 Print("<this-function>");
449 const char* PrettyPrinter::Print(AstNode* node) {
456 const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) {
458 ExpressionStatement* statement =
459 program->body()->at(0)->AsExpressionStatement();
460 Visit(statement->expression());
465 const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) {
467 PrintStatements(program->body());
473 void PrettyPrinter::PrintOut(Zone* zone, AstNode* node) {
474 PrettyPrinter printer(zone);
475 PrintF("%s", printer.Print(node));
479 void PrettyPrinter::Init() {
481 ASSERT(output_ == NULL);
482 const int initial_size = 256;
483 output_ = NewArray<char>(initial_size);
484 size_ = initial_size;
491 void PrettyPrinter::Print(const char* format, ...) {
494 va_start(arguments, format);
495 int n = OS::VSNPrintF(Vector<char>(output_, size_) + pos_,
501 // there was enough space - we are done
505 // there was not enough space - allocate more and try again
506 const int slack = 32;
507 int new_size = size_ + (size_ >> 1) + slack;
508 char* new_output = NewArray<char>(new_size);
509 OS::MemCopy(new_output, output_, pos_);
510 DeleteArray(output_);
511 output_ = new_output;
518 void PrettyPrinter::PrintStatements(ZoneList<Statement*>* statements) {
519 if (statements == NULL) return;
520 for (int i = 0; i < statements->length(); i++) {
521 if (i != 0) Print(" ");
522 Visit(statements->at(i));
527 void PrettyPrinter::PrintLabels(ZoneStringList* labels) {
528 if (labels != NULL) {
529 for (int i = 0; i < labels->length(); i++) {
530 PrintLiteral(labels->at(i), false);
537 void PrettyPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
539 for (int i = 0; i < arguments->length(); i++) {
540 if (i != 0) Print(", ");
541 Visit(arguments->at(i));
547 void PrettyPrinter::PrintLiteral(Handle<Object> value, bool quote) {
548 Object* object = *value;
549 if (object->IsString()) {
550 String* string = String::cast(object);
551 if (quote) Print("\"");
552 for (int i = 0; i < string->length(); i++) {
553 Print("%c", string->Get(i));
555 if (quote) Print("\"");
556 } else if (object->IsNull()) {
558 } else if (object->IsTrue()) {
560 } else if (object->IsFalse()) {
562 } else if (object->IsUndefined()) {
564 } else if (object->IsNumber()) {
565 Print("%g", object->Number());
566 } else if (object->IsJSObject()) {
567 // regular expression
568 if (object->IsJSFunction()) {
569 Print("JS-Function");
570 } else if (object->IsJSArray()) {
571 Print("JS-array[%u]", JSArray::cast(object)->length());
572 } else if (object->IsJSObject()) {
577 } else if (object->IsFixedArray()) {
580 Print("<unknown literal %p>", object);
585 void PrettyPrinter::PrintParameters(Scope* scope) {
587 for (int i = 0; i < scope->num_parameters(); i++) {
588 if (i > 0) Print(", ");
589 PrintLiteral(scope->parameter(i)->name(), false);
595 void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
596 for (int i = 0; i < declarations->length(); i++) {
597 if (i > 0) Print(" ");
598 Visit(declarations->at(i));
603 void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) {
605 PrintLiteral(function->name(), false);
606 PrintParameters(function->scope());
608 PrintDeclarations(function->scope()->declarations());
609 PrintStatements(function->body());
614 //-----------------------------------------------------------------------------
616 class IndentedScope BASE_EMBEDDED {
618 IndentedScope(AstPrinter* printer, const char* txt)
619 : ast_printer_(printer) {
620 ast_printer_->PrintIndented(txt);
621 ast_printer_->Print("\n");
622 ast_printer_->inc_indent();
625 virtual ~IndentedScope() {
626 ast_printer_->dec_indent();
630 AstPrinter* ast_printer_;
634 //-----------------------------------------------------------------------------
637 AstPrinter::AstPrinter(Zone* zone) : PrettyPrinter(zone), indent_(0) {
641 AstPrinter::~AstPrinter() {
642 ASSERT(indent_ == 0);
646 void AstPrinter::PrintIndented(const char* txt) {
647 for (int i = 0; i < indent_; i++) {
654 void AstPrinter::PrintLiteralIndented(const char* info,
655 Handle<Object> value,
659 PrintLiteral(value, quote);
664 void AstPrinter::PrintLiteralWithModeIndented(const char* info,
666 Handle<Object> value) {
668 PrintLiteralIndented(info, value, true);
670 EmbeddedVector<char, 256> buf;
671 int pos = OS::SNPrintF(buf, "%s (mode = %s", info,
672 Variable::Mode2String(var->mode()));
673 OS::SNPrintF(buf + pos, ")");
674 PrintLiteralIndented(buf.start(), value, true);
679 void AstPrinter::PrintLabelsIndented(ZoneStringList* labels) {
680 if (labels == NULL || labels->length() == 0) return;
681 PrintIndented("LABELS ");
687 void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
688 IndentedScope indent(this, s);
693 const char* AstPrinter::PrintProgram(FunctionLiteral* program) {
695 { IndentedScope indent(this, "FUNC");
696 PrintLiteralIndented("NAME", program->name(), true);
697 PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true);
698 PrintParameters(program->scope());
699 PrintDeclarations(program->scope()->declarations());
700 PrintStatements(program->body());
706 void AstPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
707 if (declarations->length() > 0) {
708 IndentedScope indent(this, "DECLS");
709 for (int i = 0; i < declarations->length(); i++) {
710 Visit(declarations->at(i));
716 void AstPrinter::PrintParameters(Scope* scope) {
717 if (scope->num_parameters() > 0) {
718 IndentedScope indent(this, "PARAMS");
719 for (int i = 0; i < scope->num_parameters(); i++) {
720 PrintLiteralWithModeIndented("VAR", scope->parameter(i),
721 scope->parameter(i)->name());
727 void AstPrinter::PrintStatements(ZoneList<Statement*>* statements) {
728 for (int i = 0; i < statements->length(); i++) {
729 Visit(statements->at(i));
734 void AstPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
735 for (int i = 0; i < arguments->length(); i++) {
736 Visit(arguments->at(i));
741 void AstPrinter::VisitBlock(Block* node) {
742 const char* block_txt = node->is_initializer_block() ? "BLOCK INIT" : "BLOCK";
743 IndentedScope indent(this, block_txt);
744 PrintStatements(node->statements());
748 // TODO(svenpanne) Start with IndentedScope.
749 void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
750 PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()),
751 node->proxy()->var(),
752 node->proxy()->name());
756 // TODO(svenpanne) Start with IndentedScope.
757 void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
758 PrintIndented("FUNCTION ");
759 PrintLiteral(node->proxy()->name(), true);
760 Print(" = function ");
761 PrintLiteral(node->fun()->name(), false);
766 void AstPrinter::VisitModuleDeclaration(ModuleDeclaration* node) {
767 IndentedScope indent(this, "MODULE");
768 PrintLiteralIndented("NAME", node->proxy()->name(), true);
769 Visit(node->module());
773 void AstPrinter::VisitImportDeclaration(ImportDeclaration* node) {
774 IndentedScope indent(this, "IMPORT");
775 PrintLiteralIndented("NAME", node->proxy()->name(), true);
776 Visit(node->module());
780 void AstPrinter::VisitExportDeclaration(ExportDeclaration* node) {
781 IndentedScope indent(this, "EXPORT ");
782 PrintLiteral(node->proxy()->name(), true);
786 void AstPrinter::VisitModuleLiteral(ModuleLiteral* node) {
787 IndentedScope indent(this, "MODULE LITERAL");
788 VisitBlock(node->body());
792 void AstPrinter::VisitModuleVariable(ModuleVariable* node) {
793 IndentedScope indent(this, "MODULE VARIABLE");
794 Visit(node->proxy());
798 void AstPrinter::VisitModulePath(ModulePath* node) {
799 IndentedScope indent(this, "MODULE PATH");
800 PrintIndentedVisit("MODULE PATH PARENT", node->module());
801 PrintLiteralIndented("NAME", node->name(), true);
805 void AstPrinter::VisitModuleUrl(ModuleUrl* node) {
806 PrintLiteralIndented("URL", node->url(), true);
810 void AstPrinter::VisitModuleStatement(ModuleStatement* node) {
811 IndentedScope indent(this, "MODULE STATEMENT");
812 PrintLiteralIndented("NAME", node->proxy()->name(), true);
813 PrintStatements(node->body()->statements());
817 void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) {
818 IndentedScope indent(this, "EXPRESSION STATEMENT");
819 Visit(node->expression());
823 void AstPrinter::VisitEmptyStatement(EmptyStatement* node) {
824 IndentedScope indent(this, "EMPTY");
828 void AstPrinter::VisitIfStatement(IfStatement* node) {
829 IndentedScope indent(this, "IF");
830 PrintIndentedVisit("CONDITION", node->condition());
831 PrintIndentedVisit("THEN", node->then_statement());
832 if (node->HasElseStatement()) {
833 PrintIndentedVisit("ELSE", node->else_statement());
838 void AstPrinter::VisitContinueStatement(ContinueStatement* node) {
839 IndentedScope indent(this, "CONTINUE");
840 PrintLabelsIndented(node->target()->labels());
844 void AstPrinter::VisitBreakStatement(BreakStatement* node) {
845 IndentedScope indent(this, "BREAK");
846 PrintLabelsIndented(node->target()->labels());
850 void AstPrinter::VisitReturnStatement(ReturnStatement* node) {
851 IndentedScope indent(this, "RETURN");
852 Visit(node->expression());
856 void AstPrinter::VisitWithStatement(WithStatement* node) {
857 IndentedScope indent(this, "WITH");
858 PrintIndentedVisit("OBJECT", node->expression());
859 PrintIndentedVisit("BODY", node->statement());
863 void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
864 IndentedScope indent(this, "SWITCH");
865 PrintLabelsIndented(node->labels());
866 PrintIndentedVisit("TAG", node->tag());
867 for (int i = 0; i < node->cases()->length(); i++) {
868 Visit(node->cases()->at(i));
873 void AstPrinter::VisitCaseClause(CaseClause* clause) {
874 if (clause->is_default()) {
875 IndentedScope indent(this, "DEFAULT");
876 PrintStatements(clause->statements());
878 IndentedScope indent(this, "CASE");
879 Visit(clause->label());
880 PrintStatements(clause->statements());
885 void AstPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
886 IndentedScope indent(this, "DO");
887 PrintLabelsIndented(node->labels());
888 PrintIndentedVisit("BODY", node->body());
889 PrintIndentedVisit("COND", node->cond());
893 void AstPrinter::VisitWhileStatement(WhileStatement* node) {
894 IndentedScope indent(this, "WHILE");
895 PrintLabelsIndented(node->labels());
896 PrintIndentedVisit("COND", node->cond());
897 PrintIndentedVisit("BODY", node->body());
901 void AstPrinter::VisitForStatement(ForStatement* node) {
902 IndentedScope indent(this, "FOR");
903 PrintLabelsIndented(node->labels());
904 if (node->init()) PrintIndentedVisit("INIT", node->init());
905 if (node->cond()) PrintIndentedVisit("COND", node->cond());
906 PrintIndentedVisit("BODY", node->body());
907 if (node->next()) PrintIndentedVisit("NEXT", node->next());
911 void AstPrinter::VisitForInStatement(ForInStatement* node) {
912 IndentedScope indent(this, "FOR IN");
913 PrintIndentedVisit("FOR", node->each());
914 PrintIndentedVisit("IN", node->enumerable());
915 PrintIndentedVisit("BODY", node->body());
919 void AstPrinter::VisitForOfStatement(ForOfStatement* node) {
920 IndentedScope indent(this, "FOR OF");
921 PrintIndentedVisit("FOR", node->each());
922 PrintIndentedVisit("OF", node->iterable());
923 PrintIndentedVisit("BODY", node->body());
927 void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
928 IndentedScope indent(this, "TRY CATCH");
929 PrintIndentedVisit("TRY", node->try_block());
930 PrintLiteralWithModeIndented("CATCHVAR",
932 node->variable()->name());
933 PrintIndentedVisit("CATCH", node->catch_block());
937 void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
938 IndentedScope indent(this, "TRY FINALLY");
939 PrintIndentedVisit("TRY", node->try_block());
940 PrintIndentedVisit("FINALLY", node->finally_block());
944 void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
945 IndentedScope indent(this, "DEBUGGER");
949 void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
950 IndentedScope indent(this, "FUNC LITERAL");
951 PrintLiteralIndented("NAME", node->name(), false);
952 PrintLiteralIndented("INFERRED NAME", node->inferred_name(), false);
953 PrintParameters(node->scope());
954 // We don't want to see the function literal in this case: it
955 // will be printed via PrintProgram when the code for it is
957 // PrintStatements(node->body());
961 void AstPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
962 IndentedScope indent(this, "NATIVE FUNC LITERAL");
963 PrintLiteralIndented("NAME", node->name(), false);
967 void AstPrinter::VisitConditional(Conditional* node) {
968 IndentedScope indent(this, "CONDITIONAL");
969 PrintIndentedVisit("CONDITION", node->condition());
970 PrintIndentedVisit("THEN", node->then_expression());
971 PrintIndentedVisit("ELSE", node->else_expression());
975 // TODO(svenpanne) Start with IndentedScope.
976 void AstPrinter::VisitLiteral(Literal* node) {
977 PrintLiteralIndented("LITERAL", node->value(), true);
981 void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
982 IndentedScope indent(this, "REGEXP LITERAL");
983 PrintLiteralIndented("PATTERN", node->pattern(), false);
984 PrintLiteralIndented("FLAGS", node->flags(), false);
988 void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) {
989 IndentedScope indent(this, "OBJ LITERAL");
990 for (int i = 0; i < node->properties()->length(); i++) {
991 const char* prop_kind = NULL;
992 switch (node->properties()->at(i)->kind()) {
993 case ObjectLiteral::Property::CONSTANT:
994 prop_kind = "PROPERTY - CONSTANT";
996 case ObjectLiteral::Property::COMPUTED:
997 prop_kind = "PROPERTY - COMPUTED";
999 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1000 prop_kind = "PROPERTY - MATERIALIZED_LITERAL";
1002 case ObjectLiteral::Property::PROTOTYPE:
1003 prop_kind = "PROPERTY - PROTOTYPE";
1005 case ObjectLiteral::Property::GETTER:
1006 prop_kind = "PROPERTY - GETTER";
1008 case ObjectLiteral::Property::SETTER:
1009 prop_kind = "PROPERTY - SETTER";
1014 IndentedScope prop(this, prop_kind);
1015 PrintIndentedVisit("KEY", node->properties()->at(i)->key());
1016 PrintIndentedVisit("VALUE", node->properties()->at(i)->value());
1021 void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) {
1022 IndentedScope indent(this, "ARRAY LITERAL");
1023 if (node->values()->length() > 0) {
1024 IndentedScope indent(this, "VALUES");
1025 for (int i = 0; i < node->values()->length(); i++) {
1026 Visit(node->values()->at(i));
1032 // TODO(svenpanne) Start with IndentedScope.
1033 void AstPrinter::VisitVariableProxy(VariableProxy* node) {
1034 Variable* var = node->var();
1035 EmbeddedVector<char, 128> buf;
1036 int pos = OS::SNPrintF(buf, "VAR PROXY");
1037 switch (var->location()) {
1038 case Variable::UNALLOCATED:
1040 case Variable::PARAMETER:
1041 OS::SNPrintF(buf + pos, " parameter[%d]", var->index());
1043 case Variable::LOCAL:
1044 OS::SNPrintF(buf + pos, " local[%d]", var->index());
1046 case Variable::CONTEXT:
1047 OS::SNPrintF(buf + pos, " context[%d]", var->index());
1049 case Variable::LOOKUP:
1050 OS::SNPrintF(buf + pos, " lookup");
1053 PrintLiteralWithModeIndented(buf.start(), var, node->name());
1057 void AstPrinter::VisitAssignment(Assignment* node) {
1058 IndentedScope indent(this, Token::Name(node->op()));
1059 Visit(node->target());
1060 Visit(node->value());
1064 void AstPrinter::VisitYield(Yield* node) {
1065 IndentedScope indent(this, "YIELD");
1066 Visit(node->expression());
1070 void AstPrinter::VisitThrow(Throw* node) {
1071 IndentedScope indent(this, "THROW");
1072 Visit(node->exception());
1076 void AstPrinter::VisitProperty(Property* node) {
1077 IndentedScope indent(this, "PROPERTY");
1079 Literal* literal = node->key()->AsLiteral();
1080 if (literal != NULL && literal->value()->IsInternalizedString()) {
1081 PrintLiteralIndented("NAME", literal->value(), false);
1083 PrintIndentedVisit("KEY", node->key());
1088 void AstPrinter::VisitCall(Call* node) {
1089 IndentedScope indent(this, "CALL");
1090 Visit(node->expression());
1091 PrintArguments(node->arguments());
1095 void AstPrinter::VisitCallNew(CallNew* node) {
1096 IndentedScope indent(this, "CALL NEW");
1097 Visit(node->expression());
1098 PrintArguments(node->arguments());
1102 void AstPrinter::VisitCallRuntime(CallRuntime* node) {
1103 IndentedScope indent(this, "CALL RUNTIME");
1104 PrintLiteralIndented("NAME", node->name(), false);
1105 PrintArguments(node->arguments());
1109 void AstPrinter::VisitUnaryOperation(UnaryOperation* node) {
1110 IndentedScope indent(this, Token::Name(node->op()));
1111 Visit(node->expression());
1115 void AstPrinter::VisitCountOperation(CountOperation* node) {
1116 EmbeddedVector<char, 128> buf;
1117 OS::SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"),
1118 Token::Name(node->op()));
1119 IndentedScope indent(this, buf.start());
1120 Visit(node->expression());
1124 void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
1125 IndentedScope indent(this, Token::Name(node->op()));
1126 Visit(node->left());
1127 Visit(node->right());
1131 void AstPrinter::VisitCompareOperation(CompareOperation* node) {
1132 IndentedScope indent(this, Token::Name(node->op()));
1133 Visit(node->left());
1134 Visit(node->right());
1138 void AstPrinter::VisitThisFunction(ThisFunction* node) {
1139 IndentedScope indent(this, "THIS-FUNCTION");
1144 } } // namespace v8::internal