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