Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Added compiler interact.
Browse files Browse the repository at this point in the history
  • Loading branch information
RIg410 committed Sep 27, 2021
1 parent 10eb0eb commit 11ae64b
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 90 deletions.
2 changes: 1 addition & 1 deletion language/diem-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn stdlib_bytecode_files() -> Vec<String> {

pub(crate) fn build_stdlib() -> BTreeMap<String, CompiledModule> {
let (_files, compiled_units) =
move_compile_and_report(&diem_stdlib_files(), &[], None, Flags::empty()).unwrap();
move_compile_and_report(&diem_stdlib_files(), &[], None, Flags::empty(), &mut ()).unwrap();
let mut modules = BTreeMap::new();
for (i, compiled_unit) in compiled_units.into_iter().enumerate() {
let name = compiled_unit.name();
Expand Down
4 changes: 2 additions & 2 deletions language/diem-framework/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn generate_script_abis(
..Default::default()
};
options.setup_logging_for_test();
move_prover::run_move_prover_errors_to_stderr(options).unwrap();
move_prover::run_move_prover_errors_to_stderr(options, &mut ()).unwrap();
}

fn generate_script_builder(output_path: impl AsRef<Path>, abi_paths: &[impl AsRef<Path>]) {
Expand Down Expand Up @@ -254,7 +254,7 @@ fn build_error_code_map(output_path: impl AsRef<Path>) {
..Default::default()
};
options.setup_logging_for_test();
move_prover::run_move_prover_errors_to_stderr(options).unwrap();
move_prover::run_move_prover_errors_to_stderr(options, &mut ()).unwrap();
}

/// Options to configure the generation of a release.
Expand Down
1 change: 1 addition & 0 deletions language/diem-tools/transaction-replay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ fn compile_move_script(file_path: &str) -> Result<Vec<u8>> {
&diem_framework::diem_stdlib_files(),
None,
Flags::empty().set_sources_shadow_deps(false),
&mut (),
)?;
let unit = match units_or_errors {
Err(errors) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn compile_script(source_file_str: String) -> Vec<u8> {
&diem_framework::diem_stdlib_files(),
None,
Flags::empty().set_sources_shadow_deps(false),
&mut (),
)
.unwrap();
let mut script_bytes = vec![];
Expand Down
1 change: 1 addition & 0 deletions language/move-lang/src/bin/move-build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn main() -> anyhow::Result<()> {
&dependencies,
Some(interface_files_dir),
flags,
&mut (),
)?;
move_lang::output_compiled_units(emit_source_map, files, compiled_units, &out_dir)
}
2 changes: 1 addition & 1 deletion language/move-lang/src/bin/move-check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ pub fn main() -> anyhow::Result<()> {
flags,
} = Options::from_args();

let _files = move_lang::move_check_and_report(&source_files, &dependencies, out_dir, flags)?;
let _files = move_lang::move_check_and_report(&source_files, &dependencies, out_dir, flags, &mut ())?;
Ok(())
}
42 changes: 42 additions & 0 deletions language/move-lang/src/callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fs::File;
use std::io::Read;
use anyhow::Error;
use crate::parser::ast::Definition;
use std::borrow::Cow;

/// Compiler interaction.
pub trait Interact {
/// Returns true if the compiler works on native fs, false otherwise.
fn is_native_fs(&self) -> bool {
true
}

/// Makes static string.
fn static_str(&mut self, val: String) -> &'static str {
Box::leak(Box::new(val))
}
/// Gets move file content.
fn file_access(&mut self, name: &'static str) -> Result<String, Error> {
let mut f = File::open(name)
.map_err(|err| std::io::Error::new(err.kind(), format!("{}: {}", err, name)))?;
let mut source_buffer = String::new();
f.read_to_string(&mut source_buffer)?;
let res = self.preprocess(name, Cow::Owned(source_buffer));
Ok(res.into_owned())
}
/// Preprocess source.
fn preprocess<'a>(&mut self, _: &'static str, source: Cow<'a, str>) -> Cow<'a, str> {
source
}

/// Analyze AST.
fn analyze_ast(&mut self, _: &[Definition]) {
// no-op
}
/// Returns the list of required dependencies.
fn required_dependencies(&mut self) -> Result<Vec<String>, Error> {
Ok(vec![])
}
}

impl Interact for () {}
35 changes: 14 additions & 21 deletions language/move-lang/src/interface_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,16 @@ macro_rules! push {
}};
}

/// Generate the text for the "interface" file of a compiled module. This "interface" is the
/// publically visible contents of the CompiledModule, represented in source language syntax
/// Additionally, it returns the module id (address+name) of the module that was deserialized
pub fn write_to_string(compiled_module_file_input_path: &str) -> Result<(ModuleId, String)> {
let mut out = String::new();

let file_contents = fs::read(compiled_module_file_input_path)?;
let module = CompiledModule::deserialize(&file_contents).map_err(|e| {
pub fn make_interface(name: &str, bytecode: &[u8]) -> Result<(ModuleId, String)> {
let module = CompiledModule::deserialize(&bytecode).map_err(|e| {
anyhow!(
"Unable to deserialize module at '{}': {}",
compiled_module_file_input_path,
name,
e
)
})?;

let mut out = String::new();
let id = module.self_id();
push_line!(
out,
Expand Down Expand Up @@ -74,16 +69,9 @@ pub fn write_to_string(compiled_module_file_input_path: &str) -> Result<(ModuleI
Visibility::Private => false,
})
.peekable();
let has_externally_visible_funs = externally_visible_funs.peek().is_some();
if has_externally_visible_funs {
members.push(format!(" {}", DISCLAIMER));
}
for fdef in externally_visible_funs {
members.push(write_function_def(&mut context, fdef));
}
if has_externally_visible_funs {
members.push("".to_string());
}

let has_uses = !context.uses.is_empty();
for (module_id, alias) in context.uses {
Expand Down Expand Up @@ -120,6 +108,14 @@ pub fn write_to_string(compiled_module_file_input_path: &str) -> Result<(ModuleI
Ok((id, out))
}

/// Generate the text for the "interface" file of a compiled module. This "interface" is the
/// publically visible contents of the CompiledModule, represented in source language syntax
/// Additionally, it returns the module id (address+name) of the module that was deserialized
pub fn write_to_string(compiled_module_file_input_path: &str) -> Result<(ModuleId, String)> {
let file_contents = fs::read(compiled_module_file_input_path)?;
make_interface(compiled_module_file_input_path, &file_contents)
}

struct Context<'a> {
module: &'a CompiledModule,
uses: BTreeMap<ModuleId, String>,
Expand All @@ -136,9 +132,6 @@ impl<'a> Context<'a> {
}
}

const DISCLAIMER: &str =
"// NOTE: Functions are 'native' for simplicity. They may or may not be native in actuality.";

fn write_friend_decl(ctx: &mut Context, fdecl: &ModuleHandle) -> String {
format!(
" friend {}::{};",
Expand Down Expand Up @@ -207,7 +200,7 @@ fn write_visibility(visibility: Visibility) -> String {
Visibility::Friend => "public(friend) ",
Visibility::Private => "",
}
.to_string()
.to_string()
}

fn write_ability_modifiers(abs: AbilitySet) -> String {
Expand Down Expand Up @@ -244,7 +237,7 @@ fn write_ability(ab: Ability) -> String {
Ability::Store => A_::STORE,
Ability::Key => A_::KEY,
}
.to_string()
.to_string()
}

fn write_type_parameters(tps: &[AbilitySet]) -> String {
Expand Down
Loading

0 comments on commit 11ae64b

Please sign in to comment.