|
| 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 | +// |
0 commit comments