pub struct OperandExpectation { /* private fields */ }Expand description
A verifier-derived type requirement for one operand-stack value.
The index identifies a value in InstructionInference::stack_before.
It is not an assertion that the observed value has one exact allocation
class: receiver requirements are assignability constraints.
Implementations§
Source§impl OperandExpectation
impl OperandExpectation
Sourcepub const fn stack_index(&self) -> usize
pub const fn stack_index(&self) -> usize
Returns the zero-based index in InstructionInference::stack_before.
Examples found in repository?
examples/infer.rs (line 89)
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub const fn constraint(&self) -> &OperandConstraint
pub const fn constraint(&self) -> &OperandConstraint
Returns the JVM requirement imposed on the value at Self::stack_index.
Examples found in repository?
examples/infer.rs (line 90)
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Trait Implementations§
Source§impl Clone for OperandExpectation
impl Clone for OperandExpectation
Source§impl Debug for OperandExpectation
impl Debug for OperandExpectation
impl Eq for OperandExpectation
Source§impl PartialEq for OperandExpectation
impl PartialEq for OperandExpectation
impl StructuralPartialEq for OperandExpectation
Auto Trait Implementations§
impl Forget for OperandExpectation
impl Freeze for OperandExpectation
impl Move for OperandExpectation
impl RefUnwindSafe for OperandExpectation
impl Send for OperandExpectation
impl Sync for OperandExpectation
impl Unpin for OperandExpectation
impl UnsafeUnpin for OperandExpectation
impl UnwindSafe for OperandExpectation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.