From 16854e28ef1f2010d7d7d8e959a193fbcaeb5be8 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 23 Nov 2019 22:48:34 +0800 Subject: Use macro for all the things --- crates/ra_hir_expand/src/builtin_macro.rs | 79 ++++++++++++------------------- 1 file changed, 31 insertions(+), 48 deletions(-) (limited to 'crates') 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::{ use crate::quote; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum BuiltinFnLikeExpander { - Column, - File, - Line, - Stringify, -} - -struct BuiltInMacroInfo { - name: name::Name, - kind: BuiltinFnLikeExpander, - expand: fn( - db: &dyn AstDatabase, - id: MacroCallId, - _tt: &tt::Subtree, - ) -> Result, -} - macro_rules! register_builtin { ( $(($name:ident, $kind: ident) => $expand:ident),* ) => { - const BUILTIN_MACROS: &[BuiltInMacroInfo] = &[ - $(BuiltInMacroInfo { name: name::$name, kind: BuiltinFnLikeExpander::$kind, expand: $expand }),* - ]; + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] + pub enum BuiltinFnLikeExpander { + $($kind),* + } + + impl BuiltinFnLikeExpander { + pub fn expand( + &self, + db: &dyn AstDatabase, + id: MacroCallId, + tt: &tt::Subtree, + ) -> Result { + let expander = match *self { + $( BuiltinFnLikeExpander::$kind => $expand, )* + }; + expander(db, id, tt) + } + } + + pub fn find_builtin_macro( + ident: &name::Name, + krate: CrateId, + ast_id: AstId, + ) -> Option { + let kind = match ident { + $( id if id == &name::$name => BuiltinFnLikeExpander::$kind, )* + _ => return None, + }; + + Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(kind) }) + } }; } @@ -41,33 +51,6 @@ register_builtin! { (STRINGIFY_MACRO, Stringify) => stringify_expand } -impl BuiltinFnLikeExpander { - pub fn expand( - &self, - db: &dyn AstDatabase, - id: MacroCallId, - tt: &tt::Subtree, - ) -> Result { - let expander = BUILTIN_MACROS - .iter() - .find(|it| *self == it.kind) - .map(|it| it.expand) - .ok_or_else(|| mbe::ExpandError::ConversionError)?; - - expander(db, id, tt) - } -} - -pub fn find_builtin_macro( - ident: &name::Name, - krate: CrateId, - ast_id: AstId, -) -> Option { - let kind = BUILTIN_MACROS.iter().find(|it| *ident == it.name).map(|it| it.kind)?; - - Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(kind) }) -} - fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize { // FIXME: Use expansion info let file_id = file.original_file(db); -- cgit v1.2.3