llvm_lib/core/values/function_values/
function_parameters.rs

1use super::ValueRef;
2use llvm_sys::core;
3
4/// Obtains the number of parameters in a function.
5///
6/// This function wraps the `LLVMCountParams` function from the LLVM core library. It returns the count of parameters
7/// that the specified function accepts.
8///
9/// # Parameters
10///
11/// - `fn_val`: The `ValueRef` representing the function.
12///
13/// # Returns
14///
15/// Returns an `unsigned` integer (`u32`) indicating the number of parameters in the function.
16///
17/// # Safety
18///
19/// - The `ValueRef` must represent a valid function within a module.
20#[must_use]
21pub fn count_params(fn_val: &ValueRef) -> u32 {
22    unsafe { core::LLVMCountParams(fn_val.0) }
23}
24
25/// Retrieves the parameters of a function.
26///
27/// This function wraps the `LLVMGetParams` function from the LLVM core library. It fills the provided mutable slice
28/// with the parameters of the specified function. Each parameter is represented as a `ValueRef`.
29///
30/// # Parameters
31///
32/// - `fn_val`: The `ValueRef` representing the function.
33/// - `params`: A mutable slice of `ValueRef` where the function's parameters will be stored.
34///
35/// # Safety
36///
37/// - The `ValueRef` must represent a valid function within a module.
38/// - The `params` slice must be pre-allocated and have a length of at least `count_params(fn_val)`.
39pub fn get_params(fn_val: &ValueRef, params: &mut &[ValueRef]) {
40    let params_ptr = crate::to_mut_ptr!(params);
41    unsafe {
42        core::LLVMGetParams(fn_val.0, params_ptr);
43    }
44}
45
46/// Retrieves a specific parameter of a function by index.
47///
48/// This function wraps the `LLVMGetParam` function from the LLVM core library. It returns the parameter at the
49/// specified index within the function's parameter list.
50///
51/// # Parameters
52///
53/// - `fn_val`: The `ValueRef` representing the function.
54/// - `index`: The zero-based index of the parameter to retrieve.
55///
56/// # Returns
57///
58/// Returns an `Option<ValueRef>` containing the parameter if the index is valid, or `None` if the index is out of bounds.
59///
60/// # Safety
61///
62/// - The `ValueRef` must represent a valid function within a module.
63/// - `index` must be less than the number of parameters in the function.
64#[must_use]
65pub fn get_param(fn_val: &ValueRef, index: u32) -> Option<ValueRef> {
66    unsafe {
67        let param = core::LLVMGetParam(fn_val.0, index);
68        if param.is_null() {
69            None
70        } else {
71            Some(ValueRef(param))
72        }
73    }
74}
75
76/// Retrieves the parent function of a given argument.
77///
78/// This function wraps the `LLVMGetParamParent` function from the LLVM core library. It returns the function to
79/// which the specified argument belongs.
80///
81/// # Parameters
82///
83/// - `arg`: The `ValueRef` representing the argument.
84///
85/// # Returns
86///
87/// Returns an `Option<ValueRef>` containing the parent function if it exists, or `None` otherwise.
88///
89/// # Safety
90///
91/// - The `ValueRef` must represent a valid argument within a function.
92#[must_use]
93pub fn get_param_parent(arg: &ValueRef) -> Option<ValueRef> {
94    unsafe {
95        let parent = core::LLVMGetParamParent(arg.0);
96        if parent.is_null() {
97            None
98        } else {
99            Some(ValueRef(parent))
100        }
101    }
102}
103
104/// Retrieves the first parameter of a function.
105///
106/// This function wraps the `LLVMGetFirstParam` function from the LLVM core library. It returns the first parameter
107/// of the specified function.
108///
109/// # Parameters
110///
111/// - `fn_val`: The `ValueRef` representing the function.
112///
113/// # Returns
114///
115/// Returns an `Option<ValueRef>` containing the first parameter if it exists, or `None` if the function has no parameters.
116///
117/// # Safety
118///
119/// - The `ValueRef` must represent a valid function within a module.
120#[must_use]
121pub fn get_first_param(fn_val: &ValueRef) -> Option<ValueRef> {
122    unsafe {
123        let first = core::LLVMGetFirstParam(fn_val.0);
124        if first.is_null() {
125            None
126        } else {
127            Some(ValueRef(first))
128        }
129    }
130}
131
132/// Retrieves the last parameter of a function.
133///
134/// This function wraps the `LLVMGetLastParam` function from the LLVM core library. It returns the last parameter
135/// of the specified function.
136///
137/// # Parameters
138///
139/// - `fn_val`: The `ValueRef` representing the function.
140///
141/// # Returns
142///
143/// Returns an `Option<ValueRef>` containing the last parameter if it exists, or `None` if the function has no parameters.
144///
145/// # Safety
146///
147/// - The `ValueRef` must represent a valid function within a module.
148#[must_use]
149pub fn get_last_param(fn_val: &ValueRef) -> Option<ValueRef> {
150    unsafe {
151        let last = core::LLVMGetLastParam(fn_val.0);
152        if last.is_null() {
153            None
154        } else {
155            Some(ValueRef(last))
156        }
157    }
158}
159
160/// Retrieves the next parameter in a function's parameter list.
161///
162/// This function wraps the `LLVMGetNextParam` function from the LLVM core library. Given a current parameter, it returns
163/// the next parameter in the function's parameter list.
164///
165/// # Parameters
166///
167/// - `arg`: The `ValueRef` representing the current parameter.
168///
169/// # Returns
170///
171/// Returns an `Option<ValueRef>` containing the next parameter if it exists, or `None` if there is no next parameter.
172///
173/// # Safety
174///
175/// - The `ValueRef` must represent a valid parameter within a function.
176#[must_use]
177pub fn get_next_param(arg: &ValueRef) -> Option<ValueRef> {
178    unsafe {
179        let next = core::LLVMGetNextParam(arg.0);
180        if next.is_null() {
181            None
182        } else {
183            Some(ValueRef(next))
184        }
185    }
186}
187
188/// Retrieves the previous parameter in a function's parameter list.
189///
190/// This function wraps the `LLVMGetPreviousParam` function from the LLVM core library. Given a current parameter, it returns
191/// the previous parameter in the function's parameter list.
192///
193/// # Parameters
194///
195/// - `arg`: The `ValueRef` representing the current parameter.
196///
197/// # Returns
198///
199/// Returns an `Option<ValueRef>` containing the previous parameter if it exists, or `None` if there is no previous parameter.
200///
201/// # Safety
202///
203/// - The `ValueRef` must represent a valid parameter within a function.
204#[must_use]
205pub fn get_previous_param(arg: &ValueRef) -> Option<ValueRef> {
206    unsafe {
207        let previous = core::LLVMGetPreviousParam(arg.0);
208        if previous.is_null() {
209            None
210        } else {
211            Some(ValueRef(previous))
212        }
213    }
214}
215
216/// Sets the alignment for a function parameter.
217///
218/// This function wraps the `LLVMSetParamAlignment` function from the LLVM core library. It sets the alignment requirement
219/// for the specified function parameter.
220///
221/// # Parameters
222///
223/// - `arg`: The `ValueRef` representing the parameter.
224/// - `align`: The alignment (`unsigned`, typically `u32`) to set for the parameter.
225///
226/// # Safety
227///
228/// - The `ValueRef` must represent a valid parameter within a function.
229/// - `align` must be a valid alignment value as per LLVM's requirements.
230pub fn set_param_alignment(arg: &ValueRef, align: u32) {
231    unsafe {
232        core::LLVMSetParamAlignment(arg.0, align);
233    }
234}