551c2e964d0a662102004c3644ccf218b0b94570
[oota-llvm.git] / include / llvm / MDNode.h
1 //===-- llvm/Metadata.h - Constant class subclass 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 the subclasses of Constant, 
12 /// which represent the different flavors of constant values that live in LLVM.
13 /// Note that Constants are immutable (once created they never change) and are 
14 /// fully shared by structural equivalence.  This means that two structurally
15 /// equivalent constants will always have the same address.  Constant's are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_MDNODE_H
22 #define LLVM_MDNODE_H
23
24 #include "llvm/Constant.h"
25 #include "llvm/Type.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/ValueHandle.h"
30
31 namespace llvm {
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   /// getType() specialization - Type is always MetadataTy.
42   ///
43   inline const Type *getType() const {
44     return Type::MetadataTy;
45   }
46
47   /// isNullValue - Return true if this is the value that would be returned by
48   /// getNullValue.  This always returns false because getNullValue will never
49   /// produce metadata.
50   virtual bool isNullValue() const {
51     return false;
52   }
53
54   /// Methods for support type inquiry through isa, cast, and dyn_cast:
55   static inline bool classof(const MDString *) { return true; }
56   static bool classof(const Value *V) {
57     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
58   }
59 };
60
61 //===----------------------------------------------------------------------===//
62 /// MDString - a single uniqued string.
63 /// These are used to efficiently contain a byte sequence for metadata.
64 ///
65 class MDString : public MetadataBase {
66   MDString(const MDString &);            // DO NOT IMPLEMENT
67   StringRef Str;
68   friend class LLVMContextImpl;
69
70 protected:
71   explicit MDString(const char *begin, unsigned l)
72     : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {}
73
74 public:
75   StringRef getString() const { return Str; }
76
77   unsigned length() const { return Str.size(); }
78
79   /// begin() - Pointer to the first byte of the string.
80   ///
81   const char *begin() const { return Str.begin(); }
82
83   /// end() - Pointer to one byte past the end of the string.
84   ///
85   const char *end() const { return Str.end(); }
86
87   /// Methods for support type inquiry through isa, cast, and dyn_cast:
88   static inline bool classof(const MDString *) { return true; }
89   static bool classof(const Value *V) {
90     return V->getValueID() == MDStringVal;
91   }
92 };
93
94 //===----------------------------------------------------------------------===//
95 /// MDNode - a tuple of other values.
96 /// These contain a list of the values that represent the metadata. 
97 ///
98 class MDNode : public MetadataBase, public FoldingSetNode {
99   MDNode(const MDNode &);      // DO NOT IMPLEMENT
100
101   friend class LLVMContextImpl;
102
103   void replaceElement(Value *From, Value *To);
104
105   SmallVector<WeakVH, 4> Node;
106   typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
107
108 protected:
109   explicit MDNode(Value*const* Vals, unsigned NumVals);
110 public:
111   typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
112
113   Value *getElement(unsigned i) const {
114     return Node[i];
115   }
116
117   unsigned getNumElements() const {
118     return Node.size();
119   }
120
121   bool elem_empty() const {
122     return Node.empty();
123   }
124
125   const_elem_iterator elem_begin() const {
126     return Node.begin();
127   }
128
129   const_elem_iterator elem_end() const {
130     return Node.end();
131   }
132
133   /// getType() specialization - Type is always MetadataTy.
134   ///
135   inline const Type *getType() const {
136     return Type::MetadataTy;
137   }
138
139   /// isNullValue - Return true if this is the value that would be returned by
140   /// getNullValue.  This always returns false because getNullValue will never
141   /// produce metadata.
142   virtual bool isNullValue() const {
143     return false;
144   }
145
146   /// Profile - calculate a unique identifier for this MDNode to collapse
147   /// duplicates
148   void Profile(FoldingSetNodeID &ID) const;
149
150   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
151     llvm_unreachable("This should never be called because MDNodes have no ops");
152   }
153
154   /// Methods for support type inquiry through isa, cast, and dyn_cast:
155   static inline bool classof(const MDNode *) { return true; }
156   static bool classof(const Value *V) {
157     return V->getValueID() == MDNodeVal;
158   }
159 };
160
161 } // end llvm namespace
162
163 #endif