rename MFileId -> HirFileId

This commit is contained in:
Aleksey Kladov 2019-01-01 23:21:16 +03:00
parent 9c65e61849
commit 37ed2f35ba
13 changed files with 135 additions and 99 deletions

View file

@ -27,7 +27,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
match res.import {
None => true,
Some(import) => {
let range = import.range(ctx.db, module.source().file_id());
let range = import.range(ctx.db, module.file_id());
!range.is_subrange(&ctx.leaf.range())
}
}

View file

@ -91,7 +91,7 @@ salsa::database_storage! {
fn library_symbols() for symbol_index::LibrarySymbolsQuery;
}
impl hir::db::HirDatabase {
fn m_source_file() for hir::db::MSourceFileQuery;
fn hir_source_file() for hir::db::HirSourceFileQuery;
fn expand_macro_invocation() for hir::db::ExpandMacroCallQuery;
fn module_tree() for hir::db::ModuleTreeQuery;
fn fn_scopes() for hir::db::FnScopesQuery;

View file

@ -230,7 +230,7 @@ impl AnalysisImpl {
Some(it) => it,
};
let root = descr.crate_root();
let file_id = root.source().file_id();
let file_id = root.file_id();
let crate_graph = self.db.crate_graph();
let crate_id = crate_graph.crate_id_for_crate_root(file_id);
@ -283,7 +283,7 @@ impl AnalysisImpl {
if let Some(child_module) =
source_binder::module_from_declaration(&*self.db, position.file_id, module)?
{
let file_id = child_module.source().file_id();
let file_id = child_module.file_id();
let name = match child_module.name() {
Some(name) => name.to_string().into(),
None => "".into(),

View file

@ -4,7 +4,7 @@ use ra_syntax::{SyntaxNode, SourceFileNode};
use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, Cancelable};
use crate::{
DefLoc, DefId, Name, MFileId,
DefLoc, DefId, Name, HirFileId,
SourceFileItems, SourceItemId,
query_definitions,
FnScopes,
@ -21,9 +21,9 @@ pub trait HirDatabase: SyntaxDatabase
+ AsRef<LocationIntener<DefLoc, DefId>>
+ AsRef<LocationIntener<MacroCallLoc, MacroCallId>>
{
fn m_source_file(mfile_id: MFileId) -> SourceFileNode {
type MSourceFileQuery;
use fn crate::query_definitions::m_source_file;
fn hir_source_file(file_id: HirFileId) -> SourceFileNode {
type HirSourceFileQuery;
use fn HirFileId::source_file_query;
}
fn expand_macro_invocation(invoc: MacroCallId) -> Option<Arc<MacroExpansion>> {
type ExpandMacroCallQuery;
@ -60,7 +60,7 @@ pub trait HirDatabase: SyntaxDatabase
use fn crate::ty::type_for_field;
}
fn file_items(mfile_id: MFileId) -> Arc<SourceFileItems> {
fn file_items(file_id: HirFileId) -> Arc<SourceFileItems> {
type SourceFileItemsQuery;
use fn query_definitions::file_items;
}

View file

@ -1,4 +1,6 @@
use crate::{FileId, MacroCallId};
use crate::{FileId, MacroCallId, HirDatabase};
use ra_syntax::SourceFileNode;
/// hir makes a heavy use of ids: integer (u32) handlers to various things. You
/// can think of id as a pointer (but without a lifetime) or a file descriptor
@ -20,13 +22,53 @@ use crate::{FileId, MacroCallId};
/// (because everything bottoms out at the real `FileId`) and small
/// (`MacroCallId` uses location interner).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MFileId {
pub struct HirFileId(HirFileIdRepr);
impl HirFileId {
pub(crate) fn original_file_id(self, db: &impl HirDatabase) -> FileId {
match self.0 {
HirFileIdRepr::File(file_id) => file_id,
HirFileIdRepr::Macro(macro_call_id) => {
let loc = macro_call_id.loc(db);
loc.source_item_id.file_id.original_file_id(db)
}
}
}
pub(crate) fn as_original_file(self) -> FileId {
match self.0 {
HirFileIdRepr::File(file_id) => file_id,
HirFileIdRepr::Macro(_r) => panic!("macro generated file: {:?}", self),
}
}
pub(crate) fn source_file_query(db: &impl HirDatabase, file_id: HirFileId) -> SourceFileNode {
match file_id.0 {
HirFileIdRepr::File(file_id) => db.source_file(file_id),
HirFileIdRepr::Macro(m) => {
if let Some(exp) = db.expand_macro_invocation(m) {
return exp.file();
}
// returning an empty string looks fishy...
SourceFileNode::parse("")
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum HirFileIdRepr {
File(FileId),
Macro(MacroCallId),
}
impl From<FileId> for MFileId {
fn from(file_id: FileId) -> MFileId {
MFileId::File(file_id)
impl From<FileId> for HirFileId {
fn from(file_id: FileId) -> HirFileId {
HirFileId(HirFileIdRepr::File(file_id))
}
}
impl From<MacroCallId> for HirFileId {
fn from(macro_call_id: MacroCallId) -> HirFileId {
HirFileId(HirFileIdRepr::Macro(macro_call_id))
}
}

View file

@ -1,6 +1,6 @@
pub use ra_db::CrateId;
use crate::{HirDatabase, Module, Cancelable, Name, AsName};
use crate::{HirDatabase, Module, Cancelable, Name, AsName, HirFileId};
/// hir::Crate describes a single crate. It's the main inteface with which
/// crate's dependencies interact. Mostly, it should be just a proxy for the
@ -35,6 +35,7 @@ impl Crate {
let crate_graph = db.crate_graph();
let file_id = crate_graph.crate_root(self.crate_id);
let source_root_id = db.file_source_root(file_id);
let file_id = HirFileId::from(file_id);
let module_tree = db.module_tree(source_root_id)?;
// FIXME: teach module tree about crate roots instead of guessing
let (module_id, _) = ctry!(module_tree

View file

@ -47,7 +47,7 @@ pub use self::{
path::{Path, PathKind},
name::Name,
krate::Crate,
ids::MFileId,
ids::HirFileId,
macros::{MacroDef, MacroInput, MacroExpansion, MacroCallId, MacroCallLoc},
module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution},
function::{Function, FnScopes},
@ -158,7 +158,7 @@ pub(crate) type SourceFileItemId = Id<SyntaxNode>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourceItemId {
mfile_id: MFileId,
file_id: HirFileId,
/// None for the whole file.
item_id: Option<SourceFileItemId>,
}
@ -166,14 +166,14 @@ pub struct SourceItemId {
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
#[derive(Debug, PartialEq, Eq)]
pub struct SourceFileItems {
mfile_id: MFileId,
file_id: HirFileId,
arena: Arena<SyntaxNode>,
}
impl SourceFileItems {
fn new(mfile_id: MFileId, source_file: SourceFile) -> SourceFileItems {
fn new(file_id: HirFileId, source_file: SourceFile) -> SourceFileItems {
let mut res = SourceFileItems {
mfile_id,
file_id,
arena: Arena::default(),
};
res.init(source_file);
@ -193,11 +193,11 @@ impl SourceFileItems {
fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
self.arena.alloc(item)
}
pub fn id_of(&self, mfile_id: MFileId, item: SyntaxNodeRef) -> SourceFileItemId {
pub fn id_of(&self, file_id: HirFileId, item: SyntaxNodeRef) -> SourceFileItemId {
assert_eq!(
self.mfile_id, mfile_id,
self.file_id, file_id,
"SourceFileItems: wrong file, expected {:?}, got {:?}",
self.mfile_id, mfile_id
self.file_id, file_id
);
self.id_of_unchecked(item)
}

View file

@ -189,7 +189,7 @@ salsa::database_storage! {
fn file_lines() for ra_db::FileLinesQuery;
}
impl db::HirDatabase {
fn m_source_file() for db::MSourceFileQuery;
fn hir_source_file() for db::HirSourceFileQuery;
fn expand_macro_invocation() for db::ExpandMacroCallQuery;
fn module_tree() for db::ModuleTreeQuery;
fn fn_scopes() for db::FnScopesQuery;

View file

@ -15,7 +15,7 @@ use relative_path::RelativePathBuf;
use crate::{
Def, DefKind, DefLoc, DefId,
Name, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
MFileId,
HirFileId,
arena::{Arena, Id},
};
@ -49,13 +49,17 @@ impl Module {
/// Returns `None` for the root module
pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> {
let link = self.module_id.parent_link(&self.tree)?;
let file_id = link.owner(&self.tree).source(&self.tree).file_id();
let file_id = link
.owner(&self.tree)
.source(&self.tree)
.file_id()
.as_original_file();
let src = link.bind_source(&self.tree, db);
Some((file_id, src))
}
pub fn source(&self) -> ModuleSource {
self.module_id.source(&self.tree)
pub fn file_id(&self) -> FileId {
self.source().file_id().as_original_file()
}
/// Parent module. Returns `None` if this is a root module.
@ -70,7 +74,7 @@ impl Module {
/// Returns the crate this module is part of.
pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
let root_id = self.module_id.crate_root(&self.tree);
let file_id = root_id.source(&self.tree).file_id();
let file_id = root_id.source(&self.tree).file_id().as_original_file();
let crate_graph = db.crate_graph();
let crate_id = crate_graph.crate_id_for_crate_root(file_id)?;
Some(Crate::new(crate_id))
@ -163,6 +167,10 @@ impl Module {
pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
self.module_id.problems(&self.tree, db)
}
pub(crate) fn source(&self) -> ModuleSource {
self.module_id.source(&self.tree)
}
}
/// Physically, rust source is organized as a set of files, but logically it is
@ -292,34 +300,28 @@ pub struct ModuleData {
impl ModuleSource {
// precondition: item_id **must** point to module
fn new(file_id: FileId, item_id: Option<SourceFileItemId>) -> ModuleSource {
let source_item_id = SourceItemId {
mfile_id: file_id.into(),
item_id,
};
fn new(file_id: HirFileId, item_id: Option<SourceFileItemId>) -> ModuleSource {
let source_item_id = SourceItemId { file_id, item_id };
ModuleSource(source_item_id)
}
pub(crate) fn new_file(file_id: FileId) -> ModuleSource {
pub(crate) fn new_file(file_id: HirFileId) -> ModuleSource {
ModuleSource::new(file_id, None)
}
pub(crate) fn new_inline(
db: &impl HirDatabase,
file_id: FileId,
file_id: HirFileId,
m: ast::Module,
) -> ModuleSource {
assert!(!m.has_semi());
let file_items = db.file_items(file_id.into());
let item_id = file_items.id_of(file_id.into(), m.syntax());
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, m.syntax());
ModuleSource::new(file_id, Some(item_id))
}
pub fn file_id(self) -> FileId {
match self.0.mfile_id {
MFileId::File(file_id) => file_id,
MFileId::Macro(_) => unreachable!(),
}
pub(crate) fn file_id(self) -> HirFileId {
self.0.file_id
}
pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {

View file

@ -64,7 +64,7 @@ fn create_module_tree<'a>(
let source_root = db.source_root(source_root);
for &file_id in source_root.files.values() {
let source = ModuleSource::new_file(file_id);
let source = ModuleSource::new_file(file_id.into());
if visited.contains(&source) {
continue; // TODO: use explicit crate_roots here
}
@ -123,7 +123,7 @@ fn build_subtree(
visited,
roots,
Some(link),
ModuleSource::new_file(file_id),
ModuleSource::new_file(file_id.into()),
),
})
.collect::<Cancelable<Vec<_>>>()?;
@ -155,7 +155,7 @@ fn resolve_submodule(
name: &Name,
) -> (Vec<FileId>, Option<Problem>) {
// FIXME: handle submodules of inline modules properly
let file_id = source.file_id();
let file_id = source.file_id().original_file_id(db);
let source_root_id = db.file_source_root(file_id);
let path = db.file_relative_path(file_id);
let root = RelativePathBuf::default();

View file

@ -25,7 +25,7 @@ use ra_syntax::{
use ra_db::SourceRootId;
use crate::{
Cancelable, MFileId, FileId,
Cancelable, HirFileId, FileId,
DefId, DefLoc, DefKind,
SourceItemId, SourceFileItemId, SourceFileItems,
Path, PathKind,
@ -95,9 +95,11 @@ pub struct NamedImport {
}
impl NamedImport {
// FIXME: this is only here for one use-case in completion. Seems like a
// pretty gross special case.
pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange {
let source_item_id = SourceItemId {
mfile_id: file_id.into(),
file_id: file_id.into(),
item_id: Some(self.file_item_id),
};
let syntax = db.file_item(source_item_id);
@ -211,25 +213,25 @@ impl<T> PerNs<T> {
impl InputModuleItems {
pub(crate) fn add_item(
&mut self,
mfile_id: MFileId,
file_id: HirFileId,
file_items: &SourceFileItems,
item: ast::ModuleItem,
) -> Option<()> {
match item {
ast::ModuleItem::StructDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::EnumDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::FnDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::TraitDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::TypeDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::ImplItem(_) => {
// impls don't define items
@ -239,13 +241,13 @@ impl InputModuleItems {
// TODO
}
ast::ModuleItem::ConstDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::StaticDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItem::Module(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
}
Some(())
@ -269,7 +271,7 @@ impl InputModuleItems {
impl ModuleItem {
fn new<'a>(
mfile_id: MFileId,
file_id: HirFileId,
file_items: &SourceFileItems,
item: impl ast::NameOwner<'a>,
) -> Option<ModuleItem> {
@ -277,7 +279,7 @@ impl ModuleItem {
let kind = item.syntax().kind();
let vis = Vis::Other;
let item_id = Some(file_items.id_of_unchecked(item.syntax()));
let id = SourceItemId { mfile_id, item_id };
let id = SourceItemId { file_id, item_id };
let res = ModuleItem {
id,
name,
@ -339,7 +341,8 @@ where
let root_id = module_id.crate_root(&self.module_tree);
let file_id = root_id.source(&self.module_tree).file_id();
let crate_graph = self.db.crate_graph();
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id) {
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file())
{
let krate = Crate::new(crate_id);
for dep in krate.dependencies(self.db) {
if let Some(module) = dep.krate.root_module(self.db)? {

View file

@ -5,13 +5,13 @@ use std::{
use rustc_hash::FxHashMap;
use ra_syntax::{
AstNode, SyntaxNode, SourceFileNode,
AstNode, SyntaxNode,
ast::{self, NameOwner, ModuleItemOwner}
};
use ra_db::{SourceRootId, FileId, Cancelable,};
use ra_db::{SourceRootId, Cancelable,};
use crate::{
SourceFileItems, SourceItemId, DefKind, Function, DefId, Name, AsName, MFileId,
SourceFileItems, SourceItemId, DefKind, Function, DefId, Name, AsName, HirFileId,
macros::MacroCallLoc,
db::HirDatabase,
function::FnScopes,
@ -48,29 +48,17 @@ pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<
Ok(Arc::new(EnumData::new(enum_def.borrowed())))
}
pub(super) fn m_source_file(db: &impl HirDatabase, mfile_id: MFileId) -> SourceFileNode {
match mfile_id {
MFileId::File(file_id) => db.source_file(file_id),
MFileId::Macro(m) => {
if let Some(exp) = db.expand_macro_invocation(m) {
return exp.file();
}
SourceFileNode::parse("")
}
}
}
pub(super) fn file_items(db: &impl HirDatabase, mfile_id: MFileId) -> Arc<SourceFileItems> {
let source_file = db.m_source_file(mfile_id);
pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> {
let source_file = db.hir_source_file(file_id);
let source_file = source_file.borrowed();
let res = SourceFileItems::new(mfile_id, source_file);
let res = SourceFileItems::new(file_id, source_file);
Arc::new(res)
}
pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode {
match source_item_id.item_id {
Some(id) => db.file_items(source_item_id.mfile_id)[id].clone(),
None => db.m_source_file(source_item_id.mfile_id).syntax().owned(),
Some(id) => db.file_items(source_item_id.file_id)[id].clone(),
None => db.hir_source_file(source_item_id.file_id).syntax().owned(),
}
}
@ -92,7 +80,7 @@ pub(crate) fn submodules(
fn collect_submodules<'a>(
db: &impl HirDatabase,
file_id: FileId,
file_id: HirFileId,
root: impl ast::ModuleItemOwner<'a>,
) -> Vec<Submodule> {
modules(root)
@ -129,13 +117,13 @@ pub(super) fn input_module_items(
) -> Cancelable<Arc<InputModuleItems>> {
let module_tree = db.module_tree(source_root_id)?;
let source = module_id.source(&module_tree);
let mfile_id = source.file_id().into();
let file_items = db.file_items(mfile_id);
let file_id = source.file_id();
let file_items = db.file_items(file_id);
let fill = |acc: &mut InputModuleItems, items: &mut Iterator<Item = ast::ItemOrMacro>| {
for item in items {
match item {
ast::ItemOrMacro::Item(it) => {
acc.add_item(mfile_id, &file_items, it);
acc.add_item(file_id, &file_items, it);
}
ast::ItemOrMacro::Macro(macro_call) => {
let item_id = file_items.id_of_unchecked(macro_call.syntax());
@ -143,16 +131,16 @@ pub(super) fn input_module_items(
source_root_id,
module_id,
source_item_id: SourceItemId {
mfile_id,
file_id,
item_id: Some(item_id),
},
};
let id = loc.id(db);
let mfile_id = MFileId::Macro(id);
let file_items = db.file_items(mfile_id);
let file_id = HirFileId::from(id);
let file_items = db.file_items(file_id);
//FIXME: expand recursively
for item in db.m_source_file(mfile_id).borrowed().items() {
acc.add_item(mfile_id, &file_items, item);
for item in db.hir_source_file(file_id).borrowed().items() {
acc.add_item(file_id, &file_items, item);
}
}
}

View file

@ -20,7 +20,7 @@ use crate::{
/// Locates the module by `FileId`. Picks topmost module in the file.
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> {
let module_source = ModuleSource::new_file(file_id);
let module_source = ModuleSource::new_file(file_id.into());
module_from_source(db, module_source)
}
@ -50,8 +50,8 @@ pub fn module_from_position(
) -> Cancelable<Option<Module>> {
let file = db.source_file(position.file_id);
let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) {
Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m),
_ => ModuleSource::new_file(position.file_id),
Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id.into(), m),
_ => ModuleSource::new_file(position.file_id.into()),
};
module_from_source(db, module_source)
}
@ -67,9 +67,9 @@ pub fn module_from_child_node(
.filter_map(ast::Module::cast)
.find(|it| !it.has_semi())
{
ModuleSource::new_inline(db, file_id, m)
ModuleSource::new_inline(db, file_id.into(), m)
} else {
ModuleSource::new_file(file_id)
ModuleSource::new_file(file_id.into())
};
module_from_source(db, module_source)
}
@ -78,7 +78,7 @@ fn module_from_source(
db: &impl HirDatabase,
module_source: ModuleSource,
) -> Cancelable<Option<Module>> {
let source_root_id = db.file_source_root(module_source.file_id());
let source_root_id = db.file_source_root(module_source.file_id().as_original_file());
let module_tree = db.module_tree(source_root_id)?;
let m = module_tree
.modules_with_sources()
@ -102,11 +102,11 @@ pub fn function_from_module(
module: &Module,
fn_def: ast::FnDef,
) -> Function {
let mfile_id = module.source().file_id().into();
let file_items = db.file_items(mfile_id);
let item_id = file_items.id_of(mfile_id, fn_def.syntax());
let file_id = module.source().file_id();
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, fn_def.syntax());
let source_item_id = SourceItemId {
mfile_id,
file_id,
item_id: Some(item_id),
};
let def_loc = DefLoc {