1 //===-- llvm/Metadata.h - Constant class subclass 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 the subclasses of Constant,
12 /// which represent the different flavors of constant values that live in LLVM.
13 /// Note that Constants are immutable (once created they never change) and are
14 /// fully shared by structural equivalence. This means that two structurally
15 /// equivalent constants will always have the same address. Constant's are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
19 //===----------------------------------------------------------------------===//
24 #include "llvm/Constant.h"
25 #include "llvm/Type.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/ValueHandle.h"
33 //===----------------------------------------------------------------------===//
34 // MetadataBase - A base class for MDNode and MDString.
35 class MetadataBase : public Value {
37 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 MDString *) { return true; }
55 static bool classof(const Value *V) {
56 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
60 //===----------------------------------------------------------------------===//
61 /// MDString - a single uniqued string.
62 /// These are used to efficiently contain a byte sequence for metadata.
64 class MDString : public MetadataBase {
65 MDString(const MDString &); // DO NOT IMPLEMENT
67 const char *StrBegin, *StrEnd;
68 friend class LLVMContextImpl;
71 MDString(const char *begin, const char *end)
72 : MetadataBase(Type::MetadataTy, Value::MDStringVal),
73 StrBegin(begin), StrEnd(end) {}
75 intptr_t size() const { return StrEnd - StrBegin; }
77 /// begin() - Pointer to the first byte of the string.
79 const char *begin() const { return StrBegin; }
81 /// end() - Pointer to one byte past the end of the string.
83 const char *end() const { return StrEnd; }
85 /// Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const MDString *) { return true; }
87 static bool classof(const Value *V) {
88 return V->getValueID() == MDStringVal;
92 //===----------------------------------------------------------------------===//
93 /// MDNode - a tuple of other values.
94 /// These contain a list of the values that represent the metadata.
96 class MDNode : public MetadataBase, public FoldingSetNode {
97 MDNode(const MDNode &); // DO NOT IMPLEMENT
99 friend class LLVMContextImpl;
101 void replaceElement(Value *From, Value *To);
103 SmallVector<WeakVH, 4> Node;
104 typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
107 explicit MDNode(Value*const* Vals, unsigned NumVals);
109 typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
111 Value *getElement(unsigned i) const {
115 unsigned getNumElements() const {
119 bool elem_empty() const {
123 const_elem_iterator elem_begin() const {
127 const_elem_iterator elem_end() const {
131 /// getType() specialization - Type is always MetadataTy.
133 inline const Type *getType() const {
134 return Type::MetadataTy;
137 /// isNullValue - Return true if this is the value that would be returned by
138 /// getNullValue. This always returns false because getNullValue will never
139 /// produce metadata.
140 virtual bool isNullValue() const {
144 /// Profile - calculate a unique identifier for this MDNode to collapse
146 void Profile(FoldingSetNodeID &ID) const;
148 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
149 llvm_unreachable("This should never be called because MDNodes have no ops");
152 /// Methods for support type inquiry through isa, cast, and dyn_cast:
153 static inline bool classof(const MDNode *) { return true; }
154 static bool classof(const Value *V) {
155 return V->getValueID() == MDNodeVal;
159 } // end llvm namespace