2021-06-28 14:34:30 -05:00
|
|
|
//! Builtin attributes.
|
2021-06-09 11:02:31 -05:00
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
|
2022-02-21 12:14:06 -06:00
|
|
|
use crate::{
|
|
|
|
db::AstDatabase, name, AstId, CrateId, ExpandResult, MacroCallId, MacroDefId, MacroDefKind,
|
|
|
|
};
|
2021-06-09 11:02:31 -05:00
|
|
|
|
|
|
|
macro_rules! register_builtin {
|
|
|
|
( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum BuiltinAttrExpander {
|
|
|
|
$($variant),*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltinAttrExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: MacroCallId,
|
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:34:46 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2021-06-09 11:02:31 -05:00
|
|
|
let expander = match *self {
|
|
|
|
$( BuiltinAttrExpander::$variant => $expand, )*
|
|
|
|
};
|
2021-08-21 05:55:05 -05:00
|
|
|
expander(db, id, tt)
|
2021-06-09 11:02:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find_by_name(name: &name::Name) -> Option<Self> {
|
|
|
|
match name {
|
|
|
|
$( id if id == &name::name![$name] => Some(BuiltinAttrExpander::$variant), )*
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-17 12:46:32 -06:00
|
|
|
impl BuiltinAttrExpander {
|
|
|
|
pub fn is_derive(self) -> bool {
|
|
|
|
matches!(self, BuiltinAttrExpander::Derive)
|
|
|
|
}
|
|
|
|
pub fn is_test(self) -> bool {
|
|
|
|
matches!(self, BuiltinAttrExpander::Test)
|
|
|
|
}
|
|
|
|
pub fn is_bench(self) -> bool {
|
|
|
|
matches!(self, BuiltinAttrExpander::Bench)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 11:02:31 -05:00
|
|
|
register_builtin! {
|
2021-06-09 11:42:01 -05:00
|
|
|
(bench, Bench) => dummy_attr_expand,
|
|
|
|
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
|
|
|
|
(cfg_eval, CfgEval) => dummy_attr_expand,
|
|
|
|
(derive, Derive) => dummy_attr_expand,
|
|
|
|
(global_allocator, GlobalAllocator) => dummy_attr_expand,
|
|
|
|
(test, Test) => dummy_attr_expand,
|
|
|
|
(test_case, TestCase) => dummy_attr_expand
|
2021-06-09 11:02:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_builtin_attr(
|
|
|
|
ident: &name::Name,
|
|
|
|
krate: CrateId,
|
|
|
|
ast_id: AstId<ast::Macro>,
|
|
|
|
) -> Option<MacroDefId> {
|
|
|
|
let expander = BuiltinAttrExpander::find_by_name(ident)?;
|
|
|
|
Some(MacroDefId {
|
|
|
|
krate,
|
|
|
|
kind: MacroDefKind::BuiltInAttr(expander, ast_id),
|
|
|
|
local_inner: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-09 11:42:01 -05:00
|
|
|
fn dummy_attr_expand(
|
2021-06-09 11:02:31 -05:00
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_id: MacroCallId,
|
2021-08-21 05:55:05 -05:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:34:46 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2021-08-21 05:55:05 -05:00
|
|
|
ExpandResult::ok(tt.clone())
|
2021-06-09 11:02:31 -05:00
|
|
|
}
|