#ifndef wasm_analysis_cfg_impl_h
#define wasm_analysis_cfg_impl_h
#include "cfg.h"
namespace wasm::analysis {
template<typename T> struct _indirect_ptr_iterator {
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using different_type = off_t;
using reference = const T&;
using pointer = const T*;
const T* const* ptr;
const T& operator*() const { return **ptr; }
const T& operator[](int n) const { return **(ptr + n); }
_indirect_ptr_iterator& operator+=(int n) {
ptr += n;
return *this;
}
_indirect_ptr_iterator& operator-=(int n) {
ptr -= n;
return *this;
}
_indirect_ptr_iterator& operator++() { return *this += 1; }
_indirect_ptr_iterator operator++(int) {
_indirect_ptr_iterator it = *this;
++(*this);
return it;
}
_indirect_ptr_iterator& operator--() { return *this -= 1; }
_indirect_ptr_iterator operator--(int) {
_indirect_ptr_iterator it = *this;
--(*this);
return it;
}
_indirect_ptr_iterator operator+(int n) const {
_indirect_ptr_iterator it = *this;
it += n;
return it;
}
_indirect_ptr_iterator operator-(int n) const {
_indirect_ptr_iterator it = *this;
it -= n;
return it;
}
bool operator==(const _indirect_ptr_iterator& other) const {
return ptr == other.ptr;
}
bool operator!=(const _indirect_ptr_iterator& other) const {
return !(*this == other);
}
bool operator<(const _indirect_ptr_iterator& other) const {
return ptr < other.ptr;
}
bool operator>(const _indirect_ptr_iterator& other) const {
return ptr > other.ptr;
}
bool operator<=(const _indirect_ptr_iterator& other) const {
return ptr <= other.ptr;
}
bool operator>=(const _indirect_ptr_iterator& other) const {
return ptr >= other.ptr;
}
};
template<typename T>
_indirect_ptr_iterator<T> operator+(int n,
const _indirect_ptr_iterator<T>& it) {
return it + n;
}
template<typename T> struct _indirect_ptr_vec {
using iterator = _indirect_ptr_iterator<T>;
const std::vector<T*>& vec;
_indirect_ptr_vec(const std::vector<T*>& vec) : vec(vec) {}
iterator begin() const { return {&vec.data()[0]}; }
iterator end() const { return {&vec.data()[vec.size()]}; }
};
struct BasicBlock::Predecessors : _indirect_ptr_vec<BasicBlock> {
Predecessors(const BasicBlock& block)
: _indirect_ptr_vec(block.predecessors) {}
};
struct BasicBlock::Successors : _indirect_ptr_vec<BasicBlock> {
Successors(const BasicBlock& block) : _indirect_ptr_vec(block.successors) {}
};
inline BasicBlock::Predecessors BasicBlock::preds() const {
return Predecessors(*this);
}
inline BasicBlock::Successors BasicBlock::succs() const {
return Successors(*this);
}
}
#endif