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