Skip to content

Commit

Permalink
library2
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jan 14, 2025
1 parent fabcd29 commit 7876e0d
Show file tree
Hide file tree
Showing 54 changed files with 1,725 additions and 36 deletions.
3 changes: 3 additions & 0 deletions crates/common2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ smol_str = "0.1.24"
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "a1bf3a6" }
indexmap = "2.2"
parser = { path = "../parser2", package = "fe-parser2" }
toml = "0.8.13"
serde = { version = "1", features = ["derive"] }
git2 = "0.18.3"
19 changes: 19 additions & 0 deletions crates/common2/src/home_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env::VarError;

use camino::Utf8PathBuf;

pub struct HomeDir(Utf8PathBuf);

impl HomeDir {
pub fn load() -> Self {
match std::env::var("HOME") {
Ok(home) => HomeDir(Utf8PathBuf::from(home)),
Err(VarError::NotPresent) => HomeDir(Utf8PathBuf::from("~/.fe")),
Err(_) => panic!("no unicode"),
}
}

pub fn path(&self) -> Utf8PathBuf {
self.0.clone()
}
}
8 changes: 4 additions & 4 deletions crates/common2/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{indexmap::IndexSet, 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 Down Expand Up @@ -60,7 +60,7 @@ impl InputIngot {
}

/// Set the list of files which the ingot contains.
/// All files must bee set before the ingot is used.
/// All files must be set before the ingot is used.
pub fn set_files(self, db: &mut dyn InputDb, files: IndexSet<InputFile>) {
self.__set_files_impl(db).to(files);
}
Expand Down Expand Up @@ -103,8 +103,8 @@ pub enum IngotKind {
/// An external ingot which is depended on by the current ingot.
External,

/// Standard library ingot.
Std,
/// Core library ingot.
Core,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
1 change: 1 addition & 0 deletions crates/common2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod diagnostics;
pub mod home_dir;
pub mod indexmap;
pub mod input;

Expand Down
6 changes: 6 additions & 0 deletions crates/driver2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ codespan-reporting = "0.11"
hir = { path = "../hir", package = "fe-hir" }
common = { path = "../common2", package = "fe-common2" }
hir-analysis = { path = "../hir-analysis", package = "fe-hir-analysis" }
resolver = { path = "../resolver", package = "fe-resolver" }
camino = "1.1.4"
clap = { version = "4.3", features = ["derive"] }
semver = "1.0.23"
walkdir = "2"
include_dir = "0.7"
dirs = "5.0.1"
serde = { version = "1", features = ["derive"] }
108 changes: 108 additions & 0 deletions crates/driver2/src/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::CheckArgs;
use common::indexmap::IndexSet;
use common::input::IngotDependency;
use common::{input::IngotKind, InputIngot};
use fe_driver2::DriverDataBase;
use include_dir::Dir;
use semver::Version;
use std::fs;
use std::{collections::BTreeSet, path::Path};

pub fn check_standalone(
path: &Path,
source: String,
std_ingot: InputIngot,
db: &mut DriverDataBase,
dump_scope_graph: bool,
) {
let mut dependencies = IndexSet::default();
dependencies.insert(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.standalone(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 dependencies = IndexSet::default();
// let mut main_ingot = load_ingot(path, db, IngotKind::Local, &mut dependencies);

// main_ingot.set_external_ingots(db, dependencies);

let diags = db.run_on_ingot(std_ingot);

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

// db.run_on_ingot(main_ingot);

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

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

// use include_dir::{include_dir, Dir};
// use std::path::PathBuf;
// use std::{collections::BTreeSet, path::Path};

// fn check_std_lib_exists(std_lib_files: &Dir, std_lib_path: &Path) -> bool {
// true
// }

// fn write_std_lib(std_lib_files: &Dir, std_lib_path: &Path) {
// write_files_recursive(std_lib_files, std_lib_path);
// }

// fn default_std_lib_path() -> PathBuf {
// let home_dir = dirs::home_dir().expect("Failed to get user home directory");
// home_dir.join(".fe/std")
// }

// fn write_files_recursive(dir: &Dir<'_>, base_path: &Path) {
// for file in dir.files() {
// let file_path = base_path.join(file.path());
// if let Some(parent_dir) = file_path.parent() {
// std::fs::create_dir_all(parent_dir).unwrap();
// }
// std::fs::write(file_path, file.contents()).unwrap();
// }

// for subdir in dir.dirs() {
// let subdir_path = base_path.join(subdir.path());
// std::fs::create_dir_all(&subdir_path).unwrap();
// write_files_recursive(subdir, &base_path);
// }
// }
70 changes: 66 additions & 4 deletions crates/driver2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ pub mod diagnostics;

use std::path;

use camino::Utf8PathBuf;
use codespan_reporting::term::{
self,
termcolor::{BufferWriter, ColorChoice},
};
use common::{
diagnostics::CompleteDiagnostic,
indexmap::IndexSet,
input::{IngotKind, Version},
indexmap::{IndexMap, IndexSet},
input::{IngotDependency, IngotKind, Version},
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 All @@ -24,6 +28,7 @@ use hir_analysis::{
},
HirAnalysisDb,
};
use resolver::ingot::{dependency_graph::DependencyGraph, src_files::SourceFiles};

use crate::diagnostics::ToCsDiag;

Expand Down Expand Up @@ -71,6 +76,63 @@ impl DriverDataBase {
DiagnosticsCollection(pass_manager.run_on_module(top_mod))
}

pub fn run_on_ingot<'db>(&'db mut self, ingot: InputIngot) -> DiagnosticsCollection<'db> {
self.run_on_ingot_with_pass_manager(ingot, initialize_analysis_pass)
}

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

pub fn local_ingot(
&mut self,
core_ingot: InputIngot,
dependency_graph: &DependencyGraph,
) -> (InputIngot, IndexMap<Utf8PathBuf, InputIngot>) {
let mut all_ingots = IndexMap::new();

for path in dependency_graph.reverse_toposort() {
let external_ingots = dependency_graph
.dependencies(&path)
.into_iter()
.map(|dependency| IngotDependency {
name: dependency.name,
ingot: all_ingots[&dependency.target_path],
})
.collect();

all_ingots[&path] = InputIngot::new(
self,
&path.to_string(),
IngotKind::External,
Version::new(0, 0, 0),
external_ingots,
);
}

let local_ingot = all_ingots
.shift_remove(&dependency_graph.local_path)
.expect("local is missing from input ingots");
(local_ingot, all_ingots)
}

pub fn core_ingot(&mut self, path: &Utf8PathBuf) -> InputIngot {
todo!();
}

pub fn set_ingot_files(&mut self, ingot: InputIngot, files: SourceFiles) {
todo!()
}

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

Expand Down
Loading

0 comments on commit 7876e0d

Please sign in to comment.