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/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>
DefaultConstruct()19 T DefaultConstruct() {
20 return T();
21 }
22
23 template <class T, T def() = DefaultConstruct<T>>
24 class NodeAuxData {
25 public:
NodeAuxData(Zone * zone)26 explicit NodeAuxData(Zone* zone) : aux_data_(zone) {}
NodeAuxData(size_t initial_size,Zone * zone)27 explicit NodeAuxData(size_t initial_size, Zone* zone)
28 : aux_data_(initial_size, zone) {}
29
30 // Update entry. Returns true iff entry was changed.
Set(Node * node,T const & data)31 bool Set(Node* node, T const& data) {
32 size_t const id = node->id();
33 if (id >= aux_data_.size()) aux_data_.resize(id + 1, def());
34 if (aux_data_[id] != data) {
35 aux_data_[id] = data;
36 return true;
37 }
38 return false;
39 }
40
Get(Node * node)41 T Get(Node* node) const {
42 size_t const id = node->id();
43 return (id < aux_data_.size()) ? aux_data_[id] : def();
44 }
45
46 class const_iterator;
47 friend class const_iterator;
48
49 const_iterator begin() const;
50 const_iterator end() const;
51
52 private:
53 ZoneVector<T> aux_data_;
54 };
55
56 template <class T, T def()>
57 class NodeAuxData<T, def>::const_iterator {
58 public:
59 typedef std::forward_iterator_tag iterator_category;
60 typedef int difference_type;
61 typedef std::pair<size_t, T> value_type;
62 typedef value_type* pointer;
63 typedef value_type& reference;
64
const_iterator(const ZoneVector<T> * data,size_t current)65 const_iterator(const ZoneVector<T>* data, size_t current)
66 : data_(data), current_(current) {}
const_iterator(const const_iterator & other)67 const_iterator(const const_iterator& other)
68 : data_(other.data_), current_(other.current_) {}
69
70 value_type operator*() const {
71 return std::make_pair(current_, (*data_)[current_]);
72 }
73 bool operator==(const const_iterator& other) const {
74 return current_ == other.current_ && data_ == other.data_;
75 }
76 bool operator!=(const const_iterator& other) const {
77 return !(*this == other);
78 }
79 const_iterator& operator++() {
80 ++current_;
81 return *this;
82 }
83 const_iterator operator++(int);
84
85 private:
86 const ZoneVector<T>* data_;
87 size_t current_;
88 };
89
90 template <class T, T def()>
begin()91 typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::begin()
92 const {
93 return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 0);
94 }
95
96 template <class T, T def()>
end()97 typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::end() const {
98 return typename NodeAuxData<T, def>::const_iterator(&aux_data_,
99 aux_data_.size());
100 }
101
102 } // namespace compiler
103 } // namespace internal
104 } // namespace v8
105
106 #endif // V8_COMPILER_NODE_AUX_DATA_H_
107