Home | History | Annotate | Download | only in compiler
      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, T def()>
     19 class NodeAuxData {
     20  public:
     21   explicit NodeAuxData(Zone* zone) : aux_data_(zone) {}
     22 
     23   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, def());
     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] : def();
     32   }
     33 
     34   class const_iterator;
     35   friend class const_iterator;
     36 
     37   const_iterator begin() const;
     38   const_iterator end() const;
     39 
     40  private:
     41   ZoneVector<T> aux_data_;
     42 };
     43 
     44 template <class T, T def()>
     45 class NodeAuxData<T, def>::const_iterator {
     46  public:
     47   typedef std::forward_iterator_tag iterator_category;
     48   typedef int difference_type;
     49   typedef std::pair<size_t, T> value_type;
     50   typedef value_type* pointer;
     51   typedef value_type& reference;
     52 
     53   const_iterator(const ZoneVector<T>* data, size_t current)
     54       : data_(data), current_(current) {}
     55   const_iterator(const const_iterator& other)
     56       : data_(other.data_), current_(other.current_) {}
     57 
     58   value_type operator*() const {
     59     return std::make_pair(current_, (*data_)[current_]);
     60   }
     61   bool operator==(const const_iterator& other) const {
     62     return current_ == other.current_ && data_ == other.data_;
     63   }
     64   bool operator!=(const const_iterator& other) const {
     65     return !(*this == other);
     66   }
     67   const_iterator& operator++() {
     68     ++current_;
     69     return *this;
     70   }
     71   const_iterator operator++(int);
     72 
     73  private:
     74   const ZoneVector<T>* data_;
     75   size_t current_;
     76 };
     77 
     78 template <class T, T def()>
     79 typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::begin()
     80     const {
     81   return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 0);
     82 }
     83 
     84 template <class T, T def()>
     85 typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::end() const {
     86   return typename NodeAuxData<T, def>::const_iterator(&aux_data_,
     87                                                       aux_data_.size());
     88 }
     89 
     90 }  // namespace compiler
     91 }  // namespace internal
     92 }  // namespace v8
     93 
     94 #endif  // V8_COMPILER_NODE_AUX_DATA_H_
     95