Skip to content

Commit 509fedd

Browse files
committed
Remove MacroFileKind
1 parent 9e551d5 commit 509fedd

File tree

7 files changed

+52
-70
lines changed

7 files changed

+52
-70
lines changed

crates/ra_hir/src/source_binder.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use hir_def::{
2121
};
2222
use hir_expand::{
2323
hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
24-
MacroFileKind,
2524
};
2625
use ra_syntax::{
2726
ast::{self, AstNode},
@@ -142,7 +141,6 @@ pub struct ReferenceDescriptor {
142141

143142
#[derive(Debug)]
144143
pub struct Expansion {
145-
macro_file_kind: MacroFileKind,
146144
macro_call_id: MacroCallId,
147145
}
148146

@@ -157,7 +155,7 @@ impl Expansion {
157155
}
158156

159157
pub fn file_id(&self) -> HirFileId {
160-
self.macro_call_id.as_file(self.macro_file_kind)
158+
self.macro_call_id.as_file()
161159
}
162160
}
163161

@@ -456,10 +454,7 @@ impl SourceAnalyzer {
456454
macro_call.file_id,
457455
db.ast_id_map(macro_call.file_id).ast_id(macro_call.value),
458456
);
459-
Some(Expansion {
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-
})
457+
Some(Expansion { macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)) })
463458
}
464459
}
465460

@@ -543,35 +538,3 @@ fn adjust(
543538
})
544539
.map(|(_ptr, scope)| *scope)
545540
}
546-
547-
/// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to.
548-
/// FIXME: Not completed
549-
fn 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-
}

crates/ra_hir_def/src/body.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ pub mod scope;
66
use std::{ops::Index, sync::Arc};
77

88
use either::Either;
9-
use hir_expand::{
10-
hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId, MacroFileKind,
11-
};
9+
use hir_expand::{hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId};
1210
use ra_arena::{map::ArenaMap, Arena};
1311
use ra_syntax::{ast, AstNode, AstPtr};
1412
use rustc_hash::FxHashMap;
@@ -49,7 +47,7 @@ impl Expander {
4947
if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) {
5048
if let Some(def) = self.resolve_path_as_macro(db, &path) {
5149
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();
5351
if let Some(node) = db.parse_or_expand(file_id) {
5452
if let Some(expr) = ast::Expr::cast(node) {
5553
log::debug!("macro expansion {:#?}", expr.syntax());

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hir_expand::{
77
builtin_derive::find_builtin_derive,
88
builtin_macro::find_builtin_macro,
99
name::{self, AsName, Name},
10-
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, MacroFileKind,
10+
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
1111
};
1212
use ra_cfg::CfgOptions;
1313
use ra_db::{CrateId, FileId};
@@ -545,7 +545,7 @@ where
545545
self.macro_stack_monitor.increase(macro_def_id);
546546

547547
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();
549549
let raw_items = self.db.raw_items(file_id);
550550
let mod_dir = self.mod_dirs[&module_id].clone();
551551
ModCollector {

crates/ra_hir_expand/src/builtin_derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn partial_ord_expand(
208208
#[cfg(test)]
209209
mod tests {
210210
use super::*;
211-
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc, MacroFileKind};
211+
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
212212
use ra_db::{fixture::WithFixture, SourceDatabase};
213213

214214
fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
@@ -229,7 +229,7 @@ mod tests {
229229
};
230230

231231
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();
233233

234234
// FIXME text() for syntax nodes parsed from token tree looks weird
235235
// because there's no whitespace, see below

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
use crate::db::AstDatabase;
33
use crate::{
44
ast::{self, AstNode},
5-
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
6-
TextUnit,
5+
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, TextUnit,
76
};
87

98
use crate::quote;
@@ -90,7 +89,7 @@ fn line_expand(
9089
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
9190
let arg_start = arg.text_range().start();
9291

93-
let file = id.as_file(MacroFileKind::Expr);
92+
let file = id.as_file();
9493
let line_num = to_line_number(db, file, arg_start);
9594

9695
let expanded = quote! {
@@ -158,7 +157,7 @@ fn column_expand(
158157
let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
159158
let col_start = macro_call.syntax().text_range().start();
160159

161-
let file = id.as_file(MacroFileKind::Expr);
160+
let file = id.as_file();
162161
let col_num = to_col_number(db, file, col_start);
163162

164163
let expanded = quote! {
@@ -269,7 +268,7 @@ mod tests {
269268
};
270269

271270
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();
273272

274273
parsed.text().to_string()
275274
}

crates/ra_hir_expand/src/db.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use mbe::MacroRules;
66
use ra_db::{salsa, SourceDatabase};
77
use ra_parser::FragmentKind;
88
use ra_prof::profile;
9-
use ra_syntax::{AstNode, Parse, SyntaxNode};
9+
use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
1010

1111
use crate::{
1212
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
13-
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, MacroFileKind,
13+
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
1414
};
1515

1616
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -155,11 +155,42 @@ pub(crate) fn parse_macro(
155155
})
156156
.ok()?;
157157

158-
let fragment_kind = match macro_file.macro_file_kind {
159-
MacroFileKind::Items => FragmentKind::Items,
160-
MacroFileKind::Expr => FragmentKind::Expr,
161-
MacroFileKind::Statements => FragmentKind::Statements,
162-
};
158+
let fragment_kind = to_fragment_kind(db, macro_call_id);
159+
163160
let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
164161
Some((parse, Arc::new(rev_token_map)))
165162
}
163+
164+
/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
165+
/// FIXME: Not completed
166+
fn 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+
}

crates/ra_hir_expand/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ impl HirFileId {
117117
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
118118
pub struct MacroFile {
119119
macro_call_id: MacroCallId,
120-
macro_file_kind: MacroFileKind,
121-
}
122-
123-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
124-
pub enum MacroFileKind {
125-
Items,
126-
Expr,
127-
Statements,
128120
}
129121

130122
/// `MacroCallId` identifies a particular macro invocation, like
@@ -205,9 +197,8 @@ impl MacroCallKind {
205197
}
206198

207199
impl MacroCallId {
208-
pub fn as_file(self, kind: MacroFileKind) -> HirFileId {
209-
let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind };
210-
macro_file.into()
200+
pub fn as_file(self) -> HirFileId {
201+
MacroFile { macro_call_id: self }.into()
211202
}
212203
}
213204

0 commit comments

Comments
 (0)