aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-23 14:48:34 +0000
committerEdwin Cheng <[email protected]>2019-11-23 14:48:34 +0000
commit16854e28ef1f2010d7d7d8e959a193fbcaeb5be8 (patch)
tree81ffc7b06d5f178abb6db94e1b12844254efc421 /crates/ra_hir_expand/src
parent6940ae9eab9ec8ea5fe81c8b30ccada4a62aebde (diff)
Use macro for all the things
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs79
1 files changed, 31 insertions, 48 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index a0069682d..c0e0436c0 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -8,29 +8,39 @@ use crate::{
8 8
9use crate::quote; 9use crate::quote;
10 10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum BuiltinFnLikeExpander {
13 Column,
14 File,
15 Line,
16 Stringify,
17}
18
19struct BuiltInMacroInfo {
20 name: name::Name,
21 kind: BuiltinFnLikeExpander,
22 expand: fn(
23 db: &dyn AstDatabase,
24 id: MacroCallId,
25 _tt: &tt::Subtree,
26 ) -> Result<tt::Subtree, mbe::ExpandError>,
27}
28
29macro_rules! register_builtin { 11macro_rules! register_builtin {
30 ( $(($name:ident, $kind: ident) => $expand:ident),* ) => { 12 ( $(($name:ident, $kind: ident) => $expand:ident),* ) => {
31 const BUILTIN_MACROS: &[BuiltInMacroInfo] = &[ 13 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32 $(BuiltInMacroInfo { name: name::$name, kind: BuiltinFnLikeExpander::$kind, expand: $expand }),* 14 pub enum BuiltinFnLikeExpander {
33 ]; 15 $($kind),*
16 }
17
18 impl BuiltinFnLikeExpander {
19 pub fn expand(
20 &self,
21 db: &dyn AstDatabase,
22 id: MacroCallId,
23 tt: &tt::Subtree,
24 ) -> Result<tt::Subtree, mbe::ExpandError> {
25 let expander = match *self {
26 $( BuiltinFnLikeExpander::$kind => $expand, )*
27 };
28 expander(db, id, tt)
29 }
30 }
31
32 pub fn find_builtin_macro(
33 ident: &name::Name,
34 krate: CrateId,
35 ast_id: AstId<ast::MacroCall>,
36 ) -> Option<MacroDefId> {
37 let kind = match ident {
38 $( id if id == &name::$name => BuiltinFnLikeExpander::$kind, )*
39 _ => return None,
40 };
41
42 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(kind) })
43 }
34 }; 44 };
35} 45}
36 46
@@ -41,33 +51,6 @@ register_builtin! {
41 (STRINGIFY_MACRO, Stringify) => stringify_expand 51 (STRINGIFY_MACRO, Stringify) => stringify_expand
42} 52}
43 53
44impl BuiltinFnLikeExpander {
45 pub fn expand(
46 &self,
47 db: &dyn AstDatabase,
48 id: MacroCallId,
49 tt: &tt::Subtree,
50 ) -> Result<tt::Subtree, mbe::ExpandError> {
51 let expander = BUILTIN_MACROS
52 .iter()
53 .find(|it| *self == it.kind)
54 .map(|it| it.expand)
55 .ok_or_else(|| mbe::ExpandError::ConversionError)?;
56
57 expander(db, id, tt)
58 }
59}
60
61pub fn find_builtin_macro(
62 ident: &name::Name,
63 krate: CrateId,
64 ast_id: AstId<ast::MacroCall>,
65) -> Option<MacroDefId> {
66 let kind = BUILTIN_MACROS.iter().find(|it| *ident == it.name).map(|it| it.kind)?;
67
68 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(kind) })
69}
70
71fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize { 54fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
72 // FIXME: Use expansion info 55 // FIXME: Use expansion info
73 let file_id = file.original_file(db); 56 let file_id = file.original_file(db);