pub struct CallableValue<'ctx>(_);
Expand description

A value that can be called with the build_call instruction.

In practice, the F : Into<CallableValue<'ctx>> bound of build_call means it is possible to pass a FunctionValue to build_call directly. It will be implicitly converted into a CallableValue.

use inkwell::context::Context;

// A simple function which calls itself:
let context = Context::create();
let module = context.create_module("ret");
let builder = context.create_builder();
let i32_type = context.i32_type();
let fn_type = i32_type.fn_type(&[i32_type.into()], false);
let fn_value = module.add_function("ret", fn_type, None);
let entry = context.append_basic_block(fn_value, "entry");
let i32_arg = fn_value.get_first_param().unwrap();

builder.position_at_end(entry);

let ret_val = builder.build_call(fn_value, &[i32_arg.into()], "call")
    .try_as_basic_value()
    .left()
    .unwrap();

builder.build_return(Some(&ret_val));

A PointerValue cannot be implicitly converted to a CallableValue because the pointer may point to a non-function value. Instead we can use TryFrom to handle this failure case explicitly.

use std::convert::TryFrom;
use inkwell::context::Context;
use inkwell::values::CallableValue;

// A simple function which calls itself:
let context = Context::create();
let module = context.create_module("ret");
let builder = context.create_builder();
let i32_type = context.i32_type();
let fn_type = i32_type.fn_type(&[i32_type.into()], false);
let fn_value = module.add_function("ret", fn_type, None);
let entry = context.append_basic_block(fn_value, "entry");
let i32_arg = fn_value.get_first_param().unwrap();

builder.position_at_end(entry);

// take a pointer to the function value
let fn_pointer_value = fn_value.as_global_value().as_pointer_value();

// convert that pointer value into a callable value
// explicitly handling the failure case (here with `unwrap`)
let callable_value = CallableValue::try_from(fn_pointer_value).unwrap();

let ret_val = builder.build_call(callable_value, &[i32_arg.into()], "call")
    .try_as_basic_value()
    .left()
    .unwrap();

builder.build_return(Some(&ret_val));

Trait Implementations

Returns an enum containing a typed version of AnyValue.

Prints a value to a LLVMString

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.