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
12namespace v8 {
13namespace internal {
14namespace torque {
15
16struct SourcePosition;
17
18class SourceId {
19 public:
20  static SourceId Invalid() { return SourceId(-1); }
21  bool IsValid() const { return id_ != -1; }
22  int operator==(const SourceId& s) const { return id_ == s.id_; }
23  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
32struct LineAndColumn {
33  static constexpr int kUnknownOffset = -1;
34
35  int offset;
36  int line;
37  int column;
38
39  static LineAndColumn Invalid() { return {-1, -1, -1}; }
40  static LineAndColumn WithUnknownOffset(int line, int column) {
41    return {kUnknownOffset, line, column};
42  }
43
44  bool operator==(const LineAndColumn& other) const {
45    if (offset == kUnknownOffset || other.offset == kUnknownOffset) {
46      return line == other.line && column == other.column;
47    }
48    DCHECK_EQ(offset == other.offset,
49              line == other.line && column == other.column);
50    return offset == other.offset;
51  }
52  bool operator!=(const LineAndColumn& other) const {
53    return !operator==(other);
54  }
55};
56
57struct SourcePosition {
58  SourceId source;
59  LineAndColumn start;
60  LineAndColumn end;
61
62  static SourcePosition Invalid() {
63    SourcePosition pos{SourceId::Invalid(), LineAndColumn::Invalid(),
64                       LineAndColumn::Invalid()};
65    return pos;
66  }
67
68  bool CompareStartIgnoreColumn(const SourcePosition& pos) const {
69    return start.line == pos.start.line && source == pos.source;
70  }
71
72  bool Contains(LineAndColumn pos) const {
73    if (pos.line < start.line || pos.line > end.line) return false;
74
75    if (pos.line == start.line && pos.column < start.column) return false;
76    if (pos.line == end.line && pos.column >= end.column) return false;
77    return true;
78  }
79
80  bool operator==(const SourcePosition& pos) const {
81    return source == pos.source && start == pos.start && end == pos.end;
82  }
83  bool operator!=(const SourcePosition& pos) const { return !(*this == pos); }
84};
85
86DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId);
87DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition);
88
89class V8_EXPORT_PRIVATE SourceFileMap : public ContextualClass<SourceFileMap> {
90 public:
91  explicit SourceFileMap(std::string v8_root) : v8_root_(std::move(v8_root)) {}
92  static const std::string& PathFromV8Root(SourceId file);
93  static std::string PathFromV8RootWithoutExtension(SourceId file);
94  static std::string AbsolutePath(SourceId file);
95  static SourceId AddSource(std::string path);
96  static SourceId GetSourceId(const std::string& path);
97  static std::vector<SourceId> AllSources();
98  static bool FileRelativeToV8RootExists(const std::string& path);
99
100 private:
101  std::vector<std::string> sources_;
102  std::string v8_root_;
103};
104
105inline std::string PositionAsString(SourcePosition pos) {
106  return SourceFileMap::PathFromV8Root(pos.source) + ":" +
107         std::to_string(pos.start.line + 1) + ":" +
108         std::to_string(pos.start.column + 1);
109}
110
111inline std::ostream& operator<<(std::ostream& out, SourcePosition pos) {
112  return out << "https://source.chromium.org/chromium/chromium/src/+/main:v8/"
113             << SourceFileMap::PathFromV8Root(pos.source)
114             << "?l=" << (pos.start.line + 1)
115             << "&c=" << (pos.start.column + 1);
116}
117
118}  // namespace torque
119}  // namespace internal
120}  // namespace v8
121
122#endif  // V8_TORQUE_SOURCE_POSITIONS_H_
123