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