Now Bitcode reader bug is fixed. Reapply 80839.
[oota-llvm.git] / include / llvm / Metadata.h
1 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// This file contains the declarations for metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_MDNODE_H
17 #define LLVM_MDNODE_H
18
19 #include "llvm/User.h"
20 #include "llvm/Type.h"
21 #include "llvm/OperandTraits.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/ilist_node.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/ValueHandle.h"
28
29 namespace llvm {
30 class Constant;
31 class LLVMContext;
32
33 //===----------------------------------------------------------------------===//
34 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
35 class MetadataBase : public User {
36 private:
37   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
38   /// the number actually in use.
39   unsigned ReservedSpace;
40
41 protected:
42   MetadataBase(const Type *Ty, unsigned scid)
43     : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
44
45   void resizeOperands(unsigned NumOps);
46 public:
47   /// isNullValue - Return true if this is the value that would be returned by
48   /// getNullValue.  This always returns false because getNullValue will never
49   /// produce metadata.
50   virtual bool isNullValue() const {
51     return false;
52   }
53
54   /// Methods for support type inquiry through isa, cast, and dyn_cast:
55   static inline bool classof(const MetadataBase *) { return true; }
56   static bool classof(const Value *V) {
57     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
58       || V->getValueID() == NamedMDNodeVal;
59   }
60 };
61
62 //===----------------------------------------------------------------------===//
63 /// MDString - a single uniqued string.
64 /// These are used to efficiently contain a byte sequence for metadata.
65 /// MDString is always unnamd.
66 class MDString : public MetadataBase {
67   MDString(const MDString &);            // DO NOT IMPLEMENT
68   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
69   unsigned getNumOperands();             // DO NOT IMPLEMENT
70
71   StringRef Str;
72 protected:
73   explicit MDString(LLVMContext &C, const char *begin, unsigned l)
74     : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
75
76 public:
77   // Do not allocate any space for operands.
78   void *operator new(size_t s) {
79     return User::operator new(s, 0);
80   }
81   static MDString *get(LLVMContext &Context, const StringRef &Str);
82   
83   StringRef getString() const { return Str; }
84
85   unsigned length() const { return Str.size(); }
86
87   /// begin() - Pointer to the first byte of the string.
88   ///
89   const char *begin() const { return Str.begin(); }
90
91   /// end() - Pointer to one byte past the end of the string.
92   ///
93   const char *end() const { return Str.end(); }
94
95   /// Methods for support type inquiry through isa, cast, and dyn_cast:
96   static inline bool classof(const MDString *) { return true; }
97   static bool classof(const Value *V) {
98     return V->getValueID() == MDStringVal;
99   }
100 };
101
102 //===----------------------------------------------------------------------===//
103 /// MDNode - a tuple of other values.
104 /// These contain a list of the values that represent the metadata. 
105 /// MDNode is always unnamed.
106 class MDNode : public MetadataBase, public FoldingSetNode {
107   MDNode(const MDNode &);                // DO NOT IMPLEMENT
108   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
109   // getNumOperands - Make this only available for private uses.
110   unsigned getNumOperands() { return User::getNumOperands();  }
111
112   friend class ElementVH;
113   // Use CallbackVH to hold MDNOde elements.
114   struct ElementVH : public CallbackVH {
115     MDNode *Parent;
116     ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
117     ~ElementVH() {}
118
119     virtual void deleted() {
120       Parent->replaceElement(this->operator Value*(), 0);
121     }
122
123     virtual void allUsesReplacedWith(Value *NV) {
124       Parent->replaceElement(this->operator Value*(), NV);
125     }
126   };
127   // Replace each instance of F from the element list of this node with T.
128   void replaceElement(Value *F, Value *T);
129
130   SmallVector<ElementVH, 4> Node;
131
132 protected:
133   explicit MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals);
134 public:
135   // Do not allocate any space for operands.
136   void *operator new(size_t s) {
137     return User::operator new(s, 0);
138   }
139   // Constructors and destructors.
140   static MDNode *get(LLVMContext &Context, 
141                      Value* const* Vals, unsigned NumVals);
142
143   /// dropAllReferences - Remove all uses and clear node vector.
144   void dropAllReferences();
145
146   /// ~MDNode - Destroy MDNode.
147   ~MDNode();
148   
149   /// getElement - Return specified element.
150   Value *getElement(unsigned i) const {
151     assert (getNumElements() > i && "Invalid element number!");
152     return Node[i];
153   }
154
155   /// getNumElements - Return number of MDNode elements.
156   unsigned getNumElements() const {
157     return Node.size();
158   }
159
160   // Element access
161   typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
162   typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
163   /// elem_empty - Return true if MDNode is empty.
164   bool elem_empty() const                { return Node.empty(); }
165   const_elem_iterator elem_begin() const { return Node.begin(); }
166   const_elem_iterator elem_end() const   { return Node.end();   }
167   elem_iterator elem_begin()             { return Node.begin(); }
168   elem_iterator elem_end()               { return Node.end();   }
169
170   /// isNullValue - Return true if this is the value that would be returned by
171   /// getNullValue.  This always returns false because getNullValue will never
172   /// produce metadata.
173   virtual bool isNullValue() const {
174     return false;
175   }
176
177   /// Profile - calculate a unique identifier for this MDNode to collapse
178   /// duplicates
179   void Profile(FoldingSetNodeID &ID) const;
180
181   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
182     llvm_unreachable("This should never be called because MDNodes have no ops");
183   }
184
185   /// Methods for support type inquiry through isa, cast, and dyn_cast:
186   static inline bool classof(const MDNode *) { return true; }
187   static bool classof(const Value *V) {
188     return V->getValueID() == MDNodeVal;
189   }
190 };
191
192 //===----------------------------------------------------------------------===//
193 /// WeakMetadataVH - a weak value handle for metadata.
194 class WeakMetadataVH : public WeakVH {
195 public:
196   WeakMetadataVH() : WeakVH() {}
197   WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
198   WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
199   
200   operator Value*() const {
201     llvm_unreachable("WeakMetadataVH only handles Metadata");
202   }
203
204   operator MetadataBase*() const {
205    return dyn_cast_or_null<MetadataBase>(getValPtr());
206   }
207 };
208
209 //===----------------------------------------------------------------------===//
210 /// NamedMDNode - a tuple of other metadata. 
211 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
212 template<typename ValueSubClass, typename ItemParentClass>
213   class SymbolTableListTraits;
214
215 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
216   friend class SymbolTableListTraits<NamedMDNode, Module>;
217   friend class LLVMContextImpl;
218
219   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
220   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
221   // getNumOperands - Make this only available for private uses.
222   unsigned getNumOperands() { return User::getNumOperands();  }
223
224   Module *Parent;
225   SmallVector<WeakMetadataVH, 4> Node;
226   typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
227
228 protected:
229   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const* Vals, 
230                        unsigned NumVals, Module *M = 0);
231 public:
232   // Do not allocate any space for operands.
233   void *operator new(size_t s) {
234     return User::operator new(s, 0);
235   }
236   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
237                              MetadataBase*const*MDs, 
238                              unsigned NumMDs, Module *M = 0) {
239     return new NamedMDNode(C, N, MDs, NumMDs, M);
240   }
241
242   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
243
244   /// eraseFromParent - Drop all references and remove the node from parent
245   /// module.
246   void eraseFromParent();
247
248   /// dropAllReferences - Remove all uses and clear node vector.
249   void dropAllReferences();
250
251   /// ~NamedMDNode - Destroy NamedMDNode.
252   ~NamedMDNode();
253
254   /// getParent - Get the module that holds this named metadata collection.
255   inline Module *getParent() { return Parent; }
256   inline const Module *getParent() const { return Parent; }
257   void setParent(Module *M) { Parent = M; }
258
259   /// getElement - Return specified element.
260   MetadataBase *getElement(unsigned i) const {
261     assert (getNumElements() > i && "Invalid element number!");
262     return Node[i];
263   }
264
265   /// getNumElements - Return number of NamedMDNode elements.
266   unsigned getNumElements() const {
267     return Node.size();
268   }
269
270   /// addElement - Add metadata element.
271   void addElement(MetadataBase *M) {
272     resizeOperands(0);
273     OperandList[NumOperands++] = M;
274     Node.push_back(WeakMetadataVH(M));
275   }
276
277   typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
278   bool elem_empty() const                { return Node.empty(); }
279   const_elem_iterator elem_begin() const { return Node.begin(); }
280   const_elem_iterator elem_end() const   { return Node.end();   }
281   elem_iterator elem_begin()             { return Node.begin(); }
282   elem_iterator elem_end()               { return Node.end();   }
283
284   /// isNullValue - Return true if this is the value that would be returned by
285   /// getNullValue.  This always returns false because getNullValue will never
286   /// produce metadata.
287   virtual bool isNullValue() const {
288     return false;
289   }
290
291   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
292     llvm_unreachable(
293                 "This should never be called because NamedMDNodes have no ops");
294   }
295
296   /// Methods for support type inquiry through isa, cast, and dyn_cast:
297   static inline bool classof(const NamedMDNode *) { return true; }
298   static bool classof(const Value *V) {
299     return V->getValueID() == NamedMDNodeVal;
300   }
301 };
302
303 } // end llvm namespace
304
305 #endif