source-position.h 2.52 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_SOURCE_POSITION_H_
#define V8_COMPILER_SOURCE_POSITION_H_

#include "src/compiler/node-aux-data.h"
yangguo's avatar
yangguo committed
9
#include "src/globals.h"
10 11 12 13 14 15 16

namespace v8 {
namespace internal {
namespace compiler {

// Encapsulates encoding and decoding of sources positions from which Nodes
// originated.
17
class SourcePosition final {
18 19 20 21 22
 public:
  explicit SourcePosition(int raw = kUnknownPosition) : raw_(raw) {}

  static SourcePosition Unknown() { return SourcePosition(kUnknownPosition); }
  bool IsUnknown() const { return raw() == kUnknownPosition; }
23
  bool IsKnown() const { return raw() != kUnknownPosition; }
24 25 26 27

  int raw() const { return raw_; }

 private:
yangguo's avatar
yangguo committed
28
  static const int kUnknownPosition = kNoSourcePosition;
29 30 31 32 33 34 35 36 37 38 39 40
  int raw_;
};


inline bool operator==(const SourcePosition& lhs, const SourcePosition& rhs) {
  return lhs.raw() == rhs.raw();
}

inline bool operator!=(const SourcePosition& lhs, const SourcePosition& rhs) {
  return !(lhs == rhs);
}

41
class SourcePositionTable final : public ZoneObject {
42
 public:
43
  class Scope final {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   public:
    Scope(SourcePositionTable* source_positions, SourcePosition position)
        : source_positions_(source_positions),
          prev_position_(source_positions->current_position_) {
      Init(position);
    }
    Scope(SourcePositionTable* source_positions, Node* node)
        : source_positions_(source_positions),
          prev_position_(source_positions->current_position_) {
      Init(source_positions_->GetSourcePosition(node));
    }
    ~Scope() { source_positions_->current_position_ = prev_position_; }

   private:
    void Init(SourcePosition position) {
59
      if (position.IsKnown()) source_positions_->current_position_ = position;
60 61
    }

62 63
    SourcePositionTable* const source_positions_;
    SourcePosition const prev_position_;
64 65 66 67 68 69 70 71
    DISALLOW_COPY_AND_ASSIGN(Scope);
  };

  explicit SourcePositionTable(Graph* graph);

  void AddDecorator();
  void RemoveDecorator();

72
  SourcePosition GetSourcePosition(Node* node) const;
73
  void SetSourcePosition(Node* node, SourcePosition position);
74

75 76
  void Print(std::ostream& os) const;

77 78 79
 private:
  class Decorator;

80
  Graph* const graph_;
81 82 83 84 85 86 87 88 89 90 91
  Decorator* decorator_;
  SourcePosition current_position_;
  NodeAuxData<SourcePosition> table_;

  DISALLOW_COPY_AND_ASSIGN(SourcePositionTable);
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

92
#endif  // V8_COMPILER_SOURCE_POSITION_H_