#include "llvm/Type.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ValueHandle.h"
namespace llvm {
//===----------------------------------------------------------------------===//
-/// MDNode - a tuple of other values.
-/// These contain a list of the Constants that represent the metadata. The
-/// operand list is always empty, query the element list instead.
-///
-/// This class will attempt to keep track of values as they are modified. When
-/// a value is replaced the element will be replaced with it, and when the
-/// value is deleted the element is set to a null pointer. In order to preserve
-/// structural equivalence while the elements mutate, the MDNode may call
-/// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
-/// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
-/// of the elements held by the node may change.
+// MetadataBase - A base class for MDNode and MDString.
+class MetadataBase : public Value {
+public:
+ MetadataBase(const Type *Ty, unsigned scid)
+ : Value(Ty, scid) {}
+
+ /// 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;
+ }
+
+ /// 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;
+ }
+};
+
+//===----------------------------------------------------------------------===//
+/// MDString - a single uniqued string.
+/// These are used to efficiently contain a byte sequence for metadata.
///
-class MDNode : public Constant, public FoldingSetNode {
- MDNode(const MDNode &); // DO NOT IMPLEMENT
+class MDString : public MetadataBase {
+ MDString(const MDString &); // DO NOT IMPLEMENT
+
+ const char *StrBegin, *StrEnd;
+ friend class LLVMContextImpl;
+
+public:
+ MDString(const char *begin, const char *end)
+ : MetadataBase(Type::MetadataTy, Value::MDStringVal),
+ StrBegin(begin), StrEnd(end) {}
- friend class ElementVH;
- struct ElementVH : public CallbackVH {
- MDNode *OwningNode;
+ intptr_t size() const { return StrEnd - StrBegin; }
- ElementVH(Value *V, MDNode *Parent)
- : CallbackVH(V), OwningNode(Parent) {}
+ /// begin() - Pointer to the first byte of the string.
+ ///
+ const char *begin() const { return StrBegin; }
- ~ElementVH() {}
+ /// end() - Pointer to one byte past the end of the string.
+ ///
+ const char *end() const { return StrEnd; }
+
+ /// 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;
+ }
+};
- /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
- /// the MDNode.
- virtual void deleted() {
- OwningNode->replaceElement(this->operator Value*(), 0);
- }
+//===----------------------------------------------------------------------===//
+/// MDNode - a tuple of other values.
+/// These contain a list of the values that represent the metadata.
+///
+class MDNode : public MetadataBase, public FoldingSetNode {
+ MDNode(const MDNode &); // DO NOT IMPLEMENT
- /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
- /// new_value. This will reallocate the MDNode.
- virtual void allUsesReplacedWith(Value *new_value) {
- OwningNode->replaceElement(this->operator Value*(), new_value);
- }
- };
+ friend class LLVMContextImpl;
void replaceElement(Value *From, Value *To);
- SmallVector<ElementVH, 4> Node;
- typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
+ SmallVector<WeakVH, 4> Node;
+ typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
+
protected:
explicit MDNode(Value*const* Vals, unsigned NumVals);
public:
- typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
-
- /// get() - Static factory methods - Return objects of the specified value.
- ///
- static MDNode *get(Value*const* Vals, unsigned NumVals);
+ typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
Value *getElement(unsigned i) const {
return Node[i];
/// duplicates
void Profile(FoldingSetNodeID &ID) const;
- virtual void destroyConstant();
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
- assert(0 && "This should never be called because MDNodes have no ops");
- abort();
+ llvm_unreachable("This should never be called because MDNodes have no ops");
}
/// Methods for support type inquiry through isa, cast, and dyn_cast: