pub struct Compiler<'a, 'l> {
pub evm_version: &'l EVMVersion,
pub sources: Arc<Vec<String>>,
pub output: Option<String>,
pub alternative_main: Option<String>,
pub alternative_constructor: Option<String>,
pub construct_args: Option<Vec<String>>,
pub constant_overrides: Option<BTreeMap<&'a str, Bytes>>,
pub bytecode: bool,
pub cached: bool,
pub file_provider: Arc<dyn FileProvider>,
pub relax_jumps: bool,
pub no_size_limit: bool,
}Expand description
§The Core Huff Compiler
§Usage
The canonical way to instantiate a Compiler instance is using the public associated new function.
Let’s say we want to create a Compiler for the ERC20.huff contract located in huff-examples.
We want our Compiler to output to an artifact:Vec<ethers_core::abi::token::Token>s directory,
with no constructor arguments, and no verbose output:
use huff_neo_core::Compiler;
use huff_neo_utils::prelude::EVMVersion;
use std::sync::Arc;
let compiler = Compiler::new(
&EVMVersion::default(),
Arc::new(vec!["../../resources/erc20/ERC20.huff".to_string()]),
Some("./artifacts".to_string()),
None,
None,
None,
None,
false,
false
);Fields§
§evm_version: &'l EVMVersionThe EVM version to compile for
sources: Arc<Vec<String>>The location of the files to compile
output: Option<String>The output location
alternative_main: Option<String>Macro to use a main
alternative_constructor: Option<String>Constructor macro to use
construct_args: Option<Vec<String>>Constructor Input Arguments
constant_overrides: Option<BTreeMap<&'a str, Bytes>>Constant Overrides
bytecode: boolGenerate and log bytecode
cached: boolWhether to check cached artifacts
file_provider: Arc<dyn FileProvider>The implementation of a FileReader
relax_jumps: boolWhether to apply jump relaxation optimization
no_size_limit: boolSkip the contract and initcode size limit validation (EIP-170/EIP-3860, raised by EIP-7954 in Amsterdam)
Implementations§
Source§impl<'a, 'l> Compiler<'a, 'l>
impl<'a, 'l> Compiler<'a, 'l>
Sourcepub fn new(
evm_version: &'l EVMVersion,
sources: Arc<Vec<String>>,
output: Option<String>,
alternative_main: Option<String>,
alternative_constructor: Option<String>,
construct_args: Option<Vec<String>>,
constant_overrides: Option<BTreeMap<&'a str, Bytes>>,
verbose: bool,
cached: bool,
) -> Self
pub fn new( evm_version: &'l EVMVersion, sources: Arc<Vec<String>>, output: Option<String>, alternative_main: Option<String>, alternative_constructor: Option<String>, construct_args: Option<Vec<String>>, constant_overrides: Option<BTreeMap<&'a str, Bytes>>, verbose: bool, cached: bool, ) -> Self
Public associated function to instantiate a new compiler.
Sourcepub fn new_in_memory(
evm_version: &'l EVMVersion,
sources: Arc<Vec<String>>,
file_sources: HashMap<String, String>,
alternative_main: Option<String>,
alternative_constructor: Option<String>,
construct_args: Option<Vec<String>>,
constant_overrides: Option<BTreeMap<&'a str, Bytes>>,
verbose: bool,
) -> Self
pub fn new_in_memory( evm_version: &'l EVMVersion, sources: Arc<Vec<String>>, file_sources: HashMap<String, String>, alternative_main: Option<String>, alternative_constructor: Option<String>, construct_args: Option<Vec<String>>, constant_overrides: Option<BTreeMap<&'a str, Bytes>>, verbose: bool, ) -> Self
Creates a new instance of a compiler with an in-memory FileReader from the supplied sources map.
Sourcepub fn init_tracing_subscriber(directives: Option<Vec<Directive>>)
pub fn init_tracing_subscriber(directives: Option<Vec<Directive>>)
Tracing
Creates a new tracing subscriber to span the compilation process.
Sourcepub fn execute(&self) -> Result<Vec<Arc<Artifact>>, Arc<CompilerError>>
pub fn execute(&self) -> Result<Vec<Arc<Artifact>>, Arc<CompilerError>>
Executor
The core compilation process.
§Steps
- Transform inputs into File Paths with transform_paths.
- Fetch file sources in parallel with fetch_sources.
- Recurse file dependencies in parallel with recurse_deps.
- For each top-level file generate the artifact using gen_artifact.
- Return the compiling error(s) or successfully generated artifacts.
Sourcepub fn get_flattened_source(
file: Arc<FileSource>,
file_provider: Arc<dyn FileProvider>,
) -> Result<String, Arc<CompilerError>>
pub fn get_flattened_source( file: Arc<FileSource>, file_provider: Arc<dyn FileProvider>, ) -> Result<String, Arc<CompilerError>>
Get the flattened source code for a file with all dependencies resolved and #include statements commented out
Sourcepub fn grab_contracts(&self) -> Result<Vec<Contract>, Arc<CompilerError>>
pub fn grab_contracts(&self) -> Result<Vec<Contract>, Arc<CompilerError>>
Grab the ASTs for all file sources.
§Steps
- Transform inputs into File Paths with transform_paths.
- Fetch file sources in parallel with fetch_sources.
- Recurse file dependencies in parallel with recurse_deps.
- For each top-level file, parse its contents and return a vec of [Contract] ASTs.
Sourcepub fn gen_artifact(
&self,
file: Arc<FileSource>,
) -> Result<Artifact, CompilerError>
pub fn gen_artifact( &self, file: Arc<FileSource>, ) -> Result<Artifact, CompilerError>
Artifact Generation
Compiles a FileSource into an Artifact.
Sourcepub(crate) fn generate_artifact(
&self,
file: Arc<FileSource>,
) -> Result<(Artifact, HashSet<&'a str>), CompilerError>
pub(crate) fn generate_artifact( &self, file: Arc<FileSource>, ) -> Result<(Artifact, HashSet<&'a str>), CompilerError>
Compiles a FileSource into an Artifact, together with the constant overrides its source mentions so they can be validated once all files are compiled.
Sourcepub fn fetch_sources(
paths: Vec<PathBuf>,
reader: Arc<dyn FileProvider>,
) -> Vec<Result<Arc<FileSource>, CompilerError>>
pub fn fetch_sources( paths: Vec<PathBuf>, reader: Arc<dyn FileProvider>, ) -> Vec<Result<Arc<FileSource>, CompilerError>>
Get the file sources for a vec of PathBufs
Sourcepub fn recurse_deps(
fs: Arc<FileSource>,
remapper: &Remapper,
reader: Arc<dyn FileProvider>,
walk_level: HashSet<String>,
) -> Result<Arc<FileSource>, Arc<CompilerError>>
pub fn recurse_deps( fs: Arc<FileSource>, remapper: &Remapper, reader: Arc<dyn FileProvider>, walk_level: HashSet<String>, ) -> Result<Arc<FileSource>, Arc<CompilerError>>
Recurses file dependencies
Sourcepub fn export_artifacts(artifacts: &Vec<Arc<Artifact>>, output: &OutputLocation)
pub fn export_artifacts(artifacts: &Vec<Arc<Artifact>>, output: &OutputLocation)
Export Artifacts
Writes each artifact as JSON to the output file, or below the output directory at the path of its source file. Other files in the output directory are left alone.
Sourcepub(crate) fn constant_overrides_hex(&self) -> BTreeMap<String, String>
pub(crate) fn constant_overrides_hex(&self) -> BTreeMap<String, String>
Returns the constant overrides as hex values by constant name.
Sourcepub(crate) fn found_constant_overrides(
&self,
tokens: &[Token],
) -> HashSet<&'a str>
pub(crate) fn found_constant_overrides( &self, tokens: &[Token], ) -> HashSet<&'a str>
Returns the constant overrides mentioned in a file’s tokens.
Sourcepub(crate) fn validate_constant_overrides(
&self,
found_overrides: &HashSet<&str>,
) -> Result<(), CompilerError>
pub(crate) fn validate_constant_overrides( &self, found_overrides: &HashSet<&str>, ) -> Result<(), CompilerError>
Rejects constant overrides whose name appears in none of the compiled sources.
An override may set a constant the source references without defining it, and when several files are compiled it may target only some of them. So only a name that no source mentions is rejected: it cannot affect the output and is most likely a typo.
Sourcepub fn get_constructor_args(&self) -> Vec<String>
pub fn get_constructor_args(&self) -> Vec<String>
Derives Constructor Input Arguments
Sourcepub fn get_outputs(&self) -> OutputLocation
pub fn get_outputs(&self) -> OutputLocation
Derives an output location
Trait Implementations§
Auto Trait Implementations§
impl<'a, 'l> !RefUnwindSafe for Compiler<'a, 'l>
impl<'a, 'l> !UnwindSafe for Compiler<'a, 'l>
impl<'a, 'l> Freeze for Compiler<'a, 'l>
impl<'a, 'l> Send for Compiler<'a, 'l>
impl<'a, 'l> Sync for Compiler<'a, 'l>
impl<'a, 'l> Unpin for Compiler<'a, 'l>
impl<'a, 'l> UnsafeUnpin for Compiler<'a, 'l>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<S, T> Upcast<T> for S
impl<S, T> Upcast<T> for S
§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘwhere
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘwhere
S: Into<Dispatch>,
§fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 168 bytes