source-range-ast-visitor.cc 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/ast/source-range-ast-visitor.h"

#include "src/ast/ast-source-ranges.h"

namespace v8 {
namespace internal {

SourceRangeAstVisitor::SourceRangeAstVisitor(uintptr_t stack_limit,
                                             Expression* root,
                                             SourceRangeMap* source_range_map)
    : AstTraversalVisitor(stack_limit, root),
      source_range_map_(source_range_map) {}

void SourceRangeAstVisitor::VisitBlock(Block* stmt) {
  AstTraversalVisitor::VisitBlock(stmt);
  ZonePtrList<Statement>* stmts = stmt->statements();
  AstNodeSourceRanges* enclosingSourceRanges = source_range_map_->Find(stmt);
  if (enclosingSourceRanges != nullptr) {
    CHECK(enclosingSourceRanges->HasRange(SourceRangeKind::kContinuation));
    MaybeRemoveLastContinuationRange(stmts);
  }
}

28 29 30 31 32 33 34 35
void SourceRangeAstVisitor::VisitSwitchStatement(SwitchStatement* stmt) {
  AstTraversalVisitor::VisitSwitchStatement(stmt);
  ZonePtrList<CaseClause>* clauses = stmt->cases();
  for (CaseClause* clause : *clauses) {
    MaybeRemoveLastContinuationRange(clause->statements());
  }
}

36 37 38 39 40 41
void SourceRangeAstVisitor::VisitFunctionLiteral(FunctionLiteral* expr) {
  AstTraversalVisitor::VisitFunctionLiteral(expr);
  ZonePtrList<Statement>* stmts = expr->body();
  MaybeRemoveLastContinuationRange(stmts);
}

42 43
void SourceRangeAstVisitor::VisitTryCatchStatement(TryCatchStatement* stmt) {
  AstTraversalVisitor::VisitTryCatchStatement(stmt);
44
  MaybeRemoveContinuationRange(stmt->try_block());
45 46 47
  MaybeRemoveContinuationRangeOfAsyncReturn(stmt);
}

48 49 50 51 52 53
void SourceRangeAstVisitor::VisitTryFinallyStatement(
    TryFinallyStatement* stmt) {
  AstTraversalVisitor::VisitTryFinallyStatement(stmt);
  MaybeRemoveContinuationRange(stmt->try_block());
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
bool SourceRangeAstVisitor::VisitNode(AstNode* node) {
  AstNodeSourceRanges* range = source_range_map_->Find(node);

  if (range == nullptr) return true;
  if (!range->HasRange(SourceRangeKind::kContinuation)) return true;

  // Called in pre-order. In case of conflicting continuation ranges, only the
  // outermost range may survive.

  SourceRange continuation = range->GetRange(SourceRangeKind::kContinuation);
  if (continuation_positions_.find(continuation.start) !=
      continuation_positions_.end()) {
    range->RemoveContinuationRange();
  } else {
    continuation_positions_.emplace(continuation.start);
  }

  return true;
}

74 75
void SourceRangeAstVisitor::MaybeRemoveContinuationRange(
    Statement* last_statement) {
76 77 78 79 80 81 82 83 84 85 86 87
  AstNodeSourceRanges* last_range = nullptr;

  if (last_statement->IsExpressionStatement() &&
      last_statement->AsExpressionStatement()->expression()->IsThrow()) {
    // For ThrowStatement, source range is tied to Throw expression not
    // ExpressionStatement.
    last_range = source_range_map_->Find(
        last_statement->AsExpressionStatement()->expression());
  } else {
    last_range = source_range_map_->Find(last_statement);
  }

88 89 90 91 92 93 94
  if (last_range == nullptr) return;

  if (last_range->HasRange(SourceRangeKind::kContinuation)) {
    last_range->RemoveContinuationRange();
  }
}

95 96 97 98 99 100 101
void SourceRangeAstVisitor::MaybeRemoveLastContinuationRange(
    ZonePtrList<Statement>* statements) {
  if (statements->is_empty()) return;
  MaybeRemoveContinuationRange(statements->last());
}

namespace {
102
Statement* FindLastNonSyntheticStatement(ZonePtrList<Statement>* statements) {
103 104
  for (int i = statements->length() - 1; i >= 0; --i) {
    Statement* stmt = statements->at(i);
105 106 107 108
    if (stmt->IsReturnStatement() &&
        stmt->AsReturnStatement()->is_synthetic_async_return()) {
      continue;
    }
109 110 111 112 113 114 115 116 117 118
    return stmt;
  }
  return nullptr;
}
}  // namespace

void SourceRangeAstVisitor::MaybeRemoveContinuationRangeOfAsyncReturn(
    TryCatchStatement* try_catch_stmt) {
  // Detect try-catch inserted by NewTryCatchStatementForAsyncAwait in the
  // parser (issued for async functions, including async generators), and
119 120
  // remove the continuation range of the last statement, such that the
  // range of the enclosing function body is used.
121 122
  if (try_catch_stmt->is_try_catch_for_async()) {
    Statement* last_non_synthetic =
123
      FindLastNonSyntheticStatement(try_catch_stmt->try_block()->statements());
124 125 126 127 128 129
    if (last_non_synthetic) {
      MaybeRemoveContinuationRange(last_non_synthetic);
    }
  }
}

130 131
}  // namespace internal
}  // namespace v8