2019-10-29 14:55:39 +03:00
|
|
|
//! hir makes 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
|
|
|
|
//! (but for hir objects).
|
|
|
|
//!
|
|
|
|
//! This module defines a bunch of ids we are using. The most important ones are
|
|
|
|
//! probably `HirFileId` and `DefId`.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2019-10-29 14:55:39 +03:00
|
|
|
use std::hash::{Hash, Hasher};
|
2019-01-24 15:28:50 +03:00
|
|
|
|
2019-10-29 14:55:39 +03:00
|
|
|
use ra_db::salsa;
|
|
|
|
use ra_syntax::{ast, AstNode};
|
2019-01-01 23:21:16 +03:00
|
|
|
|
2019-09-08 09:53:49 +03:00
|
|
|
use crate::{
|
2019-10-28 20:29:57 +03:00
|
|
|
db::{AstDatabase, InternDatabase},
|
2019-10-29 14:55:39 +03:00
|
|
|
AstId, FileAstId, Module, Source,
|
2019-09-08 09:53:49 +03:00
|
|
|
};
|
2019-01-06 15:16:21 +03:00
|
|
|
|
2019-10-29 15:11:42 +03:00
|
|
|
pub use hir_expand::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, MacroFileKind};
|
2019-05-04 17:42:24 +03:00
|
|
|
|
2019-04-09 22:51:22 +03:00
|
|
|
macro_rules! impl_intern_key {
|
|
|
|
($name:ident) => {
|
|
|
|
impl salsa::InternKey for $name {
|
|
|
|
fn from_intern_id(v: salsa::InternId) -> Self {
|
|
|
|
$name(v)
|
|
|
|
}
|
|
|
|
fn as_intern_id(&self) -> salsa::InternId {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-25 01:05:50 +03:00
|
|
|
#[derive(Debug)]
|
2019-01-24 15:28:50 +03:00
|
|
|
pub struct ItemLoc<N: AstNode> {
|
|
|
|
pub(crate) module: Module,
|
2019-03-26 18:27:22 +03:00
|
|
|
ast_id: AstId<N>,
|
2019-01-24 15:28:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 01:05:50 +03:00
|
|
|
impl<N: AstNode> PartialEq for ItemLoc<N> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2019-03-26 18:27:22 +03:00
|
|
|
self.module == other.module && self.ast_id == other.ast_id
|
2019-01-25 01:05:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<N: AstNode> Eq for ItemLoc<N> {}
|
|
|
|
impl<N: AstNode> Hash for ItemLoc<N> {
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
self.module.hash(hasher);
|
2019-03-26 18:27:22 +03:00
|
|
|
self.ast_id.hash(hasher);
|
2019-01-25 01:05:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 00:26:54 +03:00
|
|
|
impl<N: AstNode> Clone for ItemLoc<N> {
|
|
|
|
fn clone(&self) -> ItemLoc<N> {
|
2019-03-26 18:27:22 +03:00
|
|
|
ItemLoc { module: self.module, ast_id: self.ast_id }
|
2019-01-24 15:28:50 +03:00
|
|
|
}
|
2019-01-25 00:26:54 +03:00
|
|
|
}
|
2019-01-24 15:28:50 +03:00
|
|
|
|
2019-01-25 00:26:54 +03:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) struct LocationCtx<DB> {
|
|
|
|
db: DB,
|
|
|
|
module: Module,
|
|
|
|
file_id: HirFileId,
|
2019-01-24 15:28:50 +03:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:29:57 +03:00
|
|
|
impl<'a, DB> LocationCtx<&'a DB> {
|
2019-01-25 00:26:54 +03:00
|
|
|
pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> {
|
2019-02-08 14:49:43 +03:00
|
|
|
LocationCtx { db, module, file_id }
|
2019-01-24 15:28:50 +03:00
|
|
|
}
|
2019-06-26 21:50:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:39 +03:00
|
|
|
impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> {
|
2019-01-25 00:26:54 +03:00
|
|
|
pub(crate) fn to_def<N, DEF>(self, ast: &N) -> DEF
|
|
|
|
where
|
2019-01-25 01:05:50 +03:00
|
|
|
N: AstNode,
|
2019-01-25 00:26:54 +03:00
|
|
|
DEF: AstItemDef<N>,
|
|
|
|
{
|
|
|
|
DEF::from_ast(self, ast)
|
|
|
|
}
|
2019-01-24 15:28:50 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 22:51:22 +03:00
|
|
|
pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
|
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
|
2019-04-09 22:51:22 +03:00
|
|
|
|
2019-10-29 14:55:39 +03:00
|
|
|
fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self {
|
2019-03-26 19:00:11 +03:00
|
|
|
let items = ctx.db.ast_id_map(ctx.file_id);
|
2019-03-26 18:27:22 +03:00
|
|
|
let item_id = items.ast_id(ast);
|
|
|
|
Self::from_ast_id(ctx, item_id)
|
2019-03-02 23:59:04 +03:00
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
|
2019-10-29 11:15:51 +03:00
|
|
|
let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
|
2019-04-09 22:51:22 +03:00
|
|
|
Self::intern(ctx.db, loc)
|
2019-01-25 00:26:54 +03:00
|
|
|
}
|
2019-10-29 14:55:39 +03:00
|
|
|
fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source<N> {
|
2019-04-09 22:51:22 +03:00
|
|
|
let loc = self.lookup_intern(db);
|
2019-03-26 18:27:22 +03:00
|
|
|
let ast = loc.ast_id.to_node(db);
|
2019-06-11 18:07:42 +03:00
|
|
|
Source { file_id: loc.ast_id.file_id(), ast }
|
2019-01-25 00:02:18 +03:00
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn module(self, db: &impl InternDatabase) -> Module {
|
2019-04-09 22:51:22 +03:00
|
|
|
let loc = self.lookup_intern(db);
|
2019-01-25 00:02:18 +03:00
|
|
|
loc.module
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 13:34:41 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct FunctionId(salsa::InternId);
|
|
|
|
impl_intern_key!(FunctionId);
|
|
|
|
|
2019-01-25 00:02:18 +03:00
|
|
|
impl AstItemDef<ast::FnDef> for FunctionId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_function(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_function(self)
|
2019-01-24 13:34:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:18:20 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct StructId(salsa::InternId);
|
|
|
|
impl_intern_key!(StructId);
|
2019-01-25 00:02:18 +03:00
|
|
|
impl AstItemDef<ast::StructDef> for StructId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_struct(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_struct(self)
|
2019-01-24 16:18:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 17:56:00 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct EnumId(salsa::InternId);
|
|
|
|
impl_intern_key!(EnumId);
|
2019-01-25 00:02:18 +03:00
|
|
|
impl AstItemDef<ast::EnumDef> for EnumId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_enum(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_enum(self)
|
2019-01-24 17:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 00:50:08 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct ConstId(salsa::InternId);
|
|
|
|
impl_intern_key!(ConstId);
|
2019-01-25 00:50:08 +03:00
|
|
|
impl AstItemDef<ast::ConstDef> for ConstId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_const(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_const(self)
|
2019-01-25 00:50:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct StaticId(salsa::InternId);
|
|
|
|
impl_intern_key!(StaticId);
|
2019-01-25 00:50:08 +03:00
|
|
|
impl AstItemDef<ast::StaticDef> for StaticId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_static(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_static(self)
|
2019-01-25 00:50:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 01:31:32 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct TraitId(salsa::InternId);
|
|
|
|
impl_intern_key!(TraitId);
|
2019-01-25 01:31:32 +03:00
|
|
|
impl AstItemDef<ast::TraitDef> for TraitId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_trait(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_trait(self)
|
2019-01-25 01:31:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-04-09 22:51:22 +03:00
|
|
|
pub struct TypeAliasId(salsa::InternId);
|
|
|
|
impl_intern_key!(TypeAliasId);
|
2019-03-26 19:15:39 +03:00
|
|
|
impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
|
2019-10-28 20:29:57 +03:00
|
|
|
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.intern_type_alias(loc)
|
|
|
|
}
|
2019-10-28 20:29:57 +03:00
|
|
|
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> {
|
2019-04-09 22:51:22 +03:00
|
|
|
db.lookup_intern_type_alias(self)
|
2019-01-25 01:31:32 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-20 23:05:25 +08:00
|
|
|
|
2019-05-01 17:06:11 +02:00
|
|
|
/// This exists just for Chalk, because Chalk just has a single `StructId` where
|
|
|
|
/// we have different kinds of ADTs, primitive types and special type
|
|
|
|
/// constructors like tuples and function pointers.
|
2019-04-20 12:34:36 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct TypeCtorId(salsa::InternId);
|
|
|
|
impl_intern_key!(TypeCtorId);
|
|
|
|
|
2019-05-01 17:06:11 +02:00
|
|
|
/// This exists just for Chalk, because our ImplIds are only unique per module.
|
2019-04-20 12:34:36 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct GlobalImplId(salsa::InternId);
|
|
|
|
impl_intern_key!(GlobalImplId);
|