1 //! Functions dealing with attributes and meta items.
4 use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
5 use crate::ast::{DelimArgs, Lit, LitKind};
6 use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
7 use crate::ast::{Path, PathSegment};
9 use crate::token::{self, CommentKind, Delimiter, Token};
10 use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
11 use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
12 use crate::util::comments;
13 use rustc_data_structures::sync::WorkerLocal;
14 use rustc_index::bit_set::GrowableBitSet;
15 use rustc_span::source_map::BytePos;
16 use rustc_span::symbol::{sym, Ident, Symbol};
20 #[cfg(debug_assertions)]
22 #[cfg(debug_assertions)]
23 use std::sync::atomic::{AtomicU32, Ordering};
24 use thin_vec::thin_vec;
26 pub struct MarkedAttrs(GrowableBitSet<AttrId>);
29 pub fn new() -> Self {
30 // We have no idea how many attributes there will be, so just
31 // initiate the vectors with 0 bits. We'll grow them as necessary.
32 MarkedAttrs(GrowableBitSet::new_empty())
35 pub fn mark(&mut self, attr: &Attribute) {
36 self.0.insert(attr.id);
39 pub fn is_marked(&self, attr: &Attribute) -> bool {
40 self.0.contains(attr.id)
45 /// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
46 pub fn meta_item(&self) -> Option<&MetaItem> {
48 NestedMetaItem::MetaItem(item) => Some(item),
53 /// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
54 pub fn literal(&self) -> Option<&Lit> {
56 NestedMetaItem::Literal(lit) => Some(lit),
61 /// Returns `true` if this list item is a MetaItem with a name of `name`.
62 pub fn has_name(&self, name: Symbol) -> bool {
63 self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
66 /// For a single-segment meta item, returns its name; otherwise, returns `None`.
67 pub fn ident(&self) -> Option<Ident> {
68 self.meta_item().and_then(|meta_item| meta_item.ident())
70 pub fn name_or_empty(&self) -> Symbol {
71 self.ident().unwrap_or_else(Ident::empty).name
74 /// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a
75 /// `MetaItemKind::NameValue` variant containing a string, otherwise `None`.
76 pub fn value_str(&self) -> Option<Symbol> {
77 self.meta_item().and_then(|meta_item| meta_item.value_str())
80 /// Returns a name and single literal value tuple of the `MetaItem`.
81 pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> {
82 self.meta_item().and_then(|meta_item| {
83 meta_item.meta_item_list().and_then(|meta_item_list| {
84 if meta_item_list.len() == 1
85 && let Some(ident) = meta_item.ident()
86 && let Some(lit) = meta_item_list[0].literal()
88 return Some((ident.name, lit));
95 /// Gets a list of inner meta items from a list `MetaItem` type.
96 pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
97 self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
100 /// Returns `true` if the variant is `MetaItem`.
101 pub fn is_meta_item(&self) -> bool {
102 self.meta_item().is_some()
105 /// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
106 pub fn is_word(&self) -> bool {
107 self.meta_item().map_or(false, |meta_item| meta_item.is_word())
110 /// See [`MetaItem::name_value_literal_span`].
111 pub fn name_value_literal_span(&self) -> Option<Span> {
112 self.meta_item()?.name_value_literal_span()
118 pub fn has_name(&self, name: Symbol) -> bool {
120 AttrKind::Normal(normal) => normal.item.path == name,
121 AttrKind::DocComment(..) => false,
125 /// For a single-segment attribute, returns its name; otherwise, returns `None`.
126 pub fn ident(&self) -> Option<Ident> {
128 AttrKind::Normal(normal) => {
129 if let [ident] = &*normal.item.path.segments {
135 AttrKind::DocComment(..) => None,
138 pub fn name_or_empty(&self) -> Symbol {
139 self.ident().unwrap_or_else(Ident::empty).name
142 pub fn value_str(&self) -> Option<Symbol> {
144 AttrKind::Normal(normal) => normal.item.meta_kind().and_then(|kind| kind.value_str()),
145 AttrKind::DocComment(..) => None,
149 pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
151 AttrKind::Normal(normal) => match normal.item.meta_kind() {
152 Some(MetaItemKind::List(list)) => Some(list),
155 AttrKind::DocComment(..) => None,
159 pub fn is_word(&self) -> bool {
160 if let AttrKind::Normal(normal) = &self.kind {
161 matches!(normal.item.args, AttrArgs::Empty)
169 /// For a single-segment meta item, returns its name; otherwise, returns `None`.
170 pub fn ident(&self) -> Option<Ident> {
171 if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None }
173 pub fn name_or_empty(&self) -> Symbol {
174 self.ident().unwrap_or_else(Ident::empty).name
179 /// #[attribute(name = "value")]
182 pub fn name_value_literal(&self) -> Option<&Lit> {
184 MetaItemKind::NameValue(v) => Some(v),
189 pub fn value_str(&self) -> Option<Symbol> {
190 self.kind.value_str()
193 pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
195 MetaItemKind::List(l) => Some(&**l),
200 pub fn is_word(&self) -> bool {
201 matches!(self.kind, MetaItemKind::Word)
204 pub fn has_name(&self, name: Symbol) -> bool {
208 /// This is used in case you want the value span instead of the whole attribute. Example:
211 /// #[doc(alias = "foo")]
214 /// In here, it'll return a span for `"foo"`.
215 pub fn name_value_literal_span(&self) -> Option<Span> {
216 Some(self.name_value_literal()?.span)
221 pub fn span(&self) -> Span {
222 self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
225 pub fn meta(&self, span: Span) -> Option<MetaItem> {
227 path: self.path.clone(),
228 kind: MetaItemKind::from_attr_args(&self.args)?,
233 pub fn meta_kind(&self) -> Option<MetaItemKind> {
234 MetaItemKind::from_attr_args(&self.args)
239 /// Returns `true` if it is a sugared doc comment (`///` or `//!` for example).
240 /// So `#[doc = "doc"]` (which is a doc comment) and `#[doc(...)]` (which is not
241 /// a doc comment) will return `false`.
242 pub fn is_doc_comment(&self) -> bool {
244 AttrKind::Normal(..) => false,
245 AttrKind::DocComment(..) => true,
249 /// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
250 /// * `///doc` returns `Some(("doc", CommentKind::Line))`.
251 /// * `/** doc */` returns `Some(("doc", CommentKind::Block))`.
252 /// * `#[doc = "doc"]` returns `Some(("doc", CommentKind::Line))`.
253 /// * `#[doc(...)]` returns `None`.
254 pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
256 AttrKind::DocComment(kind, data) => Some((data, kind)),
257 AttrKind::Normal(ref normal) if normal.item.path == sym::doc => normal
260 .and_then(|kind| kind.value_str())
261 .map(|data| (data, CommentKind::Line)),
266 /// Returns the documentation if this is a doc comment or a sugared doc comment.
267 /// * `///doc` returns `Some("doc")`.
268 /// * `#[doc = "doc"]` returns `Some("doc")`.
269 /// * `#[doc(...)]` returns `None`.
270 pub fn doc_str(&self) -> Option<Symbol> {
272 AttrKind::DocComment(.., data) => Some(*data),
273 AttrKind::Normal(normal) if normal.item.path == sym::doc => {
274 normal.item.meta_kind().and_then(|kind| kind.value_str())
280 pub fn may_have_doc_links(&self) -> bool {
281 self.doc_str().map_or(false, |s| comments::may_have_doc_links(s.as_str()))
284 pub fn get_normal_item(&self) -> &AttrItem {
286 AttrKind::Normal(normal) => &normal.item,
287 AttrKind::DocComment(..) => panic!("unexpected doc comment"),
291 pub fn unwrap_normal_item(self) -> AttrItem {
293 AttrKind::Normal(normal) => normal.into_inner().item,
294 AttrKind::DocComment(..) => panic!("unexpected doc comment"),
298 /// Extracts the MetaItem from inside this Attribute.
299 pub fn meta(&self) -> Option<MetaItem> {
301 AttrKind::Normal(normal) => normal.item.meta(self.span),
302 AttrKind::DocComment(..) => None,
306 pub fn meta_kind(&self) -> Option<MetaItemKind> {
308 AttrKind::Normal(normal) => normal.item.meta_kind(),
309 AttrKind::DocComment(..) => None,
313 pub fn tokens(&self) -> TokenStream {
315 AttrKind::Normal(normal) => normal
318 .unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
319 .to_attr_token_stream()
321 &AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token(
322 Token::new(token::DocComment(comment_kind, self.style, data), self.span),
331 pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
332 let lit_kind = LitKind::Str(str, ast::StrStyle::Cooked);
333 mk_name_value_item(ident, lit_kind, str_span)
336 pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
337 let lit = Lit::from_lit_kind(lit_kind, lit_span);
338 let span = ident.span.to(lit_span);
339 MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
342 pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
343 MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::List(items) }
346 pub fn mk_word_item(ident: Ident) -> MetaItem {
347 MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::Word }
350 pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
351 NestedMetaItem::MetaItem(mk_word_item(ident))
354 pub struct AttrIdGenerator(WorkerLocal<Cell<u32>>);
356 #[cfg(debug_assertions)]
357 static MAX_ATTR_ID: AtomicU32 = AtomicU32::new(u32::MAX);
359 impl AttrIdGenerator {
360 pub fn new() -> Self {
361 // We use `(index as u32).reverse_bits()` to initialize the
362 // starting value of AttrId in each worker thread.
363 // The `index` is the index of the worker thread.
364 // This ensures that the AttrId generated in each thread is unique.
365 AttrIdGenerator(WorkerLocal::new(|index| {
366 let index: u32 = index.try_into().unwrap();
368 #[cfg(debug_assertions)]
370 let max_id = ((index + 1).next_power_of_two() - 1).bitxor(u32::MAX).reverse_bits();
371 MAX_ATTR_ID.fetch_min(max_id, Ordering::Release);
374 Cell::new(index.reverse_bits())
378 pub fn mk_attr_id(&self) -> AttrId {
379 let id = self.0.get();
381 // Ensure the assigned attr_id does not overlap the bits
382 // representing the number of threads.
383 #[cfg(debug_assertions)]
384 assert!(id <= MAX_ATTR_ID.load(Ordering::Acquire));
398 mk_attr_from_item(g, AttrItem { path, args, tokens: None }, None, style, span)
401 pub fn mk_attr_from_item(
404 tokens: Option<LazyAttrTokenStream>,
409 kind: AttrKind::Normal(P(ast::NormalAttr { item, tokens })),
416 /// Returns an inner attribute with the given value and span.
417 pub fn mk_attr_inner(g: &AttrIdGenerator, item: MetaItem) -> Attribute {
418 mk_attr(g, AttrStyle::Inner, item.path, item.kind.attr_args(item.span), item.span)
421 /// Returns an outer attribute with the given value and span.
422 pub fn mk_attr_outer(g: &AttrIdGenerator, item: MetaItem) -> Attribute {
423 mk_attr(g, AttrStyle::Outer, item.path, item.kind.attr_args(item.span), item.span)
426 pub fn mk_doc_comment(
428 comment_kind: CommentKind,
433 Attribute { kind: AttrKind::DocComment(comment_kind, data), id: g.mk_attr_id(), style, span }
436 pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
437 items.iter().any(|item| item.has_name(name))
441 fn token_trees(&self) -> Vec<TokenTree> {
442 let mut idents = vec![];
443 let mut last_pos = BytePos(0_u32);
444 for (i, segment) in self.path.segments.iter().enumerate() {
445 let is_first = i == 0;
448 Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt(), None);
449 idents.push(TokenTree::token_alone(token::ModSep, mod_sep_span));
451 idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident), Spacing::Alone));
452 last_pos = segment.ident.span.hi();
454 idents.extend(self.kind.token_trees(self.span));
458 fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
460 I: Iterator<Item = TokenTree>,
462 // FIXME: Share code with `parse_path`.
463 let path = match tokens.next().map(TokenTree::uninterpolate) {
464 Some(TokenTree::Token(
465 Token { kind: kind @ (token::Ident(..) | token::ModSep), span },
468 let mut segments = if let token::Ident(name, _) = kind {
469 if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }, _)) =
473 thin_vec![PathSegment::from_ident(Ident::new(name, span))]
475 break 'arm Path::from_ident(Ident::new(name, span));
478 thin_vec![PathSegment::path_root(span)]
481 if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
482 tokens.next().map(TokenTree::uninterpolate)
484 segments.push(PathSegment::from_ident(Ident::new(name, span)));
488 if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }, _)) =
496 let span = span.with_hi(segments.last().unwrap().ident.span.hi());
497 Path { span, segments, tokens: None }
499 Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &*nt {
500 token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
501 token::Nonterminal::NtPath(path) => (**path).clone(),
506 let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
507 let kind = MetaItemKind::from_tokens(tokens)?;
508 let hi = match &kind {
509 MetaItemKind::NameValue(lit) => lit.span.hi(),
510 MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
513 let span = path.span.with_hi(hi);
514 Some(MetaItem { path, kind, span })
519 pub fn value_str(&self) -> Option<Symbol> {
521 MetaItemKind::NameValue(v) => match v.kind {
522 LitKind::Str(s, _) => Some(s),
529 pub fn attr_args(&self, span: Span) -> AttrArgs {
531 MetaItemKind::Word => AttrArgs::Empty,
532 MetaItemKind::NameValue(lit) => {
533 let expr = P(ast::Expr {
534 id: ast::DUMMY_NODE_ID,
535 kind: ast::ExprKind::Lit(lit.token_lit.clone()),
537 attrs: ast::AttrVec::new(),
540 AttrArgs::Eq(span, AttrArgsEq::Ast(expr))
542 MetaItemKind::List(list) => {
543 let mut tts = Vec::new();
544 for (i, item) in list.iter().enumerate() {
546 tts.push(TokenTree::token_alone(token::Comma, span));
548 tts.extend(item.token_trees())
550 AttrArgs::Delimited(DelimArgs {
551 dspan: DelimSpan::from_single(span),
552 delim: MacDelimiter::Parenthesis,
553 tokens: TokenStream::new(tts),
559 fn token_trees(&self, span: Span) -> Vec<TokenTree> {
561 MetaItemKind::Word => vec![],
562 MetaItemKind::NameValue(lit) => {
564 TokenTree::token_alone(token::Eq, span),
565 TokenTree::Token(lit.to_token(), Spacing::Alone),
568 MetaItemKind::List(list) => {
569 let mut tokens = Vec::new();
570 for (i, item) in list.iter().enumerate() {
572 tokens.push(TokenTree::token_alone(token::Comma, span));
574 tokens.extend(item.token_trees())
576 vec![TokenTree::Delimited(
577 DelimSpan::from_single(span),
578 Delimiter::Parenthesis,
579 TokenStream::new(tokens),
585 fn list_from_tokens(tokens: TokenStream) -> Option<MetaItemKind> {
586 let mut tokens = tokens.into_trees().peekable();
587 let mut result = Vec::new();
588 while tokens.peek().is_some() {
589 let item = NestedMetaItem::from_tokens(&mut tokens)?;
591 match tokens.next() {
592 None | Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) => {}
596 Some(MetaItemKind::List(result))
599 fn name_value_from_tokens(
600 tokens: &mut impl Iterator<Item = TokenTree>,
601 ) -> Option<MetaItemKind> {
602 match tokens.next() {
603 Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
604 MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
606 Some(TokenTree::Token(token, _)) => {
607 Lit::from_token(&token).map(MetaItemKind::NameValue)
613 fn from_attr_args(args: &AttrArgs) -> Option<MetaItemKind> {
615 AttrArgs::Empty => Some(MetaItemKind::Word),
616 AttrArgs::Delimited(DelimArgs {
618 delim: MacDelimiter::Parenthesis,
620 }) => MetaItemKind::list_from_tokens(tokens.clone()),
621 AttrArgs::Delimited(..) => None,
622 AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
623 ast::ExprKind::Lit(token_lit) => {
624 // Turn failures to `None`, we'll get parse errors elsewhere.
625 Lit::from_token_lit(token_lit, expr.span)
627 .map(|lit| MetaItemKind::NameValue(lit))
631 AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
636 tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
637 ) -> Option<MetaItemKind> {
638 match tokens.peek() {
639 Some(TokenTree::Delimited(_, Delimiter::Parenthesis, inner_tokens)) => {
640 let inner_tokens = inner_tokens.clone();
642 MetaItemKind::list_from_tokens(inner_tokens)
644 Some(TokenTree::Delimited(..)) => None,
645 Some(TokenTree::Token(Token { kind: token::Eq, .. }, _)) => {
647 MetaItemKind::name_value_from_tokens(tokens)
649 _ => Some(MetaItemKind::Word),
654 impl NestedMetaItem {
655 pub fn span(&self) -> Span {
657 NestedMetaItem::MetaItem(item) => item.span,
658 NestedMetaItem::Literal(lit) => lit.span,
662 fn token_trees(&self) -> Vec<TokenTree> {
664 NestedMetaItem::MetaItem(item) => item.token_trees(),
665 NestedMetaItem::Literal(lit) => {
666 vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
671 fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem>
673 I: Iterator<Item = TokenTree>,
675 match tokens.peek() {
676 Some(TokenTree::Token(token, _))
677 if let Some(lit) = Lit::from_token(token) =>
680 return Some(NestedMetaItem::Literal(lit));
682 Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
683 let inner_tokens = inner_tokens.clone();
685 return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());
689 MetaItem::from_tokens(tokens).map(NestedMetaItem::MetaItem)