remove some unneeded Metadata interfaces.
[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_METADATA_H
17 #define LLVM_METADATA_H
18
19 #include "llvm/Value.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/ilist_node.h"
22
23 namespace llvm {
24 class Constant;
25 class Instruction;
26 class LLVMContext;
27 class Module;
28 class MetadataContextImpl;
29 template <typename T> class SmallVectorImpl;
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
40   /// Methods for support type inquiry through isa, cast, and dyn_cast:
41   static inline bool classof(const MetadataBase *) { return true; }
42   static bool classof(const Value *V) {
43     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
44       || V->getValueID() == NamedMDNodeVal;
45   }
46 };
47
48 //===----------------------------------------------------------------------===//
49 /// MDString - a single uniqued string.
50 /// These are used to efficiently contain a byte sequence for metadata.
51 /// MDString is always unnamd.
52 class MDString : public MetadataBase {
53   MDString(const MDString &);            // DO NOT IMPLEMENT
54
55   StringRef Str;
56 protected:
57   explicit MDString(LLVMContext &C, StringRef S);
58
59 public:
60   static MDString *get(LLVMContext &Context, StringRef Str);
61   static MDString *get(LLVMContext &Context, const char *Str);
62   
63   StringRef getString() const { return Str; }
64
65   unsigned getLength() const { return (unsigned)Str.size(); }
66
67   typedef StringRef::iterator iterator;
68   
69   /// begin() - Pointer to the first byte of the string.
70   ///
71   iterator begin() const { return Str.begin(); }
72
73   /// end() - Pointer to one byte past the end of the string.
74   ///
75   iterator end() const { return Str.end(); }
76
77   /// Methods for support type inquiry through isa, cast, and dyn_cast:
78   static inline bool classof(const MDString *) { return true; }
79   static bool classof(const Value *V) {
80     return V->getValueID() == MDStringVal;
81   }
82 };
83
84   
85 class MDNodeElement;
86   
87 //===----------------------------------------------------------------------===//
88 /// MDNode - a tuple of other values.
89 /// These contain a list of the values that represent the metadata. 
90 /// MDNode is always unnamed.
91 class MDNode : public MetadataBase, public FoldingSetNode {
92   MDNode(const MDNode &);                // DO NOT IMPLEMENT
93   void operator=(const MDNode &);        // DO NOT IMPLEMENT
94   friend class MDNodeElement;
95
96   MDNodeElement *Operands;
97   unsigned NumOperands;
98   
99   // Subclass data enums.
100   enum {
101     FunctionLocalBit = 1
102   };
103   
104   // Replace each instance of F from the element list of this node with T.
105   void replaceElement(MDNodeElement *Op, Value *NewVal);
106
107 protected:
108   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
109                   bool isFunctionLocal);
110 public:
111   // Constructors and destructors.
112   static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
113                      bool isFunctionLocal = false);
114
115   /// ~MDNode - Destroy MDNode.
116   ~MDNode();
117   
118   /// getElement - Return specified element.
119   Value *getElement(unsigned i) const;
120   
121   /// getNumElements - Return number of MDNode elements.
122   unsigned getNumElements() const { return NumOperands; }
123   
124   /// isFunctionLocal - Return whether MDNode is local to a function.
125   /// Note: MDNodes are designated as function-local when created, and keep
126   ///       that designation even if their operands are modified to no longer
127   ///       refer to function-local IR.
128   bool isFunctionLocal() const {
129     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
130   }
131
132   /// Profile - calculate a unique identifier for this MDNode to collapse
133   /// duplicates
134   void Profile(FoldingSetNodeID &ID) const;
135
136   /// Methods for support type inquiry through isa, cast, and dyn_cast:
137   static inline bool classof(const MDNode *) { return true; }
138   static bool classof(const Value *V) {
139     return V->getValueID() == MDNodeVal;
140   }
141 private:
142   // Shadow Value::setValueSubclassData with a private forwarding method so that
143   // any future subclasses cannot accidentally use it.
144   void setValueSubclassData(unsigned short D) {
145     Value::setValueSubclassData(D);
146   }
147 };
148
149 //===----------------------------------------------------------------------===//
150 /// NamedMDNode - a tuple of other metadata. 
151 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
152 template<typename ValueSubClass, typename ItemParentClass>
153   class SymbolTableListTraits;
154
155 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
156   friend class SymbolTableListTraits<NamedMDNode, Module>;
157   friend class LLVMContextImpl;
158
159   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
160
161   Module *Parent;
162   void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
163
164   void setParent(Module *M) { Parent = M; }
165 protected:
166   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals, 
167                        unsigned NumVals, Module *M = 0);
168 public:
169   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
170                              MetadataBase *const *MDs, 
171                              unsigned NumMDs, Module *M = 0) {
172     return new NamedMDNode(C, N, MDs, NumMDs, M);
173   }
174
175   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
176
177   /// eraseFromParent - Drop all references and remove the node from parent
178   /// module.
179   void eraseFromParent();
180
181   /// dropAllReferences - Remove all uses and clear node vector.
182   void dropAllReferences();
183
184   /// ~NamedMDNode - Destroy NamedMDNode.
185   ~NamedMDNode();
186
187   /// getParent - Get the module that holds this named metadata collection.
188   inline Module *getParent() { return Parent; }
189   inline const Module *getParent() const { return Parent; }
190
191   /// getElement - Return specified element.
192   MetadataBase *getElement(unsigned i) const;
193   
194   /// getNumElements - Return number of NamedMDNode elements.
195   unsigned getNumElements() const;
196
197   /// addElement - Add metadata element.
198   void addElement(MetadataBase *M);
199   
200   /// Methods for support type inquiry through isa, cast, and dyn_cast:
201   static inline bool classof(const NamedMDNode *) { return true; }
202   static bool classof(const Value *V) {
203     return V->getValueID() == NamedMDNodeVal;
204   }
205 };
206
207 //===----------------------------------------------------------------------===//
208 /// MetadataContext - MetadataContext handles uniquing and assignment of IDs for
209 /// custom metadata types.
210 ///
211 class MetadataContext {
212   MetadataContext(MetadataContext&); // DO NOT IMPLEMENT
213   void operator=(MetadataContext&);  // DO NOT IMPLEMENT
214
215   MetadataContextImpl *const pImpl;
216   friend class Instruction;
217 public:
218   MetadataContext();
219   ~MetadataContext();
220
221   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
222   unsigned getMDKindID(StringRef Name) const;
223
224   /// isValidName - Return true if Name is a valid custom metadata handler name.
225   static bool isValidName(StringRef Name);
226
227   /// copyMD - If metadata is attached with Instruction In1 then attach
228   /// the same metadata to In2.
229   void copyMD(Instruction *In1, Instruction *In2);
230
231   /// getMDKindNames - Populate client supplied SmallVector with the name for
232   /// each custom metadata ID.   ID #0 is not used, so it is filled in as empty.
233   void getMDKindNames(SmallVectorImpl<StringRef> &) const;
234 };
235
236 } // end llvm namespace
237
238 #endif