blob: 7a882921a71db37cc2727683a501dfe448e9365a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/compiler/node.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/zone-containers.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
15// Forward declarations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016class Node;
17
18template <class T>
19class NodeAuxData {
20 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021 explicit NodeAuxData(Zone* zone) : aux_data_(zone) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023 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
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;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039
40 private:
41 ZoneVector<T> aux_data_;
42};
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044
45template <class T>
46class 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
54 const_iterator(const ZoneVector<T>* data, size_t current)
55 : data_(data), current_(current) {}
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
79template <class T>
80typename NodeAuxData<T>::const_iterator NodeAuxData<T>::begin() const {
81 return typename NodeAuxData<T>::const_iterator(&aux_data_, 0);
82}
83
84template <class T>
85typename 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_