llvm_lib/core/values/function_values/mod.rs
1use super::ValueRef;
2use crate::core::context::{AttributeRef, ContextRef};
3use crate::core::module::ModuleRef;
4use crate::core::types::TypeRef;
5use crate::{CStr, CString, CUint, GetRef, SizeT};
6use llvm_sys::core;
7
8pub mod function_parameters;
9
10/// Removes a function from its containing module and deallocates it.
11///
12/// This function wraps the `LLVMDeleteFunction` function from the LLVM core library. It removes the function
13/// represented by `val` from its parent module and deallocates all associated resources. After calling this
14/// function, the `ValueRef` should no longer be used, as it references deallocated memory.
15///
16/// # Safety
17///
18/// - The `ValueRef` must represent a valid function within a module.
19/// - After deletion, the `ValueRef` becomes invalid and should not be used.
20pub fn delete_function(val: &ValueRef) {
21 unsafe {
22 core::LLVMDeleteFunction(val.0);
23 }
24}
25
26/// Checks whether the given function has an associated personality function.
27///
28/// This function wraps the `LLVMHasPersonalityFn` function from the LLVM core library. A personality function
29/// is used in exception handling to provide language-specific semantics for stack unwinding.
30///
31/// # Parameters
32///
33/// - `val`: The `ValueRef` representing the function to check.
34///
35/// # Returns
36///
37/// Returns `true` if the function has an associated personality function, or `false` otherwise.
38#[must_use]
39pub fn has_personality_fn(val: &ValueRef) -> bool {
40 unsafe { core::LLVMHasPersonalityFn(val.0) != 0 }
41}
42
43/// Retrieves the personality function attached to the function.
44///
45/// This function wraps the `LLVMGetPersonalityFn` function from the LLVM core library. The personality
46/// function is used in exception handling to provide language-specific semantics for stack unwinding.
47///
48/// # Parameters
49///
50/// - `val`: The `ValueRef` representing the function.
51///
52/// # Returns
53///
54/// Returns an `Option<ValueRef>` representing the personality function if one is attached to the function,
55/// or `None` if no personality function is present.
56#[must_use]
57pub fn get_personality_fn(val: &ValueRef) -> Option<ValueRef> {
58 unsafe {
59 let personality_fn = core::LLVMGetPersonalityFn(val.0);
60 if personality_fn.is_null() {
61 None
62 } else {
63 Some(ValueRef(personality_fn))
64 }
65 }
66}
67
68/// Sets the personality function attached to the function.
69///
70/// This function wraps the `LLVMSetPersonalityFn` function from the LLVM core library. The personality
71/// function is used in exception handling to provide language-specific semantics for stack unwinding. By
72/// setting a personality function, you define how exceptions are handled within the function.
73///
74/// # Parameters
75///
76/// - `val`: The `ValueRef` representing the function.
77/// - `personality_fn`: A reference to the `ValueRef` representing the personality function to attach.
78pub fn set_personality_fn(val: &ValueRef, personality_fn: &ValueRef) {
79 unsafe {
80 core::LLVMSetPersonalityFn(val.0, personality_fn.0);
81 }
82}
83
84/// Looks up the intrinsic ID number that matches the given function name within the module.
85///
86/// This function wraps the `LLVMLookupIntrinsicID` function from the LLVM core library. It searches for an intrinsic
87/// function within the LLVM module represented by `m` that matches the specified name. Intrinsic functions are
88/// special functions provided by LLVM that perform specific operations at a lower level.
89///
90/// # Parameters
91///
92/// - `m`: The `ModuleRef` representing the LLVM module.
93/// - `name`: The name (`&str`) of the intrinsic function to look up.
94///
95/// # Returns
96///
97/// Returns an `Option<u32>` containing the intrinsic ID if a matching intrinsic is found, or `None` if no
98/// matching intrinsic exists within the module.
99#[must_use]
100pub fn lookup_intrinsic_id(name: &str) -> Option<u32> {
101 let c_string = CString::from(name);
102 unsafe {
103 let id =
104 core::LLVMLookupIntrinsicID(c_string.as_ptr(), *SizeT::from(c_string.count_bytes()));
105 if id == 0 {
106 None
107 } else {
108 Some(id)
109 }
110 }
111}
112
113/// Retrieves the intrinsic ID number from a function instance.
114///
115/// This function wraps the `LLVMGetIntrinsicID` function from the LLVM core library. Intrinsic functions in
116/// LLVM have unique ID numbers that can be used to identify and categorize them.
117///
118/// # Parameters
119///
120/// - `val`: The `ValueRef` representing the function.
121///
122/// # Returns
123///
124/// Returns a `u32` representing the intrinsic ID of the function. If the function is not an intrinsic, the ID
125/// returned will be `0`.
126#[must_use]
127pub fn get_intrinsic_id(val: &ValueRef) -> u32 {
128 unsafe { core::LLVMGetIntrinsicID(val.0) }
129}
130
131/// Creates or inserts the declaration of an intrinsic function within the module.
132///
133/// This function wraps the `LLVMGetIntrinsicDeclaration` function from the LLVM core library. It either retrieves
134/// an existing declaration of the specified intrinsic or creates a new one if it does not already exist. For
135/// overloaded intrinsics, parameter types must be provided to uniquely identify the desired overload.
136///
137/// # Parameters
138///
139/// - `m`: The `ModuleRef` representing the LLVM module.
140/// - `id`: The intrinsic ID (`u32`) corresponding to the desired intrinsic function.
141/// - `param_types`: A slice of LLVM type references (`&[LLVMTypeRef]`) representing the parameter types of the intrinsic.
142/// This is necessary for overloaded intrinsics to uniquely identify the correct version.
143///
144/// # Returns
145///
146/// Returns a `ValueRef` representing the intrinsic function declaration within the module. If the declaration
147/// cannot be created or retrieved, the returned `ValueRef` may be null, so users should ensure that the
148/// declaration was successfully obtained.
149#[must_use]
150pub fn get_intrinsic_declaration(m: &ModuleRef, id: u32, param_types: &[TypeRef]) -> ValueRef {
151 let param_types_ptr = crate::to_mut_ptr!(param_types);
152 unsafe {
153 let intrinsic = core::LLVMGetIntrinsicDeclaration(
154 m.get_ref(),
155 *CUint::from(id),
156 param_types_ptr,
157 *SizeT::from(param_types.len()),
158 );
159 ValueRef(intrinsic)
160 }
161}
162
163/// Retrieves the type of intrinsic. For overloaded intrinsics, parameter
164/// types must be provided to uniquely identify an overload.
165///
166/// This function wraps the `LLVMIntrinsicGetType` function from the LLVM core library. It obtains the
167/// LLVM type (`LLVMTypeRef`) of the intrinsic identified by `id` within the given context. For overloaded
168/// intrinsics, providing the correct parameter types ensures that the correct type is retrieved.
169///
170/// # Parameters
171///
172/// - `ctx`: The `LLVMContextRef` representing the LLVM context.
173/// - `id`: The intrinsic ID (`u32`) corresponding to the desired intrinsic function.
174/// - `param_types`: A slice of LLVM type references (`&[LLVMTypeRef]`) representing the parameter types of the intrinsic.
175/// This is necessary for overloaded intrinsics to uniquely identify the correct version.
176///
177/// # Returns
178///
179/// Returns an `LLVMTypeRef` representing the type of the intrinsic. If the intrinsic does not exist or the
180/// parameter types do not match any overload, the returned type may be null.
181#[must_use]
182pub fn intrinsic_get_type(ctx: &ContextRef, id: u32, param_types: &[TypeRef]) -> TypeRef {
183 let param_types_ptr = crate::to_mut_ptr!(param_types);
184 unsafe {
185 let type_ref = core::LLVMIntrinsicGetType(
186 ctx.get_ref(),
187 *CUint::from(id),
188 param_types_ptr,
189 *SizeT::from(param_types.len()),
190 );
191 TypeRef::from(type_ref)
192 }
193}
194
195/// Retrieves the name of an intrinsic.
196///
197/// This function wraps the `LLVMIntrinsicGetName` function from the LLVM core library. It obtains the name of
198/// the intrinsic identified by `id`.
199///
200/// # Parameters
201///
202/// - `id`: The intrinsic ID (`u32`) corresponding to the desired intrinsic function.
203/// - `name_length`: A mutable reference to a `usize` where the length of the name will be stored.
204///
205/// # Returns
206///
207/// Returns a raw pointer to a null-terminated string (`*const i8`) representing the name of the intrinsic.
208/// The length of the name is stored in `name_length`. The returned string should not be modified or freed by
209/// the caller.
210pub fn intrinsic_get_name(id: u32, name_length: &mut usize) -> *const i8 {
211 unsafe { core::LLVMIntrinsicGetName(id, name_length) }
212}
213
214/// Copies the name of an overloaded intrinsic identified by a given list of
215/// parameter types.
216///
217/// Unlike `intrinsic_get_name`, the caller is responsible for freeing the
218/// returned string.
219///
220/// This function wraps the `LLVMIntrinsicCopyOverloadedName2` function from the LLVM core library. It retrieves
221/// the name of an overloaded intrinsic based on the provided parameter types.
222///
223/// # Parameters
224///
225/// - `m`: The `ModuleRef` representing the LLVM module.
226/// - `id`: The intrinsic ID (`u32`) corresponding to the desired intrinsic function.
227/// - `param_types`: A slice of LLVM type references (`&[LLVMTypeRef]`) representing the parameter types of the intrinsic.
228/// - `name_length`: A mutable reference to a `usize` where the length of the name will be stored.
229///
230/// # Returns
231///
232/// Returns a raw pointer to a null-terminated string (`*const i8`) representing the name of the overloaded intrinsic.
233/// The length of the name is stored in `name_length`. The caller is responsible for freeing the returned string
234/// using the appropriate memory deallocation function (e.g., `LLVMDisposeMessage`).
235#[must_use]
236pub fn intrinsic_copy_overloaded_name2(
237 m: &ModuleRef,
238 id: u32,
239 param_types: &[TypeRef],
240) -> Option<String> {
241 let param_types_ptr = crate::to_mut_ptr!(param_types);
242 unsafe {
243 let mut length = *SizeT::from(0_usize);
244
245 let c_str = core::LLVMIntrinsicCopyOverloadedName2(
246 m.get_ref(),
247 id,
248 param_types_ptr,
249 *SizeT::from(param_types.len()),
250 &mut length,
251 );
252 if c_str.is_null() {
253 return None;
254 }
255 Some(CStr::new(c_str).to_string())
256 }
257}
258
259/// Determines if the intrinsic identified by the given ID is overloaded.
260///
261/// This function wraps the `LLVMIntrinsicIsOverloaded` function from the LLVM core library. Overloaded intrinsics
262/// can have multiple versions differentiated by their parameter types.
263///
264/// # Parameters
265///
266/// - `id`: The intrinsic ID (`u32`) corresponding to the desired intrinsic function.
267///
268/// # Returns
269///
270/// Returns `true` if the intrinsic is overloaded, or `false` otherwise.
271#[must_use]
272pub fn intrinsic_is_overloaded(id: u32) -> bool {
273 unsafe { core::LLVMIntrinsicIsOverloaded(*CUint::from(id)) != 0 }
274}
275
276/// Obtains the calling convention of a function.
277///
278/// The returned value corresponds to the LLVM calling convention enumeration.
279///
280/// This function wraps the `LLVMGetFunctionCallConv` function from the LLVM core library.
281///
282/// # Parameters
283///
284/// - `fn_val`: The `ValueRef` representing the function.
285///
286/// # Returns
287///
288/// Returns an `unsigned` integer corresponding to the LLVM calling convention of the function.
289#[must_use]
290pub fn get_function_call_conv(fn_val: &ValueRef) -> u32 {
291 unsafe { core::LLVMGetFunctionCallConv(fn_val.0) }
292}
293
294/// Sets the calling convention of a function.
295///
296/// This function wraps the `LLVMSetFunctionCallConv` function from the LLVM core library.
297///
298/// # Parameters
299///
300/// - `fn_val`: The `ValueRef` representing the function.
301/// - `cc`: The LLVM calling convention (`u32`) to set for the function.
302pub fn set_function_call_conv(fn_val: &ValueRef, cc: u32) {
303 unsafe {
304 core::LLVMSetFunctionCallConv(fn_val.0, cc);
305 }
306}
307
308/// Obtains the name of the garbage collector to use during code generation.
309///
310/// This function wraps the `LLVMGetGC` function from the LLVM core library. The garbage collector name
311/// specifies which garbage collection strategy to use for the function.
312///
313/// # Parameters
314///
315/// - `fn_val`: The `ValueRef` representing the function.
316///
317/// # Returns
318///
319/// Returns a raw pointer to a null-terminated string (`*const i8`) representing the name of the garbage collector.
320/// If no garbage collector is specified, it may return a null pointer.
321///
322/// # Safety
323///
324/// The returned string should not be modified or freed by the caller.
325#[must_use]
326pub fn get_gc(fn_val: &ValueRef) -> Option<String> {
327 unsafe {
328 let c_str = core::LLVMGetGC(fn_val.0);
329 if c_str.is_null() {
330 return None;
331 }
332 Some(CStr::new(c_str).to_string())
333 }
334}
335
336/// Defines the garbage collector to use during code generation.
337///
338/// This function wraps the `LLVMSetGC` function from the LLVM core library. It sets the name of the garbage collector
339/// to be used for the specified function during code generation.
340///
341/// # Parameters
342///
343/// - `fn_val`: The `ValueRef` representing the function.
344/// - `name`: The name (`&str`) of the garbage collector to set.
345///
346/// # Safety
347///
348/// - The `ValueRef` must represent a valid function within a module.
349/// - The `name` must not contain null bytes.
350///
351/// # Example
352///
353/// ```rust
354/// set_gc(function, "my_gc");
355/// ```
356pub fn set_gc(fn_val: &ValueRef, name: &str) {
357 let c_string = CString::from(name);
358 unsafe {
359 core::LLVMSetGC(fn_val.0, c_string.as_ptr());
360 }
361}
362
363/// Retrieves the prefix data associated with a function.
364///
365/// This function wraps the `LLVMGetPrefixData` function from the LLVM core library. It obtains the prefix data
366/// attached to the specified function. Prefix data is used to attach additional information to functions.
367///
368/// # Parameters
369///
370/// - `fn_val`: The `ValueRef` representing the function.
371///
372/// # Returns
373///
374/// Returns an `Option<ValueRef>` containing the prefix data if it exists, or `None` otherwise.
375///
376/// # Safety
377///
378/// - The `ValueRef` must represent a valid function within a module.
379/// - The returned `ValueRef` should not be used if it is `None`.
380#[must_use]
381pub fn get_prefix_data(fn_val: &ValueRef) -> Option<ValueRef> {
382 unsafe {
383 let prefix_data = core::LLVMGetPrefixData(fn_val.0);
384 if prefix_data.is_null() {
385 None
386 } else {
387 Some(ValueRef(prefix_data))
388 }
389 }
390}
391
392/// Checks if a given function has prefix data.
393///
394/// This function wraps the `LLVMHasPrefixData` function from the LLVM core library. It determines whether
395/// the specified function has prefix data attached.
396///
397/// # Parameters
398///
399/// - `fn_val`: The `ValueRef` representing the function.
400///
401/// # Returns
402///
403/// Returns `true` if the function has prefix data, or `false` otherwise.
404///
405/// # Safety
406///
407/// - The `ValueRef` must represent a valid function within a module.
408#[must_use]
409pub fn has_prefix_data(fn_val: &ValueRef) -> bool {
410 unsafe { core::LLVMHasPrefixData(fn_val.0) != 0 }
411}
412
413/// Sets the prefix data for the function.
414///
415/// This function wraps the `LLVMSetPrefixData` function from the LLVM core library. It attaches prefix data
416/// to the specified function.
417///
418/// # Parameters
419///
420/// - `fn_val`: The `ValueRef` representing the function.
421/// - `prefix_data`: The `ValueRef` representing the prefix data to attach.
422///
423/// # Safety
424///
425/// - The `ValueRef` must represent a valid function within a module.
426/// - The `prefix_data` must represent valid prefix data.
427pub fn set_prefix_data(fn_val: &ValueRef, prefix_data: &ValueRef) {
428 unsafe {
429 core::LLVMSetPrefixData(fn_val.0, prefix_data.0);
430 }
431}
432
433/// Retrieves the prologue data associated with a function.
434///
435/// This function wraps the `LLVMGetPrologueData` function from the LLVM core library. It obtains the prologue data
436/// attached to the specified function. Prologue data is used to attach additional information to functions.
437///
438/// # Parameters
439///
440/// - `fn_val`: The `ValueRef` representing the function.
441///
442/// # Returns
443///
444/// Returns an `Option<ValueRef>` containing the prologue data if it exists, or `None` otherwise.
445///
446/// # Safety
447///
448/// - The `ValueRef` must represent a valid function within a module.
449/// - The returned `ValueRef` should not be used if it is `None`.
450#[must_use]
451pub fn get_prologue_data(fn_val: &ValueRef) -> Option<ValueRef> {
452 unsafe {
453 let prologue_data = core::LLVMGetPrologueData(fn_val.0);
454 if prologue_data.is_null() {
455 None
456 } else {
457 Some(ValueRef(prologue_data))
458 }
459 }
460}
461
462/// Checks if a given function has prologue data.
463///
464/// This function wraps the `LLVMHasPrologueData` function from the LLVM core library. It determines whether
465/// the specified function has prologue data attached.
466///
467/// # Parameters
468///
469/// - `fn_val`: The `ValueRef` representing the function.
470///
471/// # Returns
472///
473/// Returns `true` if the function has prologue data, or `false` otherwise.
474///
475/// # Safety
476///
477/// - The `ValueRef` must represent a valid function within a module.
478#[must_use]
479pub fn has_prologue_data(fn_val: &ValueRef) -> bool {
480 unsafe { core::LLVMHasPrologueData(fn_val.0) != 0 }
481}
482
483/// Sets the prologue data for the function.
484///
485/// This function wraps the `LLVMSetPrologueData` function from the LLVM core library. It attaches prologue data
486/// to the specified function.
487///
488/// # Parameters
489///
490/// - `fn_val`: The `ValueRef` representing the function.
491/// - `prologue_data`: The `ValueRef` representing the prologue data to attach.
492///
493/// # Safety
494///
495/// - The `ValueRef` must represent a valid function within a module.
496/// - The `prologue_data` must represent valid prologue data.
497pub fn set_prologue_data(fn_val: &ValueRef, prologue_data: &ValueRef) {
498 unsafe {
499 core::LLVMSetPrologueData(fn_val.0, prologue_data.0);
500 }
501}
502
503/// Adds an attribute to a function at a specified index.
504///
505/// This function wraps the `LLVMAddAttributeAtIndex` function from the LLVM core library. Attributes provide
506/// additional metadata about functions, such as optimization hints or specific calling conventions.
507///
508/// # Parameters
509///
510/// - `fn_val`: The `ValueRef` representing the function.
511/// - `idx`: The `LLVMAttributeIndex` indicating where to add the attribute (e.g., function attributes, return attributes).
512/// - `attr`: The `LLVMAttributeRef` representing the attribute to add.
513///
514/// # Safety
515///
516/// - The `ValueRef` must represent a valid function within a module.
517/// - The `LLVMAttributeRef` must represent a valid attribute.
518pub fn add_attribute_at_index(fn_val: &ValueRef, idx: u32, attr: &AttributeRef) {
519 unsafe {
520 core::LLVMAddAttributeAtIndex(fn_val.0, *CUint::from(idx), attr.get_ref());
521 }
522}
523
524/// Retrieves the number of attributes at a specified index for a function.
525///
526/// This function wraps the `LLVMGetAttributeCountAtIndex` function from the LLVM core library. It returns the
527/// number of attributes present at the specified index.
528///
529/// # Parameters
530///
531/// - `fn_val`: The `ValueRef` representing the function.
532/// - `idx`: The `LLVMAttributeIndex` indicating which attribute set to query.
533///
534/// # Returns
535///
536/// Returns an `unsigned` integer representing the number of attributes at the specified index.
537///
538/// # Safety
539///
540/// - The `ValueRef` must represent a valid function within a module.
541/// - The `LLVMAttributeIndex` must be valid for the function.
542#[must_use]
543pub fn get_attribute_count_at_index(fn_val: &ValueRef, idx: u32) -> u32 {
544 unsafe { core::LLVMGetAttributeCountAtIndex(fn_val.0, *CUint::from(idx)) }
545}
546
547/// Retrieves all attributes at a specified index for a function.
548///
549/// This function wraps the `LLVMGetAttributesAtIndex` function from the LLVM core library. It fills the provided
550/// slice with the attributes present at the specified index.
551///
552/// # Parameters
553///
554/// - `fn_val`: The `ValueRef` representing the function.
555/// - `idx`: The `LLVMAttributeIndex` indicating which attribute set to query.
556/// - `attrs`: A mutable slice of `LLVMAttributeRef` where the attributes will be stored.
557///
558/// # Safety
559///
560/// - The `ValueRef` must represent a valid function within a module.
561/// - The `LLVMAttributeIndex` must be valid for the function.
562/// - The `attrs` slice must be large enough to hold all attributes at the specified index.
563pub fn get_attributes_at_index(fn_val: &ValueRef, idx: u32, attrs: &[AttributeRef]) {
564 let attrs_ptr = crate::to_mut_ptr!(attrs);
565 unsafe {
566 core::LLVMGetAttributesAtIndex(fn_val.0, *CUint::from(idx), attrs_ptr);
567 }
568}
569
570/// Retrieves an enum attribute at a specified index for a function.
571///
572/// This function wraps the `LLVMGetEnumAttributeAtIndex` function from the LLVM core library. It fetches the
573/// enum attribute corresponding to the provided `KindID` at the specified index.
574///
575/// # Parameters
576///
577/// - `fn_val`: The `ValueRef` representing the function.
578/// - `idx`: The `LLVMAttributeIndex` indicating where to retrieve the attribute.
579/// - `kind_id`: The `unsigned` integer representing the kind of enum attribute to retrieve.
580///
581/// # Returns
582///
583/// Returns an `Option<LLVMAttributeRef>` containing the attribute if found, or `None` otherwise.
584///
585/// # Safety
586///
587/// - The `ValueRef` must represent a valid function within a module.
588/// - The `KindID` must correspond to a valid enum attribute.
589#[must_use]
590pub fn get_enum_attribute_at_index(
591 fn_val: &ValueRef,
592 idx: u32,
593 kind_id: u32,
594) -> Option<AttributeRef> {
595 unsafe {
596 let attr =
597 core::LLVMGetEnumAttributeAtIndex(fn_val.0, *CUint::from(idx), *CUint::from(kind_id));
598 if attr.is_null() {
599 None
600 } else {
601 Some(AttributeRef::from(attr))
602 }
603 }
604}
605
606/// Retrieves a string attribute at a specified index for a function.
607///
608/// This function wraps the `LLVMGetStringAttributeAtIndex` function from the LLVM core library. It fetches the
609/// string attribute corresponding to the provided key at the specified index.
610///
611/// # Parameters
612///
613/// - `fn_val`: The `ValueRef` representing the function.
614/// - `idx`: The `LLVMAttributeIndex` indicating where to retrieve the attribute.
615/// - `key`: The key (`&str`) identifying the string attribute.
616///
617/// # Returns
618///
619/// Returns an `Option<LLVMAttributeRef>` containing the attribute if found, or `None` otherwise.
620///
621/// # Safety
622///
623/// - The `ValueRef` must represent a valid function within a module.
624/// - The `key` must not contain null bytes.
625#[must_use]
626pub fn get_string_attribute_at_index(
627 fn_val: &ValueRef,
628 idx: u32,
629 key: &str,
630) -> Option<AttributeRef> {
631 let c_key = CString::from(key);
632 unsafe {
633 let attr = core::LLVMGetStringAttributeAtIndex(
634 fn_val.0,
635 *CUint::from(idx),
636 c_key.as_ptr(),
637 *CUint::from(c_key.count_bytes()),
638 );
639 if attr.is_null() {
640 None
641 } else {
642 Some(AttributeRef::from(attr))
643 }
644 }
645}
646
647/// Removes an enum attribute at a specified index for a function.
648///
649/// This function wraps the `LLVMRemoveEnumAttributeAtIndex` function from the LLVM core library. It removes the
650/// enum attribute corresponding to the provided `KindID` at the specified index.
651///
652/// # Parameters
653///
654/// - `fn_val`: The `ValueRef` representing the function.
655/// - `idx`: The `LLVMAttributeIndex` indicating where to remove the attribute.
656/// - `kind_id`: The `unsigned` integer representing the kind of enum attribute to remove.
657///
658/// # Safety
659///
660/// - The `ValueRef` must represent a valid function within a module.
661/// - The `KindID` must correspond to a valid enum attribute.
662pub fn remove_enum_attribute_at_index(fn_val: &ValueRef, idx: u32, kind_id: u32) {
663 unsafe {
664 core::LLVMRemoveEnumAttributeAtIndex(fn_val.0, *CUint::from(idx), *CUint::from(kind_id));
665 }
666}
667
668/// Removes a string attribute at a specified index for a function.
669///
670/// This function wraps the `LLVMRemoveStringAttributeAtIndex` function from the LLVM core library. It removes the
671/// string attribute corresponding to the provided key at the specified index.
672///
673/// # Parameters
674///
675/// - `fn_val`: The `ValueRef` representing the function.
676/// - `idx`: The `LLVMAttributeIndex` indicating where to remove the attribute.
677/// - `key`: The key (`&str`) identifying the string attribute to remove.
678///
679/// # Safety
680///
681/// - The `ValueRef` must represent a valid function within a module.
682/// - The `key` must not contain null bytes.
683pub fn remove_string_attribute_at_index(fn_val: &ValueRef, idx: u32, key: &str) {
684 let c_key = CString::from(key);
685 unsafe {
686 core::LLVMRemoveStringAttributeAtIndex(
687 fn_val.0,
688 *CUint::from(idx),
689 c_key.as_ptr(),
690 *CUint::from(c_key.count_bytes()),
691 );
692 }
693}
694
695/// Adds a target-dependent attribute to a function.
696///
697/// This function wraps the `LLVMAddTargetDependentFunctionAttr` function from the LLVM core library. It attaches
698/// a target-specific attribute with a specified value to the given function. Target-dependent attributes can be
699/// used to provide additional metadata or optimization hints that are specific to a particular target architecture.
700///
701/// # Parameters
702///
703/// - `fn_val`: The `ValueRef` representing the function to which the attribute will be added.
704/// - `attribute`: The name (`&str`) of the attribute to add.
705/// - `value`: The value (`&str`) associated with the attribute.
706///
707/// # Safety
708///
709/// - The `ValueRef` must represent a valid function within a module.
710/// - The `attribute` and `value` strings must not contain null bytes.
711///
712pub fn add_target_dependent_function_attr(fn_val: &ValueRef, attribute: &str, value: &str) {
713 let attr_cstr = CString::from(attribute);
714 let value_cstr = CString::from(value);
715 unsafe {
716 core::LLVMAddTargetDependentFunctionAttr(fn_val.0, attr_cstr.as_ptr(), value_cstr.as_ptr());
717 }
718}