aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-08 10:35:30 +0000
committerGitHub <[email protected]>2019-12-08 10:35:30 +0000
commitffcdd25cc8e7f1fd7fb4e3e8478c4cd1adfbf843 (patch)
tree973ccea0049ee917a7f901a7e0238f7ef6c984bc /crates
parent9e551d5452232d1d44e4bba754392beaa3a7628f (diff)
parent509fedd9d2f228c6dca762cbf06c31af34ac0c75 (diff)
Merge #2497
2497: Remove MacroFileKind r=matklad a=edwin0cheng This PR move `to_macro_file_kind` to `hir_expand::db` and use it to get the `FragmentKind` directly, such that we can remove `MacroFileKind`. Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/source_binder.rs41
-rw-r--r--crates/ra_hir_def/src/body.rs6
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs4
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs4
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs9
-rw-r--r--crates/ra_hir_expand/src/db.rs45
-rw-r--r--crates/ra_hir_expand/src/lib.rs13
7 files changed, 52 insertions, 70 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index b80aaeb90..c5a920688 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -21,7 +21,6 @@ use hir_def::{
21}; 21};
22use hir_expand::{ 22use hir_expand::{
23 hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, 23 hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
24 MacroFileKind,
25}; 24};
26use ra_syntax::{ 25use ra_syntax::{
27 ast::{self, AstNode}, 26 ast::{self, AstNode},
@@ -142,7 +141,6 @@ pub struct ReferenceDescriptor {
142 141
143#[derive(Debug)] 142#[derive(Debug)]
144pub struct Expansion { 143pub struct Expansion {
145 macro_file_kind: MacroFileKind,
146 macro_call_id: MacroCallId, 144 macro_call_id: MacroCallId,
147} 145}
148 146
@@ -157,7 +155,7 @@ impl Expansion {
157 } 155 }
158 156
159 pub fn file_id(&self) -> HirFileId { 157 pub fn file_id(&self) -> HirFileId {
160 self.macro_call_id.as_file(self.macro_file_kind) 158 self.macro_call_id.as_file()
161 } 159 }
162} 160}
163 161
@@ -456,10 +454,7 @@ impl SourceAnalyzer {
456 macro_call.file_id, 454 macro_call.file_id,
457 db.ast_id_map(macro_call.file_id).ast_id(macro_call.value), 455 db.ast_id_map(macro_call.file_id).ast_id(macro_call.value),
458 ); 456 );
459 Some(Expansion { 457 Some(Expansion { macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)) })
460 macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)),
461 macro_file_kind: to_macro_file_kind(macro_call.value),
462 })
463 } 458 }
464} 459}
465 460
@@ -543,35 +538,3 @@ fn adjust(
543 }) 538 })
544 .map(|(_ptr, scope)| *scope) 539 .map(|(_ptr, scope)| *scope)
545} 540}
546
547/// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to.
548/// FIXME: Not completed
549fn to_macro_file_kind(macro_call: &ast::MacroCall) -> MacroFileKind {
550 let syn = macro_call.syntax();
551 let parent = match syn.parent() {
552 Some(it) => it,
553 None => {
554 // FIXME:
555 // If it is root, which means the parent HirFile
556 // MacroKindFile must be non-items
557 // return expr now.
558 return MacroFileKind::Expr;
559 }
560 };
561
562 match parent.kind() {
563 MACRO_ITEMS | SOURCE_FILE => MacroFileKind::Items,
564 LET_STMT => {
565 // FIXME: Handle Pattern
566 MacroFileKind::Expr
567 }
568 EXPR_STMT => MacroFileKind::Statements,
569 BLOCK => MacroFileKind::Statements,
570 ARG_LIST => MacroFileKind::Expr,
571 TRY_EXPR => MacroFileKind::Expr,
572 _ => {
573 // Unknown , Just guess it is `Items`
574 MacroFileKind::Items
575 }
576 }
577}
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 7b385f3fd..b3bc336cf 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -6,9 +6,7 @@ pub mod scope;
6use std::{ops::Index, sync::Arc}; 6use std::{ops::Index, sync::Arc};
7 7
8use either::Either; 8use either::Either;
9use hir_expand::{ 9use hir_expand::{hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId};
10 hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId, MacroFileKind,
11};
12use ra_arena::{map::ArenaMap, Arena}; 10use ra_arena::{map::ArenaMap, Arena};
13use ra_syntax::{ast, AstNode, AstPtr}; 11use ra_syntax::{ast, AstNode, AstPtr};
14use rustc_hash::FxHashMap; 12use rustc_hash::FxHashMap;
@@ -49,7 +47,7 @@ impl Expander {
49 if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) { 47 if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) {
50 if let Some(def) = self.resolve_path_as_macro(db, &path) { 48 if let Some(def) = self.resolve_path_as_macro(db, &path) {
51 let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id)); 49 let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id));
52 let file_id = call_id.as_file(MacroFileKind::Expr); 50 let file_id = call_id.as_file();
53 if let Some(node) = db.parse_or_expand(file_id) { 51 if let Some(node) = db.parse_or_expand(file_id) {
54 if let Some(expr) = ast::Expr::cast(node) { 52 if let Some(expr) = ast::Expr::cast(node) {
55 log::debug!("macro expansion {:#?}", expr.syntax()); 53 log::debug!("macro expansion {:#?}", expr.syntax());
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 08693cb13..6a01e3ab7 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -7,7 +7,7 @@ use hir_expand::{
7 builtin_derive::find_builtin_derive, 7 builtin_derive::find_builtin_derive,
8 builtin_macro::find_builtin_macro, 8 builtin_macro::find_builtin_macro,
9 name::{self, AsName, Name}, 9 name::{self, AsName, Name},
10 HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, MacroFileKind, 10 HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
11}; 11};
12use ra_cfg::CfgOptions; 12use ra_cfg::CfgOptions;
13use ra_db::{CrateId, FileId}; 13use ra_db::{CrateId, FileId};
@@ -545,7 +545,7 @@ where
545 self.macro_stack_monitor.increase(macro_def_id); 545 self.macro_stack_monitor.increase(macro_def_id);
546 546
547 if !self.macro_stack_monitor.is_poison(macro_def_id) { 547 if !self.macro_stack_monitor.is_poison(macro_def_id) {
548 let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items); 548 let file_id: HirFileId = macro_call_id.as_file();
549 let raw_items = self.db.raw_items(file_id); 549 let raw_items = self.db.raw_items(file_id);
550 let mod_dir = self.mod_dirs[&module_id].clone(); 550 let mod_dir = self.mod_dirs[&module_id].clone();
551 ModCollector { 551 ModCollector {
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs
index 78fa9b09a..574637602 100644
--- a/crates/ra_hir_expand/src/builtin_derive.rs
+++ b/crates/ra_hir_expand/src/builtin_derive.rs
@@ -208,7 +208,7 @@ fn partial_ord_expand(
208#[cfg(test)] 208#[cfg(test)]
209mod tests { 209mod tests {
210 use super::*; 210 use super::*;
211 use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc, MacroFileKind}; 211 use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
212 use ra_db::{fixture::WithFixture, SourceDatabase}; 212 use ra_db::{fixture::WithFixture, SourceDatabase};
213 213
214 fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String { 214 fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
@@ -229,7 +229,7 @@ mod tests {
229 }; 229 };
230 230
231 let id = db.intern_macro(loc); 231 let id = db.intern_macro(loc);
232 let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Items)).unwrap(); 232 let parsed = db.parse_or_expand(id.as_file()).unwrap();
233 233
234 // FIXME text() for syntax nodes parsed from token tree looks weird 234 // FIXME text() for syntax nodes parsed from token tree looks weird
235 // because there's no whitespace, see below 235 // because there's no whitespace, see below
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index 99303188b..c7071fe85 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -2,8 +2,7 @@
2use crate::db::AstDatabase; 2use crate::db::AstDatabase;
3use crate::{ 3use crate::{
4 ast::{self, AstNode}, 4 ast::{self, AstNode},
5 name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind, 5 name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, TextUnit,
6 TextUnit,
7}; 6};
8 7
9use crate::quote; 8use crate::quote;
@@ -90,7 +89,7 @@ fn line_expand(
90 let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; 89 let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
91 let arg_start = arg.text_range().start(); 90 let arg_start = arg.text_range().start();
92 91
93 let file = id.as_file(MacroFileKind::Expr); 92 let file = id.as_file();
94 let line_num = to_line_number(db, file, arg_start); 93 let line_num = to_line_number(db, file, arg_start);
95 94
96 let expanded = quote! { 95 let expanded = quote! {
@@ -158,7 +157,7 @@ fn column_expand(
158 let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; 157 let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
159 let col_start = macro_call.syntax().text_range().start(); 158 let col_start = macro_call.syntax().text_range().start();
160 159
161 let file = id.as_file(MacroFileKind::Expr); 160 let file = id.as_file();
162 let col_num = to_col_number(db, file, col_start); 161 let col_num = to_col_number(db, file, col_start);
163 162
164 let expanded = quote! { 163 let expanded = quote! {
@@ -269,7 +268,7 @@ mod tests {
269 }; 268 };
270 269
271 let id = db.intern_macro(loc); 270 let id = db.intern_macro(loc);
272 let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Expr)).unwrap(); 271 let parsed = db.parse_or_expand(id.as_file()).unwrap();
273 272
274 parsed.text().to_string() 273 parsed.text().to_string()
275 } 274 }
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index 013a6c8ba..4bdb41619 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -6,11 +6,11 @@ use mbe::MacroRules;
6use ra_db::{salsa, SourceDatabase}; 6use ra_db::{salsa, SourceDatabase};
7use ra_parser::FragmentKind; 7use ra_parser::FragmentKind;
8use ra_prof::profile; 8use ra_prof::profile;
9use ra_syntax::{AstNode, Parse, SyntaxNode}; 9use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
10 10
11use crate::{ 11use crate::{
12 ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, 12 ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
13 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, MacroFileKind, 13 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
14}; 14};
15 15
16#[derive(Debug, Clone, Eq, PartialEq)] 16#[derive(Debug, Clone, Eq, PartialEq)]
@@ -155,11 +155,42 @@ pub(crate) fn parse_macro(
155 }) 155 })
156 .ok()?; 156 .ok()?;
157 157
158 let fragment_kind = match macro_file.macro_file_kind { 158 let fragment_kind = to_fragment_kind(db, macro_call_id);
159 MacroFileKind::Items => FragmentKind::Items, 159
160 MacroFileKind::Expr => FragmentKind::Expr,
161 MacroFileKind::Statements => FragmentKind::Statements,
162 };
163 let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?; 160 let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
164 Some((parse, Arc::new(rev_token_map))) 161 Some((parse, Arc::new(rev_token_map)))
165} 162}
163
164/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
165/// FIXME: Not completed
166fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
167 let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
168
169 let parent = match syn.parent() {
170 Some(it) => it,
171 None => {
172 // FIXME:
173 // If it is root, which means the parent HirFile
174 // MacroKindFile must be non-items
175 // return expr now.
176 return FragmentKind::Expr;
177 }
178 };
179
180 match parent.kind() {
181 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
182 LET_STMT => {
183 // FIXME: Handle Pattern
184 FragmentKind::Expr
185 }
186 EXPR_STMT => FragmentKind::Statements,
187 BLOCK => FragmentKind::Statements,
188 ARG_LIST => FragmentKind::Expr,
189 TRY_EXPR => FragmentKind::Expr,
190 TUPLE_EXPR => FragmentKind::Expr,
191 _ => {
192 // Unknown , Just guess it is `Items`
193 FragmentKind::Items
194 }
195 }
196}
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 0a5da7e54..94e1e466a 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -117,14 +117,6 @@ impl HirFileId {
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 117#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
118pub struct MacroFile { 118pub struct MacroFile {
119 macro_call_id: MacroCallId, 119 macro_call_id: MacroCallId,
120 macro_file_kind: MacroFileKind,
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
124pub enum MacroFileKind {
125 Items,
126 Expr,
127 Statements,
128} 120}
129 121
130/// `MacroCallId` identifies a particular macro invocation, like 122/// `MacroCallId` identifies a particular macro invocation, like
@@ -205,9 +197,8 @@ impl MacroCallKind {
205} 197}
206 198
207impl MacroCallId { 199impl MacroCallId {
208 pub fn as_file(self, kind: MacroFileKind) -> HirFileId { 200 pub fn as_file(self) -> HirFileId {
209 let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind }; 201 MacroFile { macro_call_id: self }.into()
210 macro_file.into()
211 } 202 }
212} 203}
213 204