Add NamedMDNode.
authorDevang Patel <dpatel@apple.com>
Wed, 29 Jul 2009 00:33:07 +0000 (00:33 +0000)
committerDevang Patel <dpatel@apple.com>
Wed, 29 Jul 2009 00:33:07 +0000 (00:33 +0000)
llvm-svn: 77409

llvm/include/llvm/Metadata.h
llvm/include/llvm/Value.h
llvm/lib/VMCore/Metadata.cpp

index baa9cb8..9ee9b43 100644 (file)
@@ -26,7 +26,7 @@
 namespace llvm {
 
 //===----------------------------------------------------------------------===//
-// MetadataBase  - A base class for MDNode and MDString.
+// MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
 class MetadataBase : public Value {
 protected:
   MetadataBase(const Type *Ty, unsigned scid)
@@ -49,14 +49,15 @@ public:
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const MDString *) { return true; }
   static bool classof(const Value *V) {
-    return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
+    return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
+      || V->getValueID() == NamedMDNodeVal;
   }
 };
 
 //===----------------------------------------------------------------------===//
 /// MDString - a single uniqued string.
 /// These are used to efficiently contain a byte sequence for metadata.
-///
+/// MDString is always unnamd.
 class MDString : public MetadataBase {
   MDString(const MDString &);            // DO NOT IMPLEMENT
   StringRef Str;
@@ -89,7 +90,7 @@ public:
 //===----------------------------------------------------------------------===//
 /// MDNode - a tuple of other values.
 /// These contain a list of the values that represent the metadata. 
-///
+/// MDNode is always unnamed.
 class MDNode : public MetadataBase, public FoldingSetNode {
   MDNode(const MDNode &);      // DO NOT IMPLEMENT
 
@@ -151,6 +152,104 @@ public:
   }
 };
 
+//===----------------------------------------------------------------------===//
+/// WeakMetadataVH - a weak value handle for metadata.
+class WeakMetadataVH : public WeakVH {
+public:
+  WeakMetadataVH() : WeakVH() {}
+  WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
+  WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
+  
+  operator Value*() const {
+    llvm_unreachable("WeakMetadataVH only handles Metadata");
+  }
+
+  operator MetadataBase*() const {
+   return cast<MetadataBase>(getValPtr());
+  }
+};
+
+//===----------------------------------------------------------------------===//
+/// NamedMDNode - a tuple of other metadata. 
+/// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
+class NamedMDNode : public MetadataBase {
+  NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
+
+  friend class LLVMContextImpl;
+
+  Module *Parent;
+  StringRef Name;
+  SmallVector<WeakMetadataVH, 4> Node;
+  typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
+
+protected:
+  explicit NamedMDNode(const char *N, unsigned NameLength,
+                       MetadataBase*const* Vals, unsigned NumVals,
+                       Module *M = 0);
+public:
+  static NamedMDNode *Create(const char *N, unsigned NamedLength,
+                             MetadataBase*const*MDs, unsigned NumMDs,
+                             Module *M = 0) {
+    return new NamedMDNode(N,  NamedLength, MDs, NumMDs, M);
+  }
+
+  typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
+
+  StringRef getName() const { return Name; }
+
+  /// getParent - Get the module that holds this named metadata collection.
+  inline Module *getParent() { return Parent; }
+  inline const Module *getParent() const { return Parent; }
+
+  Value *getElement(unsigned i) const {
+    return Node[i];
+  }
+
+  unsigned getNumElements() const {
+    return Node.size();
+  }
+
+  bool elem_empty() const {
+    return Node.empty();
+  }
+
+  const_elem_iterator elem_begin() const {
+    return Node.begin();
+  }
+
+  const_elem_iterator elem_end() const {
+    return Node.end();
+  }
+
+  /// getType() specialization - Type is always MetadataTy.
+  ///
+  inline const Type *getType() const {
+    return Type::MetadataTy;
+  }
+
+  /// isNullValue - Return true if this is the value that would be returned by
+  /// getNullValue.  This always returns false because getNullValue will never
+  /// produce metadata.
+  virtual bool isNullValue() const {
+    return false;
+  }
+
+  /// Profile - calculate a unique identifier for this MDNode to collapse
+  /// duplicates
+  void Profile(FoldingSetNodeID &ID) const;
+
+  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
+    llvm_unreachable(
+                "This should never be called because NamedMDNodes have no ops");
+  }
+
+  /// Methods for support type inquiry through isa, cast, and dyn_cast:
+  static inline bool classof(const NamedMDNode *) { return true; }
+  static bool classof(const Value *V) {
+    return V->getValueID() == NamedMDNodeVal;
+  }
+};
+
 } // end llvm namespace
 
 #endif
index f690f44..b1db1ce 100644 (file)
@@ -219,6 +219,7 @@ public:
     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
     MDNodeVal,                // This is an instance of MDNode
     MDStringVal,              // This is an instance of MDString
+    NamedMDNodeVal,           // This is an instance of NamedMDNode
     InlineAsmVal,             // This is an instance of InlineAsm
     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
     InstructionVal,           // This is an instance of Instruction
index 1a6288e..73f89ca 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Metadata.h"
+#include "llvm/Module.h"
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -27,3 +28,17 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
   for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
     ID.AddPointer(*I);
 }
+
+//===----------------------------------------------------------------------===//
+//NamedMDNode implementation
+//
+NamedMDNode::NamedMDNode(const char *N, unsigned NameLength,
+                         MetadataBase*const* MDs, unsigned NumMDs,
+                         Module *M)
+  : MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal),
+    Parent(M), Name(N, NameLength) {
+  for (unsigned i = 0; i != NumMDs; ++i)
+    Node.push_back(WeakMetadataVH(MDs[i]));
+
+  // FIXME : Add into the parent module.
+}