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