-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// 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 core::prelude::*;
+pub use self::StabilityLevel::*;
+pub use self::ReprAttr::*;
+pub use self::IntType::*;
use ast;
-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 ast::{AttrId, Attribute, Name};
+use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
+use ast::{Lit, Expr, Item, Local, Stmt, StmtKind};
+use codemap::{Spanned, spanned, dummy_spanned, mk_sp};
+use syntax_pos::{Span, BytePos, DUMMY_SP};
+use errors::Handler;
+use feature_gate::{Features, GatedCfg};
+use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
+use parse::ParseSess;
+use ptr::P;
+use symbol::Symbol;
+use util::ThinVec;
-use core::vec;
-use core::hashmap::linear::LinearSet;
-use std;
+use std::cell::{RefCell, Cell};
+
+thread_local! {
+ static USED_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
+ static KNOWN_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
+}
+
+enum AttrError {
+ MultipleItem(Name),
+ UnknownMetaItem(Name),
+ MissingSince,
+ MissingFeature,
+ MultipleStabilityLevels,
+ UnsupportedLiteral
+}
+
+fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
+ match error {
+ AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
+ "multiple '{}' items", item),
+ AttrError::UnknownMetaItem(item) => span_err!(diag, span, E0541,
+ "unknown meta item '{}'", item),
+ AttrError::MissingSince => span_err!(diag, span, E0542, "missing 'since'"),
+ AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
+ AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
+ "multiple stability levels"),
+ AttrError::UnsupportedLiteral => span_err!(diag, span, E0565, "unsupported literal"),
+ }
+}
+
+pub fn mark_used(attr: &Attribute) {
+ debug!("Marking {:?} as used.", attr);
+ let AttrId(id) = attr.id;
+ USED_ATTRS.with(|slot| {
+ let idx = (id / 64) as usize;
+ let shift = id % 64;
+ if slot.borrow().len() <= idx {
+ slot.borrow_mut().resize(idx + 1, 0);
+ }
+ slot.borrow_mut()[idx] |= 1 << shift;
+ });
+}
+
+pub fn is_used(attr: &Attribute) -> bool {
+ let AttrId(id) = attr.id;
+ USED_ATTRS.with(|slot| {
+ let idx = (id / 64) as usize;
+ let shift = id % 64;
+ slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
+ .unwrap_or(false)
+ })
+}
+
+pub fn mark_known(attr: &Attribute) {
+ debug!("Marking {:?} as known.", attr);
+ let AttrId(id) = attr.id;
+ KNOWN_ATTRS.with(|slot| {
+ let idx = (id / 64) as usize;
+ let shift = id % 64;
+ if slot.borrow().len() <= idx {
+ slot.borrow_mut().resize(idx + 1, 0);
+ }
+ slot.borrow_mut()[idx] |= 1 << shift;
+ });
+}
+
+pub fn is_known(attr: &Attribute) -> bool {
+ let AttrId(id) = attr.id;
+ KNOWN_ATTRS.with(|slot| {
+ let idx = (id / 64) as usize;
+ let shift = id % 64;
+ slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
+ .unwrap_or(false)
+ })
+}
+
+impl NestedMetaItem {
+ /// Returns the MetaItem if self is a NestedMetaItemKind::MetaItem.
+ pub fn meta_item(&self) -> Option<&MetaItem> {
+ match self.node {
+ NestedMetaItemKind::MetaItem(ref item) => Some(&item),
+ _ => None
+ }
+ }
+
+ /// Returns the Lit if self is a NestedMetaItemKind::Literal.
+ pub fn literal(&self) -> Option<&Lit> {
+ match self.node {
+ NestedMetaItemKind::Literal(ref lit) => Some(&lit),
+ _ => None
+ }
+ }
+
+ /// Returns the Span for `self`.
+ pub fn span(&self) -> Span {
+ self.span
+ }
+
+ /// Returns true if this list item is a MetaItem with a name of `name`.
+ pub fn check_name(&self, name: &str) -> bool {
+ self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
+ }
+
+ /// Returns the name of the meta item, e.g. `foo` in `#[foo]`,
+ /// `#[foo="bar"]` and `#[foo(bar)]`, if self is a MetaItem
+ pub fn name(&self) -> Option<Name> {
+ self.meta_item().and_then(|meta_item| Some(meta_item.name()))
+ }
+
+ /// Gets the string value if self is a MetaItem and the MetaItem is a
+ /// MetaItemKind::NameValue variant containing a string, otherwise None.
+ pub fn value_str(&self) -> Option<Symbol> {
+ self.meta_item().and_then(|meta_item| meta_item.value_str())
+ }
+
+ /// Returns a MetaItem if self is a MetaItem with Kind Word.
+ pub fn word(&self) -> Option<&MetaItem> {
+ self.meta_item().and_then(|meta_item| if meta_item.is_word() {
+ Some(meta_item)
+ } else {
+ None
+ })
+ }
+
+ /// Gets a list of inner meta items from a list MetaItem type.
+ pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
+ self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
+ }
+
+ /// Returns `true` if the variant is MetaItem.
+ pub fn is_meta_item(&self) -> bool {
+ self.meta_item().is_some()
+ }
+
+ /// Returns `true` if the variant is Literal.
+ pub fn is_literal(&self) -> bool {
+ self.literal().is_some()
+ }
+
+ /// Returns `true` if self is a MetaItem and the meta item is a word.
+ pub fn is_word(&self) -> bool {
+ self.word().is_some()
+ }
+
+ /// Returns `true` if self is a MetaItem and the meta item is a ValueString.
+ pub fn is_value_str(&self) -> bool {
+ self.value_str().is_some()
+ }
+
+ /// Returns `true` if self is a MetaItem and the meta item is a list.
+ pub fn is_meta_item_list(&self) -> bool {
+ self.meta_item_list().is_some()
+ }
+}
+
+impl Attribute {
+ pub fn check_name(&self, name: &str) -> bool {
+ let matches = self.name() == name;
+ if matches {
+ mark_used(self);
+ }
+ matches
+ }
+
+ pub fn name(&self) -> Name { self.meta().name() }
+
+ pub fn value_str(&self) -> Option<Symbol> {
+ self.meta().value_str()
+ }
+
+ pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
+ self.meta().meta_item_list()
+ }
+
+ pub fn is_word(&self) -> bool { self.meta().is_word() }
+
+ pub fn span(&self) -> Span { self.meta().span }
+
+ pub fn is_meta_item_list(&self) -> bool {
+ self.meta_item_list().is_some()
+ }
+
+ /// Indicates if the attribute is a Value String.
+ pub fn is_value_str(&self) -> bool {
+ self.value_str().is_some()
+ }
+}
+
+impl MetaItem {
+ pub fn name(&self) -> Name {
+ self.name
+ }
+
+ pub fn value_str(&self) -> Option<Symbol> {
+ match self.node {
+ MetaItemKind::NameValue(ref v) => {
+ match v.node {
+ ast::LitKind::Str(ref s, _) => Some((*s).clone()),
+ _ => None,
+ }
+ },
+ _ => None
+ }
+ }
+
+ pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
+ match self.node {
+ MetaItemKind::List(ref l) => Some(&l[..]),
+ _ => None
+ }
+ }
+
+ pub fn is_word(&self) -> bool {
+ match self.node {
+ MetaItemKind::Word => true,
+ _ => false,
+ }
+ }
+
+ pub fn span(&self) -> Span { self.span }
+
+ pub fn check_name(&self, name: &str) -> bool {
+ self.name() == name
+ }
+
+ pub fn is_value_str(&self) -> bool {
+ self.value_str().is_some()
+ }
+
+ pub fn is_meta_item_list(&self) -> bool {
+ self.meta_item_list().is_some()
+ }
+}
+
+impl Attribute {
+ /// Extract the MetaItem from inside this Attribute.
+ pub fn meta(&self) -> &MetaItem {
+ &self.value
+ }
+
+ /// 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 with_desugared_doc<T, F>(&self, f: F) -> T where
+ F: FnOnce(&Attribute) -> T,
+ {
+ if self.is_sugared_doc {
+ let comment = self.value_str().unwrap();
+ let meta = mk_name_value_item_str(
+ Symbol::intern("doc"),
+ Symbol::intern(&strip_doc_comment_decoration(&comment.as_str())));
+ if self.style == ast::AttrStyle::Outer {
+ f(&mk_attr_outer(self.span, self.id, meta))
+ } else {
+ f(&mk_attr_inner(self.span, self.id, meta))
+ }
+ } else {
+ f(self)
+ }
+ }
+}
/* 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_str(name: Name, value: Symbol) -> MetaItem {
+ let value_lit = dummy_spanned(ast::LitKind::Str(value, ast::StrStyle::Cooked));
+ mk_spanned_name_value_item(DUMMY_SP, 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_name_value_item(name: Name, value: ast::Lit) -> MetaItem {
+ mk_spanned_name_value_item(DUMMY_SP, 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_list_item(name: Name, items: Vec<NestedMetaItem>) -> MetaItem {
+ mk_spanned_list_item(DUMMY_SP, name, items)
}
-pub fn mk_word_item(name: @~str) -> @ast::meta_item {
- @dummy_spanned(ast::meta_word(name))
+pub fn mk_list_word_item(name: Name) -> ast::NestedMetaItem {
+ dummy_spanned(NestedMetaItemKind::MetaItem(mk_spanned_word_item(DUMMY_SP, name)))
}
-pub fn mk_attr(item: @ast::meta_item) -> ast::attribute {
- dummy_spanned(ast::attribute_ { style: ast::attr_inner,
- value: item,
- is_sugared_doc: false })
+pub fn mk_word_item(name: Name) -> MetaItem {
+ mk_spanned_word_item(DUMMY_SP, name)
}
-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)
+pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> MetaItem {
+ MetaItem { span: sp, name: name, node: MetaItemKind::NameValue(value) }
+}
+
+pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec<NestedMetaItem>) -> MetaItem {
+ MetaItem { span: sp, name: name, node: MetaItemKind::List(items) }
+}
+
+pub fn mk_spanned_word_item(sp: Span, name: Name) -> MetaItem {
+ MetaItem { span: sp, name: name, node: MetaItemKind::Word }
}
-/* Conversion */
-pub fn attr_meta(attr: ast::attribute) -> @ast::meta_item {
- attr.node.value
+
+thread_local! { static NEXT_ATTR_ID: Cell<usize> = Cell::new(0) }
+
+pub fn mk_attr_id() -> AttrId {
+ let id = NEXT_ATTR_ID.with(|slot| {
+ let r = slot.get();
+ slot.set(r + 1);
+ r
+ });
+ AttrId(id)
}
-// 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) }
+/// Returns an inner attribute with the given value.
+pub fn mk_attr_inner(span: Span, id: AttrId, item: MetaItem) -> Attribute {
+ mk_spanned_attr_inner(span, id, item)
}
-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));
- mk_attr(meta)
- } else {
- *attr
+/// Returns an innter attribute with the given value and span.
+pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
+ Attribute {
+ id: id,
+ style: ast::AttrStyle::Inner,
+ value: item,
+ is_sugared_doc: false,
+ span: sp,
}
}
-/* Accessors */
-pub fn get_attr_name(attr: &ast::attribute) -> @~str {
- get_meta_item_name(attr.node.value)
+/// Returns an outer attribute with the given value.
+pub fn mk_attr_outer(span: Span, id: AttrId, item: MetaItem) -> Attribute {
+ mk_spanned_attr_outer(span, id, item)
}
-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,
+/// Returns an outer attribute with the given value and span.
+pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
+ Attribute {
+ id: id,
+ style: ast::AttrStyle::Outer,
+ value: item,
+ is_sugared_doc: false,
+ span: sp,
}
}
-/**
- * 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,
- }
+pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, lo: BytePos, hi: BytePos)
+ -> Attribute {
+ let style = doc_comment_style(&text.as_str());
+ let lit = spanned(lo, hi, ast::LitKind::Str(text, ast::StrStyle::Cooked));
+ Attribute {
+ id: id,
+ style: style,
+ value: MetaItem {
+ span: mk_sp(lo, hi),
+ name: Symbol::intern("doc"),
+ node: MetaItemKind::NameValue(lit),
},
- _ => None
+ is_sugared_doc: true,
+ span: mk_sp(lo, hi),
}
}
-/// 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) */ copy *l),
- _ => None
- }
+pub fn list_contains_name(items: &[NestedMetaItem], name: &str) -> bool {
+ debug!("attr::list_contains_name (name={})", name);
+ items.iter().any(|item| {
+ debug!(" testing: {:?}", item.name());
+ item.check_name(name)
+ })
}
-/**
- * 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 contains_name(attrs: &[Attribute], name: &str) -> bool {
+ debug!("attr::contains_name (name={})", name);
+ attrs.iter().any(|item| {
+ debug!(" testing: {}", item.name());
+ item.check_name(name)
+ })
}
+pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) -> Option<Symbol> {
+ attrs.iter()
+ .find(|at| at.check_name(name))
+ .and_then(|at| at.value_str())
+}
+
+/* Higher-level applications */
-/* Searching */
+pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
+ first_attr_value_str_by_name(attrs, "crate_name")
+}
-/// 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 vec::filter_mapped(attrs) |a| {
- if name == *get_attr_name(a) {
- Some(*a)
+/// Find the value of #[export_name=*] attribute and check its validity.
+pub fn find_export_name_attr(diag: &Handler, attrs: &[Attribute]) -> Option<Symbol> {
+ attrs.iter().fold(None, |ia,attr| {
+ if attr.check_name("export_name") {
+ if let s@Some(_) = attr.value_str() {
+ s
+ } else {
+ struct_span_err!(diag, attr.span, E0558,
+ "export_name attribute has invalid format")
+ .span_label(attr.span,
+ &format!("did you mean #[export_name=\"*\"]?"))
+ .emit();
+ None
+ }
} else {
- None
+ ia
}
- }
+ })
+}
+
+pub fn contains_extern_indicator(diag: &Handler, attrs: &[Attribute]) -> bool {
+ contains_name(attrs, "no_mangle") ||
+ find_export_name_attr(diag, attrs).is_some()
}
-/// 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.each |mi| {
- if name == *get_meta_item_name(*mi) {
- rs.push(*mi)
+#[derive(Copy, Clone, PartialEq)]
+pub enum InlineAttr {
+ None,
+ Hint,
+ Always,
+ Never,
+}
+
+/// 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| {
+ match attr.value.node {
+ _ if attr.value.name != "inline" => ia,
+ MetaItemKind::Word => {
+ mark_used(attr);
+ InlineAttr::Hint
+ }
+ MetaItemKind::List(ref items) => {
+ mark_used(attr);
+ if items.len() != 1 {
+ diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); });
+ InlineAttr::None
+ } else if list_contains_name(&items[..], "always") {
+ InlineAttr::Always
+ } else if list_contains_name(&items[..], "never") {
+ InlineAttr::Never
+ } else {
+ diagnostic.map(|d| {
+ span_err!(d, items[0].span, E0535, "invalid argument");
+ });
+
+ InlineAttr::None
+ }
+ }
+ _ => ia,
}
- }
- rs
+ })
}
-/**
- * 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.each |item| {
- if eq(*item, needle) { return true; }
+/// True if `#[inline]` or `#[inline(always)]` is present in `attrs`.
+pub fn requests_inline(attrs: &[Attribute]) -> bool {
+ match find_inline_attr(None, attrs) {
+ InlineAttr::Hint | InlineAttr::Always => true,
+ InlineAttr::None | InlineAttr::Never => false,
}
- return false;
}
-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
+/// 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 mis) => {
+ for mi in mis.iter() {
+ if !mi.is_meta_item() {
+ handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral);
+ return false;
+ }
}
- _ => 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.each |mi| {
- if !misb.contains(mi) { return false; }
+
+ // The unwraps below may look dangerous, but we've already asserted
+ // that they won't fail with the loop above.
+ match &*cfg.name.as_str() {
+ "any" => mis.iter().any(|mi| {
+ cfg_matches(mi.meta_item().unwrap(), sess, features)
+ }),
+ "all" => mis.iter().all(|mi| {
+ cfg_matches(mi.meta_item().unwrap(), sess, features)
+ }),
+ "not" => {
+ if mis.len() != 1 {
+ span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
+ return false;
+ }
+
+ !cfg_matches(mis[0].meta_item().unwrap(), sess, features)
+ },
+ p => {
+ span_err!(sess.span_diagnostic, cfg.span, E0537, "invalid predicate `{}`", p);
+ false
}
- true
}
- _ => false
+ },
+ ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
+ if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
+ gated_cfg.check_and_emit(sess, feats);
+ }
+ sess.config.contains(&(cfg.name(), cfg.value_str()))
}
}
}
-pub fn contains_name(metas: &[@ast::meta_item], name: &str) -> bool {
- let matches = find_meta_items_by_name(metas, name);
- matches.len() > 0u
+/// Represents the #[stable], #[unstable] and #[rustc_deprecated] attributes.
+#[derive(RustcEncodable, RustcDecodable, Clone, Debug, PartialEq, Eq, Hash)]
+pub struct Stability {
+ pub level: StabilityLevel,
+ pub feature: Symbol,
+ pub rustc_depr: Option<RustcDeprecation>,
}
-pub fn attrs_contains_name(attrs: &[ast::attribute], name: &str) -> bool {
- !find_attrs_by_name(attrs, name).is_empty()
+/// The available stability levels.
+#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
+pub enum StabilityLevel {
+ // Reason for the current stability level and the relevant rust-lang issue
+ Unstable { reason: Option<Symbol>, issue: u32 },
+ Stable { since: Symbol },
}
-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
- }
+#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
+pub struct RustcDeprecation {
+ pub since: Symbol,
+ pub reason: Symbol,
}
-fn last_meta_item_by_name(items: &[@ast::meta_item], name: &str)
- -> Option<@ast::meta_item> {
+#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
+pub struct Deprecation {
+ pub since: Option<Symbol>,
+ pub note: Option<Symbol>,
+}
- let items = attr::find_meta_items_by_name(items, name);
- items.last_opt().map(|item| **item)
+impl StabilityLevel {
+ pub fn is_unstable(&self) -> bool { if let Unstable {..} = *self { true } else { false }}
+ pub fn is_stable(&self) -> bool { if let Stable {..} = *self { true } else { false }}
}
-pub fn last_meta_item_value_str_by_name(items: &[@ast::meta_item], name: &str)
- -> Option<@~str> {
+fn find_stability_generic<'a, I>(diagnostic: &Handler,
+ attrs_iter: I,
+ item_sp: Span)
+ -> Option<Stability>
+ where I: Iterator<Item = &'a Attribute>
+{
+ let mut stab: Option<Stability> = None;
+ let mut rustc_depr: Option<RustcDeprecation> = None;
- match last_meta_item_by_name(items, name) {
- Some(item) => {
- match attr::get_meta_item_value_str(item) {
- Some(value) => Some(value),
- None => None
+ 'outer: for attr in attrs_iter {
+ let tag = attr.name();
+ if tag != "rustc_deprecated" && tag != "unstable" && tag != "stable" {
+ continue // not a stability level
+ }
+
+ mark_used(attr);
+
+ if let Some(metas) = attr.meta_item_list() {
+ let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
+ if item.is_some() {
+ handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
+ return false
+ }
+ if let Some(v) = meta.value_str() {
+ *item = Some(v);
+ true
+ } else {
+ span_err!(diagnostic, meta.span, E0539, "incorrect meta item");
+ false
+ }
+ };
+
+ match &*tag.as_str() {
+ "rustc_deprecated" => {
+ if rustc_depr.is_some() {
+ span_err!(diagnostic, item_sp, E0540,
+ "multiple rustc_deprecated attributes");
+ break
+ }
+
+ let mut since = None;
+ let mut reason = None;
+ for meta in metas {
+ if let Some(mi) = meta.meta_item() {
+ match &*mi.name().as_str() {
+ "since" => if !get(mi, &mut since) { continue 'outer },
+ "reason" => if !get(mi, &mut reason) { continue 'outer },
+ _ => {
+ handle_errors(diagnostic, mi.span,
+ AttrError::UnknownMetaItem(mi.name()));
+ continue 'outer
+ }
+ }
+ } else {
+ handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
+ continue 'outer
+ }
+ }
+
+ match (since, reason) {
+ (Some(since), Some(reason)) => {
+ rustc_depr = Some(RustcDeprecation {
+ since: since,
+ reason: reason,
+ })
+ }
+ (None, _) => {
+ handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
+ continue
+ }
+ _ => {
+ span_err!(diagnostic, attr.span(), E0543, "missing 'reason'");
+ continue
+ }
+ }
+ }
+ "unstable" => {
+ if stab.is_some() {
+ handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
+ break
+ }
+
+ let mut feature = None;
+ let mut reason = None;
+ let mut issue = None;
+ for meta in metas {
+ if let Some(mi) = meta.meta_item() {
+ match &*mi.name().as_str() {
+ "feature" => if !get(mi, &mut feature) { continue 'outer },
+ "reason" => if !get(mi, &mut reason) { continue 'outer },
+ "issue" => if !get(mi, &mut issue) { continue 'outer },
+ _ => {
+ handle_errors(diagnostic, meta.span,
+ AttrError::UnknownMetaItem(mi.name()));
+ continue 'outer
+ }
+ }
+ } else {
+ handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
+ continue 'outer
+ }
+ }
+
+ match (feature, reason, issue) {
+ (Some(feature), reason, Some(issue)) => {
+ stab = Some(Stability {
+ level: Unstable {
+ reason: reason,
+ issue: {
+ if let Ok(issue) = issue.as_str().parse() {
+ issue
+ } else {
+ span_err!(diagnostic, attr.span(), E0545,
+ "incorrect 'issue'");
+ continue
+ }
+ }
+ },
+ feature: feature,
+ rustc_depr: None,
+ })
+ }
+ (None, _, _) => {
+ handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
+ continue
+ }
+ _ => {
+ span_err!(diagnostic, attr.span(), E0547, "missing 'issue'");
+ continue
+ }
+ }
+ }
+ "stable" => {
+ if stab.is_some() {
+ handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
+ break
+ }
+
+ let mut feature = None;
+ let mut since = None;
+ for meta in metas {
+ if let NestedMetaItemKind::MetaItem(ref mi) = meta.node {
+ match &*mi.name().as_str() {
+ "feature" => if !get(mi, &mut feature) { continue 'outer },
+ "since" => if !get(mi, &mut since) { continue 'outer },
+ _ => {
+ handle_errors(diagnostic, meta.span,
+ AttrError::UnknownMetaItem(mi.name()));
+ continue 'outer
+ }
+ }
+ } else {
+ handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
+ continue 'outer
+ }
+ }
+
+ match (feature, since) {
+ (Some(feature), Some(since)) => {
+ stab = Some(Stability {
+ level: Stable {
+ since: since,
+ },
+ feature: feature,
+ rustc_depr: None,
+ })
+ }
+ (None, _) => {
+ handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
+ continue
+ }
+ _ => {
+ handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
+ continue
+ }
+ }
+ }
+ _ => unreachable!()
}
- },
- None => None
+ } else {
+ span_err!(diagnostic, attr.span(), E0548, "incorrect stability attribute type");
+ continue
+ }
}
+
+ // Merge the deprecation info into the stability info
+ if let Some(rustc_depr) = rustc_depr {
+ if let Some(ref mut stab) = stab {
+ stab.rustc_depr = Some(rustc_depr);
+ } else {
+ span_err!(diagnostic, item_sp, E0549,
+ "rustc_deprecated attribute must be paired with \
+ either stable or unstable attribute");
+ }
+ }
+
+ stab
}
-pub fn last_meta_item_list_by_name(items: ~[@ast::meta_item], name: &str)
- -> Option<~[@ast::meta_item]> {
+fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
+ attrs_iter: I,
+ item_sp: Span)
+ -> Option<Deprecation>
+ where I: Iterator<Item = &'a Attribute>
+{
+ let mut depr: Option<Deprecation> = None;
+
+ 'outer: for attr in attrs_iter {
+ if attr.name() != "deprecated" {
+ continue
+ }
+
+ mark_used(attr);
- match last_meta_item_by_name(items, name) {
- Some(item) => attr::get_meta_item_list(item),
- None => None
+ if depr.is_some() {
+ span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
+ break
+ }
+
+ depr = if let Some(metas) = attr.meta_item_list() {
+ let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
+ if item.is_some() {
+ handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
+ return false
+ }
+ if let Some(v) = meta.value_str() {
+ *item = Some(v);
+ true
+ } else {
+ span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
+ false
+ }
+ };
+
+ let mut since = None;
+ let mut note = None;
+ for meta in metas {
+ if let NestedMetaItemKind::MetaItem(ref mi) = meta.node {
+ match &*mi.name().as_str() {
+ "since" => if !get(mi, &mut since) { continue 'outer },
+ "note" => if !get(mi, &mut note) { continue 'outer },
+ _ => {
+ handle_errors(diagnostic, meta.span,
+ AttrError::UnknownMetaItem(mi.name()));
+ continue 'outer
+ }
+ }
+ } else {
+ handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
+ continue 'outer
+ }
+ }
+
+ Some(Deprecation {since: since, note: note})
+ } else {
+ Some(Deprecation{since: None, note: None})
+ }
}
+
+ depr
}
+/// Find the first stability attribute. `None` if none exists.
+pub fn find_stability(diagnostic: &Handler, attrs: &[Attribute],
+ item_sp: Span) -> Option<Stability> {
+ find_stability_generic(diagnostic, attrs.iter(), item_sp)
+}
-/* Higher-level applications */
+/// Find the deprecation attribute. `None` if none exists.
+pub fn find_deprecation(diagnostic: &Handler, attrs: &[Attribute],
+ item_sp: Span) -> Option<Deprecation> {
+ find_deprecation_generic(diagnostic, attrs.iter(), item_sp)
+}
-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 = vec::from_slice(items);
- do std::sort::quick_sort(v) |ma, mb| {
- get_meta_item_name(*ma) <= get_meta_item_name(*mb)
- }
- // There doesn't seem to be a more optimal way to do this
- do v.map |m| {
- match m.node {
- ast::meta_list(n, ref mis) => {
- @spanned {
- node: ast::meta_list(n, sort_meta_items(*mis)),
- .. /*bad*/ copy **m
+/// Parse #[repr(...)] forms.
+///
+/// Valid repr contents: any of the primitive integral type names (see
+/// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
+/// the same discriminant size that the corresponding C enum would or C
+/// structure layout, and `packed` to remove padding.
+pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
+ let mut acc = Vec::new();
+ match attr.value.node {
+ ast::MetaItemKind::List(ref items) if attr.value.name == "repr" => {
+ mark_used(attr);
+ for item in items {
+ if !item.is_meta_item() {
+ handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral);
+ continue
+ }
+
+ if let Some(mi) = item.word() {
+ let word = &*mi.name().as_str();
+ let hint = match word {
+ // Can't use "extern" because it's not a lexical identifier.
+ "C" => Some(ReprExtern),
+ "packed" => Some(ReprPacked),
+ "simd" => Some(ReprSimd),
+ _ => match int_type_of_word(word) {
+ Some(ity) => Some(ReprInt(ity)),
+ None => {
+ // Not a word we recognize
+ span_err!(diagnostic, item.span, E0552,
+ "unrecognized representation hint");
+ None
+ }
+ }
+ };
+
+ if let Some(h) = hint {
+ acc.push(h);
+ }
+ } else {
+ span_err!(diagnostic, item.span, E0553,
+ "unrecognized enum representation hint");
}
}
- _ => /*bad*/ copy *m
}
+ // Not a "repr" hint: ignore.
+ _ => { }
}
+ acc
}
-pub fn remove_meta_items_by_name(items: ~[@ast::meta_item], name: &str) ->
- ~[@ast::meta_item] {
+fn int_type_of_word(s: &str) -> Option<IntType> {
+ match s {
+ "i8" => Some(SignedInt(ast::IntTy::I8)),
+ "u8" => Some(UnsignedInt(ast::UintTy::U8)),
+ "i16" => Some(SignedInt(ast::IntTy::I16)),
+ "u16" => Some(UnsignedInt(ast::UintTy::U16)),
+ "i32" => Some(SignedInt(ast::IntTy::I32)),
+ "u32" => Some(UnsignedInt(ast::UintTy::U32)),
+ "i64" => Some(SignedInt(ast::IntTy::I64)),
+ "u64" => Some(UnsignedInt(ast::UintTy::U64)),
+ "i128" => Some(SignedInt(ast::IntTy::I128)),
+ "u128" => Some(UnsignedInt(ast::UintTy::U128)),
+ "isize" => Some(SignedInt(ast::IntTy::Is)),
+ "usize" => Some(UnsignedInt(ast::UintTy::Us)),
+ _ => None
+ }
+}
- return vec::filter_mapped(items, |item| {
- if name != *get_meta_item_name(*item) {
- Some(*item)
- } else {
- None
- }
- });
+#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
+pub enum ReprAttr {
+ ReprInt(IntType),
+ ReprExtern,
+ ReprPacked,
+ ReprSimd,
}
-/**
- * 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) */ copy *items,
- _ => ~[]
+#[derive(Eq, Hash, PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
+pub enum IntType {
+ SignedInt(ast::IntTy),
+ UnsignedInt(ast::UintTy)
+}
+
+impl IntType {
+ #[inline]
+ pub fn is_signed(self) -> bool {
+ match self {
+ SignedInt(..) => true,
+ UnsignedInt(..) => false
}
}
}
-#[deriving(Eq)]
-pub enum inline_attr {
- ia_none,
- ia_hint,
- ia_always,
- ia_never,
+pub trait HasAttrs: Sized {
+ fn attrs(&self) -> &[ast::Attribute];
+ fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
}
-/// True if something like #[inline] is found in the list of attrs.
-pub fn find_inline_attr(attrs: &[ast::attribute]) -> inline_attr {
- // FIXME (#2809)---validate the usage of #[inline] and #[inline(always)]
- do vec::foldl(ia_none, attrs) |ia,attr| {
- match attr.node.value.node {
- ast::meta_word(@~"inline") => ia_hint,
- ast::meta_list(@~"inline", ref items) => {
- 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
- } else {
- ia_hint
+impl<T: HasAttrs> HasAttrs for Spanned<T> {
+ fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
+ fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
+ Spanned { node: self.node.map_attrs(f), span: self.span }
+ }
+}
+
+impl HasAttrs for Vec<Attribute> {
+ fn attrs(&self) -> &[Attribute] {
+ &self
+ }
+ fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
+ f(self)
+ }
+}
+
+impl HasAttrs for ThinVec<Attribute> {
+ fn attrs(&self) -> &[Attribute] {
+ &self
+ }
+ fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
+ f(self.into()).into()
+ }
+}
+
+impl<T: HasAttrs + 'static> HasAttrs for P<T> {
+ fn attrs(&self) -> &[Attribute] {
+ (**self).attrs()
+ }
+ fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
+ self.map(|t| t.map_attrs(f))
+ }
+}
+
+impl HasAttrs for StmtKind {
+ fn attrs(&self) -> &[Attribute] {
+ match *self {
+ StmtKind::Local(ref local) => local.attrs(),
+ StmtKind::Item(..) => &[],
+ StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
+ StmtKind::Mac(ref mac) => {
+ let (_, _, ref attrs) = **mac;
+ attrs.attrs()
}
- }
- _ => ia
+ }
+ }
+
+ fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
+ match self {
+ StmtKind::Local(local) => StmtKind::Local(local.map_attrs(f)),
+ StmtKind::Item(..) => self,
+ StmtKind::Expr(expr) => StmtKind::Expr(expr.map_attrs(f)),
+ StmtKind::Semi(expr) => StmtKind::Semi(expr.map_attrs(f)),
+ StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, style, attrs)| {
+ (mac, style, attrs.map_attrs(f))
+ })),
}
}
}
+impl HasAttrs for Stmt {
+ fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
+ fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
+ Stmt { id: self.id, node: self.node.map_attrs(f), span: self.span }
+ }
+}
-pub fn require_unique_names(diagnostic: @span_handler,
- metas: &[@ast::meta_item]) {
- let mut set = LinearSet::new();
- for metas.each |meta| {
- let name = get_meta_item_name(*meta);
+macro_rules! derive_has_attrs {
+ ($($ty:path),*) => { $(
+ impl HasAttrs for $ty {
+ fn attrs(&self) -> &[Attribute] {
+ &self.attrs
+ }
- // FIXME: How do I silence the warnings? --pcw (#2619)
- if !set.insert(name) {
- diagnostic.span_fatal(meta.span,
- fmt!("duplicate meta item `%s`", *name));
+ fn map_attrs<F>(mut self, f: F) -> Self
+ where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>,
+ {
+ self.attrs = self.attrs.map_attrs(f);
+ self
+ }
}
- }
+ )* }
}
-//
-// Local Variables:
-// mode: rust
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
-//
+derive_has_attrs! {
+ Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm,
+ ast::Field, ast::FieldPat, ast::Variant_
+}