Remove trailing whitespace
[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   /// OperandList - This is a pointer to the array of Users for this operand.
31   /// For nodes of fixed arity (e.g. a binary operator) this array will live
32   /// embedded into the derived class.  For nodes of variable arity
33   /// (e.g. ConstantArrays, CallInst, PHINodes, etc), this memory will be
34   /// dynamically allocated and should be destroyed by the classes virtual dtor.
35   Use *OperandList;
36
37   /// NumOperands - The number of values used by this User.
38   ///
39   unsigned NumOperands;
40
41 public:
42   User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps,
43        const std::string &name = "")
44     : Value(Ty, vty, name), OperandList(OpList), NumOperands(NumOps) {}
45
46   Value *getOperand(unsigned i) const {
47     assert(i < NumOperands && "getOperand() out of range!");
48     return OperandList[i];
49   }
50   void setOperand(unsigned i, Value *Val) {
51     assert(i < NumOperands && "setOperand() out of range!");
52     OperandList[i] = Val;
53   }
54   unsigned getNumOperands() const { return NumOperands; }
55
56   // ---------------------------------------------------------------------------
57   // Operand Iterator interface...
58   //
59   typedef Use*       op_iterator;
60   typedef const Use* const_op_iterator;
61
62   inline op_iterator       op_begin()       { return OperandList; }
63   inline const_op_iterator op_begin() const { return OperandList; }
64   inline op_iterator       op_end()         { return OperandList+NumOperands; }
65   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
66
67   // dropAllReferences() - This function is in charge of "letting go" of all
68   // objects that this User refers to.  This allows one to
69   // 'delete' a whole class at a time, even though there may be circular
70   // references... first all references are dropped, and all use counts go to
71   // zero.  Then everything is delete'd for real.  Note that no operations are
72   // valid on an object that has "dropped all references", except operator
73   // delete.
74   //
75   void dropAllReferences() {
76     Use *OL = OperandList;
77     for (unsigned i = 0, e = NumOperands; i != e; ++i)
78       OL[i].set(0);
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 isa<Instruction>(V) || isa<Constant>(V);
90   }
91 };
92
93 template<> struct simplify_type<User::op_iterator> {
94   typedef Value* SimpleType;
95
96   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
97     return static_cast<SimpleType>(Val->get());
98   }
99 };
100
101 template<> struct simplify_type<const User::op_iterator>
102   : public simplify_type<User::op_iterator> {};
103
104 template<> struct simplify_type<User::const_op_iterator> {
105   typedef Value* SimpleType;
106
107   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
108     return static_cast<SimpleType>(Val->get());
109   }
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