1 // Copyright 2014 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_COMPILER_COMPILER_SOURCE_POSITION_TABLE_H_ 6 #define V8_COMPILER_COMPILER_SOURCE_POSITION_TABLE_H_ 7 8 #include "src/base/compiler-specific.h" 9 #include "src/codegen/source-position.h" 10 #include "src/common/globals.h" 11 #include "src/compiler/node-aux-data.h" 12 13 namespace v8 { 14 namespace internal { 15 namespace compiler { 16 17 class V8_EXPORT_PRIVATE SourcePositionTable final NON_EXPORTED_BASE(ZoneObject)18 : public NON_EXPORTED_BASE(ZoneObject) { 19 public: 20 class V8_NODISCARD Scope final { 21 public: 22 Scope(SourcePositionTable* source_positions, SourcePosition position) 23 : source_positions_(source_positions), 24 prev_position_(source_positions->current_position_) { 25 Init(position); 26 } 27 Scope(SourcePositionTable* source_positions, Node* node) 28 : source_positions_(source_positions), 29 prev_position_(source_positions->current_position_) { 30 Init(source_positions_->GetSourcePosition(node)); 31 } 32 ~Scope() { source_positions_->current_position_ = prev_position_; } 33 Scope(const Scope&) = delete; 34 Scope& operator=(const Scope&) = delete; 35 36 private: 37 void Init(SourcePosition position) { 38 if (position.IsKnown()) source_positions_->current_position_ = position; 39 } 40 41 SourcePositionTable* const source_positions_; 42 SourcePosition const prev_position_; 43 }; 44 45 explicit SourcePositionTable(Graph* graph); 46 SourcePositionTable(const SourcePositionTable&) = delete; 47 SourcePositionTable& operator=(const SourcePositionTable&) = delete; 48 49 void AddDecorator(); 50 void RemoveDecorator(); 51 52 SourcePosition GetSourcePosition(Node* node) const; 53 void SetSourcePosition(Node* node, SourcePosition position); 54 55 void SetCurrentPosition(const SourcePosition& pos) { 56 current_position_ = pos; 57 } 58 SourcePosition GetCurrentPosition() const { return current_position_; } 59 60 void PrintJson(std::ostream& os) const; 61 62 private: 63 class Decorator; 64 65 static SourcePosition UnknownSourcePosition(Zone* zone) { 66 return SourcePosition::Unknown(); 67 } 68 69 Graph* const graph_; 70 Decorator* decorator_; 71 SourcePosition current_position_; 72 NodeAuxData<SourcePosition, UnknownSourcePosition> table_; 73 }; 74 75 } // namespace compiler 76 } // namespace internal 77 } // namespace v8 78 79 #endif // V8_COMPILER_COMPILER_SOURCE_POSITION_TABLE_H_ 80