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