2019-12-05 08:10:33 -06:00
|
|
|
//! Builtin derives.
|
|
|
|
|
2022-05-20 09:42:28 -05:00
|
|
|
use base_db::{CrateOrigin, LangCrateOrigin};
|
2021-08-15 07:46:13 -05:00
|
|
|
use tracing::debug;
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName},
|
2019-12-05 12:29:57 -06:00
|
|
|
match_ast,
|
|
|
|
};
|
2022-02-21 12:14:06 -06:00
|
|
|
use tt::TokenId;
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2022-03-08 14:41:19 -06:00
|
|
|
use crate::{db::AstDatabase, name, quote, ExpandError, ExpandResult, MacroCallId};
|
2019-12-05 08:10:33 -06:00
|
|
|
|
|
|
|
macro_rules! register_builtin {
|
2019-12-13 14:43:53 -06:00
|
|
|
( $($trait:ident => $expand:ident),* ) => {
|
2019-12-05 08:10:33 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum BuiltinDeriveExpander {
|
2019-12-13 14:43:53 -06:00
|
|
|
$($trait),*
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltinDeriveExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 08:10:33 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2019-12-05 08:10:33 -06:00
|
|
|
let expander = match *self {
|
2019-12-13 14:43:53 -06:00
|
|
|
$( BuiltinDeriveExpander::$trait => $expand, )*
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
|
|
|
expander(db, id, tt)
|
|
|
|
}
|
|
|
|
|
2020-12-15 13:33:05 -06:00
|
|
|
fn find_by_name(name: &name::Name) -> Option<Self> {
|
|
|
|
match name {
|
|
|
|
$( id if id == &name::name![$trait] => Some(BuiltinDeriveExpander::$trait), )*
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
2020-12-15 13:33:05 -06:00
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
register_builtin! {
|
2019-12-13 14:43:53 -06:00
|
|
|
Copy => copy_expand,
|
|
|
|
Clone => clone_expand,
|
|
|
|
Default => default_expand,
|
|
|
|
Debug => debug_expand,
|
|
|
|
Hash => hash_expand,
|
|
|
|
Ord => ord_expand,
|
|
|
|
PartialOrd => partial_ord_expand,
|
|
|
|
Eq => eq_expand,
|
|
|
|
PartialEq => partial_eq_expand
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:41:19 -06:00
|
|
|
pub fn find_builtin_derive(ident: &name::Name) -> Option<BuiltinDeriveExpander> {
|
|
|
|
BuiltinDeriveExpander::find_by_name(ident)
|
2020-12-15 13:33:05 -06:00
|
|
|
}
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
struct BasicAdtInfo {
|
|
|
|
name: tt::Ident,
|
2021-12-29 07:35:59 -06:00
|
|
|
type_or_const_params: usize,
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
|
2022-02-21 12:14:06 -06:00
|
|
|
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
|
|
|
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems);
|
2019-12-05 12:29:57 -06:00
|
|
|
let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
|
|
|
|
debug!("derive node didn't parse");
|
2022-02-21 12:14:06 -06:00
|
|
|
ExpandError::Other("invalid item definition".into())
|
2019-12-05 12:29:57 -06:00
|
|
|
})?;
|
|
|
|
let item = macro_items.items().next().ok_or_else(|| {
|
|
|
|
debug!("no module item parsed");
|
2022-02-21 12:14:06 -06:00
|
|
|
ExpandError::Other("no item found".into())
|
2019-12-05 12:29:57 -06:00
|
|
|
})?;
|
|
|
|
let node = item.syntax();
|
|
|
|
let (name, params) = match_ast! {
|
|
|
|
match node {
|
2020-07-30 10:50:40 -05:00
|
|
|
ast::Struct(it) => (it.name(), it.generic_param_list()),
|
2020-07-30 10:52:53 -05:00
|
|
|
ast::Enum(it) => (it.name(), it.generic_param_list()),
|
2020-07-30 10:36:46 -05:00
|
|
|
ast::Union(it) => (it.name(), it.generic_param_list()),
|
2019-12-05 12:29:57 -06:00
|
|
|
_ => {
|
|
|
|
debug!("unexpected node is {:?}", node);
|
2022-02-21 12:14:06 -06:00
|
|
|
return Err(ExpandError::Other("expected struct, enum or union".into()))
|
2019-12-05 12:29:57 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = name.ok_or_else(|| {
|
|
|
|
debug!("parsed item has no name");
|
2022-02-21 12:14:06 -06:00
|
|
|
ExpandError::Other("missing name".into())
|
2019-12-05 12:29:57 -06:00
|
|
|
})?;
|
2022-03-12 06:35:31 -06:00
|
|
|
let name_token_id =
|
|
|
|
token_map.token_by_range(name.syntax().text_range()).unwrap_or_else(TokenId::unspecified);
|
2021-01-19 16:56:11 -06:00
|
|
|
let name_token = tt::Ident { id: name_token_id, text: name.text().into() };
|
2021-12-29 07:35:59 -06:00
|
|
|
let type_or_const_params =
|
|
|
|
params.map_or(0, |type_param_list| type_param_list.type_or_const_params().count());
|
|
|
|
Ok(BasicAdtInfo { name: name_token, type_or_const_params })
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
|
2021-03-15 04:15:08 -05:00
|
|
|
let mut result = Vec::<tt::TokenTree>::with_capacity(n * 2);
|
2019-12-12 07:47:54 -06:00
|
|
|
result.push(
|
|
|
|
tt::Leaf::Punct(tt::Punct {
|
|
|
|
char: '<',
|
|
|
|
spacing: tt::Spacing::Alone,
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
2019-12-05 12:29:57 -06:00
|
|
|
for i in 0..n {
|
|
|
|
if i > 0 {
|
2019-12-12 07:47:54 -06:00
|
|
|
result.push(
|
|
|
|
tt::Leaf::Punct(tt::Punct {
|
|
|
|
char: ',',
|
|
|
|
spacing: tt::Spacing::Alone,
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
result.push(
|
|
|
|
tt::Leaf::Ident(tt::Ident {
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
text: format!("T{}", i).into(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
result.extend(bound.iter().cloned());
|
|
|
|
}
|
2019-12-12 07:47:54 -06:00
|
|
|
result.push(
|
|
|
|
tt::Leaf::Punct(tt::Punct {
|
|
|
|
char: '>',
|
|
|
|
spacing: tt::Spacing::Alone,
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
2019-12-05 12:29:57 -06:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-08-20 07:28:36 -05:00
|
|
|
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
|
|
|
|
let info = match parse_adt(tt) {
|
|
|
|
Ok(info) => info,
|
|
|
|
Err(e) => return ExpandResult::only_err(e),
|
|
|
|
};
|
2019-12-05 12:29:57 -06:00
|
|
|
let name = info.name;
|
2019-12-05 12:52:52 -06:00
|
|
|
let trait_path_clone = trait_path.token_trees.clone();
|
|
|
|
let bound = (quote! { : ##trait_path_clone }).token_trees;
|
2021-12-29 07:35:59 -06:00
|
|
|
let type_params = make_type_args(info.type_or_const_params, bound);
|
|
|
|
let type_args = make_type_args(info.type_or_const_params, Vec::new());
|
2019-12-05 12:52:52 -06:00
|
|
|
let trait_path = trait_path.token_trees;
|
2019-12-05 08:10:33 -06:00
|
|
|
let expanded = quote! {
|
2019-12-05 12:52:52 -06:00
|
|
|
impl ##type_params ##trait_path for #name ##type_args {}
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
2021-08-20 07:28:36 -05:00
|
|
|
ExpandResult::ok(expanded)
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:19:08 -05:00
|
|
|
fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree {
|
2020-04-27 12:48:55 -05:00
|
|
|
// FIXME: make hygiene works for builtin derive macro
|
|
|
|
// such that $crate can be used here.
|
|
|
|
let cg = db.crate_graph();
|
2021-11-14 09:25:40 -06:00
|
|
|
let krate = db.lookup_intern_macro_call(id).krate;
|
2020-04-27 12:48:55 -05:00
|
|
|
|
2022-05-20 09:42:28 -05:00
|
|
|
let tt = if matches!(cg[krate].origin, CrateOrigin::Lang(LangCrateOrigin::Core)) {
|
2021-10-10 08:13:45 -05:00
|
|
|
cov_mark::hit!(test_copy_expand_in_core);
|
2020-04-27 15:11:24 -05:00
|
|
|
quote! { crate }
|
2022-05-20 09:42:28 -05:00
|
|
|
} else {
|
|
|
|
quote! { core }
|
2020-04-27 12:48:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tt.token_trees[0].clone()
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:52:52 -06:00
|
|
|
fn copy_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::marker::Copy })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
fn clone_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:29:57 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::clone::Clone })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::default::Default })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn debug_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::fmt::Debug })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::hash::Hash })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
2021-08-20 07:28:36 -05:00
|
|
|
fn eq_expand(db: &dyn AstDatabase, id: MacroCallId, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::Eq })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_eq_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ord_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::Ord })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_ord_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|