Stop passing around Option<&substs> in trans and just pass &substs, making the code more regular

This commit is contained in:
Niko Matsakis 2014-05-14 20:53:48 -04:00
parent 0f03b5608c
commit bc5eb7d98c
14 changed files with 128 additions and 184 deletions

View File

@ -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 { pub fn is_noop(&self) -> bool {
let regions_is_noop = match self.regions { let regions_is_noop = match self.regions {
ErasedRegions => false, // may be used to canonicalize ErasedRegions => false, // may be used to canonicalize

View File

@ -1098,11 +1098,11 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
id: ast::NodeId, id: ast::NodeId,
has_env: bool, has_env: bool,
output_type: ty::t, output_type: ty::t,
param_substs: Option<&'a param_substs>, param_substs: &'a param_substs,
sp: Option<Span>, sp: Option<Span>,
block_arena: &'a TypedArena<Block<'a>>) block_arena: &'a TypedArena<Block<'a>>)
-> FunctionContext<'a> { -> FunctionContext<'a> {
for p in param_substs.iter() { p.validate(); } param_substs.validate();
debug!("new_fn_ctxt(path={}, id={}, param_substs={})", debug!("new_fn_ctxt(path={}, id={}, param_substs={})",
if id == -1 { if id == -1 {
@ -1110,7 +1110,7 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
} else { } else {
ccx.tcx.map.path_to_str(id).to_string() 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 substd_output_type = output_type.substp(ccx.tcx(), param_substs);
let uses_outptr = type_of::return_uses_outptr(ccx, substd_output_type); let uses_outptr = type_of::return_uses_outptr(ccx, substd_output_type);
@ -1303,7 +1303,7 @@ pub fn trans_closure(ccx: &CrateContext,
decl: &ast::FnDecl, decl: &ast::FnDecl,
body: &ast::Block, body: &ast::Block,
llfndecl: ValueRef, llfndecl: ValueRef,
param_substs: Option<&param_substs>, param_substs: &param_substs,
id: ast::NodeId, id: ast::NodeId,
_attributes: &[ast::Attribute], _attributes: &[ast::Attribute],
output_type: ty::t, output_type: ty::t,
@ -1314,7 +1314,7 @@ pub fn trans_closure(ccx: &CrateContext,
set_uwtable(llfndecl); set_uwtable(llfndecl);
debug!("trans_closure(..., param_substs={})", 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 { let has_env = match ty::get(ty::node_id_to_type(ccx.tcx(), id)).sty {
ty::ty_closure(_) => true, ty::ty_closure(_) => true,
@ -1327,7 +1327,7 @@ pub fn trans_closure(ccx: &CrateContext,
id, id,
has_env, has_env,
output_type, output_type,
param_substs.map(|s| &*s), param_substs,
Some(body.span), Some(body.span),
&arena); &arena);
init_function(&fcx, false, output_type); init_function(&fcx, false, output_type);
@ -1403,11 +1403,11 @@ pub fn trans_fn(ccx: &CrateContext,
decl: &ast::FnDecl, decl: &ast::FnDecl,
body: &ast::Block, body: &ast::Block,
llfndecl: ValueRef, llfndecl: ValueRef,
param_substs: Option<&param_substs>, param_substs: &param_substs,
id: ast::NodeId, id: ast::NodeId,
attrs: &[ast::Attribute]) { attrs: &[ast::Attribute]) {
let _s = StatRecorder::new(ccx, ccx.tcx.map.path_to_str(id).to_string()); 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 _icx = push_ctxt("trans_fn");
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx(), id)); let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx(), id));
trans_closure(ccx, decl, body, llfndecl, trans_closure(ccx, decl, body, llfndecl,
@ -1419,7 +1419,7 @@ pub fn trans_enum_variant(ccx: &CrateContext,
variant: &ast::Variant, variant: &ast::Variant,
_args: &[ast::VariantArg], _args: &[ast::VariantArg],
disr: ty::Disr, disr: ty::Disr,
param_substs: Option<&param_substs>, param_substs: &param_substs,
llfndecl: ValueRef) { llfndecl: ValueRef) {
let _icx = push_ctxt("trans_enum_variant"); let _icx = push_ctxt("trans_enum_variant");
@ -1434,7 +1434,7 @@ pub fn trans_enum_variant(ccx: &CrateContext,
pub fn trans_tuple_struct(ccx: &CrateContext, pub fn trans_tuple_struct(ccx: &CrateContext,
_fields: &[ast::StructField], _fields: &[ast::StructField],
ctor_id: ast::NodeId, ctor_id: ast::NodeId,
param_substs: Option<&param_substs>, param_substs: &param_substs,
llfndecl: ValueRef) { llfndecl: ValueRef) {
let _icx = push_ctxt("trans_tuple_struct"); let _icx = push_ctxt("trans_tuple_struct");
@ -1449,7 +1449,7 @@ pub fn trans_tuple_struct(ccx: &CrateContext,
fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext, fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
ctor_id: ast::NodeId, ctor_id: ast::NodeId,
disr: ty::Disr, disr: ty::Disr,
param_substs: Option<&param_substs>, param_substs: &param_substs,
llfndecl: ValueRef) { llfndecl: ValueRef) {
let ctor_ty = ty::node_id_to_type(ccx.tcx(), ctor_id); let ctor_ty = ty::node_id_to_type(ccx.tcx(), ctor_id);
let ctor_ty = ctor_ty.substp(ccx.tcx(), param_substs); let ctor_ty = ctor_ty.substp(ccx.tcx(), param_substs);
@ -1464,7 +1464,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
let arena = TypedArena::new(); let arena = TypedArena::new();
let fcx = new_fn_ctxt(ccx, llfndecl, ctor_id, false, result_ty, 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); init_function(&fcx, false, result_ty);
let arg_tys = ty::ty_fn_args(ctor_ty); let arg_tys = ty::ty_fn_args(ctor_ty);
@ -1500,7 +1500,7 @@ fn trans_enum_def(ccx: &CrateContext, enum_definition: &ast::EnumDef,
ast::TupleVariantKind(ref args) if args.len() > 0 => { ast::TupleVariantKind(ref args) if args.len() > 0 => {
let llfn = get_item_val(ccx, variant.node.id); let llfn = get_item_val(ccx, variant.node.id);
trans_enum_variant(ccx, id, variant, args.as_slice(), trans_enum_variant(ccx, id, variant, args.as_slice(),
disr_val, None, llfn); disr_val, &param_substs::empty(), llfn);
} }
ast::TupleVariantKind(_) => { ast::TupleVariantKind(_) => {
// Nothing to do. // Nothing to do.
@ -1587,7 +1587,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
decl, decl,
body, body,
llfn, llfn,
None, &param_substs::empty(),
item.id, item.id,
item.attrs.as_slice()); item.attrs.as_slice());
} else { } else {
@ -1660,7 +1660,7 @@ pub fn trans_struct_def(ccx: &CrateContext, struct_def: @ast::StructDef) {
Some(ctor_id) if struct_def.fields.len() > 0 => { Some(ctor_id) if struct_def.fields.len() > 0 => {
let llfndecl = get_item_val(ccx, ctor_id); let llfndecl = get_item_val(ccx, ctor_id);
trans_tuple_struct(ccx, struct_def.fields.as_slice(), trans_tuple_struct(ccx, struct_def.fields.as_slice(),
ctor_id, None, llfndecl); ctor_id, &param_substs::empty(), llfndecl);
} }
Some(_) | None => {} Some(_) | None => {}
} }

View File

@ -187,7 +187,7 @@ fn trans_fn_ref_with_vtables_to_callee<'a>(bcx: &'a Block<'a>,
def_id: ast::DefId, def_id: ast::DefId,
ref_id: ast::NodeId, ref_id: ast::NodeId,
substs: subst::Substs, substs: subst::Substs,
vtables: Option<typeck::vtable_res>) vtables: typeck::vtable_res)
-> Callee<'a> { -> Callee<'a> {
Callee {bcx: bcx, Callee {bcx: bcx,
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ExprId(ref_id), data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ExprId(ref_id),
@ -198,8 +198,9 @@ fn resolve_default_method_vtables(bcx: &Block,
impl_id: ast::DefId, impl_id: ast::DefId,
method: &ty::Method, method: &ty::Method,
substs: &subst::Substs, substs: &subst::Substs,
impl_vtables: Option<typeck::vtable_res>) impl_vtables: typeck::vtable_res)
-> (typeck::vtable_res, typeck::vtable_param_res) { -> (typeck::vtable_res, typeck::vtable_param_res)
{
// Get the vtables that the impl implements the trait at // Get the vtables that the impl implements the trait at
let impl_res = ty::lookup_impl_vtables(bcx.tcx(), impl_id); let impl_res = ty::lookup_impl_vtables(bcx.tcx(), impl_id);
@ -213,27 +214,15 @@ fn resolve_default_method_vtables(bcx: &Block,
}; };
let mut param_vtables = resolve_vtables_under_param_substs( 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. // Now we pull any vtables for parameters on the actual method.
let num_method_vtables = method.generics.type_param_defs().len(); let num_method_vtables = method.generics.type_param_defs().len();
match impl_vtables { let num_impl_type_parameters = impl_vtables.len() - num_method_vtables;
Some(ref vtables) => { param_vtables.push_all(impl_vtables.tailn(num_impl_type_parameters));
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 self_vtables = resolve_param_vtables_under_param_substs( 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) (param_vtables, self_vtables)
} }
@ -244,7 +233,7 @@ pub fn trans_fn_ref_with_vtables(
def_id: ast::DefId, // def id of fn def_id: ast::DefId, // def id of fn
node: ExprOrMethodCall, // node id of use of fn; may be zero if N/A node: ExprOrMethodCall, // node id of use of fn; may be zero if N/A
substs: subst::Substs, // values for fn's ty params 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 { -> ValueRef {
/*! /*!
* Translates a reference to a fn/method item, monomorphizing and * Translates a reference to a fn/method item, monomorphizing and
@ -336,7 +325,7 @@ pub fn trans_fn_ref_with_vtables(
self_vtables.repr(tcx), param_vtables.repr(tcx)); self_vtables.repr(tcx), param_vtables.repr(tcx));
(true, source_id, (true, source_id,
new_substs, Some(self_vtables), Some(param_vtables)) new_substs, Some(self_vtables), param_vtables)
} }
}; };
@ -507,7 +496,7 @@ pub fn trans_lang_call<'a>(
did, did,
0, 0,
subst::Substs::empty(), subst::Substs::empty(),
None) Vec::new())
}, },
ArgVals(args), ArgVals(args),
dest) dest)

View File

@ -421,7 +421,9 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
let _icx = push_ctxt("closure::get_wrapper_for_bare_fn"); let _icx = push_ctxt("closure::get_wrapper_for_bare_fn");
let arena = TypedArena::new(); 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); init_function(&fcx, true, f.sig.output);
let bcx = fcx.entry_bcx.borrow().clone().unwrap(); let bcx = fcx.entry_bcx.borrow().clone().unwrap();

View File

@ -180,11 +180,19 @@ pub type ExternMap = HashMap<String, ValueRef>;
// will only be set in the case of default methods. // will only be set in the case of default methods.
pub struct param_substs { pub struct param_substs {
pub substs: subst::Substs, pub substs: subst::Substs,
pub vtables: Option<typeck::vtable_res>, pub vtables: typeck::vtable_res,
pub self_vtables: Option<typeck::vtable_param_res> pub self_vtables: Option<typeck::vtable_param_res>
} }
impl param_substs { 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) { pub fn validate(&self) {
for t in self.substs.tps.iter() { for t in self.substs.tps.iter() {
assert!(!ty::type_needs_infer(*t)); assert!(!ty::type_needs_infer(*t));
@ -206,22 +214,14 @@ impl Repr for param_substs {
} }
pub trait SubstP { 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; -> Self;
} }
impl<T:Subst+Clone> SubstP for T { impl<T:Subst+Clone> SubstP for T {
fn substp(&self, tcx: &ty::ctxt, param_substs: Option<&param_substs>) fn substp(&self, tcx: &ty::ctxt, substs: &param_substs) -> T {
-> T {
match param_substs {
Some(substs) => {
self.subst(tcx, &substs.substs) self.subst(tcx, &substs.substs)
} }
None => {
(*self).clone()
}
}
}
} }
// work around bizarre resolve errors // work around bizarre resolve errors
@ -281,7 +281,7 @@ pub struct FunctionContext<'a> {
// If this function is being monomorphized, this contains the type // If this function is being monomorphized, this contains the type
// substitutions used. // 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 // The source span and nesting context where this function comes from, for
// error reporting and symbol generation. // error reporting and symbol generation.
@ -697,16 +697,7 @@ pub fn is_null(val: ValueRef) -> bool {
} }
pub fn monomorphize_type(bcx: &Block, t: ty::t) -> ty::t { pub fn monomorphize_type(bcx: &Block, t: ty::t) -> ty::t {
match bcx.fcx.param_substs { t.subst(bcx.tcx(), &bcx.fcx.param_substs.substs)
Some(ref substs) => {
t.subst(bcx.tcx(), &substs.substs)
}
_ => {
assert!(!ty::type_has_params(t));
assert!(!ty::type_has_self(t));
t
}
}
} }
pub fn node_id_type(bcx: &Block, id: ast::NodeId) -> ty::t { pub fn node_id_type(bcx: &Block, id: ast::NodeId) -> ty::t {
@ -759,10 +750,10 @@ pub fn node_id_substs(bcx: &Block,
} }
pub fn node_vtables(bcx: &Block, id: typeck::MethodCall) 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| { bcx.tcx().vtable_map.borrow().find(&id).map(|vts| {
resolve_vtables_in_fn_ctxt(bcx.fcx, vts.as_slice()) resolve_vtables_in_fn_ctxt(bcx.fcx, vts.as_slice())
}) }).unwrap_or_else(|| Vec::new())
} }
// Apply the typaram substitutions in the FunctionContext to some // Apply the typaram substitutions in the FunctionContext to some
@ -776,7 +767,7 @@ pub fn resolve_vtables_in_fn_ctxt(fcx: &FunctionContext,
} }
pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt, pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt,
param_substs: Option<&param_substs>, param_substs: &param_substs,
vts: &[typeck::vtable_param_res]) vts: &[typeck::vtable_param_res])
-> typeck::vtable_res { -> typeck::vtable_res {
vts.iter().map(|ds| { vts.iter().map(|ds| {
@ -788,7 +779,7 @@ pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt,
pub fn resolve_param_vtables_under_param_substs( pub fn resolve_param_vtables_under_param_substs(
tcx: &ty::ctxt, tcx: &ty::ctxt,
param_substs: Option<&param_substs>, param_substs: &param_substs,
ds: &[typeck::vtable_origin]) ds: &[typeck::vtable_origin])
-> typeck::vtable_param_res { -> typeck::vtable_param_res {
ds.iter().map(|d| { ds.iter().map(|d| {
@ -801,7 +792,7 @@ pub fn resolve_param_vtables_under_param_substs(
pub fn resolve_vtable_under_param_substs(tcx: &ty::ctxt, pub fn resolve_vtable_under_param_substs(tcx: &ty::ctxt,
param_substs: Option<&param_substs>, param_substs: &param_substs,
vt: &typeck::vtable_origin) vt: &typeck::vtable_origin)
-> typeck::vtable_origin { -> typeck::vtable_origin {
match *vt { match *vt {
@ -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())) resolve_vtables_under_param_substs(tcx, param_substs, sub.as_slice()))
} }
typeck::vtable_param(n_param, n_bound) => { typeck::vtable_param(n_param, n_bound) => {
match param_substs { find_vtable(tcx, param_substs, n_param, n_bound)
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())
}
}
} }
} }
} }
@ -837,9 +819,7 @@ pub fn find_vtable(tcx: &ty::ctxt,
let param_bounds = match n_param { let param_bounds = match n_param {
typeck::param_self => ps.self_vtables.as_ref().expect("self vtables missing"), typeck::param_self => ps.self_vtables.as_ref().expect("self vtables missing"),
typeck::param_numbered(n) => { typeck::param_numbered(n) => {
let tables = ps.vtables.as_ref() ps.vtables.get(n)
.expect("vtables missing where they are needed");
tables.get(n)
} }
}; };
param_bounds.get(n_bound).clone() param_bounds.get(n_bound).clone()

View File

@ -621,7 +621,7 @@ pub fn start_emitting_source_locations(fcx: &FunctionContext) {
/// indicates why no debuginfo should be created for the function. /// indicates why no debuginfo should be created for the function.
pub fn create_function_debug_context(cx: &CrateContext, pub fn create_function_debug_context(cx: &CrateContext,
fn_ast_id: ast::NodeId, fn_ast_id: ast::NodeId,
param_substs: Option<&param_substs>, param_substs: &param_substs,
llfn: ValueRef) -> FunctionDebugContext { llfn: ValueRef) -> FunctionDebugContext {
if cx.sess().opts.debuginfo == NoDebugInfo { if cx.sess().opts.debuginfo == NoDebugInfo {
return FunctionDebugContext { repr: DebugInfoDisabled }; return FunctionDebugContext { repr: DebugInfoDisabled };
@ -788,7 +788,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
fn get_function_signature(cx: &CrateContext, fn get_function_signature(cx: &CrateContext,
fn_ast_id: ast::NodeId, fn_ast_id: ast::NodeId,
fn_decl: &ast::FnDecl, fn_decl: &ast::FnDecl,
param_substs: Option<&param_substs>, param_substs: &param_substs,
error_span: Span) -> DIArray { error_span: Span) -> DIArray {
if cx.sess().opts.debuginfo == LimitedDebugInfo { if cx.sess().opts.debuginfo == LimitedDebugInfo {
return create_DIArray(DIB(cx), []); return create_DIArray(DIB(cx), []);
@ -823,14 +823,11 @@ pub fn create_function_debug_context(cx: &CrateContext,
fn get_template_parameters(cx: &CrateContext, fn get_template_parameters(cx: &CrateContext,
generics: &ast::Generics, generics: &ast::Generics,
param_substs: Option<&param_substs>, param_substs: &param_substs,
file_metadata: DIFile, file_metadata: DIFile,
name_to_append_suffix_to: &mut String) name_to_append_suffix_to: &mut String)
-> DIArray { -> DIArray {
let self_type = match param_substs { let self_type = param_substs.substs.self_ty;
Some(param_substs) => param_substs.substs.self_ty,
_ => None
};
// Only true for static default methods: // Only true for static default methods:
let has_self_type = self_type.is_some(); let has_self_type = self_type.is_some();
@ -884,13 +881,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
} }
// Handle other generic parameters // Handle other generic parameters
let actual_types = match param_substs { let actual_types = &param_substs.substs.tps;
Some(param_substs) => &param_substs.substs.tps,
None => {
return create_DIArray(DIB(cx), template_params.as_slice());
}
};
for (index, &ast::TyParam{ ident: ident, .. }) in generics.ty_params.iter().enumerate() { for (index, &ast::TyParam{ ident: ident, .. }) in generics.ty_params.iter().enumerate() {
let actual_type = *actual_types.get(index); let actual_type = *actual_types.get(index);
// Add actual type name to <...> clause of function name // Add actual type name to <...> clause of function name

View File

@ -570,7 +570,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
let llfn = base::decl_internal_rust_fn(ccx, t, ps.as_slice()); let llfn = base::decl_internal_rust_fn(ccx, t, ps.as_slice());
base::set_llvm_fn_attrs(attrs, llfn); base::set_llvm_fn_attrs(attrs, llfn);
base::trans_fn(ccx, decl, body, llfn, None, id, []); base::trans_fn(ccx, decl, body, llfn, &param_substs::empty(), id, []);
llfn llfn
} }

View File

@ -479,7 +479,9 @@ fn make_generic_glue(ccx: &CrateContext,
let _s = StatRecorder::new(ccx, glue_name); let _s = StatRecorder::new(ccx, glue_name);
let arena = TypedArena::new(); let arena = TypedArena::new();
let fcx = new_fn_ctxt(ccx, llfn, -1, false, ty::mk_nil(), None, None, &arena); let empty_param_substs = param_substs::empty();
let fcx = new_fn_ctxt(ccx, llfn, -1, false, ty::mk_nil(),
&empty_param_substs, None, &arena);
init_function(&fcx, false, ty::mk_nil()); init_function(&fcx, false, ty::mk_nil());

View File

@ -131,7 +131,8 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
if num_type_params == 0 { if num_type_params == 0 {
let llfn = get_item_val(ccx, mth.id); let llfn = get_item_val(ccx, mth.id);
trans_fn(ccx, mth.decl, mth.body, llfn, None, mth.id, []); trans_fn(ccx, mth.decl, mth.body, llfn,
&param_substs::empty(), mth.id, []);
} }
local_def(mth.id) local_def(mth.id)
} }

View File

@ -192,7 +192,7 @@ pub fn trans_intrinsic(ccx: &CrateContext,
let arena = TypedArena::new(); let arena = TypedArena::new();
let fcx = new_fn_ctxt(ccx, decl, item.id, false, output_type, let fcx = new_fn_ctxt(ccx, decl, item.id, false, output_type,
Some(&*substs), Some(item.span), &arena); substs, Some(item.span), &arena);
init_function(&fcx, true, output_type); init_function(&fcx, true, output_type);
set_always_inline(fcx.llfn); set_always_inline(fcx.llfn);

View File

@ -68,7 +68,7 @@ pub fn trans_impl(ccx: &CrateContext,
if method.generics.ty_params.len() == 0u { if method.generics.ty_params.len() == 0u {
let llfn = get_item_val(ccx, method.id); let llfn = get_item_val(ccx, method.id);
trans_fn(ccx, method.decl, method.body, trans_fn(ccx, method.decl, method.body,
llfn, None, method.id, []); llfn, &param_substs::empty(), method.id, []);
} else { } else {
let mut v = TransItemVisitor{ ccx: ccx }; let mut v = TransItemVisitor{ ccx: ccx };
visit::walk_method_helper(&mut v, *method, ()); visit::walk_method_helper(&mut v, *method, ());
@ -110,20 +110,14 @@ pub fn trans_method_callee<'a>(
param_num: p, param_num: p,
bound_num: b bound_num: b
}) => { }) => {
match bcx.fcx.param_substs {
Some(substs) => {
ty::populate_implementations_for_trait_if_necessary( ty::populate_implementations_for_trait_if_necessary(
bcx.tcx(), bcx.tcx(),
trait_id); trait_id);
let vtbl = find_vtable(bcx.tcx(), substs, p, b); let vtbl = find_vtable(bcx.tcx(), bcx.fcx.param_substs, p, b);
trans_monomorphized_callee(bcx, method_call, trans_monomorphized_callee(bcx, method_call,
trait_id, off, vtbl) trait_id, off, vtbl)
} }
// how to get rid of this?
None => fail!("trans_method_callee: missing param_substs")
}
}
typeck::MethodObject(ref mt) => { typeck::MethodObject(ref mt) => {
let self_expr = match self_expr { let self_expr = match self_expr {
@ -209,7 +203,7 @@ pub fn trans_static_method_callee(bcx: &Block,
let llfn = trans_fn_ref_with_vtables(bcx, mth_id, ExprId(expr_id), let llfn = trans_fn_ref_with_vtables(bcx, mth_id, ExprId(expr_id),
callee_substs, callee_substs,
Some(callee_origins)); callee_origins);
let callee_ty = node_id_type(bcx, expr_id); let callee_ty = node_id_type(bcx, expr_id);
let llty = type_of_fn_from_ty(ccx, callee_ty).ptr_to(); let llty = type_of_fn_from_ty(ccx, callee_ty).ptr_to();
@ -265,7 +259,7 @@ fn trans_monomorphized_callee<'a>(bcx: &'a Block<'a>,
mth_id, mth_id,
MethodCall(method_call), MethodCall(method_call),
callee_substs, callee_substs,
Some(callee_origins)); callee_origins);
Callee { bcx: bcx, data: Fn(llfn) } Callee { bcx: bcx, data: Fn(llfn) }
} }
@ -322,19 +316,9 @@ fn combine_impl_and_methods_tps(bcx: &Block,
MethodCall(method_call) => method_call MethodCall(method_call) => method_call
}; };
let mut vtables = rcvr_origins; let mut vtables = rcvr_origins;
match node_vtables(bcx, vtable_key) { let vt = node_vtables(bcx, vtable_key);
Some(vt) => {
let start = vt.len() - n_m_tps; let start = vt.len() - n_m_tps;
vtables.extend(vt.move_iter().skip(start)); vtables.extend(vt.move_iter().skip(start));
}
None => {
vtables.extend(range(0, n_m_tps).map(
|_| -> typeck::vtable_param_res {
Vec::new()
}
));
}
}
let ty_substs = subst::Substs { let ty_substs = subst::Substs {
tps: tps, tps: tps,
@ -525,7 +509,7 @@ fn emit_vtable_methods(bcx: &Block,
C_null(Type::nil(ccx).ptr_to()) C_null(Type::nil(ccx).ptr_to())
} else { } else {
trans_fn_ref_with_vtables(bcx, m_id, ExprId(0), trans_fn_ref_with_vtables(bcx, m_id, ExprId(0),
substs.clone(), Some(vtables.clone())) substs.clone(), vtables.clone())
} }
}).collect() }).collect()
} }

View File

@ -32,7 +32,7 @@ use std::hash::{sip, Hash};
pub fn monomorphic_fn(ccx: &CrateContext, pub fn monomorphic_fn(ccx: &CrateContext,
fn_id: ast::DefId, fn_id: ast::DefId,
real_substs: &subst::Substs, real_substs: &subst::Substs,
vtables: Option<typeck::vtable_res>, vtables: typeck::vtable_res,
self_vtables: Option<typeck::vtable_param_res>, self_vtables: Option<typeck::vtable_param_res>,
ref_id: Option<ast::NodeId>) ref_id: Option<ast::NodeId>)
-> (ValueRef, bool) { -> (ValueRef, bool) {
@ -206,7 +206,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
} => { } => {
let d = mk_lldecl(); let d = mk_lldecl();
set_llvm_fn_attrs(i.attrs.as_slice(), d); set_llvm_fn_attrs(i.attrs.as_slice(), d);
trans_fn(ccx, decl, body, d, Some(&psubsts), fn_id.node, []); trans_fn(ccx, decl, body, d, &psubsts, fn_id.node, []);
d d
} }
_ => { _ => {
@ -238,7 +238,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
v, v,
args.as_slice(), args.as_slice(),
this_tv.disr_val, this_tv.disr_val,
Some(&psubsts), &psubsts,
d); d);
} }
ast::StructVariantKind(_) => ast::StructVariantKind(_) =>
@ -249,7 +249,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
ast_map::NodeMethod(mth) => { ast_map::NodeMethod(mth) => {
let d = mk_lldecl(); let d = mk_lldecl();
set_llvm_fn_attrs(mth.attrs.as_slice(), d); set_llvm_fn_attrs(mth.attrs.as_slice(), d);
trans_fn(ccx, mth.decl, mth.body, d, Some(&psubsts), mth.id, []); trans_fn(ccx, mth.decl, mth.body, d, &psubsts, mth.id, []);
d d
} }
ast_map::NodeTraitMethod(method) => { ast_map::NodeTraitMethod(method) => {
@ -257,7 +257,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
ast::Provided(mth) => { ast::Provided(mth) => {
let d = mk_lldecl(); let d = mk_lldecl();
set_llvm_fn_attrs(mth.attrs.as_slice(), d); set_llvm_fn_attrs(mth.attrs.as_slice(), d);
trans_fn(ccx, mth.decl, mth.body, d, Some(&psubsts), mth.id, []); trans_fn(ccx, mth.decl, mth.body, d, &psubsts, mth.id, []);
d d
} }
_ => { _ => {
@ -273,7 +273,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
struct_def.fields.as_slice(), struct_def.fields.as_slice(),
struct_def.ctor_id.expect("ast-mapped tuple struct \ struct_def.ctor_id.expect("ast-mapped tuple struct \
didn't have a ctor id"), didn't have a ctor id"),
Some(&psubsts), &psubsts,
d); d);
d d
} }

View File

@ -297,8 +297,10 @@ impl<'a, 'b> Reflector<'a, 'b> {
fn_ty, fn_ty,
sym.as_slice()); sym.as_slice());
let arena = TypedArena::new(); let arena = TypedArena::new();
let empty_param_substs = param_substs::empty();
let fcx = new_fn_ctxt(ccx, llfdecl, -1, false, let fcx = new_fn_ctxt(ccx, llfdecl, -1, false,
ty::mk_u64(), None, None, &arena); ty::mk_u64(), &empty_param_substs,
None, &arena);
init_function(&fcx, false, ty::mk_u64()); init_function(&fcx, false, ty::mk_u64());
let arg = unsafe { let arg = unsafe {

View File

@ -74,11 +74,6 @@ impl<'a> VtableContext<'a> {
pub fn tcx(&self) -> &'a ty::ctxt { self.infcx.tcx } pub fn tcx(&self) -> &'a ty::ctxt { self.infcx.tcx }
} }
fn has_trait_bounds(type_param_defs: &[ty::TypeParameterDef]) -> bool {
type_param_defs.iter().any(
|type_param_def| !type_param_def.bounds.trait_bounds.is_empty())
}
fn lookup_vtables(vcx: &VtableContext, fn lookup_vtables(vcx: &VtableContext,
span: Span, span: Span,
type_param_defs: &[ty::TypeParameterDef], type_param_defs: &[ty::TypeParameterDef],
@ -636,7 +631,6 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
let item_ty = ty::lookup_item_type(cx.tcx, did); let item_ty = ty::lookup_item_type(cx.tcx, did);
debug!("early resolve expr: def {:?} {:?}, {:?}, {}", ex.id, did, def, debug!("early resolve expr: def {:?} {:?}, {:?}, {}", ex.id, did, def,
fcx.infcx().ty_to_str(item_ty.ty)); fcx.infcx().ty_to_str(item_ty.ty));
if has_trait_bounds(item_ty.generics.type_param_defs()) {
debug!("early_resolve_expr: looking up vtables for type params {}", debug!("early_resolve_expr: looking up vtables for type params {}",
item_ty.generics.type_param_defs().repr(fcx.tcx())); item_ty.generics.type_param_defs().repr(fcx.tcx()));
let vcx = fcx.vtable_context(); let vcx = fcx.vtable_context();
@ -646,7 +640,6 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
if !is_early { if !is_early {
insert_vtables(fcx, MethodCall::expr(ex.id), vtbls); insert_vtables(fcx, MethodCall::expr(ex.id), vtbls);
} }
}
}); });
} }
@ -661,7 +654,6 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
debug!("vtable resolution on parameter bounds for method call {}", debug!("vtable resolution on parameter bounds for method call {}",
ex.repr(fcx.tcx())); ex.repr(fcx.tcx()));
let type_param_defs = ty::method_call_type_param_defs(cx.tcx, method.origin); let type_param_defs = ty::method_call_type_param_defs(cx.tcx, method.origin);
if has_trait_bounds(type_param_defs.as_slice()) {
let substs = fcx.method_ty_substs(ex.id); let substs = fcx.method_ty_substs(ex.id);
let vcx = fcx.vtable_context(); let vcx = fcx.vtable_context();
let vtbls = lookup_vtables(&vcx, ex.span, let vtbls = lookup_vtables(&vcx, ex.span,
@ -671,7 +663,6 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
insert_vtables(fcx, MethodCall::expr(ex.id), vtbls); insert_vtables(fcx, MethodCall::expr(ex.id), vtbls);
} }
} }
}
None => {} None => {}
} }
} }
@ -696,7 +687,6 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
ex.repr(fcx.tcx())); ex.repr(fcx.tcx()));
let type_param_defs = let type_param_defs =
ty::method_call_type_param_defs(cx.tcx, method.origin); ty::method_call_type_param_defs(cx.tcx, method.origin);
if has_trait_bounds(type_param_defs.deref().as_slice()) {
let vcx = fcx.vtable_context(); let vcx = fcx.vtable_context();
let vtbls = lookup_vtables(&vcx, ex.span, let vtbls = lookup_vtables(&vcx, ex.span,
type_param_defs.deref() type_param_defs.deref()
@ -706,7 +696,6 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
insert_vtables(fcx, method_call, vtbls); insert_vtables(fcx, method_call, vtbls);
} }
} }
}
None => {} None => {}
} }
} }
@ -800,23 +789,19 @@ pub fn resolve_impl(tcx: &ty::ctxt,
/// Resolve vtables for a method call after typeck has finished. /// Resolve vtables for a method call after typeck has finished.
/// Used by trans to monomorphize artificial method callees (e.g. drop). /// Used by trans to monomorphize artificial method callees (e.g. drop).
pub fn trans_resolve_method(tcx: &ty::ctxt, id: ast::NodeId, pub fn trans_resolve_method(tcx: &ty::ctxt, id: ast::NodeId,
substs: &subst::Substs) -> Option<vtable_res> { substs: &subst::Substs) -> vtable_res {
let generics = ty::lookup_item_type(tcx, ast_util::local_def(id)).generics; let generics = ty::lookup_item_type(tcx, ast_util::local_def(id)).generics;
let type_param_defs = &*generics.type_param_defs; let type_param_defs = &*generics.type_param_defs;
if has_trait_bounds(type_param_defs.as_slice()) {
let vcx = VtableContext { let vcx = VtableContext {
infcx: &infer::new_infer_ctxt(tcx), infcx: &infer::new_infer_ctxt(tcx),
param_env: &ty::construct_parameter_environment(tcx, None, [], [], [], [], id) param_env: &ty::construct_parameter_environment(tcx, None, [], [], [], [], id)
}; };
Some(lookup_vtables(&vcx, lookup_vtables(&vcx,
tcx.map.span(id), tcx.map.span(id),
type_param_defs.as_slice(), type_param_defs.as_slice(),
substs, substs,
false)) false)
} else {
None
}
} }
impl<'a, 'b> visit::Visitor<()> for &'a FnCtxt<'b> { impl<'a, 'b> visit::Visitor<()> for &'a FnCtxt<'b> {