node-aux-data.h 2.73 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_NODE_AUX_DATA_H_
#define V8_COMPILER_NODE_AUX_DATA_H_

8
#include "src/compiler/node.h"
9
#include "src/zone/zone-containers.h"
10 11 12 13 14 15 16 17

namespace v8 {
namespace internal {
namespace compiler {

// Forward declarations.
class Node;

18 19 20 21 22 23
template <class T>
T DefaultConstruct() {
  return T();
}

template <class T, T def() = DefaultConstruct<T>>
24 25
class NodeAuxData {
 public:
26
  explicit NodeAuxData(Zone* zone) : aux_data_(zone) {}
27 28
  explicit NodeAuxData(size_t initial_size, Zone* zone)
      : aux_data_(initial_size, zone) {}
29

30 31
  // Update entry. Returns true iff entry was changed.
  bool Set(Node* node, T const& data) {
32
    size_t const id = node->id();
33
    if (id >= aux_data_.size()) aux_data_.resize(id + 1, def());
34 35 36 37 38
    if (aux_data_[id] != data) {
      aux_data_[id] = data;
      return true;
    }
    return false;
39 40 41 42
  }

  T Get(Node* node) const {
    size_t const id = node->id();
43
    return (id < aux_data_.size()) ? aux_data_[id] : def();
44
  }
45

46 47 48 49 50 51
  class const_iterator;
  friend class const_iterator;

  const_iterator begin() const;
  const_iterator end() const;

52
 private:
53
  ZoneVector<T> aux_data_;
54 55
};

56 57
template <class T, T def()>
class NodeAuxData<T, def>::const_iterator {
58
 public:
59 60 61 62 63
  using iterator_category = std::forward_iterator_tag;
  using difference_type = int;
  using value_type = std::pair<size_t, T>;
  using pointer = value_type*;
  using reference = value_type&;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  const_iterator(const ZoneVector<T>* data, size_t current)
      : data_(data), current_(current) {}
  const_iterator(const const_iterator& other)
      : data_(other.data_), current_(other.current_) {}

  value_type operator*() const {
    return std::make_pair(current_, (*data_)[current_]);
  }
  bool operator==(const const_iterator& other) const {
    return current_ == other.current_ && data_ == other.data_;
  }
  bool operator!=(const const_iterator& other) const {
    return !(*this == other);
  }
  const_iterator& operator++() {
    ++current_;
    return *this;
  }
  const_iterator operator++(int);

 private:
  const ZoneVector<T>* data_;
  size_t current_;
};

90 91 92 93
template <class T, T def()>
typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::begin()
    const {
  return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 0);
94 95
}

96 97 98 99
template <class T, T def()>
typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::end() const {
  return typename NodeAuxData<T, def>::const_iterator(&aux_data_,
                                                      aux_data_.size());
100 101
}

102 103 104 105 106
}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_NODE_AUX_DATA_H_