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 "src/torque/contextual.h" 9 10 namespace v8 { 11 namespace internal { 12 namespace torque { 13 14 class SourceId { 15 private: SourceId(int id)16 explicit SourceId(int id) : id_(id) {} 17 int id_; 18 friend class SourceFileMap; 19 }; 20 21 struct SourcePosition { 22 SourceId source; 23 int line; 24 int column; 25 }; 26 DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile,SourceId)27DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId) 28 DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition) 29 30 class SourceFileMap : public ContextualClass<SourceFileMap> { 31 public: 32 SourceFileMap() {} 33 static const std::string& GetSource(SourceId source) { 34 return Get().sources_[source.id_]; 35 } 36 37 static SourceId AddSource(std::string path) { 38 Get().sources_.push_back(std::move(path)); 39 return SourceId(static_cast<int>(Get().sources_.size()) - 1); 40 } 41 42 private: 43 std::vector<std::string> sources_; 44 }; 45 PositionAsString(SourcePosition pos)46inline std::string PositionAsString(SourcePosition pos) { 47 return SourceFileMap::GetSource(pos.source) + ":" + 48 std::to_string(pos.line + 1) + ":" + std::to_string(pos.column + 1); 49 } 50 51 } // namespace torque 52 } // namespace internal 53 } // namespace v8 54 55 #endif // V8_TORQUE_SOURCE_POSITIONS_H_ 56