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_NODE_AUX_DATA_H_
6 #define V8_COMPILER_NODE_AUX_DATA_H_
7
8 #include "src/compiler/node.h"
9 #include "src/zone-containers.h"
10
11 namespace v8 {
12 namespace internal {
13 namespace compiler {
14
15 // Forward declarations.
16 class Node;
17
18 template <class T>
19 class NodeAuxData {
20 public:
NodeAuxData(Zone * zone)21 explicit NodeAuxData(Zone* zone) : aux_data_(zone) {}
22
Set(Node * node,T const & data)23 void Set(Node* node, T const& data) {
24 size_t const id = node->id();
25 if (id >= aux_data_.size()) aux_data_.resize(id + 1);
26 aux_data_[id] = data;
27 }
28
Get(Node * node)29 T Get(Node* node) const {
30 size_t const id = node->id();
31 return (id < aux_data_.size()) ? aux_data_[id] : T();
32 }
33
34 class const_iterator;
35 friend class const_iterator;
36
37 const_iterator begin() const;
38 const_iterator end() const;
39
40 private:
41 ZoneVector<T> aux_data_;
42 };
43
44
45 template <class T>
46 class NodeAuxData<T>::const_iterator {
47 public:
48 typedef std::forward_iterator_tag iterator_category;
49 typedef int difference_type;
50 typedef std::pair<size_t, T> value_type;
51 typedef value_type* pointer;
52 typedef value_type& reference;
53
const_iterator(const ZoneVector<T> * data,size_t current)54 const_iterator(const ZoneVector<T>* data, size_t current)
55 : data_(data), current_(current) {}
const_iterator(const const_iterator & other)56 const_iterator(const const_iterator& other)
57 : data_(other.data_), current_(other.current_) {}
58
59 value_type operator*() const {
60 return std::make_pair(current_, (*data_)[current_]);
61 }
62 bool operator==(const const_iterator& other) const {
63 return current_ == other.current_ && data_ == other.data_;
64 }
65 bool operator!=(const const_iterator& other) const {
66 return !(*this == other);
67 }
68 const_iterator& operator++() {
69 ++current_;
70 return *this;
71 }
72 const_iterator operator++(int);
73
74 private:
75 const ZoneVector<T>* data_;
76 size_t current_;
77 };
78
79 template <class T>
begin()80 typename NodeAuxData<T>::const_iterator NodeAuxData<T>::begin() const {
81 return typename NodeAuxData<T>::const_iterator(&aux_data_, 0);
82 }
83
84 template <class T>
end()85 typename NodeAuxData<T>::const_iterator NodeAuxData<T>::end() const {
86 return typename NodeAuxData<T>::const_iterator(&aux_data_, aux_data_.size());
87 }
88
89 } // namespace compiler
90 } // namespace internal
91 } // namespace v8
92
93 #endif // V8_COMPILER_NODE_AUX_DATA_H_
94