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 : class SourceId {
17 : public:
18 : static SourceId Invalid() { return SourceId(-1); }
19 28770 : int operator==(const SourceId& s) const { return id_ == s.id_; }
20 :
21 : private:
22 : explicit SourceId(int id) : id_(id) {}
23 : int id_;
24 : friend class SourceFileMap;
25 : };
26 :
27 : struct LineAndColumn {
28 : int line;
29 : int column;
30 :
31 : static LineAndColumn Invalid() { return {-1, -1}; }
32 : };
33 :
34 : struct SourcePosition {
35 : SourceId source;
36 : LineAndColumn start;
37 : LineAndColumn end;
38 :
39 : static SourcePosition Invalid() {
40 : SourcePosition pos{SourceId::Invalid(), LineAndColumn::Invalid(),
41 724 : LineAndColumn::Invalid()};
42 : return pos;
43 : }
44 :
45 : bool CompareStartIgnoreColumn(const SourcePosition& pos) const {
46 62912 : return start.line == pos.start.line && source == pos.source;
47 : }
48 : };
49 :
50 : DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId);
51 : DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition);
52 :
53 2 : class SourceFileMap : public ContextualClass<SourceFileMap> {
54 : public:
55 : SourceFileMap() = default;
56 : static const std::string& GetSource(SourceId source) {
57 34916 : return Get().sources_[source.id_];
58 : }
59 :
60 30 : static SourceId AddSource(std::string path) {
61 30 : Get().sources_.push_back(std::move(path));
62 60 : return SourceId(static_cast<int>(Get().sources_.size()) - 1);
63 : }
64 :
65 : private:
66 : std::vector<std::string> sources_;
67 : };
68 :
69 278 : inline std::string PositionAsString(SourcePosition pos) {
70 1390 : return SourceFileMap::GetSource(pos.source) + ":" +
71 278 : std::to_string(pos.start.line + 1) + ":" +
72 834 : std::to_string(pos.start.column + 1);
73 : }
74 :
75 0 : inline std::ostream& operator<<(std::ostream& out, SourcePosition pos) {
76 0 : return out << PositionAsString(pos);
77 : }
78 :
79 : } // namespace torque
80 : } // namespace internal
81 : } // namespace v8
82 :
83 : #endif // V8_TORQUE_SOURCE_POSITIONS_H_
|