diff options
Diffstat (limited to 'crates/hir_expand/src/builtin_attr.rs')
-rw-r--r-- | crates/hir_expand/src/builtin_attr.rs | 115 |
1 files changed, 115 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..3024410fb --- /dev/null +++ b/crates/hir_expand/src/builtin_attr.rs | |||
@@ -0,0 +1,115 @@ | |||
1 | //! Builtin derives. | ||
2 | |||
3 | use syntax::ast; | ||
4 | |||
5 | use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind}; | ||
6 | |||
7 | macro_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 | |||
38 | register_builtin! { | ||
39 | (bench, Bench) => bench_expand, | ||
40 | (cfg_accessible, CfgAccessible) => cfg_accessible_expand, | ||
41 | (cfg_eval, CfgEval) => cfg_eval_expand, | ||
42 | (derive, Derive) => derive_expand, | ||
43 | (global_allocator, GlobalAllocator) => global_allocator_expand, | ||
44 | (test, Test) => test_expand, | ||
45 | (test_case, TestCase) => test_case_expand | ||
46 | } | ||
47 | |||
48 | pub 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 | |||
61 | fn bench_expand( | ||
62 | _db: &dyn AstDatabase, | ||
63 | _id: MacroCallId, | ||
64 | tt: &tt::Subtree, | ||
65 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
66 | Ok(tt.clone()) | ||
67 | } | ||
68 | |||
69 | fn cfg_accessible_expand( | ||
70 | _db: &dyn AstDatabase, | ||
71 | _id: MacroCallId, | ||
72 | tt: &tt::Subtree, | ||
73 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
74 | Ok(tt.clone()) | ||
75 | } | ||
76 | |||
77 | fn cfg_eval_expand( | ||
78 | _db: &dyn AstDatabase, | ||
79 | _id: MacroCallId, | ||
80 | tt: &tt::Subtree, | ||
81 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
82 | Ok(tt.clone()) | ||
83 | } | ||
84 | |||
85 | fn derive_expand( | ||
86 | _db: &dyn AstDatabase, | ||
87 | _id: MacroCallId, | ||
88 | tt: &tt::Subtree, | ||
89 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
90 | Ok(tt.clone()) | ||
91 | } | ||
92 | |||
93 | fn global_allocator_expand( | ||
94 | _db: &dyn AstDatabase, | ||
95 | _id: MacroCallId, | ||
96 | tt: &tt::Subtree, | ||
97 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
98 | Ok(tt.clone()) | ||
99 | } | ||
100 | |||
101 | fn test_expand( | ||
102 | _db: &dyn AstDatabase, | ||
103 | _id: MacroCallId, | ||
104 | tt: &tt::Subtree, | ||
105 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
106 | Ok(tt.clone()) | ||
107 | } | ||
108 | |||
109 | fn test_case_expand( | ||
110 | _db: &dyn AstDatabase, | ||
111 | _id: MacroCallId, | ||
112 | tt: &tt::Subtree, | ||
113 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
114 | Ok(tt.clone()) | ||
115 | } | ||