Skip to content

Commit

Permalink
library2
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed May 16, 2024
1 parent 70fca41 commit 3c32fdf
Show file tree
Hide file tree
Showing 22 changed files with 712 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 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 Down
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
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
13 changes: 13 additions & 0 deletions crates/driver2/tests/std_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use fe_driver2::DriverDataBase;

#[test]
fn check_std_lib() {
let mut driver = DriverDataBase::default();
let std_ingot = library2::std_lib_input_ingot(&mut driver);
driver.run_on_ingot(std_ingot);

let diags = driver.format_diags();
if !diags.is_empty() {
panic!("{diags}")
}
}
15 changes: 14 additions & 1 deletion crates/hir/src/analysis_pass.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{diagnostics::DiagnosticVoucher, hir_def::TopLevelMod};
use crate::{
diagnostics::DiagnosticVoucher,
hir_def::{ModuleTree, TopLevelMod},
};

/// All analysis passes that run analysis on the HIR top level module
/// granularity should implement this trait.
Expand Down Expand Up @@ -27,4 +30,14 @@ impl<'db> AnalysisPassManager<'db> {
}
diags
}

pub fn run_on_module_tree(&mut self, tree: &ModuleTree) -> Vec<Box<dyn DiagnosticVoucher>> {
let mut diags = vec![];
for module in tree.all_modules() {
for pass in self.module_passes.iter_mut() {
diags.extend(pass.run_on_module(module));
}
}
diags
}
}
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");
}
58 changes: 58 additions & 0 deletions crates/library2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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,
"/",
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 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
}
1 change: 1 addition & 0 deletions crates/library2/std/src/lib.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! The Fe standard library.
1 change: 1 addition & 0 deletions crates/library2/std/src/num.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Basic numeric functionality.
4 changes: 4 additions & 0 deletions crates/library2/std/src/num/int.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Basic integer functionality.

pub type isize = i256
pub type usize = u256
69 changes: 69 additions & 0 deletions crates/library2/std/src/num/int/extend.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! Integer extension.

extern {
fn u8_u16_extend(_ x: u8) -> u16
fn u8_u32_extend(_ x: u8) -> u32
fn u8_u64_extend(_ x: u8) -> u64
fn u8_u128_extend(_ x: u8) -> u128
fn u8_u256_extend(_ x: u8) -> u256
fn u16_u32_extend(_ x: u16) -> u32
fn u16_u64_extend(_ x: u16) -> u64
fn u16_u128_extend(_ x: u16) -> u128
fn u16_u256_extend(_ x: u16) -> u256
fn u32_u64_extend(_ x: u32) -> u64
fn u32_u128_extend(_ x: u32) -> u128
fn u32_u256_extend(_ x: u32) -> u256
fn u64_u128_extend(_ x: u64) -> u128
fn u64_u256_extend(_ x: u64) -> u256
fn u128_u256_extend(_ x: u128) -> u256
fn i8_i16_extend(_ x: i8) -> i16
fn i8_i32_extend(_ x: i8) -> i32
fn i8_i64_extend(_ x: i8) -> i64
fn i8_i128_extend(_ x: i8) -> i128
fn i8_i256_extend(_ x: i8) -> i256
fn i16_i32_extend(_ x: i16) -> i32
fn i16_i64_extend(_ x: i16) -> i64
fn i16_i128_extend(_ x: i16) -> i128
fn i16_i256_extend(_ x: i16) -> i256
fn i32_i64_extend(_ x: i32) -> i64
fn i32_i128_extend(_ x: i32) -> i128
fn i32_i256_extend(_ x: i32) -> i256
fn i64_i128_extend(_ x: i64) -> i128
fn i64_i256_extend(_ x: i64) -> i256
fn i128_i256_extend(_ x: i128) -> i256
}

pub trait Extend<Out> {
fn extend(self) -> Out
}

impl Extend<u16> for u8 { fn extend(self) -> u16 { u8_u16_extend(self) } }
impl Extend<u32> for u8 { fn extend(self) -> u32 { u8_u32_extend(self) } }
impl Extend<u64> for u8 { fn extend(self) -> u64 { u8_u64_extend(self) } }
impl Extend<u128> for u8 { fn extend(self) -> u128 { u8_u128_extend(self) } }
impl Extend<u256> for u8 { fn extend(self) -> u256 { u8_u256_extend(self) } }
impl Extend<u32> for u16 { fn extend(self) -> u32 { u16_u32_extend(self) } }
impl Extend<u64> for u16 { fn extend(self) -> u64 { u16_u64_extend(self) } }
impl Extend<u128> for u16 { fn extend(self) -> u128 { u16_u128_extend(self) } }
impl Extend<u256> for u16 { fn extend(self) -> u256 { u16_u256_extend(self) } }
impl Extend<u64> for u32 { fn extend(self) -> u64 { u32_u64_extend(self) } }
impl Extend<u128> for u32 { fn extend(self) -> u128 { u32_u128_extend(self) } }
impl Extend<u256> for u32 { fn extend(self) -> u256 { u32_u256_extend(self) } }
impl Extend<u128> for u64 { fn extend(self) -> u128 { u64_u128_extend(self) } }
impl Extend<u256> for u64 { fn extend(self) -> u256 { u64_u256_extend(self) } }
impl Extend<u256> for u128 { fn extend(self) -> u256 { u128_u256_extend(self) } }
impl Extend<i16> for i8 { fn extend(self) -> i16 { i8_i16_extend(self) } }
impl Extend<i32> for i8 { fn extend(self) -> i32 { i8_i32_extend(self) } }
impl Extend<i64> for i8 { fn extend(self) -> i64 { i8_i64_extend(self) } }
impl Extend<i128> for i8 { fn extend(self) -> i128 { i8_i128_extend(self) } }
impl Extend<i256> for i8 { fn extend(self) -> i256 { i8_i256_extend(self) } }
impl Extend<i32> for i16 { fn extend(self) -> i32 { i16_i32_extend(self) } }
impl Extend<i64> for i16 { fn extend(self) -> i64 { i16_i64_extend(self) } }
impl Extend<i128> for i16 { fn extend(self) -> i128 { i16_i128_extend(self) } }
impl Extend<i256> for i16 { fn extend(self) -> i256 { i16_i256_extend(self) } }
impl Extend<i64> for i32 { fn extend(self) -> i64 { i32_i64_extend(self) } }
impl Extend<i128> for i32 { fn extend(self) -> i128 { i32_i128_extend(self) } }
impl Extend<i256> for i32 { fn extend(self) -> i256 { i32_i256_extend(self) } }
impl Extend<i128> for i64 { fn extend(self) -> i128 { i64_i128_extend(self) } }
impl Extend<i256> for i64 { fn extend(self) -> i256 { i64_i256_extend(self) } }
impl Extend<i256> for i128 { fn extend(self) -> i256 { i128_i256_extend(self) } }
1 change: 1 addition & 0 deletions crates/library2/std/src/num/int/ops.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Integer operations.
Loading

0 comments on commit 3c32fdf

Please sign in to comment.