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: StringThe name of the function
params: Vec<Parameter>The parameters of the function as a vector of Parameter objects
ret: TypeThe return type of the function
is_inline: boolWhether the function is declared with the ‘inline’ keyword
storage: StorageClassThe 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
impl Function
Sourcepub fn new(name: String, ret: Type) -> FunctionBuilder
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 functionret- 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();Sourcepub fn to_prototype(&self) -> Function
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");Sourcepub fn is_prototype(&self) -> bool
pub fn is_prototype(&self) -> bool
Returns true if this function is a prototype (has no body).