1 //===-- llvm/OperandTraits.h - OperandTraits class definition ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 #ifndef LLVM_OPERAND_TRAITS_H
16 #define LLVM_OPERAND_TRAITS_H
18 #include "llvm/User.h"
22 //===----------------------------------------------------------------------===//
23 // FixedNumOperands Trait Class
24 //===----------------------------------------------------------------------===//
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.
30 template <unsigned ARITY>
31 struct FixedNumOperandTraits {
32 static Use *op_begin(User* U) {
33 return reinterpret_cast<Use*>(U) - ARITY;
35 static Use *op_end(User* U) {
36 return reinterpret_cast<Use*>(U);
38 static unsigned operands(const User*) {
43 prefix(); // DO NOT IMPLEMENT
47 struct overlay : prefix, U {
48 overlay(); // DO NOT IMPLEMENT
51 static inline void *allocate(unsigned); // FIXME
54 //===----------------------------------------------------------------------===//
55 // OptionalOperands Trait Class
56 //===----------------------------------------------------------------------===//
58 template <unsigned ARITY = 1>
59 struct OptionalOperandTraits : FixedNumOperandTraits<ARITY> {
60 static unsigned operands(const User *U) {
61 return U->getNumOperands();
65 //===----------------------------------------------------------------------===//
66 // VariadicOperand Trait Class
67 //===----------------------------------------------------------------------===//
69 /// VariadicOperandTraits - determine the allocation regime of the Use array
70 /// when it is a prefix to the User object, and the number of Use objects is
71 /// only known at allocation time.
73 template <unsigned MINARITY = 0>
74 struct VariadicOperandTraits {
75 static Use *op_begin(User* U) {
76 return reinterpret_cast<Use*>(U) - U->getNumOperands();
78 static Use *op_end(User* U) {
79 return reinterpret_cast<Use*>(U);
81 static unsigned operands(const User *U) {
82 return U->getNumOperands();
84 static inline void *allocate(unsigned); // FIXME
87 //===----------------------------------------------------------------------===//
88 // HungoffOperand Trait Class
89 //===----------------------------------------------------------------------===//
91 /// HungoffOperandTraits - determine the allocation regime of the Use array
92 /// when it is not a prefix to the User object, but allocated at an unrelated
94 /// Assumes that the User subclass that is determined by this traits class
95 /// has an OperandList member of type User::op_iterator. [Note: this is now
96 /// trivially satisfied, because User has that member for historic reasons.]
98 /// This is the traits class that is needed when the Use array must be
101 template <unsigned MINARITY = 1>
102 struct HungoffOperandTraits {
103 static Use *op_begin(User* U) {
104 return U->OperandList;
106 static Use *op_end(User* U) {
107 return U->OperandList + U->getNumOperands();
109 static unsigned operands(const User *U) {
110 return U->getNumOperands();
112 static inline void *allocate(unsigned); // FIXME
115 /// Macro for generating in-class operand accessor declarations.
116 /// It should only be called in the public section of the interface.
118 #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
120 inline VALUECLASS *getOperand(unsigned) const; \
121 inline void setOperand(unsigned, VALUECLASS*); \
123 template <unsigned> inline Use &Op(); \
124 template <unsigned> inline const Use &Op() const; \
126 inline unsigned getNumOperands() const
128 /// Macro for generating out-of-class operand accessor definitions
129 #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
130 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
131 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
132 && "getOperand() out of range!"); \
133 return static_cast<VALUECLASS*>( \
134 OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
136 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
137 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
138 && "setOperand() out of range!"); \
139 OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
141 unsigned CLASS::getNumOperands() const { \
142 return OperandTraits<CLASS>::operands(this); \
144 template <unsigned Idx_nocapture> Use &CLASS::Op() { \
145 return OperandTraits<CLASS>::op_begin(this)[Idx_nocapture]; \
147 template <unsigned Idx_nocapture> const Use &CLASS::Op() const { \
148 return OperandTraits<CLASS>::op_begin( \
149 const_cast<CLASS*>(this))[Idx_nocapture]; \
153 /// Macro for generating out-of-class operand accessor
154 /// definitions with casted result
155 #define DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
156 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
157 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
158 && "getOperand() out of range!"); \
159 return cast<VALUECLASS>( \
160 OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
162 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
163 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
164 && "setOperand() out of range!"); \
165 OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
167 unsigned CLASS::getNumOperands() const { \
168 return OperandTraits<CLASS>::operands(this); \
170 template <unsigned Idx_nocapture> Use &CLASS::Op() { \
171 return OperandTraits<CLASS>::op_begin(this)[Idx_nocapture]; \
173 template <unsigned Idx_nocapture> const Use &CLASS::Op() const { \
174 return OperandTraits<CLASS>::op_begin( \
175 const_cast<CLASS*>(this))[Idx_nocapture]; \
179 } // End llvm namespace