remove all of the various setName implementations, consolidating them into
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the very important Value class.  This is subclassed by a
11 // bunch of other important classes, like Instruction, Function, Type, etc...
12 //
13 // This file also defines the Use<> template for users of value.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_VALUE_H
18 #define LLVM_VALUE_H
19
20 #include "llvm/AbstractTypeUser.h"
21 #include "llvm/Use.h"
22 #include "llvm/Support/Casting.h"
23 #include <string>
24
25 namespace llvm {
26
27 class Constant;
28 class Argument;
29 class Instruction;
30 class BasicBlock;
31 class GlobalValue;
32 class Function;
33 class GlobalVariable;
34 class SymbolTable;
35
36 //===----------------------------------------------------------------------===//
37 //                                 Value Class
38 //===----------------------------------------------------------------------===//
39
40 /// Value - The base class of all values computed by a program that may be used
41 /// as operands to other values.
42 ///
43 class Value {
44   unsigned short SubclassID;         // Subclass identifier (for isa/dyn_cast)
45 protected:
46   /// SubclassData - This member is defined by this class, but is not used for
47   /// anything.  Subclasses can use it to hold whatever state they find useful.
48   /// This field is initialized to zero by the ctor.
49   unsigned short SubclassData;
50 private:
51   PATypeHolder Ty;
52   Use *UseList;
53   std::string Name;
54
55   void operator=(const Value &);     // Do not implement
56   Value(const Value &);              // Do not implement
57
58 public:
59   Value(const Type *Ty, unsigned scid, const std::string &name = "");
60   virtual ~Value();
61   
62   /// dump - Support for debugging, callable in GDB: V->dump()
63   //
64   virtual void dump() const;
65
66   /// print - Implement operator<< on Value...
67   ///
68   virtual void print(std::ostream &O) const = 0;
69   
70   /// All values are typed, get the type of this value.
71   ///
72   inline const Type *getType() const { return Ty; }
73   
74   // All values can potentially be named...
75   inline bool               hasName() const { return !Name.empty(); }
76   inline const std::string &getName() const { return Name; }
77
78   void setName(const std::string &name);
79   
80   /// replaceAllUsesWith - Go through the uses list for this definition and make
81   /// each use point to "V" instead of "this".  After this completes, 'this's 
82   /// use list is guaranteed to be empty.
83   ///
84   void replaceAllUsesWith(Value *V);
85
86   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
87   // Only use when in type resolution situations!
88   void uncheckedReplaceAllUsesWith(Value *V);
89
90   //----------------------------------------------------------------------
91   // Methods for handling the vector of uses of this Value.
92   //
93   typedef value_use_iterator<User>       use_iterator;
94   typedef value_use_iterator<const User> use_const_iterator;
95
96   bool               use_empty() const { return UseList == 0; }
97   use_iterator       use_begin()       { return use_iterator(UseList); }
98   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
99   use_iterator       use_end()         { return use_iterator(0);   }
100   use_const_iterator use_end()   const { return use_const_iterator(0);   }
101   User              *use_back()        { return *use_begin(); }
102   const User        *use_back() const  { return *use_begin(); }
103
104   /// hasOneUse - Return true if there is exactly one user of this value.  This
105   /// is specialized because it is a common request and does not require
106   /// traversing the whole use list.
107   ///
108   bool hasOneUse() const {
109     use_const_iterator I = use_begin(), E = use_end();
110     if (I == E) return false;
111     return ++I == E;
112   }
113
114   /// hasNUses - Return true if this Value has exactly N users.
115   ///
116   bool hasNUses(unsigned N) const;
117
118   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
119   /// logically equivalent to getNumUses() >= N.
120   ///
121   bool hasNUsesOrMore(unsigned N) const;
122
123   /// getNumUses - This method computes the number of uses of this Value.  This
124   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
125   /// to check for specific values.
126   unsigned getNumUses() const;
127
128   /// addUse/killUse - These two methods should only be used by the Use class.
129   ///
130   void addUse(Use &U) { U.addToList(&UseList); }
131
132   /// getValueType - Return an ID for the concrete type of this object.  This is
133   /// used to implement the classof checks.  This should not be used for any
134   /// other purpose, as the values may change as LLVM evolves.  Also, note that
135   /// starting with the InstructionVal value, the value stored is actually the
136   /// Instruction opcode, so there are more than just these values possible here
137   /// (and Instruction must be last).
138   ///
139   enum ValueTy {
140     ArgumentVal,              // This is an instance of Argument
141     BasicBlockVal,            // This is an instance of BasicBlock
142     FunctionVal,              // This is an instance of Function
143     GlobalVariableVal,        // This is an instance of GlobalVariable
144     UndefValueVal,            // This is an instance of UndefValue
145     ConstantExprVal,          // This is an instance of ConstantExpr
146     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
147     SimpleConstantVal,        // This is some other type of Constant
148     InstructionVal,           // This is an instance of Instruction
149     ValueListVal              // This is for bcreader, a special ValTy
150   };
151   unsigned getValueType() const {
152     return SubclassID;
153   }
154
155   // Methods for support type inquiry through isa, cast, and dyn_cast:
156   static inline bool classof(const Value *V) {
157     return true; // Values are always values.
158   }
159
160   /// getRawType - This should only be used to implement the vmcore library.
161   ///
162   const Type *getRawType() const { return Ty.getRawType(); }
163
164 private:
165   /// FIXME: this is a gross hack, needed by another gross hack.  Eliminate!
166   void setValueType(unsigned VT) { SubclassID = VT; }
167   friend class Instruction;
168 };
169
170 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
171   V.print(OS);
172   return OS;
173 }
174
175 void Use::init(Value *v, User *user) {
176   Val = v;
177   U = user;
178   if (Val) Val->addUse(*this);
179 }
180
181 Use::~Use() {
182   if (Val) removeFromList();
183 }
184
185 void Use::set(Value *V) { 
186   if (Val) removeFromList();
187   Val = V;
188   if (V) V->addUse(*this);
189 }
190
191
192 // isa - Provide some specializations of isa so that we don't have to include
193 // the subtype header files to test to see if the value is a subclass...
194 //
195 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
196   return Val.getValueType() == Value::SimpleConstantVal ||
197          Val.getValueType() == Value::FunctionVal ||
198          Val.getValueType() == Value::GlobalVariableVal ||
199          Val.getValueType() == Value::ConstantExprVal ||
200          Val.getValueType() == Value::ConstantAggregateZeroVal ||
201          Val.getValueType() == Value::UndefValueVal;
202 }
203 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
204   return Val.getValueType() == Value::ArgumentVal;
205 }
206 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
207   return Val.getValueType() >= Value::InstructionVal;
208 }
209 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
210   return Val.getValueType() == Value::BasicBlockVal;
211 }
212 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
213   return Val.getValueType() == Value::FunctionVal;
214 }
215 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
216   return Val.getValueType() == Value::GlobalVariableVal;
217 }
218 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
219   return isa<GlobalVariable>(Val) || isa<Function>(Val);
220 }
221
222 } // End llvm namespace
223
224 #endif