Convert comments to Doxygen style
[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 #include <iostream>
18
19 class User;
20 class Type;
21 class Constant;
22 class Argument;
23 class Instruction;
24 class BasicBlock;
25 class GlobalValue;
26 class Function;
27 class GlobalVariable;
28 class SymbolTable;
29
30 //===----------------------------------------------------------------------===//
31 //                                 Value Class
32 //===----------------------------------------------------------------------===//
33
34 /// Value - The base class of all values computed by a program that may be used
35 /// as operands to other values.
36 ///
37 class Value : public Annotable,         // Values are annotable
38               public AbstractTypeUser { // Values use potentially abstract types
39 public:
40   enum ValueTy {
41     TypeVal,                // This is an instance of Type
42     ConstantVal,            // This is an instance of Constant
43     ArgumentVal,            // This is an instance of Argument
44     InstructionVal,         // This is an instance of Instruction
45     BasicBlockVal,          // This is an instance of BasicBlock
46     FunctionVal,            // This is an instance of Function
47     GlobalVariableVal,      // This is an instance of GlobalVariable
48   };
49
50 private:
51   std::vector<User *> Uses;
52   std::string Name;
53   PATypeHandle<Type> Ty;
54   ValueTy VTy;
55
56   void operator=(const Value &);     // Do not implement
57   Value(const Value &);              // Do not implement
58 protected:
59   inline void setType(const Type *ty) { Ty = ty; }
60 public:
61   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
62   virtual ~Value();
63   
64   /// dump - Support for debugging, callable in GDB: V->dump()
65   //
66   void dump() const;
67
68   /// print - Implement operator<< on Value...
69   ///
70   virtual void print(std::ostream &O) const = 0;
71   
72   /// All values are typed, get the type of this value.
73   ///
74   inline const Type *getType() const { return Ty; }
75   
76   // All values can potentially be named...
77   inline bool               hasName() const { return Name != ""; }
78   inline const std::string &getName() const { return Name; }
79
80   virtual void setName(const std::string &name, SymbolTable * = 0) {
81     Name = name;
82   }
83   
84   /// getValueType - Return the immediate subclass of this Value.
85   ///
86   inline ValueTy getValueType() const { return VTy; }
87   
88   /// replaceAllUsesWith - Go through the uses list for this definition and make
89   /// each use point to "V" instead of "this".  After this completes, 'this's 
90   /// use list is guaranteed to be empty.
91   ///
92   void replaceAllUsesWith(Value *V);
93
94   /// refineAbstractType - This function is implemented because we use
95   /// potentially abstract types, and these types may be resolved to more
96   /// concrete types after we are constructed.
97   ///
98   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
99   
100   //----------------------------------------------------------------------
101   // Methods for handling the vector of uses of this Value.
102   //
103   typedef std::vector<User*>::iterator       use_iterator;
104   typedef std::vector<User*>::const_iterator use_const_iterator;
105
106   inline unsigned           use_size()  const { return Uses.size();  }
107   inline bool               use_empty() const { return Uses.empty(); }
108   inline use_iterator       use_begin()       { return Uses.begin(); }
109   inline use_const_iterator use_begin() const { return Uses.begin(); }
110   inline use_iterator       use_end()         { return Uses.end();   }
111   inline use_const_iterator use_end()   const { return Uses.end();   }
112   inline User              *use_back()        { return Uses.back();  }
113   inline const User        *use_back()  const { return Uses.back();  }
114
115   inline void use_push_back(User *I)   { Uses.push_back(I); }
116   User *use_remove(use_iterator &I);
117
118   inline void addUse(User *I)      { Uses.push_back(I); }
119   void killUse(User *I);
120 };
121
122 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
123   if (V == 0)
124     OS << "<null> value!\n";
125   else
126     V->print(OS);
127   return OS;
128 }
129
130 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
131   V.print(OS);
132   return OS;
133 }
134
135
136 //===----------------------------------------------------------------------===//
137 //                                 UseTy Class
138 //===----------------------------------------------------------------------===//
139
140 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
141 // list of a definition node up-to-date really easy.
142 //
143 template<class ValueSubclass>
144 class UseTy {
145   ValueSubclass *Val;
146   User *U;
147 public:
148   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
149     Val = v; U = user;
150     if (Val) Val->addUse(U);
151   }
152
153   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
154
155   inline operator ValueSubclass *() const { return Val; }
156
157   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
158     Val = 0;
159     U = user.U;
160     operator=(user.Val);
161   }
162   inline ValueSubclass *operator=(ValueSubclass *V) { 
163     if (Val) Val->killUse(U);
164     Val = V;
165     if (V) V->addUse(U);
166     return V;
167   }
168
169   inline       ValueSubclass *operator->()       { return Val; }
170   inline const ValueSubclass *operator->() const { return Val; }
171
172   inline       ValueSubclass *get()       { return Val; }
173   inline const ValueSubclass *get() const { return Val; }
174
175   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
176     if (Val) Val->killUse(U);
177     Val = user.Val;
178     Val->addUse(U);
179     return *this;
180   }
181 };
182
183 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
184
185 template<typename From> struct simplify_type<UseTy<From> > {
186   typedef typename simplify_type<From*>::SimpleType SimpleType;
187   
188   static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
189     return (SimpleType)Val.get();
190   }
191 };
192 template<typename From> struct simplify_type<const UseTy<From> > {
193   typedef typename simplify_type<From*>::SimpleType SimpleType;
194   
195   static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
196     return (SimpleType)Val.get();
197   }
198 };
199
200 // isa - Provide some specializations of isa so that we don't have to include
201 // the subtype header files to test to see if the value is a subclass...
202 //
203 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
204   return Val.getValueType() == Value::TypeVal;
205 }
206 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
207   return Val.getValueType() == Value::ConstantVal; 
208 }
209 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
210   return Val.getValueType() == Value::ArgumentVal;
211 }
212 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
213   return Val.getValueType() == Value::InstructionVal;
214 }
215 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
216   return Val.getValueType() == Value::BasicBlockVal;
217 }
218 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
219   return Val.getValueType() == Value::FunctionVal;
220 }
221 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
222   return Val.getValueType() == Value::GlobalVariableVal;
223 }
224 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
225   return isa<GlobalVariable>(Val) || isa<Function>(Val);
226 }
227
228 #endif