diff options
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/builtin_macro.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/db.rs | 66 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/hygiene.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 29 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/name.rs | 3 |
5 files changed, 109 insertions, 22 deletions
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 | ||
2 | use crate::{ast, name, AstId, BuiltinMacro, CrateId, MacroDefId}; | ||
3 | |||
4 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
5 | pub enum BuiltinExpander { | ||
6 | Line | ||
7 | } | ||
8 | |||
9 | impl BuiltinExpander { | ||
10 | pub fn expand(&self, _tt: &tt::Subtree) -> Result<tt::Subtree, mbe::ExpandError> { | ||
11 | Err(mbe::ExpandError::UnexpectedToken) | ||
12 | } | ||
13 | } | ||
14 | |||
15 | pub 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; | |||
9 | use ra_syntax::{AstNode, Parse, SyntaxNode}; | 9 | use ra_syntax::{AstNode, Parse, SyntaxNode}; |
10 | 10 | ||
11 | use crate::{ | 11 | use 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)] | ||
17 | pub enum TokenExpander { | ||
18 | MacroRules(mbe::MacroRules), | ||
19 | Builtin(BuiltinExpander), | ||
20 | } | ||
21 | |||
22 | impl 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)] |
18 | pub trait AstDatabase: SourceDatabase { | 45 | pub 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 | |||
41 | pub(crate) fn macro_def( | 68 | pub(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 | ||
58 | pub(crate) fn macro_arg( | 92 | pub(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(¯o_arg.0).map_err(|err| format!("{:?}", err))?; | 111 | let tt = macro_rules.0.expand(db, id, ¯o_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; | |||
10 | pub mod name; | 10 | pub mod name; |
11 | pub mod hygiene; | 11 | pub mod hygiene; |
12 | pub mod diagnostics; | 12 | pub mod diagnostics; |
13 | pub mod builtin_macro; | ||
13 | 14 | ||
14 | use std::hash::{Hash, Hasher}; | 15 | use std::hash::{Hash, Hasher}; |
15 | use std::sync::Arc; | 16 | use std::sync::Arc; |
@@ -21,6 +22,7 @@ use ra_syntax::{ | |||
21 | }; | 22 | }; |
22 | 23 | ||
23 | use crate::ast_id_map::FileAstId; | 24 | use crate::ast_id_map::FileAstId; |
25 | use 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)] |
122 | pub struct MacroDefId { | 130 | pub enum MacroDefId { |
131 | DeclarativeMacro(DeclarativeMacro), | ||
132 | BuiltinMacro(BuiltinMacro), | ||
133 | } | ||
134 | |||
135 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
136 | pub struct DeclarativeMacro { | ||
137 | pub krate: CrateId, | ||
138 | pub ast_id: AstId<ast::MacroCall>, | ||
139 | } | ||
140 | |||
141 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
142 | pub 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"); | |||
140 | pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); | 140 | pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); |
141 | pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); | 141 | pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); |
142 | pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); | 142 | pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); |
143 | |||
144 | // Builtin Macros | ||
145 | pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); | ||