Derive NamedMDNode from Value.
[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 template <typename T> class SmallVectorImpl;
29 template<typename ValueSubClass, typename ItemParentClass>
30   class SymbolTableListTraits;
31   
32   
33 //===----------------------------------------------------------------------===//
34 // MetadataBase  - A base class for MDNode and MDString.
35 class MetadataBase : public Value {
36 protected:
37   MetadataBase(const Type *Ty, unsigned scid)
38     : Value(Ty, scid) {}
39
40 public:
41
42   /// Methods for support type inquiry through isa, cast, and dyn_cast:
43   static inline bool classof(const MetadataBase *) { return true; }
44   static bool classof(const Value *V) {
45     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
46   }
47 };
48
49 //===----------------------------------------------------------------------===//
50 /// MDString - a single uniqued string.
51 /// These are used to efficiently contain a byte sequence for metadata.
52 /// MDString is always unnamd.
53 class MDString : public MetadataBase {
54   MDString(const MDString &);            // DO NOT IMPLEMENT
55
56   StringRef Str;
57 protected:
58   explicit MDString(LLVMContext &C, StringRef S);
59
60 public:
61   static MDString *get(LLVMContext &Context, StringRef Str);
62   static MDString *get(LLVMContext &Context, const char *Str);
63   
64   StringRef getString() const { return Str; }
65
66   unsigned getLength() const { return (unsigned)Str.size(); }
67
68   typedef StringRef::iterator iterator;
69   
70   /// begin() - Pointer to the first byte of the string.
71   ///
72   iterator begin() const { return Str.begin(); }
73
74   /// end() - Pointer to one byte past the end of the string.
75   ///
76   iterator end() const { return Str.end(); }
77
78   /// Methods for support type inquiry through isa, cast, and dyn_cast:
79   static inline bool classof(const MDString *) { return true; }
80   static bool classof(const Value *V) {
81     return V->getValueID() == MDStringVal;
82   }
83 };
84
85   
86 class MDNodeOperand;
87   
88 //===----------------------------------------------------------------------===//
89 /// MDNode - a tuple of other values.
90 class MDNode : public MetadataBase, public FoldingSetNode {
91   MDNode(const MDNode &);                // DO NOT IMPLEMENT
92   void operator=(const MDNode &);        // DO NOT IMPLEMENT
93   friend class MDNodeOperand;
94
95   /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
96   /// end of this MDNode.
97   unsigned NumOperands;
98   
99   // Subclass data enums.
100   enum {
101     /// FunctionLocalBit - This bit is set if this MDNode is function local.
102     /// This is true when it (potentially transitively) contains a reference to
103     /// something in a function, like an argument, basicblock, or instruction.
104     FunctionLocalBit = 1 << 0,
105     
106     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
107     /// have a null perand.
108     NotUniquedBit    = 1 << 1,
109     
110     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
111     /// that the node isn't being destroyed with a plain 'delete'.
112     DestroyFlag      = 1 << 2
113   };
114   
115   // Replace each instance of F from the operand list of this node with T.
116   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
117   ~MDNode();
118
119 protected:
120   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
121                   bool isFunctionLocal);
122 public:
123   // Constructors and destructors.
124   static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
125                      bool isFunctionLocal = false);
126   
127   /// getOperand - Return specified operand.
128   Value *getOperand(unsigned i) const;
129   
130   /// getNumOperands - Return number of MDNode operands.
131   unsigned getNumOperands() const { return NumOperands; }
132   
133   /// isFunctionLocal - Return whether MDNode is local to a function.
134   /// Note: MDNodes are designated as function-local when created, and keep
135   ///       that designation even if their operands are modified to no longer
136   ///       refer to function-local IR.
137   bool isFunctionLocal() const {
138     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
139   }
140
141   // destroy - Delete this node.  Only when there are no uses.
142   void destroy();
143
144   /// Profile - calculate a unique identifier for this MDNode to collapse
145   /// duplicates
146   void Profile(FoldingSetNodeID &ID) const;
147
148   /// Methods for support type inquiry through isa, cast, and dyn_cast:
149   static inline bool classof(const MDNode *) { return true; }
150   static bool classof(const Value *V) {
151     return V->getValueID() == MDNodeVal;
152   }
153 private:
154   bool isNotUniqued() const { 
155     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
156   }
157   void setIsNotUniqued() {
158     setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
159   }
160   
161   // Shadow Value::setValueSubclassData with a private forwarding method so that
162   // any future subclasses cannot accidentally use it.
163   void setValueSubclassData(unsigned short D) {
164     Value::setValueSubclassData(D);
165   }
166 };
167
168 //===----------------------------------------------------------------------===//
169 /// NamedMDNode - a tuple of MDNodes.
170 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
171 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
172   friend class SymbolTableListTraits<NamedMDNode, Module>;
173   friend class LLVMContextImpl;
174
175   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
176
177   std::string Name;
178   Module *Parent;
179   void *Operands; // SmallVector<WeakVH<MDNode>, 4>
180
181   void setParent(Module *M) { Parent = M; }
182 protected:
183   explicit NamedMDNode(LLVMContext &C, StringRef N, MDNode*const *Vals, 
184                        unsigned NumVals, Module *M = 0);
185 public:
186   static NamedMDNode *Create(LLVMContext &C, StringRef N,
187                              MDNode *const *MDs, 
188                              unsigned NumMDs, Module *M = 0) {
189     return new NamedMDNode(C, N, MDs, NumMDs, M);
190   }
191
192   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
193
194   /// eraseFromParent - Drop all references and remove the node from parent
195   /// module.
196   void eraseFromParent();
197
198   /// dropAllReferences - Remove all uses and clear node vector.
199   void dropAllReferences();
200
201   /// ~NamedMDNode - Destroy NamedMDNode.
202   ~NamedMDNode();
203
204   /// getParent - Get the module that holds this named metadata collection.
205   inline Module *getParent() { return Parent; }
206   inline const Module *getParent() const { return Parent; }
207
208   /// getOperand - Return specified operand.
209   MDNode *getOperand(unsigned i) const;
210   
211   /// getNumOperands - Return the number of NamedMDNode operands.
212   unsigned getNumOperands() const;
213
214   /// addOperand - Add metadata operand.
215   void addOperand(MDNode *M);
216
217   /// setName - Set the name of this named metadata.
218   void setName(StringRef Name);
219
220   /// getName - Return a constant reference to this named metadata's name.
221   StringRef getName() const;
222
223   /// Methods for support type inquiry through isa, cast, and dyn_cast:
224   static inline bool classof(const NamedMDNode *) { return true; }
225   static bool classof(const Value *V) {
226     return V->getValueID() == NamedMDNodeVal;
227   }
228 };
229
230 } // end llvm namespace
231
232 #endif