1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use std::fmt::{self, Display};

use either::Either;
use llvm_sys::core::{
    LLVMGetInstructionCallConv, LLVMGetTypeKind, LLVMIsTailCall, LLVMSetInstrParamAlignment,
    LLVMSetInstructionCallConv, LLVMSetTailCall, LLVMTypeOf,
};
#[llvm_versions(18.0..=latest)]
use llvm_sys::core::{LLVMGetTailCallKind, LLVMSetTailCallKind};
use llvm_sys::prelude::LLVMValueRef;
use llvm_sys::LLVMTypeKind;

use crate::attributes::{Attribute, AttributeLoc};
use crate::values::{AsValueRef, BasicValueEnum, FunctionValue, InstructionValue, Value};

use super::AnyValue;

/// A value resulting from a function call. It may have function attributes applied to it.
///
/// This struct may be removed in the future in favor of an `InstructionValue<CallSite>` type.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct CallSiteValue<'ctx>(Value<'ctx>);

impl<'ctx> CallSiteValue<'ctx> {
    /// Get a value from an [LLVMValueRef].
    ///
    /// # Safety
    ///
    /// The ref must be valid and of type call site.
    pub unsafe fn new(value: LLVMValueRef) -> Self {
        CallSiteValue(Value::new(value))
    }

    /// Sets whether or not this call is a tail call.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.set_tail_call(true);
    /// ```
    pub fn set_tail_call(self, tail_call: bool) {
        unsafe { LLVMSetTailCall(self.as_value_ref(), tail_call as i32) }
    }

    /// Determines whether or not this call is a tail call.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.set_tail_call(true);
    ///
    /// assert!(call_site_value.is_tail_call());
    /// ```
    pub fn is_tail_call(self) -> bool {
        unsafe { LLVMIsTailCall(self.as_value_ref()) == 1 }
    }

    /// Returns tail, musttail, and notail attributes.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::values::LLVMTailCallKind::*;
    ///
    /// let context = inkwell::context::Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// assert_eq!(call_site.get_tail_call_kind(), LLVMTailCallKindNone);
    /// ```
    #[llvm_versions(18.0..=latest)]
    pub fn get_tail_call_kind(self) -> super::LLVMTailCallKind {
        unsafe { LLVMGetTailCallKind(self.as_value_ref()) }
    }

    /// Sets tail, musttail, and notail attributes.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::values::LLVMTailCallKind::*;
    ///
    /// let context = inkwell::context::Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site.set_tail_call_kind(LLVMTailCallKindTail);
    /// assert_eq!(call_site.get_tail_call_kind(), LLVMTailCallKindTail);
    /// ```
    #[llvm_versions(18.0..=latest)]
    pub fn set_tail_call_kind(self, kind: super::LLVMTailCallKind) {
        unsafe { LLVMSetTailCallKind(self.as_value_ref(), kind) };
    }

    /// Try to convert this `CallSiteValue` to a `BasicValueEnum` if not a void return type.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// assert!(call_site_value.try_as_basic_value().is_right());
    /// ```
    pub fn try_as_basic_value(self) -> Either<BasicValueEnum<'ctx>, InstructionValue<'ctx>> {
        unsafe {
            match LLVMGetTypeKind(LLVMTypeOf(self.as_value_ref())) {
                LLVMTypeKind::LLVMVoidTypeKind => Either::Right(InstructionValue::new(self.as_value_ref())),
                _ => Either::Left(BasicValueEnum::new(self.as_value_ref())),
            }
        }
    }

    /// Adds an `Attribute` to this `CallSiteValue`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    /// ```
    pub fn add_attribute(self, loc: AttributeLoc, attribute: Attribute) {
        use llvm_sys::core::LLVMAddCallSiteAttribute;

        unsafe { LLVMAddCallSiteAttribute(self.as_value_ref(), loc.get_index(), attribute.attribute) }
    }

    /// Gets the `FunctionValue` this `CallSiteValue` is based on.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// assert_eq!(call_site_value.get_called_fn_value(), fn_value);
    /// ```
    pub fn get_called_fn_value(self) -> FunctionValue<'ctx> {
        use llvm_sys::core::LLVMGetCalledValue;

        unsafe { FunctionValue::new(LLVMGetCalledValue(self.as_value_ref())).expect("This should never be null?") }
    }

    /// Counts the number of `Attribute`s on this `CallSiteValue` at an index.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    ///
    /// assert_eq!(call_site_value.count_attributes(AttributeLoc::Return), 2);
    /// ```
    pub fn count_attributes(self, loc: AttributeLoc) -> u32 {
        use llvm_sys::core::LLVMGetCallSiteAttributeCount;

        unsafe { LLVMGetCallSiteAttributeCount(self.as_value_ref(), loc.get_index()) }
    }

    /// Get all `Attribute`s on this `CallSiteValue` at an index.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    ///
    /// assert_eq!(call_site_value.attributes(AttributeLoc::Return), vec![ string_attribute, enum_attribute ]);
    /// ```
    pub fn attributes(self, loc: AttributeLoc) -> Vec<Attribute> {
        use llvm_sys::core::LLVMGetCallSiteAttributes;
        use std::mem::{ManuallyDrop, MaybeUninit};

        let count = self.count_attributes(loc) as usize;

        // initialize a vector, but leave each element uninitialized
        let mut attribute_refs: Vec<MaybeUninit<Attribute>> = vec![MaybeUninit::uninit(); count];

        // Safety: relies on `Attribute` having the same in-memory representation as `LLVMAttributeRef`
        unsafe {
            LLVMGetCallSiteAttributes(
                self.as_value_ref(),
                loc.get_index(),
                attribute_refs.as_mut_ptr() as *mut _,
            )
        }

        // Safety: all elements are initialized
        unsafe {
            // ensure the vector is not dropped
            let mut attribute_refs = ManuallyDrop::new(attribute_refs);

            Vec::from_raw_parts(
                attribute_refs.as_mut_ptr() as *mut Attribute,
                attribute_refs.len(),
                attribute_refs.capacity(),
            )
        }
    }

    /// Gets an enum `Attribute` on this `CallSiteValue` at an index and kind id.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    ///
    /// assert_eq!(call_site_value.get_enum_attribute(AttributeLoc::Return, 1).unwrap(), enum_attribute);
    /// ```
    // SubTypes: -> Attribute<Enum>
    pub fn get_enum_attribute(self, loc: AttributeLoc, kind_id: u32) -> Option<Attribute> {
        use llvm_sys::core::LLVMGetCallSiteEnumAttribute;

        let ptr = unsafe { LLVMGetCallSiteEnumAttribute(self.as_value_ref(), loc.get_index(), kind_id) };

        if ptr.is_null() {
            return None;
        }

        unsafe { Some(Attribute::new(ptr)) }
    }

    /// Gets a string `Attribute` on this `CallSiteValue` at an index and key.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    ///
    /// assert_eq!(call_site_value.get_string_attribute(AttributeLoc::Return, "my_key").unwrap(), string_attribute);
    /// ```
    // SubTypes: -> Attribute<String>
    pub fn get_string_attribute(self, loc: AttributeLoc, key: &str) -> Option<Attribute> {
        use llvm_sys::core::LLVMGetCallSiteStringAttribute;

        let ptr = unsafe {
            LLVMGetCallSiteStringAttribute(
                self.as_value_ref(),
                loc.get_index(),
                key.as_ptr() as *const ::libc::c_char,
                key.len() as u32,
            )
        };

        if ptr.is_null() {
            return None;
        }

        unsafe { Some(Attribute::new(ptr)) }
    }

    /// Removes an enum `Attribute` on this `CallSiteValue` at an index and kind id.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    /// call_site_value.remove_enum_attribute(AttributeLoc::Return, 1);
    ///
    /// assert_eq!(call_site_value.get_enum_attribute(AttributeLoc::Return, 1), None);
    /// ```
    pub fn remove_enum_attribute(self, loc: AttributeLoc, kind_id: u32) {
        use llvm_sys::core::LLVMRemoveCallSiteEnumAttribute;

        unsafe { LLVMRemoveCallSiteEnumAttribute(self.as_value_ref(), loc.get_index(), kind_id) }
    }

    /// Removes a string `Attribute` on this `CallSiteValue` at an index and key.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.add_attribute(AttributeLoc::Return, string_attribute);
    /// call_site_value.add_attribute(AttributeLoc::Return, enum_attribute);
    /// call_site_value.remove_string_attribute(AttributeLoc::Return, "my_key");
    ///
    /// assert_eq!(call_site_value.get_string_attribute(AttributeLoc::Return, "my_key"), None);
    /// ```
    pub fn remove_string_attribute(self, loc: AttributeLoc, key: &str) {
        use llvm_sys::core::LLVMRemoveCallSiteStringAttribute;

        unsafe {
            LLVMRemoveCallSiteStringAttribute(
                self.as_value_ref(),
                loc.get_index(),
                key.as_ptr() as *const ::libc::c_char,
                key.len() as u32,
            )
        }
    }

    /// Counts the number of arguments this `CallSiteValue` was called with.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let string_attribute = context.create_string_attribute("my_key", "my_val");
    /// let enum_attribute = context.create_enum_attribute(1, 1);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// assert_eq!(call_site_value.count_arguments(), 0);
    /// ```
    pub fn count_arguments(self) -> u32 {
        use llvm_sys::core::LLVMGetNumArgOperands;

        unsafe { LLVMGetNumArgOperands(self.as_value_ref()) }
    }

    /// Gets the calling convention for this `CallSiteValue`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// assert_eq!(call_site_value.get_call_convention(), 0);
    /// ```
    pub fn get_call_convention(self) -> u32 {
        unsafe { LLVMGetInstructionCallConv(self.as_value_ref()) }
    }

    /// Sets the calling convention for this `CallSiteValue`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.set_call_convention(2);
    ///
    /// assert_eq!(call_site_value.get_call_convention(), 2);
    /// ```
    pub fn set_call_convention(self, conv: u32) {
        unsafe { LLVMSetInstructionCallConv(self.as_value_ref(), conv) }
    }

    /// Shortcut for setting the alignment `Attribute` for this `CallSiteValue`.
    ///
    /// # Panics
    ///
    /// When the alignment is not a power of 2.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::attributes::AttributeLoc;
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let builder = context.create_builder();
    /// let module = context.create_module("my_mod");
    /// let void_type = context.void_type();
    /// let fn_type = void_type.fn_type(&[], false);
    /// let fn_value = module.add_function("my_fn", fn_type, None);
    /// let entry_bb = context.append_basic_block(fn_value, "entry");
    ///
    /// builder.position_at_end(entry_bb);
    ///
    /// let call_site_value = builder.build_call(fn_value, &[], "my_fn").unwrap();
    ///
    /// call_site_value.set_alignment_attribute(AttributeLoc::Param(0), 2);
    /// ```
    pub fn set_alignment_attribute(self, loc: AttributeLoc, alignment: u32) {
        assert_eq!(alignment.count_ones(), 1, "Alignment must be a power of two.");

        unsafe { LLVMSetInstrParamAlignment(self.as_value_ref(), loc.get_index(), alignment) }
    }
}

unsafe impl AsValueRef for CallSiteValue<'_> {
    fn as_value_ref(&self) -> LLVMValueRef {
        self.0.value
    }
}

impl Display for CallSiteValue<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.print_to_string())
    }
}