Extract meta_item sorting from back::link to middle::attr

This commit is contained in:
Brian Anderson 2011-06-28 15:46:09 -07:00
parent aecc5e6e0f
commit 8ac60e99ea
2 changed files with 40 additions and 23 deletions

View File

@ -306,32 +306,15 @@ fn crate_link_metas(&ast::crate c) -> link_metas {
// This calculates CMH as defined above
fn crate_meta_extras_hash(sha1 sha, &ast::crate crate) -> str {
// FIXME (#487) Move this sorting stuff into middle::attr
fn lteq(&@ast::meta_item ma, &@ast::meta_item mb) -> bool {
fn key(&@ast::meta_item m) -> ast::ident {
alt (m.node) {
case (ast::meta_word(?name)) {
name
}
case (ast::meta_name_value(?name, _)) {
name
}
case (ast::meta_list(?name, _)) {
name
}
}
}
ret key(ma) <= key(mb);
}
fn len_and_str(&str s) -> str { ret #fmt("%u_%s", str::byte_len(s), s); }
auto cmh_items = {
auto cmh_items = crate_link_metas(crate).cmh_items;
attr::sort_meta_items(cmh_items)
};
let vec[mutable @ast::meta_item] v = [mutable ];
for (@ast::meta_item mi in crate_link_metas(crate).cmh_items) {
v += [mutable mi];
}
sort::quick_sort(lteq, v);
sha.reset();
for (@ast::meta_item m_ in v) {
for (@ast::meta_item m_ in cmh_items) {
auto m = m_;
alt (m.node) {
case (ast::meta_name_value(?key, ?value)) {

View File

@ -8,6 +8,7 @@
export find_linkage_metas;
export find_attrs_by_name;
export contains;
export sort_meta_items;
// From a list of crate attributes get only the meta_items that impact crate
// linkage
@ -103,6 +104,39 @@ fn contains(&vec[@ast::meta_item] haystack, @ast::meta_item needle) -> bool {
ret false;
}
fn sort_meta_items(&vec[@ast::meta_item] items) -> vec[@ast::meta_item] {
fn lteq(&@ast::meta_item ma, &@ast::meta_item mb) -> bool {
fn key(&@ast::meta_item m) -> ast::ident {
alt (m.node) {
case (ast::meta_word(?name)) {
name
}
case (ast::meta_name_value(?name, _)) {
name
}
case (ast::meta_list(?name, _)) {
name
}
}
}
ret key(ma) <= key(mb);
}
// This is sort of stupid here, converting to a vec of mutables and back
let vec[mutable @ast::meta_item] v = [mutable ];
for (@ast::meta_item mi in items) {
v += [mutable mi];
}
std::sort::quick_sort(lteq, v);
let vec[@ast::meta_item] v2 = [];
for (@ast::meta_item mi in v) {
v2 += [mi]
}
ret v2;
}
//
// Local Variables:
// mode: rust