hir_def/
attr.rs

1//! A higher level attributes based on TokenTree, with also some shortcuts.
2
3use std::{borrow::Cow, convert::identity, hash::Hash, ops};
4
5use base_db::Crate;
6use cfg::{CfgExpr, CfgOptions};
7use either::Either;
8use hir_expand::{
9    HirFileId, InFile,
10    attrs::{Attr, AttrId, RawAttrs, collect_attrs},
11    span_map::SpanMapRef,
12};
13use intern::{Symbol, sym};
14use la_arena::{ArenaMap, Idx, RawIdx};
15use mbe::DelimiterKind;
16use rustc_abi::ReprOptions;
17use span::AstIdNode;
18use syntax::{
19    AstPtr,
20    ast::{self, HasAttrs},
21};
22use triomphe::Arc;
23use tt::iter::{TtElement, TtIter};
24
25use crate::{
26    AdtId, AstIdLoc, AttrDefId, GenericParamId, HasModule, LocalFieldId, Lookup, MacroId,
27    VariantId,
28    db::DefDatabase,
29    item_tree::block_item_tree_query,
30    lang_item::LangItem,
31    nameres::{ModuleOrigin, ModuleSource},
32    src::{HasChildSource, HasSource},
33};
34
35/// Desugared attributes of an item post `cfg_attr` expansion.
36#[derive(Default, Debug, Clone, PartialEq, Eq)]
37pub struct Attrs(RawAttrs);
38
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct AttrsWithOwner {
41    attrs: Attrs,
42    owner: AttrDefId,
43}
44
45impl Attrs {
46    pub fn new(
47        db: &dyn DefDatabase,
48        owner: &dyn ast::HasAttrs,
49        span_map: SpanMapRef<'_>,
50        cfg_options: &CfgOptions,
51    ) -> Self {
52        Attrs(RawAttrs::new_expanded(db, owner, span_map, cfg_options))
53    }
54
55    pub fn get(&self, id: AttrId) -> Option<&Attr> {
56        (**self).iter().find(|attr| attr.id == id)
57    }
58
59    pub(crate) fn expand_cfg_attr(
60        db: &dyn DefDatabase,
61        krate: Crate,
62        raw_attrs: RawAttrs,
63    ) -> Attrs {
64        Attrs(raw_attrs.expand_cfg_attr(db, krate))
65    }
66
67    pub(crate) fn is_cfg_enabled_for(
68        db: &dyn DefDatabase,
69        owner: &dyn ast::HasAttrs,
70        span_map: SpanMapRef<'_>,
71        cfg_options: &CfgOptions,
72    ) -> Result<(), CfgExpr> {
73        RawAttrs::attrs_iter_expanded::<false>(db, owner, span_map, cfg_options)
74            .filter_map(|attr| attr.cfg())
75            .find_map(|cfg| match cfg_options.check(&cfg).is_none_or(identity) {
76                true => None,
77                false => Some(cfg),
78            })
79            .map_or(Ok(()), Err)
80    }
81}
82
83impl ops::Deref for Attrs {
84    type Target = [Attr];
85
86    fn deref(&self) -> &[Attr] {
87        &self.0
88    }
89}
90
91impl ops::Deref for AttrsWithOwner {
92    type Target = Attrs;
93
94    fn deref(&self) -> &Attrs {
95        &self.attrs
96    }
97}
98
99impl Attrs {
100    pub const EMPTY: Self = Self(RawAttrs::EMPTY);
101
102    pub(crate) fn fields_attrs_query(
103        db: &dyn DefDatabase,
104        v: VariantId,
105    ) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
106        let _p = tracing::info_span!("fields_attrs_query").entered();
107        let mut res = ArenaMap::default();
108        let (fields, file_id, krate) = match v {
109            VariantId::EnumVariantId(it) => {
110                let loc = it.lookup(db);
111                let krate = loc.parent.lookup(db).container.krate;
112                let source = loc.source(db);
113                (source.value.field_list(), source.file_id, krate)
114            }
115            VariantId::StructId(it) => {
116                let loc = it.lookup(db);
117                let krate = loc.container.krate;
118                let source = loc.source(db);
119                (source.value.field_list(), source.file_id, krate)
120            }
121            VariantId::UnionId(it) => {
122                let loc = it.lookup(db);
123                let krate = loc.container.krate;
124                let source = loc.source(db);
125                (
126                    source.value.record_field_list().map(ast::FieldList::RecordFieldList),
127                    source.file_id,
128                    krate,
129                )
130            }
131        };
132        let Some(fields) = fields else {
133            return Arc::new(res);
134        };
135
136        let cfg_options = krate.cfg_options(db);
137        let span_map = db.span_map(file_id);
138
139        match fields {
140            ast::FieldList::RecordFieldList(fields) => {
141                let mut idx = 0;
142                for field in fields.fields() {
143                    let attrs =
144                        Attrs(RawAttrs::new_expanded(db, &field, span_map.as_ref(), cfg_options));
145                    if attrs.is_cfg_enabled(cfg_options).is_ok() {
146                        res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
147                        idx += 1;
148                    }
149                }
150            }
151            ast::FieldList::TupleFieldList(fields) => {
152                let mut idx = 0;
153                for field in fields.fields() {
154                    let attrs =
155                        Attrs(RawAttrs::new_expanded(db, &field, span_map.as_ref(), cfg_options));
156                    if attrs.is_cfg_enabled(cfg_options).is_ok() {
157                        res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
158                        idx += 1;
159                    }
160                }
161            }
162        }
163
164        res.shrink_to_fit();
165        Arc::new(res)
166    }
167}
168
169impl Attrs {
170    #[inline]
171    pub fn by_key(&self, key: Symbol) -> AttrQuery<'_> {
172        AttrQuery { attrs: self, key }
173    }
174
175    #[inline]
176    pub fn rust_analyzer_tool(&self) -> impl Iterator<Item = &Attr> {
177        self.iter()
178            .filter(|&attr| attr.path.segments().first().is_some_and(|s| *s == sym::rust_analyzer))
179    }
180
181    #[inline]
182    pub fn cfg(&self) -> Option<CfgExpr> {
183        let mut cfgs = self.by_key(sym::cfg).tt_values().map(CfgExpr::parse);
184        let first = cfgs.next()?;
185        match cfgs.next() {
186            Some(second) => {
187                let cfgs = [first, second].into_iter().chain(cfgs);
188                Some(CfgExpr::All(cfgs.collect()))
189            }
190            None => Some(first),
191        }
192    }
193
194    #[inline]
195    pub fn cfgs(&self) -> impl Iterator<Item = CfgExpr> + '_ {
196        self.by_key(sym::cfg).tt_values().map(CfgExpr::parse)
197    }
198
199    #[inline]
200    pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Result<(), CfgExpr> {
201        self.cfgs().try_for_each(|cfg| {
202            if cfg_options.check(&cfg) != Some(false) { Ok(()) } else { Err(cfg) }
203        })
204    }
205
206    #[inline]
207    pub fn lang(&self) -> Option<&Symbol> {
208        self.by_key(sym::lang).string_value()
209    }
210
211    #[inline]
212    pub fn lang_item(&self) -> Option<LangItem> {
213        self.by_key(sym::lang).string_value().and_then(LangItem::from_symbol)
214    }
215
216    #[inline]
217    pub fn has_doc_hidden(&self) -> bool {
218        self.by_key(sym::doc).tt_values().any(|tt| {
219            tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
220                matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::hidden)
221        })
222    }
223
224    #[inline]
225    pub fn has_doc_notable_trait(&self) -> bool {
226        self.by_key(sym::doc).tt_values().any(|tt| {
227            tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
228                matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::notable_trait)
229        })
230    }
231
232    #[inline]
233    pub fn doc_exprs(&self) -> impl Iterator<Item = DocExpr> + '_ {
234        self.by_key(sym::doc).tt_values().map(DocExpr::parse)
235    }
236
237    #[inline]
238    pub fn doc_aliases(&self) -> impl Iterator<Item = Symbol> + '_ {
239        self.doc_exprs().flat_map(|doc_expr| doc_expr.aliases().to_vec())
240    }
241
242    #[inline]
243    pub fn export_name(&self) -> Option<&Symbol> {
244        self.by_key(sym::export_name).string_value()
245    }
246
247    #[inline]
248    pub fn is_proc_macro(&self) -> bool {
249        self.by_key(sym::proc_macro).exists()
250    }
251
252    #[inline]
253    pub fn is_proc_macro_attribute(&self) -> bool {
254        self.by_key(sym::proc_macro_attribute).exists()
255    }
256
257    #[inline]
258    pub fn is_proc_macro_derive(&self) -> bool {
259        self.by_key(sym::proc_macro_derive).exists()
260    }
261
262    #[inline]
263    pub fn is_test(&self) -> bool {
264        self.iter().any(|it| {
265            it.path()
266                .segments()
267                .iter()
268                .rev()
269                .zip([sym::core, sym::prelude, sym::v1, sym::test].iter().rev())
270                .all(|it| it.0 == it.1)
271        })
272    }
273
274    #[inline]
275    pub fn is_ignore(&self) -> bool {
276        self.by_key(sym::ignore).exists()
277    }
278
279    #[inline]
280    pub fn is_bench(&self) -> bool {
281        self.by_key(sym::bench).exists()
282    }
283
284    #[inline]
285    pub fn is_unstable(&self) -> bool {
286        self.by_key(sym::unstable).exists()
287    }
288
289    #[inline]
290    pub fn rustc_legacy_const_generics(&self) -> Option<Box<Box<[u32]>>> {
291        self.by_key(sym::rustc_legacy_const_generics)
292            .tt_values()
293            .next()
294            .map(parse_rustc_legacy_const_generics)
295            .filter(|it| !it.is_empty())
296            .map(Box::new)
297    }
298
299    #[inline]
300    pub fn repr(&self) -> Option<ReprOptions> {
301        self.by_key(sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
302            acc.map_or(Some(repr), |mut acc| {
303                merge_repr(&mut acc, repr);
304                Some(acc)
305            })
306        })
307    }
308}
309
310fn parse_rustc_legacy_const_generics(tt: &crate::tt::TopSubtree) -> Box<[u32]> {
311    let mut indices = Vec::new();
312    let mut iter = tt.iter();
313    while let (Some(first), second) = (iter.next(), iter.next()) {
314        match first {
315            TtElement::Leaf(tt::Leaf::Literal(lit)) => match lit.symbol.as_str().parse() {
316                Ok(index) => indices.push(index),
317                Err(_) => break,
318            },
319            _ => break,
320        }
321
322        if let Some(comma) = second {
323            match comma {
324                TtElement::Leaf(tt::Leaf::Punct(punct)) if punct.char == ',' => {}
325                _ => break,
326            }
327        }
328    }
329
330    indices.into_boxed_slice()
331}
332
333fn merge_repr(this: &mut ReprOptions, other: ReprOptions) {
334    let ReprOptions { int, align, pack, flags, field_shuffle_seed: _ } = this;
335    flags.insert(other.flags);
336    *align = (*align).max(other.align);
337    *pack = match (*pack, other.pack) {
338        (Some(pack), None) | (None, Some(pack)) => Some(pack),
339        _ => (*pack).min(other.pack),
340    };
341    if other.int.is_some() {
342        *int = other.int;
343    }
344}
345
346fn parse_repr_tt(tt: &crate::tt::TopSubtree) -> Option<ReprOptions> {
347    use crate::builtin_type::{BuiltinInt, BuiltinUint};
348    use rustc_abi::{Align, Integer, IntegerType, ReprFlags, ReprOptions};
349
350    match tt.top_subtree().delimiter {
351        tt::Delimiter { kind: DelimiterKind::Parenthesis, .. } => {}
352        _ => return None,
353    }
354
355    let mut acc = ReprOptions::default();
356    let mut tts = tt.iter();
357    while let Some(tt) = tts.next() {
358        let TtElement::Leaf(tt::Leaf::Ident(ident)) = tt else {
359            continue;
360        };
361        let repr = match &ident.sym {
362            s if *s == sym::packed => {
363                let pack = if let Some(TtElement::Subtree(_, mut tt_iter)) = tts.peek() {
364                    tts.next();
365                    if let Some(TtElement::Leaf(tt::Leaf::Literal(lit))) = tt_iter.next() {
366                        lit.symbol.as_str().parse().unwrap_or_default()
367                    } else {
368                        0
369                    }
370                } else {
371                    0
372                };
373                let pack = Some(Align::from_bytes(pack).unwrap_or(Align::ONE));
374                ReprOptions { pack, ..Default::default() }
375            }
376            s if *s == sym::align => {
377                let mut align = None;
378                if let Some(TtElement::Subtree(_, mut tt_iter)) = tts.peek() {
379                    tts.next();
380                    if let Some(TtElement::Leaf(tt::Leaf::Literal(lit))) = tt_iter.next()
381                        && let Ok(a) = lit.symbol.as_str().parse()
382                    {
383                        align = Align::from_bytes(a).ok();
384                    }
385                }
386                ReprOptions { align, ..Default::default() }
387            }
388            s if *s == sym::C => ReprOptions { flags: ReprFlags::IS_C, ..Default::default() },
389            s if *s == sym::transparent => {
390                ReprOptions { flags: ReprFlags::IS_TRANSPARENT, ..Default::default() }
391            }
392            s if *s == sym::simd => ReprOptions { flags: ReprFlags::IS_SIMD, ..Default::default() },
393            repr => {
394                let mut int = None;
395                if let Some(builtin) = BuiltinInt::from_suffix_sym(repr)
396                    .map(Either::Left)
397                    .or_else(|| BuiltinUint::from_suffix_sym(repr).map(Either::Right))
398                {
399                    int = Some(match builtin {
400                        Either::Left(bi) => match bi {
401                            BuiltinInt::Isize => IntegerType::Pointer(true),
402                            BuiltinInt::I8 => IntegerType::Fixed(Integer::I8, true),
403                            BuiltinInt::I16 => IntegerType::Fixed(Integer::I16, true),
404                            BuiltinInt::I32 => IntegerType::Fixed(Integer::I32, true),
405                            BuiltinInt::I64 => IntegerType::Fixed(Integer::I64, true),
406                            BuiltinInt::I128 => IntegerType::Fixed(Integer::I128, true),
407                        },
408                        Either::Right(bu) => match bu {
409                            BuiltinUint::Usize => IntegerType::Pointer(false),
410                            BuiltinUint::U8 => IntegerType::Fixed(Integer::I8, false),
411                            BuiltinUint::U16 => IntegerType::Fixed(Integer::I16, false),
412                            BuiltinUint::U32 => IntegerType::Fixed(Integer::I32, false),
413                            BuiltinUint::U64 => IntegerType::Fixed(Integer::I64, false),
414                            BuiltinUint::U128 => IntegerType::Fixed(Integer::I128, false),
415                        },
416                    });
417                }
418                ReprOptions { int, ..Default::default() }
419            }
420        };
421        merge_repr(&mut acc, repr);
422    }
423
424    Some(acc)
425}
426
427#[derive(Debug, Clone, PartialEq, Eq, Hash)]
428pub enum DocAtom {
429    /// eg. `#[doc(hidden)]`
430    Flag(Symbol),
431    /// eg. `#[doc(alias = "it")]`
432    ///
433    /// Note that a key can have multiple values that are all considered "active" at the same time.
434    /// For example, `#[doc(alias = "x")]` and `#[doc(alias = "y")]`.
435    KeyValue { key: Symbol, value: Symbol },
436}
437
438#[derive(Debug, Clone, PartialEq, Eq, Hash)]
439pub enum DocExpr {
440    Invalid,
441    /// eg. `#[doc(hidden)]`, `#[doc(alias = "x")]`
442    Atom(DocAtom),
443    /// eg. `#[doc(alias("x", "y"))]`
444    Alias(Vec<Symbol>),
445}
446
447impl From<DocAtom> for DocExpr {
448    fn from(atom: DocAtom) -> Self {
449        DocExpr::Atom(atom)
450    }
451}
452
453impl DocExpr {
454    fn parse<S: Copy>(tt: &tt::TopSubtree<S>) -> DocExpr {
455        next_doc_expr(tt.iter()).unwrap_or(DocExpr::Invalid)
456    }
457
458    pub fn aliases(&self) -> &[Symbol] {
459        match self {
460            DocExpr::Atom(DocAtom::KeyValue { key, value }) if *key == sym::alias => {
461                std::slice::from_ref(value)
462            }
463            DocExpr::Alias(aliases) => aliases,
464            _ => &[],
465        }
466    }
467}
468
469fn next_doc_expr<S: Copy>(mut it: TtIter<'_, S>) -> Option<DocExpr> {
470    let name = match it.next() {
471        None => return None,
472        Some(TtElement::Leaf(tt::Leaf::Ident(ident))) => ident.sym.clone(),
473        Some(_) => return Some(DocExpr::Invalid),
474    };
475
476    // Peek
477    let ret = match it.peek() {
478        Some(TtElement::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
479            it.next();
480            match it.next() {
481                Some(TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
482                    symbol: text,
483                    kind: tt::LitKind::Str,
484                    ..
485                }))) => DocAtom::KeyValue { key: name, value: text.clone() }.into(),
486                _ => return Some(DocExpr::Invalid),
487            }
488        }
489        Some(TtElement::Subtree(_, subtree_iter)) => {
490            it.next();
491            let subs = parse_comma_sep(subtree_iter);
492            match &name {
493                s if *s == sym::alias => DocExpr::Alias(subs),
494                _ => DocExpr::Invalid,
495            }
496        }
497        _ => DocAtom::Flag(name).into(),
498    };
499    Some(ret)
500}
501
502fn parse_comma_sep<S>(iter: TtIter<'_, S>) -> Vec<Symbol> {
503    iter.filter_map(|tt| match tt {
504        TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
505            kind: tt::LitKind::Str, symbol, ..
506        })) => Some(symbol.clone()),
507        _ => None,
508    })
509    .collect()
510}
511
512impl AttrsWithOwner {
513    pub fn new(db: &dyn DefDatabase, owner: AttrDefId) -> Self {
514        Self { attrs: db.attrs(owner), owner }
515    }
516
517    pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
518        let _p = tracing::info_span!("attrs_query").entered();
519        // FIXME: this should use `Trace` to avoid duplication in `source_map` below
520        match def {
521            AttrDefId::ModuleId(module) => {
522                let def_map = module.def_map(db);
523                let mod_data = &def_map[module.local_id];
524
525                let raw_attrs = match mod_data.origin {
526                    ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
527                        let decl_attrs = declaration_tree_id
528                            .item_tree(db)
529                            .raw_attrs(declaration.upcast())
530                            .clone();
531                        let tree = db.file_item_tree(definition.into());
532                        let def_attrs = tree.top_level_raw_attrs().clone();
533                        decl_attrs.merge(def_attrs)
534                    }
535                    ModuleOrigin::CrateRoot { definition } => {
536                        let tree = db.file_item_tree(definition.into());
537                        tree.top_level_raw_attrs().clone()
538                    }
539                    ModuleOrigin::Inline { definition_tree_id, definition } => {
540                        definition_tree_id.item_tree(db).raw_attrs(definition.upcast()).clone()
541                    }
542                    ModuleOrigin::BlockExpr { id, .. } => {
543                        let tree = block_item_tree_query(db, id);
544                        tree.top_level_raw_attrs().clone()
545                    }
546                };
547                Attrs::expand_cfg_attr(db, module.krate, raw_attrs)
548            }
549            AttrDefId::FieldId(it) => db.fields_attrs(it.parent)[it.local_id].clone(),
550            AttrDefId::EnumVariantId(it) => attrs_from_ast_id_loc(db, it),
551            AttrDefId::AdtId(it) => match it {
552                AdtId::StructId(it) => attrs_from_ast_id_loc(db, it),
553                AdtId::EnumId(it) => attrs_from_ast_id_loc(db, it),
554                AdtId::UnionId(it) => attrs_from_ast_id_loc(db, it),
555            },
556            AttrDefId::TraitId(it) => attrs_from_ast_id_loc(db, it),
557            AttrDefId::MacroId(it) => match it {
558                MacroId::Macro2Id(it) => attrs_from_ast_id_loc(db, it),
559                MacroId::MacroRulesId(it) => attrs_from_ast_id_loc(db, it),
560                MacroId::ProcMacroId(it) => attrs_from_ast_id_loc(db, it),
561            },
562            AttrDefId::ImplId(it) => attrs_from_ast_id_loc(db, it),
563            AttrDefId::ConstId(it) => attrs_from_ast_id_loc(db, it),
564            AttrDefId::StaticId(it) => attrs_from_ast_id_loc(db, it),
565            AttrDefId::FunctionId(it) => attrs_from_ast_id_loc(db, it),
566            AttrDefId::TypeAliasId(it) => attrs_from_ast_id_loc(db, it),
567            AttrDefId::GenericParamId(it) => match it {
568                GenericParamId::ConstParamId(it) => {
569                    let src = it.parent().child_source(db);
570                    // FIXME: We should be never getting `None` here.
571                    Attrs(match src.value.get(it.local_id()) {
572                        Some(val) => RawAttrs::new_expanded(
573                            db,
574                            val,
575                            db.span_map(src.file_id).as_ref(),
576                            def.krate(db).cfg_options(db),
577                        ),
578                        None => RawAttrs::EMPTY,
579                    })
580                }
581                GenericParamId::TypeParamId(it) => {
582                    let src = it.parent().child_source(db);
583                    // FIXME: We should be never getting `None` here.
584                    Attrs(match src.value.get(it.local_id()) {
585                        Some(val) => RawAttrs::new_expanded(
586                            db,
587                            val,
588                            db.span_map(src.file_id).as_ref(),
589                            def.krate(db).cfg_options(db),
590                        ),
591                        None => RawAttrs::EMPTY,
592                    })
593                }
594                GenericParamId::LifetimeParamId(it) => {
595                    let src = it.parent.child_source(db);
596                    // FIXME: We should be never getting `None` here.
597                    Attrs(match src.value.get(it.local_id) {
598                        Some(val) => RawAttrs::new_expanded(
599                            db,
600                            val,
601                            db.span_map(src.file_id).as_ref(),
602                            def.krate(db).cfg_options(db),
603                        ),
604                        None => RawAttrs::EMPTY,
605                    })
606                }
607            },
608            AttrDefId::ExternBlockId(it) => attrs_from_ast_id_loc(db, it),
609            AttrDefId::ExternCrateId(it) => attrs_from_ast_id_loc(db, it),
610            AttrDefId::UseId(it) => attrs_from_ast_id_loc(db, it),
611        }
612    }
613
614    pub fn source_map(&self, db: &dyn DefDatabase) -> AttrSourceMap {
615        let owner = match self.owner {
616            AttrDefId::ModuleId(module) => {
617                // Modules can have 2 attribute owners (the `mod x;` item, and the module file itself).
618
619                let def_map = module.def_map(db);
620                let mod_data = &def_map[module.local_id];
621                match mod_data.declaration_source(db) {
622                    Some(it) => {
623                        let mut map = AttrSourceMap::new(InFile::new(it.file_id, &it.value));
624                        if let InFile { file_id, value: ModuleSource::SourceFile(file) } =
625                            mod_data.definition_source(db)
626                        {
627                            map.append_module_inline_attrs(AttrSourceMap::new(InFile::new(
628                                file_id, &file,
629                            )));
630                        }
631                        return map;
632                    }
633                    None => {
634                        let InFile { file_id, value } = mod_data.definition_source(db);
635                        let attrs_owner = match &value {
636                            ModuleSource::SourceFile(file) => file as &dyn ast::HasAttrs,
637                            ModuleSource::Module(module) => module as &dyn ast::HasAttrs,
638                            ModuleSource::BlockExpr(block) => block as &dyn ast::HasAttrs,
639                        };
640                        return AttrSourceMap::new(InFile::new(file_id, attrs_owner));
641                    }
642                }
643            }
644            AttrDefId::FieldId(id) => {
645                let map = db.fields_attrs_source_map(id.parent);
646                let file_id = id.parent.file_id(db);
647                let root = db.parse_or_expand(file_id);
648                let owner = ast::AnyHasAttrs::new(map[id.local_id].to_node(&root));
649                InFile::new(file_id, owner)
650            }
651            AttrDefId::AdtId(adt) => match adt {
652                AdtId::StructId(id) => any_has_attrs(db, id),
653                AdtId::UnionId(id) => any_has_attrs(db, id),
654                AdtId::EnumId(id) => any_has_attrs(db, id),
655            },
656            AttrDefId::FunctionId(id) => any_has_attrs(db, id),
657            AttrDefId::EnumVariantId(id) => any_has_attrs(db, id),
658            AttrDefId::StaticId(id) => any_has_attrs(db, id),
659            AttrDefId::ConstId(id) => any_has_attrs(db, id),
660            AttrDefId::TraitId(id) => any_has_attrs(db, id),
661            AttrDefId::TypeAliasId(id) => any_has_attrs(db, id),
662            AttrDefId::MacroId(id) => match id {
663                MacroId::Macro2Id(id) => any_has_attrs(db, id),
664                MacroId::MacroRulesId(id) => any_has_attrs(db, id),
665                MacroId::ProcMacroId(id) => any_has_attrs(db, id),
666            },
667            AttrDefId::ImplId(id) => any_has_attrs(db, id),
668            AttrDefId::GenericParamId(id) => match id {
669                GenericParamId::ConstParamId(id) => id
670                    .parent()
671                    .child_source(db)
672                    .map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
673                GenericParamId::TypeParamId(id) => id
674                    .parent()
675                    .child_source(db)
676                    .map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
677                GenericParamId::LifetimeParamId(id) => id
678                    .parent
679                    .child_source(db)
680                    .map(|source| ast::AnyHasAttrs::new(source[id.local_id].clone())),
681            },
682            AttrDefId::ExternBlockId(id) => any_has_attrs(db, id),
683            AttrDefId::ExternCrateId(id) => any_has_attrs(db, id),
684            AttrDefId::UseId(id) => any_has_attrs(db, id),
685        };
686
687        AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn HasAttrs))
688    }
689}
690
691#[derive(Debug)]
692pub struct AttrSourceMap {
693    source: Vec<Either<ast::Attr, ast::Comment>>,
694    file_id: HirFileId,
695    /// If this map is for a module, this will be the [`HirFileId`] of the module's definition site,
696    /// while `file_id` will be the one of the module declaration site.
697    /// The usize is the index into `source` from which point on the entries reside in the def site
698    /// file.
699    mod_def_site_file_id: Option<(HirFileId, usize)>,
700}
701
702impl AttrSourceMap {
703    fn new(owner: InFile<&dyn ast::HasAttrs>) -> Self {
704        Self {
705            source: collect_attrs(owner.value).map(|(_, it)| it).collect(),
706            file_id: owner.file_id,
707            mod_def_site_file_id: None,
708        }
709    }
710
711    /// Append a second source map to this one, this is required for modules, whose outline and inline
712    /// attributes can reside in different files
713    fn append_module_inline_attrs(&mut self, other: Self) {
714        assert!(self.mod_def_site_file_id.is_none() && other.mod_def_site_file_id.is_none());
715        let len = self.source.len();
716        self.source.extend(other.source);
717        if other.file_id != self.file_id {
718            self.mod_def_site_file_id = Some((other.file_id, len));
719        }
720    }
721
722    /// Maps the lowered `Attr` back to its original syntax node.
723    ///
724    /// `attr` must come from the `owner` used for AttrSourceMap
725    ///
726    /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of
727    /// the attribute represented by `Attr`.
728    pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> {
729        self.source_of_id(attr.id)
730    }
731
732    pub fn source_of_id(&self, id: AttrId) -> InFile<&Either<ast::Attr, ast::Comment>> {
733        let ast_idx = id.ast_index();
734        let file_id = match self.mod_def_site_file_id {
735            Some((file_id, def_site_cut)) if def_site_cut <= ast_idx => file_id,
736            _ => self.file_id,
737        };
738
739        self.source
740            .get(ast_idx)
741            .map(|it| InFile::new(file_id, it))
742            .unwrap_or_else(|| panic!("cannot find attr at index {id:?}"))
743    }
744}
745
746#[derive(Debug, Clone)]
747pub struct AttrQuery<'attr> {
748    attrs: &'attr Attrs,
749    key: Symbol,
750}
751
752impl<'attr> AttrQuery<'attr> {
753    #[inline]
754    pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::TopSubtree> {
755        self.attrs().filter_map(|attr| attr.token_tree_value())
756    }
757
758    #[inline]
759    pub fn string_value(self) -> Option<&'attr Symbol> {
760        self.attrs().find_map(|attr| attr.string_value())
761    }
762
763    #[inline]
764    pub fn string_value_with_span(self) -> Option<(&'attr Symbol, span::Span)> {
765        self.attrs().find_map(|attr| attr.string_value_with_span())
766    }
767
768    #[inline]
769    pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
770        self.attrs().find_map(|attr| attr.string_value_unescape())
771    }
772
773    #[inline]
774    pub fn exists(self) -> bool {
775        self.attrs().next().is_some()
776    }
777
778    #[inline]
779    pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
780        let key = self.key;
781        self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == key))
782    }
783
784    /// Find string value for a specific key inside token tree
785    ///
786    /// ```ignore
787    /// #[doc(html_root_url = "url")]
788    ///       ^^^^^^^^^^^^^ key
789    /// ```
790    #[inline]
791    pub fn find_string_value_in_tt(self, key: Symbol) -> Option<&'attr str> {
792        self.tt_values().find_map(|tt| {
793            let name = tt.iter()
794                .skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == key))
795                .nth(2);
796
797            match name {
798                Some(TtElement::Leaf(tt::Leaf::Literal(tt::Literal{  symbol: text, kind: tt::LitKind::Str | tt::LitKind::StrRaw(_) , ..}))) => Some(text.as_str()),
799                _ => None
800            }
801        })
802    }
803}
804
805fn any_has_attrs<'db>(
806    db: &(dyn DefDatabase + 'db),
807    id: impl Lookup<Database = dyn DefDatabase, Data = impl HasSource<Value = impl ast::HasAttrs>>,
808) -> InFile<ast::AnyHasAttrs> {
809    id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
810}
811
812fn attrs_from_ast_id_loc<'db, N: AstIdNode + HasAttrs>(
813    db: &(dyn DefDatabase + 'db),
814    lookup: impl Lookup<Database = dyn DefDatabase, Data = impl AstIdLoc<Ast = N> + HasModule>,
815) -> Attrs {
816    let loc = lookup.lookup(db);
817    let source = loc.source(db);
818    let span_map = db.span_map(source.file_id);
819    let cfg_options = loc.krate(db).cfg_options(db);
820    Attrs(RawAttrs::new_expanded(db, &source.value, span_map.as_ref(), cfg_options))
821}
822
823pub(crate) fn fields_attrs_source_map(
824    db: &dyn DefDatabase,
825    def: VariantId,
826) -> Arc<ArenaMap<LocalFieldId, AstPtr<Either<ast::TupleField, ast::RecordField>>>> {
827    let mut res = ArenaMap::default();
828    let child_source = def.child_source(db);
829
830    for (idx, variant) in child_source.value.iter() {
831        res.insert(
832            idx,
833            variant
834                .as_ref()
835                .either(|l| AstPtr::new(l).wrap_left(), |r| AstPtr::new(r).wrap_right()),
836        );
837    }
838
839    Arc::new(res)
840}
841
842#[cfg(test)]
843mod tests {
844    //! This module contains tests for doc-expression parsing.
845    //! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
846
847    use intern::Symbol;
848    use span::EditionedFileId;
849    use triomphe::Arc;
850
851    use hir_expand::span_map::{RealSpanMap, SpanMap};
852    use span::FileId;
853    use syntax::{AstNode, TextRange, ast};
854    use syntax_bridge::{DocCommentDesugarMode, syntax_node_to_token_tree};
855
856    use crate::attr::{DocAtom, DocExpr};
857
858    fn assert_parse_result(input: &str, expected: DocExpr) {
859        let source_file = ast::SourceFile::parse(input, span::Edition::CURRENT).ok().unwrap();
860        let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
861        let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(
862            EditionedFileId::current_edition(FileId::from_raw(0)),
863        )));
864        let tt = syntax_node_to_token_tree(
865            tt.syntax(),
866            map.as_ref(),
867            map.span_for_range(TextRange::empty(0.into())),
868            DocCommentDesugarMode::ProcMacro,
869        );
870        let cfg = DocExpr::parse(&tt);
871        assert_eq!(cfg, expected);
872    }
873
874    #[test]
875    fn test_doc_expr_parser() {
876        assert_parse_result("#![doc(hidden)]", DocAtom::Flag(Symbol::intern("hidden")).into());
877
878        assert_parse_result(
879            r#"#![doc(alias = "foo")]"#,
880            DocAtom::KeyValue { key: Symbol::intern("alias"), value: Symbol::intern("foo") }.into(),
881        );
882
883        assert_parse_result(
884            r#"#![doc(alias("foo"))]"#,
885            DocExpr::Alias([Symbol::intern("foo")].into()),
886        );
887        assert_parse_result(
888            r#"#![doc(alias("foo", "bar", "baz"))]"#,
889            DocExpr::Alias(
890                [Symbol::intern("foo"), Symbol::intern("bar"), Symbol::intern("baz")].into(),
891            ),
892        );
893
894        assert_parse_result(
895            r#"
896        #[doc(alias("Bar", "Qux"))]
897        struct Foo;"#,
898            DocExpr::Alias([Symbol::intern("Bar"), Symbol::intern("Qux")].into()),
899        );
900    }
901}