Skip to content

Commit

Permalink
Make HirDb completely span-independent from the perspective of exte…
Browse files Browse the repository at this point in the history
…rnal crates
  • Loading branch information
Y-Nak committed Apr 8, 2023
1 parent 1b2f5a8 commit c983488
Show file tree
Hide file tree
Showing 19 changed files with 460 additions and 404 deletions.
2 changes: 1 addition & 1 deletion crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use common::diagnostics::{CompleteDiagnostic, GlobalErrorCode};

use crate::span::db::SpannedHirDb;
use crate::SpannedHirDb;

/// All diagnostics accumulated in salsa-db should implement
/// [`DiagnosticVoucher`] which defines the conversion into
Expand Down
14 changes: 7 additions & 7 deletions crates/hir/src/hir_def/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use cranelift_entity::{EntityRef, PrimaryMap, SecondaryMap};
use parser::ast::{self, prelude::*};
use rustc_hash::FxHashMap;

use crate::span::{HirOrigin, LocalOrigin};
use crate::span::HirOrigin;

use super::{Expr, ExprId, Partial, Pat, PatId, Stmt, StmtId, TrackedItemId};
use super::{Expr, ExprId, Partial, Pat, PatId, Stmt, StmtId, TopLevelMod, TrackedItemId};

#[salsa::tracked]
pub struct Body {
Expand All @@ -24,10 +24,10 @@ pub struct Body {
pub exprs: NodeStore<ExprId, Partial<Expr>>,
#[return_ref]
pub pats: NodeStore<PatId, Partial<Pat>>,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) source_map: BodySourceMap,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Expr>,
}
Expand Down Expand Up @@ -57,21 +57,21 @@ where
Ast: SourceAst,
Node: EntityRef,
{
pub node_to_source: SecondaryMap<Node, LocalOrigin<Ast>>,
pub source_to_node: FxHashMap<LocalOrigin<Ast>, Node>,
pub node_to_source: SecondaryMap<Node, HirOrigin<Ast>>,
pub source_to_node: FxHashMap<HirOrigin<Ast>, Node>,
}

impl<Ast, Node> SourceNodeMap<Ast, Node>
where
Ast: SourceAst,
Node: EntityRef,
{
pub(crate) fn insert(&mut self, node: Node, ast: LocalOrigin<Ast>) {
pub(crate) fn insert(&mut self, node: Node, ast: HirOrigin<Ast>) {
self.node_to_source[node] = ast.clone();
self.source_to_node.insert(ast, node);
}

pub(crate) fn node_to_source(&self, node: Node) -> &LocalOrigin<Ast> {
pub(crate) fn node_to_source(&self, node: Node) -> &HirOrigin<Ast> {
&self.node_to_source[node]
}
}
Expand Down
31 changes: 28 additions & 3 deletions crates/hir/src/hir_def/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// that may take many arguments depending on the number of fields in the struct.
#![allow(clippy::too_many_arguments)]

use common::{InputFile, InputIngot};
use parser::ast;

use crate::{
hir_def::TraitRef,
lower,
span::{
item::{
LazyConstSpan, LazyContractSpan, LazyEnumSpan, LazyExternFnSpan, LazyFnSpan,
Expand All @@ -15,10 +17,12 @@ use crate::{
},
HirOrigin,
},
HirDb,
};

use super::{
AttrListId, Body, FnParamListId, GenericParamListId, IdentId, Partial, TypeId, WhereClauseId,
ingot_module_tree_impl, AttrListId, Body, FnParamListId, GenericParamListId, IdentId,
IngotModuleTree, ItemTree, Partial, TypeId, WhereClauseId,
};

#[derive(
Expand Down Expand Up @@ -58,13 +62,21 @@ pub struct TopLevelMod {
// of `module_item_tree`.
pub name: IdentId,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Root>,
pub(crate) ingot: InputIngot,
pub(crate) file: InputFile,
}
impl TopLevelMod {
pub fn lazy_span(self) -> LazyTopLevelModSpan {
LazyTopLevelModSpan::new(self)
}

pub fn module_item_tree(self, db: &dyn HirDb) -> &ItemTree {
lower::module_item_tree_impl(db, self)
}

pub fn ingot_module_tree(self, db: &dyn HirDb) -> &IngotModuleTree {
ingot_module_tree_impl(db, self.ingot(db))
}
}

#[salsa::tracked]
Expand All @@ -76,6 +88,8 @@ pub struct Mod {
pub attributes: AttrListId,
pub is_pub: bool,

pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Mod>,
}
Expand All @@ -98,6 +112,7 @@ pub struct Func {
pub ret_ty: Option<TypeId>,
pub modifier: ItemModifier,
pub body: Option<Body>,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Fn>,
Expand All @@ -118,6 +133,7 @@ pub struct ExternFunc {
pub params: Partial<FnParamListId>,
pub ret_ty: Option<TypeId>,
pub modifier: ItemModifier,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Fn>,
Expand All @@ -139,6 +155,7 @@ pub struct Struct {
pub generic_params: GenericParamListId,
pub where_clause: WhereClauseId,
pub fields: RecordFieldListId,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Struct>,
Expand All @@ -158,6 +175,7 @@ pub struct Contract {
pub attributes: AttrListId,
pub is_pub: bool,
pub fields: RecordFieldListId,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Contract>,
Expand All @@ -179,6 +197,7 @@ pub struct Enum {
pub generic_params: GenericParamListId,
pub where_clause: WhereClauseId,
pub variants: EnumVariantListId,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Enum>,
Expand All @@ -200,6 +219,7 @@ pub struct TypeAlias {
pub generic_params: GenericParamListId,
pub where_clause: WhereClauseId,
pub ty: Partial<TypeId>,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::TypeAlias>,
Expand All @@ -219,6 +239,7 @@ pub struct Impl {
pub attributes: AttrListId,
pub generic_params: GenericParamListId,
pub where_clause: WhereClauseId,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Impl>,
Expand All @@ -240,6 +261,7 @@ pub struct Trait {
pub is_pub: bool,
pub generic_params: GenericParamListId,
pub where_clause: WhereClauseId,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Trait>,
Expand All @@ -260,6 +282,7 @@ pub struct ImplTrait {
pub attributes: AttrListId,
pub generic_params: GenericParamListId,
pub where_clause: WhereClauseId,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::ImplTrait>,
Expand All @@ -277,6 +300,7 @@ pub struct Const {

pub name: Partial<IdentId>,
pub body: Partial<Body>,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Const>,
Expand All @@ -293,6 +317,7 @@ pub struct Use {
id: TrackedItemId,

pub tree: Partial<super::UseTreeId>,
pub top_mod: TopLevelMod,

#[return_ref]
pub(crate) origin: HirOrigin<ast::Use>,
Expand Down
23 changes: 1 addition & 22 deletions crates/hir/src/hir_def/item_tree.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use std::collections::{BTreeMap, BTreeSet};

use common::InputFile;
use parser::{
ast::{self, prelude::*},
SyntaxNode,
};

use crate::{
hir_def::{module_tree, TopLevelMod},
lower, HirDb,
};
use crate::hir_def::TopLevelMod;

use super::ItemKind;

Expand All @@ -18,7 +9,6 @@ use super::ItemKind;
/// `module_tree::TopLevelModule`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItemTree {
pub file: InputFile,
pub top_mod: TopLevelMod,
pub(crate) item_tree: BTreeMap<ItemKind, ItemTreeNode>,
}
Expand Down Expand Up @@ -51,17 +41,6 @@ pub(crate) struct ItemTreeNode {
pub(crate) children: BTreeSet<ItemKind>,
}

#[salsa::tracked(return_ref)]
pub fn module_item_tree(db: &dyn HirDb, file: InputFile) -> ItemTree {
let node = SyntaxNode::new_root(crate::parse_file(db, file));
let module_tree = module_tree::ingot_module_tree(db, file.ingot(db.upcast()));

// This cast never fails even if the file content is empty.
let ast_root = ast::Root::cast(node).unwrap();
let top_mod_name = module_tree.module_name(file);
lower::lower_file(db, file, top_mod_name, ast_root)
}

#[cfg(test)]
mod tests {

Expand Down
Loading

0 comments on commit c983488

Please sign in to comment.