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