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/src/code_model/src.rs | 11 +++-- crates/ra_hir_def/src/nameres/collector.rs | 28 +++++++++++-- crates/ra_hir_def/src/nameres/raw.rs | 7 +++- 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 ++ 8 files changed, 148 insertions(+), 29 deletions(-) create mode 100644 crates/ra_hir_expand/src/builtin_macro.rs diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 6d116ee75..c4e62f799 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -6,8 +6,8 @@ use crate::{ adt::VariantDef, db::{AstDatabase, DefDatabase, HirDatabase}, ids::AstItemDef, - Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, + MacroDefId, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; @@ -140,10 +140,15 @@ impl HasSource for TypeAlias { self.id.source(db) } } + impl HasSource for MacroDef { type Ast = ast::MacroCall; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - Source { file_id: self.id.ast_id.file_id(), ast: self.id.ast_id.to_node(db) } + let ast_id = match self.id { + MacroDefId::DeclarativeMacro(it) => it.ast_id, + MacroDefId::BuiltinMacro(it) => it.ast_id, + }; + Source { file_id: ast_id.file_id(), ast: ast_id.to_node(db) } } } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index aacd50df8..5f18e9de3 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -1,8 +1,9 @@ //! FIXME: write short doc here use hir_expand::{ + builtin_macro::find_builtin_macro, name::{self, AsName, Name}, - HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, + DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, }; use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId}; @@ -688,11 +689,32 @@ where fn collect_macro(&mut self, mac: &raw::MacroData) { let ast_id = AstId::new(self.file_id, mac.ast_id); + // Case 0: builtin macros + if mac.builtin { + if let Some(name) = &mac.name { + let krate = self.def_collector.def_map.krate; + if let Some(macro_id) = find_builtin_macro(name, krate, ast_id) { + self.def_collector.define_macro( + self.module_id, + name.clone(), + macro_id, + mac.export, + ); + return; + } + } + } + // Case 1: macro rules, define a macro in crate-global mutable scope if is_macro_rules(&mac.path) { if let Some(name) = &mac.name { - let macro_id = MacroDefId { ast_id, krate: self.def_collector.def_map.krate }; - self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); + let macro_id = DeclarativeMacro { ast_id, krate: self.def_collector.def_map.krate }; + self.def_collector.define_macro( + self.module_id, + name.clone(), + MacroDefId::DeclarativeMacro(macro_id), + mac.export, + ); } return; } diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 369376f30..f52002bc0 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -200,6 +200,7 @@ pub(super) struct MacroData { pub(super) path: Path, pub(super) name: Option, pub(super) export: bool, + pub(super) builtin: bool, } struct RawItemsCollector { @@ -367,7 +368,11 @@ impl RawItemsCollector { // FIXME: cfg_attr let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export"); - let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); + // FIXME: cfg_attr + let builtin = + m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro"); + + let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin }); self.push_item(current_module, attrs, RawItemKind::Macro(m)); } 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 From 1637a8a59071d8c7976ffc8d04edc5b7f54ae40b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 11 Nov 2019 14:14:39 +0800 Subject: Add quote macro --- crates/ra_hir_expand/src/lib.rs | 1 + crates/ra_hir_expand/src/quote.rs | 261 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 crates/ra_hir_expand/src/quote.rs diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 6b71738ee..21d666f13 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -11,6 +11,7 @@ pub mod name; pub mod hygiene; pub mod diagnostics; pub mod builtin_macro; +pub mod quote; use std::hash::{Hash, Hasher}; use std::sync::Arc; diff --git a/crates/ra_hir_expand/src/quote.rs b/crates/ra_hir_expand/src/quote.rs new file mode 100644 index 000000000..9cd17f0e3 --- /dev/null +++ b/crates/ra_hir_expand/src/quote.rs @@ -0,0 +1,261 @@ +//! A simplified version of quote-crate like quasi quote macro + +// A helper macro quote macro +// FIXME: +// 1. Not all puncts are handled +// 2. #()* pattern repetition not supported now +// * But we can do it manually, see `test_quote_derive_copy_hack` +#[doc(hidden)] +#[macro_export] +macro_rules! __quote { + () => { + Vec::::new() + }; + + ( @SUBTREE $delim:ident $($tt:tt)* ) => { + { + let children = $crate::__quote!($($tt)*); + let subtree = tt::Subtree { + delimiter: tt::Delimiter::$delim, + token_trees: $crate::quote::IntoTt::to_tokens(children), + }; + subtree + } + }; + + ( @PUNCT $first:literal ) => { + { + vec![ + tt::Leaf::Punct(tt::Punct { + char: $first, + spacing: tt::Spacing::Alone, + }).into() + ] + } + }; + + ( @PUNCT $first:literal, $sec:literal ) => { + { + vec![ + tt::Leaf::Punct(tt::Punct { + char: $first, + spacing: tt::Spacing::Joint, + }).into(), + tt::Leaf::Punct(tt::Punct { + char: $sec, + spacing: tt::Spacing::Alone, + }).into() + ] + } + }; + + // hash variable + ( # $first:ident $($tail:tt)* ) => { + { + let token = $crate::quote::ToTokenTree::to_token($first); + let mut tokens = vec![token.into()]; + let mut tail_tokens = $crate::quote::IntoTt::to_tokens($crate::__quote!($($tail)*)); + tokens.append(&mut tail_tokens); + tokens + } + }; + + // Brace + ( { $($tt:tt)* } ) => { $crate::__quote!(@SUBTREE Brace $($tt)*) }; + // Bracket + ( [ $($tt:tt)* ] ) => { $crate::__quote!(@SUBTREE Bracket $($tt)*) }; + // Parenthesis + ( ( $($tt:tt)* ) ) => { $crate::__quote!(@SUBTREE Parenthesis $($tt)*) }; + + // Literal + ( $tt:literal ) => { vec![$crate::quote::ToTokenTree::to_token($tt).into()] }; + // Ident + ( $tt:ident ) => { + vec![ { + tt::Leaf::Ident(tt::Ident { + text: stringify!($tt).into(), + id: tt::TokenId::unspecified(), + }).into() + }] + }; + + // Puncts + // FIXME: Not all puncts are handled + ( -> ) => {$crate::__quote!(@PUNCT '-', '>')}; + ( & ) => {$crate::__quote!(@PUNCT '&')}; + ( , ) => {$crate::__quote!(@PUNCT ',')}; + ( : ) => {$crate::__quote!(@PUNCT ':')}; + ( . ) => {$crate::__quote!(@PUNCT '.')}; + + ( $first:tt $($tail:tt)+ ) => { + { + let mut tokens = $crate::quote::IntoTt::to_tokens($crate::__quote!($first)); + let mut tail_tokens = $crate::quote::IntoTt::to_tokens($crate::__quote!($($tail)*)); + + tokens.append(&mut tail_tokens); + tokens + } + }; +} + +/// FIXME: +/// It probably should implement in proc-macro +#[macro_export] +macro_rules! quote { + ( $($tt:tt)* ) => { + $crate::quote::IntoTt::to_subtree($crate::__quote!($($tt)*)) + } +} + +pub(crate) trait IntoTt { + fn to_subtree(self) -> tt::Subtree; + fn to_tokens(self) -> Vec; +} + +impl IntoTt for Vec { + fn to_subtree(self) -> tt::Subtree { + tt::Subtree { delimiter: tt::Delimiter::None, token_trees: self } + } + + fn to_tokens(self) -> Vec { + self + } +} + +impl IntoTt for tt::Subtree { + fn to_subtree(self) -> tt::Subtree { + self + } + + fn to_tokens(self) -> Vec { + vec![tt::TokenTree::Subtree(self)] + } +} + +pub(crate) trait ToTokenTree { + fn to_token(self) -> tt::TokenTree; +} + +impl ToTokenTree for tt::TokenTree { + fn to_token(self) -> tt::TokenTree { + self + } +} + +impl ToTokenTree for tt::Subtree { + fn to_token(self) -> tt::TokenTree { + self.into() + } +} + +macro_rules! impl_to_to_tokentrees { + ($($ty:ty => $this:ident $im:block);*) => { + $( + impl ToTokenTree for $ty { + fn to_token($this) -> tt::TokenTree { + let leaf: tt::Leaf = $im.into(); + leaf.into() + } + } + + impl ToTokenTree for &$ty { + fn to_token($this) -> tt::TokenTree { + let leaf: tt::Leaf = $im.clone().into(); + leaf.into() + } + } + )* + } +} + +impl_to_to_tokentrees! { + u32 => self { tt::Literal{text: self.to_string().into()} }; + usize => self { tt::Literal{text: self.to_string().into()}}; + i32 => self { tt::Literal{text: self.to_string().into()}}; + &str => self { tt::Literal{text: self.to_string().into()}}; + String => self { tt::Literal{text: self.into()}}; + tt::Leaf => self { self }; + tt::Literal => self { self }; + tt::Ident => self { self }; + tt::Punct => self { self } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_quote_delimiters() { + assert_eq!(quote!({}).to_string(), "{}"); + assert_eq!(quote!(()).to_string(), "()"); + assert_eq!(quote!([]).to_string(), "[]"); + } + + #[test] + fn test_quote_idents() { + assert_eq!(quote!(32).to_string(), "32"); + assert_eq!(quote!(struct).to_string(), "struct"); + } + + #[test] + fn test_quote_hash_simple_literal() { + let a = 20; + assert_eq!(quote!(#a).to_string(), "20"); + let s: String = "hello".into(); + assert_eq!(quote!(#s).to_string(), "hello"); + } + + fn mk_ident(name: &str) -> tt::Ident { + tt::Ident { text: name.into(), id: tt::TokenId::unspecified() } + } + + #[test] + fn test_quote_hash_token_tree() { + let a = mk_ident("hello"); + + let quoted = quote!(#a); + assert_eq!(quoted.to_string(), "hello"); + let t = format!("{:?}", quoted); + assert_eq!(t, "Subtree { delimiter: None, token_trees: [Leaf(Ident(Ident { text: \"hello\", id: TokenId(4294967295) }))] }"); + } + + #[test] + fn test_quote_simple_derive_copy() { + let name = mk_ident("Foo"); + + let quoted = quote! { + impl Clone for #name { + fn clone(&self) -> Self { + Self {} + } + } + }; + + assert_eq!(quoted.to_string(), "impl Clone for Foo {fn clone (& self) -> Self {Self {}}}"); + } + + #[test] + fn test_quote_derive_copy_hack() { + // Assume the given struct is: + // struct Foo { + // name: String, + // id: u32, + // } + let struct_name = mk_ident("Foo"); + let fields = [mk_ident("name"), mk_ident("id")]; + let fields = fields + .into_iter() + .map(|it| quote!(#it: self.#it.clone(), ).token_trees.clone()) + .flatten(); + + let list = tt::Subtree { delimiter: tt::Delimiter::Brace, token_trees: fields.collect() }; + + let quoted = quote! { + impl Clone for #struct_name { + fn clone(&self) -> Self { + Self #list + } + } + }; + + assert_eq!(quoted.to_string(), "impl Clone for Foo {fn clone (& self) -> Self {Self {name : self . name . clone () , id : self . id . clone () ,}}}"); + } +} -- cgit v1.2.3 From c4aa8b63bcea5faa23da56b679cafbdbad6892f1 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 11 Nov 2019 14:15:09 +0800 Subject: Add line macro and tests --- crates/ra_hir/src/ty/tests.rs | 19 +++++++++ crates/ra_hir_expand/src/builtin_macro.rs | 70 ++++++++++++++++++++++++++++--- crates/ra_hir_expand/src/db.rs | 2 +- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index e56b9356e..896bf2924 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -4810,3 +4810,22 @@ fn no_such_field_diagnostics() { "### ); } + +#[test] +fn infer_builtin_macros_line() { + assert_snapshot!( + infer(r#" +#[rustc_builtin_macro] +macro_rules! line {() => {}} + +fn main() { + let x = line!(); +} +"#), + @r###" + ![0; 1) '6': i32 + [64; 88) '{ ...!(); }': () + [74; 75) 'x': i32 + "### + ); +} diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index dca2f17ef..acb62da27 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -1,14 +1,28 @@ //! Builtin macro -use crate::{ast, name, AstId, BuiltinMacro, CrateId, MacroDefId}; +use crate::db::AstDatabase; +use crate::{ + ast::{self, AstNode}, + name, AstId, BuiltinMacro, CrateId, HirFileId, MacroCallId, MacroDefId, MacroFileKind, + TextUnit, +}; + +use crate::quote; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum BuiltinExpander { - Line + Line, } impl BuiltinExpander { - pub fn expand(&self, _tt: &tt::Subtree) -> Result { - Err(mbe::ExpandError::UnexpectedToken) + pub fn expand( + &self, + db: &dyn AstDatabase, + id: MacroCallId, + tt: &tt::Subtree, + ) -> Result { + match self { + BuiltinExpander::Line => line_expand(db, id, tt), + } } } @@ -18,9 +32,53 @@ pub fn find_builtin_macro( ast_id: AstId, ) -> Option { // FIXME: Better registering method - if ident == &name::LINE { - Some(MacroDefId::BuiltinMacro(BuiltinMacro { expander: BuiltinExpander::Line, krate, ast_id })) + if ident == &name::LINE_MACRO { + Some(MacroDefId::BuiltinMacro(BuiltinMacro { + expander: BuiltinExpander::Line, + krate, + ast_id, + })) } else { None } } + +fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize { + // FIXME: Use expansion info + let file_id = file.original_file(db); + let text = db.file_text(file_id); + let mut line_num = 1; + + // Count line end + for (i, c) in text.chars().enumerate() { + if i == pos.to_usize() { + break; + } + if c == '\n' { + line_num += 1; + } + } + + line_num +} + +fn line_expand( + db: &dyn AstDatabase, + id: MacroCallId, + _tt: &tt::Subtree, +) -> Result { + let loc = db.lookup_intern_macro(id); + let macro_call = loc.ast_id.to_node(db); + + let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; + let arg_start = arg.syntax().text_range().start(); + + let file = id.as_file(MacroFileKind::Expr); + let line_num = to_line_number(db, file, arg_start); + + let expanded = quote! { + #line_num + }; + + Ok(expanded) +} diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 343c9e6bf..009ff5312 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -28,7 +28,7 @@ impl TokenExpander { ) -> Result { match self { TokenExpander::MacroRules(it) => it.expand(tt), - TokenExpander::Builtin(it) => it.expand(tt), + TokenExpander::Builtin(it) => it.expand(db, id, tt), } } -- cgit v1.2.3 From 4f7df2aac107c0de2cab851f2a4f1ab369511fc8 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 11 Nov 2019 18:45:55 +0800 Subject: Add MacroDefKind --- crates/ra_hir/src/code_model/src.rs | 11 +++-------- crates/ra_hir_def/src/nameres/collector.rs | 15 +++++++-------- crates/ra_hir_expand/src/builtin_macro.rs | 8 ++------ crates/ra_hir_expand/src/db.rs | 12 ++++++------ crates/ra_hir_expand/src/hygiene.rs | 8 ++++---- crates/ra_hir_expand/src/lib.rs | 26 +++++++------------------- 6 files changed, 29 insertions(+), 51 deletions(-) diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index c4e62f799..6d116ee75 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -6,8 +6,8 @@ use crate::{ adt::VariantDef, db::{AstDatabase, DefDatabase, HirDatabase}, ids::AstItemDef, - Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, - MacroDefId, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, + ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; @@ -140,15 +140,10 @@ impl HasSource for TypeAlias { self.id.source(db) } } - impl HasSource for MacroDef { type Ast = ast::MacroCall; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - let ast_id = match self.id { - MacroDefId::DeclarativeMacro(it) => it.ast_id, - MacroDefId::BuiltinMacro(it) => it.ast_id, - }; - Source { file_id: ast_id.file_id(), ast: ast_id.to_node(db) } + Source { file_id: self.id.ast_id.file_id(), ast: self.id.ast_id.to_node(db) } } } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 5f18e9de3..30664278e 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -3,7 +3,7 @@ use hir_expand::{ builtin_macro::find_builtin_macro, name::{self, AsName, Name}, - DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, + HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind, }; use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId}; @@ -708,13 +708,12 @@ where // Case 1: macro rules, define a macro in crate-global mutable scope if is_macro_rules(&mac.path) { if let Some(name) = &mac.name { - let macro_id = DeclarativeMacro { ast_id, krate: self.def_collector.def_map.krate }; - self.def_collector.define_macro( - self.module_id, - name.clone(), - MacroDefId::DeclarativeMacro(macro_id), - mac.export, - ); + let macro_id = MacroDefId { + ast_id, + krate: self.def_collector.def_map.krate, + kind: MacroDefKind::Declarative, + }; + self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); } return; } diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index acb62da27..97fb0cb55 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -2,7 +2,7 @@ use crate::db::AstDatabase; use crate::{ ast::{self, AstNode}, - name, AstId, BuiltinMacro, CrateId, HirFileId, MacroCallId, MacroDefId, MacroFileKind, + name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind, TextUnit, }; @@ -33,11 +33,7 @@ pub fn find_builtin_macro( ) -> Option { // FIXME: Better registering method if ident == &name::LINE_MACRO { - Some(MacroDefId::BuiltinMacro(BuiltinMacro { - expander: BuiltinExpander::Line, - krate, - ast_id, - })) + Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) } else { None } diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 009ff5312..5eadee9c2 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -10,7 +10,7 @@ use ra_syntax::{AstNode, Parse, SyntaxNode}; use crate::{ ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, - MacroDefId, MacroFile, MacroFileKind, + MacroDefId, MacroDefKind, MacroFile, MacroFileKind, }; #[derive(Debug, Clone, Eq, PartialEq)] @@ -69,9 +69,9 @@ pub(crate) fn macro_def( db: &dyn AstDatabase, id: MacroDefId, ) -> Option> { - match id { - MacroDefId::DeclarativeMacro(it) => { - let macro_call = it.ast_id.to_node(db); + match id.kind { + MacroDefKind::Declarative => { + 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); @@ -83,8 +83,8 @@ pub(crate) fn macro_def( })?; Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) } - MacroDefId::BuiltinMacro(it) => { - Some(Arc::new((TokenExpander::Builtin(it.expander.clone()), mbe::TokenMap::default()))) + MacroDefKind::BuiltIn(expander) => { + Some(Arc::new((TokenExpander::Builtin(expander.clone()), mbe::TokenMap::default()))) } } } diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs index 6b682d3ab..379562a2c 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, MacroDefId, + HirFileId, HirFileIdRepr, MacroDefKind, }; #[derive(Debug)] @@ -24,9 +24,9 @@ impl Hygiene { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc = db.lookup_intern_macro(macro_file.macro_call_id); - match loc.def { - MacroDefId::DeclarativeMacro(it) => Some(it.krate), - MacroDefId::BuiltinMacro(_) => None, + match loc.def.kind { + MacroDefKind::Declarative => Some(loc.def.krate), + MacroDefKind::BuiltIn(_) => None, } } }; diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 21d666f13..c6ffa2c6f 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -78,15 +78,9 @@ 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 = - macro_decl.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); + loc.def.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(); @@ -94,7 +88,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 = (macro_decl.ast_id.file_id, def_start); + let def_start = (loc.def.ast_id.file_id, def_start); Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) } @@ -128,22 +122,16 @@ impl salsa::InternKey for MacroCallId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroDefId { - DeclarativeMacro(DeclarativeMacro), - BuiltinMacro(BuiltinMacro), -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct DeclarativeMacro { +pub struct MacroDefId { pub krate: CrateId, pub ast_id: AstId, + pub kind: MacroDefKind, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct BuiltinMacro { - pub krate: CrateId, - pub ast_id: AstId, - pub expander: BuiltinExpander, +pub enum MacroDefKind { + Declarative, + BuiltIn(BuiltinExpander), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -- cgit v1.2.3