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
impl Contract
Sourcepub fn map_flattened_position_to_source(
&self,
flattened_start: usize,
flattened_end: usize,
) -> (u32, usize, usize)
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)
Sourcepub fn map_original_span_to_flattened(
&self,
span: &Span,
) -> Option<(usize, usize)>
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
Sourcepub fn find_macro_by_name(&self, name: &str) -> Option<&MacroDefinition>
pub fn find_macro_by_name(&self, name: &str) -> Option<&MacroDefinition>
Returns the first macro that matches the provided name
Sourcepub fn find_table_by_name(&self, name: &str) -> Option<TableDefinition>
pub fn find_table_by_name(&self, name: &str) -> Option<TableDefinition>
Returns the first table that matches the provided name
Sourcepub fn derive_storage_pointers(&mut self)
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.
Sourcepub fn recurse_ast_constants(
&self,
macro_def: &MacroDefinition,
storage_pointers: &mut Vec<(String, [u8; 32])>,
last_p: &mut i32,
checking_constructor: bool,
)
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
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>, )
fn assign_free_storage_pointers( &self, const_name: &String, macro_name: &String, storage_pointers: &mut Vec<(String, [u8; 32])>, last_p: &mut i32, )
Sourcepub fn add_override_constants(
&self,
override_constants: &Option<BTreeMap<&str, Bytes>>,
)
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.
Sourcepub fn validate_opcodes(
&self,
evm_version: &EVMVersion,
) -> Result<(), CodegenError>
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
Sourcefn validate_statements_opcodes(
statements: &[Statement],
evm_version: &EVMVersion,
) -> Result<(), CodegenError>
fn validate_statements_opcodes( statements: &[Statement], evm_version: &EVMVersion, ) -> Result<(), CodegenError>
Helper function to recursively validate opcodes in a list of statements
Sourcefn validate_macro_invocation_args(
args: &[MacroArg],
evm_version: &EVMVersion,
invocation_span: &AstSpan,
) -> Result<(), CodegenError>
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
Sourcepub fn evaluate_constant_expression(
&self,
expr: &Expression,
) -> Result<Literal, CodegenError>
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.
Sourcefn evaluate_constant_expression_internal(
&self,
expr: &Expression,
evaluating: &mut HashSet<String>,
) -> Result<Literal, CodegenError>
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§
Auto Trait Implementations§
impl Freeze for Contract
impl RefUnwindSafe for Contract
impl Send for Contract
impl Sync for Contract
impl Unpin for Contract
impl UnsafeUnpin for Contract
impl UnwindSafe for Contract
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 moreSource§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: 360 bytes