Skip to main content

Function

Struct Function 

Source
pub struct Function {
Show 23 fields pub name: String, pub sig: Signature, pub params: Vec<ObjectId>, pub param_names: Vec<Option<String>>, pub is_static: bool, pub is_inline: bool, pub noreturn: bool, pub inline_hint: Option<InlineHint>, pub cold: bool, pub deprecated: Option<Option<String>>, pub section: Option<String>, pub asm_label: Option<String>, pub init_kind: Option<InitKind>, pub target_features: Vec<String>, pub address_taken: bool, pub intrinsic: Option<&'static Intrinsic>, pub safe: Option<SourceRange>, pub uses_arena: bool, pub locals: Vec<ObjectId>, pub body: Option<Body>, pub item_name: Option<String>, pub env: Vec<EnvParam>, pub range: SourceRange,
}
Expand description

A function declared or defined in the translation unit.

Fields§

§name: String

The name as written in C.

§sig: Signature

The signature every declaration of it must agree on.

§params: Vec<ObjectId>

The parameter objects. Empty until the definition is seen.

§param_names: Vec<Option<String>>

The parameter names as first declared, for the generated extern block.

§is_static: bool

Whether the function was declared static, i.e. is private to the unit.

§is_inline: bool

Whether the function was declared inline.

§noreturn: bool

Whether the function was declared _Noreturn (or [[noreturn]]), so that a call to it ends the statement it is in.

§inline_hint: Option<InlineHint>

What always_inline / noinline asked for.

§cold: bool

Whether __attribute__((cold)) marked it unlikely.

§deprecated: Option<Option<String>>

The message __attribute__((deprecated)) gave, if it was there at all.

§section: Option<String>

The section __attribute__((section("…"))) asked for.

§asm_label: Option<String>

The symbol __asm__("name") renamed the function to.

§init_kind: Option<InitKind>

Whether __attribute__((constructor)) asked for it to run before main, or destructor for after it.

§target_features: Vec<String>

The instruction sets __attribute__((target("…"))) or #pragma GCC target("…") asked for, in Rust’s spelling, which the generated item carries as #[target_feature(enable = "…")].

Only a definition can carry them: an extern declaration has no item for the attribute to go on, and the function it names was compiled somewhere else.

§address_taken: bool

Whether anything in the unit uses the function other than by calling it by name: its address taken as a value (&f, or f where a pointer is wanted), or it named by __attribute__((cleanup)), whose guard holds a pointer to it. Such a function has to be an extern "C" fn, since that is what a C function pointer is; one that is only ever called can be a Rust function, which is what an always-inline helper with target features needs.

§intrinsic: Option<&'static Intrinsic>

Set when this name is an x86 intrinsic rather than a symbol: a call to it is generated as ::core::arch::x86_64::<name>, and it is left out of the unit’s extern block because there is nothing to link.

§safe: Option<SourceRange>

Where the function was asked to be safe, if it was: [[cinrs::safe]], __attribute__((cinrs_safe)) or #pragma cinrs safe.

A safe function is generated as extern "C" fn rather than unsafe extern "C" fn, and its body is not wrapped in an unsafe block, so rustc checks it. The range is where the request was written, which is what the diagnostics about it point at.

§uses_arena: bool

Whether the body declares a variable length array or calls alloca, in which case the generated item opens with the bump arena both emulations allocate out of.

The arena is per function and is dropped by the return, which frees everything in it: that is alloca’s lifetime, and a variable length array gives its space back earlier, at the end of its block, by moving the arena’s position back down. See codegen.

§locals: Vec<ObjectId>

Every automatic object the body declared, in declaration order.

Code generation needs the whole list — not only the ones a let statement is visible for — because a local declared inside a statement expression is a binding too and may need renaming apart.

§body: Option<Body>

The body, present once a definition has been type checked.

§item_name: Option<String>

The Rust item name, when it is not the C name.

Only a lifted GNU nested function has one: it becomes a file-scope item, so it needs a name that cannot collide with the C function of the same name at file scope, while Function::name stays what the program called it — which is what diagnostics and __func__ say.

§env: Vec<EnvParam>

The hidden environment parameters a lifted GNU nested function takes in front of its declared ones, in the order they are passed.

Empty for every ordinary function, and for a nested one that captures nothing — which is why such a nested function’s address may still be taken: the generated item has exactly the signature C gave it.

§range: SourceRange

Where the function’s name was written, at its definition if there is one and at its first declaration otherwise.

Implementations§

Source§

impl Function

Source

pub fn is_extern(&self) -> bool

Whether this function is only declared here and linked from elsewhere.

Source

pub fn item_name(&self) -> &str

The name the generated Rust item has.

Source

pub fn is_nested(&self) -> bool

Whether this is a lifted GNU nested function.

Source

pub fn is_safe(&self) -> bool

Whether the function is generated without unsafe; see Function::safe.

Source

pub fn inline_helper(&self) -> bool

Whether the function is an always_inline helper under a target feature that is generated as a Rust #[inline(always)] unsafe fn without #[target_feature].

rustc refuses #[inline(always)] together with #[target_feature] (rust-lang/rust#145574), and #[inline] alone is a hint LLVM may decline: BLAKE3’s round_fn16, called seven times from one kernel, was left out of line, its state spilled to the stack around every call. GCC’s contract makes the attribute unnecessary: it refuses to inline an always_inline function into a caller without its target options, so every caller of one that compiles has the features, and once the helper is inlined into such a caller the intrinsics in its body are inlined there too. Without the attribute the helper must not be a C-ABI function — the C ABI passes a 256- or 512-bit vector in registers the feature provides — so it is a Rust function, and that is only possible when nothing needs it as a C function pointer: it is static, never has its address taken, is not variadic, nested, safe (whose body would then call intrinsics without unsafe), a constructor or an intrinsic.

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

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

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.