Skip to content

Commit

Permalink
library2
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jun 4, 2024
1 parent 70fca41 commit 50bdc7a
Show file tree
Hide file tree
Showing 23 changed files with 513 additions and 38 deletions.
73 changes: 60 additions & 13 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion crates/common2/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::InputDb;
#[salsa::input(constructor = __new_impl)]
pub struct InputIngot {
/// An absolute path to the ingot root directory.
/// The all files in the ingot should be located under this directory.
/// All files in the ingot should be located under this directory.
#[return_ref]
pub path: Utf8PathBuf,

Expand All @@ -23,6 +23,7 @@ pub struct InputIngot {

/// A list of ingots which the current ingot depends on.
#[return_ref]
#[set(__set_external_ingots_impl)]
pub external_ingots: BTreeSet<IngotDependency>,

/// A list of files which the current ingot contains.
Expand Down Expand Up @@ -72,6 +73,10 @@ impl InputIngot {
pub fn root_file(&self, db: &dyn InputDb) -> InputFile {
self.__get_root_file_impl(db).unwrap()
}

pub fn set_external_ingots(self, db: &mut dyn InputDb, ingots: BTreeSet<IngotDependency>) {
self.__set_external_ingots_impl(db).to(ingots);
}
}

#[salsa::input]
Expand Down
5 changes: 5 additions & 0 deletions crates/driver2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ description = "Provides Fe driver"
[dependencies]
salsa = { git = "https://github.com/salsa-rs/salsa", package = "salsa-2022" }
codespan-reporting = "0.11"
library2 = { path = "../library2", package = "fe-library2" }

hir = { path = "../hir", package = "fe-hir" }
common = { path = "../common2", package = "fe-common2" }
macros = { path = "../macros", package = "fe-macros" }
hir-analysis = { path = "../hir-analysis", package = "fe-hir-analysis" }
camino = "1.1.4"
clap = { version = "4.3", features = ["derive"] }
toml = "0.8.13"
serde = { version = "1", features = ["derive"] }
semver = "1.0.23"
walkdir = "2"
102 changes: 102 additions & 0 deletions crates/driver2/src/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use crate::{load_ingot, set_src_files, CheckArgs};
use common::input::{IngotDependency, InputFile};
use common::{input::IngotKind, InputDb, InputIngot};
use fe_driver2::DriverDataBase;
use semver::Version;
use std::fs;
use std::{collections::BTreeSet, path::Path};

pub fn run_check(args: &CheckArgs) {
let std_path = Path::new(&args.std_path);
if !std_path.exists() {
eprintln!(
"Standard library path '{}' does not exist",
std_path.display()
);
std::process::exit(2);
}

let path = Path::new(&args.path);
if !path.exists() {
eprintln!("Path '{}' does not exist", path.display());
std::process::exit(2);
}

let mut db = DriverDataBase::default();

let std_ingot = load_ingot(&std_path, &mut db, IngotKind::Std, &mut BTreeSet::new());

if path.is_file() {
let source = fs::read_to_string(path).unwrap();
check_single_file(path, source, std_ingot, &mut db, args.dump_scope_graph);
} else if path.is_dir() {
check_ingot(path, std_ingot, &mut db, args.dump_scope_graph);
} else {
eprintln!(
"Path '{}' is neither a file nor a directory",
path.display()
);
std::process::exit(2);
}
}

pub fn check_single_file(
path: &Path,
source: String,
std_ingot: InputIngot,
db: &mut DriverDataBase,
dump_scope_graph: bool,
) {
let mut dependencies = BTreeSet::from([IngotDependency::new("std", std_ingot)]);
let ingot = InputIngot::new(
db,
path.parent().unwrap().to_str().unwrap(),
IngotKind::StandAlone,
Version::new(0, 1, 0),
dependencies.clone(),
);

// let input_file = InputFile::new(
// db,
// ingot.clone(),
// path.file_name().unwrap().to_str().unwrap().into(),
// source,
// );
// ingot.set_files(db, BTreeSet::from([input_file.clone()]));
// ingot.set_root_file(db, input_file);

let top_mod = db.top_mod_from_file(path, &source);
db.run_on_top_mod(top_mod);
db.emit_diags();

// if dump_scope_graph {
// println!("{}", dump_scope_graph(db, top_mod));
// }
}

pub fn check_ingot(
path: &Path,
std_ingot: InputIngot,
db: &mut DriverDataBase,
dump_scope_graph: bool,
) {
let mut dependencies = BTreeSet::from([IngotDependency::new("std", std_ingot)]);
let mut main_ingot = load_ingot(path, db, IngotKind::Local, &mut dependencies);

main_ingot.set_external_ingots(db, dependencies);

db.run_on_ingot(std_ingot);

let diags = db.format_diags();
if !diags.is_empty() {
panic!("{diags}")
}

// let top_mod = db.top_mod_from_ingot(main_ingot);
// db.run_on_top_mod(top_mod);
// db.emit_diags();

// if dump_scope_graph {
// println!("{}", dump_scope_graph(db, top_mod));
// }
}
23 changes: 21 additions & 2 deletions crates/driver2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ use common::{
InputDb, InputFile, InputIngot,
};
use hir::{
analysis_pass::AnalysisPassManager, diagnostics::DiagnosticVoucher, hir_def::TopLevelMod,
lower::map_file_to_mod, HirDb, LowerHirDb, ParsingPass, SpannedHirDb,
analysis_pass::AnalysisPassManager,
diagnostics::DiagnosticVoucher,
hir_def::TopLevelMod,
lower::{map_file_to_mod, module_tree},
HirDb, LowerHirDb, ParsingPass, SpannedHirDb,
};
use hir_analysis::{
name_resolution::{DefConflictAnalysisPass, ImportAnalysisPass, PathAnalysisPass},
Expand Down Expand Up @@ -58,6 +61,10 @@ impl DriverDataBase {
self.run_on_file_with_pass_manager(top_mod, initialize_analysis_pass);
}

pub fn run_on_ingot(&mut self, ingot: InputIngot) {
self.run_on_ingot_with_pass_manager(ingot, initialize_analysis_pass);
}

pub fn run_on_file_with_pass_manager<F>(&mut self, top_mod: TopLevelMod, pm_builder: F)
where
F: FnOnce(&DriverDataBase) -> AnalysisPassManager<'_>,
Expand All @@ -69,6 +76,18 @@ impl DriverDataBase {
};
}

pub fn run_on_ingot_with_pass_manager<F>(&mut self, ingot: InputIngot, pm_builder: F)
where
F: FnOnce(&DriverDataBase) -> AnalysisPassManager<'_>,
{
self.diags.clear();
let tree = module_tree(self, ingot);
self.diags = {
let mut pass_manager = pm_builder(self);
pass_manager.run_on_module_tree(tree)
};
}

pub fn top_mod_from_file(&mut self, file_path: &path::Path, source: &str) -> TopLevelMod {
let kind = IngotKind::StandAlone;

Expand Down
Loading

0 comments on commit 50bdc7a

Please sign in to comment.