mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Add expansion infrastructure for derive macros
This commit is contained in:
parent
4c0bd068da
commit
18f6a995d0
18 changed files with 319 additions and 79 deletions
64
crates/ra_hir_expand/src/builtin_derive.rs
Normal file
64
crates/ra_hir_expand/src/builtin_derive.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
//! Builtin derives.
|
||||
use crate::db::AstDatabase;
|
||||
use crate::{name, MacroCallId, MacroDefId, MacroDefKind};
|
||||
|
||||
use crate::quote;
|
||||
|
||||
macro_rules! register_builtin {
|
||||
( $(($name:ident, $kind: ident) => $expand:ident),* ) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinDeriveExpander {
|
||||
$($kind),*
|
||||
}
|
||||
|
||||
impl BuiltinDeriveExpander {
|
||||
pub fn expand(
|
||||
&self,
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroCallId,
|
||||
tt: &tt::Subtree,
|
||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
let expander = match *self {
|
||||
$( BuiltinDeriveExpander::$kind => $expand, )*
|
||||
};
|
||||
expander(db, id, tt)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> {
|
||||
let kind = match ident {
|
||||
$( id if id == &name::$name => BuiltinDeriveExpander::$kind, )*
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind) })
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
register_builtin! {
|
||||
(COPY_TRAIT, Copy) => copy_expand,
|
||||
(CLONE_TRAIT, Clone) => clone_expand
|
||||
}
|
||||
|
||||
fn copy_expand(
|
||||
_db: &dyn AstDatabase,
|
||||
_id: MacroCallId,
|
||||
_tt: &tt::Subtree,
|
||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
let expanded = quote! {
|
||||
impl Copy for Foo {}
|
||||
};
|
||||
Ok(expanded)
|
||||
}
|
||||
|
||||
fn clone_expand(
|
||||
_db: &dyn AstDatabase,
|
||||
_id: MacroCallId,
|
||||
_tt: &tt::Subtree,
|
||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
let expanded = quote! {
|
||||
impl Clone for Foo {}
|
||||
};
|
||||
Ok(expanded)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue