Module's are no longer Value's.
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
2 //
3 // This file defines the very important Value class.  This is subclassed by a
4 // bunch of other important classes, like Instruction, Function, Type, etc...
5 //
6 // This file also defines the Use<> template for users of value.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_VALUE_H
11 #define LLVM_VALUE_H
12
13 #include <vector>
14 #include "llvm/Annotation.h"
15 #include "llvm/AbstractTypeUser.h"
16 #include "Support/Casting.h"
17
18 class User;
19 class Type;
20 class Constant;
21 class Argument;
22 class Instruction;
23 class BasicBlock;
24 class GlobalValue;
25 class Function;
26 class GlobalVariable;
27 class SymbolTable;
28 template<class ValueSubclass, class ItemParentType, class SymTabType> 
29   class ValueHolder;
30
31 //===----------------------------------------------------------------------===//
32 //                                 Value Class
33 //===----------------------------------------------------------------------===//
34
35 class Value : public Annotable,         // Values are annotable
36               public AbstractTypeUser { // Values use potentially abstract types
37 public:
38   enum ValueTy {
39     TypeVal,                // This is an instance of Type
40     ConstantVal,            // This is an instance of Constant
41     ArgumentVal,            // This is an instance of Argument
42     InstructionVal,         // This is an instance of Instruction
43     BasicBlockVal,          // This is an instance of BasicBlock
44     FunctionVal,            // This is an instance of Function
45     GlobalVariableVal,      // This is an instance of GlobalVariable
46   };
47
48 private:
49   std::vector<User *> Uses;
50   std::string Name;
51   PATypeHandle<Type> Ty;
52   ValueTy VTy;
53
54   Value(const Value &);              // Do not implement
55 protected:
56   inline void setType(const Type *ty) { Ty = ty; }
57 public:
58   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
59   virtual ~Value();
60   
61   // Support for debugging 
62   void dump() const;
63
64   // Implement operator<< on Value...
65   virtual void print(std::ostream &O) const = 0;
66   
67   // All values can potentially be typed
68   inline const Type *getType() const { return Ty; }
69   
70   // All values can potentially be named...
71   inline bool               hasName() const { return Name != ""; }
72   inline const std::string &getName() const { return Name; }
73
74   virtual void setName(const std::string &name, SymbolTable * = 0) {
75     Name = name;
76   }
77   
78   // Methods for determining the subtype of this Value.  The getValueType()
79   // method returns the type of the value directly.  The cast*() methods are
80   // equivalent to using dynamic_cast<>... if the cast is successful, this is
81   // returned, otherwise you get a null pointer.
82   //
83   // The family of functions Val->cast<type>Asserting() is used in the same
84   // way as the Val->cast<type>() instructions, but they assert the expected
85   // type instead of checking it at runtime.
86   //
87   inline ValueTy getValueType() const { return VTy; }
88   
89   // replaceAllUsesWith - Go through the uses list for this definition and make
90   // each use point to "D" instead of "this".  After this completes, 'this's 
91   // use list should be empty.
92   //
93   void replaceAllUsesWith(Value *D);
94
95   // refineAbstractType - This function is implemented because we use
96   // potentially abstract types, and these types may be resolved to more
97   // concrete types after we are constructed.
98   //
99   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
100   
101   //----------------------------------------------------------------------
102   // Methods for handling the vector of uses of this Value.
103   //
104   typedef std::vector<User*>::iterator       use_iterator;
105   typedef std::vector<User*>::const_iterator use_const_iterator;
106
107   inline unsigned           use_size()  const { return Uses.size();  }
108   inline bool               use_empty() const { return Uses.empty(); }
109   inline use_iterator       use_begin()       { return Uses.begin(); }
110   inline use_const_iterator use_begin() const { return Uses.begin(); }
111   inline use_iterator       use_end()         { return Uses.end();   }
112   inline use_const_iterator use_end()   const { return Uses.end();   }
113   inline User              *use_back()        { return Uses.back();  }
114   inline const User        *use_back()  const { return Uses.back();  }
115
116   inline void use_push_back(User *I)   { Uses.push_back(I); }
117   User *use_remove(use_iterator &I);
118
119   inline void addUse(User *I)      { Uses.push_back(I); }
120   void killUse(User *I);
121 };
122
123 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
124   if (V == 0)
125     OS << "<null> value!\n";
126   else
127     V->print(OS);
128   return OS;
129 }
130
131
132 //===----------------------------------------------------------------------===//
133 //                                 UseTy Class
134 //===----------------------------------------------------------------------===//
135
136 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
137 // list of a definition node up-to-date really easy.
138 //
139 template<class ValueSubclass>
140 class UseTy {
141   ValueSubclass *Val;
142   User *U;
143 public:
144   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
145     Val = v; U = user;
146     if (Val) Val->addUse(U);
147   }
148
149   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
150
151   inline operator ValueSubclass *() const { return Val; }
152
153   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
154     Val = 0;
155     U = user.U;
156     operator=(user.Val);
157   }
158   inline ValueSubclass *operator=(ValueSubclass *V) { 
159     if (Val) Val->killUse(U);
160     Val = V;
161     if (V) V->addUse(U);
162     return V;
163   }
164
165   inline       ValueSubclass *operator->()       { return Val; }
166   inline const ValueSubclass *operator->() const { return Val; }
167
168   inline       ValueSubclass *get()       { return Val; }
169   inline const ValueSubclass *get() const { return Val; }
170
171   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
172     if (Val) Val->killUse(U);
173     Val = user.Val;
174     Val->addUse(U);
175     return *this;
176   }
177 };
178
179 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
180
181 // Provide a specialization of real_type to work with use's... to make them a
182 // bit more transparent.
183 //
184 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
185
186
187 // isa - Provide some specializations of isa so that we don't have to include
188 // the subtype header files to test to see if the value is a subclass...
189 //
190 template <> inline bool isa<Type, const Value*>(const Value *Val) { 
191   return Val->getValueType() == Value::TypeVal;
192 }
193 template <> inline bool isa<Type, Value*>(Value *Val) { 
194   return Val->getValueType() == Value::TypeVal;
195 }
196 template <> inline bool isa<Constant, const Value*>(const Value *Val) { 
197   return Val->getValueType() == Value::ConstantVal; 
198 }
199 template <> inline bool isa<Constant, Value*>(Value *Val) { 
200   return Val->getValueType() == Value::ConstantVal; 
201 }
202 template <> inline bool isa<Argument, const Value*>(const Value *Val) { 
203   return Val->getValueType() == Value::ArgumentVal;
204 }
205 template <> inline bool isa<Argument, Value*>(Value *Val) { 
206   return Val->getValueType() == Value::ArgumentVal;
207 }
208 template <> inline bool isa<Instruction, const Value*>(const Value *Val) { 
209   return Val->getValueType() == Value::InstructionVal;
210 }
211 template <> inline bool isa<Instruction, Value*>(Value *Val) { 
212   return Val->getValueType() == Value::InstructionVal;
213 }
214 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) { 
215   return Val->getValueType() == Value::BasicBlockVal;
216 }
217 template <> inline bool isa<BasicBlock, Value*>(Value *Val) { 
218   return Val->getValueType() == Value::BasicBlockVal;
219 }
220 template <> inline bool isa<Function, const Value*>(const Value *Val) { 
221   return Val->getValueType() == Value::FunctionVal;
222 }
223 template <> inline bool isa<Function, Value*>(Value *Val) { 
224   return Val->getValueType() == Value::FunctionVal;
225 }
226 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) { 
227   return Val->getValueType() == Value::GlobalVariableVal;
228 }
229 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) { 
230   return Val->getValueType() == Value::GlobalVariableVal;
231 }
232 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) { 
233   return isa<GlobalVariable>(Val) || isa<Function>(Val);
234 }
235 template <> inline bool isa<GlobalValue, Value*>(Value *Val) { 
236   return isa<GlobalVariable>(Val) || isa<Function>(Val);
237 }
238
239 #endif