Skip to content

Commit

Permalink
refactor: extract mun_hir_input
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Jul 17, 2024
1 parent cf1bae3 commit 9598913
Show file tree
Hide file tree
Showing 75 changed files with 505 additions and 361 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/mun_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ apple-codesign = { workspace = true }
array-init = { workspace = true }
by_address = { workspace = true }
bytemuck = { workspace = true }
mun_db = { version = "0.6.0-dev", path = "../mun_db" }
mun_hir = { version = "0.6.0-dev", path = "../mun_hir" }
mun_hir_input = { version = "0.6.0-dev", path = "../mun_hir_input" }
inkwell = { workspace = true, features = ["llvm14-0", "target-x86", "target-aarch64"] }
itertools = { workspace = true }
mun_codegen_macros = { version = "0.6.0-dev", path = "../mun_codegen_macros" }
Expand Down
4 changes: 1 addition & 3 deletions crates/mun_codegen/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use crate::{AssemblyIr, ModuleGroupId, ModulePartition, TargetAssembly};
/// generation cache is pretty granular there is still a benefit to not having
/// to recompile assemblies if not required.
#[salsa::query_group(CodeGenDatabaseStorage)]
pub trait CodeGenDatabase:
mun_hir::HirDatabase + mun_hir::Upcast<dyn mun_hir::HirDatabase>
{
pub trait CodeGenDatabase: mun_hir::HirDatabase + mun_db::Upcast<dyn mun_hir::HirDatabase> {
/// Set the optimization level used to generate assemblies
#[salsa::input]
fn optimization_level(&self) -> inkwell::OptimizationLevel;
Expand Down
19 changes: 10 additions & 9 deletions crates/mun_codegen/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use mun_hir::{FileId, HirDatabase, SourceDatabase, SourceRoot, SourceRootId};
use mun_hir::HirDatabase;
use mun_hir_input::{FileId, PackageSet, SourceDatabase, SourceRoot, SourceRootId};
use mun_paths::RelativePathBuf;
use mun_target::spec::Target;
use parking_lot::Mutex;
Expand All @@ -13,7 +14,7 @@ use crate::{
/// A mock implementation of the IR database. It can be used to set up a simple
/// test case.
#[salsa::database(
mun_hir::SourceDatabaseStorage,
mun_hir_input::SourceDatabaseStorage,
mun_hir::AstDatabaseStorage,
mun_hir::InternDatabaseStorage,
mun_hir::DefDatabaseStorage,
Expand All @@ -34,31 +35,31 @@ impl salsa::Database for MockDatabase {
}
}

impl mun_hir::Upcast<dyn mun_hir::AstDatabase> for MockDatabase {
impl mun_db::Upcast<dyn mun_hir::AstDatabase> for MockDatabase {
fn upcast(&self) -> &(dyn mun_hir::AstDatabase + 'static) {
self
}
}

impl mun_hir::Upcast<dyn mun_hir::SourceDatabase> for MockDatabase {
fn upcast(&self) -> &(dyn mun_hir::SourceDatabase + 'static) {
impl mun_db::Upcast<dyn SourceDatabase> for MockDatabase {
fn upcast(&self) -> &(dyn SourceDatabase + 'static) {
self
}
}

impl mun_hir::Upcast<dyn mun_hir::DefDatabase> for MockDatabase {
impl mun_db::Upcast<dyn mun_hir::DefDatabase> for MockDatabase {
fn upcast(&self) -> &(dyn mun_hir::DefDatabase + 'static) {
self
}
}

impl mun_hir::Upcast<dyn mun_hir::HirDatabase> for MockDatabase {
impl mun_db::Upcast<dyn mun_hir::HirDatabase> for MockDatabase {
fn upcast(&self) -> &(dyn mun_hir::HirDatabase + 'static) {
self
}
}

impl mun_hir::Upcast<dyn CodeGenDatabase> for MockDatabase {
impl mun_db::Upcast<dyn CodeGenDatabase> for MockDatabase {
fn upcast(&self) -> &(dyn CodeGenDatabase + 'static) {
self
}
Expand Down Expand Up @@ -93,7 +94,7 @@ impl MockDatabase {

db.set_source_root(source_root_id, Arc::new(source_root));

let mut packages = mun_hir::PackageSet::default();
let mut packages = PackageSet::default();
packages.add_package(source_root_id);
db.set_packages(Arc::new(packages));

Expand Down
6 changes: 2 additions & 4 deletions crates/mun_codegen/src/module_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use mun_hir::{HasVisibility, HirDatabase};
use mun_hir_input::FileId;
use rustc_hash::{FxHashMap, FxHashSet};

/// A `ModuleGroup` describes a grouping of modules
Expand Down Expand Up @@ -118,10 +119,7 @@ impl ModuleGroup {
}

/// Returns the `mun_hir::FileId`s that are included in this module group.
pub fn files<'s>(
&'s self,
db: &'s dyn HirDatabase,
) -> impl Iterator<Item = mun_hir::FileId> + 's {
pub fn files<'s>(&'s self, db: &'s dyn HirDatabase) -> impl Iterator<Item = FileId> + 's {
self.ordered_modules
.iter()
.filter_map(move |module| module.file_id(db))
Expand Down
5 changes: 3 additions & 2 deletions crates/mun_codegen/src/module_partition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{ops::Index, sync::Arc};

use mun_hir_input::FileId;
use rustc_hash::FxHashMap;

use crate::{CodeGenDatabase, ModuleGroup};
Expand All @@ -14,7 +15,7 @@ pub struct ModuleGroupId(usize);
pub struct ModulePartition {
groups: Vec<ModuleGroup>,
module_to_group: FxHashMap<mun_hir::Module, ModuleGroupId>,
file_to_group: FxHashMap<mun_hir::FileId, ModuleGroupId>,
file_to_group: FxHashMap<FileId, ModuleGroupId>,
}

impl ModulePartition {
Expand Down Expand Up @@ -49,7 +50,7 @@ impl ModulePartition {
}

/// Returns the group to which the specified module belongs.
pub fn group_for_file(&self, file: mun_hir::FileId) -> Option<ModuleGroupId> {
pub fn group_for_file(&self, file: FileId) -> Option<ModuleGroupId> {
self.file_to_group.get(&file).copied()
}

Expand Down
6 changes: 3 additions & 3 deletions crates/mun_codegen/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::cell::RefCell;

use inkwell::{context::Context, OptimizationLevel};
use mun_hir::{
diagnostics::DiagnosticSink, with_fixture::WithFixture, HirDatabase, SourceDatabase, Upcast,
};
use mun_db::Upcast;
use mun_hir::{diagnostics::DiagnosticSink, HirDatabase};
use mun_hir_input::{SourceDatabase, WithFixture};
use mun_target::spec::Target;

use crate::{
Expand Down
2 changes: 2 additions & 0 deletions crates/mun_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ license.workspace = true
[dependencies]
mun_codegen = { version = "0.6.0-dev", path = "../mun_codegen" }
mun_syntax = { version = "0.6.0-dev", path = "../mun_syntax" }
mun_db = { version = "0.6.0-dev", path = "../mun_db" }
mun_hir_input = { version = "0.6.0-dev", path = "../mun_hir_input" }
mun_hir = { version = "0.6.0-dev", path = "../mun_hir" }
mun_paths = { version = "0.6.0-dev", path = "../mun_paths" }
mun_target = { version = "0.6.0-dev", path = "../mun_target" }
Expand Down
10 changes: 6 additions & 4 deletions crates/mun_compiler/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use mun_codegen::{CodeGenDatabase, CodeGenDatabaseStorage};
use mun_hir::{salsa, HirDatabase, Upcast};
use mun_db::Upcast;
use mun_hir::{salsa, HirDatabase};
use mun_hir_input::SourceDatabase;

use crate::Config;

/// A compiler database is a salsa database that enables increment compilation.
#[salsa::database(
mun_hir::SourceDatabaseStorage,
mun_hir_input::SourceDatabaseStorage,
mun_hir::InternDatabaseStorage,
mun_hir::AstDatabaseStorage,
mun_hir::DefDatabaseStorage,
Expand All @@ -22,8 +24,8 @@ impl Upcast<dyn mun_hir::AstDatabase> for CompilerDatabase {
}
}

impl Upcast<dyn mun_hir::SourceDatabase> for CompilerDatabase {
fn upcast(&self) -> &(dyn mun_hir::SourceDatabase + 'static) {
impl Upcast<dyn SourceDatabase> for CompilerDatabase {
fn upcast(&self) -> &(dyn SourceDatabase + 'static) {
self
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/mun_compiler/src/diagnostics_snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::{collections::HashMap, sync::Arc};

use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
use mun_diagnostics::DiagnosticForWith;
use mun_hir::{line_index::LineIndex, FileId, HirDatabase};
use mun_hir::HirDatabase;
use mun_hir_input::{FileId, LineIndex};
use mun_paths::RelativePathBuf;
use mun_syntax::SyntaxError;

Expand Down
7 changes: 3 additions & 4 deletions crates/mun_compiler/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//! compilation by retaining state from previous compilation.
use mun_codegen::{AssemblyIr, CodeGenDatabase, ModuleGroup, TargetAssembly};
use mun_hir::{
AstDatabase, DiagnosticSink, FileId, Module, PackageSet, SourceDatabase, SourceRoot,
SourceRootId, Upcast,
};
use mun_hir::{AstDatabase, DiagnosticSink, Module};
use mun_hir_input::{FileId, PackageSet, SourceDatabase, SourceRoot, SourceRootId};
use mun_paths::RelativePathBuf;

use crate::{
Expand All @@ -25,6 +23,7 @@ use std::{
time::Duration,
};

use mun_db::Upcast;
use mun_project::{Package, LOCKFILE_NAME};
use walkdir::WalkDir;

Expand Down
2 changes: 1 addition & 1 deletion crates/mun_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{

pub use annotate_snippets::AnnotationType;
pub use mun_codegen::OptimizationLevel;
pub use mun_hir::FileId;
pub use mun_hir_input::FileId;
pub use mun_paths::{RelativePath, RelativePathBuf};
use mun_project::Package;
pub use mun_target::spec::Target;
Expand Down
14 changes: 14 additions & 0 deletions crates/mun_db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "mun_db"
description = "Salsa database utilities for everything mun related."
version.workspace = true
authors.workspace = true
edition.workspace = true
documentation.workspace = true
readme.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
categories.workspace = true

[dependencies]
6 changes: 6 additions & 0 deletions crates/mun_db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! This crate provides utilitiy functions to be used alongside salsa databases
//! used by mun.
pub trait Upcast<T: ?Sized> {
fn upcast(&self) -> &T;
}
2 changes: 2 additions & 0 deletions crates/mun_hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ la-arena = { workspace = true }
mun_syntax = { version = "0.6.0-dev", path = "../mun_syntax" }
mun_target = { version = "0.6.0-dev", path = "../mun_target" }
mun_paths = { version = "0.6.0-dev", path="../mun_paths" }
mun_db = { version = "0.6.0-dev", path="../mun_db" }
mun_hir_input = { version = "0.6.0-dev", path="../mun_hir_input" }
drop_bomb = { workspace = true }
either = { workspace = true }
ena = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_hir/src/code_model/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{iter::once, sync::Arc};

use mun_hir_input::FileId;
use mun_syntax::{ast, ast::TypeAscriptionOwner};

use super::Module;
Expand All @@ -12,7 +13,7 @@ use crate::{
resolve::HasResolver,
type_ref::{LocalTypeRefId, TypeRefMap, TypeRefSourceMap},
visibility::RawVisibility,
Body, DefDatabase, DiagnosticSink, FileId, HasSource, HasVisibility, HirDatabase, InFile,
Body, DefDatabase, DiagnosticSink, HasSource, HasVisibility, HirDatabase, InFile,
InferenceResult, Name, Pat, Ty, Visibility,
};

Expand Down Expand Up @@ -117,8 +118,7 @@ impl Function {
.path_to_root(db)
.into_iter()
.filter_map(|module| module.name(db))
.chain(once(self.name(db)))
.map(|name| name.to_string()),
.chain(once(self.name(db).to_string())),
String::from("::"),
)
.collect()
Expand Down
4 changes: 3 additions & 1 deletion crates/mun_hir/src/code_model/impl.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::sync::Arc;

use mun_hir_input::FileId;

use crate::{
has_module::HasModule,
ids::{AssocItemId, FunctionLoc, ImplId, Intern, ItemContainerId, Lookup},
item_tree::{AssociatedItem, ItemTreeId},
type_ref::{LocalTypeRefId, TypeRefMap, TypeRefMapBuilder, TypeRefSourceMap},
DefDatabase, FileId, Function, HirDatabase, ItemLoc, Module, Package, Ty,
DefDatabase, Function, HirDatabase, ItemLoc, Module, Package, Ty,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
Expand Down
Loading

0 comments on commit 9598913

Please sign in to comment.