LLVM API Documentation
00001 //===-- llvm/OperandTraits.h - OperandTraits class definition ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the traits classes that are handy for enforcing the correct 00011 // layout of various User subclasses. It also provides the means for accessing 00012 // the operands in the most efficient manner. 00013 // 00014 00015 #ifndef LLVM_OPERAND_TRAITS_H 00016 #define LLVM_OPERAND_TRAITS_H 00017 00018 #include "llvm/User.h" 00019 00020 namespace llvm { 00021 00022 //===----------------------------------------------------------------------===// 00023 // FixedNumOperands Trait Class 00024 //===----------------------------------------------------------------------===// 00025 00026 /// FixedNumOperandTraits - determine the allocation regime of the Use array 00027 /// when it is a prefix to the User object, and the number of Use objects is 00028 /// known at compile time. 00029 00030 template <unsigned ARITY> 00031 struct FixedNumOperandTraits { 00032 static Use *op_begin(User* U) { 00033 return reinterpret_cast<Use*>(U) - ARITY; 00034 } 00035 static Use *op_end(User* U) { 00036 return reinterpret_cast<Use*>(U); 00037 } 00038 static unsigned operands(const User*) { 00039 return ARITY; 00040 } 00041 struct prefix { 00042 Use Ops[ARITY]; 00043 prefix(); // DO NOT IMPLEMENT 00044 }; 00045 template <class U> 00046 struct Layout { 00047 struct overlay : prefix, U { 00048 overlay(); // DO NOT IMPLEMENT 00049 }; 00050 }; 00051 }; 00052 00053 //===----------------------------------------------------------------------===// 00054 // OptionalOperands Trait Class 00055 //===----------------------------------------------------------------------===// 00056 00057 template <unsigned ARITY = 1> 00058 struct OptionalOperandTraits : FixedNumOperandTraits<ARITY> { 00059 static unsigned operands(const User *U) { 00060 return U->getNumOperands(); 00061 } 00062 }; 00063 00064 //===----------------------------------------------------------------------===// 00065 // VariadicOperand Trait Class 00066 //===----------------------------------------------------------------------===// 00067 00068 /// VariadicOperandTraits - determine the allocation regime of the Use array 00069 /// when it is a prefix to the User object, and the number of Use objects is 00070 /// only known at allocation time. 00071 00072 template <unsigned MINARITY = 0> 00073 struct VariadicOperandTraits { 00074 static Use *op_begin(User* U) { 00075 return reinterpret_cast<Use*>(U) - U->getNumOperands(); 00076 } 00077 static Use *op_end(User* U) { 00078 return reinterpret_cast<Use*>(U); 00079 } 00080 static unsigned operands(const User *U) { 00081 return U->getNumOperands(); 00082 } 00083 }; 00084 00085 //===----------------------------------------------------------------------===// 00086 // HungoffOperand Trait Class 00087 //===----------------------------------------------------------------------===// 00088 00089 /// HungoffOperandTraits - determine the allocation regime of the Use array 00090 /// when it is not a prefix to the User object, but allocated at an unrelated 00091 /// heap address. 00092 /// Assumes that the User subclass that is determined by this traits class 00093 /// has an OperandList member of type User::op_iterator. [Note: this is now 00094 /// trivially satisfied, because User has that member for historic reasons.] 00095 /// 00096 /// This is the traits class that is needed when the Use array must be 00097 /// resizable. 00098 00099 template <unsigned MINARITY = 1> 00100 struct HungoffOperandTraits { 00101 static Use *op_begin(User* U) { 00102 return U->OperandList; 00103 } 00104 static Use *op_end(User* U) { 00105 return U->OperandList + U->getNumOperands(); 00106 } 00107 static unsigned operands(const User *U) { 00108 return U->getNumOperands(); 00109 } 00110 }; 00111 00112 /// Macro for generating in-class operand accessor declarations. 00113 /// It should only be called in the public section of the interface. 00114 /// 00115 #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \ 00116 public: \ 00117 inline VALUECLASS *getOperand(unsigned) const; \ 00118 inline void setOperand(unsigned, VALUECLASS*); \ 00119 inline op_iterator op_begin(); \ 00120 inline const_op_iterator op_begin() const; \ 00121 inline op_iterator op_end(); \ 00122 inline const_op_iterator op_end() const; \ 00123 protected: \ 00124 template <int> inline Use &Op(); \ 00125 template <int> inline const Use &Op() const; \ 00126 public: \ 00127 inline unsigned getNumOperands() const 00128 00129 /// Macro for generating out-of-class operand accessor definitions 00130 #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \ 00131 CLASS::op_iterator CLASS::op_begin() { \ 00132 return OperandTraits<CLASS>::op_begin(this); \ 00133 } \ 00134 CLASS::const_op_iterator CLASS::op_begin() const { \ 00135 return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \ 00136 } \ 00137 CLASS::op_iterator CLASS::op_end() { \ 00138 return OperandTraits<CLASS>::op_end(this); \ 00139 } \ 00140 CLASS::const_op_iterator CLASS::op_end() const { \ 00141 return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \ 00142 } \ 00143 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \ 00144 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \ 00145 && "getOperand() out of range!"); \ 00146 return static_cast<VALUECLASS*>( \ 00147 OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \ 00148 } \ 00149 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \ 00150 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \ 00151 && "setOperand() out of range!"); \ 00152 OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \ 00153 } \ 00154 unsigned CLASS::getNumOperands() const { \ 00155 return OperandTraits<CLASS>::operands(this); \ 00156 } \ 00157 template <int Idx_nocapture> Use &CLASS::Op() { \ 00158 return this->OpFrom<Idx_nocapture>(this); \ 00159 } \ 00160 template <int Idx_nocapture> const Use &CLASS::Op() const { \ 00161 return this->OpFrom<Idx_nocapture>(this); \ 00162 } 00163 00164 00165 /// Macro for generating out-of-class operand accessor 00166 /// definitions with casted result 00167 #define DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(CLASS, VALUECLASS) \ 00168 CLASS::op_iterator CLASS::op_begin() { \ 00169 return OperandTraits<CLASS>::op_begin(this); \ 00170 } \ 00171 CLASS::const_op_iterator CLASS::op_begin() const { \ 00172 return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \ 00173 } \ 00174 CLASS::op_iterator CLASS::op_end() { \ 00175 return OperandTraits<CLASS>::op_end(this); \ 00176 } \ 00177 CLASS::const_op_iterator CLASS::op_end() const { \ 00178 return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \ 00179 } \ 00180 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \ 00181 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \ 00182 && "getOperand() out of range!"); \ 00183 return cast<VALUECLASS>( \ 00184 OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \ 00185 } \ 00186 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \ 00187 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \ 00188 && "setOperand() out of range!"); \ 00189 OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \ 00190 } \ 00191 unsigned CLASS::getNumOperands() const { \ 00192 return OperandTraits<CLASS>::operands(this); \ 00193 } \ 00194 template <int Idx_nocapture> Use &CLASS::Op() { \ 00195 return this->OpFrom<Idx_nocapture>(this); \ 00196 } \ 00197 template <int Idx_nocapture> const Use &CLASS::Op() const { \ 00198 return this->OpFrom<Idx_nocapture>(this); \ 00199 } 00200 00201 00202 } // End llvm namespace 00203 00204 #endif