1 //! Functions dealing with attributes and meta items.
4 use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute};
5 use crate::ast::{Expr, GenericParam, Item, Lit, LitKind, Local, Stmt, StmtKind};
6 use crate::ast::{Ident, Name, Path, PathSegment};
7 use crate::ast::{MacArgs, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
8 use crate::mut_visit::visit_clobber;
10 use crate::token::{self, Token};
11 use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
13 use rustc_data_structures::sync::Lock;
14 use rustc_index::bit_set::GrowableBitSet;
15 use rustc_span::edition::{Edition, DEFAULT_EDITION};
16 use rustc_span::source_map::{BytePos, Spanned};
17 use rustc_span::symbol::{sym, Symbol};
22 use std::ops::DerefMut;
25 used_attrs: Lock<GrowableBitSet<AttrId>>,
26 known_attrs: Lock<GrowableBitSet<AttrId>>,
27 rustc_span_globals: rustc_span::Globals,
31 fn new(edition: Edition) -> Globals {
33 // We have no idea how many attributes there will be, so just
34 // initiate the vectors with 0 bits. We'll grow them as necessary.
35 used_attrs: Lock::new(GrowableBitSet::new_empty()),
36 known_attrs: Lock::new(GrowableBitSet::new_empty()),
37 rustc_span_globals: rustc_span::Globals::new(edition),
42 pub fn with_globals<R>(edition: Edition, f: impl FnOnce() -> R) -> R {
43 let globals = Globals::new(edition);
44 GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals.rustc_span_globals, f))
47 pub fn with_default_globals<R>(f: impl FnOnce() -> R) -> R {
48 with_globals(DEFAULT_EDITION, f)
51 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
53 pub fn mark_used(attr: &Attribute) {
54 debug!("marking {:?} as used", attr);
55 GLOBALS.with(|globals| {
56 globals.used_attrs.lock().insert(attr.id);
60 pub fn is_used(attr: &Attribute) -> bool {
61 GLOBALS.with(|globals| globals.used_attrs.lock().contains(attr.id))
64 pub fn mark_known(attr: &Attribute) {
65 debug!("marking {:?} as known", attr);
66 GLOBALS.with(|globals| {
67 globals.known_attrs.lock().insert(attr.id);
71 pub fn is_known(attr: &Attribute) -> bool {
72 GLOBALS.with(|globals| globals.known_attrs.lock().contains(attr.id))
75 pub fn is_known_lint_tool(m_item: Ident) -> bool {
76 [sym::clippy, sym::rustc].contains(&m_item.name)
80 /// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
81 pub fn meta_item(&self) -> Option<&MetaItem> {
83 NestedMetaItem::MetaItem(ref item) => Some(item),
88 /// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
89 pub fn literal(&self) -> Option<&Lit> {
91 NestedMetaItem::Literal(ref lit) => Some(lit),
96 /// Returns `true` if this list item is a MetaItem with a name of `name`.
97 pub fn check_name(&self, name: Symbol) -> bool {
98 self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
101 /// For a single-segment meta item, returns its name; otherwise, returns `None`.
102 pub fn ident(&self) -> Option<Ident> {
103 self.meta_item().and_then(|meta_item| meta_item.ident())
105 pub fn name_or_empty(&self) -> Symbol {
106 self.ident().unwrap_or(Ident::invalid()).name
109 /// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a
110 /// `MetaItemKind::NameValue` variant containing a string, otherwise `None`.
111 pub fn value_str(&self) -> Option<Symbol> {
112 self.meta_item().and_then(|meta_item| meta_item.value_str())
115 /// Returns a name and single literal value tuple of the `MetaItem`.
116 pub fn name_value_literal(&self) -> Option<(Name, &Lit)> {
117 self.meta_item().and_then(|meta_item| {
118 meta_item.meta_item_list().and_then(|meta_item_list| {
119 if meta_item_list.len() == 1 {
120 if let Some(ident) = meta_item.ident() {
121 if let Some(lit) = meta_item_list[0].literal() {
122 return Some((ident.name, lit));
131 /// Gets a list of inner meta items from a list `MetaItem` type.
132 pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
133 self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
136 /// Returns `true` if the variant is `MetaItem`.
137 pub fn is_meta_item(&self) -> bool {
138 self.meta_item().is_some()
141 /// Returns `true` if the variant is `Literal`.
142 pub fn is_literal(&self) -> bool {
143 self.literal().is_some()
146 /// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
147 pub fn is_word(&self) -> bool {
148 self.meta_item().map_or(false, |meta_item| meta_item.is_word())
151 /// Returns `true` if `self` is a `MetaItem` and the meta item is a `ValueString`.
152 pub fn is_value_str(&self) -> bool {
153 self.value_str().is_some()
156 /// Returns `true` if `self` is a `MetaItem` and the meta item is a list.
157 pub fn is_meta_item_list(&self) -> bool {
158 self.meta_item_list().is_some()
163 pub fn has_name(&self, name: Symbol) -> bool {
165 AttrKind::Normal(ref item) => item.path == name,
166 AttrKind::DocComment(_) => false,
170 /// Returns `true` if the attribute's path matches the argument. If it matches, then the
171 /// attribute is marked as used.
172 pub fn check_name(&self, name: Symbol) -> bool {
173 let matches = self.has_name(name);
180 /// For a single-segment attribute, returns its name; otherwise, returns `None`.
181 pub fn ident(&self) -> Option<Ident> {
183 AttrKind::Normal(ref item) => {
184 if item.path.segments.len() == 1 {
185 Some(item.path.segments[0].ident)
190 AttrKind::DocComment(_) => None,
193 pub fn name_or_empty(&self) -> Symbol {
194 self.ident().unwrap_or(Ident::invalid()).name
197 pub fn value_str(&self) -> Option<Symbol> {
199 AttrKind::Normal(ref item) => item.meta(self.span).and_then(|meta| meta.value_str()),
200 AttrKind::DocComment(..) => None,
204 pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
206 AttrKind::Normal(ref item) => match item.meta(self.span) {
207 Some(MetaItem { kind: MetaItemKind::List(list), .. }) => Some(list),
210 AttrKind::DocComment(_) => None,
214 pub fn is_word(&self) -> bool {
215 if let AttrKind::Normal(item) = &self.kind {
216 matches!(item.args, MacArgs::Empty)
222 pub fn is_meta_item_list(&self) -> bool {
223 self.meta_item_list().is_some()
226 /// Indicates if the attribute is a `ValueString`.
227 pub fn is_value_str(&self) -> bool {
228 self.value_str().is_some()
233 /// For a single-segment meta item, returns its name; otherwise, returns `None`.
234 pub fn ident(&self) -> Option<Ident> {
235 if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None }
237 pub fn name_or_empty(&self) -> Symbol {
238 self.ident().unwrap_or(Ident::invalid()).name
242 // #[attribute(name = "value")]
244 pub fn name_value_literal(&self) -> Option<&Lit> {
246 MetaItemKind::NameValue(v) => Some(v),
251 pub fn value_str(&self) -> Option<Symbol> {
253 MetaItemKind::NameValue(ref v) => match v.kind {
254 LitKind::Str(ref s, _) => Some(*s),
261 pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
263 MetaItemKind::List(ref l) => Some(&l[..]),
268 pub fn is_word(&self) -> bool {
270 MetaItemKind::Word => true,
275 pub fn check_name(&self, name: Symbol) -> bool {
279 pub fn is_value_str(&self) -> bool {
280 self.value_str().is_some()
283 pub fn is_meta_item_list(&self) -> bool {
284 self.meta_item_list().is_some()
289 pub fn meta(&self, span: Span) -> Option<MetaItem> {
291 path: self.path.clone(),
292 kind: MetaItemKind::from_mac_args(&self.args)?,
299 pub fn is_doc_comment(&self) -> bool {
301 AttrKind::Normal(_) => false,
302 AttrKind::DocComment(_) => true,
306 pub fn doc_str(&self) -> Option<Symbol> {
308 AttrKind::DocComment(symbol) => Some(symbol),
309 AttrKind::Normal(ref item) if item.path == sym::doc => {
310 item.meta(self.span).and_then(|meta| meta.value_str())
316 pub fn get_normal_item(&self) -> &AttrItem {
318 AttrKind::Normal(ref item) => item,
319 AttrKind::DocComment(_) => panic!("unexpected doc comment"),
323 pub fn unwrap_normal_item(self) -> AttrItem {
325 AttrKind::Normal(item) => item,
326 AttrKind::DocComment(_) => panic!("unexpected doc comment"),
330 /// Extracts the MetaItem from inside this Attribute.
331 pub fn meta(&self) -> Option<MetaItem> {
333 AttrKind::Normal(ref item) => item.meta(self.span),
334 AttrKind::DocComment(..) => None,
341 pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
342 let lit_kind = LitKind::Str(str, ast::StrStyle::Cooked);
343 mk_name_value_item(ident, lit_kind, str_span)
346 pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
347 let lit = Lit::from_lit_kind(lit_kind, lit_span);
348 let span = ident.span.to(lit_span);
349 MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
352 pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
353 MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::List(items) }
356 pub fn mk_word_item(ident: Ident) -> MetaItem {
357 MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::Word }
360 pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
361 NestedMetaItem::MetaItem(mk_word_item(ident))
364 crate fn mk_attr_id() -> AttrId {
365 use std::sync::atomic::AtomicUsize;
366 use std::sync::atomic::Ordering;
368 static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0);
370 let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
371 assert!(id != ::std::usize::MAX);
375 pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {
376 mk_attr_from_item(style, AttrItem { path, args }, span)
379 pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attribute {
380 Attribute { kind: AttrKind::Normal(item), id: mk_attr_id(), style, span }
383 /// Returns an inner attribute with the given value and span.
384 pub fn mk_attr_inner(item: MetaItem) -> Attribute {
385 mk_attr(AttrStyle::Inner, item.path, item.kind.mac_args(item.span), item.span)
388 /// Returns an outer attribute with the given value and span.
389 pub fn mk_attr_outer(item: MetaItem) -> Attribute {
390 mk_attr(AttrStyle::Outer, item.path, item.kind.mac_args(item.span), item.span)
393 pub fn mk_doc_comment(style: AttrStyle, comment: Symbol, span: Span) -> Attribute {
394 Attribute { kind: AttrKind::DocComment(comment), id: mk_attr_id(), style, span }
397 pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
398 items.iter().any(|item| item.check_name(name))
401 pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
402 attrs.iter().any(|item| item.check_name(name))
405 pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
406 attrs.iter().find(|attr| attr.check_name(name))
409 pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
410 attrs.iter().filter(move |attr| attr.check_name(name))
413 pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
414 attrs.iter().find(|at| at.check_name(name)).and_then(|at| at.value_str())
418 fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
419 let mut idents = vec![];
420 let mut last_pos = BytePos(0 as u32);
421 for (i, segment) in self.path.segments.iter().enumerate() {
422 let is_first = i == 0;
425 Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt());
426 idents.push(TokenTree::token(token::ModSep, mod_sep_span).into());
428 idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
429 last_pos = segment.ident.span.hi();
431 idents.extend(self.kind.token_trees_and_joints(self.span));
435 fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
437 I: Iterator<Item = TokenTree>,
439 // FIXME: Share code with `parse_path`.
440 let path = match tokens.next() {
441 Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span }))
442 | Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: {
443 let mut segments = if let token::Ident(name, _) = kind {
444 if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek()
447 vec![PathSegment::from_ident(Ident::new(name, span))]
449 break 'arm Path::from_ident(Ident::new(name, span));
452 vec![PathSegment::path_root(span)]
455 if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) =
458 segments.push(PathSegment::from_ident(Ident::new(name, span)));
462 if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek()
469 let span = span.with_hi(segments.last().unwrap().ident.span.hi());
470 Path { span, segments }
472 Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
473 token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident),
474 token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
475 token::Nonterminal::NtPath(ref path) => path.clone(),
480 let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
481 let kind = MetaItemKind::from_tokens(tokens)?;
482 let hi = match kind {
483 MetaItemKind::NameValue(ref lit) => lit.span.hi(),
484 MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
487 let span = path.span.with_hi(hi);
488 Some(MetaItem { path, kind, span })
493 pub fn mac_args(&self, span: Span) -> MacArgs {
495 MetaItemKind::Word => MacArgs::Empty,
496 MetaItemKind::NameValue(lit) => MacArgs::Eq(span, lit.token_tree().into()),
497 MetaItemKind::List(list) => {
498 let mut tts = Vec::new();
499 for (i, item) in list.iter().enumerate() {
501 tts.push(TokenTree::token(token::Comma, span).into());
503 tts.extend(item.token_trees_and_joints())
506 DelimSpan::from_single(span),
507 MacDelimiter::Parenthesis,
508 TokenStream::new(tts),
514 fn token_trees_and_joints(&self, span: Span) -> Vec<TreeAndJoint> {
516 MetaItemKind::Word => vec![],
517 MetaItemKind::NameValue(ref lit) => {
518 vec![TokenTree::token(token::Eq, span).into(), lit.token_tree().into()]
520 MetaItemKind::List(ref list) => {
521 let mut tokens = Vec::new();
522 for (i, item) in list.iter().enumerate() {
524 tokens.push(TokenTree::token(token::Comma, span).into());
526 tokens.extend(item.token_trees_and_joints())
529 TokenTree::Delimited(
530 DelimSpan::from_single(span),
532 TokenStream::new(tokens),
540 fn list_from_tokens(tokens: TokenStream) -> Option<MetaItemKind> {
541 let mut tokens = tokens.into_trees().peekable();
542 let mut result = Vec::new();
543 while let Some(..) = tokens.peek() {
544 let item = NestedMetaItem::from_tokens(&mut tokens)?;
546 match tokens.next() {
547 None | Some(TokenTree::Token(Token { kind: token::Comma, .. })) => {}
551 Some(MetaItemKind::List(result))
554 fn name_value_from_tokens(
555 tokens: &mut impl Iterator<Item = TokenTree>,
556 ) -> Option<MetaItemKind> {
557 match tokens.next() {
558 Some(TokenTree::Token(token)) => {
559 Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
565 fn from_mac_args(args: &MacArgs) -> Option<MetaItemKind> {
567 MacArgs::Delimited(_, MacDelimiter::Parenthesis, tokens) => {
568 MetaItemKind::list_from_tokens(tokens.clone())
570 MacArgs::Delimited(..) => None,
571 MacArgs::Eq(_, tokens) => {
572 assert!(tokens.len() == 1);
573 MetaItemKind::name_value_from_tokens(&mut tokens.trees())
575 MacArgs::Empty => Some(MetaItemKind::Word),
580 tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
581 ) -> Option<MetaItemKind> {
582 match tokens.peek() {
583 Some(TokenTree::Delimited(_, token::Paren, inner_tokens)) => {
584 let inner_tokens = inner_tokens.clone();
586 MetaItemKind::list_from_tokens(inner_tokens)
588 Some(TokenTree::Delimited(..)) => None,
589 Some(TokenTree::Token(Token { kind: token::Eq, .. })) => {
591 MetaItemKind::name_value_from_tokens(tokens)
593 _ => Some(MetaItemKind::Word),
598 impl NestedMetaItem {
599 pub fn span(&self) -> Span {
601 NestedMetaItem::MetaItem(ref item) => item.span,
602 NestedMetaItem::Literal(ref lit) => lit.span,
606 fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
608 NestedMetaItem::MetaItem(ref item) => item.token_trees_and_joints(),
609 NestedMetaItem::Literal(ref lit) => vec![lit.token_tree().into()],
613 fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem>
615 I: Iterator<Item = TokenTree>,
617 if let Some(TokenTree::Token(token)) = tokens.peek() {
618 if let Ok(lit) = Lit::from_token(token) {
620 return Some(NestedMetaItem::Literal(lit));
624 MetaItem::from_tokens(tokens).map(NestedMetaItem::MetaItem)
628 pub trait HasAttrs: Sized {
629 fn attrs(&self) -> &[Attribute];
630 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>));
633 impl<T: HasAttrs> HasAttrs for Spanned<T> {
634 fn attrs(&self) -> &[Attribute] {
637 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
638 self.node.visit_attrs(f);
642 impl HasAttrs for Vec<Attribute> {
643 fn attrs(&self) -> &[Attribute] {
646 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
651 impl HasAttrs for AttrVec {
652 fn attrs(&self) -> &[Attribute] {
655 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
656 visit_clobber(self, |this| {
657 let mut vec = this.into();
664 impl<T: HasAttrs + 'static> HasAttrs for P<T> {
665 fn attrs(&self) -> &[Attribute] {
668 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
669 (**self).visit_attrs(f);
673 impl HasAttrs for StmtKind {
674 fn attrs(&self) -> &[Attribute] {
676 StmtKind::Local(ref local) => local.attrs(),
677 StmtKind::Item(..) => &[],
678 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
679 StmtKind::Mac(ref mac) => {
680 let (_, _, ref attrs) = **mac;
686 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
688 StmtKind::Local(local) => local.visit_attrs(f),
689 StmtKind::Item(..) => {}
690 StmtKind::Expr(expr) => expr.visit_attrs(f),
691 StmtKind::Semi(expr) => expr.visit_attrs(f),
692 StmtKind::Mac(mac) => {
693 let (_mac, _style, attrs) = mac.deref_mut();
694 attrs.visit_attrs(f);
700 impl HasAttrs for Stmt {
701 fn attrs(&self) -> &[ast::Attribute] {
705 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
706 self.kind.visit_attrs(f);
710 macro_rules! derive_has_attrs {
711 ($($ty:path),*) => { $(
712 impl HasAttrs for $ty {
713 fn attrs(&self) -> &[Attribute] {
717 fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
718 self.attrs.visit_attrs(f);
725 Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::Arm,
726 ast::Field, ast::FieldPat, ast::Variant, ast::Param, GenericParam