Line data Source code
1 : // Copyright 2018 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 : #ifndef V8_TORQUE_SOURCE_POSITIONS_H_
6 : #define V8_TORQUE_SOURCE_POSITIONS_H_
7 :
8 : #include <iostream>
9 :
10 : #include "src/torque/contextual.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 : namespace torque {
15 :
16 : struct SourcePosition;
17 :
18 : class SourceId {
19 : public:
20 : static SourceId Invalid() { return SourceId(-1); }
21 3 : bool IsValid() const { return id_ != -1; }
22 38399 : int operator==(const SourceId& s) const { return id_ == s.id_; }
23 16 : bool operator<(const SourceId& s) const { return id_ < s.id_; }
24 :
25 : private:
26 : explicit SourceId(int id) : id_(id) {}
27 : int id_;
28 : friend struct SourcePosition;
29 : friend class SourceFileMap;
30 : };
31 :
32 : struct LineAndColumn {
33 : int line;
34 : int column;
35 :
36 : static LineAndColumn Invalid() { return {-1, -1}; }
37 :
38 : bool operator==(const LineAndColumn& other) const {
39 6 : return line == other.line && column == other.column;
40 : }
41 : };
42 :
43 : struct SourcePosition {
44 : SourceId source;
45 : LineAndColumn start;
46 : LineAndColumn end;
47 :
48 : static SourcePosition Invalid() {
49 : SourcePosition pos{SourceId::Invalid(), LineAndColumn::Invalid(),
50 1337 : LineAndColumn::Invalid()};
51 : return pos;
52 : }
53 :
54 : bool CompareStartIgnoreColumn(const SourcePosition& pos) const {
55 83068 : return start.line == pos.start.line && source == pos.source;
56 : }
57 :
58 : bool Contains(LineAndColumn pos) const {
59 6 : if (pos.line < start.line || pos.line > end.line) return false;
60 :
61 5 : if (pos.line == start.line && pos.column < start.column) return false;
62 5 : if (pos.line == end.line && pos.column >= end.column) return false;
63 : return true;
64 : }
65 :
66 3 : bool operator==(const SourcePosition& pos) const {
67 9 : return source == pos.source && start == pos.start && end == pos.end;
68 : }
69 : };
70 :
71 : DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId);
72 : DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition);
73 :
74 30 : class SourceFileMap : public ContextualClass<SourceFileMap> {
75 : public:
76 : SourceFileMap() = default;
77 44997 : static const std::string& GetSource(SourceId source) {
78 44997 : CHECK(source.IsValid());
79 89994 : return Get().sources_[source.id_];
80 : }
81 :
82 71 : static SourceId AddSource(std::string path) {
83 71 : Get().sources_.push_back(std::move(path));
84 71 : return SourceId(static_cast<int>(Get().sources_.size()) - 1);
85 : }
86 :
87 5 : static SourceId GetSourceId(const std::string& path) {
88 5 : for (size_t i = 0; i < Get().sources_.size(); ++i) {
89 4 : if (Get().sources_[i] == path) {
90 4 : return SourceId(static_cast<int>(i));
91 : }
92 : }
93 : return SourceId::Invalid();
94 : }
95 :
96 : private:
97 : std::vector<std::string> sources_;
98 : };
99 :
100 213 : inline std::string PositionAsString(SourcePosition pos) {
101 639 : return SourceFileMap::GetSource(pos.source) + ":" +
102 852 : std::to_string(pos.start.line + 1) + ":" +
103 639 : std::to_string(pos.start.column + 1);
104 : }
105 :
106 0 : inline std::ostream& operator<<(std::ostream& out, SourcePosition pos) {
107 0 : return out << PositionAsString(pos);
108 : }
109 :
110 : } // namespace torque
111 : } // namespace internal
112 : } // namespace v8
113 :
114 : #endif // V8_TORQUE_SOURCE_POSITIONS_H_
|