Skip to main content

Contract

Struct Contract 

Source
pub struct Contract {
Show 13 fields pub macros: IndexMap<String, MacroDefinition>, pub invocations: Vec<MacroInvocation>, pub imports: Vec<FilePath>, pub constants: Arc<Mutex<Vec<ConstantDefinition>>>, pub errors: Vec<ErrorDefinition>, pub functions: Vec<FunctionDefinition>, pub events: Vec<EventDefinition>, pub tables: Vec<TableDefinition>, pub labels: HashSet<String>, pub flattened_source: Option<String>, pub source_files: Vec<(String, String)>, pub source_map: Vec<(usize, usize, usize)>, pub runtime_size: Option<usize>,
}
Expand description

A Huff Contract Representation

This is the representation of a contract as it is parsed from huff source code. Thus, it is also the root of the AST.

For examples of Huff contracts, see the huff-examples repository.

Fields§

§macros: IndexMap<String, MacroDefinition>

Macro definitions, indexed by name

§invocations: Vec<MacroInvocation>

Invocations of macros

§imports: Vec<FilePath>

File Imports

§constants: Arc<Mutex<Vec<ConstantDefinition>>>

Constants

§errors: Vec<ErrorDefinition>

Custom Errors

§functions: Vec<FunctionDefinition>

Functions

§events: Vec<EventDefinition>

Events

§tables: Vec<TableDefinition>

Tables

§labels: HashSet<String>

Labels

§flattened_source: Option<String>

Flattened source code (for debugging and source maps)

§source_files: Vec<(String, String)>

Individual source files for multi-file debugging support Vec of (file_path, source_content)

§source_map: Vec<(usize, usize, usize)>

Mapping from flattened position to (file_index, original_position)

§runtime_size: Option<usize>

Byte length of the runtime section. Set before constructor codegen so __codesize(RUNTIME) resolves; references from any other context error.

Implementations§

Source§

impl Contract

Source

pub fn map_flattened_position_to_source( &self, flattened_start: usize, flattened_end: usize, ) -> (u32, usize, usize)

Map a position from the flattened source to the original file and position Returns (file_id, original_start, original_end)

Source

pub fn map_original_span_to_flattened( &self, span: &Span, ) -> Option<(usize, usize)>

Convert a span from original file coordinates to flattened source coordinates This is needed because AST spans are in original file positions, but source maps need flattened positions

Source

pub fn find_macro_by_name(&self, name: &str) -> Option<&MacroDefinition>

Returns the first macro that matches the provided name

Source

pub fn find_table_by_name(&self, name: &str) -> Option<TableDefinition>

Returns the first table that matches the provided name

Source

pub fn derive_storage_pointers(&mut self)

Derives the FreeStoragePointers into their bytes32 representation

Slots are assigned in order of first use: the constructor, then MAIN, then each test with its decorators, so pointers only tests use never move the contract’s own slots.

Source

pub fn recurse_ast_constants( &self, macro_def: &MacroDefinition, storage_pointers: &mut Vec<(String, [u8; 32])>, last_p: &mut i32, checking_constructor: bool, )

Recurse down an AST Macro Definition to set Storage Pointers

§Overview

For each statement in the macro definition: - If it’s a free storage pointer constant, set the constant value if not already set and updated out last_p tracker value - If it’s a literal constant, we can set the constant value directly to the literal if not already set - If it’s a macro invocation, look for the macro definition and recurse into that macro definition using recurse_ast_constants

Source

fn recurse_ast_constants_inner( &self, macro_def: &MacroDefinition, storage_pointers: &mut Vec<(String, [u8; 32])>, last_p: &mut i32, checking_constructor: bool, visited: &mut HashSet<String>, )

Source

fn assign_free_storage_pointers( &self, const_name: &String, macro_name: &String, storage_pointers: &mut Vec<(String, [u8; 32])>, last_p: &mut i32, )

Source

pub fn add_override_constants( &self, override_constants: &Option<BTreeMap<&str, Bytes>>, )

Add override constants to the AST

§Overview

For each override constant, add it to the AST if it doesn’t already exist. Override constants can be passed in via the CLI.

Source

pub fn validate_opcodes( &self, evm_version: &EVMVersion, ) -> Result<(), CodegenError>

Validate that all opcodes used in the contract are compatible with the target EVM version

Source

fn validate_statements_opcodes( statements: &[Statement], evm_version: &EVMVersion, ) -> Result<(), CodegenError>

Helper function to recursively validate opcodes in a list of statements

Source

fn validate_macro_invocation_args( args: &[MacroArg], evm_version: &EVMVersion, invocation_span: &AstSpan, ) -> Result<(), CodegenError>

Helper function to validate opcodes passed as macro invocation arguments

Source

pub fn evaluate_constant_expression( &self, expr: &Expression, ) -> Result<Literal, CodegenError>

Recursively evaluates arithmetic expressions in constant definitions at compile time. This supports binary operations (+, -, *, /, %), unary negation, literals, constant references, and parenthesized expressions with proper operator precedence and overflow/underflow checking.

Source

fn evaluate_constant_expression_internal( &self, expr: &Expression, evaluating: &mut HashSet<String>, ) -> Result<Literal, CodegenError>

Internal helper that tracks which constants are currently being evaluated to detect cycles

Trait Implementations§

Source§

impl Clone for Contract

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 Debug for Contract

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Contract

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Contract> for Abi

Source§

fn from(contract: Contract) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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
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: 360 bytes