compiler-source-position-table.cc 1.91 KB
Newer Older
1 2 3 4
// Copyright 2014 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.

5
#include "src/compiler/compiler-source-position-table.h"
6
#include "src/compiler/graph.h"
7
#include "src/compiler/node-aux-data.h"
8 9 10 11 12

namespace v8 {
namespace internal {
namespace compiler {

13
class SourcePositionTable::Decorator final : public GraphDecorator {
14 15 16 17
 public:
  explicit Decorator(SourcePositionTable* source_positions)
      : source_positions_(source_positions) {}

18
  void Decorate(Node* node) final {
19 20
    source_positions_->SetSourcePosition(node,
                                         source_positions_->current_position_);
21 22 23 24 25 26 27 28
  }

 private:
  SourcePositionTable* source_positions_;
};

SourcePositionTable::SourcePositionTable(Graph* graph)
    : graph_(graph),
29
      decorator_(nullptr),
30
      current_position_(SourcePosition::Unknown()),
31
      table_(graph->zone()) {}
32 33

void SourcePositionTable::AddDecorator() {
34
  DCHECK_NULL(decorator_);
35 36 37 38 39
  decorator_ = new (graph_->zone()) Decorator(this);
  graph_->AddDecorator(decorator_);
}

void SourcePositionTable::RemoveDecorator() {
40
  DCHECK_NOT_NULL(decorator_);
41
  graph_->RemoveDecorator(decorator_);
42
  decorator_ = nullptr;
43 44
}

45
SourcePosition SourcePositionTable::GetSourcePosition(Node* node) const {
46 47 48
  return table_.Get(node);
}

49 50 51 52
void SourcePositionTable::SetSourcePosition(Node* node,
                                            SourcePosition position) {
  table_.Set(node, position);
}
53 54 55 56 57 58

void SourcePositionTable::Print(std::ostream& os) const {
  os << "{";
  bool needs_comma = false;
  for (auto i : table_) {
    SourcePosition pos = i.second;
59
    if (pos.IsKnown()) {
60 61 62 63
      if (needs_comma) {
        os << ",";
      }
      os << "\"" << i.first << "\""
64
         << ":" << pos.ScriptOffset();
65 66 67 68 69 70
      needs_comma = true;
    }
  }
  os << "}";
}

71 72 73
}  // namespace compiler
}  // namespace internal
}  // namespace v8