6 ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName},
12 db::AstDatabase, name, quote, AstId, CrateId, ExpandError, ExpandResult, MacroCallId,
13 MacroDefId, MacroDefKind,
16 macro_rules! register_builtin {
17 ( $($trait:ident => $expand:ident),* ) => {
18 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19 pub enum BuiltinDeriveExpander {
23 impl BuiltinDeriveExpander {
29 ) -> ExpandResult<tt::Subtree> {
30 let expander = match *self {
31 $( BuiltinDeriveExpander::$trait => $expand, )*
36 fn find_by_name(name: &name::Name) -> Option<Self> {
38 $( id if id == &name::name![$trait] => Some(BuiltinDeriveExpander::$trait), )*
49 Clone => clone_expand,
50 Default => default_expand,
51 Debug => debug_expand,
54 PartialOrd => partial_ord_expand,
56 PartialEq => partial_eq_expand
59 pub fn find_builtin_derive(
62 ast_id: AstId<ast::Macro>,
63 ) -> Option<MacroDefId> {
64 let expander = BuiltinDeriveExpander::find_by_name(ident)?;
67 kind: MacroDefKind::BuiltInDerive(expander, ast_id),
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())
83 let item = macro_items.items().next().ok_or_else(|| {
84 debug!("no module item parsed");
85 ExpandError::Other("no item found".into())
87 let node = item.syntax();
88 let (name, params) = match_ast! {
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()),
94 debug!("unexpected node is {:?}", node);
95 return Err(ExpandError::Other("expected struct, enum or union".into()))
99 let name = name.ok_or_else(|| {
100 debug!("parsed item has no name");
101 ExpandError::Other("missing name".into())
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 })
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);
114 tt::Leaf::Punct(tt::Punct {
116 spacing: tt::Spacing::Alone,
117 id: tt::TokenId::unspecified(),
124 tt::Leaf::Punct(tt::Punct {
126 spacing: tt::Spacing::Alone,
127 id: tt::TokenId::unspecified(),
133 tt::Leaf::Ident(tt::Ident {
134 id: tt::TokenId::unspecified(),
135 text: format!("T{}", i).into(),
139 result.extend(bound.iter().cloned());
142 tt::Leaf::Punct(tt::Punct {
144 spacing: tt::Spacing::Alone,
145 id: tt::TokenId::unspecified(),
152 fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
153 let info = match parse_adt(tt) {
155 Err(e) => return ExpandResult::only_err(e),
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 {}
166 ExpandResult::ok(expanded)
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;
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") {
181 cov_mark::hit!(test_copy_expand_in_core);
185 tt.token_trees[0].clone()
189 db: &dyn AstDatabase,
192 ) -> ExpandResult<tt::Subtree> {
193 let krate = find_builtin_crate(db, id);
194 expand_simple_derive(tt, quote! { #krate::marker::Copy })
198 db: &dyn AstDatabase,
201 ) -> ExpandResult<tt::Subtree> {
202 let krate = find_builtin_crate(db, id);
203 expand_simple_derive(tt, quote! { #krate::clone::Clone })
207 db: &dyn AstDatabase,
210 ) -> ExpandResult<tt::Subtree> {
211 let krate = find_builtin_crate(db, id);
212 expand_simple_derive(tt, quote! { #krate::default::Default })
216 db: &dyn AstDatabase,
219 ) -> ExpandResult<tt::Subtree> {
220 let krate = find_builtin_crate(db, id);
221 expand_simple_derive(tt, quote! { #krate::fmt::Debug })
225 db: &dyn AstDatabase,
228 ) -> ExpandResult<tt::Subtree> {
229 let krate = find_builtin_crate(db, id);
230 expand_simple_derive(tt, quote! { #krate::hash::Hash })
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 })
238 fn partial_eq_expand(
239 db: &dyn AstDatabase,
242 ) -> ExpandResult<tt::Subtree> {
243 let krate = find_builtin_crate(db, id);
244 expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
248 db: &dyn AstDatabase,
251 ) -> ExpandResult<tt::Subtree> {
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 ) -> ExpandResult<tt::Subtree> {
261 let krate = find_builtin_crate(db, id);
262 expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })