From c46768d13dd34bbe878cc62eca4af873ffbb7c22 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 10 Nov 2019 11:03:24 +0800 Subject: Add basic bultin macro infrastructure --- crates/ra_hir_expand/src/builtin_macro.rs | 26 ++++++++++++ crates/ra_hir_expand/src/db.rs | 66 +++++++++++++++++++++++-------- crates/ra_hir_expand/src/hygiene.rs | 7 +++- crates/ra_hir_expand/src/lib.rs | 29 ++++++++++++-- crates/ra_hir_expand/src/name.rs | 3 ++ 5 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 crates/ra_hir_expand/src/builtin_macro.rs (limited to 'crates/ra_hir_expand') diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs new file mode 100644 index 000000000..dca2f17ef --- /dev/null +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -0,0 +1,26 @@ +//! Builtin macro +use crate::{ast, name, AstId, BuiltinMacro, CrateId, MacroDefId}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BuiltinExpander { + Line +} + +impl BuiltinExpander { + pub fn expand(&self, _tt: &tt::Subtree) -> Result { + Err(mbe::ExpandError::UnexpectedToken) + } +} + +pub fn find_builtin_macro( + ident: &name::Name, + krate: CrateId, + ast_id: AstId, +) -> Option { + // FIXME: Better registering method + if ident == &name::LINE { + Some(MacroDefId::BuiltinMacro(BuiltinMacro { expander: BuiltinExpander::Line, krate, ast_id })) + } else { + None + } +} diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index b4dafe1d8..343c9e6bf 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -9,10 +9,37 @@ use ra_prof::profile; use ra_syntax::{AstNode, Parse, SyntaxNode}; use crate::{ - ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, - MacroFile, MacroFileKind, + ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, + MacroDefId, MacroFile, MacroFileKind, }; +#[derive(Debug, Clone, Eq, PartialEq)] +pub enum TokenExpander { + MacroRules(mbe::MacroRules), + Builtin(BuiltinExpander), +} + +impl TokenExpander { + pub fn expand( + &self, + db: &dyn AstDatabase, + id: MacroCallId, + tt: &tt::Subtree, + ) -> Result { + match self { + TokenExpander::MacroRules(it) => it.expand(tt), + TokenExpander::Builtin(it) => it.expand(tt), + } + } + + pub fn shift(&self) -> u32 { + match self { + TokenExpander::MacroRules(it) => it.shift(), + TokenExpander::Builtin(_) => 0, + } + } +} + // FIXME: rename to ExpandDatabase #[salsa::query_group(AstDatabaseStorage)] pub trait AstDatabase: SourceDatabase { @@ -24,7 +51,7 @@ pub trait AstDatabase: SourceDatabase { #[salsa::interned] fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; fn macro_arg(&self, id: MacroCallId) -> Option>; - fn macro_def(&self, id: MacroDefId) -> Option>; + fn macro_def(&self, id: MacroDefId) -> Option>; fn parse_macro( &self, macro_file: MacroFile, @@ -41,18 +68,25 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc Option> { - let macro_call = id.ast_id.to_node(db); - let arg = macro_call.token_tree()?; - let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { - log::warn!("fail on macro_def to token tree: {:#?}", arg); - None - })?; - let rules = MacroRules::parse(&tt).ok().or_else(|| { - log::warn!("fail on macro_def parse: {:#?}", tt); - None - })?; - Some(Arc::new((rules, tmap))) +) -> Option> { + match id { + MacroDefId::DeclarativeMacro(it) => { + let macro_call = it.ast_id.to_node(db); + let arg = macro_call.token_tree()?; + let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { + log::warn!("fail on macro_def to token tree: {:#?}", arg); + None + })?; + let rules = MacroRules::parse(&tt).ok().or_else(|| { + log::warn!("fail on macro_def parse: {:#?}", tt); + None + })?; + Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) + } + MacroDefId::BuiltinMacro(it) => { + Some(Arc::new((TokenExpander::Builtin(it.expander.clone()), mbe::TokenMap::default()))) + } + } } pub(crate) fn macro_arg( @@ -74,7 +108,7 @@ pub(crate) fn macro_expand( let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; - let tt = macro_rules.0.expand(¯o_arg.0).map_err(|err| format!("{:?}", err))?; + let tt = macro_rules.0.expand(db, id, ¯o_arg.0).map_err(|err| format!("{:?}", err))?; // Set a hard limit for the expanded tt let count = tt.count(); if count > 65536 { diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs index 77428ec99..6b682d3ab 100644 --- a/crates/ra_hir_expand/src/hygiene.rs +++ b/crates/ra_hir_expand/src/hygiene.rs @@ -9,7 +9,7 @@ use crate::{ db::AstDatabase, either::Either, name::{AsName, Name}, - HirFileId, HirFileIdRepr, + HirFileId, HirFileIdRepr, MacroDefId, }; #[derive(Debug)] @@ -24,7 +24,10 @@ impl Hygiene { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc = db.lookup_intern_macro(macro_file.macro_call_id); - Some(loc.def.krate) + match loc.def { + MacroDefId::DeclarativeMacro(it) => Some(it.krate), + MacroDefId::BuiltinMacro(_) => None, + } } }; Hygiene { def_crate } diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 151d1d785..6b71738ee 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -10,6 +10,7 @@ pub mod either; pub mod name; pub mod hygiene; pub mod diagnostics; +pub mod builtin_macro; use std::hash::{Hash, Hasher}; use std::sync::Arc; @@ -21,6 +22,7 @@ use ra_syntax::{ }; use crate::ast_id_map::FileAstId; +use crate::builtin_macro::BuiltinExpander; /// Input to the analyzer is a set of files, where each file is identified by /// `FileId` and contains source code. However, another source of source code in @@ -75,9 +77,15 @@ impl HirFileId { HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + // FIXME: Do we support expansion information in builtin macro? + let macro_decl = match loc.def { + MacroDefId::DeclarativeMacro(it) => (it), + MacroDefId::BuiltinMacro(_) => return None, + }; + let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); let def_start = - loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); + macro_decl.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); let macro_def = db.macro_def(loc.def)?; let shift = macro_def.0.shift(); @@ -85,7 +93,7 @@ impl HirFileId { let macro_arg = db.macro_arg(macro_file.macro_call_id)?; let arg_start = (loc.ast_id.file_id, arg_start); - let def_start = (loc.def.ast_id.file_id, def_start); + let def_start = (macro_decl.ast_id.file_id, def_start); Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) } @@ -119,9 +127,22 @@ impl salsa::InternKey for MacroCallId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct MacroDefId { +pub enum MacroDefId { + DeclarativeMacro(DeclarativeMacro), + BuiltinMacro(BuiltinMacro), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct DeclarativeMacro { + pub krate: CrateId, + pub ast_id: AstId, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct BuiltinMacro { pub krate: CrateId, pub ast_id: AstId, + pub expander: BuiltinExpander, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -144,7 +165,7 @@ pub struct ExpansionInfo { pub(crate) def_start: (HirFileId, TextUnit), pub(crate) shift: u32, - pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>, + pub(crate) macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, pub(crate) exp_map: Arc, } diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs index 720896ee8..1bf17d12b 100644 --- a/crates/ra_hir_expand/src/name.rs +++ b/crates/ra_hir_expand/src/name.rs @@ -140,3 +140,6 @@ pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result"); pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); + +// Builtin Macros +pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); -- cgit v1.2.3