diff options
Diffstat (limited to 'crates/ra_hir_expand/src/eager.rs')
-rw-r--r-- | crates/ra_hir_expand/src/eager.rs | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/crates/ra_hir_expand/src/eager.rs b/crates/ra_hir_expand/src/eager.rs deleted file mode 100644 index 10c45646f..000000000 --- a/crates/ra_hir_expand/src/eager.rs +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | //! Eager expansion related utils | ||
2 | //! | ||
3 | //! Here is a dump of a discussion from Vadim Petrochenkov about Eager Expansion and | ||
4 | //! Its name resolution : | ||
5 | //! | ||
6 | //! > Eagerly expanded macros (and also macros eagerly expanded by eagerly expanded macros, | ||
7 | //! > which actually happens in practice too!) are resolved at the location of the "root" macro | ||
8 | //! > that performs the eager expansion on its arguments. | ||
9 | //! > If some name cannot be resolved at the eager expansion time it's considered unresolved, | ||
10 | //! > even if becomes available later (e.g. from a glob import or other macro). | ||
11 | //! | ||
12 | //! > Eagerly expanded macros don't add anything to the module structure of the crate and | ||
13 | //! > don't build any speculative module structures, i.e. they are expanded in a "flat" | ||
14 | //! > way even if tokens in them look like modules. | ||
15 | //! | ||
16 | //! > In other words, it kinda works for simple cases for which it was originally intended, | ||
17 | //! > and we need to live with it because it's available on stable and widely relied upon. | ||
18 | //! | ||
19 | //! | ||
20 | //! See the full discussion : https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros | ||
21 | |||
22 | use crate::{ | ||
23 | ast::{self, AstNode}, | ||
24 | db::AstDatabase, | ||
25 | EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | ||
26 | }; | ||
27 | |||
28 | use base_db::CrateId; | ||
29 | use parser::FragmentKind; | ||
30 | use std::sync::Arc; | ||
31 | use syntax::{algo::SyntaxRewriter, SyntaxNode}; | ||
32 | |||
33 | pub fn expand_eager_macro( | ||
34 | db: &dyn AstDatabase, | ||
35 | krate: CrateId, | ||
36 | macro_call: InFile<ast::MacroCall>, | ||
37 | def: MacroDefId, | ||
38 | resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, | ||
39 | ) -> Option<EagerMacroId> { | ||
40 | let args = macro_call.value.token_tree()?; | ||
41 | let parsed_args = mbe::ast_to_token_tree(&args)?.0; | ||
42 | |||
43 | // Note: | ||
44 | // When `lazy_expand` is called, its *parent* file must be already exists. | ||
45 | // Here we store an eager macro id for the argument expanded subtree here | ||
46 | // for that purpose. | ||
47 | let arg_id = db.intern_eager_expansion({ | ||
48 | EagerCallLoc { | ||
49 | def, | ||
50 | fragment: FragmentKind::Expr, | ||
51 | subtree: Arc::new(parsed_args.clone()), | ||
52 | krate, | ||
53 | file_id: macro_call.file_id, | ||
54 | } | ||
55 | }); | ||
56 | let arg_file_id: MacroCallId = arg_id.into(); | ||
57 | |||
58 | let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr).ok()?.0; | ||
59 | let result = eager_macro_recur( | ||
60 | db, | ||
61 | InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()), | ||
62 | krate, | ||
63 | resolver, | ||
64 | )?; | ||
65 | let subtree = to_subtree(&result)?; | ||
66 | |||
67 | if let MacroDefKind::BuiltInEager(eager) = def.kind { | ||
68 | let (subtree, fragment) = eager.expand(db, arg_id, &subtree).ok()?; | ||
69 | let eager = EagerCallLoc { | ||
70 | def, | ||
71 | fragment, | ||
72 | subtree: Arc::new(subtree), | ||
73 | krate, | ||
74 | file_id: macro_call.file_id, | ||
75 | }; | ||
76 | |||
77 | Some(db.intern_eager_expansion(eager)) | ||
78 | } else { | ||
79 | None | ||
80 | } | ||
81 | } | ||
82 | |||
83 | fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> { | ||
84 | let mut subtree = mbe::syntax_node_to_token_tree(node)?.0; | ||
85 | subtree.delimiter = None; | ||
86 | Some(subtree) | ||
87 | } | ||
88 | |||
89 | fn lazy_expand( | ||
90 | db: &dyn AstDatabase, | ||
91 | def: &MacroDefId, | ||
92 | macro_call: InFile<ast::MacroCall>, | ||
93 | krate: CrateId, | ||
94 | ) -> Option<InFile<SyntaxNode>> { | ||
95 | let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); | ||
96 | |||
97 | let id: MacroCallId = | ||
98 | def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into(); | ||
99 | |||
100 | db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node)) | ||
101 | } | ||
102 | |||
103 | fn eager_macro_recur( | ||
104 | db: &dyn AstDatabase, | ||
105 | curr: InFile<SyntaxNode>, | ||
106 | krate: CrateId, | ||
107 | macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, | ||
108 | ) -> Option<SyntaxNode> { | ||
109 | let original = curr.value.clone(); | ||
110 | |||
111 | let children = curr.value.descendants().filter_map(ast::MacroCall::cast); | ||
112 | let mut rewriter = SyntaxRewriter::default(); | ||
113 | |||
114 | // Collect replacement | ||
115 | for child in children { | ||
116 | let def: MacroDefId = macro_resolver(child.path()?)?; | ||
117 | let insert = match def.kind { | ||
118 | MacroDefKind::BuiltInEager(_) => { | ||
119 | let id: MacroCallId = expand_eager_macro( | ||
120 | db, | ||
121 | krate, | ||
122 | curr.with_value(child.clone()), | ||
123 | def, | ||
124 | macro_resolver, | ||
125 | )? | ||
126 | .into(); | ||
127 | db.parse_or_expand(id.as_file())? | ||
128 | } | ||
129 | MacroDefKind::Declarative | ||
130 | | MacroDefKind::BuiltIn(_) | ||
131 | | MacroDefKind::BuiltInDerive(_) | ||
132 | | MacroDefKind::CustomDerive(_) => { | ||
133 | let expanded = lazy_expand(db, &def, curr.with_value(child.clone()), krate)?; | ||
134 | // replace macro inside | ||
135 | eager_macro_recur(db, expanded, krate, macro_resolver)? | ||
136 | } | ||
137 | }; | ||
138 | |||
139 | rewriter.replace(child.syntax(), &insert); | ||
140 | } | ||
141 | |||
142 | let res = rewriter.rewrite(&original); | ||
143 | Some(res) | ||
144 | } | ||