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/Constant.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ValueHandle.h"
28 //===----------------------------------------------------------------------===//
29 // MetadataBase - A base class for MDNode and MDString.
30 class MetadataBase : public Value {
32 MetadataBase(const Type *Ty, unsigned scid)
36 /// getType() specialization - Type is always MetadataTy.
38 inline const Type *getType() const {
39 return Type::MetadataTy;
42 /// isNullValue - Return true if this is the value that would be returned by
43 /// getNullValue. This always returns false because getNullValue will never
45 virtual bool isNullValue() const {
49 /// Methods for support type inquiry through isa, cast, and dyn_cast:
50 static inline bool classof(const MDString *) { return true; }
51 static bool classof(const Value *V) {
52 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
56 //===----------------------------------------------------------------------===//
57 /// MDString - a single uniqued string.
58 /// These are used to efficiently contain a byte sequence for metadata.
60 class MDString : public MetadataBase {
61 MDString(const MDString &); // DO NOT IMPLEMENT
63 friend class LLVMContextImpl;
66 explicit MDString(const char *begin, unsigned l)
67 : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
70 StringRef getString() const { return Str; }
72 unsigned length() const { return Str.size(); }
74 /// begin() - Pointer to the first byte of the string.
76 const char *begin() const { return Str.begin(); }
78 /// end() - Pointer to one byte past the end of the string.
80 const char *end() const { return Str.end(); }
82 /// Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const MDString *) { return true; }
84 static bool classof(const Value *V) {
85 return V->getValueID() == MDStringVal;
89 //===----------------------------------------------------------------------===//
90 /// MDNode - a tuple of other values.
91 /// These contain a list of the values that represent the metadata.
93 class MDNode : public MetadataBase, public FoldingSetNode {
94 MDNode(const MDNode &); // DO NOT IMPLEMENT
96 friend class LLVMContextImpl;
98 SmallVector<WeakVH, 4> Node;
99 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
102 explicit MDNode(Value*const* Vals, unsigned NumVals);
104 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
106 Value *getElement(unsigned i) const {
110 unsigned getNumElements() const {
114 bool elem_empty() const {
118 const_elem_iterator elem_begin() const {
122 const_elem_iterator elem_end() const {
126 /// getType() specialization - Type is always MetadataTy.
128 inline const Type *getType() const {
129 return Type::MetadataTy;
132 /// isNullValue - Return true if this is the value that would be returned by
133 /// getNullValue. This always returns false because getNullValue will never
134 /// produce metadata.
135 virtual bool isNullValue() const {
139 /// Profile - calculate a unique identifier for this MDNode to collapse
141 void Profile(FoldingSetNodeID &ID) const;
143 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
144 llvm_unreachable("This should never be called because MDNodes have no ops");
147 /// Methods for support type inquiry through isa, cast, and dyn_cast:
148 static inline bool classof(const MDNode *) { return true; }
149 static bool classof(const Value *V) {
150 return V->getValueID() == MDNodeVal;
154 } // end llvm namespace