Move the metadata constructors back to 2.5 syntax.
[oota-llvm.git] / include / llvm / Metadata.h
1 //===-- llvm/Metadata.h - Metadata 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 metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_MDNODE_H
17 #define LLVM_MDNODE_H
18
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"
27
28 namespace llvm {
29 class Constant;
30 class LLVMContext;
31
32 //===----------------------------------------------------------------------===//
33 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
34 class MetadataBase : public Value {
35 protected:
36   MetadataBase(const Type *Ty, unsigned scid)
37     : Value(Ty, scid) {}
38
39 public:
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 MetadataBase *) { return true; }
55   static bool classof(const Value *V) {
56     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
57       || V->getValueID() == NamedMDNodeVal;
58   }
59 };
60
61 //===----------------------------------------------------------------------===//
62 /// MDString - a single uniqued string.
63 /// These are used to efficiently contain a byte sequence for metadata.
64 /// MDString is always unnamd.
65 class MDString : public MetadataBase {
66   MDString(const MDString &);            // DO NOT IMPLEMENT
67   StringRef Str;
68
69 protected:
70   explicit MDString(const char *begin, unsigned l)
71     : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
72
73 public:
74   static MDString *get(LLVMContext &Context, const StringRef &Str);
75   
76   StringRef getString() const { return Str; }
77
78   unsigned length() const { return Str.size(); }
79
80   /// begin() - Pointer to the first byte of the string.
81   ///
82   const char *begin() const { return Str.begin(); }
83
84   /// end() - Pointer to one byte past the end of the string.
85   ///
86   const char *end() const { return Str.end(); }
87
88   /// Methods for support type inquiry through isa, cast, and dyn_cast:
89   static inline bool classof(const MDString *) { return true; }
90   static bool classof(const Value *V) {
91     return V->getValueID() == MDStringVal;
92   }
93 };
94
95 //===----------------------------------------------------------------------===//
96 /// MDNode - a tuple of other values.
97 /// These contain a list of the values that represent the metadata. 
98 /// MDNode is always unnamed.
99 class MDNode : public MetadataBase, public FoldingSetNode {
100   MDNode(const MDNode &);      // DO NOT IMPLEMENT
101
102   SmallVector<WeakVH, 4> Node;
103   typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
104
105 protected:
106   explicit MDNode(Value*const* Vals, unsigned NumVals);
107 public:
108   static MDNode *get(LLVMContext &Context, 
109                      Value* const* Vals, unsigned NumVals);
110   
111   typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
112
113   Value *getElement(unsigned i) const {
114     return Node[i];
115   }
116
117   unsigned getNumElements() const {
118     return Node.size();
119   }
120
121   bool elem_empty() const {
122     return Node.empty();
123   }
124
125   const_elem_iterator elem_begin() const {
126     return Node.begin();
127   }
128
129   const_elem_iterator elem_end() const {
130     return Node.end();
131   }
132
133   /// getType() specialization - Type is always MetadataTy.
134   ///
135   inline const Type *getType() const {
136     return Type::MetadataTy;
137   }
138
139   /// isNullValue - Return true if this is the value that would be returned by
140   /// getNullValue.  This always returns false because getNullValue will never
141   /// produce metadata.
142   virtual bool isNullValue() const {
143     return false;
144   }
145
146   /// Profile - calculate a unique identifier for this MDNode to collapse
147   /// duplicates
148   void Profile(FoldingSetNodeID &ID) const;
149
150   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
151     llvm_unreachable("This should never be called because MDNodes have no ops");
152   }
153
154   /// Methods for support type inquiry through isa, cast, and dyn_cast:
155   static inline bool classof(const MDNode *) { return true; }
156   static bool classof(const Value *V) {
157     return V->getValueID() == MDNodeVal;
158   }
159 };
160
161 //===----------------------------------------------------------------------===//
162 /// WeakMetadataVH - a weak value handle for metadata.
163 class WeakMetadataVH : public WeakVH {
164 public:
165   WeakMetadataVH() : WeakVH() {}
166   WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
167   WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
168   
169   operator Value*() const {
170     llvm_unreachable("WeakMetadataVH only handles Metadata");
171   }
172
173   operator MetadataBase*() const {
174    return dyn_cast_or_null<MetadataBase>(getValPtr());
175   }
176 };
177
178 //===----------------------------------------------------------------------===//
179 /// NamedMDNode - a tuple of other metadata. 
180 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
181 template<typename ValueSubClass, typename ItemParentClass>
182   class SymbolTableListTraits;
183
184 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
185   friend class SymbolTableListTraits<NamedMDNode, Module>;
186   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
187
188   friend class LLVMContextImpl;
189
190   Module *Parent;
191   SmallVector<WeakMetadataVH, 4> Node;
192   typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
193
194 protected:
195   explicit NamedMDNode(const Twine &N, MetadataBase*const* Vals, 
196                        unsigned NumVals, Module *M = 0);
197 public:
198   static NamedMDNode *Create(const Twine &N, MetadataBase*const*MDs, 
199                              unsigned NumMDs, Module *M = 0) {
200     return new NamedMDNode(N, MDs, NumMDs, M);
201   }
202
203   typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
204
205   /// getParent - Get the module that holds this named metadata collection.
206   inline Module *getParent() { return Parent; }
207   inline const Module *getParent() const { return Parent; }
208   void setParent(Module *M) { Parent = M; }
209
210   MetadataBase *getElement(unsigned i) const {
211     return Node[i];
212   }
213
214   unsigned getNumElements() const {
215     return Node.size();
216   }
217
218   /// addElement - Add metadata element.
219   void addElement(MetadataBase *M) {
220     Node.push_back(WeakMetadataVH(M));
221   }
222
223   bool elem_empty() const {
224     return Node.empty();
225   }
226
227   const_elem_iterator elem_begin() const {
228     return Node.begin();
229   }
230
231   const_elem_iterator elem_end() const {
232     return Node.end();
233   }
234
235   /// getType() specialization - Type is always MetadataTy.
236   ///
237   inline const Type *getType() const {
238     return Type::MetadataTy;
239   }
240
241   /// isNullValue - Return true if this is the value that would be returned by
242   /// getNullValue.  This always returns false because getNullValue will never
243   /// produce metadata.
244   virtual bool isNullValue() const {
245     return false;
246   }
247
248   /// Profile - calculate a unique identifier for this MDNode to collapse
249   /// duplicates
250   void Profile(FoldingSetNodeID &ID) const;
251
252   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
253     llvm_unreachable(
254                 "This should never be called because NamedMDNodes have no ops");
255   }
256
257   /// Methods for support type inquiry through isa, cast, and dyn_cast:
258   static inline bool classof(const NamedMDNode *) { return true; }
259   static bool classof(const Value *V) {
260     return V->getValueID() == NamedMDNodeVal;
261   }
262 };
263
264 } // end llvm namespace
265
266 #endif