Skip to content

Commit

Permalink
Expose some ref constructors
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and TheDan64 committed Jan 23, 2024
1 parent 4909eaa commit 3437e60
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ pub struct Module<'ctx> {
}

impl<'ctx> Module<'ctx> {
pub(crate) unsafe fn new(module: LLVMModuleRef) -> Self {
/// Get a module from an [LLVMModuleRef].
///
/// # Safety
///
/// The ref must be valid.
pub unsafe fn new(module: LLVMModuleRef) -> Self {
debug_assert!(!module.is_null());

Module {
Expand Down
7 changes: 6 additions & 1 deletion src/values/array_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ pub struct ArrayValue<'ctx> {
}

impl<'ctx> ArrayValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type array.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

ArrayValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/basic_value_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ use crate::values::{AnyValueEnum, BasicValueEnum};
pub struct BasicValueUse<'ctx>(LLVMUseRef, PhantomData<&'ctx ()>);

impl<'ctx> BasicValueUse<'ctx> {
pub(crate) unsafe fn new(use_: LLVMUseRef) -> Self {
/// Get a value from an [LLVMUseRef].
///
/// # Safety
///
/// The ref must be valid and of type basic value.
pub unsafe fn new(use_: LLVMUseRef) -> Self {
debug_assert!(!use_.is_null());

BasicValueUse(use_, PhantomData)
Expand Down
7 changes: 6 additions & 1 deletion src/values/call_site_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ use super::AnyValue;
pub struct CallSiteValue<'ctx>(Value<'ctx>);

impl<'ctx> CallSiteValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// 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))
}

Expand Down
7 changes: 6 additions & 1 deletion src/values/float_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ pub struct FloatValue<'ctx> {
}

impl<'ctx> FloatValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type float.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

FloatValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/fn_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ pub struct FunctionValue<'ctx> {
}

impl<'ctx> FunctionValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Option<Self> {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type function.
pub unsafe fn new(value: LLVMValueRef) -> Option<Self> {
if value.is_null() {
return None;
}
Expand Down
7 changes: 6 additions & 1 deletion src/values/global_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ pub struct GlobalValue<'ctx> {
}

impl<'ctx> GlobalValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type global.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

GlobalValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/instruction_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ impl<'ctx> InstructionValue<'ctx> {
!unsafe { LLVMIsAAtomicCmpXchgInst(self.as_value_ref()) }.is_null()
}

pub(crate) unsafe fn new(instruction_value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type instruction.
pub unsafe fn new(instruction_value: LLVMValueRef) -> Self {
debug_assert!(!instruction_value.is_null());

let value = Value::new(instruction_value);
Expand Down
7 changes: 6 additions & 1 deletion src/values/int_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ pub struct IntValue<'ctx> {
}

impl<'ctx> IntValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type int.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

IntValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/metadata_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ pub struct MetadataValue<'ctx> {
}

impl<'ctx> MetadataValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type metadata.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());
assert!(!LLVMIsAMDNode(value).is_null() || !LLVMIsAMDString(value).is_null());

Expand Down
7 changes: 6 additions & 1 deletion src/values/phi_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ pub struct PhiValue<'ctx> {
}

impl<'ctx> PhiValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type phi.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

PhiValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/ptr_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ pub struct PointerValue<'ctx> {
}

impl<'ctx> PointerValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type pointer.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

PointerValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/struct_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ pub struct StructValue<'ctx> {
}

impl<'ctx> StructValue<'ctx> {
pub(crate) unsafe fn new(value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type struct.
pub unsafe fn new(value: LLVMValueRef) -> Self {
assert!(!value.is_null());

StructValue {
Expand Down
7 changes: 6 additions & 1 deletion src/values/vec_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ pub struct VectorValue<'ctx> {
}

impl<'ctx> VectorValue<'ctx> {
pub(crate) unsafe fn new(vector_value: LLVMValueRef) -> Self {
/// Get a value from an [LLVMValueRef].
///
/// # Safety
///
/// The ref must be valid and of type vector.
pub unsafe fn new(vector_value: LLVMValueRef) -> Self {
assert!(!vector_value.is_null());

VectorValue {
Expand Down

0 comments on commit 3437e60

Please sign in to comment.