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"
31 //===----------------------------------------------------------------------===//
32 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
33 class MetadataBase : public Value {
35 MetadataBase(const Type *Ty, unsigned scid)
39 /// getType() specialization - Type is always MetadataTy.
41 inline const Type *getType() const {
42 return Type::MetadataTy;
45 /// isNullValue - Return true if this is the value that would be returned by
46 /// getNullValue. This always returns false because getNullValue will never
48 virtual bool isNullValue() const {
52 /// Methods for support type inquiry through isa, cast, and dyn_cast:
53 static inline bool classof(const MDString *) { return true; }
54 static bool classof(const Value *V) {
55 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
56 || V->getValueID() == NamedMDNodeVal;
60 //===----------------------------------------------------------------------===//
61 /// MDString - a single uniqued string.
62 /// These are used to efficiently contain a byte sequence for metadata.
63 /// MDString is always unnamd.
64 class MDString : public MetadataBase {
65 MDString(const MDString &); // DO NOT IMPLEMENT
67 friend class LLVMContextImpl;
70 explicit MDString(const char *begin, unsigned l)
71 : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
74 StringRef getString() const { return Str; }
76 unsigned length() const { return Str.size(); }
78 /// begin() - Pointer to the first byte of the string.
80 const char *begin() const { return Str.begin(); }
82 /// end() - Pointer to one byte past the end of the string.
84 const char *end() const { return Str.end(); }
86 /// Methods for support type inquiry through isa, cast, and dyn_cast:
87 static inline bool classof(const MDString *) { return true; }
88 static bool classof(const Value *V) {
89 return V->getValueID() == MDStringVal;
93 //===----------------------------------------------------------------------===//
94 /// MDNode - a tuple of other values.
95 /// These contain a list of the values that represent the metadata.
96 /// MDNode is always unnamed.
97 class MDNode : public MetadataBase, public FoldingSetNode {
98 MDNode(const MDNode &); // DO NOT IMPLEMENT
100 friend class LLVMContextImpl;
102 SmallVector<WeakVH, 4> Node;
103 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
106 explicit MDNode(Value*const* Vals, unsigned NumVals);
108 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
110 Value *getElement(unsigned i) const {
114 unsigned getNumElements() const {
118 bool elem_empty() const {
122 const_elem_iterator elem_begin() const {
126 const_elem_iterator elem_end() const {
130 /// getType() specialization - Type is always MetadataTy.
132 inline const Type *getType() const {
133 return Type::MetadataTy;
136 /// isNullValue - Return true if this is the value that would be returned by
137 /// getNullValue. This always returns false because getNullValue will never
138 /// produce metadata.
139 virtual bool isNullValue() const {
143 /// Profile - calculate a unique identifier for this MDNode to collapse
145 void Profile(FoldingSetNodeID &ID) const;
147 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
148 llvm_unreachable("This should never be called because MDNodes have no ops");
151 /// Methods for support type inquiry through isa, cast, and dyn_cast:
152 static inline bool classof(const MDNode *) { return true; }
153 static bool classof(const Value *V) {
154 return V->getValueID() == MDNodeVal;
158 //===----------------------------------------------------------------------===//
159 /// WeakMetadataVH - a weak value handle for metadata.
160 class WeakMetadataVH : public WeakVH {
162 WeakMetadataVH() : WeakVH() {}
163 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
164 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
166 operator Value*() const {
167 llvm_unreachable("WeakMetadataVH only handles Metadata");
170 operator MetadataBase*() const {
171 return cast<MetadataBase>(getValPtr());
175 //===----------------------------------------------------------------------===//
176 /// NamedMDNode - a tuple of other metadata.
177 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
178 template<typename ValueSubClass, typename ItemParentClass>
179 class SymbolTableListTraits;
181 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
182 friend class SymbolTableListTraits<NamedMDNode, Module>;
183 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
185 friend class LLVMContextImpl;
188 SmallVector<WeakMetadataVH, 4> Node;
189 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
192 explicit NamedMDNode(const Twine &N, MetadataBase*const* Vals,
193 unsigned NumVals, Module *M = 0);
195 static NamedMDNode *Create(const Twine &N, MetadataBase*const*MDs,
196 unsigned NumMDs, Module *M = 0) {
197 return new NamedMDNode(N, MDs, NumMDs, M);
200 typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
202 /// getParent - Get the module that holds this named metadata collection.
203 inline Module *getParent() { return Parent; }
204 inline const Module *getParent() const { return Parent; }
205 void setParent(Module *M) { Parent = M; }
207 MetadataBase *getElement(unsigned i) const {
211 unsigned getNumElements() const {
215 bool elem_empty() const {
219 const_elem_iterator elem_begin() const {
223 const_elem_iterator elem_end() const {
227 /// getType() specialization - Type is always MetadataTy.
229 inline const Type *getType() const {
230 return Type::MetadataTy;
233 /// isNullValue - Return true if this is the value that would be returned by
234 /// getNullValue. This always returns false because getNullValue will never
235 /// produce metadata.
236 virtual bool isNullValue() const {
240 /// Profile - calculate a unique identifier for this MDNode to collapse
242 void Profile(FoldingSetNodeID &ID) const;
244 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
246 "This should never be called because NamedMDNodes have no ops");
249 /// Methods for support type inquiry through isa, cast, and dyn_cast:
250 static inline bool classof(const NamedMDNode *) { return true; }
251 static bool classof(const Value *V) {
252 return V->getValueID() == NamedMDNodeVal;
256 } // end llvm namespace