aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/builtin_attr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src/builtin_attr.rs')
-rw-r--r--crates/hir_expand/src/builtin_attr.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/crates/hir_expand/src/builtin_attr.rs b/crates/hir_expand/src/builtin_attr.rs
new file mode 100644
index 000000000..c8432005e
--- /dev/null
+++ b/crates/hir_expand/src/builtin_attr.rs
@@ -0,0 +1,67 @@
1//! Builtin derives.
2
3use syntax::ast;
4
5use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
6
7macro_rules! register_builtin {
8 ( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
9 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10 pub enum BuiltinAttrExpander {
11 $($variant),*
12 }
13
14 impl BuiltinAttrExpander {
15 pub fn expand(
16 &self,
17 db: &dyn AstDatabase,
18 id: MacroCallId,
19 tt: &tt::Subtree,
20 ) -> Result<tt::Subtree, mbe::ExpandError> {
21 let expander = match *self {
22 $( BuiltinAttrExpander::$variant => $expand, )*
23 };
24 expander(db, id, tt)
25 }
26
27 fn find_by_name(name: &name::Name) -> Option<Self> {
28 match name {
29 $( id if id == &name::name![$name] => Some(BuiltinAttrExpander::$variant), )*
30 _ => None,
31 }
32 }
33 }
34
35 };
36}
37
38register_builtin! {
39 (bench, Bench) => dummy_attr_expand,
40 (cfg_accessible, CfgAccessible) => dummy_attr_expand,
41 (cfg_eval, CfgEval) => dummy_attr_expand,
42 (derive, Derive) => dummy_attr_expand,
43 (global_allocator, GlobalAllocator) => dummy_attr_expand,
44 (test, Test) => dummy_attr_expand,
45 (test_case, TestCase) => dummy_attr_expand
46}
47
48pub fn find_builtin_attr(
49 ident: &name::Name,
50 krate: CrateId,
51 ast_id: AstId<ast::Macro>,
52) -> Option<MacroDefId> {
53 let expander = BuiltinAttrExpander::find_by_name(ident)?;
54 Some(MacroDefId {
55 krate,
56 kind: MacroDefKind::BuiltInAttr(expander, ast_id),
57 local_inner: false,
58 })
59}
60
61fn dummy_attr_expand(
62 _db: &dyn AstDatabase,
63 _id: MacroCallId,
64 tt: &tt::Subtree,
65) -> Result<tt::Subtree, mbe::ExpandError> {
66 Ok(tt.clone())
67}