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 /// getType() specialization - Type is always MetadataTy.
49 inline const Type *getType() const {
50 return Type::MetadataTy;
53 /// isNullValue - Return true if this is the value that would be returned by
54 /// getNullValue. This always returns false because getNullValue will never
56 virtual bool isNullValue() const {
60 /// Methods for support type inquiry through isa, cast, and dyn_cast:
61 static inline bool classof(const MetadataBase *) { return true; }
62 static bool classof(const Value *V) {
63 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
64 || V->getValueID() == NamedMDNodeVal;
68 //===----------------------------------------------------------------------===//
69 /// MDString - a single uniqued string.
70 /// These are used to efficiently contain a byte sequence for metadata.
71 /// MDString is always unnamd.
72 class MDString : public MetadataBase {
73 MDString(const MDString &); // DO NOT IMPLEMENT
74 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
75 unsigned getNumOperands(); // DO NOT IMPLEMENT
79 explicit MDString(const char *begin, unsigned l)
80 : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
83 // Do not allocate any space for operands.
84 void *operator new(size_t s) {
85 return User::operator new(s, 0);
87 static MDString *get(LLVMContext &Context, const StringRef &Str);
89 StringRef getString() const { return Str; }
91 unsigned length() const { return Str.size(); }
93 /// begin() - Pointer to the first byte of the string.
95 const char *begin() const { return Str.begin(); }
97 /// end() - Pointer to one byte past the end of the string.
99 const char *end() const { return Str.end(); }
101 /// Methods for support type inquiry through isa, cast, and dyn_cast:
102 static inline bool classof(const MDString *) { return true; }
103 static bool classof(const Value *V) {
104 return V->getValueID() == MDStringVal;
108 //===----------------------------------------------------------------------===//
109 /// MDNode - a tuple of other values.
110 /// These contain a list of the values that represent the metadata.
111 /// MDNode is always unnamed.
112 class MDNode : public MetadataBase {
113 MDNode(const MDNode &); // DO NOT IMPLEMENT
114 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
115 // getNumOperands - Make this only available for private uses.
116 unsigned getNumOperands() { return User::getNumOperands(); }
118 SmallVector<WeakVH, 4> Node;
120 friend struct ConstantCreator<MDNode, Type, std::vector<Value*> >;
122 explicit MDNode(Value*const* Vals, unsigned NumVals);
124 // Do not allocate any space for operands.
125 void *operator new(size_t s) {
126 return User::operator new(s, 0);
128 // Constructors and destructors.
129 static MDNode *get(LLVMContext &Context,
130 Value* const* Vals, unsigned NumVals);
132 /// dropAllReferences - Remove all uses and clear node vector.
133 void dropAllReferences();
135 /// ~MDNode - Destroy MDNode.
138 /// getElement - Return specified element.
139 Value *getElement(unsigned i) const {
140 assert (getNumElements() > i && "Invalid element number!");
144 /// getNumElements - Return number of MDNode elements.
145 unsigned getNumElements() const {
150 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
151 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
152 /// elem_empty - Return true if MDNode is empty.
153 bool elem_empty() const { return Node.empty(); }
154 const_elem_iterator elem_begin() const { return Node.begin(); }
155 const_elem_iterator elem_end() const { return Node.end(); }
156 elem_iterator elem_begin() { return Node.begin(); }
157 elem_iterator elem_end() { return Node.end(); }
159 /// getType() specialization - Type is always MetadataTy.
161 inline const Type *getType() const {
162 return Type::MetadataTy;
165 /// isNullValue - Return true if this is the value that would be returned by
166 /// getNullValue. This always returns false because getNullValue will never
167 /// produce metadata.
168 virtual bool isNullValue() const {
172 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
173 llvm_unreachable("This should never be called because MDNodes have no ops");
176 /// Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const MDNode *) { return true; }
178 static bool classof(const Value *V) {
179 return V->getValueID() == MDNodeVal;
183 //===----------------------------------------------------------------------===//
184 /// WeakMetadataVH - a weak value handle for metadata.
185 class WeakMetadataVH : public WeakVH {
187 WeakMetadataVH() : WeakVH() {}
188 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
189 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
191 operator Value*() const {
192 llvm_unreachable("WeakMetadataVH only handles Metadata");
195 operator MetadataBase*() const {
196 return dyn_cast_or_null<MetadataBase>(getValPtr());
200 //===----------------------------------------------------------------------===//
201 /// NamedMDNode - a tuple of other metadata.
202 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
203 template<typename ValueSubClass, typename ItemParentClass>
204 class SymbolTableListTraits;
206 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
207 friend class SymbolTableListTraits<NamedMDNode, Module>;
208 friend class LLVMContextImpl;
210 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
211 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
212 // getNumOperands - Make this only available for private uses.
213 unsigned getNumOperands() { return User::getNumOperands(); }
216 SmallVector<WeakMetadataVH, 4> Node;
217 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
220 explicit NamedMDNode(const Twine &N, MetadataBase*const* Vals,
221 unsigned NumVals, Module *M = 0);
223 // Do not allocate any space for operands.
224 void *operator new(size_t s) {
225 return User::operator new(s, 0);
227 static NamedMDNode *Create(const Twine &N, MetadataBase*const*MDs,
228 unsigned NumMDs, Module *M = 0) {
229 return new NamedMDNode(N, MDs, NumMDs, M);
232 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
234 /// eraseFromParent - Drop all references and remove the node from parent
236 void eraseFromParent();
238 /// dropAllReferences - Remove all uses and clear node vector.
239 void dropAllReferences();
241 /// ~NamedMDNode - Destroy NamedMDNode.
244 /// getParent - Get the module that holds this named metadata collection.
245 inline Module *getParent() { return Parent; }
246 inline const Module *getParent() const { return Parent; }
247 void setParent(Module *M) { Parent = M; }
249 /// getElement - Return specified element.
250 MetadataBase *getElement(unsigned i) const {
251 assert (getNumElements() > i && "Invalid element number!");
255 /// getNumElements - Return number of NamedMDNode elements.
256 unsigned getNumElements() const {
260 /// addElement - Add metadata element.
261 void addElement(MetadataBase *M) {
263 OperandList[NumOperands++] = M;
264 Node.push_back(WeakMetadataVH(M));
267 typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
268 bool elem_empty() const { return Node.empty(); }
269 const_elem_iterator elem_begin() const { return Node.begin(); }
270 const_elem_iterator elem_end() const { return Node.end(); }
271 elem_iterator elem_begin() { return Node.begin(); }
272 elem_iterator elem_end() { return Node.end(); }
274 /// getType() specialization - Type is always MetadataTy.
276 inline const Type *getType() const {
277 return Type::MetadataTy;
280 /// isNullValue - Return true if this is the value that would be returned by
281 /// getNullValue. This always returns false because getNullValue will never
282 /// produce metadata.
283 virtual bool isNullValue() const {
287 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
289 "This should never be called because NamedMDNodes have no ops");
292 /// Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const NamedMDNode *) { return true; }
294 static bool classof(const Value *V) {
295 return V->getValueID() == NamedMDNodeVal;
299 } // end llvm namespace