Skip to content

Commit f0a69b1

Browse files
committed
Refactor some coherence/method trans code, add an impls map to tcx.
Rewrite method_with_name_or_default to use the new impls map. Get rid of ProvidedMethodsMap.
1 parent 3770221 commit f0a69b1

File tree

3 files changed

+70
-213
lines changed

3 files changed

+70
-213
lines changed

src/librustc/middle/trans/meth.rs

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ pub fn trans_method_callee(bcx: block,
177177
// Now that we know the impl ID, we can look up the method
178178
// ID from its name
179179
origin = typeck::method_static(
180-
method_with_name_or_default(bcx.ccx(),
181-
impl_id,
182-
method_name));
180+
method_with_name(bcx.ccx(), impl_id, method_name));
183181
}
184182
typeck::method_self(*) |
185183
typeck::method_static(*) | typeck::method_param(*) |
@@ -308,12 +306,10 @@ pub fn trans_static_method_callee(bcx: block,
308306
typeck::vtable_static(impl_did, ref rcvr_substs, rcvr_origins) => {
309307
assert!(rcvr_substs.iter().all(|t| !ty::type_needs_infer(*t)));
310308

311-
let mth_id = method_with_name_or_default(bcx.ccx(),
312-
impl_did,
313-
mname);
309+
let mth_id = method_with_name(bcx.ccx(), impl_did, mname);
314310
let (callee_substs, callee_origins) =
315311
combine_impl_and_methods_tps(
316-
bcx, mth_id, impl_did, callee_id,
312+
bcx, mth_id, callee_id,
317313
*rcvr_substs, rcvr_origins);
318314

319315
let FnData {llfn: lval} =
@@ -334,58 +330,22 @@ pub fn trans_static_method_callee(bcx: block,
334330
}
335331
}
336332

337-
pub fn method_from_methods(ms: &[@ast::method], name: ast::ident)
338-
-> Option<ast::def_id> {
339-
ms.iter().find_(|m| m.ident == name).map(|m| ast_util::local_def(m.id))
340-
}
341-
342-
pub fn method_with_name_or_default(ccx: &mut CrateContext,
343-
impl_id: ast::def_id,
344-
name: ast::ident) -> ast::def_id {
345-
let imp = ccx.impl_method_cache.find_copy(&(impl_id, name));
346-
match imp {
333+
pub fn method_with_name(ccx: &mut CrateContext,
334+
impl_id: ast::def_id,
335+
name: ast::ident) -> ast::def_id {
336+
let meth_id_opt = ccx.impl_method_cache.find_copy(&(impl_id, name));
337+
match meth_id_opt {
347338
Some(m) => return m,
348339
None => {}
349340
}
350341

351-
// None of this feels like it should be the best way to do this.
352-
let mut did = if impl_id.crate == ast::local_crate {
353-
match ccx.tcx.items.get_copy(&impl_id.node) {
354-
ast_map::node_item(@ast::item {
355-
node: ast::item_impl(_, _, _, ref ms), _
356-
}, _) => { method_from_methods(*ms, name) },
357-
_ => fail!("method_with_name")
358-
}
359-
} else {
360-
csearch::get_impl_method(ccx.sess.cstore, impl_id, name)
361-
};
362-
363-
if did.is_none() {
364-
// Look for a default method
365-
let pmm = ccx.tcx.provided_methods;
366-
match pmm.find(&impl_id) {
367-
Some(pmis) => {
368-
for pmis.iter().advance |pmi| {
369-
if pmi.method_info.ident == name {
370-
debug!("pmi.method_info.did = %?",
371-
pmi.method_info.did);
372-
did = Some(pmi.method_info.did);
373-
}
374-
}
375-
}
376-
None => {}
377-
}
378-
}
379-
380-
let imp = did.expect("could not find method while translating");
381-
ccx.impl_method_cache.insert((impl_id, name), imp);
382-
imp
383-
}
342+
let imp = ccx.tcx.impls.find(&impl_id)
343+
.expect("could not find impl while translating");
344+
let meth = imp.methods.iter().find_(|m| m.ident == name)
345+
.expect("could not find method while translating");
384346

385-
pub fn method_ty_param_count(ccx: &CrateContext, m_id: ast::def_id,
386-
i_id: ast::def_id) -> uint {
387-
debug!("method_ty_param_count: m_id: %?, i_id: %?", m_id, i_id);
388-
ty::method(ccx.tcx, m_id).generics.type_param_defs.len()
347+
ccx.impl_method_cache.insert((impl_id, name), meth.did);
348+
meth.did
389349
}
390350

391351
pub fn trans_monomorphized_callee(bcx: block,
@@ -401,8 +361,7 @@ pub fn trans_monomorphized_callee(bcx: block,
401361
typeck::vtable_static(impl_did, ref rcvr_substs, rcvr_origins) => {
402362
let ccx = bcx.ccx();
403363
let mname = ty::trait_method(ccx.tcx, trait_id, n_method).ident;
404-
let mth_id = method_with_name_or_default(
405-
bcx.ccx(), impl_did, mname);
364+
let mth_id = method_with_name(bcx.ccx(), impl_did, mname);
406365

407366
// obtain the `self` value:
408367
let mut temp_cleanups = ~[];
@@ -413,7 +372,7 @@ pub fn trans_monomorphized_callee(bcx: block,
413372
// those from the impl and those from the method:
414373
let (callee_substs, callee_origins) =
415374
combine_impl_and_methods_tps(
416-
bcx, mth_id, impl_did, callee_id,
375+
bcx, mth_id, callee_id,
417376
*rcvr_substs, rcvr_origins);
418377

419378
// translate the function
@@ -452,7 +411,6 @@ pub fn trans_monomorphized_callee(bcx: block,
452411

453412
pub fn combine_impl_and_methods_tps(bcx: block,
454413
mth_did: ast::def_id,
455-
impl_did: ast::def_id,
456414
callee_id: ast::node_id,
457415
rcvr_substs: &[ty::t],
458416
rcvr_origins: typeck::vtable_res)
@@ -475,15 +433,16 @@ pub fn combine_impl_and_methods_tps(bcx: block,
475433
* mapped to. */
476434

477435
let ccx = bcx.ccx();
478-
let n_m_tps = method_ty_param_count(ccx, mth_did, impl_did);
436+
let method = ty::method(ccx.tcx, mth_did);
437+
let n_m_tps = method.generics.type_param_defs.len();
479438
let node_substs = node_id_type_params(bcx, callee_id);
480-
debug!("rcvr_substs=%?", rcvr_substs.map(|t| bcx.ty_to_str(*t)));
439+
debug!("rcvr_substs=%?", rcvr_substs.repr(ccx.tcx));
481440
let ty_substs
482441
= vec::append(rcvr_substs.to_owned(),
483442
node_substs.tailn(node_substs.len() - n_m_tps));
484443
debug!("n_m_tps=%?", n_m_tps);
485-
debug!("node_substs=%?", node_substs.map(|t| bcx.ty_to_str(*t)));
486-
debug!("ty_substs=%?", ty_substs.map(|t| bcx.ty_to_str(*t)));
444+
debug!("node_substs=%?", node_substs.repr(ccx.tcx));
445+
debug!("ty_substs=%?", ty_substs.repr(ccx.tcx));
487446

488447

489448
// Now, do the same work for the vtables. The vtables might not
@@ -744,7 +703,7 @@ pub fn make_impl_vtable(bcx: block,
744703
} else {
745704
debug!("(making impl vtable) adding method to vtable: %s",
746705
tcx.sess.str_of(im.ident));
747-
let m_id = method_with_name_or_default(ccx, impl_id, im.ident);
706+
let m_id = method_with_name(ccx, impl_id, im.ident);
748707

749708
trans_fn_ref_with_vtables(bcx, m_id, 0,
750709
substs, Some(vtables)).llfn

src/librustc/middle/ty.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,6 @@ pub enum AutoRef {
214214
AutoUnsafe(ast::mutability)
215215
}
216216

217-
// Stores information about provided methods (a.k.a. default methods) in
218-
// implementations.
219-
//
220-
// This is a map from ID of each implementation to the method info and trait
221-
// method ID of each of the default methods belonging to the trait that
222-
// implementation implements.
223-
pub type ProvidedMethodsMap = @mut HashMap<def_id,@mut ~[@ProvidedMethodInfo]>;
224-
225-
// Stores the method info and definition ID of the associated trait method for
226-
// each instantiation of each provided method.
227-
pub struct ProvidedMethodInfo {
228-
method_info: @MethodInfo,
229-
trait_method_def_id: def_id
230-
}
231-
232217
pub struct ProvidedMethodSource {
233218
method_id: ast::def_id,
234219
impl_id: ast::def_id
@@ -287,10 +272,7 @@ struct ctxt_ {
287272
adjustments: @mut HashMap<ast::node_id, @AutoAdjustment>,
288273
normalized_cache: @mut HashMap<t, t>,
289274
lang_items: middle::lang_items::LanguageItems,
290-
// A mapping from an implementation ID to the method info and trait
291-
// method ID of the provided (a.k.a. default) methods in the traits that
292-
// that implementation implements.
293-
provided_methods: ProvidedMethodsMap,
275+
// A mapping of fake provided method def_ids to the default implementation
294276
provided_method_sources: @mut HashMap<ast::def_id, ProvidedMethodSource>,
295277
supertraits: @mut HashMap<ast::def_id, @~[@TraitRef]>,
296278

@@ -311,6 +293,12 @@ struct ctxt_ {
311293
// Methods in these implementations don't need to be exported.
312294
inherent_impls: @mut HashMap<ast::def_id, @mut ~[@Impl]>,
313295

296+
// Maps a def_id of an impl to an Impl structure.
297+
// Note that this contains all of the impls that we know about,
298+
// including ones in other crates. It's not clear that this is the best
299+
// way to do it.
300+
impls: @mut HashMap<ast::def_id, @Impl>,
301+
314302
// Set of used unsafe nodes (functions or blocks). Unsafe nodes not
315303
// present in this set can be warned about.
316304
used_unsafe: @mut HashSet<ast::node_id>,
@@ -904,13 +892,13 @@ pub fn mk_ctxt(s: session::Session,
904892
adjustments: @mut HashMap::new(),
905893
normalized_cache: new_ty_hash(),
906894
lang_items: lang_items,
907-
provided_methods: @mut HashMap::new(),
908895
provided_method_sources: @mut HashMap::new(),
909896
supertraits: @mut HashMap::new(),
910897
destructor_for_type: @mut HashMap::new(),
911898
destructors: @mut HashSet::new(),
912899
trait_impls: @mut HashMap::new(),
913900
inherent_impls: @mut HashMap::new(),
901+
impls: @mut HashMap::new(),
914902
used_unsafe: @mut HashSet::new(),
915903
used_mut_nodes: @mut HashSet::new(),
916904
}

0 commit comments

Comments
 (0)