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