blob: 80f180076dd5fada191122d11e649adb4cc7ed0e [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/compiler/source-position.h"
6#include "src/compiler/graph.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/compiler/node-aux-data.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008
9namespace v8 {
10namespace internal {
11namespace compiler {
12
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013class SourcePositionTable::Decorator final : public GraphDecorator {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014 public:
15 explicit Decorator(SourcePositionTable* source_positions)
16 : source_positions_(source_positions) {}
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018 void Decorate(Node* node) final {
Ben Murdochc5610432016-08-08 18:44:38 +010019 source_positions_->SetSourcePosition(node,
20 source_positions_->current_position_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021 }
22
23 private:
24 SourcePositionTable* source_positions_;
25};
26
27
28SourcePositionTable::SourcePositionTable(Graph* graph)
29 : graph_(graph),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030 decorator_(nullptr),
31 current_position_(SourcePosition::Unknown()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 table_(graph->zone()) {}
33
34
35void SourcePositionTable::AddDecorator() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 DCHECK_NULL(decorator_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 decorator_ = new (graph_->zone()) Decorator(this);
38 graph_->AddDecorator(decorator_);
39}
40
41
42void SourcePositionTable::RemoveDecorator() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 DCHECK_NOT_NULL(decorator_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 graph_->RemoveDecorator(decorator_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 decorator_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046}
47
48
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049SourcePosition SourcePositionTable::GetSourcePosition(Node* node) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 return table_.Get(node);
51}
52
Ben Murdochc5610432016-08-08 18:44:38 +010053void SourcePositionTable::SetSourcePosition(Node* node,
54 SourcePosition position) {
55 table_.Set(node, position);
56}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057
58void SourcePositionTable::Print(std::ostream& os) const {
59 os << "{";
60 bool needs_comma = false;
61 for (auto i : table_) {
62 SourcePosition pos = i.second;
63 if (pos.IsKnown()) {
64 if (needs_comma) {
65 os << ",";
66 }
67 os << "\"" << i.first << "\""
68 << ":" << pos.raw();
69 needs_comma = true;
70 }
71 }
72 os << "}";
73}
74
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075} // namespace compiler
76} // namespace internal
77} // namespace v8