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"
31 template<class ConstantClass, class TypeClass, class ValType>
32 struct ConstantCreator;
34 //===----------------------------------------------------------------------===//
35 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
36 class MetadataBase : public User {
38 /// ReservedSpace - The number of operands actually allocated. NumOperands is
39 /// the number actually in use.
40 unsigned ReservedSpace;
43 MetadataBase(const Type *Ty, unsigned scid)
44 : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
46 void resizeOperands(unsigned NumOps);
48 /// getType() specialization - Type is always MetadataTy.
50 inline const Type *getType() const {
51 return Type::MetadataTy;
54 /// isNullValue - Return true if this is the value that would be returned by
55 /// getNullValue. This always returns false because getNullValue will never
57 virtual bool isNullValue() const {
61 /// Methods for support type inquiry through isa, cast, and dyn_cast:
62 static inline bool classof(const MetadataBase *) { return true; }
63 static bool classof(const Value *V) {
64 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
65 || V->getValueID() == NamedMDNodeVal;
69 //===----------------------------------------------------------------------===//
70 /// MDString - a single uniqued string.
71 /// These are used to efficiently contain a byte sequence for metadata.
72 /// MDString is always unnamd.
73 class MDString : public MetadataBase {
74 MDString(const MDString &); // DO NOT IMPLEMENT
75 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
76 unsigned getNumOperands(); // DO NOT IMPLEMENT
80 explicit MDString(const char *begin, unsigned l)
81 : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
84 // Do not allocate any space for operands.
85 void *operator new(size_t s) {
86 return User::operator new(s, 0);
88 static MDString *get(LLVMContext &Context, const StringRef &Str);
90 StringRef getString() const { return Str; }
92 unsigned length() const { return Str.size(); }
94 /// begin() - Pointer to the first byte of the string.
96 const char *begin() const { return Str.begin(); }
98 /// end() - Pointer to one byte past the end of the string.
100 const char *end() const { return Str.end(); }
102 /// Methods for support type inquiry through isa, cast, and dyn_cast:
103 static inline bool classof(const MDString *) { return true; }
104 static bool classof(const Value *V) {
105 return V->getValueID() == MDStringVal;
109 //===----------------------------------------------------------------------===//
110 /// MDNode - a tuple of other values.
111 /// These contain a list of the values that represent the metadata.
112 /// MDNode is always unnamed.
113 class MDNode : public MetadataBase, public FoldingSetNode {
114 MDNode(const MDNode &); // DO NOT IMPLEMENT
115 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
116 // getNumOperands - Make this only available for private uses.
117 unsigned getNumOperands() { return User::getNumOperands(); }
119 SmallVector<WeakVH, 4> Node;
121 friend struct ConstantCreator<MDNode, Type, std::vector<Value*> >;
123 explicit MDNode(Value*const* Vals, unsigned NumVals);
125 // Do not allocate any space for operands.
126 void *operator new(size_t s) {
127 return User::operator new(s, 0);
129 // Constructors and destructors.
130 static MDNode *get(LLVMContext &Context,
131 Value* const* Vals, unsigned NumVals);
133 /// dropAllReferences - Remove all uses and clear node vector.
134 void dropAllReferences();
136 /// ~MDNode - Destroy NamedMDNode.
139 /// getElement - Return specified element.
140 Value *getElement(unsigned i) const {
141 assert (getNumElements() > i && "Invalid element number!");
145 /// getNumElements - Return number of MDNode elements.
146 unsigned getNumElements() const {
151 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
152 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
153 /// elem_empty - Return true if MDNode is empty.
154 bool elem_empty() const { return Node.empty(); }
155 const_elem_iterator elem_begin() const { return Node.begin(); }
156 const_elem_iterator elem_end() const { return Node.end(); }
157 elem_iterator elem_begin() { return Node.begin(); }
158 elem_iterator elem_end() { return Node.end(); }
160 /// getType() specialization - Type is always MetadataTy.
162 inline const Type *getType() const {
163 return Type::MetadataTy;
166 /// isNullValue - Return true if this is the value that would be returned by
167 /// getNullValue. This always returns false because getNullValue will never
168 /// produce metadata.
169 virtual bool isNullValue() const {
173 /// Profile - calculate a unique identifier for this MDNode to collapse
175 void Profile(FoldingSetNodeID &ID) const;
177 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
178 llvm_unreachable("This should never be called because MDNodes have no ops");
181 /// Methods for support type inquiry through isa, cast, and dyn_cast:
182 static inline bool classof(const MDNode *) { return true; }
183 static bool classof(const Value *V) {
184 return V->getValueID() == MDNodeVal;
188 //===----------------------------------------------------------------------===//
189 /// WeakMetadataVH - a weak value handle for metadata.
190 class WeakMetadataVH : public WeakVH {
192 WeakMetadataVH() : WeakVH() {}
193 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
194 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
196 operator Value*() const {
197 llvm_unreachable("WeakMetadataVH only handles Metadata");
200 operator MetadataBase*() const {
201 return dyn_cast_or_null<MetadataBase>(getValPtr());
205 //===----------------------------------------------------------------------===//
206 /// NamedMDNode - a tuple of other metadata.
207 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
208 template<typename ValueSubClass, typename ItemParentClass>
209 class SymbolTableListTraits;
211 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
212 friend class SymbolTableListTraits<NamedMDNode, Module>;
213 friend struct LLVMContextImpl;
215 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
216 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
217 // getNumOperands - Make this only available for private uses.
218 unsigned getNumOperands() { return User::getNumOperands(); }
221 SmallVector<WeakMetadataVH, 4> Node;
222 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
225 explicit NamedMDNode(const Twine &N, MetadataBase*const* Vals,
226 unsigned NumVals, Module *M = 0);
228 // Do not allocate any space for operands.
229 void *operator new(size_t s) {
230 return User::operator new(s, 0);
232 static NamedMDNode *Create(const Twine &N, MetadataBase*const*MDs,
233 unsigned NumMDs, Module *M = 0) {
234 return new NamedMDNode(N, MDs, NumMDs, M);
237 /// eraseFromParent - Drop all references and remove the node from parent
239 void eraseFromParent();
241 /// dropAllReferences - Remove all uses and clear node vector.
242 void dropAllReferences();
244 /// ~NamedMDNode - Destroy NamedMDNode.
247 /// getParent - Get the module that holds this named metadata collection.
248 inline Module *getParent() { return Parent; }
249 inline const Module *getParent() const { return Parent; }
250 void setParent(Module *M) { Parent = M; }
252 /// getElement - Return specified element.
253 MetadataBase *getElement(unsigned i) const {
254 assert (getNumElements() > i && "Invalid element number!");
258 /// getNumElements - Return number of NamedMDNode elements.
259 unsigned getNumElements() const {
263 /// addElement - Add metadata element.
264 void addElement(MetadataBase *M) {
266 OperandList[NumOperands++] = M;
267 Node.push_back(WeakMetadataVH(M));
270 typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
271 bool elem_empty() const { return Node.empty(); }
272 const_elem_iterator elem_begin() const { return Node.begin(); }
273 const_elem_iterator elem_end() const { return Node.end(); }
274 elem_iterator elem_begin() { return Node.begin(); }
275 elem_iterator elem_end() { return Node.end(); }
277 /// getType() specialization - Type is always MetadataTy.
279 inline const Type *getType() const {
280 return Type::MetadataTy;
283 /// isNullValue - Return true if this is the value that would be returned by
284 /// getNullValue. This always returns false because getNullValue will never
285 /// produce metadata.
286 virtual bool isNullValue() const {
290 /// Profile - calculate a unique identifier for this MDNode to collapse
292 void Profile(FoldingSetNodeID &ID) const;
294 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
296 "This should never be called because NamedMDNodes have no ops");
299 /// Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const NamedMDNode *) { return true; }
301 static bool classof(const Value *V) {
302 return V->getValueID() == NamedMDNodeVal;
306 } // end llvm namespace