Included assert.h so that the code compiles under newer versions of GCC.
[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   // dropAllReferences() - This function is in charge of "letting go" of all
53   // objects that this User refers to.  This allows one to
54   // 'delete' a whole class at a time, even though there may be circular
55   // references... first all references are dropped, and all use counts go to
56   // zero.  Then everything is delete'd for real.  Note that no operations are
57   // valid on an object that has "dropped all references", except operator 
58   // delete.
59   //
60   inline void dropAllReferences() {
61     Operands.clear();
62   }
63
64   /// replaceUsesOfWith - Replaces all references to the "From" definition with
65   /// references to the "To" definition.
66   ///
67   void replaceUsesOfWith(Value *From, Value *To);
68
69   // Methods for support type inquiry through isa, cast, and dyn_cast:
70   static inline bool classof(const User *) { return true; }
71   static inline bool classof(const Value *V) {
72     return V->getValueType() == Value::GlobalVariableVal ||
73            V->getValueType() == Value::ConstantVal ||
74            V->getValueType() == Value::InstructionVal;
75   }
76 };
77
78 template<> struct simplify_type<User::op_iterator> {
79   typedef Value* SimpleType;
80   
81   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
82     return (SimpleType)Val->get();
83   }
84 };
85 template<> struct simplify_type<const User::op_iterator>
86   : public simplify_type<User::op_iterator> {};
87
88 template<> struct simplify_type<User::const_op_iterator> {
89   typedef Value* SimpleType;
90   
91   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
92     return (SimpleType)Val->get();
93   }
94 };
95 template<> struct simplify_type<const User::const_op_iterator>
96   : public simplify_type<User::const_op_iterator> {};
97
98 #endif