]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/attr/mod.rs
Auto merge of #83599 - jyn514:unorderable, r=Aaron1011
[rust.git] / compiler / rustc_ast / src / attr / mod.rs
1 //! Functions dealing with attributes and meta items.
2
3 use crate::ast;
4 use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
5 use crate::ast::{Lit, LitKind};
6 use crate::ast::{MacArgs, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
7 use crate::ast::{Path, PathSegment};
8 use crate::token::{self, CommentKind, Token};
9 use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream, TokenTree, TreeAndSpacing};
10
11 use rustc_index::bit_set::GrowableBitSet;
12 use rustc_span::source_map::BytePos;
13 use rustc_span::symbol::{sym, Ident, Symbol};
14 use rustc_span::Span;
15
16 use std::iter;
17
18 pub struct MarkedAttrs(GrowableBitSet<AttrId>);
19
20 impl MarkedAttrs {
21     // We have no idea how many attributes there will be, so just
22     // initiate the vectors with 0 bits. We'll grow them as necessary.
23     pub fn new() -> Self {
24         MarkedAttrs(GrowableBitSet::new_empty())
25     }
26
27     pub fn mark(&mut self, attr: &Attribute) {
28         self.0.insert(attr.id);
29     }
30
31     pub fn is_marked(&self, attr: &Attribute) -> bool {
32         self.0.contains(attr.id)
33     }
34 }
35
36 impl NestedMetaItem {
37     /// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
38     pub fn meta_item(&self) -> Option<&MetaItem> {
39         match *self {
40             NestedMetaItem::MetaItem(ref item) => Some(item),
41             _ => None,
42         }
43     }
44
45     /// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
46     pub fn literal(&self) -> Option<&Lit> {
47         match *self {
48             NestedMetaItem::Literal(ref lit) => Some(lit),
49             _ => None,
50         }
51     }
52
53     /// Returns `true` if this list item is a MetaItem with a name of `name`.
54     pub fn has_name(&self, name: Symbol) -> bool {
55         self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
56     }
57
58     /// For a single-segment meta item, returns its name; otherwise, returns `None`.
59     pub fn ident(&self) -> Option<Ident> {
60         self.meta_item().and_then(|meta_item| meta_item.ident())
61     }
62     pub fn name_or_empty(&self) -> Symbol {
63         self.ident().unwrap_or_else(Ident::invalid).name
64     }
65
66     /// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a
67     /// `MetaItemKind::NameValue` variant containing a string, otherwise `None`.
68     pub fn value_str(&self) -> Option<Symbol> {
69         self.meta_item().and_then(|meta_item| meta_item.value_str())
70     }
71
72     /// Returns a name and single literal value tuple of the `MetaItem`.
73     pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> {
74         self.meta_item().and_then(|meta_item| {
75             meta_item.meta_item_list().and_then(|meta_item_list| {
76                 if meta_item_list.len() == 1 {
77                     if let Some(ident) = meta_item.ident() {
78                         if let Some(lit) = meta_item_list[0].literal() {
79                             return Some((ident.name, lit));
80                         }
81                     }
82                 }
83                 None
84             })
85         })
86     }
87
88     /// Gets a list of inner meta items from a list `MetaItem` type.
89     pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
90         self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
91     }
92
93     /// Returns `true` if the variant is `MetaItem`.
94     pub fn is_meta_item(&self) -> bool {
95         self.meta_item().is_some()
96     }
97
98     /// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
99     pub fn is_word(&self) -> bool {
100         self.meta_item().map_or(false, |meta_item| meta_item.is_word())
101     }
102
103     /// See [`MetaItem::name_value_literal_span`].
104     pub fn name_value_literal_span(&self) -> Option<Span> {
105         self.meta_item()?.name_value_literal_span()
106     }
107 }
108
109 impl Attribute {
110     #[inline]
111     pub fn has_name(&self, name: Symbol) -> bool {
112         match self.kind {
113             AttrKind::Normal(ref item, _) => item.path == name,
114             AttrKind::DocComment(..) => false,
115         }
116     }
117
118     /// For a single-segment attribute, returns its name; otherwise, returns `None`.
119     pub fn ident(&self) -> Option<Ident> {
120         match self.kind {
121             AttrKind::Normal(ref item, _) => {
122                 if item.path.segments.len() == 1 {
123                     Some(item.path.segments[0].ident)
124                 } else {
125                     None
126                 }
127             }
128             AttrKind::DocComment(..) => None,
129         }
130     }
131     pub fn name_or_empty(&self) -> Symbol {
132         self.ident().unwrap_or_else(Ident::invalid).name
133     }
134
135     pub fn value_str(&self) -> Option<Symbol> {
136         match self.kind {
137             AttrKind::Normal(ref item, _) => item.meta(self.span).and_then(|meta| meta.value_str()),
138             AttrKind::DocComment(..) => None,
139         }
140     }
141
142     pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
143         match self.kind {
144             AttrKind::Normal(ref item, _) => match item.meta(self.span) {
145                 Some(MetaItem { kind: MetaItemKind::List(list), .. }) => Some(list),
146                 _ => None,
147             },
148             AttrKind::DocComment(..) => None,
149         }
150     }
151
152     pub fn is_word(&self) -> bool {
153         if let AttrKind::Normal(item, _) = &self.kind {
154             matches!(item.args, MacArgs::Empty)
155         } else {
156             false
157         }
158     }
159 }
160
161 impl MetaItem {
162     /// For a single-segment meta item, returns its name; otherwise, returns `None`.
163     pub fn ident(&self) -> Option<Ident> {
164         if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None }
165     }
166     pub fn name_or_empty(&self) -> Symbol {
167         self.ident().unwrap_or_else(Ident::invalid).name
168     }
169
170     // Example:
171     //     #[attribute(name = "value")]
172     //                 ^^^^^^^^^^^^^^
173     pub fn name_value_literal(&self) -> Option<&Lit> {
174         match &self.kind {
175             MetaItemKind::NameValue(v) => Some(v),
176             _ => None,
177         }
178     }
179
180     pub fn value_str(&self) -> Option<Symbol> {
181         match self.kind {
182             MetaItemKind::NameValue(ref v) => match v.kind {
183                 LitKind::Str(ref s, _) => Some(*s),
184                 _ => None,
185             },
186             _ => None,
187         }
188     }
189
190     pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
191         match self.kind {
192             MetaItemKind::List(ref l) => Some(&l[..]),
193             _ => None,
194         }
195     }
196
197     pub fn is_word(&self) -> bool {
198         matches!(self.kind, MetaItemKind::Word)
199     }
200
201     pub fn has_name(&self, name: Symbol) -> bool {
202         self.path == name
203     }
204
205     /// This is used in case you want the value span instead of the whole attribute. Example:
206     ///
207     /// ```text
208     /// #[doc(alias = "foo")]
209     /// ```
210     ///
211     /// In here, it'll return a span for `"foo"`.
212     pub fn name_value_literal_span(&self) -> Option<Span> {
213         Some(self.name_value_literal()?.span)
214     }
215 }
216
217 impl AttrItem {
218     pub fn span(&self) -> Span {
219         self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
220     }
221
222     pub fn meta(&self, span: Span) -> Option<MetaItem> {
223         Some(MetaItem {
224             path: self.path.clone(),
225             kind: MetaItemKind::from_mac_args(&self.args)?,
226             span,
227         })
228     }
229 }
230
231 impl Attribute {
232     pub fn is_doc_comment(&self) -> bool {
233         match self.kind {
234             AttrKind::Normal(..) => false,
235             AttrKind::DocComment(..) => true,
236         }
237     }
238
239     pub fn doc_str(&self) -> Option<Symbol> {
240         match self.kind {
241             AttrKind::DocComment(.., data) => Some(data),
242             AttrKind::Normal(ref item, _) if item.path == sym::doc => {
243                 item.meta(self.span).and_then(|meta| meta.value_str())
244             }
245             _ => None,
246         }
247     }
248
249     pub fn get_normal_item(&self) -> &AttrItem {
250         match self.kind {
251             AttrKind::Normal(ref item, _) => item,
252             AttrKind::DocComment(..) => panic!("unexpected doc comment"),
253         }
254     }
255
256     pub fn unwrap_normal_item(self) -> AttrItem {
257         match self.kind {
258             AttrKind::Normal(item, _) => item,
259             AttrKind::DocComment(..) => panic!("unexpected doc comment"),
260         }
261     }
262
263     /// Extracts the MetaItem from inside this Attribute.
264     pub fn meta(&self) -> Option<MetaItem> {
265         match self.kind {
266             AttrKind::Normal(ref item, _) => item.meta(self.span),
267             AttrKind::DocComment(..) => None,
268         }
269     }
270
271     pub fn tokens(&self) -> TokenStream {
272         match self.kind {
273             AttrKind::Normal(_, ref tokens) => tokens
274                 .as_ref()
275                 .unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
276                 .create_token_stream(),
277             AttrKind::DocComment(comment_kind, data) => TokenStream::from(TokenTree::Token(
278                 Token::new(token::DocComment(comment_kind, self.style, data), self.span),
279             )),
280         }
281     }
282 }
283
284 /* Constructors */
285
286 pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
287     let lit_kind = LitKind::Str(str, ast::StrStyle::Cooked);
288     mk_name_value_item(ident, lit_kind, str_span)
289 }
290
291 pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
292     let lit = Lit::from_lit_kind(lit_kind, lit_span);
293     let span = ident.span.to(lit_span);
294     MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
295 }
296
297 pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
298     MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::List(items) }
299 }
300
301 pub fn mk_word_item(ident: Ident) -> MetaItem {
302     MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::Word }
303 }
304
305 pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
306     NestedMetaItem::MetaItem(mk_word_item(ident))
307 }
308
309 crate fn mk_attr_id() -> AttrId {
310     use std::sync::atomic::AtomicU32;
311     use std::sync::atomic::Ordering;
312
313     static NEXT_ATTR_ID: AtomicU32 = AtomicU32::new(0);
314
315     let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
316     assert!(id != u32::MAX);
317     AttrId::from_u32(id)
318 }
319
320 pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {
321     mk_attr_from_item(AttrItem { path, args, tokens: None }, None, style, span)
322 }
323
324 pub fn mk_attr_from_item(
325     item: AttrItem,
326     tokens: Option<LazyTokenStream>,
327     style: AttrStyle,
328     span: Span,
329 ) -> Attribute {
330     Attribute { kind: AttrKind::Normal(item, tokens), id: mk_attr_id(), style, span }
331 }
332
333 /// Returns an inner attribute with the given value and span.
334 pub fn mk_attr_inner(item: MetaItem) -> Attribute {
335     mk_attr(AttrStyle::Inner, item.path, item.kind.mac_args(item.span), item.span)
336 }
337
338 /// Returns an outer attribute with the given value and span.
339 pub fn mk_attr_outer(item: MetaItem) -> Attribute {
340     mk_attr(AttrStyle::Outer, item.path, item.kind.mac_args(item.span), item.span)
341 }
342
343 pub fn mk_doc_comment(
344     comment_kind: CommentKind,
345     style: AttrStyle,
346     data: Symbol,
347     span: Span,
348 ) -> Attribute {
349     Attribute { kind: AttrKind::DocComment(comment_kind, data), id: mk_attr_id(), style, span }
350 }
351
352 pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
353     items.iter().any(|item| item.has_name(name))
354 }
355
356 impl MetaItem {
357     fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
358         let mut idents = vec![];
359         let mut last_pos = BytePos(0_u32);
360         for (i, segment) in self.path.segments.iter().enumerate() {
361             let is_first = i == 0;
362             if !is_first {
363                 let mod_sep_span =
364                     Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt());
365                 idents.push(TokenTree::token(token::ModSep, mod_sep_span).into());
366             }
367             idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
368             last_pos = segment.ident.span.hi();
369         }
370         idents.extend(self.kind.token_trees_and_spacings(self.span));
371         idents
372     }
373
374     fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
375     where
376         I: Iterator<Item = TokenTree>,
377     {
378         // FIXME: Share code with `parse_path`.
379         let path = match tokens.next().map(TokenTree::uninterpolate) {
380             Some(TokenTree::Token(Token {
381                 kind: kind @ (token::Ident(..) | token::ModSep),
382                 span,
383             })) => 'arm: {
384                 let mut segments = if let token::Ident(name, _) = kind {
385                     if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek()
386                     {
387                         tokens.next();
388                         vec![PathSegment::from_ident(Ident::new(name, span))]
389                     } else {
390                         break 'arm Path::from_ident(Ident::new(name, span));
391                     }
392                 } else {
393                     vec![PathSegment::path_root(span)]
394                 };
395                 loop {
396                     if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) =
397                         tokens.next().map(TokenTree::uninterpolate)
398                     {
399                         segments.push(PathSegment::from_ident(Ident::new(name, span)));
400                     } else {
401                         return None;
402                     }
403                     if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek()
404                     {
405                         tokens.next();
406                     } else {
407                         break;
408                     }
409                 }
410                 let span = span.with_hi(segments.last().unwrap().ident.span.hi());
411                 Path { span, segments, tokens: None }
412             }
413             Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
414                 token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
415                 token::Nonterminal::NtPath(ref path) => path.clone(),
416                 _ => return None,
417             },
418             _ => return None,
419         };
420         let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
421         let kind = MetaItemKind::from_tokens(tokens)?;
422         let hi = match kind {
423             MetaItemKind::NameValue(ref lit) => lit.span.hi(),
424             MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
425             _ => path.span.hi(),
426         };
427         let span = path.span.with_hi(hi);
428         Some(MetaItem { path, kind, span })
429     }
430 }
431
432 impl MetaItemKind {
433     pub fn mac_args(&self, span: Span) -> MacArgs {
434         match self {
435             MetaItemKind::Word => MacArgs::Empty,
436             MetaItemKind::NameValue(lit) => MacArgs::Eq(span, lit.to_token()),
437             MetaItemKind::List(list) => {
438                 let mut tts = Vec::new();
439                 for (i, item) in list.iter().enumerate() {
440                     if i > 0 {
441                         tts.push(TokenTree::token(token::Comma, span).into());
442                     }
443                     tts.extend(item.token_trees_and_spacings())
444                 }
445                 MacArgs::Delimited(
446                     DelimSpan::from_single(span),
447                     MacDelimiter::Parenthesis,
448                     TokenStream::new(tts),
449                 )
450             }
451         }
452     }
453
454     fn token_trees_and_spacings(&self, span: Span) -> Vec<TreeAndSpacing> {
455         match *self {
456             MetaItemKind::Word => vec![],
457             MetaItemKind::NameValue(ref lit) => {
458                 vec![
459                     TokenTree::token(token::Eq, span).into(),
460                     TokenTree::Token(lit.to_token()).into(),
461                 ]
462             }
463             MetaItemKind::List(ref list) => {
464                 let mut tokens = Vec::new();
465                 for (i, item) in list.iter().enumerate() {
466                     if i > 0 {
467                         tokens.push(TokenTree::token(token::Comma, span).into());
468                     }
469                     tokens.extend(item.token_trees_and_spacings())
470                 }
471                 vec![
472                     TokenTree::Delimited(
473                         DelimSpan::from_single(span),
474                         token::Paren,
475                         TokenStream::new(tokens),
476                     )
477                     .into(),
478                 ]
479             }
480         }
481     }
482
483     fn list_from_tokens(tokens: TokenStream) -> Option<MetaItemKind> {
484         let mut tokens = tokens.into_trees().peekable();
485         let mut result = Vec::new();
486         while tokens.peek().is_some() {
487             let item = NestedMetaItem::from_tokens(&mut tokens)?;
488             result.push(item);
489             match tokens.next() {
490                 None | Some(TokenTree::Token(Token { kind: token::Comma, .. })) => {}
491                 _ => return None,
492             }
493         }
494         Some(MetaItemKind::List(result))
495     }
496
497     fn name_value_from_tokens(
498         tokens: &mut impl Iterator<Item = TokenTree>,
499     ) -> Option<MetaItemKind> {
500         match tokens.next() {
501             Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
502                 MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
503             }
504             Some(TokenTree::Token(token)) => {
505                 Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
506             }
507             _ => None,
508         }
509     }
510
511     fn from_mac_args(args: &MacArgs) -> Option<MetaItemKind> {
512         match args {
513             MacArgs::Delimited(_, MacDelimiter::Parenthesis, tokens) => {
514                 MetaItemKind::list_from_tokens(tokens.clone())
515             }
516             MacArgs::Delimited(..) => None,
517             MacArgs::Eq(_, token) => Lit::from_token(token).ok().map(MetaItemKind::NameValue),
518             MacArgs::Empty => Some(MetaItemKind::Word),
519         }
520     }
521
522     fn from_tokens(
523         tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
524     ) -> Option<MetaItemKind> {
525         match tokens.peek() {
526             Some(TokenTree::Delimited(_, token::Paren, inner_tokens)) => {
527                 let inner_tokens = inner_tokens.clone();
528                 tokens.next();
529                 MetaItemKind::list_from_tokens(inner_tokens)
530             }
531             Some(TokenTree::Delimited(..)) => None,
532             Some(TokenTree::Token(Token { kind: token::Eq, .. })) => {
533                 tokens.next();
534                 MetaItemKind::name_value_from_tokens(tokens)
535             }
536             _ => Some(MetaItemKind::Word),
537         }
538     }
539 }
540
541 impl NestedMetaItem {
542     pub fn span(&self) -> Span {
543         match *self {
544             NestedMetaItem::MetaItem(ref item) => item.span,
545             NestedMetaItem::Literal(ref lit) => lit.span,
546         }
547     }
548
549     fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
550         match *self {
551             NestedMetaItem::MetaItem(ref item) => item.token_trees_and_spacings(),
552             NestedMetaItem::Literal(ref lit) => vec![TokenTree::Token(lit.to_token()).into()],
553         }
554     }
555
556     fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem>
557     where
558         I: Iterator<Item = TokenTree>,
559     {
560         match tokens.peek() {
561             Some(TokenTree::Token(token)) => {
562                 if let Ok(lit) = Lit::from_token(token) {
563                     tokens.next();
564                     return Some(NestedMetaItem::Literal(lit));
565                 }
566             }
567             Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
568                 let inner_tokens = inner_tokens.clone();
569                 tokens.next();
570                 return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());
571             }
572             _ => {}
573         }
574         MetaItem::from_tokens(tokens).map(NestedMetaItem::MetaItem)
575     }
576 }