Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
translate tuple-variant constructors using MIR
- Loading branch information
Showing
11 changed files
with
215 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2016 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. | ||
|
||
use rustc::hir; | ||
use rustc::infer; | ||
use rustc::mir::*; | ||
use rustc::mir::transform::MirSource; | ||
use rustc::ty; | ||
|
||
use rustc_data_structures::indexed_vec::{IndexVec, Idx}; | ||
|
||
use syntax::ast; | ||
use syntax_pos::Span; | ||
|
||
use std::iter; | ||
|
||
fn local_decls_for_sig<'tcx>(sig: &ty::FnSig<'tcx>) | ||
-> IndexVec<Local, LocalDecl<'tcx>> | ||
{ | ||
iter::once(LocalDecl { | ||
mutability: Mutability::Mut, | ||
ty: sig.output(), | ||
name: None, | ||
source_info: None | ||
}).chain(sig.inputs().iter().map(|ity| LocalDecl { | ||
mutability: Mutability::Not, | ||
ty: *ity, | ||
name: None, | ||
source_info: None, | ||
})).collect() | ||
} | ||
|
||
pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>, | ||
ctor_id: ast::NodeId, | ||
fields: &[hir::StructField], | ||
span: Span) | ||
-> (Mir<'tcx>, MirSource) | ||
{ | ||
let tcx = infcx.tcx; | ||
let def_id = tcx.hir.local_def_id(ctor_id); | ||
let sig = match tcx.item_type(def_id).sty { | ||
ty::TyFnDef(_, _, fty) => tcx.no_late_bound_regions(&fty) | ||
.expect("LBR in ADT constructor signature"), | ||
_ => bug!("unexpected type for ctor {:?}", def_id) | ||
}; | ||
let sig = tcx.erase_regions(&sig); | ||
|
||
let (adt_def, substs) = match sig.output().sty { | ||
ty::TyAdt(adt_def, substs) => (adt_def, substs), | ||
_ => bug!("unexpected type for ADT ctor {:?}", sig.output()) | ||
}; | ||
|
||
debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields); | ||
|
||
let local_decls = local_decls_for_sig(&sig); | ||
|
||
let source_info = SourceInfo { | ||
span: span, | ||
scope: ARGUMENT_VISIBILITY_SCOPE | ||
}; | ||
|
||
let variant_no = if adt_def.is_enum() { | ||
adt_def.variant_index_with_id(def_id) | ||
} else { | ||
0 | ||
}; | ||
|
||
// return = ADT(arg0, arg1, ...); return | ||
let start_block = BasicBlockData { | ||
statements: vec![Statement { | ||
source_info: source_info, | ||
kind: StatementKind::Assign( | ||
Lvalue::Local(RETURN_POINTER), | ||
Rvalue::Aggregate( | ||
AggregateKind::Adt(adt_def, variant_no, substs, None), | ||
(1..sig.inputs().len()+1).map(|i| { | ||
Operand::Consume(Lvalue::Local(Local::new(i))) | ||
}).collect() | ||
) | ||
) | ||
}], | ||
terminator: Some(Terminator { | ||
source_info: source_info, | ||
kind: TerminatorKind::Return, | ||
}), | ||
is_cleanup: false | ||
}; | ||
|
||
let mir = Mir::new( | ||
IndexVec::from_elem_n(start_block, 1), | ||
IndexVec::from_elem_n( | ||
VisibilityScopeData { span: span, parent_scope: None }, 1 | ||
), | ||
IndexVec::new(), | ||
sig.output(), | ||
local_decls, | ||
sig.inputs().len(), | ||
vec![], | ||
span | ||
); | ||
(mir, MirSource::Fn(ctor_id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.