Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a InstructionValue based on its name #416

Merged
merged 4 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,41 @@ impl<'ctx> BasicBlock<'ctx> {
unsafe { Some(InstructionValue::new(value)) }
}

/// Obtains a instruction based on the name
TheDan64 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Example
/// ```rust
/// use inkwell::context::Context;
/// use inkwell::AddressSpace;
///
/// let context = Context::create();
/// let module = context.create_module("ret");
/// let builder = context.create_builder();
///
/// let void_type = context.void_type();
/// let i32_type = context.i32_type();
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::default());
///
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = context.append_basic_block(fn_value, "entry");
/// builder.position_at_end(entry);
///
/// let var = builder.build_alloca(i32_type, "some_number");
/// builder.build_store(var, i32_type.const_int(1 as u64, false));
/// builder.build_return(None);
///
/// let block = fn_value.get_first_basic_block().unwrap();
/// let some_number = block.get_instruction_with_name("some_number");
///
/// assert!(some_number.is_some());
/// assert_eq!( some_number.unwrap().get_name().unwrap().to_str(), Ok("some_number"))
TheDan64 marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn get_instruction_with_name(self, name: impl AsRef<str>) -> Option<InstructionValue<'ctx>> {
let instruction = self.get_first_instruction()?;
instruction.get_instruction_with_name(name)
}

/// Obtains the terminating `InstructionValue` in this `BasicBlock`, if any. A `BasicBlock` must have a terminating instruction to be valid.
///
/// # Example
Expand Down
13 changes: 13 additions & 0 deletions src/values/instruction_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ impl<'ctx> InstructionValue<'ctx> {
}
}

/// Get a instruction with it's name
///
/// Compares against all instructions after self, and self.
pub fn get_instruction_with_name(&self, name:impl AsRef<str>) -> Option<InstructionValue<'ctx>> {
TheDan64 marked this conversation as resolved.
Show resolved Hide resolved
let Some(ins_name) = self.get_name() else {
return self.get_next_instruction()?.get_instruction_with_name(name);
TheDan64 marked this conversation as resolved.
Show resolved Hide resolved
};
if ins_name.to_str() == Ok(name.as_ref()) {
return Some(*self);
}
return self.get_next_instruction()?.get_instruction_with_name(name);
}

/// Set name of the `InstructionValue`.
pub fn set_name(&self, name: &str) -> Result<(), &'static str> {
if self.get_type().is_void_type() {
Expand Down