rust/crates/ra_hir/src/lib.rs

158 lines
3.9 KiB
Rust
Raw Normal View History

2018-11-27 18:42:26 -06:00
//! HIR (previsouly known as descriptors) provides a high-level OO acess to Rust
//! code.
//!
//! The principal difference between HIR and syntax trees is that HIR is bound
//! to a particular crate instance. That is, it has cfg flags and features
//! applied. So, there relation between syntax and HIR is many-to-one.
macro_rules! ctry {
($expr:expr) => {
match $expr {
None => return Ok(None),
Some(it) => it,
}
};
}
2018-11-27 19:09:44 -06:00
pub mod db;
2018-11-28 07:19:01 -06:00
#[cfg(test)]
mod mock;
2018-11-27 18:42:26 -06:00
mod query_definitions;
mod function;
mod module;
mod path;
mod arena;
pub mod source_binder;
2018-11-27 18:42:26 -06:00
use std::ops::Index;
use ra_syntax::{SyntaxNodeRef, SyntaxNode};
use ra_db::{LocationIntener, SourceRootId, FileId, Cancelable};
use crate::{
db::HirDatabase,
arena::{Arena, Id},
};
2018-11-27 19:09:44 -06:00
pub use self::{
2018-11-27 18:42:26 -06:00
path::{Path, PathKind},
2018-11-27 19:09:44 -06:00
module::{Module, ModuleId, Problem, nameres::ItemMap},
2018-11-27 18:42:26 -06:00
function::{Function, FnScopes},
};
pub use self::function::FnSignatureInfo;
2018-12-04 14:52:14 -06:00
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
/// in a specific module.
2018-11-27 18:42:26 -06:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2018-11-27 19:09:44 -06:00
pub struct DefId(u32);
2018-11-27 18:42:26 -06:00
ra_db::impl_numeric_id!(DefId);
2018-12-04 14:01:53 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DefKind {
Module,
2018-12-04 14:44:00 -06:00
Function,
2018-12-04 14:01:53 -06:00
Item,
}
2018-11-27 18:42:26 -06:00
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2018-12-04 14:01:53 -06:00
pub struct DefLoc {
pub(crate) kind: DefKind,
source_root_id: SourceRootId,
module_id: ModuleId,
source_item_id: SourceItemId,
2018-11-27 18:42:26 -06:00
}
impl DefId {
2018-12-04 14:52:14 -06:00
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
2018-11-27 18:42:26 -06:00
db.as_ref().id2loc(self)
}
}
impl DefLoc {
2018-12-04 14:52:14 -06:00
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
2018-11-27 18:42:26 -06:00
db.as_ref().loc2id(&self)
}
}
2018-11-27 19:09:44 -06:00
pub enum Def {
2018-11-27 18:42:26 -06:00
Module(Module),
2018-12-04 14:44:00 -06:00
Function(Function),
2018-11-27 18:42:26 -06:00
Item,
}
impl DefId {
2018-11-27 19:09:44 -06:00
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
2018-11-27 18:42:26 -06:00
let loc = self.loc(db);
2018-12-04 14:01:53 -06:00
let res = match loc.kind {
DefKind::Module => {
2018-12-04 14:52:14 -06:00
let module = Module::new(db, loc.source_root_id, loc.module_id)?;
Def::Module(module)
2018-11-27 18:42:26 -06:00
}
2018-12-04 14:52:14 -06:00
DefKind::Function => {
let function = Function::new(self);
Def::Function(function)
}
DefKind::Item => Def::Item,
2018-11-27 18:42:26 -06:00
};
Ok(res)
}
}
/// Identifier of item within a specific file. This is stable over reparses, so
/// it's OK to use it as a salsa key/value.
pub(crate) type SourceFileItemId = Id<SyntaxNode>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2018-11-27 19:09:44 -06:00
pub struct SourceItemId {
2018-11-27 18:42:26 -06:00
file_id: FileId,
item_id: SourceFileItemId,
}
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
2018-12-09 04:18:46 -06:00
#[derive(Debug, PartialEq, Eq)]
2018-11-27 19:09:44 -06:00
pub struct SourceFileItems {
2018-12-09 04:18:46 -06:00
file_id: FileId,
2018-11-27 18:42:26 -06:00
arena: Arena<SyntaxNode>,
}
impl SourceFileItems {
2018-12-09 04:18:46 -06:00
fn new(file_id: FileId) -> SourceFileItems {
SourceFileItems {
file_id,
arena: Arena::default(),
}
}
2018-11-27 18:42:26 -06:00
fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
self.arena.alloc(item)
}
2018-12-09 04:18:46 -06:00
pub fn id_of(&self, file_id: FileId, item: SyntaxNodeRef) -> SourceFileItemId {
assert_eq!(
self.file_id, file_id,
"SourceFileItems: wrong file, expected {:?}, got {:?}",
self.file_id, file_id
);
self.id_of_unchecked(item)
}
fn id_of_unchecked(&self, item: SyntaxNodeRef) -> SourceFileItemId {
2018-11-27 18:42:26 -06:00
let (id, _item) = self
.arena
.iter()
.find(|(_id, i)| i.borrowed() == item)
.unwrap();
id
}
2018-12-04 13:46:23 -06:00
pub fn id_of_source_file(&self) -> SourceFileItemId {
let (id, _syntax) = self.arena.iter().next().unwrap();
id
}
2018-11-27 18:42:26 -06:00
}
impl Index<SourceFileItemId> for SourceFileItems {
type Output = SyntaxNode;
fn index(&self, idx: SourceFileItemId) -> &SyntaxNode {
&self.arena[idx]
}
}