Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ impl<'ctx> BasicBlock<'ctx> {
///
/// bb1.replace_all_uses_with(&bb2);
///
/// assert_eq!(branch_inst.get_operand(0).unwrap().right().unwrap(), bb2);
/// assert_eq!(branch_inst.get_operand(0).unwrap().into_basic_block().unwrap(), bb2);
/// ```
pub fn replace_all_uses_with(self, other: &BasicBlock<'ctx>) {
let value = unsafe { LLVMBasicBlockAsValue(self.basic_block) };
Expand Down Expand Up @@ -591,6 +591,12 @@ impl<'ctx> BasicBlock<'ctx> {
}
}

unsafe impl AsValueRef for BasicBlock<'_> {
fn as_value_ref(&self) -> LLVMValueRef {
self.basic_block as LLVMValueRef
}
}

impl fmt::Debug for BasicBlock<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let llvm_value = unsafe { CStr::from_ptr(LLVMPrintValueToString(self.basic_block as LLVMValueRef)) };
Expand Down
70 changes: 61 additions & 9 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ pub struct DIScope<'ctx> {
_marker: PhantomData<&'ctx Context>,
}

impl DIScope<'_> {
impl<'ctx> DIScope<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DIScope` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down Expand Up @@ -1108,7 +1112,11 @@ impl<'ctx> AsDIScope<'ctx> for DIFile<'ctx> {
}
}

impl DIFile<'_> {
impl<'ctx> DIFile<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DIFile` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand All @@ -1128,6 +1136,10 @@ impl<'ctx> DICompileUnit<'ctx> {
self.file
}

pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DICompileUnit` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand All @@ -1150,7 +1162,11 @@ pub struct DINamespace<'ctx> {
_marker: PhantomData<&'ctx Context>,
}

impl DINamespace<'_> {
impl<'ctx> DINamespace<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DINamespace` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down Expand Up @@ -1182,7 +1198,11 @@ impl<'ctx> AsDIScope<'ctx> for DISubprogram<'ctx> {
}
}

impl DISubprogram<'_> {
impl<'ctx> DISubprogram<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DISubprogram` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand All @@ -1196,7 +1216,7 @@ pub struct DIType<'ctx> {
_marker: PhantomData<&'ctx Context>,
}

impl DIType<'_> {
impl<'ctx> DIType<'ctx> {
pub fn get_size_in_bits(&self) -> u64 {
unsafe { LLVMDITypeGetSizeInBits(self.metadata_ref) }
}
Expand All @@ -1209,6 +1229,10 @@ impl DIType<'_> {
unsafe { LLVMDITypeGetOffsetInBits(self.metadata_ref) }
}

pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DIType` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down Expand Up @@ -1240,7 +1264,11 @@ impl<'ctx> DIDerivedType<'ctx> {
}
}

impl DIDerivedType<'_> {
impl<'ctx> DIDerivedType<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
}
Expand Down Expand Up @@ -1270,6 +1298,10 @@ impl<'ctx> DIBasicType<'ctx> {
}
}

pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DIBasicType` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down Expand Up @@ -1299,6 +1331,10 @@ impl<'ctx> DICompositeType<'ctx> {
}
}

pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DICompositeType` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down Expand Up @@ -1337,11 +1373,15 @@ impl<'ctx> AsDIScope<'ctx> for DILexicalBlock<'ctx> {
}
}

impl DILexicalBlock<'_> {
impl<'ctx> DILexicalBlock<'ctx> {
/// Acquires the underlying raw pointer belonging to this `DILexicalBlock` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
}

pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}
}

/// A debug location within the source code. Contains the following information:
Expand Down Expand Up @@ -1374,6 +1414,10 @@ impl<'ctx> DILocation<'ctx> {
}
}

pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DILocation` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand All @@ -1387,7 +1431,11 @@ pub struct DILocalVariable<'ctx> {
_marker: PhantomData<&'ctx Context>,
}

impl DILocalVariable<'_> {
impl<'ctx> DILocalVariable<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'ctx> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DILocalVariable` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down Expand Up @@ -1422,7 +1470,11 @@ pub struct DIExpression<'ctx> {
_marker: PhantomData<&'ctx Context>,
}

impl DIExpression<'_> {
impl<'ctx> DIExpression<'ctx> {
pub fn as_metadata_value(&self, context: impl AsContextRef<'ctx>) -> MetadataValue<'_> {
unsafe { MetadataValue::new(LLVMMetadataAsValue(context.as_ctx_ref(), self.metadata_ref)) }
}

/// Acquires the underlying raw pointer belonging to this `DIExpression` type.
pub fn as_mut_ptr(&self) -> LLVMMetadataRef {
self.metadata_ref
Expand Down
16 changes: 16 additions & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ use crate::support::{to_c_str, LLVMString};
use crate::targets::TargetMachine;
use crate::targets::{CodeModel, InitializationConfig, Target, TargetTriple};
use crate::types::{AsTypeRef, BasicType, FunctionType, StructType};
#[llvm_versions(19..)]
use llvm_sys::core::{LLVMIsNewDbgInfoFormat, LLVMSetIsNewDbgInfoFormat};

use crate::values::BasicValue;
use crate::values::{AsValueRef, FunctionValue, GlobalValue, MetadataValue};
Expand Down Expand Up @@ -1640,6 +1642,20 @@ impl<'ctx> Module<'ctx> {
}
}
}

/// https://llvm.org/docs/RemoveDIsDebugInfo.html
/// Set true to use new debug information format.
#[llvm_versions(19..)]
pub fn set_new_debug_format(&self, value: bool) {
unsafe { LLVMSetIsNewDbgInfoFormat(self.module.get(), value.into()) }
}

/// https://llvm.org/docs/RemoveDIsDebugInfo.html
/// Returns true if this module is set to utilize the new debug format.
#[llvm_versions(19..)]
pub fn is_new_debug_format(&self) -> bool {
unsafe { LLVMIsNewDbgInfoFormat(self.module.get()) != 0 }
}
}

impl Clone for Module<'_> {
Expand Down
2 changes: 1 addition & 1 deletion src/values/basic_value_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl<'ctx> BasicValueUse<'ctx> {
/// let free_instruction = builder.build_free(arg1).unwrap();
/// let return_instruction = builder.build_return(None).unwrap();
///
/// let free_operand0 = free_instruction.get_operand(0).unwrap().left().unwrap();
/// let free_operand0 = free_instruction.get_operand(0).unwrap().into_basic_value().unwrap();
/// let free_operand0_instruction = free_operand0.as_instruction_value().unwrap();
/// let bitcast_use_value = free_operand0_instruction
/// .get_first_use()
Expand Down
9 changes: 6 additions & 3 deletions src/values/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ impl<'ctx> AnyValueEnum<'ctx> {
}
AnyValueEnum::InstructionValue(InstructionValue::new(value))
},
LLVMTypeKind::LLVMMetadataTypeKind => panic!("Metadata values are not supported as AnyValue's."),
LLVMTypeKind::LLVMMetadataTypeKind => {
panic!("Metadata values are not supported as AnyValue's.")
},
_ => panic!("The given type is not supported."),
}
}
Expand Down Expand Up @@ -263,7 +265,8 @@ impl<'ctx> BasicValueEnum<'ctx> {
///
/// The ref must be valid and of supported enum type options ([LLVMTypeKind]).
pub unsafe fn new(value: LLVMValueRef) -> Self {
match LLVMGetTypeKind(LLVMTypeOf(value)) {
let kind = LLVMGetTypeKind(LLVMTypeOf(value));
match kind {
LLVMTypeKind::LLVMFloatTypeKind
| LLVMTypeKind::LLVMFP128TypeKind
| LLVMTypeKind::LLVMDoubleTypeKind
Expand All @@ -290,7 +293,7 @@ impl<'ctx> BasicValueEnum<'ctx> {
LLVMTypeKind::LLVMScalableVectorTypeKind => {
BasicValueEnum::ScalableVectorValue(ScalableVectorValue::new(value))
},
_ => unreachable!("The given type is not a basic type."),
_ => unreachable!("The given type is not a basic type: {:?}", kind),
}
}

Expand Down
Loading
Loading