aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model/src.rs11
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs28
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs7
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs26
-rw-r--r--crates/ra_hir_expand/src/db.rs66
-rw-r--r--crates/ra_hir_expand/src/hygiene.rs7
-rw-r--r--crates/ra_hir_expand/src/lib.rs29
-rw-r--r--crates/ra_hir_expand/src/name.rs3
8 files changed, 148 insertions, 29 deletions
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::{
6 adt::VariantDef, 6 adt::VariantDef,
7 db::{AstDatabase, DefDatabase, HirDatabase}, 7 db::{AstDatabase, DefDatabase, HirDatabase},
8 ids::AstItemDef, 8 ids::AstItemDef,
9 Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, 9 Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef,
10 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, 10 MacroDefId, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
11}; 11};
12 12
13pub use hir_expand::Source; 13pub use hir_expand::Source;
@@ -140,10 +140,15 @@ impl HasSource for TypeAlias {
140 self.id.source(db) 140 self.id.source(db)
141 } 141 }
142} 142}
143
143impl HasSource for MacroDef { 144impl HasSource for MacroDef {
144 type Ast = ast::MacroCall; 145 type Ast = ast::MacroCall;
145 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> { 146 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> {
146 Source { file_id: self.id.ast_id.file_id(), ast: self.id.ast_id.to_node(db) } 147 let ast_id = match self.id {
148 MacroDefId::DeclarativeMacro(it) => it.ast_id,
149 MacroDefId::BuiltinMacro(it) => it.ast_id,
150 };
151 Source { file_id: ast_id.file_id(), ast: ast_id.to_node(db) }
147 } 152 }
148} 153}
149 154
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 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_expand::{ 3use hir_expand::{
4 builtin_macro::find_builtin_macro,
4 name::{self, AsName, Name}, 5 name::{self, AsName, Name},
5 HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, 6 DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind,
6}; 7};
7use ra_cfg::CfgOptions; 8use ra_cfg::CfgOptions;
8use ra_db::{CrateId, FileId}; 9use ra_db::{CrateId, FileId};
@@ -688,11 +689,32 @@ where
688 fn collect_macro(&mut self, mac: &raw::MacroData) { 689 fn collect_macro(&mut self, mac: &raw::MacroData) {
689 let ast_id = AstId::new(self.file_id, mac.ast_id); 690 let ast_id = AstId::new(self.file_id, mac.ast_id);
690 691
692 // Case 0: builtin macros
693 if mac.builtin {
694 if let Some(name) = &mac.name {
695 let krate = self.def_collector.def_map.krate;
696 if let Some(macro_id) = find_builtin_macro(name, krate, ast_id) {
697 self.def_collector.define_macro(
698 self.module_id,
699 name.clone(),
700 macro_id,
701 mac.export,
702 );
703 return;
704 }
705 }
706 }
707
691 // Case 1: macro rules, define a macro in crate-global mutable scope 708 // Case 1: macro rules, define a macro in crate-global mutable scope
692 if is_macro_rules(&mac.path) { 709 if is_macro_rules(&mac.path) {
693 if let Some(name) = &mac.name { 710 if let Some(name) = &mac.name {
694 let macro_id = MacroDefId { ast_id, krate: self.def_collector.def_map.krate }; 711 let macro_id = DeclarativeMacro { ast_id, krate: self.def_collector.def_map.krate };
695 self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); 712 self.def_collector.define_macro(
713 self.module_id,
714 name.clone(),
715 MacroDefId::DeclarativeMacro(macro_id),
716 mac.export,
717 );
696 } 718 }
697 return; 719 return;
698 } 720 }
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 {
200 pub(super) path: Path, 200 pub(super) path: Path,
201 pub(super) name: Option<Name>, 201 pub(super) name: Option<Name>,
202 pub(super) export: bool, 202 pub(super) export: bool,
203 pub(super) builtin: bool,
203} 204}
204 205
205struct RawItemsCollector { 206struct RawItemsCollector {
@@ -367,7 +368,11 @@ impl RawItemsCollector {
367 // FIXME: cfg_attr 368 // FIXME: cfg_attr
368 let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export"); 369 let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
369 370
370 let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); 371 // FIXME: cfg_attr
372 let builtin =
373 m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro");
374
375 let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin });
371 self.push_item(current_module, attrs, RawItemKind::Macro(m)); 376 self.push_item(current_module, attrs, RawItemKind::Macro(m));
372 } 377 }
373 378
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 @@
1//! Builtin macro
2use crate::{ast, name, AstId, BuiltinMacro, CrateId, MacroDefId};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum BuiltinExpander {
6 Line
7}
8
9impl BuiltinExpander {
10 pub fn expand(&self, _tt: &tt::Subtree) -> Result<tt::Subtree, mbe::ExpandError> {
11 Err(mbe::ExpandError::UnexpectedToken)
12 }
13}
14
15pub fn find_builtin_macro(
16 ident: &name::Name,
17 krate: CrateId,
18 ast_id: AstId<ast::MacroCall>,
19) -> Option<MacroDefId> {
20 // FIXME: Better registering method
21 if ident == &name::LINE {
22 Some(MacroDefId::BuiltinMacro(BuiltinMacro { expander: BuiltinExpander::Line, krate, ast_id }))
23 } else {
24 None
25 }
26}
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;
9use ra_syntax::{AstNode, Parse, SyntaxNode}; 9use ra_syntax::{AstNode, Parse, SyntaxNode};
10 10
11use crate::{ 11use crate::{
12 ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, 12 ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc,
13 MacroFile, MacroFileKind, 13 MacroDefId, MacroFile, MacroFileKind,
14}; 14};
15 15
16#[derive(Debug, Clone, Eq, PartialEq)]
17pub enum TokenExpander {
18 MacroRules(mbe::MacroRules),
19 Builtin(BuiltinExpander),
20}
21
22impl TokenExpander {
23 pub fn expand(
24 &self,
25 db: &dyn AstDatabase,
26 id: MacroCallId,
27 tt: &tt::Subtree,
28 ) -> Result<tt::Subtree, mbe::ExpandError> {
29 match self {
30 TokenExpander::MacroRules(it) => it.expand(tt),
31 TokenExpander::Builtin(it) => it.expand(tt),
32 }
33 }
34
35 pub fn shift(&self) -> u32 {
36 match self {
37 TokenExpander::MacroRules(it) => it.shift(),
38 TokenExpander::Builtin(_) => 0,
39 }
40 }
41}
42
16// FIXME: rename to ExpandDatabase 43// FIXME: rename to ExpandDatabase
17#[salsa::query_group(AstDatabaseStorage)] 44#[salsa::query_group(AstDatabaseStorage)]
18pub trait AstDatabase: SourceDatabase { 45pub trait AstDatabase: SourceDatabase {
@@ -24,7 +51,7 @@ pub trait AstDatabase: SourceDatabase {
24 #[salsa::interned] 51 #[salsa::interned]
25 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; 52 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
26 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>; 53 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
27 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>>; 54 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
28 fn parse_macro( 55 fn parse_macro(
29 &self, 56 &self,
30 macro_file: MacroFile, 57 macro_file: MacroFile,
@@ -41,18 +68,25 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdM
41pub(crate) fn macro_def( 68pub(crate) fn macro_def(
42 db: &dyn AstDatabase, 69 db: &dyn AstDatabase,
43 id: MacroDefId, 70 id: MacroDefId,
44) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>> { 71) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
45 let macro_call = id.ast_id.to_node(db); 72 match id {
46 let arg = macro_call.token_tree()?; 73 MacroDefId::DeclarativeMacro(it) => {
47 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { 74 let macro_call = it.ast_id.to_node(db);
48 log::warn!("fail on macro_def to token tree: {:#?}", arg); 75 let arg = macro_call.token_tree()?;
49 None 76 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
50 })?; 77 log::warn!("fail on macro_def to token tree: {:#?}", arg);
51 let rules = MacroRules::parse(&tt).ok().or_else(|| { 78 None
52 log::warn!("fail on macro_def parse: {:#?}", tt); 79 })?;
53 None 80 let rules = MacroRules::parse(&tt).ok().or_else(|| {
54 })?; 81 log::warn!("fail on macro_def parse: {:#?}", tt);
55 Some(Arc::new((rules, tmap))) 82 None
83 })?;
84 Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
85 }
86 MacroDefId::BuiltinMacro(it) => {
87 Some(Arc::new((TokenExpander::Builtin(it.expander.clone()), mbe::TokenMap::default())))
88 }
89 }
56} 90}
57 91
58pub(crate) fn macro_arg( 92pub(crate) fn macro_arg(
@@ -74,7 +108,7 @@ pub(crate) fn macro_expand(
74 let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; 108 let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
75 109
76 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; 110 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
77 let tt = macro_rules.0.expand(&macro_arg.0).map_err(|err| format!("{:?}", err))?; 111 let tt = macro_rules.0.expand(db, id, &macro_arg.0).map_err(|err| format!("{:?}", err))?;
78 // Set a hard limit for the expanded tt 112 // Set a hard limit for the expanded tt
79 let count = tt.count(); 113 let count = tt.count();
80 if count > 65536 { 114 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::{
9 db::AstDatabase, 9 db::AstDatabase,
10 either::Either, 10 either::Either,
11 name::{AsName, Name}, 11 name::{AsName, Name},
12 HirFileId, HirFileIdRepr, 12 HirFileId, HirFileIdRepr, MacroDefId,
13}; 13};
14 14
15#[derive(Debug)] 15#[derive(Debug)]
@@ -24,7 +24,10 @@ impl Hygiene {
24 HirFileIdRepr::FileId(_) => None, 24 HirFileIdRepr::FileId(_) => None,
25 HirFileIdRepr::MacroFile(macro_file) => { 25 HirFileIdRepr::MacroFile(macro_file) => {
26 let loc = db.lookup_intern_macro(macro_file.macro_call_id); 26 let loc = db.lookup_intern_macro(macro_file.macro_call_id);
27 Some(loc.def.krate) 27 match loc.def {
28 MacroDefId::DeclarativeMacro(it) => Some(it.krate),
29 MacroDefId::BuiltinMacro(_) => None,
30 }
28 } 31 }
29 }; 32 };
30 Hygiene { def_crate } 33 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;
10pub mod name; 10pub mod name;
11pub mod hygiene; 11pub mod hygiene;
12pub mod diagnostics; 12pub mod diagnostics;
13pub mod builtin_macro;
13 14
14use std::hash::{Hash, Hasher}; 15use std::hash::{Hash, Hasher};
15use std::sync::Arc; 16use std::sync::Arc;
@@ -21,6 +22,7 @@ use ra_syntax::{
21}; 22};
22 23
23use crate::ast_id_map::FileAstId; 24use crate::ast_id_map::FileAstId;
25use crate::builtin_macro::BuiltinExpander;
24 26
25/// Input to the analyzer is a set of files, where each file is identified by 27/// Input to the analyzer is a set of files, where each file is identified by
26/// `FileId` and contains source code. However, another source of source code in 28/// `FileId` and contains source code. However, another source of source code in
@@ -75,9 +77,15 @@ impl HirFileId {
75 HirFileIdRepr::MacroFile(macro_file) => { 77 HirFileIdRepr::MacroFile(macro_file) => {
76 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); 78 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
77 79
80 // FIXME: Do we support expansion information in builtin macro?
81 let macro_decl = match loc.def {
82 MacroDefId::DeclarativeMacro(it) => (it),
83 MacroDefId::BuiltinMacro(_) => return None,
84 };
85
78 let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); 86 let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start();
79 let def_start = 87 let def_start =
80 loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); 88 macro_decl.ast_id.to_node(db).token_tree()?.syntax().text_range().start();
81 89
82 let macro_def = db.macro_def(loc.def)?; 90 let macro_def = db.macro_def(loc.def)?;
83 let shift = macro_def.0.shift(); 91 let shift = macro_def.0.shift();
@@ -85,7 +93,7 @@ impl HirFileId {
85 let macro_arg = db.macro_arg(macro_file.macro_call_id)?; 93 let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
86 94
87 let arg_start = (loc.ast_id.file_id, arg_start); 95 let arg_start = (loc.ast_id.file_id, arg_start);
88 let def_start = (loc.def.ast_id.file_id, def_start); 96 let def_start = (macro_decl.ast_id.file_id, def_start);
89 97
90 Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) 98 Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift })
91 } 99 }
@@ -119,9 +127,22 @@ impl salsa::InternKey for MacroCallId {
119} 127}
120 128
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
122pub struct MacroDefId { 130pub enum MacroDefId {
131 DeclarativeMacro(DeclarativeMacro),
132 BuiltinMacro(BuiltinMacro),
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
136pub struct DeclarativeMacro {
137 pub krate: CrateId,
138 pub ast_id: AstId<ast::MacroCall>,
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
142pub struct BuiltinMacro {
123 pub krate: CrateId, 143 pub krate: CrateId,
124 pub ast_id: AstId<ast::MacroCall>, 144 pub ast_id: AstId<ast::MacroCall>,
145 pub expander: BuiltinExpander,
125} 146}
126 147
127#[derive(Debug, Clone, PartialEq, Eq, Hash)] 148#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -144,7 +165,7 @@ pub struct ExpansionInfo {
144 pub(crate) def_start: (HirFileId, TextUnit), 165 pub(crate) def_start: (HirFileId, TextUnit),
145 pub(crate) shift: u32, 166 pub(crate) shift: u32,
146 167
147 pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>, 168 pub(crate) macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>,
148 pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, 169 pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
149 pub(crate) exp_map: Arc<mbe::RevTokenMap>, 170 pub(crate) exp_map: Arc<mbe::RevTokenMap>,
150} 171}
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");
140pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); 140pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
141pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); 141pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
142pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); 142pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
143
144// Builtin Macros
145pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");