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

Side by Side Diff: src/ast/prettyprinter.cc

Issue 1439693002: [runtime] Support Proxy setPrototypeOf trap (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@2015-11-09_new_Proxy_1417063011
Patch Set: merging with master Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast/prettyprinter.h ('k') | src/ast/scopeinfo.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/ast/prettyprinter.h" 5 #include "src/ast/prettyprinter.h"
6 6
7 #include <stdarg.h> 7 #include <stdarg.h>
8 8
9 #include "src/ast/ast-value-factory.h" 9 #include "src/ast/ast-value-factory.h"
10 #include "src/ast/scopes.h" 10 #include "src/ast/scopes.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 FindStatements(node->statements()); 92 FindStatements(node->statements());
93 } 93 }
94 94
95 95
96 void CallPrinter::VisitVariableDeclaration(VariableDeclaration* node) {} 96 void CallPrinter::VisitVariableDeclaration(VariableDeclaration* node) {}
97 97
98 98
99 void CallPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {} 99 void CallPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {}
100 100
101 101
102 void CallPrinter::VisitImportDeclaration(ImportDeclaration* node) { 102 void CallPrinter::VisitImportDeclaration(ImportDeclaration* node) {}
103 }
104 103
105 104
106 void CallPrinter::VisitExportDeclaration(ExportDeclaration* node) {} 105 void CallPrinter::VisitExportDeclaration(ExportDeclaration* node) {}
107 106
108 107
109 void CallPrinter::VisitExpressionStatement(ExpressionStatement* node) { 108 void CallPrinter::VisitExpressionStatement(ExpressionStatement* node) {
110 Find(node->expression()); 109 Find(node->expression());
111 } 110 }
112 111
113 112
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 457
459 458
460 PrettyPrinter::PrettyPrinter(Isolate* isolate) { 459 PrettyPrinter::PrettyPrinter(Isolate* isolate) {
461 output_ = NULL; 460 output_ = NULL;
462 size_ = 0; 461 size_ = 0;
463 pos_ = 0; 462 pos_ = 0;
464 InitializeAstVisitor(isolate); 463 InitializeAstVisitor(isolate);
465 } 464 }
466 465
467 466
468 PrettyPrinter::~PrettyPrinter() { 467 PrettyPrinter::~PrettyPrinter() { DeleteArray(output_); }
469 DeleteArray(output_);
470 }
471 468
472 469
473 void PrettyPrinter::VisitBlock(Block* node) { 470 void PrettyPrinter::VisitBlock(Block* node) {
474 if (!node->ignore_completion_value()) Print("{ "); 471 if (!node->ignore_completion_value()) Print("{ ");
475 PrintStatements(node->statements()); 472 PrintStatements(node->statements());
476 if (node->statements()->length() > 0) Print(" "); 473 if (node->statements()->length() > 0) Print(" ");
477 if (!node->ignore_completion_value()) Print("}"); 474 if (!node->ignore_completion_value()) Print("}");
478 } 475 }
479 476
480 477
(...skipping 28 matching lines...) Expand all
509 Print(";"); 506 Print(";");
510 } 507 }
511 508
512 509
513 void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) { 510 void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) {
514 Visit(node->expression()); 511 Visit(node->expression());
515 Print(";"); 512 Print(";");
516 } 513 }
517 514
518 515
519 void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) { 516 void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) { Print(";"); }
520 Print(";");
521 }
522 517
523 518
524 void PrettyPrinter::VisitSloppyBlockFunctionStatement( 519 void PrettyPrinter::VisitSloppyBlockFunctionStatement(
525 SloppyBlockFunctionStatement* node) { 520 SloppyBlockFunctionStatement* node) {
526 Visit(node->statement()); 521 Visit(node->statement());
527 } 522 }
528 523
529 524
530 void PrettyPrinter::VisitIfStatement(IfStatement* node) { 525 void PrettyPrinter::VisitIfStatement(IfStatement* node) {
531 Print("if ("); 526 Print("if (");
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 Visit(node->statement()); 572 Visit(node->statement());
578 } 573 }
579 574
580 575
581 void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) { 576 void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) {
582 PrintLabels(node->labels()); 577 PrintLabels(node->labels());
583 Print("switch ("); 578 Print("switch (");
584 Visit(node->tag()); 579 Visit(node->tag());
585 Print(") { "); 580 Print(") { ");
586 ZoneList<CaseClause*>* cases = node->cases(); 581 ZoneList<CaseClause*>* cases = node->cases();
587 for (int i = 0; i < cases->length(); i++) 582 for (int i = 0; i < cases->length(); i++) Visit(cases->at(i));
588 Visit(cases->at(i));
589 Print("}"); 583 Print("}");
590 } 584 }
591 585
592 586
593 void PrettyPrinter::VisitCaseClause(CaseClause* clause) { 587 void PrettyPrinter::VisitCaseClause(CaseClause* clause) {
594 if (clause->is_default()) { 588 if (clause->is_default()) {
595 Print("default"); 589 Print("default");
596 } else { 590 } else {
597 Print("case "); 591 Print("case ");
598 Visit(clause->label()); 592 Visit(clause->label());
599 } 593 }
600 Print(": "); 594 Print(": ");
601 PrintStatements(clause->statements()); 595 PrintStatements(clause->statements());
602 if (clause->statements()->length() > 0) 596 if (clause->statements()->length() > 0) Print(" ");
603 Print(" ");
604 } 597 }
605 598
606 599
607 void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) { 600 void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
608 PrintLabels(node->labels()); 601 PrintLabels(node->labels());
609 Print("do "); 602 Print("do ");
610 Visit(node->body()); 603 Visit(node->body());
611 Print(" while ("); 604 Print(" while (");
612 Visit(node->cond()); 605 Visit(node->cond());
613 Print(");"); 606 Print(");");
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
911 const char* PrettyPrinter::Print(AstNode* node) { 904 const char* PrettyPrinter::Print(AstNode* node) {
912 Init(); 905 Init();
913 Visit(node); 906 Visit(node);
914 return output_; 907 return output_;
915 } 908 }
916 909
917 910
918 const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) { 911 const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) {
919 Init(); 912 Init();
920 ExpressionStatement* statement = 913 ExpressionStatement* statement =
921 program->body()->at(0)->AsExpressionStatement(); 914 program->body()->at(0)->AsExpressionStatement();
922 Visit(statement->expression()); 915 Visit(statement->expression());
923 return output_; 916 return output_;
924 } 917 }
925 918
926 919
927 const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) { 920 const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) {
928 Init(); 921 Init();
929 PrintStatements(program->body()); 922 PrintStatements(program->body());
930 Print("\n"); 923 Print("\n");
931 return output_; 924 return output_;
(...skipping 15 matching lines...) Expand all
947 } 940 }
948 output_[0] = '\0'; 941 output_[0] = '\0';
949 pos_ = 0; 942 pos_ = 0;
950 } 943 }
951 944
952 945
953 void PrettyPrinter::Print(const char* format, ...) { 946 void PrettyPrinter::Print(const char* format, ...) {
954 for (;;) { 947 for (;;) {
955 va_list arguments; 948 va_list arguments;
956 va_start(arguments, format); 949 va_start(arguments, format);
957 int n = VSNPrintF(Vector<char>(output_, size_) + pos_, 950 int n = VSNPrintF(Vector<char>(output_, size_) + pos_, format, arguments);
958 format,
959 arguments);
960 va_end(arguments); 951 va_end(arguments);
961 952
962 if (n >= 0) { 953 if (n >= 0) {
963 // there was enough space - we are done 954 // there was enough space - we are done
964 pos_ += n; 955 pos_ += n;
965 return; 956 return;
966 } else { 957 } else {
967 // there was not enough space - allocate more and try again 958 // there was not enough space - allocate more and try again
968 const int slack = 32; 959 const int slack = 32;
969 int new_size = size_ + (size_ >> 1) + slack; 960 int new_size = size_ + (size_ >> 1) + slack;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 1036
1046 1037
1047 void PrettyPrinter::PrintLiteral(const AstRawString* value, bool quote) { 1038 void PrettyPrinter::PrintLiteral(const AstRawString* value, bool quote) {
1048 PrintLiteral(value->string(), quote); 1039 PrintLiteral(value->string(), quote);
1049 } 1040 }
1050 1041
1051 1042
1052 void PrettyPrinter::PrintParameters(Scope* scope) { 1043 void PrettyPrinter::PrintParameters(Scope* scope) {
1053 Print("("); 1044 Print("(");
1054 for (int i = 0; i < scope->num_parameters(); i++) { 1045 for (int i = 0; i < scope->num_parameters(); i++) {
1055 if (i > 0) Print(", "); 1046 if (i > 0) Print(", ");
1056 PrintLiteral(scope->parameter(i)->name(), false); 1047 PrintLiteral(scope->parameter(i)->name(), false);
1057 } 1048 }
1058 Print(")"); 1049 Print(")");
1059 } 1050 }
1060 1051
1061 1052
1062 void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) { 1053 void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
1063 for (int i = 0; i < declarations->length(); i++) { 1054 for (int i = 0; i < declarations->length(); i++) {
1064 if (i > 0) Print(" "); 1055 if (i > 0) Print(" ");
1065 Visit(declarations->at(i)); 1056 Visit(declarations->at(i));
1066 } 1057 }
1067 } 1058 }
1068 1059
1069 1060
1070 void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) { 1061 void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) {
1071 Print("function "); 1062 Print("function ");
1072 PrintLiteral(function->name(), false); 1063 PrintLiteral(function->name(), false);
1073 PrintParameters(function->scope()); 1064 PrintParameters(function->scope());
1074 Print(" { "); 1065 Print(" { ");
1075 PrintDeclarations(function->scope()->declarations()); 1066 PrintDeclarations(function->scope()->declarations());
1076 PrintStatements(function->body()); 1067 PrintStatements(function->body());
1077 Print(" }"); 1068 Print(" }");
1078 } 1069 }
1079 1070
1080 1071
1081 //----------------------------------------------------------------------------- 1072 //-----------------------------------------------------------------------------
1082 1073
1083 class IndentedScope BASE_EMBEDDED { 1074 class IndentedScope BASE_EMBEDDED {
1084 public: 1075 public:
1085 IndentedScope(AstPrinter* printer, const char* txt) 1076 IndentedScope(AstPrinter* printer, const char* txt) : ast_printer_(printer) {
1086 : ast_printer_(printer) {
1087 ast_printer_->PrintIndented(txt); 1077 ast_printer_->PrintIndented(txt);
1088 ast_printer_->Print("\n"); 1078 ast_printer_->Print("\n");
1089 ast_printer_->inc_indent(); 1079 ast_printer_->inc_indent();
1090 } 1080 }
1091 1081
1092 IndentedScope(AstPrinter* printer, const char* txt, int pos) 1082 IndentedScope(AstPrinter* printer, const char* txt, int pos)
1093 : ast_printer_(printer) { 1083 : ast_printer_(printer) {
1094 ast_printer_->PrintIndented(txt); 1084 ast_printer_->PrintIndented(txt);
1095 ast_printer_->Print(" at %d\n", pos); 1085 ast_printer_->Print(" at %d\n", pos);
1096 ast_printer_->inc_indent(); 1086 ast_printer_->inc_indent();
1097 } 1087 }
1098 1088
1099 virtual ~IndentedScope() { 1089 virtual ~IndentedScope() { ast_printer_->dec_indent(); }
1100 ast_printer_->dec_indent();
1101 }
1102 1090
1103 private: 1091 private:
1104 AstPrinter* ast_printer_; 1092 AstPrinter* ast_printer_;
1105 }; 1093 };
1106 1094
1107 1095
1108 //----------------------------------------------------------------------------- 1096 //-----------------------------------------------------------------------------
1109 1097
1110 1098
1111 AstPrinter::AstPrinter(Isolate* isolate) : PrettyPrinter(isolate), indent_(0) {} 1099 AstPrinter::AstPrinter(Isolate* isolate) : PrettyPrinter(isolate), indent_(0) {}
1112 1100
1113 1101
1114 AstPrinter::~AstPrinter() { 1102 AstPrinter::~AstPrinter() { DCHECK(indent_ == 0); }
1115 DCHECK(indent_ == 0);
1116 }
1117 1103
1118 1104
1119 void AstPrinter::PrintIndented(const char* txt) { 1105 void AstPrinter::PrintIndented(const char* txt) {
1120 for (int i = 0; i < indent_; i++) { 1106 for (int i = 0; i < indent_; i++) {
1121 Print(". "); 1107 Print(". ");
1122 } 1108 }
1123 Print(txt); 1109 Print(txt);
1124 } 1110 }
1125 1111
1126 1112
1127 void AstPrinter::PrintLiteralIndented(const char* info, 1113 void AstPrinter::PrintLiteralIndented(const char* info, Handle<Object> value,
1128 Handle<Object> value,
1129 bool quote) { 1114 bool quote) {
1130 PrintIndented(info); 1115 PrintIndented(info);
1131 Print(" "); 1116 Print(" ");
1132 PrintLiteral(value, quote); 1117 PrintLiteral(value, quote);
1133 Print("\n"); 1118 Print("\n");
1134 } 1119 }
1135 1120
1136 1121
1137 void AstPrinter::PrintLiteralWithModeIndented(const char* info, 1122 void AstPrinter::PrintLiteralWithModeIndented(const char* info, Variable* var,
1138 Variable* var,
1139 Handle<Object> value) { 1123 Handle<Object> value) {
1140 if (var == NULL) { 1124 if (var == NULL) {
1141 PrintLiteralIndented(info, value, true); 1125 PrintLiteralIndented(info, value, true);
1142 } else { 1126 } else {
1143 EmbeddedVector<char, 256> buf; 1127 EmbeddedVector<char, 256> buf;
1144 int pos = SNPrintF(buf, "%s (mode = %s", info, 1128 int pos = SNPrintF(buf, "%s (mode = %s", info,
1145 Variable::Mode2String(var->mode())); 1129 Variable::Mode2String(var->mode()));
1146 SNPrintF(buf + pos, ")"); 1130 SNPrintF(buf + pos, ")");
1147 PrintLiteralIndented(buf.start(), value, true); 1131 PrintLiteralIndented(buf.start(), value, true);
1148 } 1132 }
1149 } 1133 }
1150 1134
1151 1135
1152 void AstPrinter::PrintLabelsIndented(ZoneList<const AstRawString*>* labels) { 1136 void AstPrinter::PrintLabelsIndented(ZoneList<const AstRawString*>* labels) {
1153 if (labels == NULL || labels->length() == 0) return; 1137 if (labels == NULL || labels->length() == 0) return;
1154 PrintIndented("LABELS "); 1138 PrintIndented("LABELS ");
1155 PrintLabels(labels); 1139 PrintLabels(labels);
1156 Print("\n"); 1140 Print("\n");
1157 } 1141 }
1158 1142
1159 1143
1160 void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) { 1144 void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
1161 IndentedScope indent(this, s, node->position()); 1145 IndentedScope indent(this, s, node->position());
1162 Visit(node); 1146 Visit(node);
1163 } 1147 }
1164 1148
1165 1149
1166 const char* AstPrinter::PrintProgram(FunctionLiteral* program) { 1150 const char* AstPrinter::PrintProgram(FunctionLiteral* program) {
1167 Init(); 1151 Init();
1168 { IndentedScope indent(this, "FUNC", program->position()); 1152 {
1153 IndentedScope indent(this, "FUNC", program->position());
1169 PrintLiteralIndented("NAME", program->name(), true); 1154 PrintLiteralIndented("NAME", program->name(), true);
1170 PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true); 1155 PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true);
1171 PrintParameters(program->scope()); 1156 PrintParameters(program->scope());
1172 PrintDeclarations(program->scope()->declarations()); 1157 PrintDeclarations(program->scope()->declarations());
1173 PrintStatements(program->body()); 1158 PrintStatements(program->body());
1174 } 1159 }
1175 return Output(); 1160 return Output();
1176 } 1161 }
1177 1162
1178 1163
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 const char* block_txt = 1200 const char* block_txt =
1216 node->ignore_completion_value() ? "BLOCK NOCOMPLETIONS" : "BLOCK"; 1201 node->ignore_completion_value() ? "BLOCK NOCOMPLETIONS" : "BLOCK";
1217 IndentedScope indent(this, block_txt, node->position()); 1202 IndentedScope indent(this, block_txt, node->position());
1218 PrintStatements(node->statements()); 1203 PrintStatements(node->statements());
1219 } 1204 }
1220 1205
1221 1206
1222 // TODO(svenpanne) Start with IndentedScope. 1207 // TODO(svenpanne) Start with IndentedScope.
1223 void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) { 1208 void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
1224 PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()), 1209 PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()),
1225 node->proxy()->var(), 1210 node->proxy()->var(), node->proxy()->name());
1226 node->proxy()->name());
1227 } 1211 }
1228 1212
1229 1213
1230 // TODO(svenpanne) Start with IndentedScope. 1214 // TODO(svenpanne) Start with IndentedScope.
1231 void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) { 1215 void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
1232 PrintIndented("FUNCTION "); 1216 PrintIndented("FUNCTION ");
1233 PrintLiteral(node->proxy()->name(), true); 1217 PrintLiteral(node->proxy()->name(), true);
1234 Print(" = function "); 1218 Print(" = function ");
1235 PrintLiteral(node->fun()->name(), false); 1219 PrintLiteral(node->fun()->name(), false);
1236 Print("\n"); 1220 Print("\n");
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1362 IndentedScope indent(this, "FOR OF", node->position()); 1346 IndentedScope indent(this, "FOR OF", node->position());
1363 PrintIndentedVisit("FOR", node->each()); 1347 PrintIndentedVisit("FOR", node->each());
1364 PrintIndentedVisit("OF", node->iterable()); 1348 PrintIndentedVisit("OF", node->iterable());
1365 PrintIndentedVisit("BODY", node->body()); 1349 PrintIndentedVisit("BODY", node->body());
1366 } 1350 }
1367 1351
1368 1352
1369 void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) { 1353 void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
1370 IndentedScope indent(this, "TRY CATCH", node->position()); 1354 IndentedScope indent(this, "TRY CATCH", node->position());
1371 PrintIndentedVisit("TRY", node->try_block()); 1355 PrintIndentedVisit("TRY", node->try_block());
1372 PrintLiteralWithModeIndented("CATCHVAR", 1356 PrintLiteralWithModeIndented("CATCHVAR", node->variable(),
1373 node->variable(),
1374 node->variable()->name()); 1357 node->variable()->name());
1375 PrintIndentedVisit("CATCH", node->catch_block()); 1358 PrintIndentedVisit("CATCH", node->catch_block());
1376 } 1359 }
1377 1360
1378 1361
1379 void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) { 1362 void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
1380 IndentedScope indent(this, "TRY FINALLY", node->position()); 1363 IndentedScope indent(this, "TRY FINALLY", node->position());
1381 PrintIndentedVisit("TRY", node->try_block()); 1364 PrintIndentedVisit("TRY", node->try_block());
1382 PrintIndentedVisit("FINALLY", node->finally_block()); 1365 PrintIndentedVisit("FINALLY", node->finally_block());
1383 } 1366 }
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 1639
1657 void AstPrinter::VisitSuperCallReference(SuperCallReference* node) { 1640 void AstPrinter::VisitSuperCallReference(SuperCallReference* node) {
1658 IndentedScope indent(this, "SUPER-CALL-REFERENCE", node->position()); 1641 IndentedScope indent(this, "SUPER-CALL-REFERENCE", node->position());
1659 } 1642 }
1660 1643
1661 1644
1662 #endif // DEBUG 1645 #endif // DEBUG
1663 1646
1664 } // namespace internal 1647 } // namespace internal
1665 } // namespace v8 1648 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/prettyprinter.h ('k') | src/ast/scopeinfo.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698