Remove explicit inline qualifiers when the implicit ones work just as well
[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 "llvm/AbstractTypeUser.h"
14 #include "Support/Annotation.h"
15 #include "Support/Casting.h"
16 #include <iostream>
17 #include <vector>
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 struct Value : public Annotable {         // Values are annotable
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   PATypeHolder Ty;
52   ValueTy VTy;
53
54   void operator=(const Value &);     // Do not implement
55   Value(const Value &);              // Do not implement
56 public:
57   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
58   virtual ~Value();
59   
60   /// dump - Support for debugging, callable in GDB: V->dump()
61   //
62   virtual void dump() const;
63
64   /// print - Implement operator<< on Value...
65   ///
66   virtual void print(std::ostream &O) const = 0;
67   
68   /// All values are typed, get the type of this value.
69   ///
70   inline const Type *getType() const { return Ty; }
71   
72   // All values can potentially be named...
73   inline bool               hasName() const { return Name != ""; }
74   inline const std::string &getName() const { return Name; }
75
76   virtual void setName(const std::string &name, SymbolTable * = 0) {
77     Name = name;
78   }
79   
80   /// getValueType - Return the immediate subclass of this Value.
81   ///
82   inline ValueTy getValueType() const { return VTy; }
83   
84   /// replaceAllUsesWith - Go through the uses list for this definition and make
85   /// each use point to "V" instead of "this".  After this completes, 'this's 
86   /// use list is guaranteed to be empty.
87   ///
88   void replaceAllUsesWith(Value *V);
89
90   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
91   // Only use when in type resolution situations!
92   void uncheckedReplaceAllUsesWith(Value *V);
93
94   //----------------------------------------------------------------------
95   // Methods for handling the vector of uses of this Value.
96   //
97   typedef std::vector<User*>::iterator       use_iterator;
98   typedef std::vector<User*>::const_iterator use_const_iterator;
99
100   inline unsigned           use_size()  const { return Uses.size();  }
101   inline bool               use_empty() const { return Uses.empty(); }
102   inline use_iterator       use_begin()       { return Uses.begin(); }
103   inline use_const_iterator use_begin() const { return Uses.begin(); }
104   inline use_iterator       use_end()         { return Uses.end();   }
105   inline use_const_iterator use_end()   const { return Uses.end();   }
106   inline User              *use_back()        { return Uses.back();  }
107   inline const User        *use_back()  const { return Uses.back();  }
108
109   /// addUse/killUse - These two methods should only be used by the Use class
110   /// below.
111   inline void addUse(User *I)      { Uses.push_back(I); }
112   void killUse(User *I);
113 };
114
115 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
116   if (V == 0)
117     OS << "<null> value!\n";
118   else
119     V->print(OS);
120   return OS;
121 }
122
123 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
124   V.print(OS);
125   return OS;
126 }
127
128
129 //===----------------------------------------------------------------------===//
130 //                                  Use Class
131 //===----------------------------------------------------------------------===//
132
133 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
134 //
135 class Use {
136   Value *Val;
137   User *U;
138 public:
139   inline Use(Value *v, User *user) {
140     Val = v; U = user;
141     if (Val) Val->addUse(U);
142   }
143
144   inline Use(const Use &user) {
145     Val = 0;
146     U = user.U;
147     operator=(user.Val);
148   }
149   inline ~Use() { if (Val) Val->killUse(U); }
150   inline operator Value*() const { return Val; }
151
152   inline Value *operator=(Value *V) { 
153     if (Val) Val->killUse(U);
154     Val = V;
155     if (V) V->addUse(U);
156     return V;
157   }
158
159   inline       Value *operator->()       { return Val; }
160   inline const Value *operator->() const { return Val; }
161
162   inline       Value *get()       { return Val; }
163   inline const Value *get() const { return Val; }
164
165   inline const Use &operator=(const Use &user) {
166     if (Val) Val->killUse(U);
167     Val = user.Val;
168     Val->addUse(U);
169     return *this;
170   }
171 };
172
173 template<> struct simplify_type<Use> {
174   typedef Value* SimpleType;
175   
176   static SimpleType getSimplifiedValue(const Use &Val) {
177     return (SimpleType)Val.get();
178   }
179 };
180 template<> struct simplify_type<const Use> {
181   typedef Value* SimpleType;
182   
183   static SimpleType getSimplifiedValue(const Use &Val) {
184     return (SimpleType)Val.get();
185   }
186 };
187
188 // isa - Provide some specializations of isa so that we don't have to include
189 // the subtype header files to test to see if the value is a subclass...
190 //
191 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
192   return Val.getValueType() == Value::TypeVal;
193 }
194 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
195   return Val.getValueType() == Value::ConstantVal; 
196 }
197 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
198   return Val.getValueType() == Value::ArgumentVal;
199 }
200 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
201   return Val.getValueType() == Value::InstructionVal;
202 }
203 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
204   return Val.getValueType() == Value::BasicBlockVal;
205 }
206 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
207   return Val.getValueType() == Value::FunctionVal;
208 }
209 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
210   return Val.getValueType() == Value::GlobalVariableVal;
211 }
212 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
213   return isa<GlobalVariable>(Val) || isa<Function>(Val);
214 }
215
216 #endif