Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t committed Dec 1, 2021
1 parent 9908db7 commit d77e204
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 36 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions crates/analyzer/src/db/queries/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ pub fn module_item_map(

let mut map = IndexMap::<String, Item>::new();

// TODO: add global method to modules
for (name, ingot) in match module.data(db).context {
ModuleContext::Ingot(ingot) => ingot.data(db).global.data(db).ingots.clone(),
ModuleContext::Global(global) => global.data(db).ingots.clone()
} {
// TODO: handle error with panic?
map.insert(name.to_owned(), Item::Ingot(ingot));
}

for item in module.all_items(db).iter() {
let item_name = item.name(db);
if let Some(builtin) = builtin_items.get(&item_name) {
Expand Down
8 changes: 6 additions & 2 deletions crates/analyzer/src/namespace/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,17 @@ impl Item {

#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
pub struct Global {
ingots: BTreeMap<String, IngotId>,
pub ingots: BTreeMap<String, IngotId>,
}

#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub struct GlobalId(pub(crate) u32);
impl_intern_key!(GlobalId);
impl GlobalId {}
impl GlobalId {
pub fn data(&self, db: &dyn AnalyzerDb) -> Rc<Global> {
db.lookup_intern_global(*self)
}
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Ingot {
Expand Down
52 changes: 23 additions & 29 deletions crates/analyzer/tests/snapshots/analysis__basic_ingot.snap
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,17 @@ note:


note:
┌─ ingots/basic_ingot/src/ding/dong.fe:2:3
┌─ ingots/basic_ingot/src/bing.fe:4:5
2my_address: address
│ ^^^^^^^^^^^^^^^^^^^ address
3my_u256: u256
│ ^^^^^^^^^^^^^ u256
4my_i8: i8
│ ^^^^^^^^^ i8


note:
┌─ ingots/basic_ingot/src/bar/baz.fe:2:5
2my_bool: bool
│ ^^^^^^^^^^^^^ bool
3my_u256: u256
│ ^^^^^^^^^^^^^ u256


note:
┌─ ingots/basic_ingot/src/bing.fe:2:5
2my_address: address
4my_address: address
│ ^^^^^^^^^^^^^^^^^^^ address

note:
┌─ ingots/basic_ingot/src/bing.fe:4:1
┌─ ingots/basic_ingot/src/bing.fe:6:1
4 │ ╭ fn get_42_backend() -> u256:
5 │ │ return 42
│ ╰─────────────^ attributes hash: 17979516652885443340
6 │ ╭ fn get_42_backend() -> u256:
7 │ │ return std::get_42()
│ ╰────────────────────────^ attributes hash: 17979516652885443340
= FunctionSignature {
self_decl: None,
Expand All @@ -183,11 +163,25 @@ note:
),
}


note:
┌─ ingots/basic_ingot/src/ding/dong.fe:2:3
2my_address: address
│ ^^^^^^^^^^^^^^^^^^^ address
3my_u256: u256
│ ^^^^^^^^^^^^^ u256
4my_i8: i8
│ ^^^^^^^^^ i8


note:
┌─ ingots/basic_ingot/src/bing.fe:5:12
┌─ ingots/basic_ingot/src/bar/baz.fe:2:5
5return 42
│ ^^ u256: Value
2my_bool: bool
│ ^^^^^^^^^^^^^ bool
3my_u256: u256
│ ^^^^^^^^^^^^^ u256



Expand Down
2 changes: 2 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "Apache-2.0"
repository = "https://github.com/ethereum/fe"

[dependencies]
fe-library = {path = "../library", version = "^0.10.0-alpha"}
tiny-keccak = { version = "2.0", features = ["keccak"] }
hex = "0.4"
codespan-reporting = "0.11.1"
Expand All @@ -15,3 +16,4 @@ ron = "0.5.1"
difference = "2.0"
num-traits = "0.2.14"
once_cell = "1.8.0"
include_dir = "0.6.0"
18 changes: 18 additions & 0 deletions crates/common/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::utils::keccak;
use crate::Span;
use codespan_reporting as cs;
use cs::files::Error as CsError;
use include_dir::Dir;
use std::collections::HashMap;
use std::convert::TryInto;
use std::ops::Range;
Expand Down Expand Up @@ -86,6 +87,23 @@ impl FileStore {
id
}

pub fn add_included_dir(&mut self, dir: &Dir) -> Vec<SourceFileId> {
let mut file_ids = vec![];

for file in dir.files() {
file_ids.push(self.add_file(
file.path().to_str().unwrap(),
&file.contents_utf8().unwrap(),
));
}

for sub_dir in dir.dirs() {
file_ids.extend(self.add_included_dir(sub_dir))
}

file_ids
}

pub fn load_file(&mut self, path: &str) -> io::Result<(String, SourceFileId)> {
let content = self.loader.load_file(Path::new(&path))?;
let id = self.add_file(path, &content);
Expand Down
33 changes: 31 additions & 2 deletions crates/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,44 @@ pub fn compile_ingot(
name: &str,
files: &FileStore,
file_ids: &[SourceFileId],
deps: &IndexMap<String, Vec<SourceFileId>>,
_with_bytecode: bool,
_optimize: bool,
) -> Result<CompiledModule, CompileError> {
let mut errors = vec![];

let db = Db::default();

let global = Global::default();
let global_id = db.intern_global(Rc::new(global));
let global_id = {
let ingots = deps
.into_iter()
.map(|(name, file_ids)| {
// these `dep` ingots will have their own deps soon
// for now we just compile the built-in deps in an empty global context
let global = Global::default();
let global_id = db.intern_global(Rc::new(global));

let ingot = Ingot {
name: name.clone(),
global: global_id,
fe_files: file_ids
.iter()
.map(|file_id| {
let file = files.get_file(*file_id).expect("missing file for ID");
let (ast, parser_diagnostics) =
parse_file(*file_id, &file.content).map_err(CompileError).unwrap();
errors.extend(parser_diagnostics.into_iter());
(*file_id, (file.to_owned(), ast))
})
.collect(),
};
(name.to_owned(), db.intern_ingot(Rc::new(ingot)))
})
.collect();

let global = Global { ingots };
db.intern_global(Rc::new(global))
};

let ingot = Ingot {
name: name.to_string(),
Expand Down
1 change: 1 addition & 0 deletions crates/fe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ solc-backend = ["fe-driver/solc-backend"]
[dependencies]
clap = "2.33.3"
walkdir = "2"
indexmap = "1.6.2"
fe-common = {path = "../common", version = "^0.10.0-alpha"}
fe-driver = {path = "../driver", version = "^0.10.0-alpha"}
fe-parser = {path = "../parser", version = "^0.10.0-alpha"}
2 changes: 2 additions & 0 deletions crates/fe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use fe_common::diagnostics::print_diagnostics;
use fe_common::files::{FileStore, SourceFileId};
use fe_common::panic::install_panic_hook;
use fe_driver::CompiledModule;
use indexmap::map::IndexMap;
use std::ffi::OsStr;
use walkdir::WalkDir;

Expand Down Expand Up @@ -119,6 +120,7 @@ pub fn main() {
input_path,
&files,
&files.all_files(),
&IndexMap::new(),
with_bytecode,
optimize,
) {
Expand Down
10 changes: 10 additions & 0 deletions crates/library/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "fe-library"
version = "0.10.0-alpha"
authors = ["The Fe Developers <snakecharmers@ethereum.org>"]
edition = "2018"
license = "Apache-2.0"
repository = "https://github.com/ethereum/fe"

[dependencies]
include_dir = "0.6.0"
3 changes: 3 additions & 0 deletions crates/library/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use include_dir::{include_dir, Dir};

pub const STD: Dir = include_dir!("std");
2 changes: 2 additions & 0 deletions crates/library/std/lib.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fn get_42() -> u256:
return 42
4 changes: 3 additions & 1 deletion crates/test-files/fixtures/ingots/basic_ingot/src/bing.fe
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std

struct Bing:
my_address: address

fn get_42_backend() -> u256:
return 42
return std::get_42()

# currently disallowed
#contract BingContract:
Expand Down
2 changes: 2 additions & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ fe-driver = {path = "../driver", version = "^0.10.0-alpha"}
fe-yulgen = {path = "../yulgen", version = "^0.10.0-alpha"}
fe-yulc = {path = "../yulc", version = "^0.10.0-alpha", optional = true, features = ["solc-backend"]}
fe-analyzer = {path = "../analyzer", version = "^0.10.0-alpha"}
fe-library = {path = "../library", version = "^0.10.0-alpha"}
test-files = {path = "../test-files", package = "fe-test-files" }
hex = "0.4"
primitive-types = {version = "0.9", default-features = false, features = ["rlp"]}
serde_json = "1.0.64"
solc = {git = "https://github.com/g-r-a-n-t/solc-rust", optional = true}
yultsur = {git = "https://github.com/g-r-a-n-t/yultsur"}
indexmap = "1.6.2"

[features]
solc-backend = ["fe-yulc", "solc"]
10 changes: 8 additions & 2 deletions crates/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fe_common::files::FileStore;
use fe_common::utils::keccak;
use fe_driver as driver;
use fe_yulgen::runtime::functions;
use indexmap::indexmap;
use primitive_types::{H160, U256};
use std::collections::BTreeMap;
use std::str::FromStr;
Expand Down Expand Up @@ -274,9 +275,14 @@ pub fn deploy_contract_from_ingot(
contract_name: &str,
init_params: &[ethabi::Token],
) -> ContractHarness {
let files = test_files::build_filestore(path);
let mut files = test_files::build_filestore(path);
let ingot_files = files.all_files();

let compiled_module = match driver::compile_ingot(path, &files, &files.all_files(), true, true)
let deps = indexmap! {
"std".to_string() => files.add_included_dir(&fe_library::STD)
};

let compiled_module = match driver::compile_ingot(path, &files, &ingot_files, &deps, true, true)
{
Ok(module) => module,
Err(error) => {
Expand Down

0 comments on commit d77e204

Please sign in to comment.