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/Value.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 Value {
36 MetadataBase(const Type *Ty, unsigned scid)
40 /// getType() specialization - Type is always MetadataTy.
42 inline const Type *getType() const {
43 return Type::MetadataTy;
46 /// isNullValue - Return true if this is the value that would be returned by
47 /// getNullValue. This always returns false because getNullValue will never
49 virtual bool isNullValue() const {
53 /// Methods for support type inquiry through isa, cast, and dyn_cast:
54 static inline bool classof(const MetadataBase *) { return true; }
55 static bool classof(const Value *V) {
56 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
57 || V->getValueID() == NamedMDNodeVal;
61 //===----------------------------------------------------------------------===//
62 /// MDString - a single uniqued string.
63 /// These are used to efficiently contain a byte sequence for metadata.
64 /// MDString is always unnamd.
65 class MDString : public MetadataBase {
66 MDString(const MDString &); // DO NOT IMPLEMENT
70 explicit MDString(const char *begin, unsigned l)
71 : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
74 static MDString *get(LLVMContext &Context, const StringRef &Str);
76 StringRef getString() const { return Str; }
78 unsigned length() const { return Str.size(); }
80 /// begin() - Pointer to the first byte of the string.
82 const char *begin() const { return Str.begin(); }
84 /// end() - Pointer to one byte past the end of the string.
86 const char *end() const { return Str.end(); }
88 /// Methods for support type inquiry through isa, cast, and dyn_cast:
89 static inline bool classof(const MDString *) { return true; }
90 static bool classof(const Value *V) {
91 return V->getValueID() == MDStringVal;
95 //===----------------------------------------------------------------------===//
96 /// MDNode - a tuple of other values.
97 /// These contain a list of the values that represent the metadata.
98 /// MDNode is always unnamed.
99 class MDNode : public MetadataBase, public FoldingSetNode {
100 MDNode(const MDNode &); // DO NOT IMPLEMENT
102 SmallVector<WeakVH, 4> Node;
103 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
106 explicit MDNode(Value*const* Vals, unsigned NumVals);
108 static MDNode *get(LLVMContext &Context,
109 Value* const* Vals, unsigned NumVals);
111 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
113 Value *getElement(unsigned i) const {
117 unsigned getNumElements() const {
121 bool elem_empty() const {
125 const_elem_iterator elem_begin() const {
129 const_elem_iterator elem_end() const {
133 /// getType() specialization - Type is always MetadataTy.
135 inline const Type *getType() const {
136 return Type::MetadataTy;
139 /// isNullValue - Return true if this is the value that would be returned by
140 /// getNullValue. This always returns false because getNullValue will never
141 /// produce metadata.
142 virtual bool isNullValue() const {
146 /// Profile - calculate a unique identifier for this MDNode to collapse
148 void Profile(FoldingSetNodeID &ID) const;
150 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
151 llvm_unreachable("This should never be called because MDNodes have no ops");
154 /// Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const MDNode *) { return true; }
156 static bool classof(const Value *V) {
157 return V->getValueID() == MDNodeVal;
161 //===----------------------------------------------------------------------===//
162 /// WeakMetadataVH - a weak value handle for metadata.
163 class WeakMetadataVH : public WeakVH {
165 WeakMetadataVH() : WeakVH() {}
166 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
167 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
169 operator Value*() const {
170 llvm_unreachable("WeakMetadataVH only handles Metadata");
173 operator MetadataBase*() const {
174 return dyn_cast_or_null<MetadataBase>(getValPtr());
178 //===----------------------------------------------------------------------===//
179 /// NamedMDNode - a tuple of other metadata.
180 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
181 template<typename ValueSubClass, typename ItemParentClass>
182 class SymbolTableListTraits;
184 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
185 friend class SymbolTableListTraits<NamedMDNode, Module>;
186 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
188 friend class LLVMContextImpl;
191 SmallVector<WeakMetadataVH, 4> Node;
192 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
195 explicit NamedMDNode(const Twine &N, MetadataBase*const* Vals,
196 unsigned NumVals, Module *M = 0);
198 static NamedMDNode *Create(const Twine &N, MetadataBase*const*MDs,
199 unsigned NumMDs, Module *M = 0) {
200 return new NamedMDNode(N, MDs, NumMDs, M);
203 /// eraseFromParent - Drop all references and remove the node from parent
205 void eraseFromParent();
207 /// dropAllReferences - Remove all uses and clear node vector.
208 void dropAllReferences();
212 typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
214 /// getParent - Get the module that holds this named metadata collection.
215 inline Module *getParent() { return Parent; }
216 inline const Module *getParent() const { return Parent; }
217 void setParent(Module *M) { Parent = M; }
219 MetadataBase *getElement(unsigned i) const {
223 unsigned getNumElements() const {
227 /// addElement - Add metadata element.
228 void addElement(MetadataBase *M) {
229 Node.push_back(WeakMetadataVH(M));
232 bool elem_empty() const {
236 const_elem_iterator elem_begin() const {
240 const_elem_iterator elem_end() const {
244 /// getType() specialization - Type is always MetadataTy.
246 inline const Type *getType() const {
247 return Type::MetadataTy;
250 /// isNullValue - Return true if this is the value that would be returned by
251 /// getNullValue. This always returns false because getNullValue will never
252 /// produce metadata.
253 virtual bool isNullValue() const {
257 /// Profile - calculate a unique identifier for this MDNode to collapse
259 void Profile(FoldingSetNodeID &ID) const;
261 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
263 "This should never be called because NamedMDNodes have no ops");
266 /// Methods for support type inquiry through isa, cast, and dyn_cast:
267 static inline bool classof(const NamedMDNode *) { return true; }
268 static bool classof(const Value *V) {
269 return V->getValueID() == NamedMDNodeVal;
273 } // end llvm namespace