Rename MDNode.h header. It defines MDnode and other metadata classes.
[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_MDNODE_H
17 #define LLVM_MDNODE_H
18
19 #include "llvm/Constant.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ValueHandle.h"
25
26 namespace llvm {
27
28 //===----------------------------------------------------------------------===//
29 // MetadataBase  - A base class for MDNode and MDString.
30 class MetadataBase : public Value {
31 protected:
32   MetadataBase(const Type *Ty, unsigned scid)
33     : Value(Ty, scid) {}
34
35 public:
36   /// getType() specialization - Type is always MetadataTy.
37   ///
38   inline const Type *getType() const {
39     return Type::MetadataTy;
40   }
41
42   /// isNullValue - Return true if this is the value that would be returned by
43   /// getNullValue.  This always returns false because getNullValue will never
44   /// produce metadata.
45   virtual bool isNullValue() const {
46     return false;
47   }
48
49   /// Methods for support type inquiry through isa, cast, and dyn_cast:
50   static inline bool classof(const MDString *) { return true; }
51   static bool classof(const Value *V) {
52     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
53   }
54 };
55
56 //===----------------------------------------------------------------------===//
57 /// MDString - a single uniqued string.
58 /// These are used to efficiently contain a byte sequence for metadata.
59 ///
60 class MDString : public MetadataBase {
61   MDString(const MDString &);            // DO NOT IMPLEMENT
62   StringRef Str;
63   friend class LLVMContextImpl;
64
65 protected:
66   explicit MDString(const char *begin, unsigned l)
67     : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
68
69 public:
70   StringRef getString() const { return Str; }
71
72   unsigned length() const { return Str.size(); }
73
74   /// begin() - Pointer to the first byte of the string.
75   ///
76   const char *begin() const { return Str.begin(); }
77
78   /// end() - Pointer to one byte past the end of the string.
79   ///
80   const char *end() const { return Str.end(); }
81
82   /// Methods for support type inquiry through isa, cast, and dyn_cast:
83   static inline bool classof(const MDString *) { return true; }
84   static bool classof(const Value *V) {
85     return V->getValueID() == MDStringVal;
86   }
87 };
88
89 //===----------------------------------------------------------------------===//
90 /// MDNode - a tuple of other values.
91 /// These contain a list of the values that represent the metadata. 
92 ///
93 class MDNode : public MetadataBase, public FoldingSetNode {
94   MDNode(const MDNode &);      // DO NOT IMPLEMENT
95
96   friend class LLVMContextImpl;
97
98   void replaceElement(Value *From, Value *To);
99
100   SmallVector<WeakVH, 4> Node;
101   typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
102
103 protected:
104   explicit MDNode(Value*const* Vals, unsigned NumVals);
105 public:
106   typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
107
108   Value *getElement(unsigned i) const {
109     return Node[i];
110   }
111
112   unsigned getNumElements() const {
113     return Node.size();
114   }
115
116   bool elem_empty() const {
117     return Node.empty();
118   }
119
120   const_elem_iterator elem_begin() const {
121     return Node.begin();
122   }
123
124   const_elem_iterator elem_end() const {
125     return Node.end();
126   }
127
128   /// getType() specialization - Type is always MetadataTy.
129   ///
130   inline const Type *getType() const {
131     return Type::MetadataTy;
132   }
133
134   /// isNullValue - Return true if this is the value that would be returned by
135   /// getNullValue.  This always returns false because getNullValue will never
136   /// produce metadata.
137   virtual bool isNullValue() const {
138     return false;
139   }
140
141   /// Profile - calculate a unique identifier for this MDNode to collapse
142   /// duplicates
143   void Profile(FoldingSetNodeID &ID) const;
144
145   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
146     llvm_unreachable("This should never be called because MDNodes have no ops");
147   }
148
149   /// Methods for support type inquiry through isa, cast, and dyn_cast:
150   static inline bool classof(const MDNode *) { return true; }
151   static bool classof(const Value *V) {
152     return V->getValueID() == MDNodeVal;
153   }
154 };
155
156 } // end llvm namespace
157
158 #endif