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/SmallVector.h"
23 #include "llvm/ADT/ilist_node.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/ValueHandle.h"
30 template<class ConstantClass, class TypeClass, class ValType>
31 struct ConstantCreator;
33 //===----------------------------------------------------------------------===//
34 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
35 class MetadataBase : public User {
37 /// ReservedSpace - The number of operands actually allocated. NumOperands is
38 /// the number actually in use.
39 unsigned ReservedSpace;
42 MetadataBase(const Type *Ty, unsigned scid)
43 : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
45 void resizeOperands(unsigned NumOps);
47 /// isNullValue - Return true if this is the value that would be returned by
48 /// getNullValue. This always returns false because getNullValue will never
50 virtual bool isNullValue() const {
54 /// Methods for support type inquiry through isa, cast, and dyn_cast:
55 static inline bool classof(const MetadataBase *) { return true; }
56 static bool classof(const Value *V) {
57 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
58 || V->getValueID() == NamedMDNodeVal;
62 //===----------------------------------------------------------------------===//
63 /// MDString - a single uniqued string.
64 /// These are used to efficiently contain a byte sequence for metadata.
65 /// MDString is always unnamd.
66 class MDString : public MetadataBase {
67 MDString(const MDString &); // DO NOT IMPLEMENT
68 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
69 unsigned getNumOperands(); // DO NOT IMPLEMENT
73 explicit MDString(LLVMContext &C, const char *begin, unsigned l)
74 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
77 // Do not allocate any space for operands.
78 void *operator new(size_t s) {
79 return User::operator new(s, 0);
81 static MDString *get(LLVMContext &Context, const StringRef &Str);
83 StringRef getString() const { return Str; }
85 unsigned length() const { return Str.size(); }
87 /// begin() - Pointer to the first byte of the string.
89 const char *begin() const { return Str.begin(); }
91 /// end() - Pointer to one byte past the end of the string.
93 const char *end() const { return Str.end(); }
95 /// Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const MDString *) { return true; }
97 static bool classof(const Value *V) {
98 return V->getValueID() == MDStringVal;
102 //===----------------------------------------------------------------------===//
103 /// MDNode - a tuple of other values.
104 /// These contain a list of the values that represent the metadata.
105 /// MDNode is always unnamed.
106 class MDNode : public MetadataBase {
107 MDNode(const MDNode &); // DO NOT IMPLEMENT
108 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
109 // getNumOperands - Make this only available for private uses.
110 unsigned getNumOperands() { return User::getNumOperands(); }
112 SmallVector<WeakVH, 4> Node;
113 friend struct ConstantCreator<MDNode, Type, std::vector<Value*> >;
115 explicit MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals);
117 // Do not allocate any space for operands.
118 void *operator new(size_t s) {
119 return User::operator new(s, 0);
121 // Constructors and destructors.
122 static MDNode *get(LLVMContext &Context,
123 Value* const* Vals, unsigned NumVals);
125 /// dropAllReferences - Remove all uses and clear node vector.
126 void dropAllReferences();
128 /// ~MDNode - Destroy MDNode.
131 /// getElement - Return specified element.
132 Value *getElement(unsigned i) const {
133 assert (getNumElements() > i && "Invalid element number!");
137 /// getNumElements - Return number of MDNode elements.
138 unsigned getNumElements() const {
143 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
144 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
145 /// elem_empty - Return true if MDNode is empty.
146 bool elem_empty() const { return Node.empty(); }
147 const_elem_iterator elem_begin() const { return Node.begin(); }
148 const_elem_iterator elem_end() const { return Node.end(); }
149 elem_iterator elem_begin() { return Node.begin(); }
150 elem_iterator elem_end() { return Node.end(); }
152 /// isNullValue - Return true if this is the value that would be returned by
153 /// getNullValue. This always returns false because getNullValue will never
154 /// produce metadata.
155 virtual bool isNullValue() const {
159 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
160 llvm_unreachable("This should never be called because MDNodes have no ops");
163 /// Methods for support type inquiry through isa, cast, and dyn_cast:
164 static inline bool classof(const MDNode *) { return true; }
165 static bool classof(const Value *V) {
166 return V->getValueID() == MDNodeVal;
170 //===----------------------------------------------------------------------===//
171 /// WeakMetadataVH - a weak value handle for metadata.
172 class WeakMetadataVH : public WeakVH {
174 WeakMetadataVH() : WeakVH() {}
175 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
176 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
178 operator Value*() const {
179 llvm_unreachable("WeakMetadataVH only handles Metadata");
182 operator MetadataBase*() const {
183 return dyn_cast_or_null<MetadataBase>(getValPtr());
187 //===----------------------------------------------------------------------===//
188 /// NamedMDNode - a tuple of other metadata.
189 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
190 template<typename ValueSubClass, typename ItemParentClass>
191 class SymbolTableListTraits;
193 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
194 friend class SymbolTableListTraits<NamedMDNode, Module>;
195 friend class LLVMContextImpl;
197 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
198 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
199 // getNumOperands - Make this only available for private uses.
200 unsigned getNumOperands() { return User::getNumOperands(); }
203 SmallVector<WeakMetadataVH, 4> Node;
204 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
207 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const* Vals,
208 unsigned NumVals, Module *M = 0);
210 // Do not allocate any space for operands.
211 void *operator new(size_t s) {
212 return User::operator new(s, 0);
214 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
215 MetadataBase*const*MDs,
216 unsigned NumMDs, Module *M = 0) {
217 return new NamedMDNode(C, N, MDs, NumMDs, M);
220 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
222 /// eraseFromParent - Drop all references and remove the node from parent
224 void eraseFromParent();
226 /// dropAllReferences - Remove all uses and clear node vector.
227 void dropAllReferences();
229 /// ~NamedMDNode - Destroy NamedMDNode.
232 /// getParent - Get the module that holds this named metadata collection.
233 inline Module *getParent() { return Parent; }
234 inline const Module *getParent() const { return Parent; }
235 void setParent(Module *M) { Parent = M; }
237 /// getElement - Return specified element.
238 MetadataBase *getElement(unsigned i) const {
239 assert (getNumElements() > i && "Invalid element number!");
243 /// getNumElements - Return number of NamedMDNode elements.
244 unsigned getNumElements() const {
248 /// addElement - Add metadata element.
249 void addElement(MetadataBase *M) {
251 OperandList[NumOperands++] = M;
252 Node.push_back(WeakMetadataVH(M));
255 typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
256 bool elem_empty() const { return Node.empty(); }
257 const_elem_iterator elem_begin() const { return Node.begin(); }
258 const_elem_iterator elem_end() const { return Node.end(); }
259 elem_iterator elem_begin() { return Node.begin(); }
260 elem_iterator elem_end() { return Node.end(); }
262 /// isNullValue - Return true if this is the value that would be returned by
263 /// getNullValue. This always returns false because getNullValue will never
264 /// produce metadata.
265 virtual bool isNullValue() const {
269 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
271 "This should never be called because NamedMDNodes have no ops");
274 /// Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const NamedMDNode *) { return true; }
276 static bool classof(const Value *V) {
277 return V->getValueID() == NamedMDNodeVal;
281 } // end llvm namespace