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