diff options
author | Seivan Heidari <[email protected]> | 2019-10-31 08:43:20 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-10-31 08:43:20 +0000 |
commit | 8edda0e7b164009d6c03bb3d4be603fb38ad2e2a (patch) | |
tree | 744cf81075d394e2f9c06afb07642a2601800dda /crates/ra_hir_expand/src/db.rs | |
parent | 49562d36b97ddde34cf7585a8c2e8f232519b657 (diff) | |
parent | d067afb064a7fa67b172abf561b7d80740cd6f18 (diff) |
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir_expand/src/db.rs')
-rw-r--r-- | crates/ra_hir_expand/src/db.rs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs new file mode 100644 index 000000000..a4ee9a529 --- /dev/null +++ b/crates/ra_hir_expand/src/db.rs | |||
@@ -0,0 +1,104 @@ | |||
1 | //! Defines database & queries for macro expansion. | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use mbe::MacroRules; | ||
6 | use ra_db::{salsa, SourceDatabase}; | ||
7 | use ra_prof::profile; | ||
8 | use ra_syntax::{AstNode, Parse, SyntaxNode}; | ||
9 | |||
10 | use crate::{ | ||
11 | ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, | ||
12 | MacroFile, MacroFileKind, | ||
13 | }; | ||
14 | |||
15 | // FIXME: rename to ExpandDatabase | ||
16 | #[salsa::query_group(AstDatabaseStorage)] | ||
17 | pub trait AstDatabase: SourceDatabase { | ||
18 | fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>; | ||
19 | |||
20 | #[salsa::transparent] | ||
21 | fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>; | ||
22 | |||
23 | #[salsa::interned] | ||
24 | fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; | ||
25 | fn macro_arg(&self, id: MacroCallId) -> Option<Arc<tt::Subtree>>; | ||
26 | fn macro_def(&self, id: MacroDefId) -> Option<Arc<mbe::MacroRules>>; | ||
27 | fn parse_macro(&self, macro_file: MacroFile) -> Option<Parse<SyntaxNode>>; | ||
28 | fn macro_expand(&self, macro_call: MacroCallId) -> Result<Arc<tt::Subtree>, String>; | ||
29 | } | ||
30 | |||
31 | pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> { | ||
32 | let map = | ||
33 | db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it)); | ||
34 | Arc::new(map) | ||
35 | } | ||
36 | |||
37 | pub(crate) fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { | ||
38 | let macro_call = id.ast_id.to_node(db); | ||
39 | let arg = macro_call.token_tree()?; | ||
40 | let (tt, _) = mbe::ast_to_token_tree(&arg).or_else(|| { | ||
41 | log::warn!("fail on macro_def to token tree: {:#?}", arg); | ||
42 | None | ||
43 | })?; | ||
44 | let rules = MacroRules::parse(&tt).ok().or_else(|| { | ||
45 | log::warn!("fail on macro_def parse: {:#?}", tt); | ||
46 | None | ||
47 | })?; | ||
48 | Some(Arc::new(rules)) | ||
49 | } | ||
50 | |||
51 | pub(crate) fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<tt::Subtree>> { | ||
52 | let loc = db.lookup_intern_macro(id); | ||
53 | let macro_call = loc.ast_id.to_node(db); | ||
54 | let arg = macro_call.token_tree()?; | ||
55 | let (tt, _) = mbe::ast_to_token_tree(&arg)?; | ||
56 | Some(Arc::new(tt)) | ||
57 | } | ||
58 | |||
59 | pub(crate) fn macro_expand( | ||
60 | db: &dyn AstDatabase, | ||
61 | id: MacroCallId, | ||
62 | ) -> Result<Arc<tt::Subtree>, String> { | ||
63 | let loc = db.lookup_intern_macro(id); | ||
64 | let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; | ||
65 | |||
66 | let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; | ||
67 | let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?; | ||
68 | // Set a hard limit for the expanded tt | ||
69 | let count = tt.count(); | ||
70 | if count > 65536 { | ||
71 | return Err(format!("Total tokens count exceed limit : count = {}", count)); | ||
72 | } | ||
73 | Ok(Arc::new(tt)) | ||
74 | } | ||
75 | |||
76 | pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> { | ||
77 | match file_id.0 { | ||
78 | HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), | ||
79 | HirFileIdRepr::MacroFile(macro_file) => { | ||
80 | db.parse_macro(macro_file).map(|it| it.syntax_node()) | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | |||
85 | pub(crate) fn parse_macro( | ||
86 | db: &dyn AstDatabase, | ||
87 | macro_file: MacroFile, | ||
88 | ) -> Option<Parse<SyntaxNode>> { | ||
89 | let _p = profile("parse_macro_query"); | ||
90 | let macro_call_id = macro_file.macro_call_id; | ||
91 | let tt = db | ||
92 | .macro_expand(macro_call_id) | ||
93 | .map_err(|err| { | ||
94 | // Note: | ||
95 | // The final goal we would like to make all parse_macro success, | ||
96 | // such that the following log will not call anyway. | ||
97 | log::warn!("fail on macro_parse: (reason: {})", err,); | ||
98 | }) | ||
99 | .ok()?; | ||
100 | match macro_file.macro_file_kind { | ||
101 | MacroFileKind::Items => mbe::token_tree_to_items(&tt).ok().map(Parse::to_syntax), | ||
102 | MacroFileKind::Expr => mbe::token_tree_to_expr(&tt).ok().map(Parse::to_syntax), | ||
103 | } | ||
104 | } | ||