2019-12-05 08:10:33 -06:00
|
|
|
//! Builtin derives.
|
|
|
|
|
2022-05-20 09:42:28 -05:00
|
|
|
use base_db::{CrateOrigin, LangCrateOrigin};
|
2023-04-06 07:44:00 -05:00
|
|
|
use either::Either;
|
2021-08-15 07:46:13 -05:00
|
|
|
use tracing::debug;
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2023-01-31 04:49:49 -06:00
|
|
|
use crate::tt::{self, TokenId};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2023-04-06 07:44:00 -05:00
|
|
|
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName, HasTypeBounds},
|
2019-12-05 12:29:57 -06:00
|
|
|
match_ast,
|
|
|
|
};
|
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
use crate::{db::ExpandDatabase, 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,
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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,
|
2023-04-06 07:44:00 -05:00
|
|
|
/// first field is the name, and
|
|
|
|
/// second field is `Some(ty)` if it's a const param of type `ty`, `None` if it's a type param.
|
|
|
|
/// third fields is where bounds, if any
|
|
|
|
param_types: Vec<(tt::Subtree, Option<tt::Subtree>, Option<tt::Subtree>)>,
|
|
|
|
field_types: Vec<tt::Subtree>,
|
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();
|
2023-04-06 07:44:00 -05:00
|
|
|
let (name, params, fields) = match_ast! {
|
2019-12-05 12:29:57 -06:00
|
|
|
match node {
|
2023-04-06 07:44:00 -05:00
|
|
|
ast::Struct(it) => {
|
|
|
|
(it.name(), it.generic_param_list(), it.field_list().into_iter().collect::<Vec<_>>())
|
|
|
|
},
|
|
|
|
ast::Enum(it) => (it.name(), it.generic_param_list(), it.variant_list().into_iter().flat_map(|x| x.variants()).filter_map(|x| x.field_list()).collect()),
|
|
|
|
ast::Union(it) => (it.name(), it.generic_param_list(), it.record_field_list().into_iter().map(|x| ast::FieldList::RecordFieldList(x)).collect()),
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
2023-04-06 07:44:00 -05:00
|
|
|
let field_types = fields
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|f| match f {
|
|
|
|
ast::FieldList::RecordFieldList(x) => Either::Left(
|
|
|
|
x.fields()
|
|
|
|
.filter_map(|x| x.ty())
|
|
|
|
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0),
|
|
|
|
),
|
|
|
|
ast::FieldList::TupleFieldList(x) => Either::Right(
|
|
|
|
x.fields()
|
|
|
|
.filter_map(|x| x.ty())
|
|
|
|
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
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);
|
2023-01-31 04:49:49 -06:00
|
|
|
let name_token = tt::Ident { span: name_token_id, text: name.text().into() };
|
2022-10-22 03:49:38 -05:00
|
|
|
let param_types = params
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|param_list| param_list.type_or_const_params())
|
|
|
|
.map(|param| {
|
2023-04-06 07:44:00 -05:00
|
|
|
let name = param
|
|
|
|
.name()
|
|
|
|
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
|
|
|
|
.unwrap_or_else(tt::Subtree::empty);
|
|
|
|
let bounds = match ¶m {
|
|
|
|
ast::TypeOrConstParam::Type(x) => {
|
|
|
|
x.type_bound_list().map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
|
|
|
|
}
|
|
|
|
ast::TypeOrConstParam::Const(_) => None,
|
|
|
|
};
|
|
|
|
let ty = if let ast::TypeOrConstParam::Const(param) = param {
|
2022-10-22 03:49:38 -05:00
|
|
|
let ty = param
|
|
|
|
.ty()
|
|
|
|
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax()).0)
|
2023-01-31 04:49:49 -06:00
|
|
|
.unwrap_or_else(tt::Subtree::empty);
|
2022-10-22 03:49:38 -05:00
|
|
|
Some(ty)
|
|
|
|
} else {
|
|
|
|
None
|
2023-04-06 07:44:00 -05:00
|
|
|
};
|
|
|
|
(name, ty, bounds)
|
2019-12-12 07:47:54 -06:00
|
|
|
})
|
2022-10-22 03:49:38 -05:00
|
|
|
.collect();
|
2023-04-06 07:44:00 -05:00
|
|
|
Ok(BasicAdtInfo { name: name_token, param_types, field_types })
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
|
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,
|
2023-01-31 04:49:49 -06:00
|
|
|
Err(e) => return ExpandResult::with_err(tt::Subtree::empty(), e),
|
2021-08-20 07:28:36 -05:00
|
|
|
};
|
2023-04-06 07:44:00 -05:00
|
|
|
let mut where_block = vec![];
|
2022-10-22 03:49:38 -05:00
|
|
|
let (params, args): (Vec<_>, Vec<_>) = info
|
|
|
|
.param_types
|
|
|
|
.into_iter()
|
2023-04-06 07:44:00 -05:00
|
|
|
.map(|(ident, param_ty, bound)| {
|
2022-10-22 03:49:38 -05:00
|
|
|
let ident_ = ident.clone();
|
2023-04-06 07:44:00 -05:00
|
|
|
if let Some(b) = bound {
|
|
|
|
let ident = ident.clone();
|
|
|
|
where_block.push(quote! { #ident : #b , });
|
|
|
|
}
|
2022-10-22 03:49:38 -05:00
|
|
|
if let Some(ty) = param_ty {
|
|
|
|
(quote! { const #ident : #ty , }, quote! { #ident_ , })
|
|
|
|
} else {
|
|
|
|
let bound = trait_path.clone();
|
|
|
|
(quote! { #ident : #bound , }, quote! { #ident_ , })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unzip();
|
2023-04-06 07:44:00 -05:00
|
|
|
|
|
|
|
where_block.extend(info.field_types.iter().map(|x| {
|
|
|
|
let x = x.clone();
|
|
|
|
let bound = trait_path.clone();
|
|
|
|
quote! { #x : #bound , }
|
|
|
|
}));
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
let name = info.name;
|
2019-12-05 08:10:33 -06:00
|
|
|
let expanded = quote! {
|
2023-04-06 07:44:00 -05:00
|
|
|
impl < ##params > #trait_path for #name < ##args > where ##where_block {}
|
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
|
|
|
}
|
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
fn find_builtin_crate(db: &dyn ExpandDatabase, 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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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
|
|
|
}
|
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
fn eq_expand(
|
|
|
|
db: &dyn ExpandDatabase,
|
|
|
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
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
|
|
|
}
|