LLVM API Documentation

Metadata.h

Go to the documentation of this file.
00001 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 /// @file
00011 /// This file contains the declarations for metadata subclasses.
00012 /// They represent the different flavors of metadata that live in LLVM.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_MDNODE_H
00017 #define LLVM_MDNODE_H
00018 
00019 #include "llvm/User.h"
00020 #include "llvm/Type.h"
00021 #include "llvm/OperandTraits.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/ADT/ilist_node.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 #include "llvm/Support/ValueHandle.h"
00026 
00027 namespace llvm {
00028 class Constant;
00029 class LLVMContext;
00030 template<class ConstantClass, class TypeClass, class ValType>
00031 struct ConstantCreator;
00032 
00033 //===----------------------------------------------------------------------===//
00034 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
00035 class MetadataBase : public User {
00036 private:
00037   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
00038   /// the number actually in use.
00039   unsigned ReservedSpace;
00040 
00041 protected:
00042   MetadataBase(const Type *Ty, unsigned scid)
00043     : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
00044 
00045   void resizeOperands(unsigned NumOps);
00046 public:
00047   /// isNullValue - Return true if this is the value that would be returned by
00048   /// getNullValue.  This always returns false because getNullValue will never
00049   /// produce metadata.
00050   virtual bool isNullValue() const {
00051     return false;
00052   }
00053 
00054   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00055   static inline bool classof(const MetadataBase *) { return true; }
00056   static bool classof(const Value *V) {
00057     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
00058       || V->getValueID() == NamedMDNodeVal;
00059   }
00060 };
00061 
00062 //===----------------------------------------------------------------------===//
00063 /// MDString - a single uniqued string.
00064 /// These are used to efficiently contain a byte sequence for metadata.
00065 /// MDString is always unnamd.
00066 class MDString : public MetadataBase {
00067   MDString(const MDString &);            // DO NOT IMPLEMENT
00068   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
00069   unsigned getNumOperands();             // DO NOT IMPLEMENT
00070 
00071   StringRef Str;
00072 protected:
00073   explicit MDString(LLVMContext &C, const char *begin, unsigned l)
00074     : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
00075 
00076 public:
00077   // Do not allocate any space for operands.
00078   void *operator new(size_t s) {
00079     return User::operator new(s, 0);
00080   }
00081   static MDString *get(LLVMContext &Context, const StringRef &Str);
00082   
00083   StringRef getString() const { return Str; }
00084 
00085   unsigned length() const { return Str.size(); }
00086 
00087   /// begin() - Pointer to the first byte of the string.
00088   ///
00089   const char *begin() const { return Str.begin(); }
00090 
00091   /// end() - Pointer to one byte past the end of the string.
00092   ///
00093   const char *end() const { return Str.end(); }
00094 
00095   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00096   static inline bool classof(const MDString *) { return true; }
00097   static bool classof(const Value *V) {
00098     return V->getValueID() == MDStringVal;
00099   }
00100 };
00101 
00102 //===----------------------------------------------------------------------===//
00103 /// MDNode - a tuple of other values.
00104 /// These contain a list of the values that represent the metadata. 
00105 /// MDNode is always unnamed.
00106 class MDNode : public MetadataBase {
00107   MDNode(const MDNode &);                // DO NOT IMPLEMENT
00108   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
00109   // getNumOperands - Make this only available for private uses.
00110   unsigned getNumOperands() { return User::getNumOperands();  }
00111 
00112   SmallVector<WeakVH, 4> Node;
00113   
00114   friend struct ConstantCreator<MDNode, Type, std::vector<Value*> >;
00115 protected:
00116   explicit MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals);
00117 public:
00118   // Do not allocate any space for operands.
00119   void *operator new(size_t s) {
00120     return User::operator new(s, 0);
00121   }
00122   // Constructors and destructors.
00123   static MDNode *get(LLVMContext &Context, 
00124                      Value* const* Vals, unsigned NumVals);
00125 
00126   /// dropAllReferences - Remove all uses and clear node vector.
00127   void dropAllReferences();
00128 
00129   /// ~MDNode - Destroy MDNode.
00130   ~MDNode();
00131   
00132   /// getElement - Return specified element.
00133   Value *getElement(unsigned i) const {
00134     assert (getNumElements() > i && "Invalid element number!");
00135     return Node[i];
00136   }
00137 
00138   /// getNumElements - Return number of MDNode elements.
00139   unsigned getNumElements() const {
00140     return Node.size();
00141   }
00142 
00143   // Element access
00144   typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
00145   typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
00146   /// elem_empty - Return true if MDNode is empty.
00147   bool elem_empty() const                { return Node.empty(); }
00148   const_elem_iterator elem_begin() const { return Node.begin(); }
00149   const_elem_iterator elem_end() const   { return Node.end();   }
00150   elem_iterator elem_begin()             { return Node.begin(); }
00151   elem_iterator elem_end()               { return Node.end();   }
00152 
00153   /// isNullValue - Return true if this is the value that would be returned by
00154   /// getNullValue.  This always returns false because getNullValue will never
00155   /// produce metadata.
00156   virtual bool isNullValue() const {
00157     return false;
00158   }
00159 
00160   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
00161     llvm_unreachable("This should never be called because MDNodes have no ops");
00162   }
00163 
00164   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00165   static inline bool classof(const MDNode *) { return true; }
00166   static bool classof(const Value *V) {
00167     return V->getValueID() == MDNodeVal;
00168   }
00169 };
00170 
00171 //===----------------------------------------------------------------------===//
00172 /// WeakMetadataVH - a weak value handle for metadata.
00173 class WeakMetadataVH : public WeakVH {
00174 public:
00175   WeakMetadataVH() : WeakVH() {}
00176   WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
00177   WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
00178   
00179   operator Value*() const {
00180     llvm_unreachable("WeakMetadataVH only handles Metadata");
00181   }
00182 
00183   operator MetadataBase*() const {
00184    return dyn_cast_or_null<MetadataBase>(getValPtr());
00185   }
00186 };
00187 
00188 //===----------------------------------------------------------------------===//
00189 /// NamedMDNode - a tuple of other metadata. 
00190 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
00191 template<typename ValueSubClass, typename ItemParentClass>
00192   class SymbolTableListTraits;
00193 
00194 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
00195   friend class SymbolTableListTraits<NamedMDNode, Module>;
00196   friend class LLVMContextImpl;
00197 
00198   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
00199   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
00200   // getNumOperands - Make this only available for private uses.
00201   unsigned getNumOperands() { return User::getNumOperands();  }
00202 
00203   Module *Parent;
00204   SmallVector<WeakMetadataVH, 4> Node;
00205   typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
00206 
00207 protected:
00208   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const* Vals, 
00209                        unsigned NumVals, Module *M = 0);
00210 public:
00211   // Do not allocate any space for operands.
00212   void *operator new(size_t s) {
00213     return User::operator new(s, 0);
00214   }
00215   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
00216                              MetadataBase*const*MDs, 
00217                              unsigned NumMDs, Module *M = 0) {
00218     return new NamedMDNode(C, N, MDs, NumMDs, M);
00219   }
00220 
00221   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
00222 
00223   /// eraseFromParent - Drop all references and remove the node from parent
00224   /// module.
00225   void eraseFromParent();
00226 
00227   /// dropAllReferences - Remove all uses and clear node vector.
00228   void dropAllReferences();
00229 
00230   /// ~NamedMDNode - Destroy NamedMDNode.
00231   ~NamedMDNode();
00232 
00233   /// getParent - Get the module that holds this named metadata collection.
00234   inline Module *getParent() { return Parent; }
00235   inline const Module *getParent() const { return Parent; }
00236   void setParent(Module *M) { Parent = M; }
00237 
00238   /// getElement - Return specified element.
00239   MetadataBase *getElement(unsigned i) const {
00240     assert (getNumElements() > i && "Invalid element number!");
00241     return Node[i];
00242   }
00243 
00244   /// getNumElements - Return number of NamedMDNode elements.
00245   unsigned getNumElements() const {
00246     return Node.size();
00247   }
00248 
00249   /// addElement - Add metadata element.
00250   void addElement(MetadataBase *M) {
00251     resizeOperands(0);
00252     OperandList[NumOperands++] = M;
00253     Node.push_back(WeakMetadataVH(M));
00254   }
00255 
00256   typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
00257   bool elem_empty() const                { return Node.empty(); }
00258   const_elem_iterator elem_begin() const { return Node.begin(); }
00259   const_elem_iterator elem_end() const   { return Node.end();   }
00260   elem_iterator elem_begin()             { return Node.begin(); }
00261   elem_iterator elem_end()               { return Node.end();   }
00262 
00263   /// isNullValue - Return true if this is the value that would be returned by
00264   /// getNullValue.  This always returns false because getNullValue will never
00265   /// produce metadata.
00266   virtual bool isNullValue() const {
00267     return false;
00268   }
00269 
00270   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
00271     llvm_unreachable(
00272                 "This should never be called because NamedMDNodes have no ops");
00273   }
00274 
00275   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00276   static inline bool classof(const NamedMDNode *) { return true; }
00277   static bool classof(const Value *V) {
00278     return V->getValueID() == NamedMDNodeVal;
00279   }
00280 };
00281 
00282 } // end llvm namespace
00283 
00284 #endif