Skip to main content

HirFunction

Struct HirFunction 

Source
pub struct HirFunction { /* private fields */ }
Expand description

Represents a function in HIR.

Implementations§

Source§

impl HirFunction

Source

pub fn new( name: String, return_type: HirType, parameters: Vec<HirParameter>, ) -> Self

Create a new HIR function.

§Examples
use decy_hir::{HirFunction, HirType, HirParameter};

let func = HirFunction::new(
    "add".to_string(),
    HirType::Int,
    vec![
        HirParameter::new("a".to_string(), HirType::Int),
        HirParameter::new("b".to_string(), HirType::Int),
    ],
);

assert_eq!(func.name(), "add");
assert_eq!(func.parameters().len(), 2);
Source

pub fn name(&self) -> &str

Get the function name.

Source

pub fn return_type(&self) -> &HirType

Get the return type.

Source

pub fn parameters(&self) -> &[HirParameter]

Get the parameters.

Source

pub fn from_ast_function(ast_func: &Function) -> Self

Convert from parser AST function to HIR function.

§Examples
use decy_hir::HirFunction;
use decy_parser::parser::{Function, Type, Parameter};

let ast_func = Function::new(
    "test".to_string(),
    Type::Void,
    vec![],
);

let hir_func = HirFunction::from_ast_function(&ast_func);
assert_eq!(hir_func.name(), "test");
Source

pub fn new_with_body( name: String, return_type: HirType, parameters: Vec<HirParameter>, body: Vec<HirStatement>, ) -> Self

Create a new HIR function with a body.

§Examples
use decy_hir::{HirFunction, HirType, HirStatement, HirExpression};

let func = HirFunction::new_with_body(
    "test".to_string(),
    HirType::Int,
    vec![],
    vec![
        HirStatement::VariableDeclaration {
            name: "x".to_string(),
            var_type: HirType::Int,
            initializer: Some(HirExpression::IntLiteral(5)),
        },
        HirStatement::Return(Some(HirExpression::Variable("x".to_string()))),
    ],
);

assert_eq!(func.name(), "test");
assert_eq!(func.body().len(), 2);
Source

pub fn body(&self) -> &[HirStatement]

Get the function body.

Source

pub fn has_body(&self) -> bool

DECY-190: Check if this function has a body (is a definition, not just a declaration). Returns true for definitions, false for forward declarations/prototypes.

Trait Implementations§

Source§

impl Clone for HirFunction

Source§

fn clone(&self) -> HirFunction

Returns a duplicate 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 HirFunction

Source§

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

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

impl PartialEq for HirFunction

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for HirFunction

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.

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> 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 = Infallible

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

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

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.