Add another required #include for freestanding .h files.
[oota-llvm.git] / include / llvm / OperandTraits.h
1 //===-- llvm/OperandTraits.h - OperandTraits class definition ---*- 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 // This file defines the traits classes that are handy for enforcing the correct
11 // layout of various User subclasses. It also provides the means for accessing
12 // the operands in the most efficient manner.
13 //
14
15 #ifndef LLVM_OPERAND_TRAITS_H
16 #define LLVM_OPERAND_TRAITS_H
17
18 #include "llvm/User.h"
19
20 namespace llvm {
21
22 //===----------------------------------------------------------------------===//
23 //                          FixedNumOperands Trait Class
24 //===----------------------------------------------------------------------===//
25
26 /// FixedNumOperandTraits - determine the allocation regime of the Use array
27 /// when it is a prefix to the User object, and the number of Use objects is
28 /// known at compile time.
29
30 template <unsigned ARITY>
31 struct FixedNumOperandTraits {
32   static Use *op_begin(User* U) {
33     return reinterpret_cast<Use*>(U) - ARITY;
34   }
35   static Use *op_end(User* U) {
36     return reinterpret_cast<Use*>(U);
37   }
38   static unsigned operands(const User*) {
39     return ARITY;
40   }
41   struct prefix {
42     Use Ops[ARITY];
43     prefix(); // DO NOT IMPLEMENT
44   };
45   template <class U>
46   struct Layout {
47     struct overlay : public prefix, public U {
48       overlay(); // DO NOT IMPLEMENT
49     };
50   };
51 };
52
53 //===----------------------------------------------------------------------===//
54 //                          OptionalOperands Trait Class
55 //===----------------------------------------------------------------------===//
56
57 template <unsigned ARITY = 1>
58 struct OptionalOperandTraits : public FixedNumOperandTraits<ARITY> {
59   static unsigned operands(const User *U) {
60     return U->getNumOperands();
61   }
62 };
63
64 //===----------------------------------------------------------------------===//
65 //                          VariadicOperand Trait Class
66 //===----------------------------------------------------------------------===//
67
68 /// VariadicOperandTraits - determine the allocation regime of the Use array
69 /// when it is a prefix to the User object, and the number of Use objects is
70 /// only known at allocation time.
71
72 template <unsigned MINARITY = 0>
73 struct VariadicOperandTraits {
74   static Use *op_begin(User* U) {
75     return reinterpret_cast<Use*>(U) - U->getNumOperands();
76   }
77   static Use *op_end(User* U) {
78     return reinterpret_cast<Use*>(U);
79   }
80   static unsigned operands(const User *U) {
81     return U->getNumOperands();
82   }
83 };
84
85 //===----------------------------------------------------------------------===//
86 //                          HungoffOperand Trait Class
87 //===----------------------------------------------------------------------===//
88
89 /// HungoffOperandTraits - determine the allocation regime of the Use array
90 /// when it is not a prefix to the User object, but allocated at an unrelated
91 /// heap address.
92 /// Assumes that the User subclass that is determined by this traits class
93 /// has an OperandList member of type User::op_iterator. [Note: this is now
94 /// trivially satisfied, because User has that member for historic reasons.]
95 ///
96 /// This is the traits class that is needed when the Use array must be
97 /// resizable.
98
99 template <unsigned MINARITY = 1>
100 struct HungoffOperandTraits {
101   static Use *op_begin(User* U) {
102     return U->OperandList;
103   }
104   static Use *op_end(User* U) {
105     return U->OperandList + U->getNumOperands();
106   }
107   static unsigned operands(const User *U) {
108     return U->getNumOperands();
109   }
110 };
111
112 /// Macro for generating in-class operand accessor declarations.
113 /// It should only be called in the public section of the interface.
114 ///
115 #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
116   public: \
117   inline VALUECLASS *getOperand(unsigned) const; \
118   inline void setOperand(unsigned, VALUECLASS*); \
119   inline op_iterator op_begin(); \
120   inline const_op_iterator op_begin() const; \
121   inline op_iterator op_end(); \
122   inline const_op_iterator op_end() const; \
123   protected: \
124   template <int> inline Use &Op(); \
125   template <int> inline const Use &Op() const; \
126   public: \
127   inline unsigned getNumOperands() const
128
129 /// Macro for generating out-of-class operand accessor definitions
130 #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
131 CLASS::op_iterator CLASS::op_begin() { \
132   return OperandTraits<CLASS>::op_begin(this); \
133 } \
134 CLASS::const_op_iterator CLASS::op_begin() const { \
135   return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
136 } \
137 CLASS::op_iterator CLASS::op_end() { \
138   return OperandTraits<CLASS>::op_end(this); \
139 } \
140 CLASS::const_op_iterator CLASS::op_end() const { \
141   return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
142 } \
143 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
144   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
145          && "getOperand() out of range!"); \
146   return static_cast<VALUECLASS*>( \
147     OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
148 } \
149 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
150   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
151          && "setOperand() out of range!"); \
152   OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
153 } \
154 unsigned CLASS::getNumOperands() const { \
155   return OperandTraits<CLASS>::operands(this);  \
156 } \
157 template <int Idx_nocapture> Use &CLASS::Op() { \
158   return this->OpFrom<Idx_nocapture>(this); \
159 } \
160 template <int Idx_nocapture> const Use &CLASS::Op() const { \
161   return this->OpFrom<Idx_nocapture>(this); \
162 }
163
164
165 /// Macro for generating out-of-class operand accessor
166 /// definitions with casted result
167 #define DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
168 CLASS::op_iterator CLASS::op_begin() { \
169   return OperandTraits<CLASS>::op_begin(this); \
170 } \
171 CLASS::const_op_iterator CLASS::op_begin() const { \
172   return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
173 } \
174 CLASS::op_iterator CLASS::op_end() { \
175   return OperandTraits<CLASS>::op_end(this); \
176 } \
177 CLASS::const_op_iterator CLASS::op_end() const { \
178   return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
179 } \
180 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
181   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
182          && "getOperand() out of range!"); \
183   return cast<VALUECLASS>( \
184     OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
185 } \
186 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
187   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
188          && "setOperand() out of range!"); \
189   OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
190 } \
191 unsigned CLASS::getNumOperands() const { \
192   return OperandTraits<CLASS>::operands(this); \
193 } \
194 template <int Idx_nocapture> Use &CLASS::Op() { \
195   return this->OpFrom<Idx_nocapture>(this); \
196 } \
197 template <int Idx_nocapture> const Use &CLASS::Op() const { \
198   return this->OpFrom<Idx_nocapture>(this); \
199 }
200
201
202 } // End llvm namespace
203
204 #endif