rust/crates/ra_hir/src/lib.rs

175 lines
4.5 KiB
Rust
Raw Normal View History

2018-11-28 01:42:26 +01: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-28 02:09:44 +01:00
pub mod db;
2018-11-28 14:19:01 +01:00
#[cfg(test)]
mod mock;
2018-11-28 01:42:26 +01:00
mod query_definitions;
mod path;
mod arena;
pub mod source_binder;
2018-11-28 01:42:26 +01:00
2018-12-08 21:40:55 +01:00
mod krate;
mod module;
mod function;
2018-12-20 21:56:28 +01:00
mod ty;
2018-12-08 21:40:55 +01:00
2018-11-28 01:42:26 +01: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-28 02:09:44 +01:00
pub use self::{
2018-11-28 01:42:26 +01:00
path::{Path, PathKind},
2018-12-08 21:40:55 +01:00
krate::Crate,
2018-12-22 00:20:14 +01:00
module::{Module, ModuleId, Problem, nameres::ItemMap, ModuleScope, Resolution},
2018-11-28 01:42:26 +01:00
function::{Function, FnScopes},
};
pub use self::function::FnSignatureInfo;
2018-12-04 21:52:14 +01:00
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
/// in a specific module.
2018-11-28 01:42:26 +01:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2018-11-28 02:09:44 +01:00
pub struct DefId(u32);
2018-11-28 01:42:26 +01:00
ra_db::impl_numeric_id!(DefId);
2018-12-04 21:01:53 +01:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DefKind {
Module,
2018-12-04 21:44:00 +01:00
Function,
2018-12-04 21:01:53 +01:00
Item,
}
2018-11-28 01:42:26 +01:00
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2018-12-04 21:01:53 +01:00
pub struct DefLoc {
pub(crate) kind: DefKind,
source_root_id: SourceRootId,
module_id: ModuleId,
source_item_id: SourceItemId,
2018-11-28 01:42:26 +01:00
}
impl DefId {
2018-12-04 21:52:14 +01:00
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
2018-11-28 01:42:26 +01:00
db.as_ref().id2loc(self)
}
}
impl DefLoc {
2018-12-04 21:52:14 +01:00
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
2018-11-28 01:42:26 +01:00
db.as_ref().loc2id(&self)
}
}
2018-11-28 02:09:44 +01:00
pub enum Def {
2018-11-28 01:42:26 +01:00
Module(Module),
2018-12-04 21:44:00 +01:00
Function(Function),
2018-11-28 01:42:26 +01:00
Item,
}
impl DefId {
2018-11-28 02:09:44 +01:00
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
2018-11-28 01:42:26 +01:00
let loc = self.loc(db);
2018-12-04 21:01:53 +01:00
let res = match loc.kind {
DefKind::Module => {
2018-12-04 21:52:14 +01:00
let module = Module::new(db, loc.source_root_id, loc.module_id)?;
Def::Module(module)
2018-11-28 01:42:26 +01:00
}
2018-12-04 21:52:14 +01:00
DefKind::Function => {
let function = Function::new(self);
Def::Function(function)
}
DefKind::Item => Def::Item,
2018-11-28 01:42:26 +01: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-28 02:09:44 +01:00
pub struct SourceItemId {
2018-11-28 01:42:26 +01:00
file_id: FileId,
/// None for the whole file.
item_id: Option<SourceFileItemId>,
2018-11-28 01:42:26 +01:00
}
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
2018-12-09 11:18:46 +01:00
#[derive(Debug, PartialEq, Eq)]
2018-11-28 02:09:44 +01:00
pub struct SourceFileItems {
2018-12-09 11:18:46 +01:00
file_id: FileId,
2018-11-28 01:42:26 +01:00
arena: Arena<SyntaxNode>,
}
impl SourceFileItems {
2018-12-09 11:18:46 +01:00
fn new(file_id: FileId) -> SourceFileItems {
SourceFileItems {
file_id,
arena: Arena::default(),
}
}
2018-11-28 01:42:26 +01:00
fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
self.arena.alloc(item)
}
2018-12-09 11:18:46 +01: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-12-09 12:21:54 +01:00
if let Some((id, _)) = self.arena.iter().find(|(_id, i)| i.borrowed() == item) {
return id;
}
// This should not happen. Let's try to give a sensible diagnostics.
2018-12-18 12:58:54 +01:00
if let Some((id, i)) = self.arena.iter().find(|(_id, i)| i.range() == item.range()) {
// FIXME(#288): whyyy are we getting here?
log::error!(
2018-12-09 12:21:54 +01:00
"unequal syntax nodes with the same range:\n{:?}\n{:?}",
2018-12-18 12:58:54 +01:00
item,
i
);
return id;
2018-12-09 12:21:54 +01:00
}
panic!(
"Can't find {:?} in SourceFileItems:\n{:?}",
item,
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
);
2018-11-28 01:42:26 +01:00
}
2018-12-04 20:46:23 +01:00
pub fn id_of_source_file(&self) -> SourceFileItemId {
let (id, _syntax) = self.arena.iter().next().unwrap();
id
}
2018-11-28 01:42:26 +01:00
}
impl Index<SourceFileItemId> for SourceFileItems {
type Output = SyntaxNode;
fn index(&self, idx: SourceFileItemId) -> &SyntaxNode {
&self.arena[idx]
}
}