1- use either:: {
2- Either ,
3- Either :: { Left , Right } ,
4- } ;
51use llvm_sys:: core:: { LLVMGetNextUse , LLVMGetUsedValue , LLVMGetUser , LLVMIsABasicBlock , LLVMValueAsBasicBlock } ;
62use llvm_sys:: prelude:: LLVMUseRef ;
73
@@ -10,6 +6,87 @@ use std::marker::PhantomData;
106use crate :: basic_block:: BasicBlock ;
117use crate :: values:: { AnyValueEnum , BasicValueEnum } ;
128
9+ /// Either [BasicValueEnum] or [BasicBlock].
10+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
11+ pub enum Operand < ' ctx > {
12+ Value ( BasicValueEnum < ' ctx > ) ,
13+ Block ( BasicBlock < ' ctx > ) ,
14+ }
15+
16+ impl < ' ctx > Operand < ' ctx > {
17+ /// Determines if the [Operand] is a [BasicValueEnum].
18+ #[ inline]
19+ #[ must_use]
20+ pub fn is_value ( self ) -> bool {
21+ matches ! ( self , Self :: Value ( _) )
22+ }
23+
24+ /// Determines if the [Operand] is a [BasicBlock].
25+ #[ inline]
26+ #[ must_use]
27+ pub fn is_block ( self ) -> bool {
28+ matches ! ( self , Self :: Block ( _) )
29+ }
30+
31+ /// If the [Operand] is a [BasicValueEnum], map it into [Option::Some].
32+ #[ inline]
33+ #[ must_use]
34+ pub fn value ( self ) -> Option < BasicValueEnum < ' ctx > > {
35+ match self {
36+ Self :: Value ( value) => Some ( value) ,
37+ _ => None ,
38+ }
39+ }
40+
41+ /// If the [Operand] is a [BasicBlock], map it into [Option::Some].
42+ #[ inline]
43+ #[ must_use]
44+ pub fn block ( self ) -> Option < BasicBlock < ' ctx > > {
45+ match self {
46+ Self :: Block ( block) => Some ( block) ,
47+ _ => None ,
48+ }
49+ }
50+
51+ /// Expect [BasicValueEnum], panic with the message if it is not.
52+ #[ inline]
53+ #[ must_use]
54+ #[ track_caller]
55+ pub fn expect_value ( self , msg : & str ) -> BasicValueEnum < ' ctx > {
56+ match self {
57+ Self :: Value ( value) => value,
58+ _ => panic ! ( "{msg}" ) ,
59+ }
60+ }
61+
62+ /// Expect [BasicBlock], panic with the message if it is not.
63+ #[ inline]
64+ #[ must_use]
65+ #[ track_caller]
66+ pub fn expect_block ( self , msg : & str ) -> BasicBlock < ' ctx > {
67+ match self {
68+ Self :: Block ( block) => block,
69+ _ => panic ! ( "{msg}" ) ,
70+ }
71+ }
72+
73+ /// Unwrap [BasicValueEnum]. Will panic if it is not.
74+ #[ inline]
75+ #[ must_use]
76+ #[ track_caller]
77+ pub fn unwrap_value ( self ) -> BasicValueEnum < ' ctx > {
78+ self . expect_value ( "Called unwrap_value() on UsedValue::Block." )
79+ }
80+
81+ /// Unwrap [BasicBlock]. Will panic if it is not.
82+ #[ inline]
83+ #[ must_use]
84+ #[ track_caller]
85+ pub fn unwrap_block ( self ) -> BasicBlock < ' ctx > {
86+ self . expect_block ( "Called unwrap_block() on UsedValue::Value." )
87+ }
88+ }
89+
1390/// A usage of a `BasicValue` in another value.
1491#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
1592pub struct BasicValueUse < ' ctx > ( LLVMUseRef , PhantomData < & ' ctx ( ) > ) ;
@@ -160,28 +237,28 @@ impl<'ctx> BasicValueUse<'ctx> {
160237 /// let free_instruction = builder.build_free(arg1).unwrap();
161238 /// let return_instruction = builder.build_return(None).unwrap();
162239 ///
163- /// let free_operand0 = free_instruction.get_operand(0).unwrap().left().unwrap ();
240+ /// let free_operand0 = free_instruction.get_operand(0).unwrap().unwrap_value ();
164241 /// let free_operand0_instruction = free_operand0.as_instruction_value().unwrap();
165242 /// let bitcast_use_value = free_operand0_instruction
166243 /// .get_first_use()
167244 /// .unwrap()
168245 /// .get_used_value()
169- /// .left ()
246+ /// .value ()
170247 /// .unwrap();
171248 ///
172249 /// assert_eq!(bitcast_use_value, free_operand0);
173250 /// ```
174- pub fn get_used_value ( self ) -> Either < BasicValueEnum < ' ctx > , BasicBlock < ' ctx > > {
251+ pub fn get_used_value ( self ) -> Operand < ' ctx > {
175252 let used_value = unsafe { LLVMGetUsedValue ( self . 0 ) } ;
176253
177254 let is_basic_block = unsafe { !LLVMIsABasicBlock ( used_value) . is_null ( ) } ;
178255
179256 if is_basic_block {
180257 let bb = unsafe { BasicBlock :: new ( LLVMValueAsBasicBlock ( used_value) ) } ;
181258
182- Right ( bb. expect ( "BasicBlock should always be valid" ) )
259+ Operand :: Block ( bb. expect ( "BasicBlock should always be valid" ) )
183260 } else {
184- unsafe { Left ( BasicValueEnum :: new ( used_value) ) }
261+ unsafe { Operand :: Value ( BasicValueEnum :: new ( used_value) ) }
185262 }
186263 }
187264}
0 commit comments