Skip to main content

Compiler

Struct Compiler 

Source
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 EVMVersion

The 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: bool

Generate and log bytecode

§cached: bool

Whether to check cached artifacts

§file_provider: Arc<dyn FileProvider>

The implementation of a FileReader

§relax_jumps: bool

Whether to apply jump relaxation optimization

§no_size_limit: bool

Skip 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>

Source

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.

Source

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.

Source

pub fn init_tracing_subscriber(directives: Option<Vec<Directive>>)

Tracing

Creates a new tracing subscriber to span the compilation process.

Source

pub fn execute(&self) -> Result<Vec<Arc<Artifact>>, Arc<CompilerError>>

Executor

The core compilation process.

§Steps
  1. Transform inputs into File Paths with transform_paths.
  2. Fetch file sources in parallel with fetch_sources.
  3. Recurse file dependencies in parallel with recurse_deps.
  4. For each top-level file generate the artifact using gen_artifact.
  5. Return the compiling error(s) or successfully generated artifacts.
Source

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

Source

pub fn grab_contracts(&self) -> Result<Vec<Contract>, Arc<CompilerError>>

Grab the ASTs for all file sources.

§Steps
  1. Transform inputs into File Paths with transform_paths.
  2. Fetch file sources in parallel with fetch_sources.
  3. Recurse file dependencies in parallel with recurse_deps.
  4. For each top-level file, parse its contents and return a vec of [Contract] ASTs.
Source

pub fn gen_artifact( &self, file: Arc<FileSource>, ) -> Result<Artifact, CompilerError>

Artifact Generation

Compiles a FileSource into an Artifact.

Source

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.

Source

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

Source

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

Source

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.

Source

pub(crate) fn constant_overrides_hex(&self) -> BTreeMap<String, String>

Returns the constant overrides as hex values by constant name.

Source

pub(crate) fn found_constant_overrides( &self, tokens: &[Token], ) -> HashSet<&'a str>

Returns the constant overrides mentioned in a file’s tokens.

Source

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.

Source

pub fn get_constructor_args(&self) -> Vec<String>

Derives Constructor Input Arguments

Source

pub fn get_outputs(&self) -> OutputLocation

Derives an output location

Trait Implementations§

Source§

impl<'a, 'l> Clone for Compiler<'a, 'l>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, 'l> Debug for Compiler<'a, 'l>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more

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