1 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declarations for metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
14 //===----------------------------------------------------------------------===//
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"
32 //===----------------------------------------------------------------------===//
33 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
34 class MetadataBase : public User {
36 /// ReservedSpace - The number of operands actually allocated. NumOperands is
37 /// the number actually in use.
38 unsigned ReservedSpace;
41 MetadataBase(const Type *Ty, unsigned scid)
42 : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
44 void resizeOperands(unsigned NumOps);
46 /// getType() specialization - Type is always MetadataTy.
48 inline const Type *getType() const {
49 return Type::MetadataTy;
52 /// isNullValue - Return true if this is the value that would be returned by
53 /// getNullValue. This always returns false because getNullValue will never
55 virtual bool isNullValue() const {
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;
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
78 explicit MDString(const char *begin, unsigned l)
79 : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
82 // Do not allocate any space for operands.
83 void *operator new(size_t s) {
84 return User::operator new(s, 0);
86 static MDString *get(LLVMContext &Context, const StringRef &Str);
88 StringRef getString() const { return Str; }
90 unsigned length() const { return Str.size(); }
92 /// begin() - Pointer to the first byte of the string.
94 const char *begin() const { return Str.begin(); }
96 /// end() - Pointer to one byte past the end of the string.
98 const char *end() const { return Str.end(); }
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;
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(); }
117 SmallVector<WeakVH, 4> Node;
119 explicit MDNode(Value*const* Vals, unsigned NumVals);
121 // Do not allocate any space for operands.
122 void *operator new(size_t s) {
123 return User::operator new(s, 0);
125 // Constructors and destructors.
126 static MDNode *get(LLVMContext &Context,
127 Value* const* Vals, unsigned NumVals);
129 /// dropAllReferences - Remove all uses and clear node vector.
130 void dropAllReferences();
132 /// ~MDNode - Destroy NamedMDNode.
135 /// getElement - Return specified element.
136 Value *getElement(unsigned i) const {
137 assert (getNumElements() > i && "Invalid element number!");
141 /// getNumElements - Return number of MDNode elements.
142 unsigned getNumElements() const {
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(); }
156 /// getType() specialization - Type is always MetadataTy.
158 inline const Type *getType() const {
159 return Type::MetadataTy;
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 {
169 /// Profile - calculate a unique identifier for this MDNode to collapse
171 void Profile(FoldingSetNodeID &ID) const;
173 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
174 llvm_unreachable("This should never be called because MDNodes have no ops");
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;
184 //===----------------------------------------------------------------------===//
185 /// WeakMetadataVH - a weak value handle for metadata.
186 class WeakMetadataVH : public WeakVH {
188 WeakMetadataVH() : WeakVH() {}
189 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
190 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
192 operator Value*() const {
193 llvm_unreachable("WeakMetadataVH only handles Metadata");
196 operator MetadataBase*() const {
197 return dyn_cast_or_null<MetadataBase>(getValPtr());
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;
207 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
208 friend class SymbolTableListTraits<NamedMDNode, Module>;
209 friend class LLVMContextImpl;
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(); }
217 SmallVector<WeakMetadataVH, 4> Node;
218 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
221 explicit NamedMDNode(const Twine &N, MetadataBase*const* Vals,
222 unsigned NumVals, Module *M = 0);
224 // Do not allocate any space for operands.
225 void *operator new(size_t s) {
226 return User::operator new(s, 0);
228 static NamedMDNode *Create(const Twine &N, MetadataBase*const*MDs,
229 unsigned NumMDs, Module *M = 0) {
230 return new NamedMDNode(N, MDs, NumMDs, M);
233 /// eraseFromParent - Drop all references and remove the node from parent
235 void eraseFromParent();
237 /// dropAllReferences - Remove all uses and clear node vector.
238 void dropAllReferences();
240 /// ~NamedMDNode - Destroy NamedMDNode.
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; }
248 /// getElement - Return specified element.
249 MetadataBase *getElement(unsigned i) const {
250 assert (getNumElements() > i && "Invalid element number!");
254 /// getNumElements - Return number of NamedMDNode elements.
255 unsigned getNumElements() const {
259 /// addElement - Add metadata element.
260 void addElement(MetadataBase *M) {
262 OperandList[NumOperands++] = M;
263 Node.push_back(WeakMetadataVH(M));
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(); }
273 /// getType() specialization - Type is always MetadataTy.
275 inline const Type *getType() const {
276 return Type::MetadataTy;
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 {
286 /// Profile - calculate a unique identifier for this MDNode to collapse
288 void Profile(FoldingSetNodeID &ID) const;
290 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
292 "This should never be called because NamedMDNodes have no ops");
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;
302 } // end llvm namespace