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 11namespace v8 { 12namespace internal { 13namespace compiler { 14 15// Forward declarations. 16class Node; 17 18template <class T> 19T DefaultConstruct(Zone* zone) { 20 return T(); 21} 22 23template <class T> 24T ZoneConstruct(Zone* zone) { 25 return T(zone); 26} 27 28template <class T, T def(Zone*) = DefaultConstruct<T>> 29class NodeAuxData { 30 public: 31 explicit NodeAuxData(Zone* zone) : zone_(zone), aux_data_(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. 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 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 62template <class T, T def(Zone*)> 63class 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 71 const_iterator(const ZoneVector<T>* data, size_t current) 72 : data_(data), current_(current) {} 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 96template <class T, T def(Zone*)> 97typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::begin() 98 const { 99 return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 0); 100} 101 102template <class T, T def(Zone*)> 103typename 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