2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
// Functions dealing with attributes and meta items
|
2011-06-28 17:25:20 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-07-19 06:51:37 -05:00
|
|
|
use ast::{Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
|
2013-05-24 17:08:45 -05:00
|
|
|
use codemap::{Span, Spanned, spanned, dummy_spanned};
|
2012-11-15 21:37:29 -06:00
|
|
|
use codemap::BytePos;
|
2013-12-31 08:17:59 -06:00
|
|
|
use diagnostic::SpanHandler;
|
2012-12-23 16:41:37 -06:00
|
|
|
use parse::comments::{doc_comment_style, strip_doc_comment_decoration};
|
2013-12-28 11:16:48 -06:00
|
|
|
use crateid::CrateId;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-06-24 19:40:33 -05:00
|
|
|
use std::hashmap::HashSet;
|
2012-03-02 15:14:10 -06:00
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
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)>;
|
2012-03-02 12:05:30 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
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() }
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
impl AttrMetaMethods for MetaItem {
|
|
|
|
fn name(&self) -> @str {
|
|
|
|
match self.node {
|
|
|
|
MetaWord(n) => n,
|
|
|
|
MetaNameValue(n, _) => n,
|
|
|
|
MetaList(n, _) => n
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
fn value_str(&self) -> Option<@str> {
|
|
|
|
match self.node {
|
|
|
|
MetaNameValue(_, ref v) => {
|
|
|
|
match v.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::LitStr(s, _) => Some(s),
|
2013-07-19 06:51:37 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
fn meta_item_list<'a>(&'a self) -> Option<&'a [@MetaItem]> {
|
|
|
|
match self.node {
|
|
|
|
MetaList(_, ref l) => Some(l.as_slice()),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
2013-08-09 03:25:24 -05:00
|
|
|
fn name_str_pair(&self) -> Option<(@str, @str)> {
|
2013-09-20 01:08:47 -05:00
|
|
|
self.value_str().map(|s| (self.name(), s))
|
2013-07-19 06:51:37 -05:00
|
|
|
}
|
2012-01-26 18:23:34 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
// 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()
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2013-07-19 06:51:37 -05:00
|
|
|
fn name_str_pair(&self) -> Option<(@str, @str)> { (**self).name_str_pair() }
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub trait AttributeMethods {
|
|
|
|
fn meta(&self) -> @MetaItem;
|
|
|
|
fn desugar_doc(&self) -> Attribute;
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
impl AttributeMethods for Attribute {
|
|
|
|
/// Extract the MetaItem from inside this Attribute.
|
2013-08-09 03:25:24 -05:00
|
|
|
fn meta(&self) -> @MetaItem {
|
2013-07-19 06:51:37 -05:00
|
|
|
self.node.value
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/// Convert self to a normal #[doc="foo"] comment, if it is a
|
|
|
|
/// comment like `///` or `/** */`. (Returns self unchanged for
|
|
|
|
/// non-sugared doc attributes.)
|
2013-08-09 03:25:24 -05:00
|
|
|
fn desugar_doc(&self) -> Attribute {
|
2013-07-19 06:51:37 -05:00
|
|
|
if self.node.is_sugared_doc {
|
2013-08-03 18:59:24 -05:00
|
|
|
let comment = self.value_str().unwrap();
|
2013-07-19 06:51:37 -05:00
|
|
|
let meta = mk_name_value_item_str(@"doc",
|
|
|
|
strip_doc_comment_decoration(comment).to_managed());
|
|
|
|
mk_attr(meta)
|
|
|
|
} else {
|
|
|
|
*self
|
|
|
|
}
|
2011-07-05 19:01:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/* Constructors */
|
2011-10-29 19:35:45 -05:00
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn mk_name_value_item_str(name: @str, value: @str) -> @MetaItem {
|
2014-01-09 07:05:33 -06:00
|
|
|
let value_lit = dummy_spanned(ast::LitStr(value, ast::CookedStr));
|
2013-07-19 06:51:37 -05:00
|
|
|
mk_name_value_item(name, value_lit)
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
2011-06-28 14:53:59 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn mk_name_value_item(name: @str, value: ast::Lit) -> @MetaItem {
|
2013-07-19 06:51:37 -05:00
|
|
|
@dummy_spanned(MetaNameValue(name, value))
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn mk_list_item(name: @str, items: ~[@MetaItem]) -> @MetaItem {
|
|
|
|
@dummy_spanned(MetaList(name, items))
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn mk_word_item(name: @str) -> @MetaItem {
|
|
|
|
@dummy_spanned(MetaWord(name))
|
2011-06-28 14:53:59 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn mk_attr(item: @MetaItem) -> Attribute {
|
|
|
|
dummy_spanned(Attribute_ {
|
|
|
|
style: ast::AttrInner,
|
|
|
|
value: item,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
})
|
2011-06-28 14:53:59 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn mk_sugared_doc_attr(text: @str, lo: BytePos, hi: BytePos) -> Attribute {
|
|
|
|
let style = doc_comment_style(text);
|
2014-01-09 07:05:33 -06:00
|
|
|
let lit = spanned(lo, hi, ast::LitStr(text, ast::CookedStr));
|
2013-07-19 06:51:37 -05:00
|
|
|
let attr = Attribute_ {
|
|
|
|
style: style,
|
|
|
|
value: @spanned(lo, hi, MetaNameValue(@"doc", lit)),
|
|
|
|
is_sugared_doc: true
|
|
|
|
};
|
|
|
|
spanned(lo, hi, attr)
|
2011-07-13 20:13:19 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/* 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 {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("attr::contains (name={})", needle.name());
|
2013-11-20 18:23:04 -06:00
|
|
|
haystack.iter().any(|item| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" testing: {}", item.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
item.node == needle.node
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn contains_name<AM: AttrMetaMethods>(metas: &[AM], name: &str) -> bool {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("attr::contains_name (name={})", name);
|
2013-11-20 18:23:04 -06:00
|
|
|
metas.iter().any(|item| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" testing: {}", item.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
name == item.name()
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
|
|
|
|
-> Option<@str> {
|
|
|
|
attrs.iter()
|
2013-08-09 22:49:29 -05:00
|
|
|
.find(|at| name == at.name())
|
2013-09-11 14:52:17 -05:00
|
|
|
.and_then(|at| at.value_str())
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str)
|
2013-06-12 12:02:55 -05:00
|
|
|
-> Option<@str> {
|
2013-09-11 14:52:17 -05:00
|
|
|
items.rev_iter().find(|mi| name == mi.name()).and_then(|i| i.value_str())
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Higher-level applications */
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
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()
|
2013-08-09 22:09:47 -05:00
|
|
|
.map(|&mi| (mi.name(), mi))
|
2013-07-19 06:51:37 -05:00
|
|
|
.collect::<~[(@str, @MetaItem)]>();
|
|
|
|
|
2013-12-19 21:42:00 -06:00
|
|
|
v.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
|
2013-02-16 08:31:57 -06:00
|
|
|
|
|
|
|
// There doesn't seem to be a more optimal way to do this
|
2013-11-20 18:23:04 -06:00
|
|
|
v.move_iter().map(|(_, m)| {
|
2013-02-16 08:31:57 -06:00
|
|
|
match m.node {
|
2013-07-19 06:51:37 -05:00
|
|
|
MetaList(n, ref mis) => {
|
2013-08-31 11:13:04 -05:00
|
|
|
@Spanned {
|
2013-07-19 06:51:37 -05:00
|
|
|
node: MetaList(n, sort_meta_items(*mis)),
|
|
|
|
.. /*bad*/ (*m).clone()
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 06:51:37 -05:00
|
|
|
_ => m
|
2013-02-16 08:31:57 -06:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
}).collect()
|
2011-06-29 16:17:23 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-08-22 18:43:23 -05:00
|
|
|
* From a list of crate attributes get only the meta_items that affect crate
|
2012-07-04 16:53:12 -05:00
|
|
|
* linkage
|
|
|
|
*/
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn find_linkage_metas(attrs: &[Attribute]) -> ~[@MetaItem] {
|
|
|
|
let mut result = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for attr in attrs.iter().filter(|at| "link" == at.name()) {
|
2013-07-19 06:51:37 -05:00
|
|
|
match attr.meta().node {
|
|
|
|
MetaList(_, ref items) => result.push_all(*items),
|
|
|
|
_ => ()
|
2011-07-05 13:46:02 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 06:51:37 -05:00
|
|
|
result
|
2011-07-05 13:46:02 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:14:01 -06:00
|
|
|
pub fn find_crateid(attrs: &[Attribute]) -> Option<CrateId> {
|
2013-12-17 15:40:33 -06:00
|
|
|
match first_attr_value_str_by_name(attrs, "crate_id") {
|
2013-12-09 15:56:53 -06:00
|
|
|
None => None,
|
2013-12-27 18:14:01 -06:00
|
|
|
Some(id) => from_str::<CrateId>(id),
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 07:04:54 -05:00
|
|
|
#[deriving(Eq)]
|
2013-07-19 06:51:37 -05:00
|
|
|
pub enum InlineAttr {
|
|
|
|
InlineNone,
|
|
|
|
InlineHint,
|
|
|
|
InlineAlways,
|
|
|
|
InlineNever,
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// True if something like #[inline] is found in the list of attrs.
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
|
2013-06-18 16:45:18 -05:00
|
|
|
// FIXME (#2809)---validate the usage of #[inline] and #[inline]
|
2013-11-20 18:23:04 -06:00
|
|
|
attrs.iter().fold(InlineNone, |ia,attr| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match attr.node.value.node {
|
2013-07-19 06:51:37 -05:00
|
|
|
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
|
2012-04-15 03:07:47 -05:00
|
|
|
} else {
|
2013-07-19 06:51:37 -05:00
|
|
|
InlineHint
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ia
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/// 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.
|
2013-11-20 18:23:04 -06:00
|
|
|
let some_cfg_matches = metas.any(|mi| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("testing name: {}", mi.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
if "cfg" == mi.name() { // it is a #[cfg()] attribute
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is cfg");
|
2013-07-19 06:51:37 -05:00
|
|
|
no_cfgs = false;
|
|
|
|
// only #[cfg(...)] ones are understood.
|
|
|
|
match mi.meta_item_list() {
|
|
|
|
Some(cfg_meta) => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is cfg(...)");
|
2013-11-20 18:23:04 -06:00
|
|
|
cfg_meta.iter().all(|cfg_mi| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("cfg({}[...])", cfg_mi.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
match cfg_mi.node {
|
|
|
|
ast::MetaList(s, ref not_cfgs) if "not" == s => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("not!");
|
2013-07-19 06:51:37 -05:00
|
|
|
// inside #[cfg(not(...))], so these need to all
|
|
|
|
// not match.
|
|
|
|
not_cfgs.iter().all(|mi| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("cfg(not({}[...]))", mi.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
!contains(cfg, *mi)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => contains(cfg, *cfg_mi)
|
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2013-07-19 06:51:37 -05:00
|
|
|
}
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
});
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("test_cfg (no_cfgs={}, some_cfg_matches={})", no_cfgs, some_cfg_matches);
|
2013-07-19 06:51:37 -05:00
|
|
|
no_cfgs || some_cfg_matches
|
|
|
|
}
|
2012-01-16 23:12:08 -06:00
|
|
|
|
2013-08-31 02:13:57 -05:00
|
|
|
/// Represents the #[deprecated="foo"] (etc) attributes.
|
|
|
|
pub struct Stability {
|
|
|
|
level: StabilityLevel,
|
|
|
|
text: Option<@str>
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The available stability levels.
|
2013-09-26 14:53:06 -05:00
|
|
|
#[deriving(Eq,Ord,Clone,ToStr)]
|
2013-08-31 02:13:57 -05:00
|
|
|
pub enum StabilityLevel {
|
|
|
|
Deprecated,
|
|
|
|
Experimental,
|
|
|
|
Unstable,
|
|
|
|
Stable,
|
|
|
|
Frozen,
|
|
|
|
Locked
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the first stability attribute. `None` if none exists.
|
|
|
|
pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It) -> Option<Stability> {
|
|
|
|
for m in metas {
|
|
|
|
let level = match m.name().as_slice() {
|
|
|
|
"deprecated" => Deprecated,
|
|
|
|
"experimental" => Experimental,
|
|
|
|
"unstable" => Unstable,
|
|
|
|
"stable" => Stable,
|
|
|
|
"frozen" => Frozen,
|
|
|
|
"locked" => Locked,
|
2013-10-01 16:31:03 -05:00
|
|
|
_ => continue // not a stability level
|
2013-08-31 02:13:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return Some(Stability {
|
|
|
|
level: level,
|
|
|
|
text: m.value_str()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2013-12-27 15:48:00 -06:00
|
|
|
pub fn require_unique_names(diagnostic: @SpanHandler, metas: &[@MetaItem]) {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut set = HashSet::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for meta in metas.iter() {
|
2013-07-19 06:51:37 -05:00
|
|
|
let name = meta.name();
|
2012-06-07 22:12:05 -05:00
|
|
|
|
2013-02-14 22:19:27 -06:00
|
|
|
if !set.insert(name) {
|
2012-04-15 03:07:47 -05:00
|
|
|
diagnostic.span_fatal(meta.span,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("duplicate meta item `{}`", name));
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 17:08:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fold this over attributes to parse #[repr(...)] forms.
|
|
|
|
*
|
|
|
|
* Valid repr contents: any of the primitive integral type names (see
|
|
|
|
* `int_type_of_word`, below) to specify the discriminant type; and `C`, to use
|
|
|
|
* the same discriminant size that the corresponding C enum would. These are
|
|
|
|
* not allowed on univariant or zero-variant enums, which have no discriminant.
|
|
|
|
*
|
|
|
|
* If a discriminant type is so specified, then the discriminant will be
|
|
|
|
* present (before fields, if any) with that type; reprensentation
|
|
|
|
* optimizations which would remove it will not be done.
|
|
|
|
*/
|
2013-12-27 15:48:00 -06:00
|
|
|
pub fn find_repr_attr(diagnostic: @SpanHandler, attr: @ast::MetaItem, acc: ReprAttr)
|
2013-05-24 17:08:45 -05:00
|
|
|
-> ReprAttr {
|
|
|
|
let mut acc = acc;
|
|
|
|
match attr.node {
|
|
|
|
ast::MetaList(s, ref items) if "repr" == s => {
|
|
|
|
for item in items.iter() {
|
|
|
|
match item.node {
|
|
|
|
ast::MetaWord(word) => {
|
2013-09-25 11:41:10 -05:00
|
|
|
let hint = match word.as_slice() {
|
2013-05-24 17:08:45 -05:00
|
|
|
// Can't use "extern" because it's not a lexical identifier.
|
|
|
|
"C" => ReprExtern,
|
|
|
|
_ => match int_type_of_word(word) {
|
|
|
|
Some(ity) => ReprInt(item.span, ity),
|
|
|
|
None => {
|
|
|
|
// Not a word we recognize
|
|
|
|
diagnostic.span_err(item.span,
|
|
|
|
"unrecognized representation hint");
|
|
|
|
ReprAny
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if hint != ReprAny {
|
|
|
|
if acc == ReprAny {
|
|
|
|
acc = hint;
|
|
|
|
} else if acc != hint {
|
|
|
|
diagnostic.span_warn(item.span,
|
|
|
|
"conflicting representation hint ignored")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not a word:
|
|
|
|
_ => diagnostic.span_err(item.span, "unrecognized representation hint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not a "repr" hint: ignore.
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_type_of_word(s: &str) -> Option<IntType> {
|
|
|
|
match s {
|
2014-01-09 07:05:33 -06:00
|
|
|
"i8" => Some(SignedInt(ast::TyI8)),
|
|
|
|
"u8" => Some(UnsignedInt(ast::TyU8)),
|
|
|
|
"i16" => Some(SignedInt(ast::TyI16)),
|
|
|
|
"u16" => Some(UnsignedInt(ast::TyU16)),
|
|
|
|
"i32" => Some(SignedInt(ast::TyI32)),
|
|
|
|
"u32" => Some(UnsignedInt(ast::TyU32)),
|
|
|
|
"i64" => Some(SignedInt(ast::TyI64)),
|
|
|
|
"u64" => Some(UnsignedInt(ast::TyU64)),
|
|
|
|
"int" => Some(SignedInt(ast::TyI)),
|
|
|
|
"uint" => Some(UnsignedInt(ast::TyU)),
|
2013-05-24 17:08:45 -05:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Eq)]
|
|
|
|
pub enum ReprAttr {
|
|
|
|
ReprAny,
|
|
|
|
ReprInt(Span, IntType),
|
|
|
|
ReprExtern
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:03:35 -05:00
|
|
|
impl ReprAttr {
|
|
|
|
pub fn is_ffi_safe(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
ReprAny => false,
|
|
|
|
ReprInt(_sp, ity) => ity.is_ffi_safe(),
|
|
|
|
ReprExtern => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 17:08:45 -05:00
|
|
|
#[deriving(Eq)]
|
|
|
|
pub enum IntType {
|
2014-01-09 07:05:33 -06:00
|
|
|
SignedInt(ast::IntTy),
|
|
|
|
UnsignedInt(ast::UintTy)
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntType {
|
|
|
|
#[inline]
|
|
|
|
pub fn is_signed(self) -> bool {
|
|
|
|
match self {
|
2013-11-28 14:22:53 -06:00
|
|
|
SignedInt(..) => true,
|
|
|
|
UnsignedInt(..) => false
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-02 15:03:35 -05:00
|
|
|
fn is_ffi_safe(self) -> bool {
|
|
|
|
match self {
|
2014-01-09 07:05:33 -06:00
|
|
|
SignedInt(ast::TyI8) | UnsignedInt(ast::TyU8) |
|
|
|
|
SignedInt(ast::TyI16) | UnsignedInt(ast::TyU16) |
|
|
|
|
SignedInt(ast::TyI32) | UnsignedInt(ast::TyU32) |
|
|
|
|
SignedInt(ast::TyI64) | UnsignedInt(ast::TyU64) => true,
|
2013-06-02 15:03:35 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|