1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Compiled declarative macro expanders (`macro_rules!`` and `macro`)
use std::sync::OnceLock;

use base_db::{CrateId, VersionReq};
use mbe::DocCommentDesugarMode;
use span::{Edition, MacroCallId, Span, SyntaxContextId};
use stdx::TupleExt;
use syntax::{ast, AstNode};
use triomphe::Arc;

use crate::{
    attrs::RawAttrs,
    db::ExpandDatabase,
    hygiene::{apply_mark, Transparency},
    tt, AstId, ExpandError, ExpandResult, Lookup,
};

/// Old-style `macro_rules` or the new macros 2.0
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DeclarativeMacroExpander {
    pub mac: mbe::DeclarativeMacro,
    pub transparency: Transparency,
}

// FIXME: Remove this once we drop support for 1.76
static REQUIREMENT: OnceLock<VersionReq> = OnceLock::new();

impl DeclarativeMacroExpander {
    pub fn expand(
        &self,
        db: &dyn ExpandDatabase,
        tt: tt::Subtree,
        call_id: MacroCallId,
        span: Span,
    ) -> ExpandResult<(tt::Subtree, Option<u32>)> {
        let loc = db.lookup_intern_macro_call(call_id);
        let toolchain = db.toolchain(loc.def.krate);
        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
                &base_db::Version {
                    pre: base_db::Prerelease::EMPTY,
                    build: base_db::BuildMetadata::EMPTY,
                    major: version.major,
                    minor: version.minor,
                    patch: version.patch,
                },
            )
        });
        match self.mac.err() {
            Some(_) => ExpandResult::new(
                (tt::Subtree::empty(tt::DelimSpan { open: span, close: span }), None),
                ExpandError::MacroDefinition,
            ),
            None => self
                .mac
                .expand(
                    &tt,
                    |s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
                    new_meta_vars,
                    span,
                    loc.def.edition,
                )
                .map_err(Into::into),
        }
    }

    pub fn expand_unhygienic(
        &self,
        db: &dyn ExpandDatabase,
        tt: tt::Subtree,
        krate: CrateId,
        call_site: Span,
        def_site_edition: Edition,
    ) -> ExpandResult<tt::Subtree> {
        let toolchain = db.toolchain(krate);
        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
                &base_db::Version {
                    pre: base_db::Prerelease::EMPTY,
                    build: base_db::BuildMetadata::EMPTY,
                    major: version.major,
                    minor: version.minor,
                    patch: version.patch,
                },
            )
        });
        match self.mac.err() {
            Some(_) => ExpandResult::new(
                tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
                ExpandError::MacroDefinition,
            ),
            None => self
                .mac
                .expand(&tt, |_| (), new_meta_vars, call_site, def_site_edition)
                .map(TupleExt::head)
                .map_err(Into::into),
        }
    }

    pub(crate) fn expander(
        db: &dyn ExpandDatabase,
        def_crate: CrateId,
        id: AstId<ast::Macro>,
    ) -> Arc<DeclarativeMacroExpander> {
        let (root, map) = crate::db::parse_with_map(db, id.file_id);
        let root = root.syntax_node();

        let transparency = |node| {
            // ... would be nice to have the item tree here
            let attrs = RawAttrs::new(db, node, map.as_ref()).filter(db, def_crate);
            match &*attrs
                .iter()
                .find(|it| {
                    it.path.as_ident().and_then(|it| it.as_str())
                        == Some("rustc_macro_transparency")
                })?
                .token_tree_value()?
                .token_trees
            {
                [tt::TokenTree::Leaf(tt::Leaf::Ident(i)), ..] => match &*i.text {
                    "transparent" => Some(Transparency::Transparent),
                    "semitransparent" => Some(Transparency::SemiTransparent),
                    "opaque" => Some(Transparency::Opaque),
                    _ => None,
                },
                _ => None,
            }
        };
        let toolchain = db.toolchain(def_crate);
        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
                &base_db::Version {
                    pre: base_db::Prerelease::EMPTY,
                    build: base_db::BuildMetadata::EMPTY,
                    major: version.major,
                    minor: version.minor,
                    patch: version.patch,
                },
            )
        });

        let edition = |ctx: SyntaxContextId| {
            let crate_graph = db.crate_graph();
            if ctx.is_root() {
                crate_graph[def_crate].edition
            } else {
                let data = db.lookup_intern_syntax_context(ctx);
                // UNWRAP-SAFETY: Only the root context has no outer expansion
                crate_graph[data.outer_expn.unwrap().lookup(db).def.krate].edition
            }
        };
        let (mac, transparency) = match id.to_ptr(db).to_node(&root) {
            ast::Macro::MacroRules(macro_rules) => (
                match macro_rules.token_tree() {
                    Some(arg) => {
                        let tt = mbe::syntax_node_to_token_tree(
                            arg.syntax(),
                            map.as_ref(),
                            map.span_for_range(
                                macro_rules.macro_rules_token().unwrap().text_range(),
                            ),
                            DocCommentDesugarMode::Mbe,
                        );

                        mbe::DeclarativeMacro::parse_macro_rules(&tt, edition, new_meta_vars)
                    }
                    None => mbe::DeclarativeMacro::from_err(mbe::ParseError::Expected(
                        "expected a token tree".into(),
                    )),
                },
                transparency(&macro_rules).unwrap_or(Transparency::SemiTransparent),
            ),
            ast::Macro::MacroDef(macro_def) => (
                match macro_def.body() {
                    Some(arg) => {
                        let tt = mbe::syntax_node_to_token_tree(
                            arg.syntax(),
                            map.as_ref(),
                            map.span_for_range(macro_def.macro_token().unwrap().text_range()),
                            DocCommentDesugarMode::Mbe,
                        );

                        mbe::DeclarativeMacro::parse_macro2(&tt, edition, new_meta_vars)
                    }
                    None => mbe::DeclarativeMacro::from_err(mbe::ParseError::Expected(
                        "expected a token tree".into(),
                    )),
                },
                transparency(&macro_def).unwrap_or(Transparency::Opaque),
            ),
        };
        Arc::new(DeclarativeMacroExpander { mac, transparency })
    }
}