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 /// MDNode - a tuple of other values.
35 /// These contain a list of the Constants that represent the metadata. The
36 /// operand list is always empty, query the element list instead.
38 /// This class will attempt to keep track of values as they are modified. When
39 /// a value is replaced the element will be replaced with it, and when the
40 /// value is deleted the element is set to a null pointer. In order to preserve
41 /// structural equivalence while the elements mutate, the MDNode may call
42 /// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
43 /// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
44 /// of the elements held by the node may change.
46 class MDNode : public Constant, public FoldingSetNode {
47 MDNode(const MDNode &); // DO NOT IMPLEMENT
49 friend class LLVMContextImpl;
51 friend class ElementVH;
52 struct ElementVH : public CallbackVH {
55 ElementVH(Value *V, MDNode *Parent)
56 : CallbackVH(V), OwningNode(Parent) {}
60 /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
62 virtual void deleted() {
63 OwningNode->replaceElement(this->operator Value*(), 0);
66 /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
67 /// new_value. This will reallocate the MDNode.
68 virtual void allUsesReplacedWith(Value *new_value) {
69 OwningNode->replaceElement(this->operator Value*(), new_value);
73 void replaceElement(Value *From, Value *To);
75 SmallVector<ElementVH, 4> Node;
76 typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
79 explicit MDNode(Value*const* Vals, unsigned NumVals);
81 typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
83 Value *getElement(unsigned i) const {
87 unsigned getNumElements() const {
91 bool elem_empty() const {
95 const_elem_iterator elem_begin() const {
99 const_elem_iterator elem_end() const {
103 /// getType() specialization - Type is always MetadataTy.
105 inline const Type *getType() const {
106 return Type::MetadataTy;
109 /// isNullValue - Return true if this is the value that would be returned by
110 /// getNullValue. This always returns false because getNullValue will never
111 /// produce metadata.
112 virtual bool isNullValue() const {
116 /// Profile - calculate a unique identifier for this MDNode to collapse
118 void Profile(FoldingSetNodeID &ID) const;
120 virtual void destroyConstant();
121 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
122 llvm_unreachable("This should never be called because MDNodes have no ops");
125 /// Methods for support type inquiry through isa, cast, and dyn_cast:
126 static inline bool classof(const MDNode *) { return true; }
127 static bool classof(const Value *V) {
128 return V->getValueID() == MDNodeVal;
132 } // end llvm namespace