diff options
| author | bors <bors@rust-lang.org> | 2013-07-20 20:25:31 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-07-20 20:25:31 -0700 |
| commit | d029ebfc5f69f830fe24b4c8a979970d9a7d297d (patch) | |
| tree | 80d9e825ff9f805d15c98441560c6decf85f0739 /src/libsyntax | |
| parent | 8476419fefda988f66ab6b2a1847e402133a0a29 (diff) | |
| parent | cc760a647ac0094814f592d08813ebae0b3bec47 (diff) | |
| download | rust-d029ebfc5f69f830fe24b4c8a979970d9a7d297d.tar.gz rust-d029ebfc5f69f830fe24b4c8a979970d9a7d297d.zip | |
auto merge of #7902 : huonw/rust/attr++, r=cmr,pcwalton
This does a number of things, but especially dramatically reduce the number of allocations performed for operations involving attributes/ meta items: - Converts ast::meta_item & ast::attribute and other associated enums to CamelCase. - Converts several standalone functions in syntax::attr into methods, defined on two traits AttrMetaMethods & AttributeMethods. The former is common to both MetaItem and Attribute since the latter is a thin wrapper around the former. - Deletes functions that are unnecessary due to iterators. - Converts other standalone functions to use iterators and the generic AttrMetaMethods rather than allocating a lot of new vectors (e.g. the old code would have to allocate a new vector to use functions that operated on &[meta_item] on &[attribute].) - Moves the core algorithm of the #[cfg] matching to syntax::attr, similar to find_inline_attr and find_linkage_metas. This doesn't have much of an effect on the speed of #[cfg] stripping, despite hugely reducing the number of allocations performed; presumably most of the time is spent in the ast folder rather than doing attribute checks. Also fixes the Eq instance of MetaItem_ to correctly ignore spans, so that `rustc --cfg 'foo(bar)'` now works.
Diffstat (limited to 'src/libsyntax')
26 files changed, 441 insertions, 438 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 7fa2c2700c9..f2974423a1a 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -193,26 +193,51 @@ pub enum def { } -// The set of meta_items that define the compilation environment of the crate, +// The set of MetaItems that define the compilation environment of the crate, // used to drive conditional compilation -pub type crate_cfg = ~[@meta_item]; +pub type crate_cfg = ~[@MetaItem]; pub type crate = spanned<crate_>; #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub struct crate_ { module: _mod, - attrs: ~[attribute], + attrs: ~[Attribute], config: crate_cfg, } -pub type meta_item = spanned<meta_item_>; +pub type MetaItem = spanned<MetaItem_>; -#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] -pub enum meta_item_ { - meta_word(@str), - meta_list(@str, ~[@meta_item]), - meta_name_value(@str, lit), +#[deriving(Clone, Encodable, Decodable, IterBytes)] +pub enum MetaItem_ { + MetaWord(@str), + MetaList(@str, ~[@MetaItem]), + MetaNameValue(@str, lit), +} + +// can't be derived because the MetaList requires an unordered comparison +impl Eq for MetaItem_ { + fn eq(&self, other: &MetaItem_) -> bool { + match *self { + MetaWord(ref ns) => match *other { + MetaWord(ref no) => (*ns) == (*no), + _ => false + }, + MetaNameValue(ref ns, ref vs) => match *other { + MetaNameValue(ref no, ref vo) => { + (*ns) == (*no) && vs.node == vo.node + } + _ => false + }, + MetaList(ref ns, ref miss) => match *other { + MetaList(ref no, ref miso) => { + ns == no && + miss.iter().all(|mi| miso.iter().any(|x| x.node == mi.node)) + } + _ => false + } + } + } } //pub type blk = spanned<blk_>; @@ -622,7 +647,7 @@ pub type ty_field = spanned<ty_field_>; #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub struct ty_method { ident: ident, - attrs: ~[attribute], + attrs: ~[Attribute], purity: purity, decl: fn_decl, generics: Generics, @@ -833,7 +858,7 @@ pub type explicit_self = spanned<explicit_self_>; #[deriving(Eq, Encodable, Decodable,IterBytes)] pub struct method { ident: ident, - attrs: ~[attribute], + attrs: ~[Attribute], generics: Generics, explicit_self: explicit_self, purity: purity, @@ -886,7 +911,7 @@ pub struct enum_def { #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub struct variant_ { name: ident, - attrs: ~[attribute], + attrs: ~[Attribute], kind: variant_kind, id: node_id, disr_expr: Option<@expr>, @@ -925,34 +950,34 @@ pub enum view_path_ { #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub struct view_item { node: view_item_, - attrs: ~[attribute], + attrs: ~[Attribute], vis: visibility, span: span, } #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub enum view_item_ { - view_item_extern_mod(ident, ~[@meta_item], node_id), + view_item_extern_mod(ident, ~[@MetaItem], node_id), view_item_use(~[@view_path]), } // Meta-data associated with an item -pub type attribute = spanned<attribute_>; +pub type Attribute = spanned<Attribute_>; -// Distinguishes between attributes that decorate items and attributes that +// Distinguishes between Attributes that decorate items and Attributes that // are contained as statements within items. These two cases need to be // distinguished for pretty-printing. #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)] -pub enum attr_style { - attr_outer, - attr_inner, +pub enum AttrStyle { + AttrOuter, + AttrInner, } // doc-comments are promoted to attributes that have is_sugared_doc = true #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)] -pub struct attribute_ { - style: attr_style, - value: @meta_item, +pub struct Attribute_ { + style: AttrStyle, + value: @MetaItem, is_sugared_doc: bool, } @@ -990,7 +1015,7 @@ pub struct struct_field_ { kind: struct_field_kind, id: node_id, ty: Ty, - attrs: ~[attribute], + attrs: ~[Attribute], } pub type struct_field = spanned<struct_field_>; @@ -1016,7 +1041,7 @@ pub struct struct_def { #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub struct item { ident: ident, - attrs: ~[attribute], + attrs: ~[Attribute], id: node_id, node: item_, vis: visibility, @@ -1044,7 +1069,7 @@ pub enum item_ { #[deriving(Eq, Encodable, Decodable,IterBytes)] pub struct foreign_item { ident: ident, - attrs: ~[attribute], + attrs: ~[Attribute], node: foreign_item_, id: node_id, span: span, diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 7dd01a54f7a..3c560d211fd 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -8,313 +8,256 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Functions dealing with attributes and meta_items +// Functions dealing with attributes and meta items use extra; use ast; +use ast::{Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList}; use codemap::{spanned, dummy_spanned}; -use attr; use codemap::BytePos; use diagnostic::span_handler; use parse::comments::{doc_comment_style, strip_doc_comment_decoration}; use std::hashmap::HashSet; -/* Constructors */ - -pub fn mk_name_value_item_str(name: @str, value: @str) - -> @ast::meta_item { - let value_lit = dummy_spanned(ast::lit_str(value)); - mk_name_value_item(name, value_lit) -} - -pub fn mk_name_value_item(name: @str, value: ast::lit) - -> @ast::meta_item { - @dummy_spanned(ast::meta_name_value(name, value)) -} - -pub fn mk_list_item(name: @str, items: ~[@ast::meta_item]) -> - @ast::meta_item { - @dummy_spanned(ast::meta_list(name, items)) -} -pub fn mk_word_item(name: @str) -> @ast::meta_item { - @dummy_spanned(ast::meta_word(name)) +pub trait AttrMetaMethods { + // This could be changed to `fn check_name(&self, name: @str) -> + // bool` which would facilitate a side table recording which + // attributes/meta items are used/unused. + + /// Retrieve the name of the meta item, e.g. foo in #[foo], + /// #[foo="bar"] and #[foo(bar)] + fn name(&self) -> @str; + + /** + * Gets the string value if self is a MetaNameValue variant + * containing a string, otherwise None. + */ + fn value_str(&self) -> Option<@str>; + /// Gets a list of inner meta items from a list MetaItem type. + fn meta_item_list<'a>(&'a self) -> Option<&'a [@MetaItem]>; + + /** + * If the meta item is a name-value type with a string value then returns + * a tuple containing the name and string value, otherwise `None` + */ + fn name_str_pair(&self) -> Option<(@str, @str)>; } -pub fn mk_attr(item: @ast::meta_item) -> ast::attribute { - dummy_spanned(ast::attribute_ { style: ast::attr_inner, - value: item, - is_sugared_doc: false }) +impl AttrMetaMethods for Attribute { + fn name(&self) -> @str { self.meta().name() } + fn value_str(&self) -> Option<@str> { self.meta().value_str() } + fn meta_item_list<'a>(&'a self) -> Option<&'a [@MetaItem]> { + self.node.value.meta_item_list() + } + fn name_str_pair(&self) -> Option<(@str, @str)> { self.meta().name_str_pair() } } -pub fn mk_sugared_doc_attr(text: @str, - lo: BytePos, hi: BytePos) -> ast::attribute { - let style = doc_comment_style(text); - let lit = spanned(lo, hi, ast::lit_str(text)); - let attr = ast::attribute_ { - style: style, - value: @spanned(lo, hi, ast::meta_name_value(@"doc", lit)), - is_sugared_doc: true - }; - spanned(lo, hi, attr) -} +impl AttrMetaMethods for MetaItem { + fn name(&self) -> @str { + match self.node { + MetaWord(n) => n, + MetaNameValue(n, _) => n, + MetaList(n, _) => n + } + } -/* Conversion */ + fn value_str(&self) -> Option<@str> { + match self.node { + MetaNameValue(_, ref v) => { + match v.node { + ast::lit_str(s) => Some(s), + _ => None, + } + }, + _ => None + } + } -pub fn attr_meta(attr: ast::attribute) -> @ast::meta_item { - attr.node.value -} + fn meta_item_list<'a>(&'a self) -> Option<&'a [@MetaItem]> { + match self.node { + MetaList(_, ref l) => Some(l.as_slice()), + _ => None + } + } -// Get the meta_items from inside a vector of attributes -pub fn attr_metas(attrs: &[ast::attribute]) -> ~[@ast::meta_item] { - do attrs.map |a| { attr_meta(*a) } + pub fn name_str_pair(&self) -> Option<(@str, @str)> { + self.value_str().map_consume(|s| (self.name(), s)) + } } -pub fn desugar_doc_attr(attr: &ast::attribute) -> ast::attribute { - if attr.node.is_sugared_doc { - let comment = get_meta_item_value_str(attr.node.value).get(); - let meta = mk_name_value_item_str(@"doc", - strip_doc_comment_decoration(comment).to_managed()); - mk_attr(meta) - } else { - *attr +// Annoying, but required to get test_cfg to work +impl AttrMetaMethods for @MetaItem { + fn name(&self) -> @str { (**self).name() } + fn value_str(&self) -> Option<@str> { (**self).value_str() } + fn meta_item_list<'a>(&'a self) -> Option<&'a [@MetaItem]> { + (**self).meta_item_list() } + fn name_str_pair(&self) -> Option<(@str, @str)> { (**self).name_str_pair() } } -/* Accessors */ -pub fn get_attr_name(attr: &ast::attribute) -> @str { - get_meta_item_name(attr.node.value) +pub trait AttributeMethods { + fn meta(&self) -> @MetaItem; + fn desugar_doc(&self) -> Attribute; } -pub fn get_meta_item_name(meta: @ast::meta_item) -> @str { - match meta.node { - ast::meta_word(n) => n, - ast::meta_name_value(n, _) => n, - ast::meta_list(n, _) => n, +impl AttributeMethods for Attribute { + /// Extract the MetaItem from inside this Attribute. + pub fn meta(&self) -> @MetaItem { + self.node.value } -} -/** - * Gets the string value if the meta_item is a meta_name_value variant - * containing a string, otherwise none - */ -pub fn get_meta_item_value_str(meta: @ast::meta_item) -> Option<@str> { - match meta.node { - ast::meta_name_value(_, v) => { - match v.node { - ast::lit_str(s) => Some(s), - _ => None, - } - }, - _ => None + /// Convert self to a normal #[doc="foo"] comment, if it is a + /// comment like `///` or `/** */`. (Returns self unchanged for + /// non-sugared doc attributes.) + pub fn desugar_doc(&self) -> Attribute { + if self.node.is_sugared_doc { + let comment = self.value_str().get(); + let meta = mk_name_value_item_str(@"doc", + strip_doc_comment_decoration(comment).to_managed()); + mk_attr(meta) + } else { + *self + } } } -/// Gets a list of inner meta items from a list meta_item type -pub fn get_meta_item_list(meta: @ast::meta_item) - -> Option<~[@ast::meta_item]> { - match meta.node { - ast::meta_list(_, ref l) => Some(/* FIXME (#2543) */ (*l).clone()), - _ => None - } -} +/* Constructors */ -/** - * If the meta item is a nam-value type with a string value then returns - * a tuple containing the name and string value, otherwise `none` - */ -pub fn get_name_value_str_pair(item: @ast::meta_item) - -> Option<(@str, @str)> { - match attr::get_meta_item_value_str(item) { - Some(value) => { - let name = attr::get_meta_item_name(item); - Some((name, value)) - } - None => None - } +pub fn mk_name_value_item_str(name: @str, value: @str) -> @MetaItem { + let value_lit = dummy_spanned(ast::lit_str(value)); + mk_name_value_item(name, value_lit) } - -/* Searching */ - -/// Search a list of attributes and return only those with a specific name -pub fn find_attrs_by_name(attrs: &[ast::attribute], name: &str) -> - ~[ast::attribute] { - do attrs.iter().filter_map |a| { - if name == get_attr_name(a) { - Some(*a) - } else { - None - } - }.collect() +pub fn mk_name_value_item(name: @str, value: ast::lit) -> @MetaItem { + @dummy_spanned(MetaNameValue(name, value)) } -/// Search a list of meta items and return only those with a specific name -pub fn find_meta_items_by_name(metas: &[@ast::meta_item], name: &str) -> - ~[@ast::meta_item] { - let mut rs = ~[]; - for metas.iter().advance |mi| { - if name == get_meta_item_name(*mi) { - rs.push(*mi) - } - } - rs +pub fn mk_list_item(name: @str, items: ~[@MetaItem]) -> @MetaItem { + @dummy_spanned(MetaList(name, items)) } -/** - * Returns true if a list of meta items contains another meta item. The - * comparison is performed structurally. - */ -pub fn contains(haystack: &[@ast::meta_item], - needle: @ast::meta_item) -> bool { - for haystack.iter().advance |item| { - if eq(*item, needle) { return true; } - } - return false; +pub fn mk_word_item(name: @str) -> @MetaItem { + @dummy_spanned(MetaWord(name)) } -fn eq(a: @ast::meta_item, b: @ast::meta_item) -> bool { - match a.node { - ast::meta_word(ref na) => match b.node { - ast::meta_word(ref nb) => (*na) == (*nb), - _ => false - }, - ast::meta_name_value(ref na, va) => match b.node { - ast::meta_name_value(ref nb, vb) => { - (*na) == (*nb) && va.node == vb.node - } - _ => false - }, - ast::meta_list(ref na, ref misa) => match b.node { - ast::meta_list(ref nb, ref misb) => { - if na != nb { return false; } - for misa.iter().advance |mi| { - if !misb.iter().any(|x| x == mi) { return false; } - } - true - } - _ => false - } - } +pub fn mk_attr(item: @MetaItem) -> Attribute { + dummy_spanned(Attribute_ { + style: ast::AttrInner, + value: item, + is_sugared_doc: false, + }) } -pub fn contains_name(metas: &[@ast::meta_item], name: &str) -> bool { - let matches = find_meta_items_by_name(metas, name); - matches.len() > 0u +pub fn mk_sugared_doc_attr(text: @str, lo: BytePos, hi: BytePos) -> Attribute { + let style = doc_comment_style(text); + let lit = spanned(lo, hi, ast::lit_str(text)); + let attr = Attribute_ { + style: style, + value: @spanned(lo, hi, MetaNameValue(@"doc", lit)), + is_sugared_doc: true + }; + spanned(lo, hi, attr) } -pub fn attrs_contains_name(attrs: &[ast::attribute], name: &str) -> bool { - !find_attrs_by_name(attrs, name).is_empty() +/* Searching */ +/// Check if `needle` occurs in `haystack` by a structural +/// comparison. This is slightly subtle, and relies on ignoring the +/// span included in the `==` comparison a plain MetaItem. +pub fn contains(haystack: &[@ast::MetaItem], + needle: @ast::MetaItem) -> bool { + debug!("attr::contains (name=%s)", needle.name()); + do haystack.iter().any |item| { + debug!(" testing: %s", item.name()); + item.node == needle.node + } } -pub fn first_attr_value_str_by_name(attrs: &[ast::attribute], name: &str) - -> Option<@str> { - - let mattrs = find_attrs_by_name(attrs, name); - if mattrs.len() > 0 { - get_meta_item_value_str(attr_meta(mattrs[0])) - } else { - None +pub fn contains_name<AM: AttrMetaMethods>(metas: &[AM], name: &str) -> bool { + debug!("attr::contains_name (name=%s)", name); + do metas.iter().any |item| { + debug!(" testing: %s", item.name()); + name == item.name() } } -fn last_meta_item_by_name(items: &[@ast::meta_item], name: &str) - -> Option<@ast::meta_item> { - - let items = attr::find_meta_items_by_name(items, name); - items.last_opt().map(|item| **item) +pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) + -> Option<@str> { + attrs.iter() + .find_(|at| name == at.name()) + .chain(|at| at.value_str()) } -pub fn last_meta_item_value_str_by_name(items: &[@ast::meta_item], name: &str) +pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str) -> Option<@str> { - - match last_meta_item_by_name(items, name) { - Some(item) => { - match attr::get_meta_item_value_str(item) { - Some(value) => Some(value), - None => None - } - }, - None => None - } + items.rev_iter().find_(|mi| name == mi.name()).chain(|i| i.value_str()) } -pub fn last_meta_item_list_by_name(items: ~[@ast::meta_item], name: &str) - -> Option<~[@ast::meta_item]> { - - match last_meta_item_by_name(items, name) { - Some(item) => attr::get_meta_item_list(item), - None => None - } -} - - /* Higher-level applications */ -pub fn sort_meta_items(items: &[@ast::meta_item]) -> ~[@ast::meta_item] { - // This is sort of stupid here, converting to a vec of mutables and back - let mut v = items.to_owned(); - do extra::sort::quick_sort(v) |ma, mb| { - get_meta_item_name(*ma) <= get_meta_item_name(*mb) +pub fn sort_meta_items(items: &[@MetaItem]) -> ~[@MetaItem] { + // This is sort of stupid here, but we need to sort by + // human-readable strings. + let mut v = items.iter() + .transform(|&mi| (mi.name(), mi)) + .collect::<~[(@str, @MetaItem)]>(); + + do extra::sort::quick_sort(v) |&(a, _), &(b, _)| { + a <= b } // There doesn't seem to be a more optimal way to do this - do v.map |m| { + do v.consume_iter().transform |(_, m)| { match m.node { - ast::meta_list(n, ref mis) => { + MetaList(n, ref mis) => { @spanned { - node: ast::meta_list(n, sort_meta_items(*mis)), - .. /*bad*/ (**m).clone() + node: MetaList(n, sort_meta_items(*mis)), + .. /*bad*/ (*m).clone() } } - _ => *m + _ => m } - } -} - -pub fn remove_meta_items_by_name(items: ~[@ast::meta_item], name: &str) -> - ~[@ast::meta_item] { - items.consume_iter().filter(|item| name != get_meta_item_name(*item)).collect() + }.collect() } /** * From a list of crate attributes get only the meta_items that affect crate * linkage */ -pub fn find_linkage_metas(attrs: &[ast::attribute]) -> ~[@ast::meta_item] { - do find_attrs_by_name(attrs, "link").flat_map |attr| { - match attr.node.value.node { - ast::meta_list(_, ref items) => { - /* FIXME (#2543) */ (*items).clone() - } - _ => ~[] +pub fn find_linkage_metas(attrs: &[Attribute]) -> ~[@MetaItem] { + let mut result = ~[]; + for attrs.iter().filter(|at| "link" == at.name()).advance |attr| { + match attr.meta().node { + MetaList(_, ref items) => result.push_all(*items), + _ => () } } + result } #[deriving(Eq)] -pub enum inline_attr { - ia_none, - ia_hint, - ia_always, - ia_never, +pub enum InlineAttr { + InlineNone, + InlineHint, + InlineAlways, + InlineNever, } /// True if something like #[inline] is found in the list of attrs. -pub fn find_inline_attr(attrs: &[ast::attribute]) -> inline_attr { +pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr { // FIXME (#2809)---validate the usage of #[inline] and #[inline] - do attrs.iter().fold(ia_none) |ia,attr| { + do attrs.iter().fold(InlineNone) |ia,attr| { match attr.node.value.node { - ast::meta_word(s) if "inline" == s => ia_hint, - ast::meta_list(s, ref items) if "inline" == s => { - if !find_meta_items_by_name(*items, "always").is_empty() { - ia_always - } else if !find_meta_items_by_name(*items, "never").is_empty() { - ia_never + MetaWord(n) if "inline" == n => InlineHint, + MetaList(n, ref items) if "inline" == n => { + if contains_name(*items, "always") { + InlineAlways + } else if contains_name(*items, "never") { + InlineNever } else { - ia_hint + InlineHint } } _ => ia @@ -322,12 +265,59 @@ pub fn find_inline_attr(attrs: &[ast::attribute]) -> inline_attr { } } +/// Tests if any `cfg(...)` meta items in `metas` match `cfg`. e.g. +/// +/// test_cfg(`[foo="a", bar]`, `[cfg(foo), cfg(bar)]`) == true +/// test_cfg(`[foo="a", bar]`, `[cfg(not(bar))]`) == false +/// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="a")]`) == true +/// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="b")]`) == false +pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>> + (cfg: &[@MetaItem], mut metas: It) -> bool { + // having no #[cfg(...)] attributes counts as matching. + let mut no_cfgs = true; + + // this would be much nicer as a chain of iterator adaptors, but + // this doesn't work. + let some_cfg_matches = do metas.any |mi| { + debug!("testing name: %s", mi.name()); + if "cfg" == mi.name() { // it is a #[cfg()] attribute + debug!("is cfg"); + no_cfgs = false; + // only #[cfg(...)] ones are understood. + match mi.meta_item_list() { + Some(cfg_meta) => { + debug!("is cfg(...)"); + do cfg_meta.iter().all |cfg_mi| { + debug!("cfg(%s[...])", cfg_mi.name()); + match cfg_mi.node { + ast::MetaList(s, ref not_cfgs) if "not" == s => { + debug!("not!"); + // inside #[cfg(not(...))], so these need to all + // not match. + not_cfgs.iter().all(|mi| { + debug!("cfg(not(%s[...]))", mi.name()); + !contains(cfg, *mi) + }) + } + _ => contains(cfg, *cfg_mi) + } + } + } + None => false + } + } else { + false + } + }; + debug!("test_cfg (no_cfgs=%?, some_cfg_matches=%?)", no_cfgs, some_cfg_matches); + no_cfgs || some_cfg_matches +} pub fn require_unique_names(diagnostic: @span_handler, - metas: &[@ast::meta_item]) { + metas: &[@MetaItem]) { let mut set = HashSet::new(); for metas.iter().advance |meta| { - let name = get_meta_item_name(*meta); + let name = meta.name(); // FIXME: How do I silence the warnings? --pcw (#2619) if !set.insert(name) { diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 64d2644b383..4ada7f7479b 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -17,7 +17,7 @@ use ext::base::*; pub fn expand_auto_encode( cx: @ExtCtxt, span: span, - _mitem: @ast::meta_item, + _mitem: @ast::MetaItem, in_items: ~[@ast::item] ) -> ~[@ast::item] { cx.span_err(span, "`#[auto_encode]` is deprecated, use `#[deriving(Encodable)]` instead"); @@ -27,7 +27,7 @@ pub fn expand_auto_encode( pub fn expand_auto_decode( cx: @ExtCtxt, span: span, - _mitem: @ast::meta_item, + _mitem: @ast::MetaItem, in_items: ~[@ast::item] ) -> ~[@ast::item] { cx.span_err(span, "`#[auto_decode]` is deprecated, use `#[deriving(Decodable)]` instead"); diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 2f86a0460d1..753d32fee5a 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -36,7 +36,7 @@ pub struct MacroDef { pub type ItemDecorator = @fn(@ExtCtxt, span, - @ast::meta_item, + @ast::MetaItem, ~[@ast::item]) -> ~[@ast::item]; diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 59754f5519e..df5f3d8d895 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -163,7 +163,7 @@ pub trait AstBuilder { // items fn item(&self, span: span, - name: ident, attrs: ~[ast::attribute], node: ast::item_) -> @ast::item; + name: ident, attrs: ~[ast::Attribute], node: ast::item_) -> @ast::item; fn arg(&self, span: span, name: ident, ty: ast::Ty) -> ast::arg; // XXX unused self @@ -199,7 +199,7 @@ pub trait AstBuilder { fn item_struct(&self, span: span, name: ident, struct_def: ast::struct_def) -> @ast::item; fn item_mod(&self, span: span, - name: ident, attrs: ~[ast::attribute], + name: ident, attrs: ~[ast::Attribute], vi: ~[ast::view_item], items: ~[@ast::item]) -> @ast::item; fn item_ty_poly(&self, @@ -209,11 +209,11 @@ pub trait AstBuilder { generics: Generics) -> @ast::item; fn item_ty(&self, span: span, name: ident, ty: ast::Ty) -> @ast::item; - fn attribute(&self, sp: span, mi: @ast::meta_item) -> ast::attribute; + fn attribute(&self, sp: span, mi: @ast::MetaItem) -> ast::Attribute; - fn meta_word(&self, sp: span, w: @str) -> @ast::meta_item; - fn meta_list(&self, sp: span, name: @str, mis: ~[@ast::meta_item]) -> @ast::meta_item; - fn meta_name_value(&self, sp: span, name: @str, value: ast::lit_) -> @ast::meta_item; + fn meta_word(&self, sp: span, w: @str) -> @ast::MetaItem; + fn meta_list(&self, sp: span, name: @str, mis: ~[@ast::MetaItem]) -> @ast::MetaItem; + fn meta_name_value(&self, sp: span, name: @str, value: ast::lit_) -> @ast::MetaItem; fn view_use(&self, sp: span, vis: ast::visibility, vp: ~[@ast::view_path]) -> ast::view_item; @@ -657,7 +657,7 @@ impl AstBuilder for @ExtCtxt { } fn item(&self, span: span, - name: ident, attrs: ~[ast::attribute], node: ast::item_) -> @ast::item { + name: ident, attrs: ~[ast::Attribute], node: ast::item_) -> @ast::item { // XXX: Would be nice if our generated code didn't violate // Rust coding conventions @ast::item { ident: name, @@ -754,7 +754,7 @@ impl AstBuilder for @ExtCtxt { } fn item_mod(&self, span: span, name: ident, - attrs: ~[ast::attribute], + attrs: ~[ast::Attribute], vi: ~[ast::view_item], items: ~[@ast::item]) -> @ast::item { self.item( @@ -777,23 +777,22 @@ impl AstBuilder for @ExtCtxt { self.item_ty_poly(span, name, ty, ast_util::empty_generics()) } - fn attribute(&self, sp: span, mi: @ast::meta_item) -> ast::attribute { - respan(sp, - ast::attribute_ { - style: ast::attr_outer, - value: mi, - is_sugared_doc: false - }) + fn attribute(&self, sp: span, mi: @ast::MetaItem) -> ast::Attribute { + respan(sp, ast::Attribute_ { + style: ast::AttrOuter, + value: mi, + is_sugared_doc: false, + }) } - fn meta_word(&self, sp: span, w: @str) -> @ast::meta_item { - @respan(sp, ast::meta_word(w)) + fn meta_word(&self, sp: span, w: @str) -> @ast::MetaItem { + @respan(sp, ast::MetaWord(w)) } - fn meta_list(&self, sp: span, name: @str, mis: ~[@ast::meta_item]) -> @ast::meta_item { - @respan(sp, ast::meta_list(name, mis)) + fn meta_list(&self, sp: span, name: @str, mis: ~[@ast::MetaItem]) -> @ast::MetaItem { + @respan(sp, ast::MetaList(name, mis)) } - fn meta_name_value(&self, sp: span, name: @str, value: ast::lit_) -> @ast::meta_item { - @respan(sp, ast::meta_name_value(name, respan(sp, value))) + fn meta_name_value(&self, sp: span, name: @str, value: ast::lit_) -> @ast::MetaItem { + @respan(sp, ast::MetaNameValue(name, respan(sp, value))) } fn view_use(&self, sp: span, diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index edaf2b8cae6..02dcb2cdbc9 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -16,7 +16,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_clone(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { @@ -40,9 +40,9 @@ pub fn expand_deriving_clone(cx: @ExtCtxt, } pub fn expand_deriving_deep_clone(cx: @ExtCtxt, - span: span, - mitem: @meta_item, - in_items: ~[@item]) + span: span, + mitem: @MetaItem, + in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { path: Path::new(~["std", "clone", "DeepClone"]), diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs index cea88bb7bbb..a7d9db59130 100644 --- a/src/libsyntax/ext/deriving/cmp/eq.rs +++ b/src/libsyntax/ext/deriving/cmp/eq.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -16,7 +16,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_eq(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { // structures are equal if all fields are equal, and non equal, if // any fields are not equal or if the enum variants are different diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index 3b9691cd42c..d532eedd291 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -9,7 +9,7 @@ // except according to those terms. use ast; -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -17,7 +17,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_ord(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { macro_rules! md ( ($name:expr, $op:expr, $equal:expr) => { diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs index 70ac4d3d4c1..8285de1d561 100644 --- a/src/libsyntax/ext/deriving/cmp/totaleq.rs +++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs @@ -8,17 +8,16 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; pub fn expand_deriving_totaleq(cx: @ExtCtxt, - span: span, - mitem: @meta_item, - in_items: ~[@item]) -> ~[@item] { - + span: span, + mitem: @MetaItem, + in_items: ~[@item]) -> ~[@item] { fn cs_equals(cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr { cs_and(|cx, span, _, _| cx.expr_bool(span, false), cx, span, substr) diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs index 84d7320fe1c..01dacdfe453 100644 --- a/src/libsyntax/ext/deriving/cmp/totalord.rs +++ b/src/libsyntax/ext/deriving/cmp/totalord.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -17,7 +17,7 @@ use std::cmp::{Ordering, Equal, Less, Greater}; pub fn expand_deriving_totalord(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { path: Path::new(~["std", "cmp", "TotalOrd"]), diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 405f9e3438b..bde0a345b10 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -16,7 +16,7 @@ encodable.rs for more. use std::vec; use std::uint; -use ast::{meta_item, item, expr, m_mutbl}; +use ast::{MetaItem, item, expr, m_mutbl}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -24,7 +24,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_decodable(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { path: Path::new_(~["extra", "serialize", "Decodable"], None, diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 5514fd0b6ab..1f969b4e078 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -75,7 +75,7 @@ would yield functions like: } */ -use ast::{meta_item, item, expr, m_imm, m_mutbl}; +use ast::{MetaItem, item, expr, m_imm, m_mutbl}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -83,7 +83,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_encodable(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { path: Path::new_(~["extra", "serialize", "Encodable"], None, diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs index a50f4d70f0e..04bbe5ae1d6 100644 --- a/src/libsyntax/ext/deriving/generic.rs +++ b/src/libsyntax/ext/deriving/generic.rs @@ -281,7 +281,7 @@ pub type EnumNonMatchFunc<'self> = impl<'self> TraitDef<'self> { pub fn expand(&self, cx: @ExtCtxt, span: span, - _mitem: @ast::meta_item, + _mitem: @ast::MetaItem, in_items: ~[@ast::item]) -> ~[@ast::item] { let mut result = ~[]; for in_items.iter().advance |item| { @@ -361,7 +361,8 @@ impl<'self> TraitDef<'self> { let doc_attr = cx.attribute( span, cx.meta_name_value(span, - @"doc", ast::lit_str(@"Automatically derived."))); + @"doc", + ast::lit_str(@"Automatically derived."))); cx.item( span, ::parse::token::special_idents::clownshoes_extensions, diff --git a/src/libsyntax/ext/deriving/iter_bytes.rs b/src/libsyntax/ext/deriving/iter_bytes.rs index be13e103a72..57a4f0899b5 100644 --- a/src/libsyntax/ext/deriving/iter_bytes.rs +++ b/src/libsyntax/ext/deriving/iter_bytes.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{meta_item, item, expr, and}; +use ast::{MetaItem, item, expr, and}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -17,7 +17,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_iter_bytes(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { path: Path::new(~["std", "to_bytes", "IterBytes"]), diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 537d9efbb26..cde7dcc5dbe 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -18,7 +18,8 @@ library. */ -use ast::{enum_def, ident, item, Generics, meta_item, struct_def}; +use ast::{enum_def, ident, item, Generics, struct_def}; +use ast::{MetaItem, MetaList, MetaNameValue, MetaWord}; use ext::base::ExtCtxt; use ext::build::AstBuilder; use codemap::span; @@ -58,26 +59,24 @@ pub type ExpandDerivingEnumDefFn<'self> = &'self fn(@ExtCtxt, pub fn expand_meta_deriving(cx: @ExtCtxt, _span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { - use ast::{meta_list, meta_name_value, meta_word}; - match mitem.node { - meta_name_value(_, ref l) => { + MetaNameValue(_, ref l) => { cx.span_err(l.span, "unexpected value in `deriving`"); in_items } - meta_word(_) | meta_list(_, []) => { + MetaWord(_) | MetaList(_, []) => { cx.span_warn(mitem.span, "empty trait list in `deriving`"); in_items } - meta_list(_, ref titems) => { + MetaList(_, ref titems) => { do titems.rev_iter().fold(in_items) |in_items, &titem| { match titem.node { - meta_name_value(tname, _) | - meta_list(tname, _) | - meta_word(tname) => { + MetaNameValue(tname, _) | + MetaList(tname, _) | + MetaWord(tname) => { macro_rules! expand(($func:path) => ($func(cx, titem.span, titem, in_items))); match tname.as_slice() { diff --git a/src/libsyntax/ext/deriving/rand.rs b/src/libsyntax/ext/deriving/rand.rs index 823f21401ca..2966a8c114d 100644 --- a/src/libsyntax/ext/deriving/rand.rs +++ b/src/libsyntax/ext/deriving/rand.rs @@ -9,7 +9,7 @@ // except according to those terms. use ast; -use ast::{meta_item, item, expr, ident}; +use ast::{MetaItem, item, expr, ident}; use codemap::span; use ext::base::ExtCtxt; use ext::build::{AstBuilder, Duplicate}; @@ -19,7 +19,7 @@ use std::vec; pub fn expand_deriving_rand(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { diff --git a/src/libsyntax/ext/deriving/to_str.rs b/src/libsyntax/ext/deriving/to_str.rs index c9d63d2c416..9b544eb0796 100644 --- a/src/libsyntax/ext/deriving/to_str.rs +++ b/src/libsyntax/ext/deriving/to_str.rs @@ -9,7 +9,7 @@ // except according to those terms. use ast; -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -17,7 +17,7 @@ use ext::deriving::generic::*; pub fn expand_deriving_to_str(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { diff --git a/src/libsyntax/ext/deriving/zero.rs b/src/libsyntax/ext/deriving/zero.rs index 5bee804d582..3c9e842473c 100644 --- a/src/libsyntax/ext/deriving/zero.rs +++ b/src/libsyntax/ext/deriving/zero.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{meta_item, item, expr}; +use ast::{MetaItem, item, expr}; use codemap::span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -18,7 +18,7 @@ use std::vec; pub fn expand_deriving_zero(cx: @ExtCtxt, span: span, - mitem: @meta_item, + mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { let trait_def = TraitDef { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 2f1d320fef7..e78254f11f5 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -14,6 +14,7 @@ use ast::{illegal_ctxt}; use ast; use ast_util::{new_rename, new_mark, resolve}; use attr; +use attr::AttrMetaMethods; use codemap; use codemap::{span, ExpnInfo, NameAndSpan}; use ext::base::*; @@ -126,7 +127,7 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv, // the item into a new set of items. let new_items = do vec::flat_map(module_.items) |item| { do item.attrs.rev_iter().fold(~[*item]) |items, attr| { - let mname = attr::get_attr_name(attr); + let mname = attr.name(); match (*extsbox).find(&intern(mname)) { Some(@SE(ItemDecorator(dec_fn))) => { @@ -196,8 +197,8 @@ pub fn expand_item(extsbox: @mut SyntaxEnv, } // does this attribute list contain "macro_escape" ? -pub fn contains_macro_escape (attrs: &[ast::attribute]) -> bool { - attrs.iter().any(|attr| "macro_escape" == attr::get_attr_name(attr)) +pub fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool { + attr::contains_name(attrs, "macro_escape") } // Support for item-position macro invocations, exactly the same @@ -793,7 +794,7 @@ pub fn new_ident_resolver() -> mod test { use super::*; use ast; - use ast::{attribute_, attr_outer, meta_word, empty_ctxt}; + use ast::{Attribute_, AttrOuter, MetaWord, empty_ctxt}; use codemap; use codemap::spanned; use parse; @@ -883,14 +884,14 @@ mod test { assert_eq!(contains_macro_escape (attrs2),false); } - // make a "meta_word" outer attribute with the given name - fn make_dummy_attr(s: @str) -> ast::attribute { + // make a MetaWord outer attribute with the given name + fn make_dummy_attr(s: @str) -> ast::Attribute { spanned { span:codemap::dummy_sp(), - node: attribute_ { - style: attr_outer, + node: Attribute_ { + style: AttrOuter, value: @spanned { - node: meta_word(s), + node: MetaWord(s), span: codemap::dummy_sp(), }, is_sugared_doc: false, diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index cfd858eed47..63cb3cf38f6 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -74,35 +74,34 @@ pub type ast_fold_fns = @AstFoldFns; /* some little folds that probably aren't useful to have in ast_fold itself*/ //used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive -fn fold_meta_item_(mi: @meta_item, fld: @ast_fold) -> @meta_item { +fn fold_meta_item_(mi: @MetaItem, fld: @ast_fold) -> @MetaItem { @spanned { node: match mi.node { - meta_word(id) => meta_word(id), - meta_list(id, ref mis) => { + MetaWord(id) => MetaWord(id), + MetaList(id, ref mis) => { let fold_meta_item = |x| fold_meta_item_(x, fld); - meta_list( + MetaList( id, mis.map(|e| fold_meta_item(*e)) ) } - meta_name_value(id, s) => { - meta_name_value(id, s) - } + MetaNameValue(id, s) => MetaNameValue(id, s) }, span: fld.new_span(mi.span) } } //used in noop_fold_item and noop_fold_crate -fn fold_attribute_(at: attribute, fld: @ast_fold) -> attribute { +fn fold_attribute_(at: Attribute, fld: @ast_fold) -> Attribute { spanned { - node: ast::attribute_ { + span: fld.new_span(at.span), + node: ast::Attribute_ { style: at.node.style, value: fold_meta_item_(at.node.value, fld), - is_sugared_doc: at.node.is_sugared_doc, - }, - span: fld.new_span(at.span), + is_sugared_doc: at.node.is_sugared_doc + } } } + //used in noop_fold_foreign_item and noop_fold_fn_decl fn fold_arg_(a: arg, fld: @ast_fold) -> arg { ast::arg { @@ -936,11 +935,11 @@ impl ast_fold for AstFoldFns { } pub trait AstFoldExtensions { - fn fold_attributes(&self, attrs: ~[attribute]) -> ~[attribute]; + fn fold_attributes(&self, attrs: ~[Attribute]) -> ~[Attribute]; } impl AstFoldExtensions for @ast_fold { - fn fold_attributes(&self, attrs: ~[attribute]) -> ~[attribute] { + fn fold_attributes(&self, attrs: ~[Attribute]) -> ~[Attribute] { attrs.map(|x| fold_attribute_(*x, *self)) } } diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 85c7d5de064..8cce5f15e67 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -17,32 +17,32 @@ use parse::parser::Parser; // a parser that can parse attributes. pub trait parser_attr { - fn parse_outer_attributes(&self) -> ~[ast::attribute]; - fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute; + fn parse_outer_attributes(&self) -> ~[ast::Attribute]; + fn parse_attribute(&self, style: ast::AttrStyle) -> ast::Attribute; fn parse_attribute_naked( &self, - style: ast::attr_style, + style: ast::AttrStyle, lo: BytePos - ) -> ast::attribute; + ) -> ast::Attribute; fn parse_inner_attrs_and_next(&self) -> - (~[ast::attribute], ~[ast::attribute]); - fn parse_meta_item(&self) -> @ast::meta_item; - fn parse_meta_seq(&self) -> ~[@ast::meta_item]; - fn parse_optional_meta(&self) -> ~[@ast::meta_item]; + (~[ast::Attribute], ~[ast::Attribute]); + fn parse_meta_item(&self) -> @ast::MetaItem; + fn parse_meta_seq(&self) -> ~[@ast::MetaItem]; + fn parse_optional_meta(&self) -> ~[@ast::MetaItem]; } impl parser_attr for Parser { // Parse attributes that appear before an item - fn parse_outer_attributes(&self) -> ~[ast::attribute] { - let mut attrs: ~[ast::attribute] = ~[]; + fn parse_outer_attributes(&self) -> ~[ast::Attribute] { + let mut attrs: ~[ast::Attribute] = ~[]; loop { match *self.token { token::POUND => { if self.look_ahead(1, |t| *t != token::LBRACKET) { break; } - attrs.push(self.parse_attribute(ast::attr_outer)); + attrs.push(self.parse_attribute(ast::AttrOuter)); } token::DOC_COMMENT(s) => { let attr = ::attr::mk_sugared_doc_attr( @@ -50,7 +50,7 @@ impl parser_attr for Parser { self.span.lo, self.span.hi ); - if attr.node.style != ast::attr_outer { + if attr.node.style != ast::AttrOuter { self.fatal("expected outer comment"); } attrs.push(attr); @@ -63,20 +63,20 @@ impl parser_attr for Parser { } // matches attribute = # attribute_naked - fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute { + fn parse_attribute(&self, style: ast::AttrStyle) -> ast::Attribute { let lo = self.span.lo; self.expect(&token::POUND); return self.parse_attribute_naked(style, lo); } // matches attribute_naked = [ meta_item ] - fn parse_attribute_naked(&self, style: ast::attr_style, lo: BytePos) -> - ast::attribute { + fn parse_attribute_naked(&self, style: ast::AttrStyle, lo: BytePos) -> + ast::Attribute { self.expect(&token::LBRACKET); let meta_item = self.parse_meta_item(); self.expect(&token::RBRACKET); let hi = self.span.hi; - return spanned(lo, hi, ast::attribute_ { style: style, + return spanned(lo, hi, ast::Attribute_ { style: style, value: meta_item, is_sugared_doc: false }); } // Parse attributes that appear after the opening of an item, each @@ -90,9 +90,9 @@ impl parser_attr for Parser { // you can make the 'next' field an Option, but the result is going to be // more useful as a vector. fn parse_inner_attrs_and_next(&self) -> - (~[ast::attribute], ~[ast::attribute]) { - let mut inner_attrs: ~[ast::attribute] = ~[]; - let mut next_outer_attrs: ~[ast::attribute] = ~[]; + (~[ast::Attribute], ~[ast::Attribute]) { + let mut inner_attrs: ~[ast::Attribute] = ~[]; + let mut next_outer_attrs: ~[ast::Attribute] = ~[]; loop { match *self.token { token::POUND => { @@ -100,7 +100,7 @@ impl parser_attr for Parser { // This is an extension break; } - let attr = self.parse_attribute(ast::attr_inner); + let attr = self.parse_attribute(ast::AttrInner); if *self.token == token::SEMI { self.bump(); inner_attrs.push(attr); @@ -108,7 +108,7 @@ impl parser_attr for Parser { // It's not really an inner attribute let outer_attr = spanned(attr.span.lo, attr.span.hi, - ast::attribute_ { style: ast::attr_outer, + ast::Attribute_ { style: ast::AttrOuter, value: attr.node.value, is_sugared_doc: false }); next_outer_attrs.push(outer_attr); @@ -122,7 +122,7 @@ impl parser_attr for Parser { self.span.hi ); self.bump(); - if attr.node.style == ast::attr_inner { + if attr.node.style == ast::AttrInner { inner_attrs.push(attr); } else { next_outer_attrs.push(attr); @@ -138,7 +138,7 @@ impl parser_attr for Parser { // matches meta_item = IDENT // | IDENT = lit // | IDENT meta_seq - fn parse_meta_item(&self) -> @ast::meta_item { + fn parse_meta_item(&self) -> @ast::MetaItem { let lo = self.span.lo; let name = self.id_to_str(self.parse_ident()); match *self.token { @@ -146,29 +146,29 @@ impl parser_attr for Parser { self.bump(); let lit = self.parse_lit(); let hi = self.span.hi; - @spanned(lo, hi, ast::meta_name_value(name, lit)) + @spanned(lo, hi, ast::MetaNameValue(name, lit)) } token::LPAREN => { let inner_items = self.parse_meta_seq(); let hi = self.span.hi; - @spanned(lo, hi, ast::meta_list(name, inner_items)) + @spanned(lo, hi, ast::MetaList(name, inner_items)) } _ => { let hi = self.last_span.hi; - @spanned(lo, hi, ast::meta_word(name)) + @spanned(lo, hi, ast::MetaWord(name)) } } } // matches meta_seq = ( COMMASEP(meta_item) ) - fn parse_meta_seq(&self) -> ~[@ast::meta_item] { + fn parse_meta_seq(&self) -> ~[@ast::MetaItem] { self.parse_seq(&token::LPAREN, &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_meta_item()).node } - fn parse_optional_meta(&self) -> ~[@ast::meta_item] { + fn parse_optional_meta(&self) -> ~[@ast::MetaItem] { match *self.token { token::LPAREN => self.parse_meta_seq(), _ => ~[] diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 83af5bade3a..6daeb1b3e1e 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -44,12 +44,12 @@ pub fn is_doc_comment(s: &str) -> bool { s.starts_with("/*!") } -pub fn doc_comment_style(comment: &str) -> ast::attr_style { +pub fn doc_comment_style(comment: &str) -> ast::AttrStyle { assert!(is_doc_comment(comment)); if comment.starts_with("//!") || comment.starts_with("/*!") { - ast::attr_inner + ast::AttrInner } else { - ast::attr_outer + ast::AttrOuter } } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 410849b4482..c7a65c80de1 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -115,7 +115,7 @@ pub fn parse_item_from_source_str( name: @str, source: @str, cfg: ast::crate_cfg, - attrs: ~[ast::attribute], + attrs: ~[ast::Attribute], sess: @mut ParseSess ) -> Option<@ast::item> { let p = new_parser_from_source_str( @@ -132,7 +132,7 @@ pub fn parse_meta_from_source_str( source: @str, cfg: ast::crate_cfg, sess: @mut ParseSess -) -> @ast::meta_item { +) -> @ast::MetaItem { let p = new_parser_from_source_str( sess, cfg, @@ -146,7 +146,7 @@ pub fn parse_stmt_from_source_str( name: @str, source: @str, cfg: ast::crate_cfg, - attrs: ~[ast::attribute], + attrs: ~[ast::Attribute], sess: @mut ParseSess ) -> @ast::stmt { let p = new_parser_from_source_str( diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index d844344d9f2..7e0081bdb68 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -17,7 +17,7 @@ Obsolete syntax that becomes too hard to parse can be removed. */ -use ast::{expr, expr_lit, lit_nil, attribute}; +use ast::{expr, expr_lit, lit_nil, Attribute}; use ast; use codemap::{span, respan}; use parse::parser::Parser; @@ -89,7 +89,7 @@ pub trait ParserObsoleteMethods { fn eat_obsolete_ident(&self, ident: &str) -> bool; fn try_parse_obsolete_struct_ctor(&self) -> bool; fn try_parse_obsolete_with(&self) -> bool; - fn try_parse_obsolete_priv_section(&self, attrs: &[attribute]) -> bool; + fn try_parse_obsolete_priv_section(&self, attrs: &[Attribute]) -> bool; } impl ParserObsoleteMethods for Parser { @@ -328,7 +328,7 @@ impl ParserObsoleteMethods for Parser { } } - pub fn try_parse_obsolete_priv_section(&self, attrs: &[attribute]) + pub fn try_parse_obsolete_priv_section(&self, attrs: &[Attribute]) -> bool { if self.is_keyword(keywords::Priv) && self.look_ahead(1, |t| *t == token::LBRACE) { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7f309a01877..e6a66b9c61e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -15,7 +15,7 @@ use ast::{CallSugar, NoSugar, DoSugar, ForSugar}; use ast::{TyBareFn, TyClosure}; use ast::{RegionTyParamBound, TraitTyParamBound}; use ast::{provided, public, purity}; -use ast::{_mod, add, arg, arm, attribute, bind_by_ref, bind_infer}; +use ast::{_mod, add, arg, arm, Attribute, bind_by_ref, bind_infer}; use ast::{bitand, bitor, bitxor, blk}; use ast::{blk_check_mode, box}; use ast::{crate, crate_cfg, decl, decl_item}; @@ -109,12 +109,12 @@ enum restriction { } type arg_or_capture_item = Either<arg, ()>; -type item_info = (ident, item_, Option<~[attribute]>); +type item_info = (ident, item_, Option<~[Attribute]>); pub enum item_or_view_item { // Indicates a failure to parse any kind of item. The attributes are // returned. - iovi_none(~[attribute]), + iovi_none(~[Attribute]), iovi_item(@item), iovi_foreign_item(@foreign_item), iovi_view_item(view_item) @@ -242,8 +242,8 @@ macro_rules! maybe_whole ( ) -fn maybe_append(lhs: ~[attribute], rhs: Option<~[attribute]>) - -> ~[attribute] { +fn maybe_append(lhs: ~[Attribute], rhs: Option<~[Attribute]>) + -> ~[Attribute] { match rhs { None => lhs, Some(ref attrs) => vec::append(lhs, (*attrs)) @@ -252,7 +252,7 @@ fn maybe_append(lhs: ~[attribute], rhs: Option<~[attribute]>) struct ParsedItemsAndViewItems { - attrs_remaining: ~[attribute], + attrs_remaining: ~[Attribute], view_items: ~[view_item], items: ~[@item], foreign_items: ~[@foreign_item] @@ -2959,7 +2959,7 @@ impl Parser { // parse a structure field fn parse_name_and_ty(&self, pr: visibility, - attrs: ~[attribute]) -> @struct_field { + attrs: ~[Attribute]) -> @struct_field { let lo = self.span.lo; if !is_plain_ident(&*self.token) { self.fatal("expected ident"); @@ -2977,7 +2977,7 @@ impl Parser { // parse a statement. may include decl. // precondition: any attributes are parsed already - pub fn parse_stmt(&self, item_attrs: ~[attribute]) -> @stmt { + pub fn parse_stmt(&self, item_attrs: ~[Attribute]) -> @stmt { maybe_whole!(self, nt_stmt); fn check_expected_item(p: &Parser, found_attrs: bool) { @@ -3091,7 +3091,7 @@ impl Parser { // parse a block. Inner attrs are allowed. fn parse_inner_attrs_and_block(&self) - -> (~[attribute], blk) { + -> (~[Attribute], blk) { maybe_whole!(pair_empty self, nt_block); @@ -3115,7 +3115,7 @@ impl Parser { // parse the rest of a block expression or function body fn parse_block_tail_(&self, lo: BytePos, s: blk_check_mode, - first_item_attrs: ~[attribute]) -> blk { + first_item_attrs: ~[Attribute]) -> blk { let mut stmts = ~[]; let mut expr = None; @@ -3594,7 +3594,7 @@ impl Parser { fn mk_item(&self, lo: BytePos, hi: BytePos, ident: ident, node: item_, vis: visibility, - attrs: ~[attribute]) -> @item { + attrs: ~[Attribute]) -> @item { @ast::item { ident: ident, attrs: attrs, id: self.get_id(), @@ -3825,7 +3825,7 @@ impl Parser { // parse a structure field declaration pub fn parse_single_struct_field(&self, vis: visibility, - attrs: ~[attribute]) + attrs: ~[Attribute]) -> @struct_field { if self.eat_obsolete_ident("let") { self.obsolete(*self.last_span, ObsoleteLet); @@ -3894,7 +3894,7 @@ impl Parser { // attributes (of length 0 or 1), parse all of the items in a module fn parse_mod_items(&self, term: token::Token, - first_item_attrs: ~[attribute]) + first_item_attrs: ~[Attribute]) -> _mod { // parse all of the items up to closing or an attribute. // view items are legal here. @@ -3953,7 +3953,7 @@ impl Parser { } // parse a `mod <foo> { ... }` or `mod <foo>;` item - fn parse_item_mod(&self, outer_attrs: &[ast::attribute]) -> item_info { + fn parse_item_mod(&self, outer_attrs: &[Attribute]) -> item_info { let id_span = *self.span; let id = self.parse_ident(); if *self.token == token::SEMI { @@ -3972,11 +3972,10 @@ impl Parser { } } - fn push_mod_path(&self, id: ident, attrs: &[ast::attribute]) { + fn push_mod_path(&self, id: ident, attrs: &[Attribute]) { let default_path = token::interner_get(id.name); - let file_path = match ::attr::first_attr_value_str_by_name( - attrs, "path") { - + let file_path = match ::attr::first_attr_value_str_by_name(attrs, + "path") { Some(d) => d, None => default_path }; @@ -3990,14 +3989,13 @@ impl Parser { // read a module from a source file. fn eval_src_mod(&self, id: ast::ident, - outer_attrs: &[ast::attribute], + outer_attrs: &[ast::Attribute], id_sp: span) - -> (ast::item_, ~[ast::attribute]) { + -> (ast::item_, ~[ast::Attribute]) { let prefix = Path(self.sess.cm.span_to_filename(*self.span)); let prefix = prefix.dir_path(); let mod_path_stack = &*self.mod_path_stack; let mod_path = Path(".").push_many(*mod_path_stack); - let default_path = token::interner_get(id.name).to_owned() + ".rs"; let file_path = match ::attr::first_attr_value_str_by_name( outer_attrs, "path") { Some(d) => { @@ -4008,7 +4006,7 @@ impl Parser { path } } - None => mod_path.push(default_path) + None => mod_path.push(token::interner_get(id.name) + ".rs") // default }; self.eval_src_mod_from_path(prefix, @@ -4020,8 +4018,8 @@ impl Parser { fn eval_src_mod_from_path(&self, prefix: Path, path: Path, - outer_attrs: ~[ast::attribute], - id_sp: span) -> (ast::item_, ~[ast::attribute]) { + outer_attrs: ~[ast::Attribute], + id_sp: span) -> (ast::item_, ~[ast::Attribute]) { let full_path = if path.is_absolute { path @@ -4057,17 +4055,10 @@ impl Parser { let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs); self.sess.included_mod_stack.pop(); return (ast::item_mod(m0), mod_attrs); - - fn cdir_path_opt(default: @str, attrs: ~[ast::attribute]) -> @str { - match ::attr::first_attr_value_str_by_name(attrs, "path") { - Some(d) => d, - None => default - } - } } // parse a function declaration from a foreign module - fn parse_item_foreign_fn(&self, attrs: ~[attribute]) -> @foreign_item { + fn parse_item_foreign_fn(&self, attrs: ~[Attribute]) -> @foreign_item { let lo = self.span.lo; let vis = self.parse_visibility(); let purity = self.parse_fn_purity(); @@ -4085,7 +4076,7 @@ impl Parser { // parse a const definition from a foreign module fn parse_item_foreign_const(&self, vis: ast::visibility, - attrs: ~[attribute]) -> @foreign_item { + attrs: ~[Attribute]) -> @foreign_item { let lo = self.span.lo; // XXX: Obsolete; remove after snap. @@ -4130,7 +4121,7 @@ impl Parser { fn parse_foreign_mod_items(&self, sort: ast::foreign_mod_sort, abis: AbiSet, - first_item_attrs: ~[attribute]) + first_item_attrs: ~[Attribute]) -> foreign_mod { let ParsedItemsAndViewItems { attrs_remaining: attrs_remaining, @@ -4156,7 +4147,7 @@ impl Parser { lo: BytePos, opt_abis: Option<AbiSet>, visibility: visibility, - attrs: ~[attribute], + attrs: ~[Attribute], items_allowed: bool) -> item_or_view_item { let mut must_be_named_mod = false; @@ -4437,7 +4428,7 @@ impl Parser { // NB: this function no longer parses the items inside an // extern mod. fn parse_item_or_view_item(&self, - attrs: ~[attribute], + attrs: ~[Attribute], macros_allowed: bool) -> item_or_view_item { maybe_whole!(iovi self, nt_item); @@ -4569,7 +4560,7 @@ impl Parser { // parse a foreign item; on failure, return iovi_none. fn parse_foreign_item(&self, - attrs: ~[attribute], + attrs: ~[Attribute], macros_allowed: bool) -> item_or_view_item { maybe_whole!(iovi self, nt_item); @@ -4594,7 +4585,7 @@ impl Parser { // this is the fall-through for parsing items. fn parse_macro_use_or_failure( &self, - attrs: ~[attribute], + attrs: ~[Attribute], macros_allowed: bool, lo : BytePos, visibility : visibility @@ -4656,7 +4647,7 @@ impl Parser { return iovi_none(attrs); } - pub fn parse_item(&self, attrs: ~[attribute]) -> Option<@ast::item> { + pub fn parse_item(&self, attrs: ~[Attribute]) -> Option<@ast::item> { match self.parse_item_or_view_item(attrs, true) { iovi_none(_) => None, @@ -4793,7 +4784,7 @@ impl Parser { // parse a view item. fn parse_view_item( &self, - attrs: ~[attribute], + attrs: ~[Attribute], vis: visibility ) -> view_item { let lo = self.span.lo; @@ -4819,7 +4810,7 @@ impl Parser { // - mod_items uses extern_mod_allowed = true // - block_tail_ uses extern_mod_allowed = false fn parse_items_and_view_items(&self, - first_item_attrs: ~[attribute], + first_item_attrs: ~[Attribute], mut extern_mod_allowed: bool, macros_allowed: bool) -> ParsedItemsAndViewItems { @@ -4901,7 +4892,7 @@ impl Parser { // Parses a sequence of foreign items. Stops when it finds program // text that can't be parsed as an item - fn parse_foreign_items(&self, first_item_attrs: ~[attribute], + fn parse_foreign_items(&self, first_item_attrs: ~[Attribute], macros_allowed: bool) -> ParsedItemsAndViewItems { let mut attrs = vec::append(first_item_attrs, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index f88b7b9a1f8..a74b89ce5f5 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -14,7 +14,7 @@ use ast; use ast_util; use opt_vec::OptVec; use opt_vec; -use attr; +use attr::{AttrMetaMethods, AttributeMethods}; use codemap::{CodeMap, BytePos}; use codemap; use diagnostic; @@ -212,11 +212,11 @@ pub fn block_to_str(blk: &ast::blk, intr: @ident_interner) -> ~str { } } -pub fn meta_item_to_str(mi: &ast::meta_item, intr: @ident_interner) -> ~str { +pub fn meta_item_to_str(mi: &ast::MetaItem, intr: @ident_interner) -> ~str { to_str(mi, print_meta_item, intr) } -pub fn attribute_to_str(attr: &ast::attribute, intr: @ident_interner) -> ~str { +pub fn attribute_to_str(attr: &ast::Attribute, intr: @ident_interner) -> ~str { to_str(attr, print_attribute, intr) } @@ -352,7 +352,7 @@ pub fn commasep_exprs(s: @ps, b: breaks, exprs: &[@ast::expr]) { commasep_cmnt(s, b, exprs, |p, &e| print_expr(p, e), |e| e.span); } -pub fn print_mod(s: @ps, _mod: &ast::_mod, attrs: &[ast::attribute]) { +pub fn print_mod(s: @ps, _mod: &ast::_mod, attrs: &[ast::Attribute]) { print_inner_attributes(s, attrs); for _mod.view_items.iter().advance |vitem| { print_view_item(s, vitem); @@ -361,7 +361,7 @@ pub fn print_mod(s: @ps, _mod: &ast::_mod, attrs: &[ast::attribute]) { } pub fn print_foreign_mod(s: @ps, nmod: &ast::foreign_mod, - attrs: &[ast::attribute]) { + attrs: &[ast::Attribute]) { print_inner_attributes(s, attrs); for nmod.view_items.iter().advance |vitem| { print_view_item(s, vitem); @@ -843,22 +843,22 @@ pub fn print_method(s: @ps, meth: &ast::method) { print_block_with_attrs(s, &meth.body, meth.attrs); } -pub fn print_outer_attributes(s: @ps, attrs: &[ast::attribute]) { +pub fn print_outer_attributes(s: @ps, attrs: &[ast::Attribute]) { let mut count = 0; for attrs.iter().advance |attr| { match attr.node.style { - ast::attr_outer => { print_attribute(s, attr); count += 1; } + ast::AttrOuter => { print_attribute(s, attr); count += 1; } _ => {/* fallthrough */ } } } if count > 0 { hardbreak_if_not_bol(s); } } -pub fn print_inner_attributes(s: @ps, attrs: &[ast::attribute]) { +pub fn print_inner_attributes(s: @ps, attrs: &[ast::Attribute]) { let mut count = 0; for attrs.iter().advance |attr| { match attr.node.style { - ast::attr_inner => { + ast::AttrInner => { print_attribute(s, attr); if !attr.node.is_sugared_doc { word(s.s, ";"); @@ -871,16 +871,15 @@ pub fn print_inner_attributes(s: @ps, attrs: &[ast::attribute]) { if count > 0 { hardbreak_if_not_bol(s); } } -pub fn print_attribute(s: @ps, attr: &ast::attribute) { +pub fn print_attribute(s: @ps, attr: &ast::Attribute) { hardbreak_if_not_bol(s); maybe_print_comment(s, attr.span.lo); if attr.node.is_sugared_doc { - let meta = attr::attr_meta(*attr); - let comment = attr::get_meta_item_value_str(meta).get(); + let comment = attr.value_str().get(); word(s.s, comment); } else { word(s.s, "#["); - print_meta_item(s, attr.node.value); + print_meta_item(s, attr.meta()); word(s.s, "]"); } } @@ -927,7 +926,7 @@ pub fn print_block_unclosed_indent(s: @ps, blk: &ast::blk, indented: uint) { pub fn print_block_with_attrs(s: @ps, blk: &ast::blk, - attrs: &[ast::attribute]) { + attrs: &[ast::Attribute]) { print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs, true); } @@ -946,7 +945,7 @@ pub fn print_possibly_embedded_block_(s: @ps, blk: &ast::blk, embedded: embed_type, indented: uint, - attrs: &[ast::attribute], + attrs: &[ast::Attribute], close_box: bool) { match blk.rules { ast::unsafe_blk => word_space(s, "unsafe"), @@ -1793,16 +1792,16 @@ pub fn print_generics(s: @ps, generics: &ast::Generics) { } } -pub fn print_meta_item(s: @ps, item: &ast::meta_item) { +pub fn print_meta_item(s: @ps, item: &ast::MetaItem) { ibox(s, indent_unit); match item.node { - ast::meta_word(name) => word(s.s, name), - ast::meta_name_value(name, value) => { + ast::MetaWord(name) => word(s.s, name), + ast::MetaNameValue(name, value) => { word_space(s, name); word_space(s, "="); print_literal(s, @value); } - ast::meta_list(name, ref items) => { + ast::MetaList(name, ref items) => { word(s.s, name); popen(s); commasep(s, |
