forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexing_facts_tree.cc
99 lines (85 loc) · 3.3 KB
/
indexing_facts_tree.cc
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "verilog/tools/kythe/indexing_facts_tree.h"
#include <algorithm>
#include <iostream>
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "common/util/range.h"
#include "common/util/tree_operations.h"
namespace verilog {
namespace kythe {
Anchor::Anchor(const Anchor &other) = default;
std::string Anchor::DebugString() const {
if (source_text_range_) {
return absl::StrCat("{", Text(), " @", source_text_range_->begin, "-",
source_text_range_->begin + source_text_range_->length,
"}");
}
return absl::StrCat("{", Text(), "}");
}
std::ostream &operator<<(std::ostream &stream, const Anchor &anchor) {
return stream << anchor.DebugString();
}
bool Anchor::operator==(const Anchor &rhs) const {
if (source_text_range_) {
if (!rhs.source_text_range_) {
return false;
}
if (std::tie(source_text_range_->begin, source_text_range_->length) !=
std::tie(rhs.source_text_range_->begin,
rhs.source_text_range_->length)) {
return false;
}
}
return Text() == rhs.Text();
}
std::ostream &IndexingNodeData::DebugString(std::ostream *stream) const {
*stream << indexing_fact_type_ << ": ["
<< absl::StrJoin(anchors_.begin(), anchors_.end(), ", ",
[](std::string *out, const Anchor &anchor) {
absl::StrAppend(out, anchor.DebugString());
})
<< ']';
return *stream;
}
std::ostream &operator<<(std::ostream &stream, const IndexingNodeData &data) {
const auto &anchors(data.Anchors());
return stream << data.GetIndexingFactType() << ": ["
<< absl::StrJoin(anchors.begin(), anchors.end(), ", ",
absl::StreamFormatter())
<< ']';
}
bool IndexingNodeData::operator==(const IndexingNodeData &rhs) const {
return indexing_fact_type_ == rhs.GetIndexingFactType() &&
anchors_.size() == rhs.Anchors().size() &&
std::equal(anchors_.begin(), anchors_.end(), rhs.Anchors().begin());
}
std::ostream &operator<<(std::ostream &stream,
const PrintableIndexingNodeData &printable_node) {
return printable_node.data.DebugString(&stream);
}
std::ostream &operator<<(std::ostream &stream,
const PrintableIndexingFactNode &printable_node) {
return PrintTree(
printable_node.data, &stream,
[&printable_node](std::ostream &s,
const IndexingNodeData &d) -> std::ostream & {
return s << PrintableIndexingNodeData(d, printable_node.base);
});
}
} // namespace kythe
} // namespace verilog