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