@@ -113,55 +113,38 @@ impl<'tcx> TypeFoldable<'tcx> for LvalueTy<'tcx> {
113
113
}
114
114
}
115
115
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) => l. ty ( self , tcx) . 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
- }
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) =>
126
+ LvalueTy :: Ty { ty : tcx. lookup_item_type ( def_id) . ty } ,
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 ) ,
148
131
}
149
132
}
133
+ }
150
134
151
- pub fn rvalue_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
152
- rvalue : & Rvalue < ' tcx > )
153
- -> 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 > >
154
137
{
155
- match * rvalue {
156
- Rvalue :: Use ( ref operand) => Some ( self . operand_ty ( tcx , operand ) ) ,
157
- Rvalue :: Repeat ( ref operand, ref count) => {
158
- 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 ) ;
159
142
let count = count. value . as_u64 ( tcx. sess . target . uint_type ) ;
160
143
assert_eq ! ( count as usize as u64 , count) ;
161
144
Some ( tcx. mk_array ( op_ty, count as usize ) )
162
145
}
163
- Rvalue :: Ref ( reg, bk, ref lv) => {
164
- let lv_ty = lv. ty ( self , tcx) . to_ty ( tcx) ;
146
+ & Rvalue :: Ref ( reg, bk, ref lv) => {
147
+ let lv_ty = lv. ty ( mir , tcx) . to_ty ( tcx) ;
165
148
Some ( tcx. mk_ref (
166
149
tcx. mk_region ( reg) ,
167
150
ty:: TypeAndMut {
@@ -170,39 +153,39 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
170
153
}
171
154
) )
172
155
}
173
- Rvalue :: Len ( ..) => Some ( tcx. types . usize ) ,
174
- Rvalue :: Cast ( _, _, ty) => Some ( ty) ,
175
- Rvalue :: BinaryOp ( op, ref lhs, ref rhs) => {
176
- let lhs_ty = self . operand_ty ( tcx , lhs ) ;
177
- let rhs_ty = self . operand_ty ( tcx , rhs ) ;
178
- 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) )
179
162
}
180
- Rvalue :: CheckedBinaryOp ( op, ref lhs, ref rhs) => {
181
- let lhs_ty = self . operand_ty ( tcx , lhs ) ;
182
- let rhs_ty = self . operand_ty ( tcx , rhs ) ;
183
- 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) ;
184
167
let ty = tcx. mk_tup ( vec ! [ ty, tcx. types. bool ] ) ;
185
168
Some ( ty)
186
169
}
187
- Rvalue :: UnaryOp ( _, ref operand) => {
188
- Some ( self . operand_ty ( tcx , operand ) )
170
+ & Rvalue :: UnaryOp ( _, ref operand) => {
171
+ Some ( operand . ty ( mir , tcx ) )
189
172
}
190
- Rvalue :: Box ( t) => {
173
+ & Rvalue :: Box ( t) => {
191
174
Some ( tcx. mk_box ( t) )
192
175
}
193
- Rvalue :: Aggregate ( ref ak, ref ops) => {
176
+ & Rvalue :: Aggregate ( ref ak, ref ops) => {
194
177
match * ak {
195
178
AggregateKind :: Vec => {
196
179
if let Some ( operand) = ops. get ( 0 ) {
197
- let ty = self . operand_ty ( tcx , operand ) ;
180
+ let ty = operand . ty ( mir , tcx ) ;
198
181
Some ( tcx. mk_array ( ty, ops. len ( ) ) )
199
182
} else {
200
183
None
201
184
}
202
185
}
203
186
AggregateKind :: Tuple => {
204
187
Some ( tcx. mk_tup (
205
- ops. iter ( ) . map ( |op| self . operand_ty ( tcx , op ) ) . collect ( )
188
+ ops. iter ( ) . map ( |op| op . ty ( mir , tcx ) ) . collect ( )
206
189
) )
207
190
}
208
191
AggregateKind :: Adt ( def, _, substs) => {
@@ -213,11 +196,45 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
213
196
}
214
197
}
215
198
}
216
- Rvalue :: InlineAsm { .. } => None
199
+ & Rvalue :: InlineAsm { .. } => None
217
200
}
218
201
}
219
202
}
220
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
+ {
219
+ // FIXME: handle SIMD correctly
220
+ match self {
221
+ & BinOp :: Add | & BinOp :: Sub | & BinOp :: Mul | & BinOp :: Div | & BinOp :: Rem |
222
+ & BinOp :: BitXor | & BinOp :: BitAnd | & BinOp :: BitOr => {
223
+ // these should be integers or floats of the same size.
224
+ assert_eq ! ( lhs_ty, rhs_ty) ;
225
+ lhs_ty
226
+ }
227
+ & BinOp :: Shl | & BinOp :: Shr => {
228
+ lhs_ty // lhs_ty can be != rhs_ty
229
+ }
230
+ & BinOp :: Eq | & BinOp :: Lt | & BinOp :: Le |
231
+ & BinOp :: Ne | & BinOp :: Ge | & BinOp :: Gt => {
232
+ tcx. types . bool
233
+ }
234
+ }
235
+ }
236
+ }
237
+
221
238
impl BorrowKind {
222
239
pub fn to_mutbl_lossy ( self ) -> hir:: Mutability {
223
240
match self {
0 commit comments