@@ -515,7 +515,11 @@ impl<'ctx> BasicBlock<'ctx> {
515
515
}
516
516
}
517
517
518
- /// Gets the address of this `BasicBlock` if possible.
518
+ /// Gets the address of this `BasicBlock` if possible. Returns `None` if `self` is the entry block to a function.
519
+ ///
520
+ /// # Safety
521
+ ///
522
+ /// The returned PointerValue may only be used for `call` and `indirect_branch` instructions
519
523
///
520
524
/// # Example
521
525
///
@@ -526,16 +530,19 @@ impl<'ctx> BasicBlock<'ctx> {
526
530
/// let void_type = context.void_type();
527
531
/// let fn_type = void_type.fn_type(&[], false);
528
532
/// let fn_val = module.add_function("my_fn", fn_type, None);
529
- /// let bb = context.append_basic_block(fn_val, "entry");
533
+ /// let entry_bb = context.append_basic_block(fn_val, "entry");
534
+ /// let next_bb = context.append_basic_block(fn_val, "next");
530
535
///
531
- /// assert!(bb.get_address().is_some());
536
+ /// assert!(unsafe { entry_bb.get_address() }.is_none());
537
+ /// assert!(unsafe { next_bb.get_address() }.is_some());
532
538
/// ```
533
- pub fn get_address ( self ) -> Option < PointerValue < ' ctx > > {
539
+ pub unsafe fn get_address ( self ) -> Option < PointerValue < ' ctx > > {
534
540
let parent = self . get_parent ( ) ?;
535
541
536
- let value = unsafe {
537
- PointerValue :: new ( LLVMBlockAddress ( parent. as_value_ref ( ) , self . basic_block ) )
538
- } ;
542
+ // Taking the address of the entry block is illegal.
543
+ self . get_previous_basic_block ( ) ?;
544
+
545
+ let value = PointerValue :: new ( LLVMBlockAddress ( parent. as_value_ref ( ) , self . basic_block ) ) ;
539
546
540
547
if value. is_null ( ) {
541
548
return None ;
0 commit comments