prettyprinter.cc 41 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/base/strings.h"
13
#include "src/base/vector.h"
14
#include "src/common/globals.h"
15
#include "src/objects/objects-inl.h"
16
#include "src/regexp/regexp-flags.h"
17
#include "src/strings/string-builder-inl.h"
18

19 20
namespace v8 {
namespace internal {
21

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

42
CallPrinter::~CallPrinter() = default;
43

44 45 46 47 48 49 50 51 52
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;
53 54
}

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


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

76 77 78 79 80 81
void CallPrinter::Print(char c) {
  if (!found_ || done_) return;
  num_prints_++;
  builder_->AppendCharacter(c);
}

82 83 84
void CallPrinter::Print(const char* str) {
  if (!found_ || done_) return;
  num_prints_++;
85
  builder_->AppendCString(str);
86 87
}

88
void CallPrinter::Print(Handle<String> str) {
89
  if (!found_ || done_) return;
90
  num_prints_++;
91
  builder_->AppendString(str);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

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) {}


113 114 115 116 117 118
void CallPrinter::VisitSloppyBlockFunctionStatement(
    SloppyBlockFunctionStatement* node) {
  Find(node->statement());
}


119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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());
147 148 149
  for (CaseClause* clause : *node->cases()) {
    if (!clause->is_default()) Find(clause->label());
    FindStatements(clause->statements());
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  }
}


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) {
167
  if (node->init() != nullptr) {
168 169
    Find(node->init());
  }
170 171
  if (node->cond() != nullptr) Find(node->cond());
  if (node->next() != nullptr) Find(node->next());
172 173 174 175 176 177
  Find(node->body());
}


void CallPrinter::VisitForInStatement(ForInStatement* node) {
  Find(node->each());
178
  Find(node->subject());
179 180 181 182 183
  Find(node->body());
}


void CallPrinter::VisitForOfStatement(ForOfStatement* node) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  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;
  }

202
  Find(node->body());
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}


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) {
222 223
  FunctionKind last_function_kind = function_kind_;
  function_kind_ = node->kind();
224
  FindStatements(node->body());
225
  function_kind_ = last_function_kind;
226 227 228 229 230
}


void CallPrinter::VisitClassLiteral(ClassLiteral* node) {
  if (node->extends()) Find(node->extends());
231 232 233 234 235
  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());
236 237 238
  }
}

239 240
void CallPrinter::VisitInitializeClassMembersStatement(
    InitializeClassMembersStatement* node) {
241 242 243 244
  for (int i = 0; i < node->fields()->length(); i++) {
    Find(node->fields()->at(i)->value());
  }
}
245

246 247 248 249 250 251 252 253 254 255 256 257
void CallPrinter::VisitInitializeClassStaticElementsStatement(
    InitializeClassStaticElementsStatement* node) {
  for (int i = 0; i < node->elements()->length(); i++) {
    ClassLiteral::StaticElement* element = node->elements()->at(i);
    if (element->kind() == ClassLiteral::StaticElement::PROPERTY) {
      Find(element->property()->value());
    } else {
      Find(element->static_block());
    }
  }
}

258 259 260 261 262 263 264 265 266
void CallPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {}


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

267 268 269 270 271 272 273

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

274 275 276

void CallPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
  Print("/");
277
  PrintLiteral(node->pattern(), false);
278
  Print("/");
279 280 281 282
#define V(Lower, Camel, LowerCamel, Char, Bit) \
  if (node->flags() & RegExp::k##Camel) Print(Char);
  REGEXP_FLAG_LIST(V)
#undef V
283 284 285 286
}


void CallPrinter::VisitObjectLiteral(ObjectLiteral* node) {
287
  Print("{");
288 289 290
  for (int i = 0; i < node->properties()->length(); i++) {
    Find(node->properties()->at(i)->value());
  }
291
  Print("}");
292 293 294 295 296 297 298
}


void CallPrinter::VisitArrayLiteral(ArrayLiteral* node) {
  Print("[");
  for (int i = 0; i < node->values()->length(); i++) {
    if (i != 0) Print(",");
299 300 301 302 303 304 305 306 307 308 309
    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);
310 311 312 313 314 315
  }
  Print("]");
}


void CallPrinter::VisitVariableProxy(VariableProxy* node) {
316
  if (is_user_js_) {
317
    PrintLiteral(node->name(), false);
318 319 320
  } else {
    // Variable names of non-user code are meaningless due to minification.
    Print("(var)");
321
  }
322 323 324 325
}


void CallPrinter::VisitAssignment(Assignment* node) {
326 327 328 329
  bool was_found = false;
  if (node->target()->IsObjectLiteral()) {
    ObjectLiteral* target = node->target()->AsObjectLiteral();
    if (target->position() == position_) {
330
      was_found = !found_;
331 332 333 334 335 336 337 338 339 340 341
      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;
        }
342
      }
343
    }
344 345 346 347 348 349 350 351 352 353 354 355 356
  }
  if (!was_found) {
    Find(node->target());
    if (node->target()->IsArrayLiteral()) {
      // Special case the visit for destructuring array assignment.
      if (node->value()->position() == position_) {
        is_iterator_error_ = true;
        was_found = !found_;
        found_ = true;
      }
      Find(node->value(), true);
    } else {
      Find(node->value());
357 358
    }
  } else {
359 360 361 362 363 364
    Find(node->value(), true);
  }

  if (was_found) {
    done_ = true;
    found_ = false;
365
  }
366 367
}

368 369 370 371
void CallPrinter::VisitCompoundAssignment(CompoundAssignment* node) {
  VisitAssignment(node);
}

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

374 375 376 377 378 379 380 381 382 383 384
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());
}
385

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

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

390 391 392
void CallPrinter::VisitOptionalChain(OptionalChain* node) {
  Find(node->expression());
}
393 394 395 396

void CallPrinter::VisitProperty(Property* node) {
  Expression* key = node->key();
  Literal* literal = key->AsLiteral();
397 398
  if (literal != nullptr &&
      literal->BuildValue(isolate_)->IsInternalizedString()) {
399
    Find(node->obj(), true);
400 401 402
    if (node->is_optional_chain_link()) {
      Print("?");
    }
403
    Print(".");
404 405 406
    // TODO(adamk): Teach Literal how to print its values without
    // allocating on the heap.
    PrintLiteral(literal->BuildValue(isolate_), false);
407 408
  } else {
    Find(node->obj(), true);
409 410 411
    if (node->is_optional_chain_link()) {
      Print("?.");
    }
412 413 414 415 416 417 418
    Print("[");
    Find(key, true);
    Print("]");
  }
}

void CallPrinter::VisitCall(Call* node) {
419 420
  bool was_found = false;
  if (node->position() == position_) {
421 422 423 424 425 426 427 428 429 430
    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;
    }

431 432 433
    is_call_error_ = true;
    was_found = !found_;
  }
434

435
  if (was_found) {
436 437 438
    // 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()) {
439 440 441 442 443
      done_ = true;
      return;
    }
    found_ = true;
  }
444
  Find(node->expression(), true);
445
  if (!was_found && !is_iterator_error_) Print("(...)");
446
  FindArguments(node->arguments());
447 448 449 450
  if (was_found) {
    done_ = true;
    found_ = false;
  }
451 452 453 454
}


void CallPrinter::VisitCallNew(CallNew* node) {
455 456
  bool was_found = false;
  if (node->position() == position_) {
457 458 459 460 461 462 463 464 465 466
    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;
    }

467 468 469
    is_call_error_ = true;
    was_found = !found_;
  }
470
  if (was_found) {
471 472 473
    // 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()) {
474 475 476 477 478
      done_ = true;
      return;
    }
    found_ = true;
  }
479
  Find(node->expression(), was_found || is_iterator_error_);
480
  FindArguments(node->arguments());
481 482 483 484
  if (was_found) {
    done_ = true;
    found_ = false;
  }
485 486 487 488 489 490 491 492 493 494 495 496
}


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;
497 498 499
  Print("(");
  Print(Token::String(op));
  if (needsSpace) Print(" ");
500 501 502 503 504 505 506
  Find(node->expression(), true);
  Print(")");
}


void CallPrinter::VisitCountOperation(CountOperation* node) {
  Print("(");
507
  if (node->is_prefix()) Print(Token::String(node->op()));
508
  Find(node->expression(), true);
509
  if (node->is_postfix()) Print(Token::String(node->op()));
510 511 512 513 514 515 516
  Print(")");
}


void CallPrinter::VisitBinaryOperation(BinaryOperation* node) {
  Print("(");
  Find(node->left(), true);
517 518 519
  Print(" ");
  Print(Token::String(node->op()));
  Print(" ");
520 521 522 523
  Find(node->right(), true);
  Print(")");
}

524 525 526 527 528 529 530 531 532 533 534
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(")");
}
535 536 537 538

void CallPrinter::VisitCompareOperation(CompareOperation* node) {
  Print("(");
  Find(node->left(), true);
539 540 541
  Print(" ");
  Print(Token::String(node->op()));
  Print(" ");
542 543 544 545 546
  Find(node->right(), true);
  Print(")");
}


547 548 549 550 551 552
void CallPrinter::VisitSpread(Spread* node) {
  Print("(...");
  Find(node->expression(), true);
  Print(")");
}

553 554 555 556
void CallPrinter::VisitEmptyParentheses(EmptyParentheses* node) {
  UNREACHABLE();
}

557 558
void CallPrinter::VisitGetTemplateObject(GetTemplateObject* node) {}

559 560 561 562 563 564
void CallPrinter::VisitTemplateLiteral(TemplateLiteral* node) {
  for (Expression* substitution : *node->substitutions()) {
    Find(substitution, true);
  }
}

565 566
void CallPrinter::VisitImportCallExpression(ImportCallExpression* node) {
  Print("ImportCall(");
567 568 569 570
  Find(node->specifier(), true);
  if (node->import_assertions()) {
    Find(node->import_assertions(), true);
  }
571 572 573
  Print(")");
}

574
void CallPrinter::VisitThisExpression(ThisExpression* node) { Print("this"); }
575

576 577 578
void CallPrinter::VisitSuperPropertyReference(SuperPropertyReference* node) {}


579 580 581
void CallPrinter::VisitSuperCallReference(SuperCallReference* node) {
  Print("super");
}
582 583


584
void CallPrinter::FindStatements(const ZonePtrList<Statement>* statements) {
585
  if (statements == nullptr) return;
586 587 588 589 590
  for (int i = 0; i < statements->length(); i++) {
    Find(statements->at(i));
  }
}

591
void CallPrinter::FindArguments(const ZonePtrList<Expression>* arguments) {
592 593 594 595 596 597
  if (found_) return;
  for (int i = 0; i < arguments->length(); i++) {
    Find(arguments->at(i));
  }
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
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);
617 618 619
  }
}

620

621
void CallPrinter::PrintLiteral(const AstRawString* value, bool quote) {
622
  PrintLiteral(value->string(), quote);
623 624 625 626 627
}

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


628 629
#ifdef DEBUG

630
const char* AstPrinter::Print(AstNode* node) {
631 632 633 634 635
  Init();
  Visit(node);
  return output_;
}

636
void AstPrinter::Init() {
637
  if (size_ == 0) {
638
    DCHECK_NULL(output_);
639 640 641 642 643 644 645 646
    const int initial_size = 256;
    output_ = NewArray<char>(initial_size);
    size_ = initial_size;
  }
  output_[0] = '\0';
  pos_ = 0;
}

647
void AstPrinter::Print(const char* format, ...) {
648 649 650
  for (;;) {
    va_list arguments;
    va_start(arguments, format);
651 652
    int n = base::VSNPrintF(base::Vector<char>(output_, size_) + pos_, format,
                            arguments);
653 654
    va_end(arguments);

655
    if (n >= 0) {
656 657 658 659 660 661 662 663
      // 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);
664
      MemCopy(new_output, output_, pos_);
665 666 667 668 669 670 671
      DeleteArray(output_);
      output_ = new_output;
      size_ = new_size;
    }
  }
}

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
void AstPrinter::PrintLiteral(Literal* literal, bool quote) {
  switch (literal->type()) {
    case Literal::kString:
      PrintLiteral(literal->AsRawString(), quote);
      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;
702 703 704
  }
}

705
void AstPrinter::PrintLiteral(const AstRawString* value, bool quote) {
706 707 708 709 710 711 712 713 714 715
  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("\"");
716 717
}

718 719 720 721 722 723 724 725 726 727
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("\"");
}
728

729 730
//-----------------------------------------------------------------------------

731
class V8_NODISCARD IndentedScope {
732
 public:
733
  IndentedScope(AstPrinter* printer, const char* txt)
734
      : ast_printer_(printer) {
735 736 737 738 739
    ast_printer_->PrintIndented(txt);
    ast_printer_->Print("\n");
    ast_printer_->inc_indent();
  }

740 741 742 743 744 745 746
  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();
  }

747 748 749 750 751
  virtual ~IndentedScope() {
    ast_printer_->dec_indent();
  }

 private:
752
  AstPrinter* ast_printer_;
753 754 755 756
};

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

757 758 759
AstPrinter::AstPrinter(uintptr_t stack_limit)
    : output_(nullptr), size_(0), pos_(0), indent_(0) {
  InitializeAstVisitor(stack_limit);
760
}
761 762

AstPrinter::~AstPrinter() {
763
  DCHECK_EQ(indent_, 0);
764
  DeleteArray(output_);
765 766 767 768 769
}


void AstPrinter::PrintIndented(const char* txt) {
  for (int i = 0; i < indent_; i++) {
770
    Print(". ");
771
  }
jfb's avatar
jfb committed
772
  Print("%s", txt);
773 774
}

775
void AstPrinter::PrintLiteralIndented(const char* info, Literal* literal,
776 777 778
                                      bool quote) {
  PrintIndented(info);
  Print(" ");
779 780 781 782 783 784 785 786 787
  PrintLiteral(literal, quote);
  Print("\n");
}

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

791 792 793 794 795 796 797
void AstPrinter::PrintLiteralIndented(const char* info,
                                      const AstConsString* value, bool quote) {
  PrintIndented(info);
  Print(" ");
  PrintLiteral(value, quote);
  Print("\n");
}
798

799 800
void AstPrinter::PrintLiteralWithModeIndented(const char* info, Variable* var,
                                              const AstRawString* value) {
801
  if (var == nullptr) {
802 803
    PrintLiteralIndented(info, value, true);
  } else {
804
    base::EmbeddedVector<char, 256> buf;
805
    int pos =
806 807 808
        SNPrintF(buf, "%s (%p) (mode = %s, assigned = %s", info,
                 reinterpret_cast<void*>(var), VariableMode2String(var->mode()),
                 var->maybe_assigned() == kMaybeAssigned ? "true" : "false");
809
    SNPrintF(buf + pos, ")");
810
    PrintLiteralIndented(buf.begin(), value, true);
811 812 813
  }
}

814
void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
815 816 817 818
  if (node != nullptr) {
    IndentedScope indent(this, s, node->position());
    Visit(node);
  }
819 820 821 822 823
}


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


852
void AstPrinter::PrintOut(Isolate* isolate, AstNode* node) {
853
  AstPrinter printer(isolate->stack_guard()->real_climit());
854 855
  printer.Init();
  printer.Visit(node);
856
  PrintF("%s", printer.output_);
857 858
}

859 860
void AstPrinter::PrintDeclarations(Declaration::List* declarations) {
  if (!declarations->is_empty()) {
861
    IndentedScope indent(this, "DECLS");
862
    for (Declaration* decl : *declarations) Visit(decl);
863 864 865
  }
}

866
void AstPrinter::PrintParameters(DeclarationScope* scope) {
867
  if (scope->num_parameters() > 0) {
868
    IndentedScope indent(this, "PARAMS");
869
    for (int i = 0; i < scope->num_parameters(); i++) {
870
      PrintLiteralWithModeIndented("VAR", scope->parameter(i),
871
                                   scope->parameter(i)->raw_name());
872 873 874 875
    }
  }
}

876
void AstPrinter::PrintStatements(const ZonePtrList<Statement>* statements) {
877 878 879 880 881
  for (int i = 0; i < statements->length(); i++) {
    Visit(statements->at(i));
  }
}

882
void AstPrinter::PrintArguments(const ZonePtrList<Expression>* arguments) {
883 884 885 886 887 888 889
  for (int i = 0; i < arguments->length(); i++) {
    Visit(arguments->at(i));
  }
}


void AstPrinter::VisitBlock(Block* node) {
890 891
  const char* block_txt =
      node->ignore_completion_value() ? "BLOCK NOCOMPLETIONS" : "BLOCK";
892
  IndentedScope indent(this, block_txt, node->position());
893 894 895 896
  PrintStatements(node->statements());
}


897
// TODO(svenpanne) Start with IndentedScope.
898
void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
899 900
  PrintLiteralWithModeIndented("VARIABLE", node->var(),
                               node->var()->raw_name());
901 902 903
}


904
// TODO(svenpanne) Start with IndentedScope.
905 906
void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
  PrintIndented("FUNCTION ");
907
  PrintLiteral(node->var()->raw_name(), true);
908
  Print(" = function ");
909
  PrintLiteral(node->fun()->raw_name(), false);
910
  Print("\n");
911 912 913 914
}


void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) {
915
  IndentedScope indent(this, "EXPRESSION STATEMENT", node->position());
916 917 918 919 920
  Visit(node->expression());
}


void AstPrinter::VisitEmptyStatement(EmptyStatement* node) {
921
  IndentedScope indent(this, "EMPTY", node->position());
922 923 924
}


925 926 927 928 929 930
void AstPrinter::VisitSloppyBlockFunctionStatement(
    SloppyBlockFunctionStatement* node) {
  Visit(node->statement());
}


931
void AstPrinter::VisitIfStatement(IfStatement* node) {
932
  IndentedScope indent(this, "IF", node->position());
933
  PrintIndentedVisit("CONDITION", node->condition());
934 935 936 937 938 939 940 941
  PrintIndentedVisit("THEN", node->then_statement());
  if (node->HasElseStatement()) {
    PrintIndentedVisit("ELSE", node->else_statement());
  }
}


void AstPrinter::VisitContinueStatement(ContinueStatement* node) {
942
  IndentedScope indent(this, "CONTINUE", node->position());
943 944 945 946
}


void AstPrinter::VisitBreakStatement(BreakStatement* node) {
947
  IndentedScope indent(this, "BREAK", node->position());
948 949 950 951
}


void AstPrinter::VisitReturnStatement(ReturnStatement* node) {
952
  IndentedScope indent(this, "RETURN", node->position());
953
  Visit(node->expression());
954 955 956
}


957
void AstPrinter::VisitWithStatement(WithStatement* node) {
958
  IndentedScope indent(this, "WITH", node->position());
959 960
  PrintIndentedVisit("OBJECT", node->expression());
  PrintIndentedVisit("BODY", node->statement());
961 962 963 964
}


void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
965
  IndentedScope switch_indent(this, "SWITCH", node->position());
966
  PrintIndentedVisit("TAG", node->tag());
967 968 969 970 971 972 973 974 975
  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());
    }
976 977 978 979
  }
}


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


void AstPrinter::VisitWhileStatement(WhileStatement* node) {
988
  IndentedScope indent(this, "WHILE", node->position());
989 990 991 992 993 994
  PrintIndentedVisit("COND", node->cond());
  PrintIndentedVisit("BODY", node->body());
}


void AstPrinter::VisitForStatement(ForStatement* node) {
995
  IndentedScope indent(this, "FOR", node->position());
996 997
  if (node->init()) PrintIndentedVisit("INIT", node->init());
  if (node->cond()) PrintIndentedVisit("COND", node->cond());
998
  PrintIndentedVisit("BODY", node->body());
999 1000 1001 1002 1003
  if (node->next()) PrintIndentedVisit("NEXT", node->next());
}


void AstPrinter::VisitForInStatement(ForInStatement* node) {
1004
  IndentedScope indent(this, "FOR IN", node->position());
1005
  PrintIndentedVisit("FOR", node->each());
1006
  PrintIndentedVisit("IN", node->subject());
1007 1008 1009 1010
  PrintIndentedVisit("BODY", node->body());
}


1011
void AstPrinter::VisitForOfStatement(ForOfStatement* node) {
1012
  IndentedScope indent(this, "FOR OF", node->position());
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  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());
1024
  PrintIndentedVisit("BODY", node->body());
1025 1026 1027
}


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

void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
  IndentedScope indent(this, "TRY FINALLY", node->position());
1061
  PrintIndentedVisit("TRY", node->try_block());
1062
  PrintIndentedVisit("FINALLY", node->finally_block());
1063
}
1064 1065

void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
1066
  IndentedScope indent(this, "DEBUGGER", node->position());
1067 1068 1069 1070
}


void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
1071
  IndentedScope indent(this, "FUNC LITERAL", node->position());
1072 1073
  PrintIndented("LITERAL ID");
  Print(" %d\n", node->function_literal_id());
1074 1075
  PrintLiteralIndented("NAME", node->raw_name(), false);
  PrintLiteralIndented("INFERRED NAME", node->raw_inferred_name(), false);
1076 1077 1078
  // 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.
1079
  // PrintParameters(node->scope());
1080 1081 1082 1083
  // PrintStatements(node->body());
}


arv@chromium.org's avatar
arv@chromium.org committed
1084
void AstPrinter::VisitClassLiteral(ClassLiteral* node) {
1085
  IndentedScope indent(this, "CLASS LITERAL", node->position());
1086
  PrintLiteralIndented("NAME", node->constructor()->raw_name(), false);
1087 1088 1089
  if (node->extends() != nullptr) {
    PrintIndentedVisit("EXTENDS", node->extends());
  }
1090 1091 1092 1093 1094 1095 1096
  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());
    }
  }
1097 1098
  if (node->static_initializer() != nullptr) {
    PrintIndentedVisit("STATIC INITIALIZER", node->static_initializer());
1099
  }
1100
  if (node->instance_members_initializer_function() != nullptr) {
1101
    PrintIndentedVisit("INSTANCE MEMBERS INITIALIZER",
1102
                       node->instance_members_initializer_function());
1103
  }
1104 1105
  PrintClassProperties(node->private_members());
  PrintClassProperties(node->public_members());
1106 1107
}

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

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
void AstPrinter::VisitInitializeClassStaticElementsStatement(
    InitializeClassStaticElementsStatement* node) {
  IndentedScope indent(this, "INITIALIZE CLASS STATIC ELEMENTS",
                       node->position());
  PrintClassStaticElements(node->elements());
}

void AstPrinter::PrintClassProperty(ClassLiteral::Property* property) {
  const char* prop_kind = nullptr;
  switch (property->kind()) {
    case ClassLiteral::Property::METHOD:
      prop_kind = "METHOD";
      break;
    case ClassLiteral::Property::GETTER:
      prop_kind = "GETTER";
      break;
    case ClassLiteral::Property::SETTER:
      prop_kind = "SETTER";
      break;
    case ClassLiteral::Property::FIELD:
      prop_kind = "FIELD";
      break;
  }
1137
  base::EmbeddedVector<char, 128> buf;
1138 1139 1140 1141 1142 1143 1144
  SNPrintF(buf, "PROPERTY%s%s - %s", property->is_static() ? " - STATIC" : "",
           property->is_private() ? " - PRIVATE" : " - PUBLIC", prop_kind);
  IndentedScope prop(this, buf.begin());
  PrintIndentedVisit("KEY", property->key());
  PrintIndentedVisit("VALUE", property->value());
}

1145
void AstPrinter::PrintClassProperties(
1146
    const ZonePtrList<ClassLiteral::Property>* properties) {
1147
  for (int i = 0; i < properties->length(); i++) {
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
    PrintClassProperty(properties->at(i));
  }
}

void AstPrinter::PrintClassStaticElements(
    const ZonePtrList<ClassLiteral::StaticElement>* static_elements) {
  for (int i = 0; i < static_elements->length(); i++) {
    ClassLiteral::StaticElement* element = static_elements->at(i);
    switch (element->kind()) {
      case ClassLiteral::StaticElement::PROPERTY:
        PrintClassProperty(element->property());
1159
        break;
1160 1161
      case ClassLiteral::StaticElement::STATIC_BLOCK:
        PrintIndentedVisit("STATIC BLOCK", element->static_block());
1162
        break;
1163 1164
    }
  }
arv@chromium.org's avatar
arv@chromium.org committed
1165 1166
}

1167
void AstPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
1168
  IndentedScope indent(this, "NATIVE FUNC LITERAL", node->position());
1169
  PrintLiteralIndented("NAME", node->raw_name(), false);
1170 1171 1172 1173
}


void AstPrinter::VisitConditional(Conditional* node) {
1174
  IndentedScope indent(this, "CONDITIONAL", node->position());
1175
  PrintIndentedVisit("CONDITION", node->condition());
1176 1177 1178 1179 1180 1181
  PrintIndentedVisit("THEN", node->then_expression());
  PrintIndentedVisit("ELSE", node->else_expression());
}


void AstPrinter::VisitLiteral(Literal* node) {
1182
  PrintLiteralIndented("LITERAL", node, true);
1183 1184 1185 1186
}


void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
1187
  IndentedScope indent(this, "REGEXP LITERAL", node->position());
1188
  PrintLiteralIndented("PATTERN", node->raw_pattern(), false);
1189
  int i = 0;
1190
  base::EmbeddedVector<char, 128> buf;
1191 1192 1193 1194
#define V(Lower, Camel, LowerCamel, Char, Bit) \
  if (node->flags() & RegExp::k##Camel) buf[i++] = Char;
  REGEXP_FLAG_LIST(V)
#undef V
1195 1196
  buf[i] = '\0';
  PrintIndented("FLAGS ");
1197
  Print("%s", buf.begin());
1198
  Print("\n");
1199 1200 1201 1202
}


void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) {
1203
  IndentedScope indent(this, "OBJ LITERAL", node->position());
1204 1205 1206 1207
  PrintObjectProperties(node->properties());
}

void AstPrinter::PrintObjectProperties(
1208
    const ZonePtrList<ObjectLiteral::Property>* properties) {
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  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;
1231 1232 1233
      case ObjectLiteral::Property::SPREAD:
        prop_kind = "SPREAD";
        break;
1234
    }
1235
    base::EmbeddedVector<char, 128> buf;
1236
    SNPrintF(buf, "PROPERTY - %s", prop_kind);
1237
    IndentedScope prop(this, buf.begin());
1238 1239 1240
    PrintIndentedVisit("KEY", properties->at(i)->key());
    PrintIndentedVisit("VALUE", properties->at(i)->value());
  }
1241 1242 1243 1244
}


void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) {
1245
  IndentedScope array_indent(this, "ARRAY LITERAL", node->position());
1246
  if (node->values()->length() > 0) {
1247
    IndentedScope indent(this, "VALUES", node->position());
1248 1249 1250 1251 1252 1253 1254 1255
    for (int i = 0; i < node->values()->length(); i++) {
      Visit(node->values()->at(i));
    }
  }
}


void AstPrinter::VisitVariableProxy(VariableProxy* node) {
1256
  base::EmbeddedVector<char, 128> buf;
1257
  int pos = SNPrintF(buf, "VAR PROXY");
1258

1259 1260
  if (!node->is_resolved()) {
    SNPrintF(buf + pos, " unresolved");
1261
    PrintLiteralWithModeIndented(buf.begin(), nullptr, node->raw_name());
1262 1263 1264 1265
  } else {
    Variable* var = node->var();
    switch (var->location()) {
      case VariableLocation::UNALLOCATED:
1266
        SNPrintF(buf + pos, " unallocated");
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        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;
1280 1281 1282
      case VariableLocation::MODULE:
        SNPrintF(buf + pos, " module");
        break;
Simon Zünd's avatar
Simon Zünd committed
1283 1284 1285
      case VariableLocation::REPL_GLOBAL:
        SNPrintF(buf + pos, " repl global[%d]", var->index());
        break;
1286
    }
1287
    PrintLiteralWithModeIndented(buf.begin(), var, node->raw_name());
1288
  }
1289 1290 1291 1292
}


void AstPrinter::VisitAssignment(Assignment* node) {
1293
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1294 1295 1296 1297
  Visit(node->target());
  Visit(node->value());
}

1298 1299 1300 1301
void AstPrinter::VisitCompoundAssignment(CompoundAssignment* node) {
  VisitAssignment(node);
}

1302
void AstPrinter::VisitYield(Yield* node) {
1303
  base::EmbeddedVector<char, 128> buf;
1304
  SNPrintF(buf, "YIELD");
1305
  IndentedScope indent(this, buf.begin(), node->position());
1306
  Visit(node->expression());
1307 1308
}

1309
void AstPrinter::VisitYieldStar(YieldStar* node) {
1310
  base::EmbeddedVector<char, 128> buf;
1311
  SNPrintF(buf, "YIELD_STAR");
1312
  IndentedScope indent(this, buf.begin(), node->position());
1313 1314
  Visit(node->expression());
}
1315

1316
void AstPrinter::VisitAwait(Await* node) {
1317
  base::EmbeddedVector<char, 128> buf;
1318
  SNPrintF(buf, "AWAIT");
1319
  IndentedScope indent(this, buf.begin(), node->position());
1320 1321 1322
  Visit(node->expression());
}

1323
void AstPrinter::VisitThrow(Throw* node) {
1324
  IndentedScope indent(this, "THROW", node->position());
1325
  Visit(node->exception());
1326 1327
}

1328 1329 1330 1331 1332
void AstPrinter::VisitOptionalChain(OptionalChain* node) {
  IndentedScope indent(this, "OPTIONAL_CHAIN", node->position());
  Visit(node->expression());
}

1333
void AstPrinter::VisitProperty(Property* node) {
1334
  base::EmbeddedVector<char, 128> buf;
1335
  SNPrintF(buf, "PROPERTY");
1336
  IndentedScope indent(this, buf.begin(), node->position());
1337

1338
  Visit(node->obj());
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  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;
    }
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    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;
    }
1362 1363 1364 1365 1366 1367 1368
    case KEYED_PROPERTY:
    case KEYED_SUPER_PROPERTY: {
      PrintIndentedVisit("KEY", node->key());
      break;
    }
    case NON_PROPERTY:
      UNREACHABLE();
1369 1370 1371 1372
  }
}

void AstPrinter::VisitCall(Call* node) {
1373
  base::EmbeddedVector<char, 128> buf;
1374
  SNPrintF(buf, "CALL");
1375
  IndentedScope indent(this, buf.begin());
1376

1377 1378 1379 1380 1381 1382
  Visit(node->expression());
  PrintArguments(node->arguments());
}


void AstPrinter::VisitCallNew(CallNew* node) {
1383
  IndentedScope indent(this, "CALL NEW", node->position());
1384 1385 1386 1387 1388 1389
  Visit(node->expression());
  PrintArguments(node->arguments());
}


void AstPrinter::VisitCallRuntime(CallRuntime* node) {
1390
  base::EmbeddedVector<char, 128> buf;
1391 1392
  SNPrintF(buf, "CALL RUNTIME %s%s", node->debug_name(),
           node->is_jsruntime() ? " (JS function)" : "");
1393
  IndentedScope indent(this, buf.begin(), node->position());
1394 1395 1396 1397 1398
  PrintArguments(node->arguments());
}


void AstPrinter::VisitUnaryOperation(UnaryOperation* node) {
1399
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1400
  Visit(node->expression());
1401 1402 1403 1404
}


void AstPrinter::VisitCountOperation(CountOperation* node) {
1405
  base::EmbeddedVector<char, 128> buf;
1406 1407
  SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"),
           Token::Name(node->op()));
1408
  IndentedScope indent(this, buf.begin(), node->position());
1409
  Visit(node->expression());
1410 1411 1412 1413
}


void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
1414
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1415 1416 1417 1418
  Visit(node->left());
  Visit(node->right());
}

1419 1420 1421 1422 1423 1424 1425
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));
  }
}
1426 1427

void AstPrinter::VisitCompareOperation(CompareOperation* node) {
1428
  IndentedScope indent(this, Token::Name(node->op()), node->position());
1429 1430 1431 1432 1433
  Visit(node->left());
  Visit(node->right());
}


1434
void AstPrinter::VisitSpread(Spread* node) {
1435
  IndentedScope indent(this, "SPREAD", node->position());
1436 1437 1438
  Visit(node->expression());
}

1439
void AstPrinter::VisitEmptyParentheses(EmptyParentheses* node) {
1440
  IndentedScope indent(this, "()", node->position());
1441 1442
}

1443 1444 1445 1446
void AstPrinter::VisitGetTemplateObject(GetTemplateObject* node) {
  IndentedScope indent(this, "GET-TEMPLATE-OBJECT", node->position());
}

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
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);
    }
  }
}

1460 1461
void AstPrinter::VisitImportCallExpression(ImportCallExpression* node) {
  IndentedScope indent(this, "IMPORT-CALL", node->position());
1462 1463 1464 1465
  Visit(node->specifier());
  if (node->import_assertions()) {
    Visit(node->import_assertions());
  }
1466 1467
}

1468 1469
void AstPrinter::VisitThisExpression(ThisExpression* node) {
  IndentedScope indent(this, "THIS-EXPRESSION", node->position());
1470 1471
}

1472
void AstPrinter::VisitSuperPropertyReference(SuperPropertyReference* node) {
1473
  IndentedScope indent(this, "SUPER-PROPERTY-REFERENCE", node->position());
1474 1475
}

1476 1477

void AstPrinter::VisitSuperCallReference(SuperCallReference* node) {
1478
  IndentedScope indent(this, "SUPER-CALL-REFERENCE", node->position());
1479 1480 1481
}


1482 1483
#endif  // DEBUG

1484 1485
}  // namespace internal
}  // namespace v8