Skip to main content

Function

Struct Function 

Source
pub struct Function {
    pub name: String,
    pub params: Vec<Parameter>,
    pub ret: Type,
    pub is_inline: bool,
    pub storage: StorageClass,
    pub body: Option<Block>,
    pub attrs: Vec<Attribute>,
    pub raw_attrs: Vec<String>,
    pub doc: Option<DocComment>,
}
Expand description

Represents a C function with all its components and attributes.

The Function struct enables the creation of complete C function declarations and definitions, supporting inline, static, and extern modifiers, documentation, parameter lists, return types, and function bodies.

§Examples

Basic function with a return value:

int main(void) {
  return 0;
}

Function with parameters and modifiers:

static inline double calculate_area(double width, double height) {
  return width * height;
}

External function declaration:

extern void process_data(char *buffer, size_t length);

Fields§

§name: String

The name of the function

§params: Vec<Parameter>

The parameters of the function as a vector of Parameter objects

§ret: Type

The return type of the function

§is_inline: bool

Whether the function is declared with the ‘inline’ keyword

§storage: StorageClass

The storage class (static, extern, or none). static and extern are mutually exclusive, so they share one field.

§body: Option<Block>

The body of the function. None means this is a prototype (a declaration only); Some(block) is a definition, even when the block is empty.

§attrs: Vec<Attribute>

The attributes applied to the function (e.g. noreturn, always_inline)

§raw_attrs: Vec<String>

Raw macro/specifier tokens emitted verbatim at the leading annotation slot (e.g. an export macro like MYLANG_API, or __declspec(dllexport)).

§doc: Option<DocComment>

The optional documentation comment for the function

Implementations§

Source§

impl Function

Source

pub fn new(name: String, ret: Type) -> FunctionBuilder

Creates and returns a new FunctionBuilder to construct a Function using the builder pattern.

This method simplifies the process of creating complex function declarations by providing an interface for defining all function components.

§Parameters
  • name - The name of the function
  • ret - The return type of the function
§Returns

A FunctionBuilder instance that can be used to configure and build a Function

§Examples
let func = Function::new("calculate_sum".to_string(), Type::new(BaseType::Int).build())
    .params(vec![
        Parameter::new("a".to_string(), Type::new(BaseType::Int).build()),
        Parameter::new("b".to_string(), Type::new(BaseType::Int).build())
    ])
    .body(Block::new()
        .statement(Statement::Return(Some(Expr::Binary {
            left: Box::new(Expr::Ident("a".to_string())),
            op: BinOp::Add,
            right: Box::new(Expr::Ident("b".to_string()))
        })))
        .build())
    .build();
Source

pub fn to_prototype(&self) -> Function

Returns a prototype (declaration-only) copy of this function: same name, return type, parameters, and modifiers, but with the body dropped so it renders as ret name(params);

This is the main way to place a declaration in a header while keeping the definition in a source file.

§Examples
let def = FunctionBuilder::new_with_str("add", Type::new(BaseType::Int).build())
    .body(Block::new().build())
    .build();
assert_eq!(def.to_prototype().to_string(), "int add(void);\n");
Source

pub fn is_prototype(&self) -> bool

Returns true if this function is a prototype (has no body).

Trait Implementations§

Source§

impl Clone for Function

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 Function

Source§

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

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

impl Display for Function

Source§

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

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

impl Format for Function

Source§

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

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.