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: Invalid()20 static SourceId Invalid() { return SourceId(-1); } IsValid()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: SourceId(int id)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 InvalidLineAndColumn36 static LineAndColumn Invalid() { return {-1, -1}; } 37 38 bool operator==(const LineAndColumn& other) const { 39 return line == other.line && column == other.column; 40 } 41 bool operator!=(const LineAndColumn& other) const { 42 return !(*this == other); 43 } 44 }; 45 46 struct SourcePosition { 47 SourceId source; 48 LineAndColumn start; 49 LineAndColumn end; 50 InvalidSourcePosition51 static SourcePosition Invalid() { 52 SourcePosition pos{SourceId::Invalid(), LineAndColumn::Invalid(), 53 LineAndColumn::Invalid()}; 54 return pos; 55 } 56 CompareStartIgnoreColumnSourcePosition57 bool CompareStartIgnoreColumn(const SourcePosition& pos) const { 58 return start.line == pos.start.line && source == pos.source; 59 } 60 ContainsSourcePosition61 bool Contains(LineAndColumn pos) const { 62 if (pos.line < start.line || pos.line > end.line) return false; 63 64 if (pos.line == start.line && pos.column < start.column) return false; 65 if (pos.line == end.line && pos.column >= end.column) return false; 66 return true; 67 } 68 69 bool operator==(const SourcePosition& pos) const { 70 return source == pos.source && start == pos.start && end == pos.end; 71 } 72 bool operator!=(const SourcePosition& pos) const { return !(*this == pos); } 73 }; 74 75 DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId); 76 DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition); 77 78 class V8_EXPORT_PRIVATE SourceFileMap : public ContextualClass<SourceFileMap> { 79 public: SourceFileMap(std::string v8_root)80 explicit SourceFileMap(std::string v8_root) : v8_root_(std::move(v8_root)) {} 81 static const std::string& PathFromV8Root(SourceId file); 82 static std::string PathFromV8RootWithoutExtension(SourceId file); 83 static std::string AbsolutePath(SourceId file); 84 static SourceId AddSource(std::string path); 85 static SourceId GetSourceId(const std::string& path); 86 static std::vector<SourceId> AllSources(); 87 static bool FileRelativeToV8RootExists(const std::string& path); 88 89 private: 90 std::vector<std::string> sources_; 91 std::string v8_root_; 92 }; 93 PositionAsString(SourcePosition pos)94inline std::string PositionAsString(SourcePosition pos) { 95 return SourceFileMap::PathFromV8Root(pos.source) + ":" + 96 std::to_string(pos.start.line + 1) + ":" + 97 std::to_string(pos.start.column + 1); 98 } 99 100 inline std::ostream& operator<<(std::ostream& out, SourcePosition pos) { 101 return out << PositionAsString(pos); 102 } 103 104 } // namespace torque 105 } // namespace internal 106 } // namespace v8 107 108 #endif // V8_TORQUE_SOURCE_POSITIONS_H_ 109