move ids to HIR

This commit is contained in:
Aleksey Kladov 2018-11-28 03:31:50 +03:00
parent 11168c464c
commit f66e5b6e6b
10 changed files with 71 additions and 80 deletions

View file

@ -5,9 +5,8 @@ use salsa::{self, Database};
use ra_db::{LocationIntener, BaseDatabase}; use ra_db::{LocationIntener, BaseDatabase};
use crate::{ use crate::{
hir, hir::{self, DefId, DefLoc, FnId, SourceItemId},
symbol_index, symbol_index,
loc2id::{IdMaps, DefId, DefLoc, FnId},
}; };
#[derive(Debug)] #[derive(Debug)]
@ -21,6 +20,12 @@ pub(crate) struct RootDatabase {
id_maps: Arc<IdMaps>, id_maps: Arc<IdMaps>,
} }
#[derive(Debug, Default)]
struct IdMaps {
fns: LocationIntener<SourceItemId, FnId>,
defs: LocationIntener<DefLoc, DefId>,
}
impl salsa::Database for RootDatabase { impl salsa::Database for RootDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> { fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
&self.runtime &self.runtime

View file

@ -9,13 +9,13 @@ use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase};
use crate::{ use crate::{
FileId, FileId,
hir::{ hir::{
DefLoc, DefId, FnId,
SourceFileItems, SourceItemId, SourceFileItems, SourceItemId,
query_definitions, query_definitions,
function::{FnScopes}, function::{FnScopes},
module::{ModuleId, ModuleTree, ModuleSource, module::{ModuleId, ModuleTree, ModuleSource,
nameres::{ItemMap, InputModuleItems}}, nameres::{ItemMap, InputModuleItems}},
}, },
loc2id::{DefLoc, DefId, FnId},
Cancelable, Cancelable,
}; };

View file

@ -11,12 +11,11 @@ use ra_syntax::{
}; };
use crate::{ use crate::{
hir::{HirDatabase, SourceItemId}, hir::{FnId, HirDatabase, SourceItemId},
FileId, FileId,
}; };
pub(crate) use self::scope::FnScopes; pub(crate) use self::scope::FnScopes;
pub(crate) use crate::loc2id::FnId;
impl FnId { impl FnId {
pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {

View file

@ -14,11 +14,11 @@ mod path;
use std::ops::Index; use std::ops::Index;
use ra_syntax::{SyntaxNodeRef, SyntaxNode}; use ra_syntax::{SyntaxNodeRef, SyntaxNode};
use ra_db::{LocationIntener, SourceRootId};
use crate::{ use crate::{
FileId, FileId,
hir::db::HirDatabase, hir::db::HirDatabase,
loc2id::{DefId, DefLoc},
Cancelable, Cancelable,
arena::{Arena, Id}, arena::{Arena, Id},
}; };
@ -31,6 +31,49 @@ pub(crate) use self::{
pub use self::function::FnSignatureInfo; pub use self::function::FnSignatureInfo;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct FnId(u32);
ra_db::impl_numeric_id!(FnId);
impl FnId {
pub(crate) fn from_loc(
db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
loc: &SourceItemId,
) -> FnId {
db.as_ref().loc2id(loc)
}
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
db.as_ref().id2loc(self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct DefId(u32);
ra_db::impl_numeric_id!(DefId);
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DefLoc {
Module {
id: ModuleId,
source_root: SourceRootId,
},
Item {
source_item_id: SourceItemId,
},
}
impl DefId {
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
db.as_ref().id2loc(self)
}
}
impl DefLoc {
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
db.as_ref().loc2id(&self)
}
}
pub(crate) enum Def { pub(crate) enum Def {
Module(Module), Module(Module),
Item, Item,

View file

@ -15,9 +15,8 @@ use relative_path::RelativePathBuf;
use crate::{ use crate::{
FileId, FilePosition, Cancelable, FileId, FilePosition, Cancelable,
hir::{Path, PathKind, HirDatabase, SourceItemId}, hir::{DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId},
arena::{Arena, Id}, arena::{Arena, Id},
loc2id::{DefLoc, DefId},
}; };
pub(crate) use self::nameres::ModuleScope; pub(crate) use self::nameres::ModuleScope;

View file

@ -28,8 +28,8 @@ use ra_db::SourceRootId;
use crate::{ use crate::{
Cancelable, FileId, Cancelable, FileId,
loc2id::{DefId, DefLoc},
hir::{ hir::{
DefId, DefLoc,
SourceItemId, SourceFileItemId, SourceFileItems, SourceItemId, SourceFileItemId, SourceFileItems,
Path, PathKind, Path, PathKind,
HirDatabase, HirDatabase,

View file

@ -13,9 +13,10 @@ use ra_db::SourceRootId;
use crate::{ use crate::{
FileId, Cancelable, FileId, Cancelable,
hir::{ hir::{
FnId,
SourceFileItems, SourceItemId, SourceFileItems, SourceItemId,
db::HirDatabase, db::HirDatabase,
function::{FnId, FnScopes}, function::FnScopes,
module::{ module::{
ModuleSource, ModuleSourceNode, ModuleId, ModuleSource, ModuleSourceNode, ModuleId,
imp::Submodule, imp::Submodule,

View file

@ -20,7 +20,6 @@ macro_rules! ctry {
mod arena; mod arena;
mod db; mod db;
mod loc2id;
mod imp; mod imp;
mod completion; mod completion;
mod hir; mod hir;

View file

@ -1,69 +0,0 @@
use ra_db::SourceRootId;
use crate::{
hir::{SourceItemId, ModuleId},
};
use ra_db::{NumericId, LocationIntener};
macro_rules! impl_numeric_id {
($id:ident) => {
impl NumericId for $id {
fn from_u32(id: u32) -> Self {
$id(id)
}
fn to_u32(self) -> u32 {
self.0
}
}
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct FnId(u32);
impl_numeric_id!(FnId);
impl FnId {
pub(crate) fn from_loc(
db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
loc: &SourceItemId,
) -> FnId {
db.as_ref().loc2id(loc)
}
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
db.as_ref().id2loc(self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct DefId(u32);
impl_numeric_id!(DefId);
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DefLoc {
Module {
id: ModuleId,
source_root: SourceRootId,
},
Item {
source_item_id: SourceItemId,
},
}
impl DefId {
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
db.as_ref().id2loc(self)
}
}
impl DefLoc {
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
db.as_ref().loc2id(&self)
}
}
#[derive(Debug, Default)]
pub(crate) struct IdMaps {
pub(crate) fns: LocationIntener<SourceItemId, FnId>,
pub(crate) defs: LocationIntener<DefLoc, DefId>,
}

View file

@ -38,6 +38,20 @@ pub use crate::{
loc2id::{LocationIntener, NumericId}, loc2id::{LocationIntener, NumericId},
}; };
#[macro_export]
macro_rules! impl_numeric_id {
($id:ident) => {
impl $crate::NumericId for $id {
fn from_u32(id: u32) -> Self {
$id(id)
}
fn to_u32(self) -> u32 {
self.0
}
}
};
}
pub trait BaseDatabase: salsa::Database { pub trait BaseDatabase: salsa::Database {
fn check_canceled(&self) -> Cancelable<()> { fn check_canceled(&self) -> Cancelable<()> {
if self.salsa_runtime().is_current_revision_canceled() { if self.salsa_runtime().is_current_revision_canceled() {