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