5 use ra_parser::FragmentKind;
7 ast::{self, AstNode, ModuleItemOwner, NameOwner, TypeParamsOwner},
11 use crate::db::AstDatabase;
12 use crate::{name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
14 macro_rules! register_builtin {
15 ( $($trait:ident => $expand:ident),* ) => {
16 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17 pub enum BuiltinDeriveExpander {
21 impl BuiltinDeriveExpander {
27 ) -> Result<tt::Subtree, mbe::ExpandError> {
28 let expander = match *self {
29 $( BuiltinDeriveExpander::$trait => $expand, )*
35 pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> {
36 let kind = match ident {
37 $( id if id == &name::name![$trait] => BuiltinDeriveExpander::$trait, )*
41 Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind) })
48 Clone => clone_expand,
49 Default => default_expand,
50 Debug => debug_expand,
53 PartialOrd => partial_ord_expand,
55 PartialEq => partial_eq_expand
63 fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> {
64 let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, FragmentKind::Items)?; // FragmentKind::Items doesn't parse attrs?
65 let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
66 debug!("derive node didn't parse");
67 mbe::ExpandError::UnexpectedToken
69 let item = macro_items.items().next().ok_or_else(|| {
70 debug!("no module item parsed");
71 mbe::ExpandError::NoMatchingRule
73 let node = item.syntax();
74 let (name, params) = match_ast! {
76 ast::StructDef(it) => (it.name(), it.type_param_list()),
77 ast::EnumDef(it) => (it.name(), it.type_param_list()),
78 ast::UnionDef(it) => (it.name(), it.type_param_list()),
80 debug!("unexpected node is {:?}", node);
81 return Err(mbe::ExpandError::ConversionError)
85 let name = name.ok_or_else(|| {
86 debug!("parsed item has no name");
87 mbe::ExpandError::NoMatchingRule
89 let name_token_id = token_map.token_by_range(name.syntax().text_range()).ok_or_else(|| {
90 debug!("name token not found");
91 mbe::ExpandError::ConversionError
93 let name_token = tt::Ident { id: name_token_id, text: name.text().clone() };
94 let type_params = params.map_or(0, |type_param_list| type_param_list.type_params().count());
95 Ok(BasicAdtInfo { name: name_token, type_params })
98 fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
99 let mut result = Vec::<tt::TokenTree>::new();
101 tt::Leaf::Punct(tt::Punct {
103 spacing: tt::Spacing::Alone,
104 id: tt::TokenId::unspecified(),
111 tt::Leaf::Punct(tt::Punct {
113 spacing: tt::Spacing::Alone,
114 id: tt::TokenId::unspecified(),
120 tt::Leaf::Ident(tt::Ident {
121 id: tt::TokenId::unspecified(),
122 text: format!("T{}", i).into(),
126 result.extend(bound.iter().cloned());
129 tt::Leaf::Punct(tt::Punct {
131 spacing: tt::Spacing::Alone,
132 id: tt::TokenId::unspecified(),
139 fn expand_simple_derive(
141 trait_path: tt::Subtree,
142 ) -> Result<tt::Subtree, mbe::ExpandError> {
143 let info = parse_adt(tt)?;
144 let name = info.name;
145 let trait_path_clone = trait_path.token_trees.clone();
146 let bound = (quote! { : ##trait_path_clone }).token_trees;
147 let type_params = make_type_args(info.type_params, bound);
148 let type_args = make_type_args(info.type_params, Vec::new());
149 let trait_path = trait_path.token_trees;
150 let expanded = quote! {
151 impl ##type_params ##trait_path for #name ##type_args {}
156 fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
157 // FIXME: make hygiene works for builtin derive macro
158 // such that $crate can be used here.
160 let m: MacroCallId = id.into();
161 let file_id = m.as_file().original_file(db);
162 let cg = db.crate_graph();
163 let krates = db.relevant_crates(file_id);
164 let krate = match krates.get(0) {
165 Some(krate) => krate,
167 let tt = quote! { core };
168 return tt.token_trees[0].clone();
173 // All crates except core itself should have a dependency on core,
174 // We detect `core` by seeing whether it doesn't have such a dependency.
175 let tt = if cg[*krate].dependencies.iter().any(|dep| dep.name == "core") {
181 tt.token_trees[0].clone()
185 db: &dyn AstDatabase,
188 ) -> Result<tt::Subtree, mbe::ExpandError> {
189 let krate = find_builtin_crate(db, id);
190 expand_simple_derive(tt, quote! { #krate::marker::Copy })
194 db: &dyn AstDatabase,
197 ) -> Result<tt::Subtree, mbe::ExpandError> {
198 let krate = find_builtin_crate(db, id);
199 expand_simple_derive(tt, quote! { #krate::clone::Clone })
203 db: &dyn AstDatabase,
206 ) -> Result<tt::Subtree, mbe::ExpandError> {
207 let krate = find_builtin_crate(db, id);
208 expand_simple_derive(tt, quote! { #krate::default::Default })
212 db: &dyn AstDatabase,
215 ) -> Result<tt::Subtree, mbe::ExpandError> {
216 let krate = find_builtin_crate(db, id);
217 expand_simple_derive(tt, quote! { #krate::fmt::Debug })
221 db: &dyn AstDatabase,
224 ) -> Result<tt::Subtree, mbe::ExpandError> {
225 let krate = find_builtin_crate(db, id);
226 expand_simple_derive(tt, quote! { #krate::hash::Hash })
230 db: &dyn AstDatabase,
233 ) -> Result<tt::Subtree, mbe::ExpandError> {
234 let krate = find_builtin_crate(db, id);
235 expand_simple_derive(tt, quote! { #krate::cmp::Eq })
238 fn partial_eq_expand(
239 db: &dyn AstDatabase,
242 ) -> Result<tt::Subtree, mbe::ExpandError> {
243 let krate = find_builtin_crate(db, id);
244 expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
248 db: &dyn AstDatabase,
251 ) -> Result<tt::Subtree, mbe::ExpandError> {
252 let krate = find_builtin_crate(db, id);
253 expand_simple_derive(tt, quote! { #krate::cmp::Ord })
256 fn partial_ord_expand(
257 db: &dyn AstDatabase,
260 ) -> Result<tt::Subtree, mbe::ExpandError> {
261 let krate = find_builtin_crate(db, id);
262 expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
268 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
269 use name::{known, Name};
270 use ra_db::{fixture::WithFixture, SourceDatabase};
272 fn expand_builtin_derive(s: &str, name: Name) -> String {
273 let def = find_builtin_derive(&name).unwrap();
274 let fixture = format!(
275 r#"//- /main.rs crate:main deps:core
278 //- /lib.rs crate:core
284 let (db, file_pos) = TestDB::with_position(&fixture);
285 let file_id = file_pos.file_id;
286 let parsed = db.parse(file_id);
288 parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect();
290 let ast_id_map = db.ast_id_map(file_id.into());
292 let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
294 let loc = MacroCallLoc { def, kind: MacroCallKind::Attr(attr_id, name.to_string()) };
296 let id: MacroCallId = db.intern_macro(loc).into();
297 let parsed = db.parse_or_expand(id.as_file()).unwrap();
299 // FIXME text() for syntax nodes parsed from token tree looks weird
300 // because there's no whitespace, see below
301 parsed.text().to_string()
305 fn test_copy_expand_simple() {
306 let expanded = expand_builtin_derive(
314 assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}");
318 fn test_copy_expand_with_type_params() {
319 let expanded = expand_builtin_derive(
329 "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
334 fn test_copy_expand_with_lifetimes() {
335 let expanded = expand_builtin_derive(
338 struct Foo<A, B, 'a, 'b>;
343 // We currently just ignore lifetimes
347 "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
352 fn test_clone_expand() {
353 let expanded = expand_builtin_derive(
363 "impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"