Use StringRef to construct MDString.
[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_METADATA_H
17 #define LLVM_METADATA_H
18
19 #include "llvm/Value.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/StringMap.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 Instruction;
32 class LLVMContext;
33
34 //===----------------------------------------------------------------------===//
35 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
36 class MetadataBase : public Value {
37 protected:
38   MetadataBase(const Type *Ty, unsigned scid)
39     : Value(Ty, scid) {}
40
41 public:
42
43   /// Methods for support type inquiry through isa, cast, and dyn_cast:
44   static inline bool classof(const MetadataBase *) { return true; }
45   static bool classof(const Value *V) {
46     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
47       || V->getValueID() == NamedMDNodeVal;
48   }
49 };
50
51 //===----------------------------------------------------------------------===//
52 /// MDString - a single uniqued string.
53 /// These are used to efficiently contain a byte sequence for metadata.
54 /// MDString is always unnamd.
55 class MDString : public MetadataBase {
56   MDString(const MDString &);            // DO NOT IMPLEMENT
57
58   StringRef Str;
59 protected:
60   explicit MDString(LLVMContext &C, StringRef S)
61     : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
62
63 public:
64   static MDString *get(LLVMContext &Context, StringRef Str);
65   
66   StringRef getString() const { return Str; }
67
68   unsigned getLength() const { return Str.size(); }
69
70   typedef StringRef::iterator iterator;
71   
72   /// begin() - Pointer to the first byte of the string.
73   ///
74   iterator begin() const { return Str.begin(); }
75
76   /// end() - Pointer to one byte past the end of the string.
77   ///
78   iterator end() const { return Str.end(); }
79
80   /// Methods for support type inquiry through isa, cast, and dyn_cast:
81   static inline bool classof(const MDString *) { return true; }
82   static bool classof(const Value *V) {
83     return V->getValueID() == MDStringVal;
84   }
85 };
86
87 //===----------------------------------------------------------------------===//
88 /// MDNode - a tuple of other values.
89 /// These contain a list of the values that represent the metadata. 
90 /// MDNode is always unnamed.
91 class MDNode : public MetadataBase, public FoldingSetNode {
92   MDNode(const MDNode &);                // DO NOT IMPLEMENT
93
94   friend class ElementVH;
95   // Use CallbackVH to hold MDNOde elements.
96   struct ElementVH : public CallbackVH {
97     MDNode *Parent;
98     ElementVH() {}
99     ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
100     ~ElementVH() {}
101
102     virtual void deleted() {
103       Parent->replaceElement(this->operator Value*(), 0);
104     }
105
106     virtual void allUsesReplacedWith(Value *NV) {
107       Parent->replaceElement(this->operator Value*(), NV);
108     }
109   };
110   // Replace each instance of F from the element list of this node with T.
111   void replaceElement(Value *F, Value *T);
112
113   ElementVH *Node;
114   unsigned NodeSize;
115
116 protected:
117   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
118 public:
119   // Constructors and destructors.
120   static MDNode *get(LLVMContext &Context, 
121                      Value *const *Vals, unsigned NumVals);
122
123   /// ~MDNode - Destroy MDNode.
124   ~MDNode();
125   
126   /// getElement - Return specified element.
127   Value *getElement(unsigned i) const {
128     assert(i < getNumElements() && "Invalid element number!");
129     return Node[i];
130   }
131
132   /// getNumElements - Return number of MDNode elements.
133   unsigned getNumElements() const { return NodeSize; }
134
135   /// Profile - calculate a unique identifier for this MDNode to collapse
136   /// duplicates
137   void Profile(FoldingSetNodeID &ID) const;
138
139   /// Methods for support type inquiry through isa, cast, and dyn_cast:
140   static inline bool classof(const MDNode *) { return true; }
141   static bool classof(const Value *V) {
142     return V->getValueID() == MDNodeVal;
143   }
144 };
145
146 //===----------------------------------------------------------------------===//
147 /// WeakMetadataVH - a weak value handle for metadata.
148 class WeakMetadataVH : public WeakVH {
149 public:
150   WeakMetadataVH() : WeakVH() {}
151   WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
152   WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
153   
154   operator Value*() const {
155     llvm_unreachable("WeakMetadataVH only handles Metadata");
156   }
157
158   operator MetadataBase*() const {
159    return dyn_cast_or_null<MetadataBase>(getValPtr());
160   }
161 };
162
163 //===----------------------------------------------------------------------===//
164 /// NamedMDNode - a tuple of other metadata. 
165 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
166 template<typename ValueSubClass, typename ItemParentClass>
167   class SymbolTableListTraits;
168
169 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
170   friend class SymbolTableListTraits<NamedMDNode, Module>;
171   friend class LLVMContextImpl;
172
173   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
174
175   Module *Parent;
176   SmallVector<WeakMetadataVH, 4> Node;
177   typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
178
179   void setParent(Module *M) { Parent = M; }
180 protected:
181   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals, 
182                        unsigned NumVals, Module *M = 0);
183 public:
184   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
185                              MetadataBase *const *MDs, 
186                              unsigned NumMDs, Module *M = 0) {
187     return new NamedMDNode(C, N, MDs, NumMDs, M);
188   }
189
190   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
191
192   /// eraseFromParent - Drop all references and remove the node from parent
193   /// module.
194   void eraseFromParent();
195
196   /// dropAllReferences - Remove all uses and clear node vector.
197   void dropAllReferences();
198
199   /// ~NamedMDNode - Destroy NamedMDNode.
200   ~NamedMDNode();
201
202   /// getParent - Get the module that holds this named metadata collection.
203   inline Module *getParent() { return Parent; }
204   inline const Module *getParent() const { return Parent; }
205
206   /// getElement - Return specified element.
207   MetadataBase *getElement(unsigned i) const {
208     assert(i < getNumElements() && "Invalid element number!");
209     return Node[i];
210   }
211
212   /// getNumElements - Return number of NamedMDNode elements.
213   unsigned getNumElements() const {
214     return Node.size();
215   }
216
217   /// addElement - Add metadata element.
218   void addElement(MetadataBase *M) {
219     Node.push_back(WeakMetadataVH(M));
220   }
221
222   typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
223   bool elem_empty() const                { return Node.empty(); }
224   const_elem_iterator elem_begin() const { return Node.begin(); }
225   const_elem_iterator elem_end() const   { return Node.end();   }
226   elem_iterator elem_begin()             { return Node.begin(); }
227   elem_iterator elem_end()               { return Node.end();   }
228
229   /// Methods for support type inquiry through isa, cast, and dyn_cast:
230   static inline bool classof(const NamedMDNode *) { return true; }
231   static bool classof(const Value *V) {
232     return V->getValueID() == NamedMDNodeVal;
233   }
234 };
235
236 //===----------------------------------------------------------------------===//
237 /// MetadataContext -
238 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
239 /// types. Custom metadata handler names do not contain spaces. And the name
240 /// must start with an alphabet. The regular expression used to check name
241 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
242 class MetadataContext {
243 public:
244   typedef std::pair<unsigned, WeakVH> MDPairTy;
245   typedef SmallVector<MDPairTy, 2> MDMapTy;
246   typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
247   friend class BitcodeReader;
248 private:
249
250   /// MetadataStore - Collection of metadata used in this context.
251   MDStoreTy MetadataStore;
252
253   /// MDHandlerNames - Map to hold metadata handler names.
254   StringMap<unsigned> MDHandlerNames;
255
256 public:
257   /// registerMDKind - Register a new metadata kind and return its ID.
258   /// A metadata kind can be registered only once. 
259   unsigned registerMDKind(StringRef Name);
260
261   /// getMDKind - Return metadata kind. If the requested metadata kind
262   /// is not registered then return 0.
263   unsigned getMDKind(StringRef Name) const;
264
265   /// isValidName - Return true if Name is a valid custom metadata handler name.
266   static bool isValidName(StringRef Name);
267
268   /// getMD - Get the metadata of given kind attached to an Instruction.
269   /// If the metadata is not found then return 0.
270   MDNode *getMD(unsigned Kind, const Instruction *Inst);
271
272   /// getMDs - Get the metadata attached to an Instruction.
273   const MDMapTy *getMDs(const Instruction *Inst);
274
275   /// addMD - Attach the metadata of given kind to an Instruction.
276   void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
277   
278   /// removeMD - Remove metadata of given kind attached with an instuction.
279   void removeMD(unsigned Kind, Instruction *Inst);
280   
281   /// removeAllMetadata - Remove all metadata attached with an instruction.
282   void removeAllMetadata(Instruction *Inst);
283
284   /// copyMD - If metadata is attached with Instruction In1 then attach
285   /// the same metadata to In2.
286   void copyMD(Instruction *In1, Instruction *In2);
287
288   /// getHandlerNames - Get handler names. This is used by bitcode
289   /// writer.
290   const StringMap<unsigned> *getHandlerNames();
291
292   /// ValueIsDeleted - This handler is used to update metadata store
293   /// when a value is deleted.
294   void ValueIsDeleted(const Value *) {}
295   void ValueIsDeleted(Instruction *Inst) {
296     removeAllMetadata(Inst);
297   }
298   void ValueIsRAUWd(Value *V1, Value *V2);
299
300   /// ValueIsCloned - This handler is used to update metadata store
301   /// when In1 is cloned to create In2.
302   void ValueIsCloned(const Instruction *In1, Instruction *In2);
303 };
304
305 } // end llvm namespace
306
307 #endif