changes to make it compatible with 64bit gcc
[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 <ostream>
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 class Value : public Annotable,         // Values are annotable
35               public AbstractTypeUser { // Values use potentially abstract types
36 public:
37   enum ValueTy {
38     TypeVal,                // This is an instance of Type
39     ConstantVal,            // This is an instance of Constant
40     ArgumentVal,            // This is an instance of Argument
41     InstructionVal,         // This is an instance of Instruction
42     BasicBlockVal,          // This is an instance of BasicBlock
43     FunctionVal,            // This is an instance of Function
44     GlobalVariableVal,      // This is an instance of GlobalVariable
45   };
46
47 private:
48   std::vector<User *> Uses;
49   std::string Name;
50   PATypeHandle<Type> Ty;
51   ValueTy VTy;
52
53   Value(const Value &);              // Do not implement
54 protected:
55   inline void setType(const Type *ty) { Ty = ty; }
56 public:
57   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
58   virtual ~Value();
59   
60   // Support for debugging 
61   void dump() const;
62
63   // Implement operator<< on Value...
64   virtual void print(std::ostream &O) const = 0;
65   
66   // All values can potentially be typed
67   inline const Type *getType() const { return Ty; }
68   
69   // All values can potentially be named...
70   inline bool               hasName() const { return Name != ""; }
71   inline const std::string &getName() const { return Name; }
72
73   virtual void setName(const std::string &name, SymbolTable * = 0) {
74     Name = name;
75   }
76   
77   // Methods for determining the subtype of this Value.  The getValueType()
78   // method returns the type of the value directly.  The cast*() methods are
79   // equivalent to using dynamic_cast<>... if the cast is successful, this is
80   // returned, otherwise you get a null pointer.
81   //
82   // The family of functions Val->cast<type>Asserting() is used in the same
83   // way as the Val->cast<type>() instructions, but they assert the expected
84   // type instead of checking it at runtime.
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 "D" instead of "this".  After this completes, 'this's 
90   // use list should be empty.
91   //
92   void replaceAllUsesWith(Value *D);
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