@@ -113,75 +113,38 @@ impl<'tcx> TypeFoldable<'tcx> for LvalueTy<'tcx> {
113113 }
114114}
115115
116- impl < ' a , ' gcx , ' tcx > Mir < ' tcx > {
117- pub fn operand_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
118- operand : & Operand < ' tcx > )
119- -> Ty < ' tcx >
120- {
121- match * operand {
122- Operand :: Consume ( ref l) => self . lvalue_ty ( tcx, l) . to_ty ( tcx) ,
123- Operand :: Constant ( ref c) => c. ty ,
124- }
125- }
126-
127- pub fn binop_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
128- op : BinOp ,
129- lhs_ty : Ty < ' tcx > ,
130- rhs_ty : Ty < ' tcx > )
131- -> Ty < ' tcx >
132- {
133- // FIXME: handle SIMD correctly
134- match op {
135- BinOp :: Add | BinOp :: Sub | BinOp :: Mul | BinOp :: Div | BinOp :: Rem |
136- BinOp :: BitXor | BinOp :: BitAnd | BinOp :: BitOr => {
137- // these should be integers or floats of the same size.
138- assert_eq ! ( lhs_ty, rhs_ty) ;
139- lhs_ty
140- }
141- BinOp :: Shl | BinOp :: Shr => {
142- lhs_ty // lhs_ty can be != rhs_ty
143- }
144- BinOp :: Eq | BinOp :: Lt | BinOp :: Le |
145- BinOp :: Ne | BinOp :: Ge | BinOp :: Gt => {
146- tcx. types . bool
147- }
148- }
149- }
150-
151- pub fn lvalue_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
152- lvalue : & Lvalue < ' tcx > )
153- -> LvalueTy < ' tcx >
154- {
155- match * lvalue {
156- Lvalue :: Var ( index) =>
157- LvalueTy :: Ty { ty : self . var_decls [ index] . ty } ,
158- Lvalue :: Temp ( index) =>
159- LvalueTy :: Ty { ty : self . temp_decls [ index] . ty } ,
160- Lvalue :: Arg ( index) =>
161- LvalueTy :: Ty { ty : self . arg_decls [ index] . ty } ,
162- Lvalue :: Static ( def_id) =>
116+ impl < ' tcx > Lvalue < ' tcx > {
117+ pub fn ty < ' a , ' gcx > ( & self , mir : & Mir < ' tcx > , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> LvalueTy < ' tcx > {
118+ match self {
119+ & Lvalue :: Var ( index) =>
120+ LvalueTy :: Ty { ty : mir. var_decls [ index] . ty } ,
121+ & Lvalue :: Temp ( index) =>
122+ LvalueTy :: Ty { ty : mir. temp_decls [ index] . ty } ,
123+ & Lvalue :: Arg ( index) =>
124+ LvalueTy :: Ty { ty : mir. arg_decls [ index] . ty } ,
125+ & Lvalue :: Static ( def_id) =>
163126 LvalueTy :: Ty { ty : tcx. lookup_item_type ( def_id) . ty } ,
164- Lvalue :: ReturnPointer =>
165- LvalueTy :: Ty { ty : self . return_ty . unwrap ( ) } ,
166- Lvalue :: Projection ( ref proj) =>
167- self . lvalue_ty ( tcx , & proj . base ) . projection_ty ( tcx, & proj. elem )
127+ & Lvalue :: ReturnPointer =>
128+ LvalueTy :: Ty { ty : mir . return_ty . unwrap ( ) } ,
129+ & Lvalue :: Projection ( ref proj) =>
130+ proj . base . ty ( mir , tcx ) . projection_ty ( tcx, & proj. elem ) ,
168131 }
169132 }
133+ }
170134
171- pub fn rvalue_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
172- rvalue : & Rvalue < ' tcx > )
173- -> Option < Ty < ' tcx > >
135+ impl < ' tcx > Rvalue < ' tcx > {
136+ pub fn ty < ' a , ' gcx > ( & self , mir : & Mir < ' tcx > , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Option < Ty < ' tcx > >
174137 {
175- match * rvalue {
176- Rvalue :: Use ( ref operand) => Some ( self . operand_ty ( tcx , operand ) ) ,
177- Rvalue :: Repeat ( ref operand, ref count) => {
178- let op_ty = self . operand_ty ( tcx , operand ) ;
138+ match self {
139+ & Rvalue :: Use ( ref operand) => Some ( operand . ty ( mir , tcx ) ) ,
140+ & Rvalue :: Repeat ( ref operand, ref count) => {
141+ let op_ty = operand . ty ( mir , tcx ) ;
179142 let count = count. value . as_u64 ( tcx. sess . target . uint_type ) ;
180143 assert_eq ! ( count as usize as u64 , count) ;
181144 Some ( tcx. mk_array ( op_ty, count as usize ) )
182145 }
183- Rvalue :: Ref ( reg, bk, ref lv) => {
184- let lv_ty = self . lvalue_ty ( tcx , lv ) . to_ty ( tcx) ;
146+ & Rvalue :: Ref ( reg, bk, ref lv) => {
147+ let lv_ty = lv . ty ( mir , tcx ) . to_ty ( tcx) ;
185148 Some ( tcx. mk_ref (
186149 tcx. mk_region ( reg) ,
187150 ty:: TypeAndMut {
@@ -190,39 +153,39 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
190153 }
191154 ) )
192155 }
193- Rvalue :: Len ( ..) => Some ( tcx. types . usize ) ,
194- Rvalue :: Cast ( _, _, ty) => Some ( ty) ,
195- Rvalue :: BinaryOp ( op, ref lhs, ref rhs) => {
196- let lhs_ty = self . operand_ty ( tcx , lhs ) ;
197- let rhs_ty = self . operand_ty ( tcx , rhs ) ;
198- Some ( self . binop_ty ( tcx, op , lhs_ty, rhs_ty) )
156+ & Rvalue :: Len ( ..) => Some ( tcx. types . usize ) ,
157+ & Rvalue :: Cast ( _, _, ty) => Some ( ty) ,
158+ & Rvalue :: BinaryOp ( op, ref lhs, ref rhs) => {
159+ let lhs_ty = lhs . ty ( mir , tcx ) ;
160+ let rhs_ty = rhs . ty ( mir , tcx ) ;
161+ Some ( op . ty ( tcx, lhs_ty, rhs_ty) )
199162 }
200- Rvalue :: CheckedBinaryOp ( op, ref lhs, ref rhs) => {
201- let lhs_ty = self . operand_ty ( tcx , lhs ) ;
202- let rhs_ty = self . operand_ty ( tcx , rhs ) ;
203- let ty = self . binop_ty ( tcx, op , lhs_ty, rhs_ty) ;
163+ & Rvalue :: CheckedBinaryOp ( op, ref lhs, ref rhs) => {
164+ let lhs_ty = lhs . ty ( mir , tcx ) ;
165+ let rhs_ty = rhs . ty ( mir , tcx ) ;
166+ let ty = op . ty ( tcx, lhs_ty, rhs_ty) ;
204167 let ty = tcx. mk_tup ( vec ! [ ty, tcx. types. bool ] ) ;
205168 Some ( ty)
206169 }
207- Rvalue :: UnaryOp ( _, ref operand) => {
208- Some ( self . operand_ty ( tcx , operand ) )
170+ & Rvalue :: UnaryOp ( _, ref operand) => {
171+ Some ( operand . ty ( mir , tcx ) )
209172 }
210- Rvalue :: Box ( t) => {
173+ & Rvalue :: Box ( t) => {
211174 Some ( tcx. mk_box ( t) )
212175 }
213- Rvalue :: Aggregate ( ref ak, ref ops) => {
176+ & Rvalue :: Aggregate ( ref ak, ref ops) => {
214177 match * ak {
215178 AggregateKind :: Vec => {
216179 if let Some ( operand) = ops. get ( 0 ) {
217- let ty = self . operand_ty ( tcx , operand ) ;
180+ let ty = operand . ty ( mir , tcx ) ;
218181 Some ( tcx. mk_array ( ty, ops. len ( ) ) )
219182 } else {
220183 None
221184 }
222185 }
223186 AggregateKind :: Tuple => {
224187 Some ( tcx. mk_tup (
225- ops. iter ( ) . map ( |op| self . operand_ty ( tcx , op ) ) . collect ( )
188+ ops. iter ( ) . map ( |op| op . ty ( mir , tcx ) ) . collect ( )
226189 ) )
227190 }
228191 AggregateKind :: Adt ( def, _, substs) => {
@@ -233,7 +196,40 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
233196 }
234197 }
235198 }
236- Rvalue :: InlineAsm { .. } => None
199+ & Rvalue :: InlineAsm { .. } => None
200+ }
201+ }
202+ }
203+
204+ impl < ' tcx > Operand < ' tcx > {
205+ pub fn ty < ' a , ' gcx > ( & self , mir : & Mir < ' tcx > , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Ty < ' tcx > {
206+ match self {
207+ & Operand :: Consume ( ref l) => l. ty ( mir, tcx) . to_ty ( tcx) ,
208+ & Operand :: Constant ( ref c) => c. ty ,
209+ }
210+ }
211+ }
212+
213+ impl < ' tcx > BinOp {
214+ pub fn ty < ' a , ' gcx > ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
215+ lhs_ty : Ty < ' tcx > ,
216+ rhs_ty : Ty < ' tcx > )
217+ -> Ty < ' tcx > {
218+ // FIXME: handle SIMD correctly
219+ match self {
220+ & BinOp :: Add | & BinOp :: Sub | & BinOp :: Mul | & BinOp :: Div | & BinOp :: Rem |
221+ & BinOp :: BitXor | & BinOp :: BitAnd | & BinOp :: BitOr => {
222+ // these should be integers or floats of the same size.
223+ assert_eq ! ( lhs_ty, rhs_ty) ;
224+ lhs_ty
225+ }
226+ & BinOp :: Shl | & BinOp :: Shr => {
227+ lhs_ty // lhs_ty can be != rhs_ty
228+ }
229+ & BinOp :: Eq | & BinOp :: Lt | & BinOp :: Le |
230+ & BinOp :: Ne | & BinOp :: Ge | & BinOp :: Gt => {
231+ tcx. types . bool
232+ }
237233 }
238234 }
239235}
0 commit comments