Skip to content

Commit

Permalink
library2
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed May 14, 2024
1 parent 4a18de0 commit 048e43b
Show file tree
Hide file tree
Showing 21 changed files with 1,267 additions and 5 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
25 changes: 22 additions & 3 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 Expand Up @@ -142,7 +161,7 @@ impl Default for DriverDataBase {
}
}

fn initialize_analysis_pass(db: &DriverDataBase) -> AnalysisPassManager<'_> {
pub fn initialize_analysis_pass(db: &DriverDataBase) -> AnalysisPassManager<'_> {
let mut pass_manager = AnalysisPassManager::new();
pass_manager.add_module_pass(Box::new(ParsingPass::new(db)));
pass_manager.add_module_pass(Box::new(DefConflictAnalysisPass::new(db)));
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
}
Empty file added crates/library2/std/src/lib.fe
Empty file.
2 changes: 2 additions & 0 deletions crates/library2/std/src/num.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub type isize = i256
pub type usize = u256
156 changes: 156 additions & 0 deletions crates/library2/std/src/num/int/extend.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
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) }
}
Empty file.
Loading

0 comments on commit 048e43b

Please sign in to comment.