From: Owen Anderson Date: Thu, 6 Mar 2014 01:12:56 +0000 (+0000) Subject: Add a iterator and interator_range interface to the operands of a NamedMDNode. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d6ea721c1f92d1b35af98fc4a6a74fb9f05be22d;p=oota-llvm.git Add a iterator and interator_range interface to the operands of a NamedMDNode. The iterator is a little complex because we don't want to expose the implementation details (TrackingVH) of the operand vector to clients. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203053 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/IR/Metadata.h b/include/llvm/IR/Metadata.h index 2d272d3fcec..27f7fd935de 100644 --- a/include/llvm/IR/Metadata.h +++ b/include/llvm/IR/Metadata.h @@ -19,6 +19,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/ilist_node.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/IR/Value.h" namespace llvm { @@ -207,6 +208,35 @@ class NamedMDNode : public ilist_node { explicit NamedMDNode(const Twine &N); + template + class op_iterator_impl { + const NamedMDNode *Node; + unsigned Idx; + op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { } + + friend class NamedMDNode; + + public: + op_iterator_impl() : Node(0), Idx(0) { } + op_iterator_impl(const op_iterator_impl &o) : Node(o.Node), Idx(o.Idx) { } + + bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; } + bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; } + op_iterator_impl &operator++() { + ++Idx; return *this; + } + op_iterator_impl operator++(int) { + op_iterator_impl tmp(*this); + operator++(); + return tmp; + } + op_iterator_impl &operator=(const op_iterator_impl &o) { + Idx = o.Idx; + return *this; + } + T operator*() const { return Node->getOperand(Idx); } + }; + public: /// eraseFromParent - Drop all references and remove the node from parent /// module. @@ -239,6 +269,24 @@ public: /// dump() - Allow printing of NamedMDNodes from the debugger. void dump() const; + + // --------------------------------------------------------------------------- + // Operand Iterator interface... + // + typedef op_iterator_impl op_iterator; + op_iterator op_begin() { return op_iterator(this, 0); } + op_iterator op_end() { return op_iterator(this, getNumOperands()); } + + typedef op_iterator_impl const_op_iterator; + const_op_iterator op_begin() const { return const_op_iterator(this, 0); } + const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); } + + inline iterator_range operands() { + return iterator_range(op_begin(), op_end()); + } + inline iterator_range operands() const { + return iterator_range(op_begin(), op_end()); + } }; } // end llvm namespace