Added debugging support.
[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 Def, Method, Module, Type, etc...
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_VALUE_H
9 #define LLVM_VALUE_H
10
11 #include <vector>
12 #include "llvm/Annotation.h"
13 #include "llvm/AbstractTypeUser.h"
14
15 class User;
16 class Type;
17 class ConstPoolVal;
18 class MethodArgument;
19 class Instruction;
20 class BasicBlock;
21 class Method;
22 class GlobalVariable;
23 class Module;
24 class SymbolTable;
25 template<class ValueSubclass, class ItemParentType, class SymTabType> 
26   class ValueHolder;
27
28 //===----------------------------------------------------------------------===//
29 //                                 Value Class
30 //===----------------------------------------------------------------------===//
31
32 class Value : public Annotable,         // Values are annotable
33               public AbstractTypeUser { // Values use potentially abstract types
34 public:
35   enum ValueTy {
36     TypeVal,                // This is an instance of Type
37     ConstantVal,            // This is an instance of ConstPoolVal
38     MethodArgumentVal,      // This is an instance of MethodArgument
39     InstructionVal,         // This is an instance of Instruction
40     BasicBlockVal,          // This is an instance of BasicBlock
41     MethodVal,              // This is an instance of Method
42     GlobalVal,              // This is an instance of GlobalVariable
43     ModuleVal,              // This is an instance of Module
44   };
45
46 private:
47   vector<User *> Uses;
48   string Name;
49   PATypeHandle<Type> Ty;
50   ValueTy VTy;
51
52   Value(const Value &);              // Do not implement
53 protected:
54   inline void setType(const Type *ty) { Ty = ty; }
55 public:
56   Value(const Type *Ty, ValueTy vty, const string &name = "");
57   virtual ~Value();
58   
59   // Support for debugging 
60   void                  dump() const;
61   
62   // All values can potentially be typed
63   inline const Type*    getType() const { return Ty; }
64   
65   // All values can potentially be named...
66   inline bool           hasName() const { return Name != ""; }
67   inline const string&  getName() const { return Name; }
68   virtual void          setName(const string &name, SymbolTable * = 0)
69                                         { Name = name; }
70   
71   // Methods for determining the subtype of this Value.  The getValueType()
72   // method returns the type of the value directly.  The cast*() methods are
73   // equivalent to using dynamic_cast<>... if the cast is successful, this is
74   // returned, otherwise you get a null pointer, allowing expressions like:
75   //
76   // if (Instruction *I = Val->castInstruction()) { ... }
77   //
78   // This section also defines a family of isType, isConstant,
79   // isMethodArgument, etc functions...
80   //
81   // The family of functions Val->cast<type>Asserting() is used in the same
82   // way as the Val->cast<type>() instructions, but they assert the expected
83   // type instead of checking it at runtime.
84   //
85   inline ValueTy getValueType() const { return VTy; }
86   
87   // Use a macro to define the functions, otherwise these definitions are just
88   // really long and ugly.
89 #define CAST_FN(NAME, CLASS)                                              \
90   inline bool is##NAME() const { return VTy == NAME##Val; }               \
91   inline const CLASS *cast##NAME() const { /*const version */             \
92     return is##NAME() ? (const CLASS*)this : 0;                           \
93   }                                                                       \
94   inline CLASS *cast##NAME() {         /* nonconst version */             \
95     return is##NAME() ? (CLASS*)this : 0;                                 \
96   }                                                                       \
97   inline const CLASS *cast##NAME##Asserting() const { /*const version */  \
98     assert(is##NAME() && "Expected Value Type: " #NAME);                  \
99     return (const CLASS*)this;                                            \
100   }                                                                       \
101   inline CLASS *cast##NAME##Asserting() {         /* nonconst version */  \
102     assert(is##NAME() && "Expected Value Type: " #NAME);                  \
103     return (CLASS*)this;                                                  \
104   }                                                                       \
105
106   CAST_FN(Constant      ,       ConstPoolVal  )
107   CAST_FN(MethodArgument,       MethodArgument)
108   CAST_FN(Instruction   ,       Instruction   )
109   CAST_FN(BasicBlock    ,       BasicBlock    )
110   CAST_FN(Method        ,       Method        )
111   CAST_FN(Global        ,       GlobalVariable)
112   CAST_FN(Module        ,       Module        )
113 #undef CAST_FN
114
115   // Type value is special, because there is no nonconst version of functions!
116   inline bool isType() const { return VTy == TypeVal; }
117   inline const Type *castType() const {
118     return (VTy == TypeVal) ? (const Type*)this : 0;
119   }
120   inline const Type *castTypeAsserting() const {
121     assert(isType() && "Expected Value Type: Type");
122     return (const Type*)this;
123   }
124
125   // replaceAllUsesWith - Go through the uses list for this definition and make
126   // each use point to "D" instead of "this".  After this completes, 'this's 
127   // use list should be empty.
128   //
129   void replaceAllUsesWith(Value *D);
130
131   // refineAbstractType - This function is implemented because we use
132   // potentially abstract types, and these types may be resolved to more
133   // concrete types after we are constructed.
134   //
135   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
136   
137   //----------------------------------------------------------------------
138   // Methods for handling the vector of uses of this Value.
139   //
140   typedef vector<User*>::iterator       use_iterator;
141   typedef vector<User*>::const_iterator use_const_iterator;
142
143   inline unsigned           use_size()  const { return Uses.size();  }
144   inline bool               use_empty() const { return Uses.empty(); }
145   inline use_iterator       use_begin()       { return Uses.begin(); }
146   inline use_const_iterator use_begin() const { return Uses.begin(); }
147   inline use_iterator       use_end()         { return Uses.end();   }
148   inline use_const_iterator use_end()   const { return Uses.end();   }
149
150   inline void use_push_back(User *I)   { Uses.push_back(I); }
151   User *use_remove(use_iterator &I);
152
153   inline void addUse(User *I)      { Uses.push_back(I); }
154   void killUse(User *I);
155 };
156
157 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
158 // list of a definition node up-to-date really easy.
159 //
160 template<class ValueSubclass>
161 class UseTy {
162   ValueSubclass *Val;
163   User *U;
164 public:
165   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
166     Val = v; U = user;
167     if (Val) Val->addUse(U);
168   }
169
170   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
171
172   inline operator ValueSubclass *() const { return Val; }
173
174   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
175     Val = 0;
176     U = user.U;
177     operator=(user.Val);
178   }
179   inline ValueSubclass *operator=(ValueSubclass *V) { 
180     if (Val) Val->killUse(U);
181     Val = V;
182     if (V) V->addUse(U);
183     return V;
184   }
185
186   inline       ValueSubclass *operator->()       { return Val; }
187   inline const ValueSubclass *operator->() const { return Val; }
188
189   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
190     if (Val) Val->killUse(U);
191     Val = user.Val;
192     Val->addUse(U);
193     return *this;
194   }
195 };
196
197 typedef UseTy<Value> Use;
198
199 //----------------------------------------------------------------------
200 // Debugging support for class Value and its subclasses.
201 //
202
203 void            DebugValue      (const Value* V);
204 void            DebugValue      (const Value& V);
205
206 ostream&        operator<<      (ostream &o, const Value& I);
207
208 #endif