]> git.lizzy.rs Git - rust.git/blob - crates/hir_expand/src/builtin_derive_macro.rs
internal: Split unresolve proc-macro error out of mbe
[rust.git] / crates / hir_expand / src / builtin_derive_macro.rs
1 //! Builtin derives.
2
3 use tracing::debug;
4
5 use syntax::{
6     ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName},
7     match_ast,
8 };
9 use tt::TokenId;
10
11 use crate::{
12     db::AstDatabase, name, quote, AstId, CrateId, ExpandError, ExpandResult, MacroCallId,
13     MacroDefId, MacroDefKind,
14 };
15
16 macro_rules! register_builtin {
17     ( $($trait:ident => $expand:ident),* ) => {
18         #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19         pub enum BuiltinDeriveExpander {
20             $($trait),*
21         }
22
23         impl BuiltinDeriveExpander {
24             pub fn expand(
25                 &self,
26                 db: &dyn AstDatabase,
27                 id: MacroCallId,
28                 tt: &tt::Subtree,
29             ) -> ExpandResult<tt::Subtree> {
30                 let expander = match *self {
31                     $( BuiltinDeriveExpander::$trait => $expand, )*
32                 };
33                 expander(db, id, tt)
34             }
35
36             fn find_by_name(name: &name::Name) -> Option<Self> {
37                 match name {
38                     $( id if id == &name::name![$trait] => Some(BuiltinDeriveExpander::$trait), )*
39                      _ => None,
40                 }
41             }
42         }
43
44     };
45 }
46
47 register_builtin! {
48     Copy => copy_expand,
49     Clone => clone_expand,
50     Default => default_expand,
51     Debug => debug_expand,
52     Hash => hash_expand,
53     Ord => ord_expand,
54     PartialOrd => partial_ord_expand,
55     Eq => eq_expand,
56     PartialEq => partial_eq_expand
57 }
58
59 pub fn find_builtin_derive(
60     ident: &name::Name,
61     krate: CrateId,
62     ast_id: AstId<ast::Macro>,
63 ) -> Option<MacroDefId> {
64     let expander = BuiltinDeriveExpander::find_by_name(ident)?;
65     Some(MacroDefId {
66         krate,
67         kind: MacroDefKind::BuiltInDerive(expander, ast_id),
68         local_inner: false,
69     })
70 }
71
72 struct BasicAdtInfo {
73     name: tt::Ident,
74     type_params: usize,
75 }
76
77 fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
78     let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems);
79     let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
80         debug!("derive node didn't parse");
81         ExpandError::Other("invalid item definition".into())
82     })?;
83     let item = macro_items.items().next().ok_or_else(|| {
84         debug!("no module item parsed");
85         ExpandError::Other("no item found".into())
86     })?;
87     let node = item.syntax();
88     let (name, params) = match_ast! {
89         match node {
90             ast::Struct(it) => (it.name(), it.generic_param_list()),
91             ast::Enum(it) => (it.name(), it.generic_param_list()),
92             ast::Union(it) => (it.name(), it.generic_param_list()),
93             _ => {
94                 debug!("unexpected node is {:?}", node);
95                 return Err(ExpandError::Other("expected struct, enum or union".into()))
96             },
97         }
98     };
99     let name = name.ok_or_else(|| {
100         debug!("parsed item has no name");
101         ExpandError::Other("missing name".into())
102     })?;
103     let name_token_id = token_map
104         .token_by_range(name.syntax().text_range())
105         .unwrap_or_else(|| TokenId::unspecified());
106     let name_token = tt::Ident { id: name_token_id, text: name.text().into() };
107     let type_params = params.map_or(0, |type_param_list| type_param_list.type_params().count());
108     Ok(BasicAdtInfo { name: name_token, type_params })
109 }
110
111 fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
112     let mut result = Vec::<tt::TokenTree>::with_capacity(n * 2);
113     result.push(
114         tt::Leaf::Punct(tt::Punct {
115             char: '<',
116             spacing: tt::Spacing::Alone,
117             id: tt::TokenId::unspecified(),
118         })
119         .into(),
120     );
121     for i in 0..n {
122         if i > 0 {
123             result.push(
124                 tt::Leaf::Punct(tt::Punct {
125                     char: ',',
126                     spacing: tt::Spacing::Alone,
127                     id: tt::TokenId::unspecified(),
128                 })
129                 .into(),
130             );
131         }
132         result.push(
133             tt::Leaf::Ident(tt::Ident {
134                 id: tt::TokenId::unspecified(),
135                 text: format!("T{}", i).into(),
136             })
137             .into(),
138         );
139         result.extend(bound.iter().cloned());
140     }
141     result.push(
142         tt::Leaf::Punct(tt::Punct {
143             char: '>',
144             spacing: tt::Spacing::Alone,
145             id: tt::TokenId::unspecified(),
146         })
147         .into(),
148     );
149     result
150 }
151
152 fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
153     let info = match parse_adt(tt) {
154         Ok(info) => info,
155         Err(e) => return ExpandResult::only_err(e),
156     };
157     let name = info.name;
158     let trait_path_clone = trait_path.token_trees.clone();
159     let bound = (quote! { : ##trait_path_clone }).token_trees;
160     let type_params = make_type_args(info.type_params, bound);
161     let type_args = make_type_args(info.type_params, Vec::new());
162     let trait_path = trait_path.token_trees;
163     let expanded = quote! {
164         impl ##type_params ##trait_path for #name ##type_args {}
165     };
166     ExpandResult::ok(expanded)
167 }
168
169 fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree {
170     // FIXME: make hygiene works for builtin derive macro
171     // such that $crate can be used here.
172     let cg = db.crate_graph();
173     let krate = db.lookup_intern_macro_call(id).krate;
174
175     // XXX
176     //  All crates except core itself should have a dependency on core,
177     //  We detect `core` by seeing whether it doesn't have such a dependency.
178     let tt = if cg[krate].dependencies.iter().any(|dep| &*dep.name == "core") {
179         quote! { core }
180     } else {
181         cov_mark::hit!(test_copy_expand_in_core);
182         quote! { crate }
183     };
184
185     tt.token_trees[0].clone()
186 }
187
188 fn copy_expand(
189     db: &dyn AstDatabase,
190     id: MacroCallId,
191     tt: &tt::Subtree,
192 ) -> ExpandResult<tt::Subtree> {
193     let krate = find_builtin_crate(db, id);
194     expand_simple_derive(tt, quote! { #krate::marker::Copy })
195 }
196
197 fn clone_expand(
198     db: &dyn AstDatabase,
199     id: MacroCallId,
200     tt: &tt::Subtree,
201 ) -> ExpandResult<tt::Subtree> {
202     let krate = find_builtin_crate(db, id);
203     expand_simple_derive(tt, quote! { #krate::clone::Clone })
204 }
205
206 fn default_expand(
207     db: &dyn AstDatabase,
208     id: MacroCallId,
209     tt: &tt::Subtree,
210 ) -> ExpandResult<tt::Subtree> {
211     let krate = find_builtin_crate(db, id);
212     expand_simple_derive(tt, quote! { #krate::default::Default })
213 }
214
215 fn debug_expand(
216     db: &dyn AstDatabase,
217     id: MacroCallId,
218     tt: &tt::Subtree,
219 ) -> ExpandResult<tt::Subtree> {
220     let krate = find_builtin_crate(db, id);
221     expand_simple_derive(tt, quote! { #krate::fmt::Debug })
222 }
223
224 fn hash_expand(
225     db: &dyn AstDatabase,
226     id: MacroCallId,
227     tt: &tt::Subtree,
228 ) -> ExpandResult<tt::Subtree> {
229     let krate = find_builtin_crate(db, id);
230     expand_simple_derive(tt, quote! { #krate::hash::Hash })
231 }
232
233 fn eq_expand(db: &dyn AstDatabase, id: MacroCallId, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
234     let krate = find_builtin_crate(db, id);
235     expand_simple_derive(tt, quote! { #krate::cmp::Eq })
236 }
237
238 fn partial_eq_expand(
239     db: &dyn AstDatabase,
240     id: MacroCallId,
241     tt: &tt::Subtree,
242 ) -> ExpandResult<tt::Subtree> {
243     let krate = find_builtin_crate(db, id);
244     expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
245 }
246
247 fn ord_expand(
248     db: &dyn AstDatabase,
249     id: MacroCallId,
250     tt: &tt::Subtree,
251 ) -> ExpandResult<tt::Subtree> {
252     let krate = find_builtin_crate(db, id);
253     expand_simple_derive(tt, quote! { #krate::cmp::Ord })
254 }
255
256 fn partial_ord_expand(
257     db: &dyn AstDatabase,
258     id: MacroCallId,
259     tt: &tt::Subtree,
260 ) -> ExpandResult<tt::Subtree> {
261     let krate = find_builtin_crate(db, id);
262     expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
263 }