提交 e97686d0 编写于 作者: J Jeffrey Seyfried

Move `MetaItemKind`'s `Name` to a field of `MetaItem`.

上级 4b9b0d34
......@@ -615,7 +615,8 @@ fn print_crate_info(sess: &Session,
let mut cfgs = Vec::new();
for &(name, ref value) in sess.parse_sess.config.iter() {
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
node: ast::MetaItemKind::Word(name),
name: name,
node: ast::MetaItemKind::Word,
span: DUMMY_SP,
});
if !allow_unstable_cfg && gated_cfg.is_some() {
......
......@@ -874,22 +874,20 @@ fn hash_meta_item(&mut self, meta_item: &ast::MetaItem) {
// ignoring span information, it doesn't matter here
self.hash_discriminant(&meta_item.node);
let name = &*meta_item.name.as_str();
match meta_item.node {
ast::MetaItemKind::Word(s) => {
let s = &*s.as_str();
s.len().hash(self.st);
s.hash(self.st);
ast::MetaItemKind::Word => {
name.len().hash(self.st);
name.hash(self.st);
}
ast::MetaItemKind::NameValue(s, ref lit) => {
let s = &*s.as_str();
s.len().hash(self.st);
s.hash(self.st);
ast::MetaItemKind::NameValue(ref lit) => {
name.len().hash(self.st);
name.hash(self.st);
lit.node.hash(self.st);
}
ast::MetaItemKind::List(s, ref items) => {
let s = &*s.as_str();
s.len().hash(self.st);
s.hash(self.st);
ast::MetaItemKind::List(ref items) => {
name.len().hash(self.st);
name.hash(self.st);
// Sort subitems so the hash does not depend on their order
let indices = self.indices_sorted_by(&items, |p| {
(p.name(), fnv::hash(&p.literal().map(|i| &i.node)))
......
......@@ -515,7 +515,12 @@ pub enum NestedMetaItemKind {
/// A spanned compile-time attribute item.
///
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
pub type MetaItem = Spanned<MetaItemKind>;
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct MetaItem {
pub name: Name,
pub node: MetaItemKind,
pub span: Span,
}
/// A compile-time attribute item.
///
......@@ -525,15 +530,15 @@ pub enum MetaItemKind {
/// Word meta item.
///
/// E.g. `test` as in `#[test]`
Word(Name),
Word,
/// List meta item.
///
/// E.g. `derive(..)` as in `#[derive(..)]`
List(Name, Vec<NestedMetaItem>),
List(Vec<NestedMetaItem>),
/// Name value meta item.
///
/// E.g. `feature = "foo"` as in `#[feature = "foo"]`
NameValue(Name, Lit),
NameValue(Lit)
}
/// A Block (`{ .. }`).
......
......@@ -18,7 +18,7 @@
use ast::{AttrId, Attribute, Name};
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use ast::{Lit, Expr, Item, Local, Stmt, StmtKind};
use codemap::{respan, spanned, dummy_spanned, mk_sp};
use codemap::{spanned, dummy_spanned, mk_sp};
use syntax_pos::{Span, BytePos, DUMMY_SP};
use errors::Handler;
use feature_gate::{Features, GatedCfg};
......@@ -219,16 +219,12 @@ pub fn is_value_str(&self) -> bool {
impl MetaItem {
pub fn name(&self) -> Name {
match self.node {
MetaItemKind::Word(n) => n,
MetaItemKind::NameValue(n, _) => n,
MetaItemKind::List(n, _) => n,
}
self.name
}
pub fn value_str(&self) -> Option<InternedString> {
match self.node {
MetaItemKind::NameValue(_, ref v) => {
MetaItemKind::NameValue(ref v) => {
match v.node {
ast::LitKind::Str(ref s, _) => Some((*s).clone()),
_ => None,
......@@ -240,14 +236,14 @@ pub fn value_str(&self) -> Option<InternedString> {
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
match self.node {
MetaItemKind::List(_, ref l) => Some(&l[..]),
MetaItemKind::List(ref l) => Some(&l[..]),
_ => None
}
}
pub fn is_word(&self) -> bool {
match self.node {
MetaItemKind::Word(_) => true,
MetaItemKind::Word => true,
_ => false,
}
}
......@@ -320,15 +316,15 @@ pub fn mk_word_item(name: Name) -> P<MetaItem> {
}
pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> P<MetaItem> {
P(respan(sp, MetaItemKind::NameValue(name, value)))
P(MetaItem { span: sp, name: name, node: MetaItemKind::NameValue(value) })
}
pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec<NestedMetaItem>) -> P<MetaItem> {
P(respan(sp, MetaItemKind::List(name, items)))
P(MetaItem { span: sp, name: name, node: MetaItemKind::List(items) })
}
pub fn mk_spanned_word_item(sp: Span, name: Name) -> P<MetaItem> {
P(respan(sp, MetaItemKind::Word(name)))
P(MetaItem { span: sp, name: name, node: MetaItemKind::Word })
}
......@@ -394,7 +390,11 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos, hi: By
Attribute {
id: id,
style: style,
value: P(spanned(lo, hi, MetaItemKind::NameValue(token::intern("doc"), lit))),
value: P(MetaItem {
span: mk_sp(lo, hi),
name: token::intern("doc"),
node: MetaItemKind::NameValue(lit),
}),
is_sugared_doc: true,
span: mk_sp(lo, hi),
}
......@@ -472,13 +472,14 @@ pub enum InlineAttr {
/// Determine what `#[inline]` attribute is present in `attrs`, if any.
pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr {
attrs.iter().fold(InlineAttr::None, |ia,attr| {
attrs.iter().fold(InlineAttr::None, |ia, attr| {
match attr.value.node {
MetaItemKind::Word(n) if n == "inline" => {
_ if attr.value.name != "inline" => ia,
MetaItemKind::Word => {
mark_used(attr);
InlineAttr::Hint
}
MetaItemKind::List(n, ref items) if n == "inline" => {
MetaItemKind::List(ref items) => {
mark_used(attr);
if items.len() != 1 {
diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); });
......@@ -511,7 +512,7 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
/// Tests if a cfg-pattern matches the cfg set
pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
match cfg.node {
ast::MetaItemKind::List(ref pred, ref mis) => {
ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() {
if !mi.is_meta_item() {
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral);
......@@ -521,7 +522,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
// The unwraps below may look dangerous, but we've already asserted
// that they won't fail with the loop above.
match &*pred.as_str() {
match &*cfg.name.as_str() {
"any" => mis.iter().any(|mi| {
cfg_matches(mi.meta_item().unwrap(), sess, features)
}),
......@@ -542,7 +543,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
}
}
},
ast::MetaItemKind::Word(_) | ast::MetaItemKind::NameValue(..) => {
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
gated_cfg.check_and_emit(sess, feats);
}
......@@ -880,7 +881,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
let mut acc = Vec::new();
match attr.value.node {
ast::MetaItemKind::List(s, ref items) if s == "repr" => {
ast::MetaItemKind::List(ref items) if attr.value.name == "repr" => {
mark_used(attr);
for item in items {
if !item.is_meta_item() {
......
......@@ -133,7 +133,7 @@ pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
}
let mis = match attr.value.node {
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
ast::MetaItemKind::List(ref mis) if is_cfg(&attr) => mis,
_ => return true
};
......
......@@ -991,9 +991,9 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool {
use ast::NestedMetaItemKind::*;
match item.node {
Word(..) => false,
NameValue(_, ref lit) => !lit.node.is_str(),
List(_, ref list) => list.iter().any(|li| {
Word => false,
NameValue(ref lit) => !lit.node.is_str(),
List(ref list) => list.iter().any(|li| {
match li.node {
MetaItem(ref mi) => contains_novel_literal(&**mi),
Literal(_) => true,
......
......@@ -520,13 +520,14 @@ pub fn noop_fold_meta_list_item<T: Folder>(li: NestedMetaItem, fld: &mut T)
}
pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
mi.map(|Spanned {node, span}| Spanned {
mi.map(|MetaItem { name, node, span }| MetaItem {
name: name,
node: match node {
MetaItemKind::Word(id) => MetaItemKind::Word(id),
MetaItemKind::List(id, mis) => {
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_list_item(e)))
}
MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s)
MetaItemKind::Word => MetaItemKind::Word,
MetaItemKind::List(mis) => {
MetaItemKind::List(mis.move_map(|e| fld.fold_meta_list_item(e)))
},
MetaItemKind::NameValue(s) => MetaItemKind::NameValue(s),
},
span: fld.new_span(span)
})
......
......@@ -227,23 +227,15 @@ pub fn parse_meta_item(&mut self) -> PResult<'a, P<ast::MetaItem>> {
let lo = self.span.lo;
let ident = self.parse_ident()?;
match self.token {
token::Eq => {
self.bump();
let lit = self.parse_unsuffixed_lit()?;
let hi = self.prev_span.hi;
Ok(P(spanned(lo, hi, ast::MetaItemKind::NameValue(ident.name, lit))))
}
token::OpenDelim(token::Paren) => {
let inner_items = self.parse_meta_seq()?;
let hi = self.prev_span.hi;
Ok(P(spanned(lo, hi, ast::MetaItemKind::List(ident.name, inner_items))))
}
_ => {
let hi = self.prev_span.hi;
Ok(P(spanned(lo, hi, ast::MetaItemKind::Word(ident.name))))
}
}
let node = if self.eat(&token::Eq) {
ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
} else if self.token == token::OpenDelim(token::Paren) {
ast::MetaItemKind::List(self.parse_meta_seq()?)
} else {
ast::MetaItemKind::Word
};
let hi = self.prev_span.hi;
Ok(P(ast::MetaItem { name: ident.name, node: node, span: mk_sp(lo, hi) }))
}
/// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;
......
......@@ -777,16 +777,16 @@ fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) -> io::Result<()>
fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
try!(self.ibox(INDENT_UNIT));
match item.node {
ast::MetaItemKind::Word(ref name) => {
try!(word(self.writer(), &name.as_str()));
ast::MetaItemKind::Word => {
try!(word(self.writer(), &item.name.as_str()));
}
ast::MetaItemKind::NameValue(ref name, ref value) => {
try!(self.word_space(&name.as_str()));
ast::MetaItemKind::NameValue(ref value) => {
try!(self.word_space(&item.name.as_str()));
try!(self.word_space("="));
try!(self.print_literal(value));
}
ast::MetaItemKind::List(ref name, ref items) => {
try!(word(self.writer(), &name.as_str()));
ast::MetaItemKind::List(ref items) => {
try!(word(self.writer(), &item.name.as_str()));
try!(self.popen());
try!(self.commasep(Consistent,
&items[..],
......
......@@ -70,7 +70,8 @@ pub fn maybe_inject_crates_ref(sess: &ParseSess,
attrs: vec![ast::Attribute {
style: ast::AttrStyle::Outer,
value: P(ast::MetaItem {
node: ast::MetaItemKind::Word(token::intern("prelude_import")),
name: token::intern("prelude_import"),
node: ast::MetaItemKind::Word,
span: span,
}),
id: attr::mk_attr_id(),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册