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