Skip to content

Commit

Permalink
Stop passing around Option<&substs> in trans and just pass &substs, m…
Browse files Browse the repository at this point in the history
…aking the code more regular
  • Loading branch information
nikomatsakis committed Jun 6, 2014
1 parent 0f03b56 commit bc5eb7d
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 185 deletions.
8 changes: 8 additions & 0 deletions src/librustc/middle/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ impl Substs {
}
}

pub fn trans_empty() -> Substs {
Substs {
self_ty: None,
tps: Vec::new(),
regions: ErasedRegions
}
}

pub fn is_noop(&self) -> bool {
let regions_is_noop = match self.regions {
ErasedRegions => false, // may be used to canonicalize
Expand Down
30 changes: 15 additions & 15 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,19 +1098,19 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
id: ast::NodeId,
has_env: bool,
output_type: ty::t,
param_substs: Option<&'a param_substs>,
param_substs: &'a param_substs,
sp: Option<Span>,
block_arena: &'a TypedArena<Block<'a>>)
-> FunctionContext<'a> {
for p in param_substs.iter() { p.validate(); }
param_substs.validate();

debug!("new_fn_ctxt(path={}, id={}, param_substs={})",
if id == -1 {
"".to_string()
} else {
ccx.tcx.map.path_to_str(id).to_string()
},
id, param_substs.map(|s| s.repr(ccx.tcx())));
id, param_substs.repr(ccx.tcx()));

let substd_output_type = output_type.substp(ccx.tcx(), param_substs);
let uses_outptr = type_of::return_uses_outptr(ccx, substd_output_type);
Expand Down Expand Up @@ -1303,7 +1303,7 @@ pub fn trans_closure(ccx: &CrateContext,
decl: &ast::FnDecl,
body: &ast::Block,
llfndecl: ValueRef,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
id: ast::NodeId,
_attributes: &[ast::Attribute],
output_type: ty::t,
Expand All @@ -1314,7 +1314,7 @@ pub fn trans_closure(ccx: &CrateContext,
set_uwtable(llfndecl);

debug!("trans_closure(..., param_substs={})",
param_substs.map(|s| s.repr(ccx.tcx())));
param_substs.repr(ccx.tcx()));

let has_env = match ty::get(ty::node_id_to_type(ccx.tcx(), id)).sty {
ty::ty_closure(_) => true,
Expand All @@ -1327,7 +1327,7 @@ pub fn trans_closure(ccx: &CrateContext,
id,
has_env,
output_type,
param_substs.map(|s| &*s),
param_substs,
Some(body.span),
&arena);
init_function(&fcx, false, output_type);
Expand Down Expand Up @@ -1403,11 +1403,11 @@ pub fn trans_fn(ccx: &CrateContext,
decl: &ast::FnDecl,
body: &ast::Block,
llfndecl: ValueRef,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
id: ast::NodeId,
attrs: &[ast::Attribute]) {
let _s = StatRecorder::new(ccx, ccx.tcx.map.path_to_str(id).to_string());
debug!("trans_fn(param_substs={})", param_substs.map(|s| s.repr(ccx.tcx())));
debug!("trans_fn(param_substs={})", param_substs.repr(ccx.tcx()));
let _icx = push_ctxt("trans_fn");
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx(), id));
trans_closure(ccx, decl, body, llfndecl,
Expand All @@ -1419,7 +1419,7 @@ pub fn trans_enum_variant(ccx: &CrateContext,
variant: &ast::Variant,
_args: &[ast::VariantArg],
disr: ty::Disr,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
llfndecl: ValueRef) {
let _icx = push_ctxt("trans_enum_variant");

Expand All @@ -1434,7 +1434,7 @@ pub fn trans_enum_variant(ccx: &CrateContext,
pub fn trans_tuple_struct(ccx: &CrateContext,
_fields: &[ast::StructField],
ctor_id: ast::NodeId,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
llfndecl: ValueRef) {
let _icx = push_ctxt("trans_tuple_struct");

Expand All @@ -1449,7 +1449,7 @@ pub fn trans_tuple_struct(ccx: &CrateContext,
fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
ctor_id: ast::NodeId,
disr: ty::Disr,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
llfndecl: ValueRef) {
let ctor_ty = ty::node_id_to_type(ccx.tcx(), ctor_id);
let ctor_ty = ctor_ty.substp(ccx.tcx(), param_substs);
Expand All @@ -1464,7 +1464,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,

let arena = TypedArena::new();
let fcx = new_fn_ctxt(ccx, llfndecl, ctor_id, false, result_ty,
param_substs.map(|s| &*s), None, &arena);
param_substs, None, &arena);
init_function(&fcx, false, result_ty);

let arg_tys = ty::ty_fn_args(ctor_ty);
Expand Down Expand Up @@ -1500,7 +1500,7 @@ fn trans_enum_def(ccx: &CrateContext, enum_definition: &ast::EnumDef,
ast::TupleVariantKind(ref args) if args.len() > 0 => {
let llfn = get_item_val(ccx, variant.node.id);
trans_enum_variant(ccx, id, variant, args.as_slice(),
disr_val, None, llfn);
disr_val, &param_substs::empty(), llfn);
}
ast::TupleVariantKind(_) => {
// Nothing to do.
Expand Down Expand Up @@ -1587,7 +1587,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
decl,
body,
llfn,
None,
&param_substs::empty(),
item.id,
item.attrs.as_slice());
} else {
Expand Down Expand Up @@ -1660,7 +1660,7 @@ pub fn trans_struct_def(ccx: &CrateContext, struct_def: @ast::StructDef) {
Some(ctor_id) if struct_def.fields.len() > 0 => {
let llfndecl = get_item_val(ccx, ctor_id);
trans_tuple_struct(ccx, struct_def.fields.as_slice(),
ctor_id, None, llfndecl);
ctor_id, &param_substs::empty(), llfndecl);
}
Some(_) | None => {}
}
Expand Down
33 changes: 11 additions & 22 deletions src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn trans_fn_ref_with_vtables_to_callee<'a>(bcx: &'a Block<'a>,
def_id: ast::DefId,
ref_id: ast::NodeId,
substs: subst::Substs,
vtables: Option<typeck::vtable_res>)
vtables: typeck::vtable_res)
-> Callee<'a> {
Callee {bcx: bcx,
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ExprId(ref_id),
Expand All @@ -198,8 +198,9 @@ fn resolve_default_method_vtables(bcx: &Block,
impl_id: ast::DefId,
method: &ty::Method,
substs: &subst::Substs,
impl_vtables: Option<typeck::vtable_res>)
-> (typeck::vtable_res, typeck::vtable_param_res) {
impl_vtables: typeck::vtable_res)
-> (typeck::vtable_res, typeck::vtable_param_res)
{

// Get the vtables that the impl implements the trait at
let impl_res = ty::lookup_impl_vtables(bcx.tcx(), impl_id);
Expand All @@ -213,27 +214,15 @@ fn resolve_default_method_vtables(bcx: &Block,
};

let mut param_vtables = resolve_vtables_under_param_substs(
bcx.tcx(), Some(&param_substs), impl_res.trait_vtables.as_slice());
bcx.tcx(), &param_substs, impl_res.trait_vtables.as_slice());

// Now we pull any vtables for parameters on the actual method.
let num_method_vtables = method.generics.type_param_defs().len();
match impl_vtables {
Some(ref vtables) => {
let num_impl_type_parameters =
vtables.len() - num_method_vtables;
param_vtables.push_all(vtables.tailn(num_impl_type_parameters))
},
None => {
param_vtables.extend(range(0, num_method_vtables).map(
|_| -> typeck::vtable_param_res {
Vec::new()
}
))
}
}
let num_impl_type_parameters = impl_vtables.len() - num_method_vtables;
param_vtables.push_all(impl_vtables.tailn(num_impl_type_parameters));

let self_vtables = resolve_param_vtables_under_param_substs(
bcx.tcx(), Some(&param_substs), impl_res.self_vtables.as_slice());
bcx.tcx(), &param_substs, impl_res.self_vtables.as_slice());

(param_vtables, self_vtables)
}
Expand All @@ -244,7 +233,7 @@ pub fn trans_fn_ref_with_vtables(
def_id: ast::DefId, // def id of fn
node: ExprOrMethodCall, // node id of use of fn; may be zero if N/A
substs: subst::Substs, // values for fn's ty params
vtables: Option<typeck::vtable_res>) // vtables for the call
vtables: typeck::vtable_res) // vtables for the call
-> ValueRef {
/*!
* Translates a reference to a fn/method item, monomorphizing and
Expand Down Expand Up @@ -336,7 +325,7 @@ pub fn trans_fn_ref_with_vtables(
self_vtables.repr(tcx), param_vtables.repr(tcx));

(true, source_id,
new_substs, Some(self_vtables), Some(param_vtables))
new_substs, Some(self_vtables), param_vtables)
}
};

Expand Down Expand Up @@ -507,7 +496,7 @@ pub fn trans_lang_call<'a>(
did,
0,
subst::Substs::empty(),
None)
Vec::new())
},
ArgVals(args),
dest)
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/middle/trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
let _icx = push_ctxt("closure::get_wrapper_for_bare_fn");

let arena = TypedArena::new();
let fcx = new_fn_ctxt(ccx, llfn, -1, true, f.sig.output, None, None, &arena);
let empty_param_substs = param_substs::empty();
let fcx = new_fn_ctxt(ccx, llfn, -1, true, f.sig.output,
&empty_param_substs, None, &arena);
init_function(&fcx, true, f.sig.output);
let bcx = fcx.entry_bcx.borrow().clone().unwrap();

Expand Down
62 changes: 21 additions & 41 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,19 @@ pub type ExternMap = HashMap<String, ValueRef>;
// will only be set in the case of default methods.
pub struct param_substs {
pub substs: subst::Substs,
pub vtables: Option<typeck::vtable_res>,
pub vtables: typeck::vtable_res,
pub self_vtables: Option<typeck::vtable_param_res>
}

impl param_substs {
pub fn empty() -> param_substs {
param_substs {
substs: subst::Substs::trans_empty(),
vtables: Vec::new(),
self_vtables: None
}
}

pub fn validate(&self) {
for t in self.substs.tps.iter() {
assert!(!ty::type_needs_infer(*t));
Expand All @@ -206,21 +214,13 @@ impl Repr for param_substs {
}

pub trait SubstP {
fn substp(&self, tcx: &ty::ctxt, param_substs: Option<&param_substs>)
fn substp(&self, tcx: &ty::ctxt, param_substs: &param_substs)
-> Self;
}

impl<T:Subst+Clone> SubstP for T {
fn substp(&self, tcx: &ty::ctxt, param_substs: Option<&param_substs>)
-> T {
match param_substs {
Some(substs) => {
self.subst(tcx, &substs.substs)
}
None => {
(*self).clone()
}
}
fn substp(&self, tcx: &ty::ctxt, substs: &param_substs) -> T {
self.subst(tcx, &substs.substs)
}
}

Expand Down Expand Up @@ -281,7 +281,7 @@ pub struct FunctionContext<'a> {

// If this function is being monomorphized, this contains the type
// substitutions used.
pub param_substs: Option<&'a param_substs>,
pub param_substs: &'a param_substs,

// The source span and nesting context where this function comes from, for
// error reporting and symbol generation.
Expand Down Expand Up @@ -697,16 +697,7 @@ pub fn is_null(val: ValueRef) -> bool {
}

pub fn monomorphize_type(bcx: &Block, t: ty::t) -> ty::t {
match bcx.fcx.param_substs {
Some(ref substs) => {
t.subst(bcx.tcx(), &substs.substs)
}
_ => {
assert!(!ty::type_has_params(t));
assert!(!ty::type_has_self(t));
t
}
}
t.subst(bcx.tcx(), &bcx.fcx.param_substs.substs)
}

pub fn node_id_type(bcx: &Block, id: ast::NodeId) -> ty::t {
Expand Down Expand Up @@ -759,10 +750,10 @@ pub fn node_id_substs(bcx: &Block,
}

pub fn node_vtables(bcx: &Block, id: typeck::MethodCall)
-> Option<typeck::vtable_res> {
-> typeck::vtable_res {
bcx.tcx().vtable_map.borrow().find(&id).map(|vts| {
resolve_vtables_in_fn_ctxt(bcx.fcx, vts.as_slice())
})
}).unwrap_or_else(|| Vec::new())
}

// Apply the typaram substitutions in the FunctionContext to some
Expand All @@ -776,7 +767,7 @@ pub fn resolve_vtables_in_fn_ctxt(fcx: &FunctionContext,
}

pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
vts: &[typeck::vtable_param_res])
-> typeck::vtable_res {
vts.iter().map(|ds| {
Expand All @@ -788,7 +779,7 @@ pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt,

pub fn resolve_param_vtables_under_param_substs(
tcx: &ty::ctxt,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
ds: &[typeck::vtable_origin])
-> typeck::vtable_param_res {
ds.iter().map(|d| {
Expand All @@ -801,7 +792,7 @@ pub fn resolve_param_vtables_under_param_substs(


pub fn resolve_vtable_under_param_substs(tcx: &ty::ctxt,
param_substs: Option<&param_substs>,
param_substs: &param_substs,
vt: &typeck::vtable_origin)
-> typeck::vtable_origin {
match *vt {
Expand All @@ -812,16 +803,7 @@ pub fn resolve_vtable_under_param_substs(tcx: &ty::ctxt,
resolve_vtables_under_param_substs(tcx, param_substs, sub.as_slice()))
}
typeck::vtable_param(n_param, n_bound) => {
match param_substs {
Some(substs) => {
find_vtable(tcx, substs, n_param, n_bound)
}
_ => {
tcx.sess.bug(format!(
"resolve_vtable_under_param_substs: asked to lookup \
but no vtables in the fn_ctxt!").as_slice())
}
}
find_vtable(tcx, param_substs, n_param, n_bound)
}
}
}
Expand All @@ -837,9 +819,7 @@ pub fn find_vtable(tcx: &ty::ctxt,
let param_bounds = match n_param {
typeck::param_self => ps.self_vtables.as_ref().expect("self vtables missing"),
typeck::param_numbered(n) => {
let tables = ps.vtables.as_ref()
.expect("vtables missing where they are needed");
tables.get(n)
ps.vtables.get(n)
}
};
param_bounds.get(n_bound).clone()
Expand Down
Loading

0 comments on commit bc5eb7d

Please sign in to comment.