Skip to content

Remove MacroFileKind #2497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 2 additions & 39 deletions crates/ra_hir/src/source_binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use hir_def::{
};
use hir_expand::{
hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
MacroFileKind,
};
use ra_syntax::{
ast::{self, AstNode},
Expand Down Expand Up @@ -142,7 +141,6 @@ pub struct ReferenceDescriptor {

#[derive(Debug)]
pub struct Expansion {
macro_file_kind: MacroFileKind,
macro_call_id: MacroCallId,
}

Expand All @@ -157,7 +155,7 @@ impl Expansion {
}

pub fn file_id(&self) -> HirFileId {
self.macro_call_id.as_file(self.macro_file_kind)
self.macro_call_id.as_file()
}
}

Expand Down Expand Up @@ -456,10 +454,7 @@ impl SourceAnalyzer {
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)) })
}
}

Expand Down Expand Up @@ -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
}
}
}
6 changes: 2 additions & 4 deletions crates/ra_hir_def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ pub mod scope;
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;
Expand Down Expand Up @@ -49,7 +47,7 @@ impl Expander {
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());
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hir_expand::{
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};
Expand Down Expand Up @@ -545,7 +545,7 @@ where
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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_hir_expand/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -229,7 +229,7 @@ mod tests {
};

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
Expand Down
9 changes: 4 additions & 5 deletions crates/ra_hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -269,7 +268,7 @@ mod tests {
};

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()
}
Expand Down
45 changes: 38 additions & 7 deletions crates/ra_hir_expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use mbe::MacroRules;
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)]
Expand Down Expand Up @@ -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
}
}
}
13 changes: 2 additions & 11 deletions crates/ra_hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ impl HirFileId {
#[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
Expand Down Expand Up @@ -205,9 +197,8 @@ impl MacroCallKind {
}

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()
}
}

Expand Down