prettyprinter.cc 40.1 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/ast/prettyprinter.h"
6

7
#include <stdarg.h>
8

9 10
#include "src/ast/ast-value-factory.h"
#include "src/ast/scopes.h"
11
#include "src/base/platform/platform.h"
12
#include "src/common/globals.h"
13
#include "src/objects/objects-inl.h"
14
#include "src/strings/string-builder-inl.h"
15
#include "src/utils/vector.h"
16

17 18
namespace v8 {
namespace internal {
19

20 21
CallPrinter::CallPrinter(Isolate* isolate, bool is_user_js,
                         SpreadErrorInArgsHint error_in_spread_args)
22
    : builder_(new IncrementalStringBuilder(isolate)) {
23
  isolate_ = isolate;
24
  position_ = 0;
25
  num_prints_ = 0;
26 27
  found_ = false;
  done_ = false;
28 29 30
  is_call_error_ = false;
  is_iterator_error_ = false;
  is_async_iterator_error_ = false;
31 32
  destructuring_prop_ = nullptr;
  destructuring_assignment_ = nullptr;
33
  is_user_js_ = is_user_js;
34 35
  error_in_spread_args_ = error_in_spread_args;
  spread_arg_ = nullptr;
36
  function_kind_ = kNormalFunction;
37
  InitializeAstVisitor(isolate);
38 39
}

40
CallPrinter::~CallPrinter() = default;
41

42 43 44 45 46 47 48 49 50
CallPrinter::ErrorHint CallPrinter::GetErrorHint() const {
  if (is_call_error_) {
    if (is_iterator_error_) return ErrorHint::kCallAndNormalIterator;
    if (is_async_iterator_error_) return ErrorHint::kCallAndAsyncIterator;
  } else {
    if (is_iterator_error_) return ErrorHint::kNormalIterator;
    if (is_async_iterator_error_) return ErrorHint::kAsyncIterator;
  }
  return ErrorHint::kNone;
51 52
}

53 54
Handle<String> CallPrinter::Print(FunctionLiteral* program, int position) {
  num_prints_ = 0;
55 56
  position_ = position;
  Find(program);
57
  return builder_->Finish().ToHandleChecked();
58 59 60 61 62 63
}


void CallPrinter::Find(AstNode* node, bool print) {
  if (found_) {
    if (print) {
64
      int prev_num_prints = num_prints_;
65
      Visit(node);
66
      if (prev_num_prints != num_prints_) return;
67 68 69 70 71 72 73
    }
    Print("(intermediate value)");
  } else {
    Visit(node);
  }
}

74 75 76
void CallPrinter::Print(const char* str) {
  if (!found_ || done_) return;
  num_prints_++;
77
  builder_->AppendCString(str);
78 79
}

80
void CallPrinter::Print(Handle<String> str) {
81
  if (!found_ || done_) return;
82
  num_prints_++;
83
  builder_->AppendString(str);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
}

void CallPrinter::VisitBlock(Block* node) {
  FindStatements(node->statements());
}


void CallPrinter::VisitVariableDeclaration(VariableDeclaration* node) {}


void CallPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {}


void CallPrinter::VisitExpressionStatement(ExpressionStatement* node) {
  Find(node->expression());
}


void CallPrinter::VisitEmptyStatement(EmptyStatement* node) {}


105 106 107 108 109 110
void CallPrinter::VisitSloppyBlockFunctionStatement(
    SloppyBlockFunctionStatement* node) {
  Find(node->statement());
}


111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
void CallPrinter::VisitIfStatement(IfStatement* node) {
  Find(node->condition());
  Find(node->then_statement());
  if (node->HasElseStatement()) {
    Find(node->else_statement());
  }
}


void CallPrinter::VisitContinueStatement(ContinueStatement* node) {}


void CallPrinter::VisitBreakStatement(BreakStatement* node) {}


void CallPrinter::VisitReturnStatement(ReturnStatement* node) {
  Find(node->expression());
}


void CallPrinter::VisitWithStatement(WithStatement* node) {
  Find(node->expression());
  Find(node->statement());
}


void CallPrinter::VisitSwitchStatement(SwitchStatement* node) {
  Find(node->tag());
139 140 141
  for (CaseClause* clause : *node->cases()) {
    if (!clause->is_default()) Find(clause->label());
    FindStatements(clause->statements());
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  }
}


void CallPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
  Find(node->body());
  Find(node->cond());
}


void CallPrinter::VisitWhileStatement(WhileStatement* node) {
  Find(node->cond());
  Find(node->body());
}


void CallPrinter::VisitForStatement(ForStatement* node) {
159
  if (node->init() != nullptr) {
160 161
    Find(node->init());
  }
162 163
  if (node->cond() != nullptr) Find(node->cond());
  if (node->next() != nullptr) Find(node->next());
164 165 166 167 168 169
  Find(node->body());
}


void CallPrinter::VisitForInStatement(ForInStatement* node) {
  Find(node->each());
170
  Find(node->subject());
171 172 173 174 175
  Find(node->body());
}


void CallPrinter::VisitForOfStatement(ForOfStatement* node) {
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  Find(node->each());

  // Check the subject's position in case there was a GetIterator error.
  bool was_found = false;
  if (node->subject()->position() == position_) {
    is_async_iterator_error_ = node->type() == IteratorType::kAsync;
    is_iterator_error_ = !is_async_iterator_error_;
    was_found = !found_;
    if (was_found) {
      found_ = true;
    }
  }
  Find(node->subject(), true);
  if (was_found) {
    done_ = true;
    found_ = false;
  }

194
  Find(node->body());
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}


void CallPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
  Find(node->try_block());
  Find(node->catch_block());
}


void CallPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
  Find(node->try_block());
  Find(node->finally_block());
}


void CallPrinter::VisitDebuggerStatement(DebuggerStatement* node) {}


void CallPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
214 215
  FunctionKind last_function_kind = function_kind_;
  function_kind_ = node->kind();
216
  FindStatements(node->body());
217
  function_kind_ = last_function_kind;
218 219 220 221 222
}


void CallPrinter::VisitClassLiteral(ClassLiteral* node) {
  if (node->extends()) Find(node->extends());
223 224 225 226 227
  for (int i = 0; i < node->public_members()->length(); i++) {
    Find(node->public_members()->at(i)->value());
  }
  for (int i = 0; i < node->private_members()->length(); i++) {
    Find(node->private_members()->at(i)->value());
228 229 230
  }
}

231 232
void CallPrinter::VisitInitializeClassMembersStatement(
    InitializeClassMembersStatement* node) {
233 234 235 236
  for (int i = 0; i < node->fields()->length(); i++) {
    Find(node->fields()->at(i)->value());
  }
}
237 238 239 240 241 242 243 244 245 246

void CallPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {}


void CallPrinter::VisitConditional(Conditional* node) {
  Find(node->condition());
  Find(node->then_expression());
  Find(node->else_expression());
}

247 248 249 250 251 252 253

void CallPrinter::VisitLiteral(Literal* node) {
  // TODO(adamk): Teach Literal how to print its values without
  // allocating on the heap.
  PrintLiteral(node->BuildValue(isolate_), true);
}

254 255 256

void CallPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
  Print("/");
257
  PrintLiteral(node->pattern(), false);
258
  Print("/");
259 260 261 262 263
  if (node->flags() & RegExp::kGlobal) Print("g");
  if (node->flags() & RegExp::kIgnoreCase) Print("i");
  if (node->flags() & RegExp::kMultiline) Print("m");
  if (node->flags() & RegExp::kUnicode) Print("u");
  if (node->flags() & RegExp::kSticky) Print("y");
264 265 266 267
}


void CallPrinter::VisitObjectLiteral(ObjectLiteral* node) {
268
  Print("{");
269 270 271
  for (int i = 0; i < node->properties()->length(); i++) {
    Find(node->properties()->at(i)->value());
  }
272
  Print("}");
273 274 275 276 277 278 279
}


void CallPrinter::VisitArrayLiteral(ArrayLiteral* node) {
  Print("[");
  for (int i = 0; i < node->values()->length(); i++) {
    if (i != 0) Print(",");
280 281 282 283 284 285 286 287 288 289 290
    Expression* subexpr = node->values()->at(i);
    Spread* spread = subexpr->AsSpread();
    if (spread != nullptr && !found_ &&
        position_ == spread->expression()->position()) {
      found_ = true;
      is_iterator_error_ = true;
      Find(spread->expression(), true);
      done_ = true;
      return;
    }
    Find(subexpr, true);
291 292 293 294 295 296
  }
  Print("]");
}


void CallPrinter::VisitVariableProxy(VariableProxy* node) {
297
  if (is_user_js_) {
298
    PrintLiteral(node->name(), false);
299 300 301
  } else {
    // Variable names of non-user code are meaningless due to minification.
    Print("(var)");
302
  }
303 304 305 306
}


void CallPrinter::VisitAssignment(Assignment* node) {
307 308 309 310
  bool was_found = false;
  if (node->target()->IsObjectLiteral()) {
    ObjectLiteral* target = node->target()->AsObjectLiteral();
    if (target->position() == position_) {
311
      was_found = !found_;
312 313 314 315 316 317 318 319 320 321 322
      found_ = true;
      destructuring_assignment_ = node;
    } else {
      for (ObjectLiteralProperty* prop : *target->properties()) {
        if (prop->value()->position() == position_) {
          was_found = !found_;
          found_ = true;
          destructuring_prop_ = prop;
          destructuring_assignment_ = node;
          break;
        }
323
      }
324
    }
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  }
  if (!was_found) {
    Find(node->target());
    if (node->target()->IsArrayLiteral()) {
      // Special case the visit for destructuring array assignment.
      bool was_found = false;
      if (node->value()->position() == position_) {
        is_iterator_error_ = true;
        was_found = !found_;
        found_ = true;
      }
      Find(node->value(), true);
      if (was_found) {
        done_ = true;
        found_ = false;
      }
    } else {
      Find(node->value());
343 344
    }
  } else {
345 346 347 348 349 350
    Find(node->value(), true);
  }

  if (was_found) {
    done_ = true;
    found_ = false;
351
  }
352 353
}

354 355 356 357
void CallPrinter::VisitCompoundAssignment(CompoundAssignment* node) {
  VisitAssignment(node);
}

358
void CallPrinter::VisitYield(Yield* node) { Find(node->expression()); }
359

360 361 362 363 364 365 366 367 368 369 370
void CallPrinter::VisitYieldStar(YieldStar* node) {
  if (!found_ && position_ == node->expression()->position()) {
    found_ = true;
    if (IsAsyncFunction(function_kind_))
      is_async_iterator_error_ = true;
    else
      is_iterator_error_ = true;
    Print("yield* ");
  }
  Find(node->expression());
}
371

372 373
void CallPrinter::VisitAwait(Await* node) { Find(node->expression()); }

374 375
void CallPrinter::VisitThrow(Throw* node) { Find(node->exception()); }

376 377 378
void CallPrinter::VisitOptionalChain(OptionalChain* node) {
  Find(node->expression());
}
379 380 381 382

void CallPrinter::VisitProperty(Property* node) {
  Expression* key = node->key();
  Literal* literal = key->AsLiteral();
383 384
  if (literal != nullptr &&
      literal->BuildValue(isolate_)->IsInternalizedString()) {
385
    Find(node->obj(), true);
386 387 388
    if (node->is_optional_chain_link()) {
      Print("?");
    }
389
    Print(".");
390 391 392
    // TODO(adamk): Teach Literal how to print its values without
    // allocating on the heap.
    PrintLiteral(literal->BuildValue(isolate_), false);
393 394
  } else {
    Find(node->obj(), true);
395 396 397
    if (node->is_optional_chain_link()) {
      Print("?.");
    }
398 399 400 401 402 403 404
    Print("[");
    Find(key, true);
    Print("]");
  }
}

void CallPrinter::VisitCall(Call* node) {
405 406
  bool was_found = false;
  if (node->position() == position_) {
407 408 409 410 411 412 413 414 415 416
    if (error_in_spread_args_ == SpreadErrorInArgsHint::kErrorInArgs) {
      found_ = true;
      spread_arg_ = node->arguments()->last()->AsSpread()->expression();
      Find(spread_arg_, true);

      done_ = true;
      found_ = false;
      return;
    }

417 418 419
    is_call_error_ = true;
    was_found = !found_;
  }
420

421
  if (was_found) {
422 423 424
    // Bail out if the error is caused by a direct call to a variable in
    // non-user JS code. The variable name is meaningless due to minification.
    if (!is_user_js_ && node->expression()->IsVariableProxy()) {
425 426 427 428 429
      done_ = true;
      return;
    }
    found_ = true;
  }
430
  Find(node->expression(), true);
431
  if (!was_found && !is_iterator_error_) Print("(...)");
432
  FindArguments(node->arguments());
433 434 435 436
  if (was_found) {
    done_ = true;
    found_ = false;
  }
437 438 439 440
}


void CallPrinter::VisitCallNew(CallNew* node) {
441 442
  bool was_found = false;
  if (node->position() == position_) {
443 444 445 446 447 448 449 450 451 452
    if (error_in_spread_args_ == SpreadErrorInArgsHint::kErrorInArgs) {
      found_ = true;
      spread_arg_ = node->arguments()->last()->AsSpread()->expression();
      Find(spread_arg_, true);

      done_ = true;
      found_ = false;
      return;
    }

453 454 455
    is_call_error_ = true;
    was_found = !found_;
  }
456
  if (was_found) {
457 458 459
    // Bail out if the error is caused by a direct call to a variable in
    // non-user JS code. The variable name is meaningless due to minification.
    if (!is_user_js_ && node->expression()->IsVariableProxy()) {
460 461 462 463 464
      done_ = true;
      return;
    }
    found_ = true;
  }
465
  Find(node->expression(), was_found || is_iterator_error_);
466
  FindArguments(node->arguments());
467 468 469 470
  if (was_found) {
    done_ = true;
    found_ = false;
  }
471 472 473 474 475 476 477 478 479 480 481 482
}


void CallPrinter::VisitCallRuntime(CallRuntime* node) {
  FindArguments(node->arguments());
}


void CallPrinter::VisitUnaryOperation(UnaryOperation* node) {
  Token::Value op = node->op();
  bool needsSpace =
      op == Token::DELETE || op == Token::TYPEOF || op == Token::VOID;
483 484 485
  Print("(");
  Print(Token::String(op));
  if (needsSpace) Print(" ");
486 487 488 489 490 491 492
  Find(node->expression(), true);
  Print(")");
}


void CallPrinter::VisitCountOperation(CountOperation* node) {
  Print("(");
493
  if (node->is_prefix()) Print(Token::String(node->op()));
494
  Find(node->expression(), true);
495
  if (node->is_postfix()) Print(Token::String(node->op()));
496 497 498 499 500 501 502
  Print(")");
}


void CallPrinter::VisitBinaryOperation(BinaryOperation* node) {
  Print("(");
  Find(node->left(), true);
503 504 505
  Print(" ");
  Print(Token::String(node->op()));
  Print(" ");
506 507 508 509
  Find(node->right(), true);
  Print(")");
}

510 511 512 513 514 515 516 517 518 519 520
void CallPrinter::VisitNaryOperation(NaryOperation* node) {
  Print("(");
  Find(node->first(), true);
  for (size_t i = 0; i < node->subsequent_length(); ++i) {
    Print(" ");
    Print(Token::String(node->op()));
    Print(" ");
    Find(node->subsequent(i), true);
  }
  Print(")");
}
521 522 523 524

void CallPrinter::VisitCompareOperation(CompareOperation* node) {
  Print("(");
  Find(node->left(), true);
525 526 527
  Print(" ");
  Print(Token::String(node->op()));
  Print(" ");
528 529 530 531 532
  Find(node->right(), true);
  Print(")");
}


533 534 535 536 537 538
void CallPrinter::VisitSpread(Spread* node) {
  Print("(...");
  Find(node->expression(), true);
  Print(")");
}

539 540 541 542
void CallPrinter::VisitEmptyParentheses(EmptyParentheses* node) {
  UNREACHABLE();
}

543 544
void CallPrinter::VisitGetTemplateObject(GetTemplateObject* node) {}

545 546 547 548 549 550
void CallPrinter::VisitTemplateLiteral(TemplateLiteral* node) {
  for (Expression* substitution : *node->substitutions()) {
    Find(substitution, true);
  }
}

551 552 553 554 555 556
void CallPrinter::VisitImportCallExpression(ImportCallExpression* node) {
  Print("ImportCall(");
  Find(node->argument(), true);
  Print(")");
}

557
void CallPrinter::VisitThisExpression(ThisExpression* node) { Print("this"); }
558

559 560 561
void CallPrinter::VisitSuperPropertyReference(SuperPropertyReference* node) {}


562 563 564
void CallPrinter::VisitSuperCallReference(SuperCallReference* node) {
  Print("super");
}
565 566


567
void CallPrinter::FindStatements(const ZonePtrList<Statement>* statements) {
568
  if (statements == nullptr) return;
569 570 571 572 573
  for (int i = 0; i < statements->length(); i++) {
    Find(statements->at(i));
  }
}

574
void CallPrinter::FindArguments(const ZonePtrList<Expression>* arguments) {
575 576 577 578 579 580
  if (found_) return;
  for (int i = 0; i < arguments->length(); i++) {
    Find(arguments->at(i));
  }
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
void CallPrinter::PrintLiteral(Handle<Object> value, bool quote) {
  if (value->IsString()) {
    if (quote) Print("\"");
    Print(Handle<String>::cast(value));
    if (quote) Print("\"");
  } else if (value->IsNull(isolate_)) {
    Print("null");
  } else if (value->IsTrue(isolate_)) {
    Print("true");
  } else if (value->IsFalse(isolate_)) {
    Print("false");
  } else if (value->IsUndefined(isolate_)) {
    Print("undefined");
  } else if (value->IsNumber()) {
    Print(isolate_->factory()->NumberToString(value));
  } else if (value->IsSymbol()) {
    // Symbols can only occur as literals if they were inserted by the parser.
    PrintLiteral(handle(Handle<Symbol>::cast(value)->description(), isolate_),
                 false);
600 601 602
  }
}

603

604
void CallPrinter::PrintLiteral(const AstRawString* value, bool quote) {
605
  PrintLiteral(value->string(), quote);
606 607 608 609 610
}

//-----------------------------------------------------------------------------


611 612
#ifdef DEBUG

613
const char* AstPrinter::Print(AstNode* node) {
614 615 616 617 618
  Init();
  Visit(node);
  return output_;
}

619
void AstPrinter::Init() {
620
  if (size_ == 0) {
621
    DCHECK_NULL(output_);
622 623 624 625 626 627 628 629
    const int initial_size = 256;
    output_ = NewArray<char>(initial_size);
    size_ = initial_size;
  }
  output_[0] = '\0';
  pos_ = 0;
}

630
void AstPrinter::Print(const char* format, ...) {
631 632 633
  for (;;) {
    va_list arguments;
    va_start(arguments, format);
634 635 636
    int n = VSNPrintF(Vector<char>(output_, size_) + pos_,
                      format,
                      arguments);
637 638
    va_end(arguments);

639
    if (n >= 0) {
640 641 642 643 644 645 646 647
      // there was enough space - we are done
      pos_ += n;
      return;
    } else {
      // there was not enough space - allocate more and try again
      const int slack = 32;
      int new_size = size_ + (size_ >> 1) + slack;
      char* new_output = NewArray<char>(new_size);
648
      MemCopy(new_output, output_, pos_);
649 650 651 652 653 654 655
      DeleteArray(output_);
      output_ = new_output;
      size_ = new_size;
    }
  }
}

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
void AstPrinter::PrintLiteral(Literal* literal, bool quote) {
  switch (literal->type()) {
    case Literal::kString:
      PrintLiteral(literal->AsRawString(), quote);
      break;
    case Literal::kSymbol:
      const char* symbol;
      switch (literal->AsSymbol()) {
        case AstSymbol::kHomeObjectSymbol:
          symbol = "HomeObjectSymbol";
      }
      Print("%s", symbol);
      break;
    case Literal::kSmi:
      Print("%d", Smi::ToInt(literal->AsSmiLiteral()));
      break;
    case Literal::kHeapNumber:
      Print("%g", literal->AsNumber());
      break;
    case Literal::kBigInt:
      Print("%sn", literal->AsBigInt().c_str());
      break;
    case Literal::kNull:
      Print("null");
      break;
    case Literal::kUndefined:
      Print("undefined");
      break;
    case Literal::kTheHole:
      Print("the hole");
      break;
    case Literal::kBoolean:
      if (literal->ToBooleanIsTrue()) {
        Print("true");
      } else {
        Print("false");
      }
      break;
694 695 696
  }
}

697
void AstPrinter::PrintLiteral(const AstRawString* value, bool quote) {
698 699 700 701 702 703 704 705 706 707
  if (quote) Print("\"");
  if (value != nullptr) {
    const char* format = value->is_one_byte() ? "%c" : "%lc";
    const int increment = value->is_one_byte() ? 1 : 2;
    const unsigned char* raw_bytes = value->raw_data();
    for (int i = 0; i < value->length(); i += increment) {
      Print(format, raw_bytes[i]);
    }
  }
  if (quote) Print("\"");
708 709
}

710 711 712 713 714 715 716 717 718 719
void AstPrinter::PrintLiteral(const AstConsString* value, bool quote) {
  if (quote) Print("\"");
  if (value != nullptr) {
    std::forward_list<const AstRawString*> strings = value->ToRawStrings();
    for (const AstRawString* string : strings) {
      PrintLiteral(string, false);
    }
  }
  if (quote) Print("\"");
}
720

721 722
//-----------------------------------------------------------------------------

723
class IndentedScope {
724
 public:
725
  IndentedScope(AstPrinter* printer, const char* txt)
726
      : ast_printer_(printer) {
727 728 729 730 731
    ast_printer_->PrintIndented(txt);
    ast_printer_->Print("\n");
    ast_printer_->inc_indent();
  }

732 733 734 735 736 737 738
  IndentedScope(AstPrinter* printer, const char* txt, int pos)
      : ast_printer_(printer) {
    ast_printer_->PrintIndented(txt);
    ast_printer_->Print(" at %d\n", pos);
    ast_printer_->inc_indent();
  }

739 740 741 742 743
  virtual ~IndentedScope() {
    ast_printer_->dec_indent();
  }

 private:
744
  AstPrinter* ast_printer_;
745 746 747 748 749
};


//-----------------------------------------------------------------------------

750 751 752
AstPrinter::AstPrinter(uintptr_t stack_limit)
    : output_(nullptr), size_(0), pos_(0), indent_(0) {
  InitializeAstVisitor(stack_limit);
753
}
754 755

AstPrinter::~AstPrinter() {
756
  DCHECK_EQ(indent_, 0);
757
  DeleteArray(output_);
758 759 760 761 762
}


void AstPrinter::PrintIndented(const char* txt) {
  for (int i = 0; i < indent_; i++) {
763
    Print(". ");
764
  }
jfb's avatar
jfb committed
765
  Print("%s", txt);
766 767
}

768
void AstPrinter::PrintLiteralIndented(const char* info, Literal* literal,
769 770 771
                                      bool quote) {
  PrintIndented(info);
  Print(" ");
772 773 774 775 776 777 778 779 780
  PrintLiteral(literal, quote);
  Print("\n");
}

void AstPrinter::PrintLiteralIndented(const char* info,
                                      const AstRawString* value, bool quote) {
  PrintIndented(info);
  Print(" ");
  PrintLiteral(value, quote);
781 782 783
  Print("\n");
}

784 785 786 787 788 789 790
void AstPrinter::PrintLiteralIndented(const char* info,
                                      const AstConsString* value, bool quote) {
  PrintIndented(info);
  Print(" ");
  PrintLiteral(value, quote);
  Print("\n");
}
791

792 793
void AstPrinter::PrintLiteralWithModeIndented(const char* info, Variable* var,
                                              const AstRawString* value) {
794
  if (var == nullptr) {
795 796
    PrintLiteralIndented(info, value, true);
  } else {
797
    EmbeddedVector<char, 256> buf;
798
    int pos =
799 800 801
        SNPrintF(buf, "%s (%p) (mode = %s, assigned = %s", info,
                 reinterpret_cast<void*>(var), VariableMode2String(var->mode()),
                 var->maybe_assigned() == kMaybeAssigned ? "true" : "false");
802
    SNPrintF(buf + pos, ")");
803
    PrintLiteralIndented(buf.begin(), value, true);
804 805 806
  }
}

807
void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
808 809 810 811
  if (node != nullptr) {
    IndentedScope indent(this, s, node->position());
    Visit(node);
  }
812 813 814 815 816
}


const char* AstPrinter::PrintProgram(FunctionLiteral* program) {
  Init();
817
  { IndentedScope indent(this, "FUNC", program->position());
818 819
    PrintIndented("KIND");
    Print(" %d\n", program->kind());
820 821
    PrintIndented("LITERAL ID");
    Print(" %d\n", program->function_literal_id());
822 823
    PrintIndented("SUSPEND COUNT");
    Print(" %d\n", program->suspend_count());
824 825 826 827
    PrintLiteralIndented("NAME", program->raw_name(), true);
    if (program->raw_inferred_name()) {
      PrintLiteralIndented("INFERRED NAME", program->raw_inferred_name(), true);
    }
828
    if (program->requires_instance_members_initializer()) {
829
      Print(" REQUIRES INSTANCE FIELDS INITIALIZER\n");
830
    }
831 832 833
    if (program->class_scope_has_private_brand()) {
      Print(" CLASS SCOPE HAS PRIVATE BRAND\n");
    }
834 835 836
    if (program->has_static_private_methods_or_accessors()) {
      Print(" HAS STATIC PRIVATE METHODS\n");
    }
837 838 839 840
    PrintParameters(program->scope());
    PrintDeclarations(program->scope()->declarations());
    PrintStatements(program->body());
  }
841
  return output_;
842 843 844
}


845
void AstPrinter::PrintOut(Isolate* isolate, AstNode* node) {
846
  AstPrinter printer(isolate->stack_guard()->real_climit());
847 848
  printer.Init();
  printer.Visit(node);
849
  PrintF("%s", printer.output_);
850 851
}

852 853
void AstPrinter::PrintDeclarations(Declaration::List* declarations) {
  if (!declarations->is_empty()) {
854
    IndentedScope indent(this, "DECLS");
855
    for (Declaration* decl : *declarations) Visit(decl);
856 857 858
  }
}

859
void AstPrinter::PrintParameters(DeclarationScope* scope) {
860
  if (scope->num_parameters() > 0) {
861
    IndentedScope indent(this, "PARAMS");
862
    for (int i = 0; i < scope->num_parameters(); i++) {
863
      PrintLiteralWithModeIndented("VAR", scope->parameter(i),
864
                                   scope->parameter(i)->raw_name());
865 866 867 868
    }
  }
}

869
void AstPrinter::PrintStatements(const ZonePtrList<Statement>* statements) {
870 871 872 873 874
  for (int i = 0; i < statements->length(); i++) {
    Visit(statements->at(i));
  }
}

875
void AstPrinter::PrintArguments(const ZonePtrList<Expression>* arguments) {
876 877 878 879 880 881 882
  for (int i = 0; i < arguments->length(); i++) {
    Visit(arguments->at(i));
  }
}


void AstPrinter::VisitBlock(Block* node) {
883 884
  const char* block_txt =
      node->ignore_completion_value() ? "BLOCK NOCOMPLETIONS" : "BLOCK";
885
  IndentedScope indent(this, block_txt, node->position());
886 887 888 889
  PrintStatements(node->statements());
}


890
// TODO(svenpanne) Start with IndentedScope.
891
void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
892 893
  PrintLiteralWithModeIndented("VARIABLE", node->var(),
                               node->var()->raw_name());
894 895 896
}


897
// TODO(svenpanne) Start with IndentedScope.
898 899
void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
  PrintIndented("FUNCTION ");
900
  PrintLiteral(node->var()->raw_name(), true);
901
  Print(" = function ");
902
  PrintLiteral(node->fun()->raw_name(), false);
903
  Print("\n");
904 905 906 907
}


void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) {
908
  IndentedScope indent(this, "EXPRESSION STATEMENT", node->position());
909 910 911 912 913
  Visit(node->expression());
}


void AstPrinter::VisitEmptyStatement(EmptyStatement* node) {
914
  IndentedScope indent(this, "EMPTY", node->position());
915 916 917
}


918 919 920 921 922 923
void AstPrinter::VisitSloppyBlockFunctionStatement(
    SloppyBlockFunctionStatement* node) {
  Visit(node->statement());
}


924
void AstPrinter::VisitIfStatement(IfStatement* node) {
925
  IndentedScope indent(this, "IF", node->position());
926
  PrintIndentedVisit("CONDITION", node->condition());
927 928 929 930 931 932 933 934
  PrintIndentedVisit("THEN", node->then_statement());
  if (node->HasElseStatement()) {
    PrintIndentedVisit("ELSE", node->else_statement());
  }
}


void AstPrinter::VisitContinueStatement(ContinueStatement* node) {
935
  IndentedScope indent(this, "CONTINUE", node->position());
936 937 938 939
}


void AstPrinter::VisitBreakStatement(BreakStatement* node) {
940
  IndentedScope indent(this, "BREAK", node->position());
941 942 943 944
}


void AstPrinter::VisitReturnStatement(ReturnStatement* node) {
945
  IndentedScope indent(this, "RETURN", node->position());
946
  Visit(node->expression());
947 948 949
}


950
void AstPrinter::VisitWithStatement(WithStatement* node) {
951
  IndentedScope indent(this, "WITH", node->position());
952 953
  PrintIndentedVisit("OBJECT", node->expression());
  PrintIndentedVisit("BODY", node->statement());
954 955 956 957
}


void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
958
  IndentedScope indent(this, "SWITCH", node->position());
959
  PrintIndentedVisit("TAG", node->tag());
960 961 962 963 964 965 966 967 968
  for (CaseClause* clause : *node->cases()) {
    if (clause->is_default()) {
      IndentedScope indent(this, "DEFAULT");
      PrintStatements(clause->statements());
    } else {
      IndentedScope indent(this, "CASE");
      Visit(clause->label());
      PrintStatements(clause->statements());
    }
969 970 971 972
  }
}


973
void AstPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
974
  IndentedScope indent(this, "DO", node->position());
975 976 977 978 979 980
  PrintIndentedVisit("BODY", node->body());
  PrintIndentedVisit("COND", node->cond());
}


void AstPrinter::VisitWhileStatement(WhileStatement* node) {
981
  IndentedScope indent(this, "WHILE", node->position());
982 983 984 985 986 987
  PrintIndentedVisit("COND", node->cond());
  PrintIndentedVisit("BODY", node->body());
}


void AstPrinter::VisitForStatement(ForStatement* node) {
988
  IndentedScope indent(this, "FOR", node->position());
989 990
  if (node->init()) PrintIndentedVisit("INIT", node->init());
  if (node->cond()) PrintIndentedVisit("COND", node->cond());
991
  PrintIndentedVisit("BODY", node->body());
992 993 994 995 996
  if (node->next()) PrintIndentedVisit("NEXT", node->next());
}


void AstPrinter::VisitForInStatement(ForInStatement* node) {
997
  IndentedScope indent(this, "FOR IN", node->position());
998
  PrintIndentedVisit("FOR", node->each());
999
  PrintIndentedVisit("IN", node->subject());
1000 1001 1002 1003
  PrintIndentedVisit("BODY", node->body());
}


1004
void AstPrinter::VisitForOfStatement(ForOfStatement* node) {
1005
  IndentedScope indent(this, "FOR OF", node->position());
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  const char* for_type;
  switch (node->type()) {
    case IteratorType::kNormal:
      for_type = "FOR";
      break;
    case IteratorType::kAsync:
      for_type = "FOR AWAIT";
      break;
  }
  PrintIndentedVisit(for_type, node->each());
  PrintIndentedVisit("OF", node->subject());
1017
  PrintIndentedVisit("BODY", node->body());
1018 1019 1020
}


1021
void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
1022
  IndentedScope indent(this, "TRY CATCH", node->position());
1023
  PrintIndentedVisit("TRY", node->try_block());
1024
  PrintIndented("CATCH PREDICTION");
yangguo's avatar
yangguo committed
1025
  const char* prediction = "";
1026
  switch (node->GetCatchPrediction(HandlerTable::UNCAUGHT)) {
1027 1028 1029 1030 1031 1032
    case HandlerTable::UNCAUGHT:
      prediction = "UNCAUGHT";
      break;
    case HandlerTable::CAUGHT:
      prediction = "CAUGHT";
      break;
1033 1034 1035
    case HandlerTable::DESUGARING:
      prediction = "DESUGARING";
      break;
1036 1037 1038
    case HandlerTable::ASYNC_AWAIT:
      prediction = "ASYNC_AWAIT";
      break;
1039 1040 1041
    case HandlerTable::UNCAUGHT_ASYNC_AWAIT:
      prediction = "UNCAUGHT_ASYNC_AWAIT";
      break;
1042 1043 1044 1045
    case HandlerTable::PROMISE:
      // Catch prediction resulting in promise rejections aren't
      // parsed by the parser.
      UNREACHABLE();
1046 1047
  }
  Print(" %s\n", prediction);
1048 1049 1050 1051
  if (node->scope()) {
    PrintLiteralWithModeIndented("CATCHVAR", node->scope()->catch_variable(),
                                 node->scope()->catch_variable()->raw_name());
  }
1052 1053 1054 1055 1056
  PrintIndentedVisit("CATCH", node->catch_block());
}

void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
  IndentedScope indent(this, "TRY FINALLY", node->position());
1057
  PrintIndentedVisit("TRY", node->try_block());
1058
  PrintIndentedVisit("FINALLY", node->finally_block());
1059
}
1060 1061

void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
1062
  IndentedScope indent(this, "DEBUGGER", node->position());
1063 1064 1065 1066
}


void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
1067
  IndentedScope indent(this, "FUNC LITERAL", node->position());
1068 1069
  PrintIndented("LITERAL ID");
  Print(" %d\n", node->function_literal_id());
1070 1071
  PrintLiteralIndented("NAME", node->raw_name(), false);
  PrintLiteralIndented("INFERRED NAME", node->raw_inferred_name(), false);
1072 1073 1074
  // We don't want to see the function literal in this case: it
  // will be printed via PrintProgram when the code for it is
  // generated.
1075
  // PrintParameters(node->scope());
1076 1077 1078 1079
  // PrintStatements(node->body());
}


arv@chromium.org's avatar
arv@chromium.org committed
1080
void AstPrinter::VisitClassLiteral(ClassLiteral* node) {
1081
  IndentedScope indent(this, "CLASS LITERAL", node->position());
1082
  PrintLiteralIndented("NAME", node->constructor()->raw_name(), false);
1083 1084 1085
  if (node->extends() != nullptr) {
    PrintIndentedVisit("EXTENDS", node->extends());
  }
1086 1087 1088 1089 1090 1091 1092
  Scope* outer = node->constructor()->scope()->outer_scope();
  if (outer->is_class_scope()) {
    Variable* brand = outer->AsClassScope()->brand();
    if (brand != nullptr) {
      PrintLiteralWithModeIndented("BRAND", brand, brand->raw_name());
    }
  }
1093 1094 1095 1096
  if (node->static_fields_initializer() != nullptr) {
    PrintIndentedVisit("STATIC FIELDS INITIALIZER",
                       node->static_fields_initializer());
  }
1097
  if (node->instance_members_initializer_function() != nullptr) {
1098
    PrintIndentedVisit("INSTANCE MEMBERS INITIALIZER",
1099
                       node->instance_members_initializer_function());
1100
  }
1101 1102
  PrintClassProperties(node->private_members());
  PrintClassProperties(node->public_members());
1103 1104
}

1105 1106
void AstPrinter::VisitInitializeClassMembersStatement(
    InitializeClassMembersStatement* node) {
1107
  IndentedScope indent(this, "INITIALIZE CLASS MEMBERS", node->position());
1108 1109 1110
  PrintClassProperties(node->fields());
}

1111
void AstPrinter::PrintClassProperties(
1112
    const ZonePtrList<ClassLiteral::Property>* properties) {
1113
  for (int i = 0; i < properties->length(); i++) {
1114
    ClassLiteral::Property* property = properties->at(i);
1115 1116
    const char* prop_kind = nullptr;
    switch (property->kind()) {
1117 1118
      case ClassLiteral::Property::METHOD:
        prop_kind = "METHOD";
1119
        break;
1120
      case ClassLiteral::Property::GETTER:
1121 1122
        prop_kind = "GETTER";
        break;
1123
      case ClassLiteral::Property::SETTER:
1124 1125
        prop_kind = "SETTER";
        break;
1126 1127
      case ClassLiteral::Property::FIELD:
        prop_kind = "FIELD";
1128
        break;
1129 1130
    }
    EmbeddedVector<char, 128> buf;
1131
    SNPrintF(buf, "PROPERTY%s%s - %s", property->is_static() ? " - STATIC" : "",
1132
             property->is_private() ? " - PRIVATE" : " - PUBLIC", prop_kind);
1133
    IndentedScope prop(this, buf.begin());
1134 1135 1136
    PrintIndentedVisit("KEY", properties->at(i)->key());
    PrintIndentedVisit("VALUE", properties->at(i)->value());
  }
arv@chromium.org's avatar
arv@chromium.org committed
1137 1138 1139
}


1140
void AstPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
1141
  IndentedScope indent(this, "NATIVE FUNC LITERAL", node->position());
1142
  PrintLiteralIndented("NAME", node->raw_name(), false);
1143 1144 1145 1146
}


void AstPrinter::VisitConditional(Conditional* node) {
1147
  IndentedScope indent(this, "CONDITIONAL", node->position());
1148
  PrintIndentedVisit("CONDITION", node->condition());
1149 1150 1151 1152 1153 1154
  PrintIndentedVisit("THEN", node->then_expression());
  PrintIndentedVisit("ELSE", node->else_expression());
}


void AstPrinter::VisitLiteral(Literal* node) {
1155
  PrintLiteralIndented("LITERAL", node, true);
1156 1157 1158 1159
}


void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
1160
  IndentedScope indent(this, "REGEXP LITERAL", node->position());
1161
  PrintLiteralIndented("PATTERN", node->raw_pattern(), false);
1162
  int i = 0;
1163
  EmbeddedVector<char, 128> buf;
1164 1165 1166 1167 1168 1169 1170
  if (node->flags() & RegExp::kGlobal) buf[i++] = 'g';
  if (node->flags() & RegExp::kIgnoreCase) buf[i++] = 'i';
  if (node->flags() & RegExp::kMultiline) buf[i++] = 'm';
  if (node->flags() & RegExp::kUnicode) buf[i++] = 'u';
  if (node->flags() & RegExp::kSticky) buf[i++] = 'y';
  buf[i] = '\0';
  PrintIndented("FLAGS ");
1171
  Print("%s", buf.begin());
1172
  Print("\n");
1173 1174 1175 1176
}


void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) {
1177
  IndentedScope indent(this, "OBJ LITERAL", node->position());
1178 1179 1180 1181
  PrintObjectProperties(node->properties());
}

void AstPrinter::PrintObjectProperties(
1182
    const ZonePtrList<ObjectLiteral::Property>* properties) {
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
  for (int i = 0; i < properties->length(); i++) {
    ObjectLiteral::Property* property = properties->at(i);
    const char* prop_kind = nullptr;
    switch (property->kind()) {
      case ObjectLiteral::Property::CONSTANT:
        prop_kind = "CONSTANT";
        break;
      case ObjectLiteral::Property::COMPUTED:
        prop_kind = "COMPUTED";
        break;
      case ObjectLiteral::Property::MATERIALIZED_LITERAL:
        prop_kind = "MATERIALIZED_LITERAL";
        break;
      case ObjectLiteral::Property::PROTOTYPE:
        prop_kind = "PROTOTYPE";
        break;
      case ObjectLiteral::Property::GETTER:
        prop_kind = "GETTER";
        break;
      case ObjectLiteral::Property::SETTER:
        prop_kind = "SETTER";
        break;
1205 1206 1207
      case ObjectLiteral::Property::SPREAD:
        prop_kind = "SPREAD";
        break;
1208 1209 1210
    }
    EmbeddedVector<char, 128> buf;
    SNPrintF(buf, "PROPERTY - %s", prop_kind);
1211
    IndentedScope prop(this, buf.begin());
1212 1213 1214
    PrintIndentedVisit("KEY", properties->at(i)->key());
    PrintIndentedVisit("VALUE", properties->at(i)->value());
  }
1215 1216 1217 1218
}


void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) {
1219
  IndentedScope indent(this, "ARRAY LITERAL", node->position());
1220
  if (node->values()->length() > 0) {
1221
    IndentedScope indent(this, "VALUES", node->position());
1222 1223 1224 1225 1226 1227 1228 1229
    for (int i = 0; i < node->values()->length(); i++) {
      Visit(node->values()->at(i));
    }
  }
}


void AstPrinter::VisitVariableProxy(VariableProxy* node) {
1230
  EmbeddedVector<char, 128> buf;
1231
  int pos = SNPrintF(buf, "VAR PROXY");
1232

1233 1234
  if (!node->is_resolved()) {
    SNPrintF(buf + pos, " unresolved");
1235
    PrintLiteralWithModeIndented(buf.begin(), nullptr, node->raw_name());
1236 1237 1238 1239
  } else {
    Variable* var = node->var();
    switch (var->location()) {
      case VariableLocation::UNALLOCATED:
1240
        SNPrintF(buf + pos, " unallocated");
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
        break;
      case VariableLocation::PARAMETER:
        SNPrintF(buf + pos, " parameter[%d]", var->index());
        break;
      case VariableLocation::LOCAL:
        SNPrintF(buf + pos, " local[%d]", var->index());
        break;
      case VariableLocation::CONTEXT:
        SNPrintF(buf + pos, " context[%d]", var->index());
        break;
      case VariableLocation::LOOKUP:
        SNPrintF(buf + pos, " lookup");
        break;
1254 1255 1256
      case VariableLocation::MODULE:
        SNPrintF(buf + pos, " module");
        break;
Simon Zünd's avatar
Simon Zünd committed
1257 1258 1259
      case VariableLocation::REPL_GLOBAL:
        SNPrintF(buf + pos, " repl global[%d]", var->index());
        break;
1260
    }
1261
    PrintLiteralWithModeIndented(buf.begin(), var, node->raw_name());
1262
  }
1263 1264 1265 1266
}


void AstPrinter::VisitAssignment(Assignment* node) {
1267
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1268 1269 1270 1271
  Visit(node->target());
  Visit(node->value());
}

1272 1273 1274 1275
void AstPrinter::VisitCompoundAssignment(CompoundAssignment* node) {
  VisitAssignment(node);
}

1276
void AstPrinter::VisitYield(Yield* node) {
1277
  EmbeddedVector<char, 128> buf;
1278
  SNPrintF(buf, "YIELD");
1279
  IndentedScope indent(this, buf.begin(), node->position());
1280
  Visit(node->expression());
1281 1282
}

1283 1284
void AstPrinter::VisitYieldStar(YieldStar* node) {
  EmbeddedVector<char, 128> buf;
1285
  SNPrintF(buf, "YIELD_STAR");
1286
  IndentedScope indent(this, buf.begin(), node->position());
1287 1288
  Visit(node->expression());
}
1289

1290 1291
void AstPrinter::VisitAwait(Await* node) {
  EmbeddedVector<char, 128> buf;
1292
  SNPrintF(buf, "AWAIT");
1293
  IndentedScope indent(this, buf.begin(), node->position());
1294 1295 1296
  Visit(node->expression());
}

1297
void AstPrinter::VisitThrow(Throw* node) {
1298
  IndentedScope indent(this, "THROW", node->position());
1299
  Visit(node->exception());
1300 1301
}

1302 1303 1304 1305 1306
void AstPrinter::VisitOptionalChain(OptionalChain* node) {
  IndentedScope indent(this, "OPTIONAL_CHAIN", node->position());
  Visit(node->expression());
}

1307
void AstPrinter::VisitProperty(Property* node) {
1308
  EmbeddedVector<char, 128> buf;
1309
  SNPrintF(buf, "PROPERTY");
1310
  IndentedScope indent(this, buf.begin(), node->position());
1311

1312
  Visit(node->obj());
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
  AssignType type = Property::GetAssignType(node);
  switch (type) {
    case NAMED_PROPERTY:
    case NAMED_SUPER_PROPERTY: {
      PrintLiteralIndented("NAME", node->key()->AsLiteral(), false);
      break;
    }
    case PRIVATE_METHOD: {
      PrintIndentedVisit("PRIVATE_METHOD", node->key());
      break;
    }
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
    case PRIVATE_GETTER_ONLY: {
      PrintIndentedVisit("PRIVATE_GETTER_ONLY", node->key());
      break;
    }
    case PRIVATE_SETTER_ONLY: {
      PrintIndentedVisit("PRIVATE_SETTER_ONLY", node->key());
      break;
    }
    case PRIVATE_GETTER_AND_SETTER: {
      PrintIndentedVisit("PRIVATE_GETTER_AND_SETTER", node->key());
      break;
    }
1336 1337 1338 1339 1340 1341 1342
    case KEYED_PROPERTY:
    case KEYED_SUPER_PROPERTY: {
      PrintIndentedVisit("KEY", node->key());
      break;
    }
    case NON_PROPERTY:
      UNREACHABLE();
1343 1344 1345 1346
  }
}

void AstPrinter::VisitCall(Call* node) {
1347
  EmbeddedVector<char, 128> buf;
1348
  SNPrintF(buf, "CALL");
1349
  IndentedScope indent(this, buf.begin());
1350

1351 1352 1353 1354 1355 1356
  Visit(node->expression());
  PrintArguments(node->arguments());
}


void AstPrinter::VisitCallNew(CallNew* node) {
1357
  IndentedScope indent(this, "CALL NEW", node->position());
1358 1359 1360 1361 1362 1363
  Visit(node->expression());
  PrintArguments(node->arguments());
}


void AstPrinter::VisitCallRuntime(CallRuntime* node) {
1364
  EmbeddedVector<char, 128> buf;
1365 1366
  SNPrintF(buf, "CALL RUNTIME %s%s", node->debug_name(),
           node->is_jsruntime() ? " (JS function)" : "");
1367
  IndentedScope indent(this, buf.begin(), node->position());
1368 1369 1370 1371 1372
  PrintArguments(node->arguments());
}


void AstPrinter::VisitUnaryOperation(UnaryOperation* node) {
1373
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1374
  Visit(node->expression());
1375 1376 1377 1378
}


void AstPrinter::VisitCountOperation(CountOperation* node) {
1379
  EmbeddedVector<char, 128> buf;
1380 1381
  SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"),
           Token::Name(node->op()));
1382
  IndentedScope indent(this, buf.begin(), node->position());
1383
  Visit(node->expression());
1384 1385 1386 1387
}


void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
1388
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1389 1390 1391 1392
  Visit(node->left());
  Visit(node->right());
}

1393 1394 1395 1396 1397 1398 1399
void AstPrinter::VisitNaryOperation(NaryOperation* node) {
  IndentedScope indent(this, Token::Name(node->op()), node->position());
  Visit(node->first());
  for (size_t i = 0; i < node->subsequent_length(); ++i) {
    Visit(node->subsequent(i));
  }
}
1400 1401

void AstPrinter::VisitCompareOperation(CompareOperation* node) {
1402
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1403 1404 1405 1406 1407
  Visit(node->left());
  Visit(node->right());
}


1408
void AstPrinter::VisitSpread(Spread* node) {
1409
  IndentedScope indent(this, "SPREAD", node->position());
1410 1411 1412
  Visit(node->expression());
}

1413
void AstPrinter::VisitEmptyParentheses(EmptyParentheses* node) {
1414
  IndentedScope indent(this, "()", node->position());
1415 1416
}

1417 1418 1419 1420
void AstPrinter::VisitGetTemplateObject(GetTemplateObject* node) {
  IndentedScope indent(this, "GET-TEMPLATE-OBJECT", node->position());
}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
void AstPrinter::VisitTemplateLiteral(TemplateLiteral* node) {
  IndentedScope indent(this, "TEMPLATE-LITERAL", node->position());
  const AstRawString* string = node->string_parts()->first();
  if (!string->IsEmpty()) PrintLiteralIndented("SPAN", string, true);
  for (int i = 0; i < node->substitutions()->length();) {
    PrintIndentedVisit("EXPR", node->substitutions()->at(i++));
    if (i < node->string_parts()->length()) {
      string = node->string_parts()->at(i);
      if (!string->IsEmpty()) PrintLiteralIndented("SPAN", string, true);
    }
  }
}

1434 1435 1436 1437 1438
void AstPrinter::VisitImportCallExpression(ImportCallExpression* node) {
  IndentedScope indent(this, "IMPORT-CALL", node->position());
  Visit(node->argument());
}

1439 1440
void AstPrinter::VisitThisExpression(ThisExpression* node) {
  IndentedScope indent(this, "THIS-EXPRESSION", node->position());
1441 1442
}

1443
void AstPrinter::VisitSuperPropertyReference(SuperPropertyReference* node) {
1444
  IndentedScope indent(this, "SUPER-PROPERTY-REFERENCE", node->position());
1445 1446
}

1447 1448

void AstPrinter::VisitSuperCallReference(SuperCallReference* node) {
1449
  IndentedScope indent(this, "SUPER-CALL-REFERENCE", node->position());
1450 1451 1452
}


1453 1454
#endif  // DEBUG

1455 1456
}  // namespace internal
}  // namespace v8