d751eb1c6a8e960b689b895efe3f3b7d58013cb7
[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 <string>
12 #include <list>
13
14 class User;
15 class Type;
16 template<class ValueSubclass, class ItemParentType> class ValueHolder;
17
18 //===----------------------------------------------------------------------===//
19 //                                 Value Class
20 //===----------------------------------------------------------------------===//
21
22 class Value {
23 public:
24   enum ValueTy {
25     TypeVal,                // This is an instance of Type
26     ConstantVal,            // This is an instance of ConstPoolVal
27     MethodArgumentVal,      // This is an instance of MethodArgument
28     InstructionVal,         // This is an instance of Instruction
29
30     BasicBlockVal,          // This is an instance of BasicBlock
31     MethodVal,              // This is an instance of Method
32     ModuleVal,              // This is an instance of Module
33   };
34
35 private:
36   list<User *> Uses;
37   string Name;
38   const Type *Ty;
39   ValueTy VTy;
40
41   Value(const Value &);              // Do not implement
42 protected:
43   inline void setType(const Type *ty) { Ty = ty; }
44 public:
45   Value(const Type *Ty, ValueTy vty, const string &name = "");
46   virtual ~Value();
47
48   inline const Type *getType() const { return Ty; }
49   inline ValueTy getValueType() const { return VTy; }
50
51   inline bool hasName() const { return Name != ""; }
52   inline const string &getName() const { return Name; }
53   virtual void setName(const string &name) { Name = name; }
54
55
56   // replaceAllUsesWith - Go through the uses list for this definition and make
57   // each use point to "D" instead of "this".  After this completes, 'this's 
58   // use list should be empty.
59   //
60   void replaceAllUsesWith(Value *D);
61
62   //----------------------------------------------------------------------
63   // Methods for handling the list of uses of this DEF.
64   //
65   typedef list<User*>::iterator       use_iterator;
66   typedef list<User*>::const_iterator use_const_iterator;
67
68   inline bool               use_size()  const { return Uses.size();  }
69   inline bool               use_empty() const { return Uses.empty(); }
70   inline use_iterator       use_begin()       { return Uses.begin(); }
71   inline use_const_iterator use_begin() const { return Uses.begin(); }
72   inline use_iterator       use_end()         { return Uses.end();   }
73   inline use_const_iterator use_end()   const { return Uses.end();   }
74
75   inline void use_push_back(User *I)   { Uses.push_back(I); }
76   User *use_remove(use_iterator &I);
77
78   inline void addUse(User *I)      { Uses.push_back(I); }
79   void killUse(User *I);
80 };
81
82 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
83 // list of a definition node up-to-date really easy.
84 //
85 template<class ValueSubclass>
86 class UseTy {
87   ValueSubclass *Val;
88   User *U;
89 public:
90   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
91     Val = v; U = user;
92     if (Val) Val->addUse(U);
93   }
94
95   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
96
97   inline operator ValueSubclass *() const { return Val; }
98
99   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
100     Val = 0;
101     U = user.U;
102     operator=(user);
103   }
104   inline ValueSubclass *operator=(ValueSubclass *V) { 
105     if (Val) Val->killUse(U);
106     Val = V;
107     if (V) V->addUse(U);
108     return V;
109   }
110
111   inline       ValueSubclass *operator->()       { return Val; }
112   inline const ValueSubclass *operator->() const { return Val; }
113
114   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
115     if (Val) Val->killUse(U);
116     Val = user.Val;
117     Val->addUse(U);
118     return *this;
119   }
120 };
121
122 typedef UseTy<Value> Use;
123
124 #endif