Skip to content

Commit 007abe9

Browse files
committed
Rather than storing a list of ty::method per trait, store one ty::method
per method and list of def-ids per trait.
1 parent d948308 commit 007abe9

File tree

24 files changed

+414
-323
lines changed

24 files changed

+414
-323
lines changed

src/librustc/metadata/common.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub static tag_item_dtor: uint = 0x49u;
102102
pub static tag_item_trait_method_self_ty: uint = 0x4b;
103103
pub static tag_item_trait_method_self_ty_region: uint = 0x4c;
104104

105+
105106
// Reexports are found within module tags. Each reexport contains def_ids
106107
// and names.
107108
pub static tag_items_data_item_reexport: uint = 0x4d;
@@ -159,6 +160,9 @@ pub static tag_items_data_item_visibility: uint = 0x78;
159160
pub static tag_link_args: uint = 0x79;
160161
pub static tag_link_args_arg: uint = 0x7a;
161162

163+
pub static tag_item_method_tps: uint = 0x7b;
164+
pub static tag_item_method_fty: uint = 0x7c;
165+
162166
pub struct LinkMeta {
163167
name: @str,
164168
vers: @str,

src/librustc/metadata/csearch.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,24 @@ pub fn get_impls_for_mod(cstore: @mut cstore::CStore, def: ast::def_id,
111111
}
112112
}
113113

114-
pub fn get_trait_methods(tcx: ty::ctxt,
115-
def: ast::def_id)
116-
-> @~[ty::method] {
117-
let cstore = tcx.cstore;
114+
pub fn get_method(tcx: ty::ctxt,
115+
def: ast::def_id) -> ty::method
116+
{
117+
let cdata = cstore::get_crate_data(tcx.cstore, def.crate);
118+
decoder::get_method(tcx.cstore.intr, cdata, def.node, tcx)
119+
}
120+
121+
pub fn get_method_name_and_self_ty(cstore: @mut cstore::CStore,
122+
def: ast::def_id) -> (ast::ident, ast::self_ty_)
123+
{
124+
let cdata = cstore::get_crate_data(cstore, def.crate);
125+
decoder::get_method_name_and_self_ty(cstore.intr, cdata, def.node)
126+
}
127+
128+
pub fn get_trait_method_def_ids(cstore: @mut cstore::CStore,
129+
def: ast::def_id) -> ~[ast::def_id] {
118130
let cdata = cstore::get_crate_data(cstore, def.crate);
119-
decoder::get_trait_methods(cstore.intr, cdata, def.node, tcx)
131+
decoder::get_trait_method_def_ids(cdata, def.node)
120132
}
121133

122134
pub fn get_provided_trait_methods(tcx: ty::ctxt,
@@ -133,13 +145,6 @@ pub fn get_supertraits(tcx: ty::ctxt, def: ast::def_id) -> ~[ty::t] {
133145
decoder::get_supertraits(cdata, def.node, tcx)
134146
}
135147

136-
pub fn get_method_names_if_trait(cstore: @mut cstore::CStore,
137-
def: ast::def_id)
138-
-> Option<~[(ast::ident, ast::self_ty_)]> {
139-
let cdata = cstore::get_crate_data(cstore, def.crate);
140-
return decoder::get_method_names_if_trait(cstore.intr, cdata, def.node);
141-
}
142-
143148
pub fn get_type_name_if_impl(cstore: @mut cstore::CStore, def: ast::def_id)
144149
-> Option<ast::ident> {
145150
let cdata = cstore::get_crate_data(cstore, def.crate);

src/librustc/metadata/decoder.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use metadata::csearch::{ProvidedTraitMethodInfo, StaticMethodInfo};
1919
use metadata::csearch;
2020
use metadata::cstore;
2121
use metadata::decoder;
22-
use metadata::tydecode::{parse_ty_data, parse_def_id, parse_bounds_data};
22+
use metadata::tydecode::{parse_ty_data, parse_def_id, parse_bounds_data,
23+
parse_bare_fn_ty_data};
2324
use middle::{ty, resolve};
2425

2526
use core::hash::HashUtil;
@@ -229,6 +230,12 @@ fn doc_type(doc: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::t {
229230
|_, did| translate_def_id(cdata, did))
230231
}
231232
233+
fn doc_method_fty(doc: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::BareFnTy {
234+
let tp = reader::get_doc(doc, tag_item_method_fty);
235+
parse_bare_fn_ty_data(tp.data, cdata.cnum, tp.start, tcx,
236+
|_, did| translate_def_id(cdata, did))
237+
}
238+
232239
pub fn item_type(item_id: ast::def_id, item: ebml::Doc,
233240
tcx: ty::ctxt, cdata: cmd) -> ty::t {
234241
let t = doc_type(item, tcx, cdata);
@@ -247,10 +254,11 @@ fn item_impl_traits(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ~[ty::t] {
247254
results
248255
}
249256
250-
fn item_ty_param_bounds(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd)
257+
fn item_ty_param_bounds(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd,
258+
tag: uint)
251259
-> @~[ty::param_bounds] {
252260
let mut bounds = ~[];
253-
for reader::tagged_docs(item, tag_items_data_item_ty_param_bounds) |p| {
261+
for reader::tagged_docs(item, tag) |p| {
254262
let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx,
255263
|_, did| translate_def_id(cdata, did));
256264
bounds.push(bd);
@@ -338,7 +346,8 @@ fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)
338346
let enum_did = item_reqd_and_translated_parent_item(cnum, item);
339347
dl_def(ast::def_variant(enum_did, did))
340348
}
341-
Trait | Enum => dl_def(ast::def_ty(did)),
349+
Trait => dl_def(ast::def_trait(did)),
350+
Enum => dl_def(ast::def_ty(did)),
342351
Impl => dl_impl(did),
343352
PublicField | PrivateField | InheritedField => dl_field,
344353
}
@@ -359,7 +368,7 @@ pub fn get_type(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
359368
let t = item_type(ast::def_id { crate: cdata.cnum, node: id }, item, tcx,
360369
cdata);
361370
let tp_bounds = if family_has_type_params(item_family(item)) {
362-
item_ty_param_bounds(item, tcx, cdata)
371+
item_ty_param_bounds(item, tcx, cdata, tag_items_data_item_ty_param_bounds)
363372
} else { @~[] };
364373
let rp = item_ty_region_param(item);
365374
ty::ty_param_bounds_and_ty {
@@ -690,36 +699,46 @@ pub fn get_impls_for_mod(intr: @ident_interner,
690699
@result
691700
}
692701

693-
/* Works for both classes and traits */
694-
pub fn get_trait_methods(intr: @ident_interner, cdata: cmd, id: ast::node_id,
695-
tcx: ty::ctxt) -> @~[ty::method] {
702+
pub fn get_method_name_and_self_ty(
703+
intr: @ident_interner,
704+
cdata: cmd,
705+
id: ast::node_id) -> (ast::ident, ast::self_ty_)
706+
{
707+
let method_doc = lookup_item(id, cdata.data);
708+
let name = item_name(intr, method_doc);
709+
let self_ty = get_self_ty(method_doc);
710+
(name, self_ty)
711+
}
712+
713+
pub fn get_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
714+
tcx: ty::ctxt) -> ty::method
715+
{
716+
let method_doc = lookup_item(id, cdata.data);
717+
let bounds = item_ty_param_bounds(method_doc, tcx, cdata,
718+
tag_item_method_tps);
719+
let name = item_name(intr, method_doc);
720+
let def_id = item_def_id(method_doc, cdata);
721+
let fty = doc_method_fty(method_doc, tcx, cdata);
722+
let self_ty = get_self_ty(method_doc);
723+
ty::method {
724+
ident: name,
725+
tps: bounds,
726+
fty: fty,
727+
self_ty: self_ty,
728+
vis: ast::public,
729+
def_id: def_id
730+
}
731+
}
732+
733+
pub fn get_trait_method_def_ids(cdata: cmd,
734+
id: ast::node_id) -> ~[ast::def_id] {
696735
let data = cdata.data;
697736
let item = lookup_item(id, data);
698737
let mut result = ~[];
699738
for reader::tagged_docs(item, tag_item_trait_method) |mth| {
700-
let bounds = item_ty_param_bounds(mth, tcx, cdata);
701-
let name = item_name(intr, mth);
702-
let ty = doc_type(mth, tcx, cdata);
703-
let def_id = item_def_id(mth, cdata);
704-
let fty = match ty::get(ty).sty {
705-
ty::ty_bare_fn(ref f) => copy *f,
706-
_ => {
707-
tcx.diag.handler().bug(
708-
~"get_trait_methods: id has non-function type");
709-
}
710-
};
711-
let self_ty = get_self_ty(mth);
712-
result.push(ty::method {
713-
ident: name,
714-
tps: bounds,
715-
fty: fty,
716-
self_ty: self_ty,
717-
vis: ast::public,
718-
def_id: def_id
719-
});
739+
result.push(item_def_id(mth, cdata));
720740
}
721-
debug!("get_trait_methods: }");
722-
@result
741+
result
723742
}
724743

725744
pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd,
@@ -734,7 +753,8 @@ pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd,
734753

735754
let did = item_def_id(mth, cdata);
736755

737-
let bounds = item_ty_param_bounds(mth, tcx, cdata);
756+
let bounds = item_ty_param_bounds(mth, tcx, cdata,
757+
tag_items_data_item_ty_param_bounds);
738758
let name = item_name(intr, mth);
739759
let ty = doc_type(mth, tcx, cdata);
740760

@@ -777,26 +797,6 @@ pub fn get_supertraits(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
777797
return results;
778798
}
779799

780-
// If the item in question is a trait, returns its set of methods and
781-
// their self types. Otherwise, returns none. This overlaps in an
782-
// annoying way with get_trait_methods.
783-
pub fn get_method_names_if_trait(intr: @ident_interner, cdata: cmd,
784-
node_id: ast::node_id)
785-
-> Option<~[(ast::ident, ast::self_ty_)]> {
786-
787-
let item = lookup_item(node_id, cdata.data);
788-
if item_family(item) != Trait {
789-
return None;
790-
}
791-
792-
let mut resulting_methods = ~[];
793-
for reader::tagged_docs(item, tag_item_trait_method) |method| {
794-
resulting_methods.push(
795-
(item_name(intr, method), get_self_ty(method)));
796-
}
797-
return Some(resulting_methods);
798-
}
799-
800800
pub fn get_type_name_if_impl(intr: @ident_interner,
801801
cdata: cmd,
802802
node_id: ast::node_id) -> Option<ast::ident> {

0 commit comments

Comments
 (0)