mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
use loc2id for FnIds
This commit is contained in:
parent
cb22a799d6
commit
4e48917c00
6 changed files with 70 additions and 22 deletions
|
@ -1,7 +1,5 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
|
|
||||||
use ra_editor::LineIndex;
|
use ra_editor::LineIndex;
|
||||||
use ra_syntax::{SourceFileNode, SyntaxNode};
|
use ra_syntax::{SourceFileNode, SyntaxNode};
|
||||||
use salsa::{self, Database};
|
use salsa::{self, Database};
|
||||||
|
@ -11,19 +9,17 @@ use crate::{
|
||||||
descriptors::{
|
descriptors::{
|
||||||
DescriptorDatabase, FnScopesQuery, FnSyntaxQuery, ModuleScopeQuery, ModuleTreeQuery,
|
DescriptorDatabase, FnScopesQuery, FnSyntaxQuery, ModuleScopeQuery, ModuleTreeQuery,
|
||||||
SubmodulesQuery,
|
SubmodulesQuery,
|
||||||
module::{ModuleSource, ModuleId},
|
|
||||||
},
|
},
|
||||||
input::SourceRootId,
|
|
||||||
symbol_index::SymbolIndex,
|
symbol_index::SymbolIndex,
|
||||||
syntax_ptr::SyntaxPtr,
|
syntax_ptr::SyntaxPtr,
|
||||||
loc2id::Loc2IdMap,
|
loc2id::{IdMaps, IdDatabase},
|
||||||
Cancelable, Canceled, FileId,
|
Cancelable, Canceled, FileId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct RootDatabase {
|
pub(crate) struct RootDatabase {
|
||||||
runtime: salsa::Runtime<RootDatabase>,
|
runtime: salsa::Runtime<RootDatabase>,
|
||||||
loc2id: Arc<Mutex<Loc2IdMap<(SourceRootId, ModuleSource), ModuleId>>>,
|
id_maps: IdMaps,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl salsa::Database for RootDatabase {
|
impl salsa::Database for RootDatabase {
|
||||||
|
@ -35,8 +31,8 @@ impl salsa::Database for RootDatabase {
|
||||||
impl Default for RootDatabase {
|
impl Default for RootDatabase {
|
||||||
fn default() -> RootDatabase {
|
fn default() -> RootDatabase {
|
||||||
let mut db = RootDatabase {
|
let mut db = RootDatabase {
|
||||||
runtime: Default::default(),
|
runtime: salsa::Runtime::default(),
|
||||||
loc2id: Default::default(),
|
id_maps: IdMaps::default(),
|
||||||
};
|
};
|
||||||
db.query_mut(crate::input::SourceRootQuery)
|
db.query_mut(crate::input::SourceRootQuery)
|
||||||
.set(crate::input::WORKSPACE, Default::default());
|
.set(crate::input::WORKSPACE, Default::default());
|
||||||
|
@ -60,11 +56,17 @@ impl salsa::ParallelDatabase for RootDatabase {
|
||||||
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
|
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
|
||||||
salsa::Snapshot::new(RootDatabase {
|
salsa::Snapshot::new(RootDatabase {
|
||||||
runtime: self.runtime.snapshot(self),
|
runtime: self.runtime.snapshot(self),
|
||||||
loc2id: Arc::clone(&self.loc2id),
|
id_maps: self.id_maps.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IdDatabase for RootDatabase {
|
||||||
|
fn id_maps(&self) -> &IdMaps {
|
||||||
|
&self.id_maps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
salsa::database_storage! {
|
salsa::database_storage! {
|
||||||
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
||||||
impl crate::input::FilesDatabase {
|
impl crate::input::FilesDatabase {
|
||||||
|
|
|
@ -8,9 +8,9 @@ use crate::descriptors::{
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Resolve `FnId` to the corresponding `SyntaxNode`
|
/// Resolve `FnId` to the corresponding `SyntaxNode`
|
||||||
/// TODO: this should return something more type-safe then `SyntaxNode`
|
|
||||||
pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode {
|
pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode {
|
||||||
let syntax = db.resolve_syntax_ptr(fn_id.0);
|
let ptr = db.id_maps().fn_ptr(fn_id);
|
||||||
|
let syntax = db.resolve_syntax_ptr(ptr);
|
||||||
FnDef::cast(syntax.borrowed()).unwrap().owned()
|
FnDef::cast(syntax.borrowed()).unwrap().owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,17 +8,18 @@ use ra_syntax::{
|
||||||
TextRange, TextUnit,
|
TextRange, TextUnit,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{syntax_ptr::SyntaxPtr, FileId};
|
use crate::{
|
||||||
|
syntax_ptr::SyntaxPtr, FileId,
|
||||||
|
loc2id::IdDatabase,
|
||||||
|
};
|
||||||
|
|
||||||
pub(crate) use self::scope::{resolve_local_name, FnScopes};
|
pub(crate) use self::scope::{resolve_local_name, FnScopes};
|
||||||
|
pub(crate) use crate::loc2id::FnId;
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub(crate) struct FnId(SyntaxPtr);
|
|
||||||
|
|
||||||
impl FnId {
|
impl FnId {
|
||||||
pub(crate) fn new(file_id: FileId, fn_def: ast::FnDef) -> FnId {
|
pub(crate) fn get(db: &impl IdDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
|
||||||
let ptr = SyntaxPtr::new(file_id, fn_def.syntax());
|
let ptr = SyntaxPtr::new(file_id, fn_def.syntax());
|
||||||
FnId(ptr)
|
db.id_maps().fn_id(ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,13 @@ use crate::{
|
||||||
descriptors::function::{resolve_local_name, FnId, FnScopes},
|
descriptors::function::{resolve_local_name, FnId, FnScopes},
|
||||||
descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
|
descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
|
||||||
input::SourceRootId,
|
input::SourceRootId,
|
||||||
|
loc2id::IdDatabase,
|
||||||
syntax_ptr::LocalSyntaxPtr,
|
syntax_ptr::LocalSyntaxPtr,
|
||||||
Cancelable,
|
Cancelable,
|
||||||
};
|
};
|
||||||
|
|
||||||
salsa::query_group! {
|
salsa::query_group! {
|
||||||
pub(crate) trait DescriptorDatabase: SyntaxDatabase {
|
pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase {
|
||||||
fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
|
fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
|
||||||
type ModuleTreeQuery;
|
type ModuleTreeQuery;
|
||||||
use fn module::imp::module_tree;
|
use fn module::imp::module_tree;
|
||||||
|
|
|
@ -621,7 +621,7 @@ fn resolve_local_name(
|
||||||
name_ref: ast::NameRef,
|
name_ref: ast::NameRef,
|
||||||
) -> Option<(SmolStr, TextRange)> {
|
) -> Option<(SmolStr, TextRange)> {
|
||||||
let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
|
let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
|
||||||
let fn_id = FnId::new(file_id, fn_def);
|
let fn_id = FnId::get(db, file_id, fn_def);
|
||||||
let scopes = db.fn_scopes(fn_id);
|
let scopes = db.fn_scopes(fn_id);
|
||||||
let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?;
|
let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?;
|
||||||
let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
|
let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
|
||||||
|
|
|
@ -1,7 +1,16 @@
|
||||||
use std::hash::Hash;
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
hash::Hash,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
syntax_ptr::SyntaxPtr,
|
||||||
|
};
|
||||||
|
|
||||||
/// There are two principle ways to refer to things:
|
/// There are two principle ways to refer to things:
|
||||||
/// - by their locatinon (module in foo/bar/baz.rs at line 42)
|
/// - by their locatinon (module in foo/bar/baz.rs at line 42)
|
||||||
/// - by their numeric id (module `ModuleId(42)`)
|
/// - by their numeric id (module `ModuleId(42)`)
|
||||||
|
@ -53,8 +62,8 @@ where
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn id2loc(&self, id: &ID) -> L {
|
pub fn id2loc(&self, id: ID) -> L {
|
||||||
self.id2loc[id].clone()
|
self.id2loc[&id].clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,3 +71,38 @@ pub(crate) trait NumericId: Clone + Eq + Hash {
|
||||||
fn from_u32(id: u32) -> Self;
|
fn from_u32(id: u32) -> Self;
|
||||||
fn to_u32(self) -> u32;
|
fn to_u32(self) -> u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub(crate) struct FnId(u32);
|
||||||
|
|
||||||
|
impl NumericId for FnId {
|
||||||
|
fn from_u32(id: u32) -> FnId {
|
||||||
|
FnId(id)
|
||||||
|
}
|
||||||
|
fn to_u32(self) -> u32 {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait IdDatabase: salsa::Database {
|
||||||
|
fn id_maps(&self) -> &IdMaps;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub(crate) struct IdMaps {
|
||||||
|
inner: Arc<IdMapsInner>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdMaps {
|
||||||
|
pub(crate) fn fn_id(&self, ptr: SyntaxPtr) -> FnId {
|
||||||
|
self.inner.fns.lock().loc2id(&ptr)
|
||||||
|
}
|
||||||
|
pub(crate) fn fn_ptr(&self, fn_id: FnId) -> SyntaxPtr {
|
||||||
|
self.inner.fns.lock().id2loc(fn_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct IdMapsInner {
|
||||||
|
fns: Mutex<Loc2IdMap<SyntaxPtr, FnId>>,
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue