Skip to content

Commit

Permalink
library2
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Feb 27, 2024
1 parent fd274c9 commit 2c12665
Show file tree
Hide file tree
Showing 17 changed files with 1,283 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/driver2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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" }
Expand Down
9 changes: 9 additions & 0 deletions crates/driver2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ impl DriverDataBase {
map_file_to_mod(self, file)
}

fn top_mod_from_ingot(&self, ingot: InputIngot) -> TopLevelMod {
map_file_to_mod(self, ingot.root_file(self))
}

pub fn top_mod_from_std_ingot(&mut self) -> TopLevelMod {
let ingot = library2::std_lib_input_ingot(self);
self.top_mod_from_ingot(ingot)
}

/// Prints accumulated diagnostics to stderr.
pub fn emit_diags(&self) {
let writer = BufferWriter::stderr(ColorChoice::Auto);
Expand Down
12 changes: 12 additions & 0 deletions crates/driver2/tests/std_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use fe_driver2::DriverDataBase;

#[test]
fn check_std_lib() {
let mut driver = DriverDataBase::default();
let top_mod = driver.top_mod_from_std_ingot();
driver.run_on_top_mod(top_mod);
let diags = driver.format_diags();
if !diags.is_empty() {
panic!("{diags}")
}
}
1 change: 1 addition & 0 deletions crates/hir-analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ regex = "1.10"
[dev-dependencies]
codespan-reporting = "0.11"
dir-test = "0.1"
library2 = { path = "../library2", package = "fe-library2" }
11 changes: 11 additions & 0 deletions crates/library2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "fe-library2"
version = "0.23.0"
authors = ["The Fe Developers <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/ethereum/fe"

[dependencies]
include_dir = "0.7.2"
common = { path = "../common2", package = "fe-common2" }
3 changes: 3 additions & 0 deletions crates/library2/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rerun-if-changed=./std");
}
62 changes: 62 additions & 0 deletions crates/library2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::collections::BTreeSet;

pub use ::include_dir;
use common::{
input::{IngotKind, Version},
InputDb, InputFile, InputIngot,
};
use include_dir::{include_dir, Dir};

pub const STD: Dir = include_dir!("$CARGO_MANIFEST_DIR/std");

fn std_src_input_files(db: &mut dyn InputDb, ingot: InputIngot) -> BTreeSet<InputFile> {
static_dir_files(&STD)
.into_iter()
.map(|(path, content)| InputFile::new(db, ingot, path.into(), content.into()))
.collect()
}

pub fn std_lib_input_ingot(db: &mut dyn InputDb) -> InputIngot {
let ingot = InputIngot::new(
db,
"std",
IngotKind::Std,
Version::new(0, 0, 0),
BTreeSet::default(),
);

let input_files = std_src_input_files(db, ingot);
let root_file = input_files
.iter()
.find(|file| file.path(db).ends_with("lib.fe"))
.unwrap()
.to_owned();

ingot.set_root_file(db, root_file);

ingot.set_files(db, input_files);
ingot
}

// pub fn std_src_files() -> Vec<(&'static str, &'static str)> {
// static_dir_files(STD.get_dir("src").unwrap())
// }

pub fn static_dir_files(dir: &'static Dir) -> Vec<(&'static str, &'static str)> {
fn add_files(dir: &'static Dir, accum: &mut Vec<(&'static str, &'static str)>) {
accum.extend(dir.files().map(|file| {
(
file.path().to_str().unwrap(),
file.contents_utf8().expect("non-utf8 static file"),
)
}));

for sub_dir in dir.dirs() {
add_files(sub_dir, accum)
}
}

let mut files = vec![];
add_files(dir, &mut files);
files
}
Loading

0 comments on commit 2c12665

Please sign in to comment.