]> git.lizzy.rs Git - rust.git/blob - crates/hir_expand/src/builtin_attr_macro.rs
Merge #10699
[rust.git] / crates / hir_expand / src / builtin_attr_macro.rs
1 //! Builtin attributes.
2
3 use mbe::ExpandResult;
4 use syntax::ast;
5
6 use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
7
8 macro_rules! register_builtin {
9     ( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
10         #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11         pub enum BuiltinAttrExpander {
12             $($variant),*
13         }
14
15         impl BuiltinAttrExpander {
16             pub fn expand(
17                 &self,
18                 db: &dyn AstDatabase,
19                 id: MacroCallId,
20                 tt: &tt::Subtree,
21             ) -> ExpandResult<tt::Subtree> {
22                 let expander = match *self {
23                     $( BuiltinAttrExpander::$variant => $expand, )*
24                 };
25                 expander(db, id, tt)
26             }
27
28             fn find_by_name(name: &name::Name) -> Option<Self> {
29                 match name {
30                     $( id if id == &name::name![$name] => Some(BuiltinAttrExpander::$variant), )*
31                      _ => None,
32                 }
33             }
34         }
35
36     };
37 }
38
39 register_builtin! {
40     (bench, Bench) => dummy_attr_expand,
41     (cfg_accessible, CfgAccessible) => dummy_attr_expand,
42     (cfg_eval, CfgEval) => dummy_attr_expand,
43     (derive, Derive) => dummy_attr_expand,
44     (global_allocator, GlobalAllocator) => dummy_attr_expand,
45     (test, Test) => dummy_attr_expand,
46     (test_case, TestCase) => dummy_attr_expand
47 }
48
49 pub fn find_builtin_attr(
50     ident: &name::Name,
51     krate: CrateId,
52     ast_id: AstId<ast::Macro>,
53 ) -> Option<MacroDefId> {
54     let expander = BuiltinAttrExpander::find_by_name(ident)?;
55     Some(MacroDefId {
56         krate,
57         kind: MacroDefKind::BuiltInAttr(expander, ast_id),
58         local_inner: false,
59     })
60 }
61
62 fn dummy_attr_expand(
63     _db: &dyn AstDatabase,
64     _id: MacroCallId,
65     tt: &tt::Subtree,
66 ) -> ExpandResult<tt::Subtree> {
67     ExpandResult::ok(tt.clone())
68 }