d3af74a804f998542d194ef18296fb075deab8bd
[oota-llvm.git] / include / llvm / MDNode.h
1 //===-- llvm/Metadata.h - Constant class subclass definitions ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
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.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_MDNODE_H
22 #define LLVM_MDNODE_H
23
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"
30
31 namespace llvm {
32
33 //===----------------------------------------------------------------------===//
34 // MetadataBase  - A base class for MDNode and MDString.
35 class MetadataBase : public Value {
36 public:
37   MetadataBase(const Type *Ty, unsigned scid)
38     : Value(Ty, scid) {}
39
40   /// getType() specialization - Type is always MetadataTy.
41   ///
42   inline const Type *getType() const {
43     return Type::MetadataTy;
44   }
45
46   /// isNullValue - Return true if this is the value that would be returned by
47   /// getNullValue.  This always returns false because getNullValue will never
48   /// produce metadata.
49   virtual bool isNullValue() const {
50     return false;
51   }
52
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;
57   }
58 };
59
60 //===----------------------------------------------------------------------===//
61 /// MDString - a single uniqued string.
62 /// These are used to efficiently contain a byte sequence for metadata.
63 ///
64 class MDString : public MetadataBase {
65   MDString(const MDString &);            // DO NOT IMPLEMENT
66
67   const char *StrBegin, *StrEnd;
68   friend class LLVMContextImpl;
69
70 public:
71   MDString(const char *begin, const char *end)
72     : MetadataBase(Type::MetadataTy, Value::MDStringVal),
73       StrBegin(begin), StrEnd(end) {}
74
75   intptr_t size() const { return StrEnd - StrBegin; }
76
77   /// begin() - Pointer to the first byte of the string.
78   ///
79   const char *begin() const { return StrBegin; }
80
81   /// end() - Pointer to one byte past the end of the string.
82   ///
83   const char *end() const { return StrEnd; }
84
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;
89   }
90 };
91
92 //===----------------------------------------------------------------------===//
93 /// MDNode - a tuple of other values.
94 /// These contain a list of the Constants that represent the metadata. The
95 /// operand list is always empty, query the element list instead.
96 ///
97 /// This class will attempt to keep track of values as they are modified. When
98 /// a value is replaced the element will be replaced with it, and when the
99 /// value is deleted the element is set to a null pointer. In order to preserve
100 /// structural equivalence while the elements mutate, the MDNode may call
101 /// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
102 /// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
103 /// of the elements held by the node may change.
104 ///
105 class MDNode : public Constant, public FoldingSetNode {
106   MDNode(const MDNode &);      // DO NOT IMPLEMENT
107
108   friend class LLVMContextImpl;
109
110   friend class ElementVH;
111   struct ElementVH : public CallbackVH {
112     MDNode *OwningNode;
113
114     ElementVH(Value *V, MDNode *Parent)
115       : CallbackVH(V), OwningNode(Parent) {}
116
117     ~ElementVH() {}
118
119     /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
120     /// the MDNode.
121     virtual void deleted() {
122       OwningNode->replaceElement(this->operator Value*(), 0);
123     }
124
125     /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
126     /// new_value. This will reallocate the MDNode.
127     virtual void allUsesReplacedWith(Value *new_value) {
128       OwningNode->replaceElement(this->operator Value*(), new_value);
129     }
130   };
131
132   void replaceElement(Value *From, Value *To);
133
134   SmallVector<ElementVH, 4> Node;
135   typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
136
137 protected:
138   explicit MDNode(Value*const* Vals, unsigned NumVals);
139 public:
140   typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
141
142   Value *getElement(unsigned i) const {
143     return Node[i];
144   }
145
146   unsigned getNumElements() const {
147     return Node.size();
148   }
149
150   bool elem_empty() const {
151     return Node.empty();
152   }
153
154   const_elem_iterator elem_begin() const {
155     return Node.begin();
156   }
157
158   const_elem_iterator elem_end() const {
159     return Node.end();
160   }
161
162   /// getType() specialization - Type is always MetadataTy.
163   ///
164   inline const Type *getType() const {
165     return Type::MetadataTy;
166   }
167
168   /// isNullValue - Return true if this is the value that would be returned by
169   /// getNullValue.  This always returns false because getNullValue will never
170   /// produce metadata.
171   virtual bool isNullValue() const {
172     return false;
173   }
174
175   /// Profile - calculate a unique identifier for this MDNode to collapse
176   /// duplicates
177   void Profile(FoldingSetNodeID &ID) const;
178
179   virtual void destroyConstant();
180   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
181     llvm_unreachable("This should never be called because MDNodes have no ops");
182   }
183
184   /// Methods for support type inquiry through isa, cast, and dyn_cast:
185   static inline bool classof(const MDNode *) { return true; }
186   static bool classof(const Value *V) {
187     return V->getValueID() == MDNodeVal;
188   }
189 };
190
191 } // end llvm namespace
192
193 #endif