Struct sway_ir::function::Function

source ·
pub struct Function(pub Index);
Expand description

A wrapper around an ECS handle into the Context.

Tuple Fields§

§0: Index

Implementations§

source§

impl Function

source

pub fn new(
context: &mut Context,
module: Module,
name: String,
args: Vec<(String, Type, bool, Option<MetadataIndex>)>,
return_type: Type,
selector: Option<[u8; 4]>,
is_public: bool,
is_entry: bool,
metadata: Option<MetadataIndex>
) -> Function

Return a new Function handle.

Creates a Function in the context within module and returns a handle.

name, args, return_type and is_public are the usual suspects. selector is a special value used for Sway contract calls; much like name is unique and not particularly used elsewhere in the IR.

source

pub fn create_block(&self, context: &mut Context, label: Option<Label>) -> Block

Create and append a new Block to this function.

source

pub fn create_block_before(
&self,
context: &mut Context,
other: &Block,
label: Option<Label>
) -> Result<Block, IrError>

Create and insert a new Block into this function.

The new block is inserted before other.

source

pub fn create_block_after(
&self,
context: &mut Context,
other: &Block,
label: Option<Label>
) -> Result<Block, IrError>

Create and insert a new Block into this function.

The new block is inserted after other.

source

pub fn remove_block(
&self,
context: &mut Context,
block: &Block
) -> Result<(), IrError>

Remove a Block from this function.

Care must be taken to ensure the block has no predecessors otherwise the function will be made invalid.

source

pub fn get_unique_label(
&self,
context: &mut Context,
hint: Option<String>
) -> String

Get a new unique block label.

If hint is None then the label will be in the form "blockN" where N is an incrementing decimal.

Otherwise if the hint is already unique to this function it will be returned. If not already unique it will have N appended to it until it is unique.

source

pub fn num_blocks(&self, context: &Context) -> usize

Return the number of blocks in this function.

source

pub fn num_instructions(&self, context: &Context) -> usize

Return the number of instructions in this function.

source

pub fn get_name<'a>(&self, context: &'a Context) -> &'a str

Return the function name.

source

pub fn get_entry_block(&self, context: &Context) -> Block

Return the function entry (i.e., the first) block.

source

pub fn get_metadata(&self, context: &Context) -> Option<MetadataIndex>

Return the attached metadata.

source

pub fn has_selector(&self, context: &Context) -> bool

Whether this function has a valid selector.

source

pub fn get_selector(&self, context: &Context) -> Option<[u8; 4]>

Return the function selector, if it has one.

source

pub fn is_entry(&self, context: &Context) -> bool

Whether or not the function is a program entry point, i.e. main, #[test] fns or abi methods.

source

pub fn get_return_type(&self, context: &Context) -> Type

source

pub fn num_args(&self, context: &Context) -> usize

Get the number of args.

source

pub fn get_arg(&self, context: &Context, name: &str) -> Option<Value>

Get an arg value by name, if found.

source

pub fn lookup_arg_name<'a>(
&self,
context: &'a Context,
value: &Value
) -> Option<&'a String>

Find the name of an arg by value.

source

pub fn args_iter<'a>(
&self,
context: &'a Context
) -> impl Iterator<Item = &'a (String, Value)>

Return an iterator for each of the function arguments.

source

pub fn get_local_var(&self, context: &Context, name: &str) -> Option<LocalVar>

Get a pointer to a local value by name, if found.

source

pub fn lookup_local_name<'a>(
&self,
context: &'a Context,
var: &LocalVar
) -> Option<&'a String>

Find the name of a local value by pointer.

source

pub fn new_local_var(
&self,
context: &mut Context,
name: String,
local_type: Type,
initializer: Option<Constant>
) -> Result<LocalVar, IrError>

Add a value to the function local storage.

The name must be unique to this function else an error is returned.

source

pub fn new_unique_local_var(
&self,
context: &mut Context,
name: String,
local_type: Type,
initializer: Option<Constant>
) -> LocalVar

Add a value to the function local storage, by forcing the name to be unique if needed.

Will use the provided name as a hint and rename to guarantee insertion.

source

pub fn locals_iter<'a>(
&self,
context: &'a Context
) -> impl Iterator<Item = (&'a String, &'a LocalVar)>

Return an iterator to all of the values in this function’s local storage.

source

pub fn merge_locals_from(
&self,
context: &mut Context,
other: Function
) -> HashMap<LocalVar, LocalVar>

Merge values from another Function into this one.

The names of the merged values are guaranteed to be unique via the use of Function::new_unique_local_var.

Returns a map from the original pointers to the newly merged pointers.

XXX This function returns a Result but can’t actually fail?

source

pub fn block_iter(&self, context: &Context) -> BlockIterator

Return an iterator to each block in this function.

source

pub fn instruction_iter<'a>(
&self,
context: &'a Context
) -> impl Iterator<Item = (Block, Value)> + 'a

Return an iterator to each instruction in each block in this function.

This is a convenience method for when all instructions in a function need to be inspected. The instruction value is returned from the iterator along with the block it belongs to.

source

pub fn replace_values(
&self,
context: &mut Context,
replace_map: &FxHashMap<Value, Value>,
starting_block: Option<Block>
)

Replace a value with another within this function.

This is a convenience method which iterates over this function’s blocks and calls Block::replace_values in turn.

starting_block is an optimisation for when the first possible reference to old_val is known.

source

pub fn replace_value(
&self,
context: &mut Context,
old_val: Value,
new_val: Value,
starting_block: Option<Block>
)

source

pub fn dot_cfg(&self, context: &Context) -> String

A graphviz dot graph of the control-flow-graph.

Trait Implementations§

source§

impl Clone for Function

source§

fn clone(&self) -> Function

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Function

source§

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

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

impl Hash for Function

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<Function> for Function

source§

fn eq(&self, other: &Function) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PassScope for Function

source§

impl Copy for Function

source§

impl Eq for Function

source§

impl StructuralEq for Function

source§

impl StructuralPartialEq for Function

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast for Twhere
T: Any,

source§

fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for Twhere
T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync + 'static>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> Same<T> for T

§

type Output = T

Should always be Self
§

impl<T> StorageAsMut for T

§

fn storage<Type>(&mut self) -> StorageMut<'_, Self, Type>where
Type: Mappable,

§

fn storage_as_mut<Type>(&mut self) -> StorageMut<'_, Self, Type>where
Type: Mappable,

§

impl<T> StorageAsRef for T

§

fn storage<Type>(&self) -> StorageRef<'_, Self, Type>where
Type: Mappable,

§

fn storage_as_ref<Type>(&self) -> StorageRef<'_, Self, Type>where
Type: Mappable,

source§

impl<T> ToOwned for Twhere
T: Clone,

§

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 Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V