提交 0226f561 编写于 作者: P Patrick Walton

rustc: Convert attribute in the AST to interior vectors

上级 7714cb29
// Functions dealing with attributes and meta_items
import std::ivec;
import std::vec;
import std::str;
import std::map;
......@@ -28,7 +29,7 @@
// From a list of crate attributes get only the meta_items that impact crate
// linkage
fn find_linkage_metas(vec[ast::attribute] attrs) -> vec[@ast::meta_item] {
fn find_linkage_metas(&ast::attribute[] attrs) -> vec[@ast::meta_item] {
let vec[@ast::meta_item] metas = [];
for (ast::attribute attr in find_attrs_by_name(attrs, "link")) {
alt (attr.node.value.node) {
......@@ -44,8 +45,8 @@ fn find_linkage_metas(vec[ast::attribute] attrs) -> vec[@ast::meta_item] {
}
// Search a list of attributes and return only those with a specific name
fn find_attrs_by_name(vec[ast::attribute] attrs,
ast::ident name) -> vec[ast::attribute] {
fn find_attrs_by_name(&ast::attribute[] attrs,
ast::ident name) -> ast::attribute[] {
auto filter = bind fn(&ast::attribute a,
ast::ident name) -> option::t[ast::attribute] {
if (get_attr_name(a) == name) {
......@@ -54,7 +55,7 @@ fn find_attrs_by_name(vec[ast::attribute] attrs,
option::none
}
} (_, name);
ret vec::filter_map(filter, attrs);
ret ivec::filter_map(filter, attrs);
}
fn get_attr_name(&ast::attribute attr) -> ast::ident {
......@@ -101,8 +102,10 @@ fn get_meta_item_value_str(&@ast::meta_item meta) -> option::t[str] {
fn attr_meta(&ast::attribute attr) -> @ast::meta_item { @attr.node.value }
// Get the meta_items from inside a vector of attributes
fn attr_metas(&vec[ast::attribute] attrs) -> vec[@ast::meta_item] {
ret vec::map(attr_meta, attrs);
fn attr_metas(&ast::attribute[] attrs) -> vec[@ast::meta_item] {
auto mitems = [];
for (ast::attribute a in attrs) { mitems += [attr_meta(a)]; }
ret mitems;
}
fn eq(@ast::meta_item a, @ast::meta_item b) -> bool {
......
import std::ivec;
import std::option;
import std::vec;
import syntax::ast;
......@@ -97,11 +98,11 @@ fn native_item_in_cfg(&ast::crate_cfg cfg, &@ast::native_item item) -> bool {
// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(&ast::crate_cfg cfg, &vec[ast::attribute] attrs) -> bool {
fn in_cfg(&ast::crate_cfg cfg, &ast::attribute[] attrs) -> bool {
// The "cfg" attributes on the item
auto item_cfg_attrs = attr::find_attrs_by_name(attrs, "cfg");
auto item_has_cfg_attrs = vec::len(item_cfg_attrs) > 0u;
auto item_has_cfg_attrs = ivec::len(item_cfg_attrs) > 0u;
if (!item_has_cfg_attrs) { ret true; }
// Pull the inner meta_items from the #[cfg(meta_item, ...)] attributes,
......
......@@ -290,8 +290,8 @@ fn get_meta_items(&ebml::doc md) -> vec[@ast::meta_item] {
ret items;
}
fn get_attributes(&ebml::doc md) -> vec[ast::attribute] {
let vec[ast::attribute] attrs = [];
fn get_attributes(&ebml::doc md) -> ast::attribute[] {
let ast::attribute[] attrs = ~[];
alt (ebml::maybe_get_doc(md, tag_attributes)) {
case (option::some(?attrs_d)) {
for each (ebml::doc attr_doc in
......@@ -301,7 +301,7 @@ fn get_attributes(&ebml::doc md) -> vec[ast::attribute] {
// an attribute
assert (vec::len(meta_items) == 1u);
auto meta_item = meta_items.(0);
attrs += [rec(node=rec(style=ast::attr_outer,
attrs += ~[rec(node=rec(style=ast::attr_outer,
value=*meta_item),
span=rec(lo=0u, hi=0u))];
}
......@@ -327,7 +327,7 @@ fn list_crate_attributes(&ebml::doc md, io::writer out) {
out.write_str("\n\n");
}
fn get_crate_attributes(&vec[u8] data) -> vec[ast::attribute] {
fn get_crate_attributes(&vec[u8] data) -> ast::attribute[] {
ret get_attributes(ebml::new_doc(data));
}
......
......@@ -87,13 +87,13 @@ fn def_id_of_def(def d) -> def_id {
type crate_ = rec(vec[@crate_directive] directives,
_mod module,
vec[attribute] attrs,
attribute[] attrs,
crate_cfg config);
tag crate_directive_ {
cdir_src_mod(ident, option::t[filename], vec[attribute]);
cdir_src_mod(ident, option::t[filename], attribute[]);
cdir_dir_mod(ident, option::t[filename],
vec[@crate_directive], vec[attribute]);
vec[@crate_directive], attribute[]);
cdir_view_item(@view_item);
cdir_syntax(path);
cdir_auth(path, _auth);
......@@ -526,7 +526,7 @@ fn ty_mach_to_str(ty_mach tm) -> str {
type attribute_ = rec(attr_style style, meta_item value);
type item = rec(ident ident,
vec[attribute] attrs,
attribute[] attrs,
node_id id, // For objs and resources, this is the type def_id
item_ node,
span span);
......@@ -544,7 +544,7 @@ fn ty_mach_to_str(ty_mach tm) -> str {
}
type native_item = rec(ident ident,
vec[attribute] attrs,
attribute[] attrs,
native_item_ node,
node_id id,
span span);
......
......@@ -132,10 +132,10 @@ fn noop_fold_crate(&crate_ c, ast_fold fld) -> crate_ {
auto fold_meta_item = bind fold_meta_item_(_,fld);
auto fold_attribute = bind fold_attribute_(_,fold_meta_item);
ret rec(directives=map(fld.fold_crate_directive, c.directives),
ret rec(directives=vec::map(fld.fold_crate_directive, c.directives),
module=fld.fold_mod(c.module),
attrs=map(fold_attribute, c.attrs),
config=map(fold_meta_item, c.config));
attrs=ivec::map(fold_attribute, c.attrs),
config=vec::map(fold_meta_item, c.config));
}
fn noop_fold_crate_directive(&crate_directive_ cd, ast_fold fld)
......@@ -167,7 +167,7 @@ fn noop_fold_native_item(&@native_item ni, ast_fold fld) -> @native_item {
auto fold_attribute = bind fold_attribute_(_,fold_meta_item);
ret @rec(ident=fld.fold_ident(ni.ident),
attrs=map(fold_attribute, ni.attrs),
attrs=ivec::map(fold_attribute, ni.attrs),
node=alt (ni.node) {
case (native_item_ty) { native_item_ty }
case (native_item_fn(?st, ?fdec, ?typms)) {
......@@ -189,7 +189,7 @@ fn noop_fold_item(&@item i, ast_fold fld) -> @item {
auto fold_attribute = bind fold_attribute_(_,fold_meta_item);
ret @rec(ident=fld.fold_ident(i.ident),
attrs=map(fold_attribute,i.attrs),
attrs=ivec::map(fold_attribute,i.attrs),
id=i.id, node=fld.fold_item_underscore(i.node),
span=i.span);
}
......
......@@ -1479,7 +1479,7 @@ fn parse_stmt(&parser p) -> @ast::stmt {
}
fn parse_crate_stmt(&parser p) -> @ast::stmt {
auto cdir = parse_crate_directive(p, []);
auto cdir = parse_crate_directive(p, ~[]);
ret @spanned(cdir.span.lo, cdir.span.hi,
ast::stmt_crate_directive(@cdir));
}
......@@ -1497,7 +1497,7 @@ fn parse_source_stmt(&parser p) -> @ast::stmt {
auto item_attrs;
alt (parse_outer_attrs_or_ext(p)) {
case (none) {
item_attrs = [];
item_attrs = ~[];
}
case (some(left(?attrs))) {
item_attrs = attrs;
......@@ -1511,7 +1511,7 @@ fn parse_source_stmt(&parser p) -> @ast::stmt {
auto maybe_item = parse_item(p, item_attrs);
// If we have attributes then we should have an item
if (vec::len(item_attrs) > 0u) {
if (ivec::len(item_attrs) > 0u) {
alt (maybe_item) {
case (got_item(_)) { /* fallthrough */ }
case (_) {
......@@ -1717,7 +1717,7 @@ fn parse_fn_header(&parser p) -> tup(ast::ident, vec[ast::ty_param]) {
}
fn mk_item(&parser p, uint lo, uint hi, &ast::ident ident, &ast::item_ node,
&vec[ast::attribute] attrs) -> @ast::item {
&ast::attribute[] attrs) -> @ast::item {
ret @rec(ident=ident,
attrs=attrs,
id=p.get_id(),
......@@ -1726,7 +1726,7 @@ fn mk_item(&parser p, uint lo, uint hi, &ast::ident ident, &ast::item_ node,
}
fn parse_item_fn_or_iter(&parser p, ast::purity purity, ast::proto proto,
vec[ast::attribute] attrs) -> @ast::item {
&ast::attribute[] attrs) -> @ast::item {
auto lo = p.get_last_lo_pos();
auto t = parse_fn_header(p);
auto f = parse_fn(p, proto, purity);
......@@ -1777,7 +1777,7 @@ fn parse_dtor(&parser p) -> @ast::method {
ret @spanned(lo, f.body.span.hi, m);
}
fn parse_item_obj(&parser p, ast::layer lyr, vec[ast::attribute] attrs) ->
fn parse_item_obj(&parser p, ast::layer lyr, &ast::attribute[] attrs) ->
@ast::item {
auto lo = p.get_last_lo_pos();
auto ident = parse_value_ident(p);
......@@ -1800,7 +1800,7 @@ fn parse_item_obj(&parser p, ast::layer lyr, vec[ast::attribute] attrs) ->
p.get_id()), attrs);
}
fn parse_item_res(&parser p, ast::layer lyr, vec[ast::attribute] attrs) ->
fn parse_item_res(&parser p, ast::layer lyr, &ast::attribute[] attrs) ->
@ast::item {
auto lo = p.get_last_lo_pos();
auto ident = parse_value_ident(p);
......@@ -1822,8 +1822,8 @@ fn parse_item_res(&parser p, ast::layer lyr, vec[ast::attribute] attrs) ->
}
fn parse_mod_items(&parser p, token::token term,
vec[ast::attribute] first_item_attrs) -> ast::_mod {
auto view_items = if (vec::len(first_item_attrs) == 0u) {
&ast::attribute[] first_item_attrs) -> ast::_mod {
auto view_items = if (ivec::len(first_item_attrs) == 0u) {
parse_view(p)
} else {
// Shouldn't be any view items since we've already parsed an item attr
......@@ -1833,7 +1833,7 @@ fn parse_mod_items(&parser p, token::token term,
auto initial_attrs = first_item_attrs;
while (p.peek() != term) {
auto attrs = initial_attrs + parse_outer_attributes(p);
initial_attrs = [];
initial_attrs = ~[];
alt (parse_item(p, attrs)) {
case (got_item(?i)) { vec::push(items, i); }
case (_) {
......@@ -1845,7 +1845,7 @@ fn parse_mod_items(&parser p, token::token term,
ret rec(view_items=view_items, items=items);
}
fn parse_item_const(&parser p, vec[ast::attribute] attrs) -> @ast::item {
fn parse_item_const(&parser p, &ast::attribute[] attrs) -> @ast::item {
auto lo = p.get_last_lo_pos();
auto ty = parse_ty(p);
auto id = parse_value_ident(p);
......@@ -1856,21 +1856,20 @@ fn parse_item_const(&parser p, vec[ast::attribute] attrs) -> @ast::item {
ret mk_item(p, lo, hi, id, ast::item_const(ty, e), attrs);
}
fn parse_item_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
fn parse_item_mod(&parser p, &ast::attribute[] attrs) -> @ast::item {
auto lo = p.get_last_lo_pos();
auto id = parse_ident(p);
expect(p, token::LBRACE);
auto inner_attrs = parse_inner_attrs_and_next(p);
auto first_item_outer_attrs = inner_attrs._1;
auto m = parse_mod_items(p, token::RBRACE,
first_item_outer_attrs);
auto m = parse_mod_items(p, token::RBRACE, first_item_outer_attrs);
auto hi = p.get_hi_pos();
expect(p, token::RBRACE);
ret mk_item(p, lo, hi, id, ast::item_mod(m), attrs + inner_attrs._0);
}
fn parse_item_native_type(&parser p,
&vec[ast::attribute] attrs) -> @ast::native_item {
fn parse_item_native_type(&parser p, &ast::attribute[] attrs)
-> @ast::native_item {
auto t = parse_type_decl(p);
auto hi = p.get_hi_pos();
expect(p, token::SEMI);
......@@ -1881,8 +1880,8 @@ fn parse_item_native_type(&parser p,
span=rec(lo=t._0, hi=hi));
}
fn parse_item_native_fn(&parser p,
&vec[ast::attribute] attrs) -> @ast::native_item {
fn parse_item_native_fn(&parser p, &ast::attribute[] attrs)
-> @ast::native_item {
auto lo = p.get_last_lo_pos();
auto t = parse_fn_header(p);
auto decl = parse_fn_decl(p, ast::impure_fn);
......@@ -1900,8 +1899,8 @@ fn parse_item_native_fn(&parser p,
span=rec(lo=lo, hi=hi));
}
fn parse_native_item(&parser p,
&vec[ast::attribute] attrs) -> @ast::native_item {
fn parse_native_item(&parser p, &ast::attribute[] attrs)
-> @ast::native_item {
parse_layer(p);
if (eat_word(p, "type")) {
ret parse_item_native_type(p, attrs);
......@@ -1911,9 +1910,9 @@ fn parse_native_item(&parser p,
}
fn parse_native_mod_items(&parser p, &str native_name, ast::native_abi abi,
&vec[ast::attribute] first_item_attrs) ->
ast::native_mod {
auto view_items = if (vec::len(first_item_attrs) == 0u) {
&ast::attribute[] first_item_attrs)
-> ast::native_mod {
auto view_items = if (ivec::len(first_item_attrs) == 0u) {
parse_native_view(p)
} else {
// Shouldn't be any view items since we've already parsed an item attr
......@@ -1923,7 +1922,7 @@ fn parse_native_mod_items(&parser p, &str native_name, ast::native_abi abi,
auto initial_attrs = first_item_attrs;
while (p.peek() != token::RBRACE) {
auto attrs = initial_attrs + parse_outer_attributes(p);
initial_attrs = [];
initial_attrs = ~[];
items += [parse_native_item(p, attrs)];
}
ret rec(native_name=native_name,
......@@ -1932,7 +1931,7 @@ fn parse_native_mod_items(&parser p, &str native_name, ast::native_abi abi,
items=items);
}
fn parse_item_native_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
fn parse_item_native_mod(&parser p, &ast::attribute[] attrs) -> @ast::item {
auto lo = p.get_last_lo_pos();
auto abi = ast::native_abi_cdecl;
if (!is_word(p, "mod")) {
......@@ -1972,7 +1971,7 @@ fn parse_type_decl(&parser p) -> tup(uint, ast::ident) {
ret tup(lo, id);
}
fn parse_item_type(&parser p, vec[ast::attribute] attrs) -> @ast::item {
fn parse_item_type(&parser p, &ast::attribute[] attrs) -> @ast::item {
auto t = parse_type_decl(p);
auto tps = parse_ty_params(p);
expect(p, token::EQ);
......@@ -1982,7 +1981,7 @@ fn parse_item_type(&parser p, vec[ast::attribute] attrs) -> @ast::item {
ret mk_item(p, t._0, hi, t._1, ast::item_ty(ty, tps), attrs);
}
fn parse_item_tag(&parser p, vec[ast::attribute] attrs) -> @ast::item {
fn parse_item_tag(&parser p, &ast::attribute[] attrs) -> @ast::item {
auto lo = p.get_last_lo_pos();
auto id = parse_ident(p);
auto ty_params = parse_ty_params(p);
......@@ -2061,7 +2060,7 @@ fn parse_auth(&parser p) -> ast::_auth {
tag parsed_item { got_item(@ast::item); no_item; fn_no_item; }
fn parse_item(&parser p, vec[ast::attribute] attrs) -> parsed_item {
fn parse_item(&parser p, &ast::attribute[] attrs) -> parsed_item {
if (eat_word(p, "const")) {
ret got_item(parse_item_const(p, attrs));
} else if (eat_word(p, "fn")) {
......@@ -2095,8 +2094,7 @@ fn parse_item(&parser p, vec[ast::attribute] attrs) -> parsed_item {
// A type to distingush between the parsing of item attributes or syntax
// extensions, which both begin with token.POUND
type attr_or_ext = option::t[either::t[vec[ast::attribute],
@ast::expr]];
type attr_or_ext = option::t[either::t[ast::attribute[], @ast::expr]];
fn parse_outer_attrs_or_ext(&parser p) -> attr_or_ext {
if (p.peek() == token::POUND) {
......@@ -2104,7 +2102,7 @@ fn parse_outer_attrs_or_ext(&parser p) -> attr_or_ext {
p.bump();
if (p.peek() == token::LBRACKET) {
auto first_attr = parse_attribute_naked(p, ast::attr_outer, lo);
ret some(left([first_attr] + parse_outer_attributes(p)));
ret some(left(~[first_attr] + parse_outer_attributes(p)));
} else {
ret some(right(parse_syntax_ext_naked(p, lo)));
}
......@@ -2114,10 +2112,10 @@ fn parse_outer_attrs_or_ext(&parser p) -> attr_or_ext {
}
// Parse attributes that appear before an item
fn parse_outer_attributes(&parser p) -> vec[ast::attribute] {
let vec[ast::attribute] attrs = [];
fn parse_outer_attributes(&parser p) -> ast::attribute[] {
let ast::attribute[] attrs = ~[];
while (p.peek() == token::POUND) {
attrs += [parse_attribute(p, ast::attr_outer)];
attrs += ~[parse_attribute(p, ast::attr_outer)];
}
ret attrs;
}
......@@ -2143,22 +2141,22 @@ fn parse_attribute_naked(&parser p, ast::attr_style style,
// next item (since we can't know whether the attribute is an inner attribute
// of the containing item or an outer attribute of the first contained item
// until we see the semi).
fn parse_inner_attrs_and_next(&parser p) -> tup(vec[ast::attribute],
vec[ast::attribute]) {
let vec[ast::attribute] inner_attrs = [];
let vec[ast::attribute] next_outer_attrs = [];
fn parse_inner_attrs_and_next(&parser p) -> tup(ast::attribute[],
ast::attribute[]) {
let ast::attribute[] inner_attrs = ~[];
let ast::attribute[] next_outer_attrs = ~[];
while (p.peek() == token::POUND) {
auto attr = parse_attribute(p, ast::attr_inner);
if (p.peek() == token::SEMI) {
p.bump();
inner_attrs += [attr];
inner_attrs += ~[attr];
} else {
// It's not really an inner attribute
auto outer_attr = spanned(attr.span.lo,
attr.span.hi,
rec(style=ast::attr_outer,
value=attr.node.value));
next_outer_attrs += [outer_attr];
next_outer_attrs += ~[outer_attr];
break;
}
}
......@@ -2339,8 +2337,7 @@ fn parse_crate_from_source_file(&str input, &ast::crate_cfg cfg,
auto first_item_outer_attrs = crate_attrs._1;
auto m = parse_mod_items(p, token::EOF,
first_item_outer_attrs);
let vec[@ast::crate_directive] cdirs = [];
ret @spanned(lo, p.get_lo_pos(), rec(directives=cdirs,
ret @spanned(lo, p.get_lo_pos(), rec(directives=[],
module=m,
attrs=crate_attrs._0,
config=p.get_cfg()));
......@@ -2361,14 +2358,13 @@ fn parse_str(&parser p) -> ast::ident {
// Each crate file is a sequence of directives.
//
// Each directive imperatively extends its environment with 0 or more items.
fn parse_crate_directive(&parser p, vec[ast::attribute] first_outer_attr)
fn parse_crate_directive(&parser p, &ast::attribute[] first_outer_attr)
-> ast::crate_directive {
// Collect the next attributes
auto outer_attrs = first_outer_attr
+ parse_outer_attributes(p);
auto outer_attrs = first_outer_attr + parse_outer_attributes(p);
// In a crate file outer attributes are only going to apply to mods
auto expect_mod = vec::len(outer_attrs) > 0u;
auto expect_mod = ivec::len(outer_attrs) > 0u;
auto lo = p.get_lo_pos();
if (expect_mod || is_word(p, "mod")) {
......@@ -2423,13 +2419,13 @@ fn parse_crate_directive(&parser p, vec[ast::attribute] first_outer_attr)
}
fn parse_crate_directives(&parser p, token::token term,
vec[ast::attribute] first_outer_attr) ->
&ast::attribute[] first_outer_attr) ->
vec[@ast::crate_directive] {
// This is pretty ugly. If we have an outer attribute then we can't accept
// seeing the terminator next, so if we do see it then fail the same way
// parse_crate_directive would
if (vec::len(first_outer_attr) > 0u && p.peek() == term) {
if (ivec::len(first_outer_attr) > 0u && p.peek() == term) {
expect_word(p, "mod");
}
......
......@@ -242,7 +242,7 @@ fn commasep_exprs(&ps s, breaks b, vec[@ast::expr] exprs) {
commasep_cmnt(s, b, exprs, print_expr, expr_span);
}
fn print_mod(&ps s, ast::_mod _mod, &vec[ast::attribute] attrs) {
fn print_mod(&ps s, ast::_mod _mod, &ast::attribute[] attrs) {
print_inner_attributes(s, attrs);
for (@ast::view_item vitem in _mod.view_items) {
print_view_item(s, vitem);
......@@ -533,7 +533,7 @@ fn print_field(&ps s, &ast::obj_field field) {
s.ann.post(ann_node);
}
fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) {
fn print_outer_attributes(&ps s, &ast::attribute[] attrs) {
auto count = 0;
for (ast::attribute attr in attrs) {
alt (attr.node.style) {
......@@ -544,7 +544,7 @@ fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) {
if (count > 0) { hardbreak_if_not_bol(s); }
}
fn print_inner_attributes(&ps s, vec[ast::attribute] attrs) {
fn print_inner_attributes(&ps s, &ast::attribute[] attrs) {
auto count = 0;
for (ast::attribute attr in attrs) {
alt (attr.node.style) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册