aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/eager.rs
blob: f95f37ede14810e2ae03b070be6588b2ada67491 (plain)
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
//! Eager expansion related utils
//!
//! Here is a dump of a discussion from Vadim Petrochenkov about Eager Expansion and
//! Its name resolution :
//!
//! > Eagerly expanded macros (and also macros eagerly expanded by eagerly expanded macros,
//! > which actually happens in practice too!) are resolved at the location of the "root" macro
//! > that performs the eager expansion on its arguments.
//! > If some name cannot be resolved at the eager expansion time it's considered unresolved,
//! > even if becomes available later (e.g. from a glob import or other macro).
//!
//! > Eagerly expanded macros don't add anything to the module structure of the crate and
//! > don't build any speculative module structures, i.e. they are expanded in a "flat"
//! > way even if tokens in them look like modules.
//!
//! > In other words, it kinda works for simple cases for which it was originally intended,
//! > and we need to live with it because it's available on stable and widely relied upon.
//!
//!
//! See the full discussion : https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros

use crate::{
    ast::{self, AstNode},
    db::AstDatabase,
    EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
};

use ra_parser::FragmentKind;
use ra_syntax::{algo::replace_descendants, SyntaxElement, SyntaxNode};
use std::{collections::HashMap, sync::Arc};

pub fn expand_eager_macro(
    db: &impl AstDatabase,
    macro_call: InFile<ast::MacroCall>,
    def: MacroDefId,
    resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
) -> Option<EagerMacroId> {
    let args = macro_call.value.token_tree()?;
    let parsed_args = mbe::ast_to_token_tree(&args)?.0;

    // Note:
    // When `lazy_expand` is called, its *parent* file must be already exists.
    // Here we store an eager macro id for the argument expanded subtree here
    // for that purpose.
    let arg_id: MacroCallId = db
        .intern_eager_expansion({
            EagerCallLoc {
                def,
                fragment: FragmentKind::Expr,
                subtree: Arc::new(parsed_args.clone()),
                file_id: macro_call.file_id,
            }
        })
        .into();

    let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr).ok()?.0;
    let result =
        eager_macro_recur(db, InFile::new(arg_id.as_file(), parsed_args.syntax_node()), resolver)?;
    let subtree = to_subtree(&result)?;

    if let MacroDefKind::BuiltInEager(eager) = def.kind {
        let (subtree, fragment) = eager.expand(&subtree).ok()?;
        let eager =
            EagerCallLoc { def, fragment, subtree: Arc::new(subtree), file_id: macro_call.file_id };

        Some(db.intern_eager_expansion(eager))
    } else {
        None
    }
}

fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> {
    let mut subtree = mbe::syntax_node_to_token_tree(node)?.0;
    subtree.delimiter = None;
    Some(subtree)
}

fn lazy_expand(
    db: &impl AstDatabase,
    def: &MacroDefId,
    macro_call: InFile<ast::MacroCall>,
) -> Option<InFile<SyntaxNode>> {
    let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);

    let id: MacroCallId =
        def.as_lazy_macro(db, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into();

    db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node))
}

fn eager_macro_recur(
    db: &impl AstDatabase,
    curr: InFile<SyntaxNode>,
    macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
) -> Option<SyntaxNode> {
    let mut original = curr.value.clone();

    let children = curr.value.descendants().filter_map(ast::MacroCall::cast);
    let mut replaces: HashMap<SyntaxElement, SyntaxElement> = HashMap::default();

    // Collect replacement
    for child in children {
        let def: MacroDefId = macro_resolver(child.path()?)?;
        let insert = match def.kind {
            MacroDefKind::BuiltInEager(_) => {
                let id: MacroCallId =
                    expand_eager_macro(db, curr.with_value(child.clone()), def, macro_resolver)?
                        .into();
                db.parse_or_expand(id.as_file())?
            }
            MacroDefKind::Declarative
            | MacroDefKind::BuiltIn(_)
            | MacroDefKind::BuiltInDerive(_) => {
                let expanded = lazy_expand(db, &def, curr.with_value(child.clone()))?;
                // replace macro inside
                eager_macro_recur(db, expanded, macro_resolver)?
            }
        };

        replaces.insert(child.syntax().clone().into(), insert.into());
    }

    if !replaces.is_empty() {
        original = replace_descendants(&original, |n| replaces.get(n).cloned());
    }

    Some(original)
}