Skip to content

Commit 0608e27

Browse files
committed
Add some helper functions for attributes. Issue #487
1 parent 7554423 commit 0608e27

File tree

4 files changed

+79
-201
lines changed

4 files changed

+79
-201
lines changed

src/comp/metadata/decoder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ fn list_crate_attributes(&ebml::doc md, io::writer out) {
328328
out.write_str("\n\n");
329329
}
330330

331+
fn get_crate_attributes(&vec[u8] data) -> vec[ast::attribute] {
332+
ret get_attributes(ebml::new_doc(data));
333+
}
334+
331335
fn list_crate_items(vec[u8] bytes, &ebml::doc md, io::writer out) {
332336
out.write_str("=Items=\n");
333337
auto paths = ebml::get_doc(md, tag_paths);

src/comp/middle/attr.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import std::vec;
2+
import std::option;
3+
import front::ast;
4+
5+
export get_linkage_metas;
6+
export find_attrs_by_name;
7+
8+
// From a list of crate attributes get only the meta_items that impact crate
9+
// linkage
10+
fn find_linkage_metas(vec[ast::attribute] attrs) -> vec[@ast::meta_item] {
11+
let vec[@ast::meta_item] metas = [];
12+
for (ast::attribute attr in find_attrs_by_name(attrs, "link")) {
13+
alt (attr.node.value.node) {
14+
case (ast::meta_list(_, ?items)) {
15+
metas += items;
16+
}
17+
case (_) {
18+
// FIXME: Maybe need a warning that this attr isn't
19+
// being used for linkage
20+
}
21+
}
22+
}
23+
ret metas;
24+
}
25+
26+
// Search a list of attributes and return only those with a specific name
27+
fn find_attrs_by_name(vec[ast::attribute] attrs,
28+
ast::ident name) -> vec[ast::attribute] {
29+
auto filter = bind fn(&ast::attribute a,
30+
ast::ident name) -> option::t[ast::attribute] {
31+
if (get_attr_name(a) == name) {
32+
option::some(a)
33+
} else {
34+
option::none
35+
}
36+
} (_, name);
37+
ret vec::filter_map(filter, attrs);
38+
}
39+
40+
fn get_attr_name(&ast::attribute attr) -> ast::ident {
41+
get_meta_item_name(@attr.node.value)
42+
}
43+
44+
fn find_meta_items_by_name(vec[@ast::meta_item] metas,
45+
ast::ident name) -> vec[@ast::meta_item] {
46+
auto filter = bind fn(&@ast::meta_item m,
47+
ast::ident name) -> option::t[@ast::meta_item] {
48+
if (get_meta_item_name(m) == name) {
49+
option::some(m)
50+
} else {
51+
option::none
52+
}
53+
} (_, name);
54+
ret vec::filter_map(filter, metas);
55+
}
56+
57+
fn get_meta_item_name(&@ast::meta_item meta) -> ast::ident {
58+
alt (meta.node) {
59+
case (ast::meta_word(?n)) { n }
60+
case (ast::meta_name_value(?n, _)) { n }
61+
case (ast::meta_list(?n, _)) { n }
62+
}
63+
}
64+
65+
//
66+
// Local Variables:
67+
// mode: rust
68+
// fill-column: 78;
69+
// indent-tabs-mode: nil
70+
// c-basic-offset: 4
71+
// buffer-file-coding-system: utf-8-unix
72+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
73+
// End:
74+
//

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std (name = "std",
1414
url = "http://rust-lang.org/src/std");
1515

1616
mod middle {
17+
mod attr;
1718
mod trans;
1819
mod ty;
1920
mod walk;

src/test/run-pass/item-attributes.rs

Lines changed: 0 additions & 201 deletions
This file was deleted.

0 commit comments

Comments
 (0)