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 <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot] 2019-12-08 10:35:30 +00:00 committed by GitHub
commit ffcdd25cc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 70 deletions

View File

@ -21,7 +21,6 @@
};
use hir_expand::{
hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
MacroFileKind,
};
use ra_syntax::{
ast::{self, AstNode},
@ -142,7 +141,6 @@ pub struct ReferenceDescriptor {
#[derive(Debug)]
pub struct Expansion {
macro_file_kind: MacroFileKind,
macro_call_id: MacroCallId,
}
@ -157,7 +155,7 @@ pub fn map_token_down(
}
pub fn file_id(&self) -> HirFileId {
self.macro_call_id.as_file(self.macro_file_kind)
self.macro_call_id.as_file()
}
}
@ -456,10 +454,7 @@ pub fn expand(
macro_call.file_id,
db.ast_id_map(macro_call.file_id).ast_id(macro_call.value),
);
Some(Expansion {
macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)),
macro_file_kind: to_macro_file_kind(macro_call.value),
})
Some(Expansion { macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)) })
}
}
@ -543,35 +538,3 @@ fn adjust(
})
.map(|(_ptr, scope)| *scope)
}
/// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to.
/// FIXME: Not completed
fn to_macro_file_kind(macro_call: &ast::MacroCall) -> MacroFileKind {
let syn = macro_call.syntax();
let parent = match syn.parent() {
Some(it) => it,
None => {
// FIXME:
// If it is root, which means the parent HirFile
// MacroKindFile must be non-items
// return expr now.
return MacroFileKind::Expr;
}
};
match parent.kind() {
MACRO_ITEMS | SOURCE_FILE => MacroFileKind::Items,
LET_STMT => {
// FIXME: Handle Pattern
MacroFileKind::Expr
}
EXPR_STMT => MacroFileKind::Statements,
BLOCK => MacroFileKind::Statements,
ARG_LIST => MacroFileKind::Expr,
TRY_EXPR => MacroFileKind::Expr,
_ => {
// Unknown , Just guess it is `Items`
MacroFileKind::Items
}
}
}

View File

@ -6,9 +6,7 @@
use std::{ops::Index, sync::Arc};
use either::Either;
use hir_expand::{
hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId, MacroFileKind,
};
use hir_expand::{hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId};
use ra_arena::{map::ArenaMap, Arena};
use ra_syntax::{ast, AstNode, AstPtr};
use rustc_hash::FxHashMap;
@ -49,7 +47,7 @@ fn enter_expand(
if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) {
if let Some(def) = self.resolve_path_as_macro(db, &path) {
let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id));
let file_id = call_id.as_file(MacroFileKind::Expr);
let file_id = call_id.as_file();
if let Some(node) = db.parse_or_expand(file_id) {
if let Some(expr) = ast::Expr::cast(node) {
log::debug!("macro expansion {:#?}", expr.syntax());

View File

@ -7,7 +7,7 @@
builtin_derive::find_builtin_derive,
builtin_macro::find_builtin_macro,
name::{self, AsName, Name},
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, MacroFileKind,
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
};
use ra_cfg::CfgOptions;
use ra_db::{CrateId, FileId};
@ -545,7 +545,7 @@ fn collect_macro_expansion(
self.macro_stack_monitor.increase(macro_def_id);
if !self.macro_stack_monitor.is_poison(macro_def_id) {
let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items);
let file_id: HirFileId = macro_call_id.as_file();
let raw_items = self.db.raw_items(file_id);
let mod_dir = self.mod_dirs[&module_id].clone();
ModCollector {

View File

@ -208,7 +208,7 @@ fn partial_ord_expand(
#[cfg(test)]
mod tests {
use super::*;
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc, MacroFileKind};
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
use ra_db::{fixture::WithFixture, SourceDatabase};
fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
@ -229,7 +229,7 @@ fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
};
let id = db.intern_macro(loc);
let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Items)).unwrap();
let parsed = db.parse_or_expand(id.as_file()).unwrap();
// FIXME text() for syntax nodes parsed from token tree looks weird
// because there's no whitespace, see below

View File

@ -2,8 +2,7 @@
use crate::db::AstDatabase;
use crate::{
ast::{self, AstNode},
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
TextUnit,
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, TextUnit,
};
use crate::quote;
@ -90,7 +89,7 @@ fn line_expand(
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
let arg_start = arg.text_range().start();
let file = id.as_file(MacroFileKind::Expr);
let file = id.as_file();
let line_num = to_line_number(db, file, arg_start);
let expanded = quote! {
@ -158,7 +157,7 @@ fn column_expand(
let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
let col_start = macro_call.syntax().text_range().start();
let file = id.as_file(MacroFileKind::Expr);
let file = id.as_file();
let col_num = to_col_number(db, file, col_start);
let expanded = quote! {
@ -269,7 +268,7 @@ fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String {
};
let id = db.intern_macro(loc);
let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Expr)).unwrap();
let parsed = db.parse_or_expand(id.as_file()).unwrap();
parsed.text().to_string()
}

View File

@ -6,11 +6,11 @@
use ra_db::{salsa, SourceDatabase};
use ra_parser::FragmentKind;
use ra_prof::profile;
use ra_syntax::{AstNode, Parse, SyntaxNode};
use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
use crate::{
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, MacroFileKind,
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
};
#[derive(Debug, Clone, Eq, PartialEq)]
@ -155,11 +155,42 @@ pub(crate) fn parse_macro(
})
.ok()?;
let fragment_kind = match macro_file.macro_file_kind {
MacroFileKind::Items => FragmentKind::Items,
MacroFileKind::Expr => FragmentKind::Expr,
MacroFileKind::Statements => FragmentKind::Statements,
};
let fragment_kind = to_fragment_kind(db, macro_call_id);
let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
Some((parse, Arc::new(rev_token_map)))
}
/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
/// FIXME: Not completed
fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
let parent = match syn.parent() {
Some(it) => it,
None => {
// FIXME:
// If it is root, which means the parent HirFile
// MacroKindFile must be non-items
// return expr now.
return FragmentKind::Expr;
}
};
match parent.kind() {
MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
LET_STMT => {
// FIXME: Handle Pattern
FragmentKind::Expr
}
EXPR_STMT => FragmentKind::Statements,
BLOCK => FragmentKind::Statements,
ARG_LIST => FragmentKind::Expr,
TRY_EXPR => FragmentKind::Expr,
TUPLE_EXPR => FragmentKind::Expr,
_ => {
// Unknown , Just guess it is `Items`
FragmentKind::Items
}
}
}

View File

@ -117,14 +117,6 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroFile {
macro_call_id: MacroCallId,
macro_file_kind: MacroFileKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroFileKind {
Items,
Expr,
Statements,
}
/// `MacroCallId` identifies a particular macro invocation, like
@ -205,9 +197,8 @@ pub fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> {
}
impl MacroCallId {
pub fn as_file(self, kind: MacroFileKind) -> HirFileId {
let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind };
macro_file.into()
pub fn as_file(self) -> HirFileId {
MacroFile { macro_call_id: self }.into()
}
}