Add new op_erase method
[oota-llvm.git] / include / llvm / User.h
1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
2 //
3 // This class defines the interface that one who 'use's a Value must implement.
4 // Each instance of the Value class keeps track of what User's have handles
5 // to it.
6 //
7 //  * Instructions are the largest class of User's.
8 //  * Constants may be users of other constants (think arrays and stuff)
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_USER_H
13 #define LLVM_USER_H
14
15 #include "llvm/Value.h"
16
17 class User : public Value {
18   User(const User &);             // Do not implement
19 protected:
20   std::vector<Use> Operands;
21 public:
22   User(const Type *Ty, ValueTy vty, const std::string &name = "");
23
24   inline Value *getOperand(unsigned i) { 
25     assert(i < Operands.size() && "getOperand() out of range!");
26     return Operands[i];
27   }
28   inline const Value *getOperand(unsigned i) const {
29     assert(i < Operands.size() && "getOperand() const out of range!");
30     return Operands[i];
31   }
32   inline void setOperand(unsigned i, Value *Val) {
33     assert(i < Operands.size() && "setOperand() out of range!");
34     Operands[i] = Val;
35   }
36   inline unsigned getNumOperands() const { return Operands.size(); }
37
38   // ---------------------------------------------------------------------------
39   // Operand Iterator interface...
40   //
41   typedef std::vector<Use>::iterator       op_iterator;
42   typedef std::vector<Use>::const_iterator const_op_iterator;
43
44   void op_reserve(unsigned NumElements) { Operands.reserve(NumElements); }
45
46   inline op_iterator       op_begin()       { return Operands.begin(); }
47   inline const_op_iterator op_begin() const { return Operands.begin(); }
48   inline op_iterator       op_end()         { return Operands.end(); }
49   inline const_op_iterator op_end()   const { return Operands.end(); }
50
51   /// op_erase - This method is used to remove one of the arguments from the
52   /// operands list.  Only use this if you know what you are doing.
53   ///
54   op_iterator op_erase(op_iterator I) { return Operands.erase(I); }
55   op_iterator op_erase(op_iterator I, op_iterator E) {
56     return Operands.erase(I, E);
57   }
58
59   // dropAllReferences() - This function is in charge of "letting go" of all
60   // objects that this User refers to.  This allows one to
61   // 'delete' a whole class at a time, even though there may be circular
62   // references... first all references are dropped, and all use counts go to
63   // zero.  Then everything is delete'd for real.  Note that no operations are
64   // valid on an object that has "dropped all references", except operator 
65   // delete.
66   //
67   inline void dropAllReferences() {
68     Operands.clear();
69   }
70
71   /// replaceUsesOfWith - Replaces all references to the "From" definition with
72   /// references to the "To" definition.
73   ///
74   void replaceUsesOfWith(Value *From, Value *To);
75
76   // Methods for support type inquiry through isa, cast, and dyn_cast:
77   static inline bool classof(const User *) { return true; }
78   static inline bool classof(const Value *V) {
79     return V->getValueType() == Value::GlobalVariableVal ||
80            V->getValueType() == Value::ConstantVal ||
81            V->getValueType() == Value::InstructionVal;
82   }
83 };
84
85 template<> struct simplify_type<User::op_iterator> {
86   typedef Value* SimpleType;
87   
88   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
89     return (SimpleType)Val->get();
90   }
91 };
92 template<> struct simplify_type<const User::op_iterator>
93   : public simplify_type<User::op_iterator> {};
94
95 template<> struct simplify_type<User::const_op_iterator> {
96   typedef Value* SimpleType;
97   
98   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
99     return (SimpleType)Val->get();
100   }
101 };
102 template<> struct simplify_type<const User::const_op_iterator>
103   : public simplify_type<User::const_op_iterator> {};
104
105 #endif