V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
source-positions.h
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  private:
18  explicit SourceId(int id) : id_(id) {}
19  int id_;
20  friend class SourceFileMap;
21 };
22 
24  SourceId source;
25  int line;
26  int column;
27 };
28 
29 DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId)
30 DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition)
31 
33  public:
34  SourceFileMap() = default;
35  static const std::string& GetSource(SourceId source) {
36  return Get().sources_[source.id_];
37  }
38 
39  static SourceId AddSource(std::string path) {
40  Get().sources_.push_back(std::move(path));
41  return SourceId(static_cast<int>(Get().sources_.size()) - 1);
42  }
43 
44  private:
45  std::vector<std::string> sources_;
46 };
47 
48 inline std::string PositionAsString(SourcePosition pos) {
49  return SourceFileMap::GetSource(pos.source) + ":" +
50  std::to_string(pos.line + 1) + ":" + std::to_string(pos.column + 1);
51 }
52 
53 inline std::ostream& operator<<(std::ostream& out, SourcePosition pos) {
54  return out << PositionAsString(pos);
55 }
56 
57 } // namespace torque
58 } // namespace internal
59 } // namespace v8
60 
61 #endif // V8_TORQUE_SOURCE_POSITIONS_H_
Definition: libplatform.h:13