Skip to content

Commit 9bd35c0

Browse files
committed
add MIR code (unused thus far)
1 parent 0e764ec commit 9bd35c0

33 files changed

+5856
-12
lines changed

src/librustc/middle/region.rs

+3
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ impl RegionMaps {
329329
pub fn item_extent(&self, n: ast::NodeId) -> CodeExtent {
330330
self.lookup_code_extent(CodeExtentData::DestructionScope(n))
331331
}
332+
pub fn opt_destruction_extent(&self, n: ast::NodeId) -> Option<CodeExtent> {
333+
self.code_extent_interner.borrow().get(&CodeExtentData::DestructionScope(n)).cloned()
334+
}
332335
pub fn intern_code_extent(&self,
333336
e: CodeExtentData,
334337
parent: CodeExtent) -> CodeExtent {

src/librustc/middle/ty.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,13 @@ impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
34753475
.expect("variant_with_id: unknown variant")
34763476
}
34773477

3478+
pub fn variant_index_with_id(&self, vid: DefId) -> usize {
3479+
self.variants
3480+
.iter()
3481+
.position(|v| v.did == vid)
3482+
.expect("variant_index_with_id: unknown variant")
3483+
}
3484+
34783485
pub fn variant_of_def(&self, def: def::Def) -> &VariantDefData<'tcx, 'container> {
34793486
match def {
34803487
def::DefVariant(_, vid, _) => self.variant_with_id(vid),
@@ -5223,14 +5230,11 @@ impl<'tcx> TyS<'tcx> {
52235230
{
52245231
let method_call = MethodCall::autoderef(expr_id, autoderef);
52255232
let mut adjusted_ty = self;
5226-
match method_type(method_call) {
5227-
Some(method_ty) => {
5228-
// Method calls always have all late-bound regions
5229-
// fully instantiated.
5230-
let fn_ret = cx.no_late_bound_regions(&method_ty.fn_ret()).unwrap();
5231-
adjusted_ty = fn_ret.unwrap();
5232-
}
5233-
None => {}
5233+
if let Some(method_ty) = method_type(method_call) {
5234+
// Method calls always have all late-bound regions
5235+
// fully instantiated.
5236+
let fn_ret = cx.no_late_bound_regions(&method_ty.fn_ret()).unwrap();
5237+
adjusted_ty = fn_ret.unwrap();
52345238
}
52355239
match adjusted_ty.builtin_deref(true, NoPreference) {
52365240
Some(mt) => mt.ty,

src/librustc_mir/build/block.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use hair::*;
12+
use repr::*;
13+
use build::{BlockAnd, Builder};
14+
15+
impl<H:Hair> Builder<H> {
16+
pub fn ast_block(&mut self,
17+
destination: &Lvalue<H>,
18+
mut block: BasicBlock,
19+
ast_block: H::Block)
20+
-> BlockAnd<()> {
21+
let this = self;
22+
let Block { extent, span: _, stmts, expr } = this.hir.mirror(ast_block);
23+
this.in_scope(extent, block, |this| {
24+
unpack!(block = this.stmts(block, stmts));
25+
this.into(destination, block, expr)
26+
})
27+
}
28+
}

src/librustc_mir/build/cfg.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
13+
14+
//! Routines for manipulating the control-flow graph.
15+
16+
use build::CFG;
17+
use hair::*;
18+
use repr::*;
19+
20+
impl<H:Hair> CFG<H> {
21+
pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<H> {
22+
&self.basic_blocks[blk.index()]
23+
}
24+
25+
pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<H> {
26+
&mut self.basic_blocks[blk.index()]
27+
}
28+
29+
pub fn end_point(&self, block: BasicBlock) -> ExecutionPoint {
30+
ExecutionPoint {
31+
block: block,
32+
statement: self.block_data(block).statements.len() as u32
33+
}
34+
}
35+
36+
pub fn start_new_block(&mut self) -> BasicBlock {
37+
let node_index = self.basic_blocks.len();
38+
self.basic_blocks.push(BasicBlockData::new(Terminator::Diverge));
39+
BasicBlock::new(node_index)
40+
}
41+
42+
pub fn push(&mut self, block: BasicBlock, statement: Statement<H>) {
43+
debug!("push({:?}, {:?})", block, statement);
44+
self.block_data_mut(block).statements.push(statement);
45+
}
46+
47+
pub fn push_assign_constant(&mut self,
48+
block: BasicBlock,
49+
span: H::Span,
50+
temp: &Lvalue<H>,
51+
constant: Constant<H>) {
52+
self.push_assign(block, span, temp, Rvalue::Use(Operand::Constant(constant)));
53+
}
54+
55+
pub fn push_drop(&mut self, block: BasicBlock, span: H::Span,
56+
kind: DropKind, lvalue: &Lvalue<H>) {
57+
self.push(block, Statement {
58+
span: span,
59+
kind: StatementKind::Drop(kind, lvalue.clone())
60+
});
61+
}
62+
63+
pub fn push_assign(&mut self,
64+
block: BasicBlock,
65+
span: H::Span,
66+
lvalue: &Lvalue<H>,
67+
rvalue: Rvalue<H>) {
68+
self.push(block, Statement {
69+
span: span,
70+
kind: StatementKind::Assign(lvalue.clone(), rvalue)
71+
});
72+
}
73+
74+
pub fn terminate(&mut self,
75+
block: BasicBlock,
76+
terminator: Terminator<H>) {
77+
// Check whether this block has already been terminated. For
78+
// this, we rely on the fact that the initial state is to have
79+
// a Diverge terminator and an empty list of targets (which
80+
// is not a valid state).
81+
debug_assert!(match self.block_data(block).terminator { Terminator::Diverge => true,
82+
_ => false },
83+
"terminate: block {:?} already has a terminator set", block);
84+
85+
self.block_data_mut(block).terminator = terminator;
86+
}
87+
}
88+
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! See docs in build/expr/mod.rs
12+
13+
use rustc_data_structures::fnv::FnvHashMap;
14+
15+
use build::{Builder};
16+
use hair::*;
17+
use repr::*;
18+
19+
impl<H:Hair> Builder<H> {
20+
/// Compile `expr`, yielding a compile-time constant. Assumes that
21+
/// `expr` is a valid compile-time constant!
22+
pub fn as_constant<M>(&mut self, expr: M) -> Constant<H>
23+
where M: Mirror<H, Output=Expr<H>>
24+
{
25+
let expr = self.hir.mirror(expr);
26+
self.expr_as_constant(expr)
27+
}
28+
29+
fn expr_as_constant(&mut self, expr: Expr<H>) -> Constant<H> {
30+
let this = self;
31+
let Expr { ty: _, temp_lifetime: _, span, kind } = expr;
32+
let kind = match kind {
33+
ExprKind::Scope { extent: _, value } => {
34+
return this.as_constant(value);
35+
}
36+
ExprKind::Paren { arg } => {
37+
return this.as_constant(arg);
38+
}
39+
ExprKind::Literal { literal } => {
40+
ConstantKind::Literal(literal)
41+
}
42+
ExprKind::Vec { fields } => {
43+
let fields = this.as_constants(fields);
44+
ConstantKind::Aggregate(AggregateKind::Vec, fields)
45+
}
46+
ExprKind::Tuple { fields } => {
47+
let fields = this.as_constants(fields);
48+
ConstantKind::Aggregate(AggregateKind::Tuple, fields)
49+
}
50+
ExprKind::Adt { adt_def, variant_index, substs, fields, base: None } => {
51+
let field_names = this.hir.fields(adt_def, variant_index);
52+
let fields = this.named_field_constants(field_names, fields);
53+
ConstantKind::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs), fields)
54+
}
55+
ExprKind::Repeat { value, count } => {
56+
let value = Box::new(this.as_constant(value));
57+
let count = Box::new(this.as_constant(count));
58+
ConstantKind::Repeat(value, count)
59+
}
60+
ExprKind::Binary { op, lhs, rhs } => {
61+
let lhs = Box::new(this.as_constant(lhs));
62+
let rhs = Box::new(this.as_constant(rhs));
63+
ConstantKind::BinaryOp(op, lhs, rhs)
64+
}
65+
ExprKind::Unary { op, arg } => {
66+
let arg = Box::new(this.as_constant(arg));
67+
ConstantKind::UnaryOp(op, arg)
68+
}
69+
ExprKind::Field { lhs, name } => {
70+
let lhs = this.as_constant(lhs);
71+
ConstantKind::Projection(
72+
Box::new(ConstantProjection {
73+
base: lhs,
74+
elem: ProjectionElem::Field(name),
75+
}))
76+
}
77+
ExprKind::Deref { arg } => {
78+
let arg = this.as_constant(arg);
79+
ConstantKind::Projection(
80+
Box::new(ConstantProjection {
81+
base: arg,
82+
elem: ProjectionElem::Deref,
83+
}))
84+
}
85+
ExprKind::Call { fun, args } => {
86+
let fun = this.as_constant(fun);
87+
let args = this.as_constants(args);
88+
ConstantKind::Call(Box::new(fun), args)
89+
}
90+
_ => {
91+
this.hir.span_bug(
92+
span,
93+
&format!("expression is not a valid constant {:?}", kind));
94+
}
95+
};
96+
Constant { span: span, kind: kind }
97+
}
98+
99+
fn as_constants(&mut self,
100+
exprs: Vec<ExprRef<H>>)
101+
-> Vec<Constant<H>>
102+
{
103+
exprs.into_iter().map(|expr| self.as_constant(expr)).collect()
104+
}
105+
106+
fn named_field_constants(&mut self,
107+
field_names: Vec<Field<H>>,
108+
field_exprs: Vec<FieldExprRef<H>>)
109+
-> Vec<Constant<H>>
110+
{
111+
let fields_map: FnvHashMap<_, _> =
112+
field_exprs.into_iter()
113+
.map(|f| (f.name, self.as_constant(f.expr)))
114+
.collect();
115+
116+
let fields: Vec<_> =
117+
field_names.into_iter()
118+
.map(|n| fields_map[&n].clone())
119+
.collect();
120+
121+
fields
122+
}
123+
}

0 commit comments

Comments
 (0)