11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_COMPILER_NODE_AUX_DATA_H_ 61cb0ef41Sopenharmony_ci#define V8_COMPILER_NODE_AUX_DATA_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "src/compiler/node.h" 91cb0ef41Sopenharmony_ci#include "src/zone/zone-containers.h" 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cinamespace v8 { 121cb0ef41Sopenharmony_cinamespace internal { 131cb0ef41Sopenharmony_cinamespace compiler { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Forward declarations. 161cb0ef41Sopenharmony_ciclass Node; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_citemplate <class T> 191cb0ef41Sopenharmony_ciT DefaultConstruct(Zone* zone) { 201cb0ef41Sopenharmony_ci return T(); 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_citemplate <class T> 241cb0ef41Sopenharmony_ciT ZoneConstruct(Zone* zone) { 251cb0ef41Sopenharmony_ci return T(zone); 261cb0ef41Sopenharmony_ci} 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_citemplate <class T, T def(Zone*) = DefaultConstruct<T>> 291cb0ef41Sopenharmony_ciclass NodeAuxData { 301cb0ef41Sopenharmony_ci public: 311cb0ef41Sopenharmony_ci explicit NodeAuxData(Zone* zone) : zone_(zone), aux_data_(zone) {} 321cb0ef41Sopenharmony_ci explicit NodeAuxData(size_t initial_size, Zone* zone) 331cb0ef41Sopenharmony_ci : zone_(zone), aux_data_(initial_size, def(zone), zone) {} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci // Update entry. Returns true iff entry was changed. 361cb0ef41Sopenharmony_ci bool Set(Node* node, T const& data) { 371cb0ef41Sopenharmony_ci size_t const id = node->id(); 381cb0ef41Sopenharmony_ci if (id >= aux_data_.size()) aux_data_.resize(id + 1, def(zone_)); 391cb0ef41Sopenharmony_ci if (aux_data_[id] != data) { 401cb0ef41Sopenharmony_ci aux_data_[id] = data; 411cb0ef41Sopenharmony_ci return true; 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci return false; 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci T Get(Node* node) const { 471cb0ef41Sopenharmony_ci size_t const id = node->id(); 481cb0ef41Sopenharmony_ci return (id < aux_data_.size()) ? aux_data_[id] : def(zone_); 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci class const_iterator; 521cb0ef41Sopenharmony_ci friend class const_iterator; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci const_iterator begin() const; 551cb0ef41Sopenharmony_ci const_iterator end() const; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci private: 581cb0ef41Sopenharmony_ci Zone* zone_; 591cb0ef41Sopenharmony_ci ZoneVector<T> aux_data_; 601cb0ef41Sopenharmony_ci}; 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_citemplate <class T, T def(Zone*)> 631cb0ef41Sopenharmony_ciclass NodeAuxData<T, def>::const_iterator { 641cb0ef41Sopenharmony_ci public: 651cb0ef41Sopenharmony_ci using iterator_category = std::forward_iterator_tag; 661cb0ef41Sopenharmony_ci using difference_type = int; 671cb0ef41Sopenharmony_ci using value_type = std::pair<size_t, T>; 681cb0ef41Sopenharmony_ci using pointer = value_type*; 691cb0ef41Sopenharmony_ci using reference = value_type&; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci const_iterator(const ZoneVector<T>* data, size_t current) 721cb0ef41Sopenharmony_ci : data_(data), current_(current) {} 731cb0ef41Sopenharmony_ci const_iterator(const const_iterator& other) 741cb0ef41Sopenharmony_ci : data_(other.data_), current_(other.current_) {} 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci value_type operator*() const { 771cb0ef41Sopenharmony_ci return std::make_pair(current_, (*data_)[current_]); 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci bool operator==(const const_iterator& other) const { 801cb0ef41Sopenharmony_ci return current_ == other.current_ && data_ == other.data_; 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci bool operator!=(const const_iterator& other) const { 831cb0ef41Sopenharmony_ci return !(*this == other); 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci const_iterator& operator++() { 861cb0ef41Sopenharmony_ci ++current_; 871cb0ef41Sopenharmony_ci return *this; 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci const_iterator operator++(int); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci private: 921cb0ef41Sopenharmony_ci const ZoneVector<T>* data_; 931cb0ef41Sopenharmony_ci size_t current_; 941cb0ef41Sopenharmony_ci}; 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_citemplate <class T, T def(Zone*)> 971cb0ef41Sopenharmony_citypename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::begin() 981cb0ef41Sopenharmony_ci const { 991cb0ef41Sopenharmony_ci return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 0); 1001cb0ef41Sopenharmony_ci} 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_citemplate <class T, T def(Zone*)> 1031cb0ef41Sopenharmony_citypename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::end() const { 1041cb0ef41Sopenharmony_ci return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 1051cb0ef41Sopenharmony_ci aux_data_.size()); 1061cb0ef41Sopenharmony_ci} 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci} // namespace compiler 1091cb0ef41Sopenharmony_ci} // namespace internal 1101cb0ef41Sopenharmony_ci} // namespace v8 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci#endif // V8_COMPILER_NODE_AUX_DATA_H_ 113