source-position.cc 1.69 KB
Newer Older
1 2 3 4 5 6
// 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.

#include "src/compiler/source-position.h"
#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 21 22 23 24 25 26 27 28
    source_positions_->table_.Set(node, source_positions_->current_position_);
  }

 private:
  SourcePositionTable* source_positions_;
};


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


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


void SourcePositionTable::RemoveDecorator() {
42
  DCHECK_NOT_NULL(decorator_);
43
  graph_->RemoveDecorator(decorator_);
44
  decorator_ = nullptr;
45 46 47
}


48
SourcePosition SourcePositionTable::GetSourcePosition(Node* node) const {
49 50 51
  return table_.Get(node);
}

52 53 54 55 56 57

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

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