pub struct Module<'ctx> { /* private fields */ }
Expand description

Represents a reference to an LLVM Module. The underlying module will be disposed when dropping this object.

Implementations§

Instantiate a new Module from a raw LLVM module pointer.

Safety

No check is being made to ensure the given pointer points to some valid LLVM module.

Creates a function given its name and ty, adds it to the Module and returns it.

An optional linkage can be specified, without which the default value Linkage::ExternalLinkage will be used.

Example
use inkwell::context::Context;
use inkwell::module::{Module, Linkage};
use inkwell::types::FunctionType;

let context = Context::create();
let module = context.create_module("my_module");

let fn_type = context.f32_type().fn_type(&[], false);
let fn_val = module.add_function("my_function", fn_type, None);

assert_eq!(fn_val.get_name().to_str(), Ok("my_function"));
assert_eq!(fn_val.get_linkage(), Linkage::External);

Gets the Context from which this Module originates.

Example
use inkwell::context::{Context, ContextRef};
use inkwell::module::Module;

let local_context = Context::create();
let local_module = local_context.create_module("my_module");

assert_eq!(local_module.get_context(), local_context);

Gets the first FunctionValue defined in this Module.

Example
use inkwell::context::Context;
use inkwell::module::Module;

let context = Context::create();
let module = context.create_module("my_mod");

assert!(module.get_first_function().is_none());

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);

assert_eq!(fn_value, module.get_first_function().unwrap());

Gets the last FunctionValue defined in this Module.

Example
use inkwell::context::Context;
use inkwell::module::Module;

let context = Context::create();
let module = context.create_module("my_mod");

assert!(module.get_last_function().is_none());

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);

assert_eq!(fn_value, module.get_last_function().unwrap());

Gets a FunctionValue defined in this Module by its name.

Example
use inkwell::context::Context;
use inkwell::module::Module;

let context = Context::create();
let module = context.create_module("my_mod");

assert!(module.get_function("my_fn").is_none());

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);

assert_eq!(fn_value, module.get_function("my_fn").unwrap());

An iterator over the functions in this Module.

use inkwell::context::Context;
use inkwell::module::Module;

let context = Context::create();
let module = context.create_module("my_mod");

assert!(module.get_function("my_fn").is_none());

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 names: Vec<String> = module
    .get_functions()
    .map(|f| f.get_name().to_string_lossy().to_string())
    .collect();

assert_eq!(vec!["my_fn".to_owned()], names);

Gets a named StructType from this Module’s Context.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_module");

assert!(module.get_struct_type("foo").is_none());

let opaque = context.opaque_struct_type("foo");

assert_eq!(module.get_struct_type("foo").unwrap(), opaque);

Assigns a TargetTriple to this Module.

Example
use inkwell::context::Context;
use inkwell::targets::{Target, TargetTriple};

Target::initialize_x86(&Default::default());
let context = Context::create();
let module = context.create_module("mod");
let triple = TargetTriple::create("x86_64-pc-linux-gnu");

assert_eq!(module.get_triple(), TargetTriple::create(""));

module.set_triple(&triple);

assert_eq!(module.get_triple(), triple);

Gets the TargetTriple assigned to this Module. If none has been assigned, the triple will default to “”.

Example
use inkwell::context::Context;
use inkwell::targets::{Target, TargetTriple};

Target::initialize_x86(&Default::default());
let context = Context::create();
let module = context.create_module("mod");
let triple = TargetTriple::create("x86_64-pc-linux-gnu");

assert_eq!(module.get_triple(), TargetTriple::create(""));

module.set_triple(&triple);

assert_eq!(module.get_triple(), triple);

Creates an ExecutionEngine from this Module.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::targets::{InitializationConfig, Target};

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("my_module");
let execution_engine = module.create_execution_engine().unwrap();

assert_eq!(module.get_context(), context);

Creates an interpreter ExecutionEngine from this Module.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::targets::{InitializationConfig, Target};

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("my_module");
let execution_engine = module.create_interpreter_execution_engine().unwrap();

assert_eq!(module.get_context(), context);

Creates a JIT ExecutionEngine from this Module.

Example
use inkwell::OptimizationLevel;
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::targets::{InitializationConfig, Target};

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("my_module");
let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

assert_eq!(module.get_context(), context);

Creates a GlobalValue based on a type in an address space.

Example
use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("mod");
let i8_type = context.i8_type();
let global = module.add_global(i8_type, Some(AddressSpace::from(1u16)), "my_global");

assert_eq!(module.get_first_global().unwrap(), global);
assert_eq!(module.get_last_global().unwrap(), global);

Writes a Module to a Path.

Example
use inkwell::context::Context;

use std::path::Path;

let mut path = Path::new("module.bc");

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);

module.add_function("my_fn", fn_type, None);
module.write_bitcode_to_path(&path);

write_bitcode_to_path should be preferred over this method, as it does not work on all operating systems.

Writes this Module to a MemoryBuffer.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("mod");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let f = module.add_function("f", fn_type, None);
let basic_block = context.append_basic_block(f, "entry");
let builder = context.create_builder();

builder.position_at_end(basic_block);
builder.build_return(None);

let buffer = module.write_bitcode_to_memory();

Ensures that the current Module is valid, and returns a Result that describes whether or not it is, returning a LLVM allocated string on error.

Remarks

See also: http://llvm.org/doxygen/Analysis_2Analysis_8cpp_source.html

Gets a smart pointer to the DataLayout belonging to a particular Module.

Example
use inkwell::OptimizationLevel;
use inkwell::context::Context;
use inkwell::targets::{InitializationConfig, Target};

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("sum");
let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();
let target_data = execution_engine.get_target_data();
let data_layout = target_data.get_data_layout();

module.set_data_layout(&data_layout);

assert_eq!(*module.get_data_layout(), data_layout);

Sets the DataLayout for a particular Module.

Example
use inkwell::OptimizationLevel;
use inkwell::context::Context;
use inkwell::targets::{InitializationConfig, Target};

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("sum");
let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();
let target_data = execution_engine.get_target_data();
let data_layout = target_data.get_data_layout();

module.set_data_layout(&data_layout);

assert_eq!(*module.get_data_layout(), data_layout);

Prints the content of the Module to stderr.

Prints the content of the Module to an LLVMString.

Prints the content of the Module to a file.

Prints the content of the Module to a String.

Sets the inline assembly for the Module.

Appends a MetaDataValue to a global list indexed by a particular key.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_module");
let bool_type = context.bool_type();
let f32_type = context.f32_type();
let bool_val = bool_type.const_int(0, false);
let f32_val = f32_type.const_float(0.0);

assert_eq!(module.get_global_metadata_size("my_md"), 0);

let md_string = context.metadata_string("lots of metadata here");
let md_node = context.metadata_node(&[bool_val.into(), f32_val.into()]);

module.add_global_metadata("my_md", &md_string).unwrap();
module.add_global_metadata("my_md", &md_node).unwrap();

assert_eq!(module.get_global_metadata_size("my_md"), 2);

let global_md = module.get_global_metadata("my_md");

assert_eq!(global_md.len(), 2);

let (md_0, md_1) = (global_md[0].get_node_values(), global_md[1].get_node_values());

assert_eq!(md_0.len(), 1);
assert_eq!(md_1.len(), 2);
assert_eq!(md_0[0].into_metadata_value().get_string_value(), md_string.get_string_value());
assert_eq!(md_1[0].into_int_value(), bool_val);
assert_eq!(md_1[1].into_float_value(), f32_val);

Obtains the number of MetaDataValues indexed by a particular key.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_module");
let bool_type = context.bool_type();
let f32_type = context.f32_type();
let bool_val = bool_type.const_int(0, false);
let f32_val = f32_type.const_float(0.0);

assert_eq!(module.get_global_metadata_size("my_md"), 0);

let md_string = context.metadata_string("lots of metadata here");
let md_node = context.metadata_node(&[bool_val.into(), f32_val.into()]);

module.add_global_metadata("my_md", &md_string).unwrap();
module.add_global_metadata("my_md", &md_node).unwrap();

assert_eq!(module.get_global_metadata_size("my_md"), 2);

let global_md = module.get_global_metadata("my_md");

assert_eq!(global_md.len(), 2);

let (md_0, md_1) = (global_md[0].get_node_values(), global_md[1].get_node_values());

assert_eq!(md_0.len(), 1);
assert_eq!(md_1.len(), 2);
assert_eq!(md_0[0].into_metadata_value().get_string_value(), md_string.get_string_value());
assert_eq!(md_1[0].into_int_value(), bool_val);
assert_eq!(md_1[1].into_float_value(), f32_val);

Obtains the global MetaDataValue node indexed by key, which may contain 1 string or multiple values as its get_node_values()

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_module");
let bool_type = context.bool_type();
let f32_type = context.f32_type();
let bool_val = bool_type.const_int(0, false);
let f32_val = f32_type.const_float(0.0);

assert_eq!(module.get_global_metadata_size("my_md"), 0);

let md_string = context.metadata_string("lots of metadata here");
let md_node = context.metadata_node(&[bool_val.into(), f32_val.into()]);

module.add_global_metadata("my_md", &md_string).unwrap();
module.add_global_metadata("my_md", &md_node).unwrap();

assert_eq!(module.get_global_metadata_size("my_md"), 2);

let global_md = module.get_global_metadata("my_md");

assert_eq!(global_md.len(), 2);

let (md_0, md_1) = (global_md[0].get_node_values(), global_md[1].get_node_values());

assert_eq!(md_0.len(), 1);
assert_eq!(md_1.len(), 2);
assert_eq!(md_0[0].into_metadata_value().get_string_value(), md_string.get_string_value());
assert_eq!(md_1[0].into_int_value(), bool_val);
assert_eq!(md_1[1].into_float_value(), f32_val);

Gets the first GlobalValue in a module.

Example
use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let i8_type = context.i8_type();
let module = context.create_module("mod");

assert!(module.get_first_global().is_none());

let global = module.add_global(i8_type, Some(AddressSpace::from(4u16)), "my_global");

assert_eq!(module.get_first_global().unwrap(), global);

Gets the last GlobalValue in a module.

Example
use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("mod");
let i8_type = context.i8_type();

assert!(module.get_last_global().is_none());

let global = module.add_global(i8_type, Some(AddressSpace::from(4u16)), "my_global");

assert_eq!(module.get_last_global().unwrap(), global);

Gets a named GlobalValue in a module.

Example
use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("mod");
let i8_type = context.i8_type();

assert!(module.get_global("my_global").is_none());

let global = module.add_global(i8_type, Some(AddressSpace::from(4u16)), "my_global");

assert_eq!(module.get_global("my_global").unwrap(), global);

Creates a new Module from a MemoryBuffer with bitcode.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::memory_buffer::MemoryBuffer;
use std::path::Path;

let path = Path::new("foo/bar.bc");
let context = Context::create();
let buffer = MemoryBuffer::create_from_file(&path).unwrap();
let module = Module::parse_bitcode_from_buffer(&buffer, &context);

assert_eq!(module.unwrap().get_context(), context);

A convenience function for creating a Module from a bitcode file for a given context.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use std::path::Path;

let path = Path::new("foo/bar.bc");
let context = Context::create();
let module = Module::parse_bitcode_from_path(&path, &context);

assert_eq!(module.unwrap().get_context(), context);

Gets the name of this Module.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_module");

assert_eq!(module.get_name().to_str(), Ok("my_mdoule"));

Assigns the name of this Module.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_module");

module.set_name("my_module2");

assert_eq!(module.get_name().to_str(), Ok("my_module2"));

Gets the source file name. It defaults to the module identifier but is separate from it.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_mod");

assert_eq!(module.get_source_file_name().to_str(), Ok("my_mod"));

module.set_source_file_name("my_mod.rs");

assert_eq!(module.get_name().to_str(), Ok("my_mod"));
assert_eq!(module.get_source_file_name().to_str(), Ok("my_mod.rs"));

Sets the source file name. It defaults to the module identifier but is separate from it.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("my_mod");

assert_eq!(module.get_source_file_name().to_str(), Ok("my_mod"));

module.set_source_file_name("my_mod.rs");

assert_eq!(module.get_name().to_str(), Ok("my_mod"));
assert_eq!(module.get_source_file_name().to_str(), Ok("my_mod.rs"));

Links one module into another. This will merge two Modules into one.

Example
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("mod");
let module2 = context.create_module("mod2");

assert!(module.link_in_module(module2).is_ok());

Gets the Comdat associated with a particular name. If it does not exist, it will be created. A new Comdat defaults to a kind of ComdatSelectionKind::Any.

Gets the MetadataValue flag associated with the key in this module, if any. If a BasicValue was used to create this flag, it will be wrapped in a MetadataValue when returned from this function.

Append a MetadataValue as a module wide flag. Note that using the same key twice will likely invalidate the module.

Append a BasicValue as a module wide flag. Note that using the same key twice will likely invalidate the module.

Strips and debug info from the module, if it exists.

Gets the version of debug metadata contained in this Module.

Creates a DebugInfoBuilder for this Module.

Construct and run a set of passes over a module. This function takes a string with the passes that should be used. The format of this string is the same as opt’s -passes argument for the new pass manager. Individual passes may be specified, separated by commas. Full pipelines may also be invoked using default and friends. See opt for full reference of the Passes format.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. 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.