/
tcx.rs
242 lines (228 loc) · 8.55 KB
/
tcx.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
* Methods for the various MIR types. These are intended for use after
* building is complete.
*/
use mir::repr::*;
use middle::const_eval::ConstVal;
use middle::subst::{Subst, Substs};
use middle::ty::{self, AdtDef, Ty};
use rustc_front::hir;
#[derive(Copy, Clone, Debug)]
pub enum LvalueTy<'tcx> {
/// Normal type.
Ty { ty: Ty<'tcx> },
/// Downcast to a particular variant of an enum.
Downcast { adt_def: AdtDef<'tcx>,
substs: &'tcx Substs<'tcx>,
variant_index: usize },
}
impl<'tcx> LvalueTy<'tcx> {
pub fn from_ty(ty: Ty<'tcx>) -> LvalueTy<'tcx> {
LvalueTy::Ty { ty: ty }
}
pub fn to_ty(&self, tcx: &ty::ctxt<'tcx>) -> Ty<'tcx> {
match *self {
LvalueTy::Ty { ty } =>
ty,
LvalueTy::Downcast { adt_def, substs, variant_index: _ } =>
tcx.mk_enum(adt_def, substs),
}
}
pub fn projection_ty(self,
tcx: &ty::ctxt<'tcx>,
elem: &LvalueElem<'tcx>)
-> LvalueTy<'tcx>
{
match *elem {
ProjectionElem::Deref =>
LvalueTy::Ty {
ty: self.to_ty(tcx).builtin_deref(true, ty::LvaluePreference::NoPreference)
.unwrap()
.ty
},
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } =>
LvalueTy::Ty {
ty: self.to_ty(tcx).builtin_index().unwrap()
},
ProjectionElem::Downcast(adt_def1, index) =>
match self.to_ty(tcx).sty {
ty::TyEnum(adt_def, substs) => {
assert!(index < adt_def.variants.len());
assert_eq!(adt_def, adt_def1);
LvalueTy::Downcast { adt_def: adt_def,
substs: substs,
variant_index: index }
}
_ => {
tcx.sess.bug(&format!("cannot downcast non-enum type: `{:?}`", self))
}
},
ProjectionElem::Field(_, fty) => LvalueTy::Ty { ty: fty }
}
}
}
impl<'tcx> Mir<'tcx> {
pub fn operand_ty(&self,
tcx: &ty::ctxt<'tcx>,
operand: &Operand<'tcx>)
-> Ty<'tcx>
{
match *operand {
Operand::Consume(ref l) => self.lvalue_ty(tcx, l).to_ty(tcx),
Operand::Constant(ref c) => c.ty,
}
}
pub fn binop_ty(&self,
tcx: &ty::ctxt<'tcx>,
op: BinOp,
lhs_ty: Ty<'tcx>,
rhs_ty: Ty<'tcx>)
-> Ty<'tcx>
{
// FIXME: handle SIMD correctly
match op {
BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Rem |
BinOp::BitXor | BinOp::BitAnd | BinOp::BitOr => {
// these should be integers or floats of the same size.
assert_eq!(lhs_ty, rhs_ty);
lhs_ty
}
BinOp::Shl | BinOp::Shr => {
lhs_ty // lhs_ty can be != rhs_ty
}
BinOp::Eq | BinOp::Lt | BinOp::Le |
BinOp::Ne | BinOp::Ge | BinOp::Gt => {
tcx.types.bool
}
}
}
pub fn lvalue_ty(&self,
tcx: &ty::ctxt<'tcx>,
lvalue: &Lvalue<'tcx>)
-> LvalueTy<'tcx>
{
match *lvalue {
Lvalue::Var(index) =>
LvalueTy::Ty { ty: self.var_decls[index as usize].ty },
Lvalue::Temp(index) =>
LvalueTy::Ty { ty: self.temp_decls[index as usize].ty },
Lvalue::Arg(index) =>
LvalueTy::Ty { ty: self.arg_decls[index as usize].ty },
Lvalue::Static(def_id) =>
LvalueTy::Ty { ty: tcx.lookup_item_type(def_id).ty },
Lvalue::ReturnPointer =>
LvalueTy::Ty { ty: self.return_ty.unwrap() },
Lvalue::Projection(ref proj) =>
self.lvalue_ty(tcx, &proj.base).projection_ty(tcx, &proj.elem)
}
}
pub fn rvalue_ty(&self,
tcx: &ty::ctxt<'tcx>,
rvalue: &Rvalue<'tcx>)
-> Option<Ty<'tcx>>
{
match *rvalue {
Rvalue::Use(ref operand) => Some(self.operand_ty(tcx, operand)),
Rvalue::Repeat(ref operand, ref count) => {
if let ConstVal::Uint(u) = count.value {
let op_ty = self.operand_ty(tcx, operand);
Some(tcx.mk_array(op_ty, u as usize))
} else {
None
}
}
Rvalue::Ref(reg, bk, ref lv) => {
let lv_ty = self.lvalue_ty(tcx, lv).to_ty(tcx);
Some(tcx.mk_ref(
tcx.mk_region(reg),
ty::TypeAndMut {
ty: lv_ty,
mutbl: bk.to_mutbl_lossy()
}
))
}
Rvalue::Len(..) => Some(tcx.types.usize),
Rvalue::Cast(_, _, ty) => Some(ty),
Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
let lhs_ty = self.operand_ty(tcx, lhs);
let rhs_ty = self.operand_ty(tcx, rhs);
Some(self.binop_ty(tcx, op, lhs_ty, rhs_ty))
}
Rvalue::UnaryOp(_, ref operand) => {
Some(self.operand_ty(tcx, operand))
}
Rvalue::Box(t) => {
Some(tcx.mk_box(t))
}
Rvalue::Aggregate(ref ak, ref ops) => {
match *ak {
AggregateKind::Vec => {
if let Some(operand) = ops.get(0) {
let ty = self.operand_ty(tcx, operand);
Some(tcx.mk_array(ty, ops.len()))
} else {
None
}
}
AggregateKind::Tuple => {
Some(tcx.mk_tup(
ops.iter().map(|op| self.operand_ty(tcx, op)).collect()
))
}
AggregateKind::Adt(def, _, substs) => {
Some(def.type_scheme(tcx).ty.subst(tcx, substs))
}
AggregateKind::Closure(did, substs) => {
Some(tcx.mk_closure_from_closure_substs(
did, Box::new(substs.clone())))
}
}
}
Rvalue::Slice { .. } => None,
Rvalue::InlineAsm(..) => None
}
}
}
impl BorrowKind {
pub fn to_mutbl_lossy(self) -> hir::Mutability {
match self {
BorrowKind::Mut => hir::MutMutable,
BorrowKind::Shared => hir::MutImmutable,
// We have no type corresponding to a unique imm borrow, so
// use `&mut`. It gives all the capabilities of an `&uniq`
// and hence is a safe "over approximation".
BorrowKind::Unique => hir::MutMutable,
}
}
}
impl BinOp {
pub fn to_hir_binop(self) -> hir::BinOp_ {
match self {
BinOp::Add => hir::BinOp_::BiAdd,
BinOp::Sub => hir::BinOp_::BiSub,
BinOp::Mul => hir::BinOp_::BiMul,
BinOp::Div => hir::BinOp_::BiDiv,
BinOp::Rem => hir::BinOp_::BiRem,
BinOp::BitXor => hir::BinOp_::BiBitXor,
BinOp::BitAnd => hir::BinOp_::BiBitAnd,
BinOp::BitOr => hir::BinOp_::BiBitOr,
BinOp::Shl => hir::BinOp_::BiShl,
BinOp::Shr => hir::BinOp_::BiShr,
BinOp::Eq => hir::BinOp_::BiEq,
BinOp::Ne => hir::BinOp_::BiNe,
BinOp::Lt => hir::BinOp_::BiLt,
BinOp::Gt => hir::BinOp_::BiGt,
BinOp::Le => hir::BinOp_::BiLe,
BinOp::Ge => hir::BinOp_::BiGe
}
}
}