Skip to content

Commit addc653

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
begin implementing mir-typeck
1 parent 15611f7 commit addc653

File tree

6 files changed

+423
-8
lines changed

6 files changed

+423
-8
lines changed

src/librustc/mir/repr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub struct Mir<'tcx> {
4545
/// values in that it is possible to borrow them and mutate them
4646
/// through the resulting reference.
4747
pub temp_decls: Vec<TempDecl<'tcx>>,
48+
49+
/// A span representing this MIR, for error reporting
50+
pub span: Span,
4851
}
4952

5053
/// where execution begins
@@ -145,7 +148,7 @@ pub enum BorrowKind {
145148

146149
/// A "variable" is a binding declared by the user as part of the fn
147150
/// decl, a let, etc.
148-
#[derive(Clone, RustcEncodable, RustcDecodable)]
151+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
149152
pub struct VarDecl<'tcx> {
150153
pub mutability: Mutability,
151154
pub name: Name,
@@ -154,7 +157,7 @@ pub struct VarDecl<'tcx> {
154157

155158
/// A "temp" is a temporary that we place on the stack. They are
156159
/// anonymous, always mutable, and have only a type.
157-
#[derive(Clone, RustcEncodable, RustcDecodable)]
160+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
158161
pub struct TempDecl<'tcx> {
159162
pub ty: Ty<'tcx>,
160163
}
@@ -170,7 +173,7 @@ pub struct TempDecl<'tcx> {
170173
///
171174
/// there is only one argument, of type `(i32, u32)`, but two bindings
172175
/// (`x` and `y`).
173-
#[derive(Clone, RustcEncodable, RustcDecodable)]
176+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
174177
pub struct ArgDecl<'tcx> {
175178
pub ty: Ty<'tcx>,
176179
}

src/librustc/mir/tcx.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515

1616
use mir::repr::*;
17-
use middle::subst::Substs;
17+
use middle::const_eval::ConstVal;
18+
use middle::subst::{Subst, Substs};
1819
use middle::ty::{self, AdtDef, Ty};
1920
use rustc_front::hir;
2021

@@ -150,6 +151,73 @@ impl<'tcx> Mir<'tcx> {
150151
self.lvalue_ty(tcx, &proj.base).projection_ty(tcx, &proj.elem)
151152
}
152153
}
154+
155+
pub fn rvalue_ty(&self,
156+
tcx: &ty::ctxt<'tcx>,
157+
rvalue: &Rvalue<'tcx>)
158+
-> Option<Ty<'tcx>>
159+
{
160+
match *rvalue {
161+
Rvalue::Use(ref operand) => Some(self.operand_ty(tcx, operand)),
162+
Rvalue::Repeat(ref operand, ref count) => {
163+
if let ConstVal::Uint(u) = count.value {
164+
let op_ty = self.operand_ty(tcx, operand);
165+
Some(tcx.mk_array(op_ty, u as usize))
166+
} else {
167+
None
168+
}
169+
}
170+
Rvalue::Ref(reg, bk, ref lv) => {
171+
let lv_ty = self.lvalue_ty(tcx, lv).to_ty(tcx);
172+
Some(tcx.mk_ref(
173+
tcx.mk_region(reg),
174+
ty::TypeAndMut {
175+
ty: lv_ty,
176+
mutbl: bk.to_mutbl_lossy()
177+
}
178+
))
179+
}
180+
Rvalue::Len(..) => Some(tcx.types.usize),
181+
Rvalue::Cast(_, _, ty) => Some(ty),
182+
Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
183+
let lhs_ty = self.operand_ty(tcx, lhs);
184+
let rhs_ty = self.operand_ty(tcx, rhs);
185+
Some(self.binop_ty(tcx, op, lhs_ty, rhs_ty))
186+
}
187+
Rvalue::UnaryOp(_, ref operand) => {
188+
Some(self.operand_ty(tcx, operand))
189+
}
190+
Rvalue::Box(t) => {
191+
Some(tcx.mk_box(t))
192+
}
193+
Rvalue::Aggregate(ref ak, ref ops) => {
194+
match *ak {
195+
AggregateKind::Vec => {
196+
if let Some(operand) = ops.get(0) {
197+
let ty = self.operand_ty(tcx, operand);
198+
Some(tcx.mk_array(ty, ops.len()))
199+
} else {
200+
None
201+
}
202+
}
203+
AggregateKind::Tuple => {
204+
Some(tcx.mk_tup(
205+
ops.iter().map(|op| self.operand_ty(tcx, op)).collect()
206+
))
207+
}
208+
AggregateKind::Adt(def, _, substs) => {
209+
Some(def.type_scheme(tcx).ty.subst(tcx, substs))
210+
}
211+
AggregateKind::Closure(did, substs) => {
212+
Some(tcx.mk_closure_from_closure_substs(
213+
did, Box::new(substs.clone())))
214+
}
215+
}
216+
}
217+
Rvalue::Slice { .. } => None,
218+
Rvalue::InlineAsm(..) => None
219+
}
220+
}
153221
}
154222

155223
impl BorrowKind {

src/librustc_mir/build/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ macro_rules! unpack {
8080
/// the main entry point for building MIR for a function
8181
8282
pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
83-
_span: Span,
83+
span: Span,
8484
implicit_arguments: Vec<Ty<'tcx>>,
8585
explicit_arguments: Vec<(Ty<'tcx>, &'tcx hir::Pat)>,
8686
argument_extent: CodeExtent,
@@ -97,7 +97,7 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
9797
temp_decls: vec![],
9898
var_decls: vec![],
9999
var_indices: FnvHashMap(),
100-
unit_temp: None
100+
unit_temp: None,
101101
};
102102

103103
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
@@ -119,6 +119,7 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
119119
arg_decls: arg_decls,
120120
temp_decls: builder.temp_decls,
121121
return_ty: return_ty,
122+
span: span
122123
}
123124
}
124125

src/librustc_mir/mir_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern crate rustc_front;
2222
use build;
2323
use graphviz;
2424
use pretty;
25-
use transform::{simplify_cfg, no_landing_pads};
25+
use transform::{simplify_cfg, type_check, no_landing_pads};
2626
use rustc::dep_graph::DepNode;
2727
use rustc::mir::repr::Mir;
2828
use hair::cx::Cx;
@@ -148,8 +148,9 @@ impl<'a, 'm, 'tcx> Visitor<'tcx> for InnerDump<'a,'m,'tcx> {
148148

149149
match build_mir(Cx::new(&infcx), implicit_arg_tys, id, span, decl, body) {
150150
Ok(mut mir) => {
151-
no_landing_pads::NoLandingPads.run_on_mir(&mut mir, self.tcx);
152151
simplify_cfg::SimplifyCfg::new().run_on_mir(&mut mir, self.tcx);
152+
type_check::TypeckMir::new(&infcx).run_on_mir(&mut mir, self.tcx);
153+
no_landing_pads::NoLandingPads.run_on_mir(&mut mir, self.tcx);
153154

154155
let meta_item_list = self.attr
155156
.iter()

src/librustc_mir/transform/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
pub mod simplify_cfg;
1212
pub mod erase_regions;
1313
pub mod no_landing_pads;
14+
pub mod type_check;
1415
mod util;

0 commit comments

Comments
 (0)