diff options
author | Aleksey Kladov <[email protected]> | 2019-05-04 16:01:43 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-05-04 16:01:43 +0100 |
commit | bcf45371ff19882e67300cc483b481450ee129fb (patch) | |
tree | 616d3e4dd5fa15228ac2ea85a93d76df0c3b8581 /crates/ra_hir | |
parent | 87a1e276d56a3cb5f9d9c59f8c52c5573e19982b (diff) |
make macro expansion into a proper query
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/db.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/expr.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir/src/ids.rs | 43 |
3 files changed, 28 insertions, 35 deletions
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index f88ae61bb..398e00c42 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -45,6 +45,9 @@ pub trait DefDatabase: SourceDatabase { | |||
45 | #[salsa::invoke(crate::ids::macro_arg_query)] | 45 | #[salsa::invoke(crate::ids::macro_arg_query)] |
46 | fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>; | 46 | fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>; |
47 | 47 | ||
48 | #[salsa::invoke(crate::ids::macro_expand_query)] | ||
49 | fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result<Arc<tt::Subtree>, String>; | ||
50 | |||
48 | #[salsa::invoke(crate::ids::HirFileId::hir_parse_query)] | 51 | #[salsa::invoke(crate::ids::HirFileId::hir_parse_query)] |
49 | fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>; | 52 | fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>; |
50 | 53 | ||
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index f9b3f5443..692da2895 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -5,14 +5,13 @@ use rustc_hash::FxHashMap; | |||
5 | 5 | ||
6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; | 6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | SyntaxNodePtr, AstPtr, AstNode,TreeArc, | 8 | SyntaxNodePtr, AstPtr, AstNode, |
9 | ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner} | 9 | ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner} |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, | 13 | Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, |
14 | name::AsName, | 14 | name::AsName, |
15 | ids::{MacroCallId}, | ||
16 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeRef}, |
17 | }; | 16 | }; |
18 | use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}}; | 17 | use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}}; |
@@ -826,8 +825,8 @@ where | |||
826 | .with_file_id(self.current_file_id); | 825 | .with_file_id(self.current_file_id); |
827 | 826 | ||
828 | if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) { | 827 | if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) { |
829 | if let Some(arg) = self.db.macro_arg(call_id) { | 828 | if let Some(tt) = self.db.macro_expand(call_id).ok() { |
830 | if let Some(expr) = expand_macro_to_expr(self.db, call_id, &arg) { | 829 | if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() { |
831 | log::debug!("macro expansion {}", expr.syntax().debug_dump()); | 830 | log::debug!("macro expansion {}", expr.syntax().debug_dump()); |
832 | let old_file_id = | 831 | let old_file_id = |
833 | std::mem::replace(&mut self.current_file_id, call_id.into()); | 832 | std::mem::replace(&mut self.current_file_id, call_id.into()); |
@@ -998,16 +997,6 @@ where | |||
998 | } | 997 | } |
999 | } | 998 | } |
1000 | 999 | ||
1001 | fn expand_macro_to_expr( | ||
1002 | db: &impl HirDatabase, | ||
1003 | macro_call: MacroCallId, | ||
1004 | arg: &tt::Subtree, | ||
1005 | ) -> Option<TreeArc<ast::Expr>> { | ||
1006 | let rules = db.macro_def(macro_call.loc(db).def)?; | ||
1007 | let expanded = rules.expand(&arg).ok()?; | ||
1008 | mbe::token_tree_to_expr(&expanded).ok() | ||
1009 | } | ||
1010 | |||
1011 | pub(crate) fn body_with_source_map_query( | 1000 | pub(crate) fn body_with_source_map_query( |
1012 | db: &impl HirDatabase, | 1001 | db: &impl HirDatabase, |
1013 | def: DefWithBody, | 1002 | def: DefWithBody, |
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index cf0e934a9..4102951c9 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
@@ -63,19 +63,22 @@ impl HirFileId { | |||
63 | match file_id.0 { | 63 | match file_id.0 { |
64 | HirFileIdRepr::File(file_id) => db.parse(file_id), | 64 | HirFileIdRepr::File(file_id) => db.parse(file_id), |
65 | HirFileIdRepr::Macro(macro_call_id) => { | 65 | HirFileIdRepr::Macro(macro_call_id) => { |
66 | parse_macro(db, macro_call_id).unwrap_or_else(|err| { | 66 | match db.macro_expand(macro_call_id) { |
67 | // Note: | 67 | Ok(tt) => mbe::token_tree_to_ast_item_list(&tt), |
68 | // The final goal we would like to make all parse_macro success, | 68 | Err(err) => { |
69 | // such that the following log will not call anyway. | 69 | // Note: |
70 | log::warn!( | 70 | // The final goal we would like to make all parse_macro success, |
71 | "fail on macro_parse: (reason: {}) {}", | 71 | // such that the following log will not call anyway. |
72 | err, | 72 | log::warn!( |
73 | macro_call_id.debug_dump(db) | 73 | "fail on macro_parse: (reason: {}) {}", |
74 | ); | 74 | err, |
75 | 75 | macro_call_id.debug_dump(db) | |
76 | // returning an empty string looks fishy... | 76 | ); |
77 | SourceFile::parse("") | 77 | |
78 | }) | 78 | // returning an empty string looks fishy... |
79 | SourceFile::parse("") | ||
80 | } | ||
81 | } | ||
79 | } | 82 | } |
80 | } | 83 | } |
81 | } | 84 | } |
@@ -124,23 +127,21 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option< | |||
124 | Some(Arc::new(tt)) | 127 | Some(Arc::new(tt)) |
125 | } | 128 | } |
126 | 129 | ||
127 | fn parse_macro( | 130 | pub(crate) fn macro_expand_query( |
128 | db: &impl DefDatabase, | 131 | db: &impl DefDatabase, |
129 | macro_call_id: MacroCallId, | 132 | id: MacroCallId, |
130 | ) -> Result<TreeArc<SourceFile>, String> { | 133 | ) -> Result<Arc<tt::Subtree>, String> { |
131 | let loc = macro_call_id.loc(db); | 134 | let loc = id.loc(db); |
132 | let macro_arg = db.macro_arg(macro_call_id).ok_or("Fail to args in to tt::TokenTree")?; | 135 | let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; |
133 | 136 | ||
134 | let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; | 137 | let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; |
135 | let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?; | 138 | let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?; |
136 | |||
137 | // Set a hard limit for the expanded tt | 139 | // Set a hard limit for the expanded tt |
138 | let count = tt.count(); | 140 | let count = tt.count(); |
139 | if count > 65536 { | 141 | if count > 65536 { |
140 | return Err(format!("Total tokens count exceed limit : count = {}", count)); | 142 | return Err(format!("Total tokens count exceed limit : count = {}", count)); |
141 | } | 143 | } |
142 | 144 | Ok(Arc::new(tt)) | |
143 | Ok(mbe::token_tree_to_ast_item_list(&tt)) | ||
144 | } | 145 | } |
145 | 146 | ||
146 | macro_rules! impl_intern_key { | 147 | macro_rules! impl_intern_key { |