1use std::{collections::BTreeSet, sync::Arc};
2
3use crate::ir::parse_and_lower;
4use crate::solver::{analyze_class, analyze_classes};
5use crate::{
6 ClassInference, ClassInferences, Error, FieldSummaryResolver, MethodSummaryResolver,
7 TypeHierarchy,
8};
9
10#[derive(Clone)]
16pub struct InferenceConfig {
17 strict: bool,
18 max_block_iterations: usize,
19 max_work_items: usize,
20 unbounded_analysis: bool,
21 use_local_variable_metadata: bool,
22 hierarchy: Option<Arc<dyn TypeHierarchy>>,
23 method_summaries: Option<Arc<dyn MethodSummaryResolver>>,
24 field_summaries: Option<Arc<dyn FieldSummaryResolver>>,
25}
26
27impl std::fmt::Debug for InferenceConfig {
28 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 formatter
30 .debug_struct("InferenceConfig")
31 .field("strict", &self.strict)
32 .field("max_block_iterations", &self.max_block_iterations)
33 .field("max_work_items", &self.max_work_items)
34 .field("unbounded_analysis", &self.unbounded_analysis)
35 .field(
36 "use_local_variable_metadata",
37 &self.use_local_variable_metadata,
38 )
39 .field("has_type_hierarchy", &self.hierarchy.is_some())
40 .field("has_method_summaries", &self.method_summaries.is_some())
41 .field("has_field_summaries", &self.field_summaries.is_some())
42 .finish()
43 }
44}
45
46impl PartialEq for InferenceConfig {
47 fn eq(&self, other: &Self) -> bool {
48 self.strict == other.strict
49 && self.max_block_iterations == other.max_block_iterations
50 && self.max_work_items == other.max_work_items
51 && self.unbounded_analysis == other.unbounded_analysis
52 && self.use_local_variable_metadata == other.use_local_variable_metadata
53 && match (&self.hierarchy, &other.hierarchy) {
54 (Some(left), Some(right)) => Arc::ptr_eq(left, right),
55 (None, None) => true,
56 _ => false,
57 }
58 && match (&self.method_summaries, &other.method_summaries) {
59 (Some(left), Some(right)) => Arc::ptr_eq(left, right),
60 (None, None) => true,
61 _ => false,
62 }
63 && match (&self.field_summaries, &other.field_summaries) {
64 (Some(left), Some(right)) => Arc::ptr_eq(left, right),
65 (None, None) => true,
66 _ => false,
67 }
68 }
69}
70
71impl Eq for InferenceConfig {}
72
73impl Default for InferenceConfig {
74 fn default() -> Self {
75 Self {
76 strict: false,
77 max_block_iterations: 128,
78 max_work_items: 50_000,
79 unbounded_analysis: true,
80 use_local_variable_metadata: false,
81 hierarchy: None,
82 method_summaries: None,
83 field_summaries: None,
84 }
85 }
86}
87
88impl InferenceConfig {
89 #[must_use]
91 pub const fn strict(&self) -> bool {
92 self.strict
93 }
94
95 #[must_use]
98 pub const fn max_block_iterations(&self) -> usize {
99 self.max_block_iterations
100 }
101
102 #[must_use]
108 pub const fn max_work_items(&self) -> usize {
109 self.max_work_items
110 }
111
112 #[must_use]
114 pub const fn unbounded_analysis(&self) -> bool {
115 self.unbounded_analysis
116 }
117
118 #[must_use]
123 pub const fn uses_local_variable_metadata(&self) -> bool {
124 self.use_local_variable_metadata
125 }
126
127 #[must_use]
129 pub const fn has_type_hierarchy(&self) -> bool {
130 self.hierarchy.is_some()
131 }
132
133 #[must_use]
135 pub const fn has_method_summaries(&self) -> bool {
136 self.method_summaries.is_some()
137 }
138
139 #[must_use]
141 pub const fn has_field_summaries(&self) -> bool {
142 self.field_summaries.is_some()
143 }
144
145 #[must_use]
147 pub const fn with_strict(mut self, strict: bool) -> Self {
148 self.strict = strict;
149 self
150 }
151
152 #[must_use]
156 pub const fn with_max_block_iterations(mut self, max_block_iterations: usize) -> Self {
157 self.max_block_iterations = max_block_iterations;
158 self.unbounded_analysis = false;
159 self
160 }
161
162 #[must_use]
167 pub const fn with_max_work_items(mut self, max_work_items: usize) -> Self {
168 self.max_work_items = max_work_items;
169 self.unbounded_analysis = false;
170 self
171 }
172
173 #[must_use]
180 pub const fn with_unbounded_analysis(mut self) -> Self {
181 self.unbounded_analysis = true;
182 self
183 }
184
185 #[must_use]
192 pub const fn with_local_variable_metadata(mut self, use_local_variable_metadata: bool) -> Self {
193 self.use_local_variable_metadata = use_local_variable_metadata;
194 self
195 }
196
197 #[must_use]
205 pub fn with_shared_type_hierarchy(mut self, hierarchy: Arc<dyn TypeHierarchy>) -> Self {
206 self.hierarchy = Some(hierarchy);
207 self
208 }
209
210 #[must_use]
216 pub fn with_shared_method_summaries(
217 mut self,
218 method_summaries: Arc<dyn MethodSummaryResolver>,
219 ) -> Self {
220 self.method_summaries = Some(method_summaries);
221 self
222 }
223
224 #[must_use]
230 pub fn with_shared_field_summaries(
231 mut self,
232 field_summaries: Arc<dyn FieldSummaryResolver>,
233 ) -> Self {
234 self.field_summaries = Some(field_summaries);
235 self
236 }
237
238 pub(crate) fn type_hierarchy(&self) -> Option<&dyn TypeHierarchy> {
239 self.hierarchy.as_deref()
240 }
241
242 pub(crate) fn method_summaries(&self) -> Option<&dyn MethodSummaryResolver> {
243 self.method_summaries.as_deref()
244 }
245
246 pub(crate) fn field_summaries(&self) -> Option<&dyn FieldSummaryResolver> {
247 self.field_summaries.as_deref()
248 }
249
250 pub(crate) fn validate(&self) -> Result<(), Error> {
251 if self.max_block_iterations == 0 {
252 return Err(Error::InvalidConfiguration {
253 message: "max_block_iterations must be greater than zero".to_owned(),
254 });
255 }
256 if self.max_work_items == 0 {
257 return Err(Error::InvalidConfiguration {
258 message: "max_work_items must be greater than zero".to_owned(),
259 });
260 }
261 Ok(())
262 }
263}
264
265#[derive(Debug, Clone)]
270pub struct Inferer {
271 config: InferenceConfig,
272}
273
274impl Default for Inferer {
275 fn default() -> Self {
276 Self::new(InferenceConfig::default()).expect("default inference configuration is valid")
277 }
278}
279
280impl Inferer {
281 pub fn new(config: InferenceConfig) -> Result<Self, Error> {
286 config.validate()?;
287 Ok(Self { config })
288 }
289
290 #[must_use]
292 pub const fn config(&self) -> &InferenceConfig {
293 &self.config
294 }
295
296 pub fn infer_class(&self, bytes: &[u8]) -> Result<ClassInference, Error> {
302 let class = parse_and_lower(bytes)?;
303 analyze_class(&class, &self.config)
304 }
305
306 pub fn infer_classes<I, B>(&self, class_files: I) -> Result<ClassInferences, Error>
319 where
320 I: IntoIterator<Item = B>,
321 B: AsRef<[u8]>,
322 {
323 let mut names = BTreeSet::new();
324 let mut classes = Vec::new();
325 for bytes in class_files {
326 let class = parse_and_lower(bytes.as_ref())?;
327 if !names.insert(class.name.clone()) {
328 return Err(Error::DuplicateClass {
329 class_name: class.name,
330 });
331 }
332 classes.push(class);
333 }
334
335 analyze_classes(&classes, &self.config).map(ClassInferences::new)
336 }
337}
338
339pub fn infer_class(bytes: &[u8]) -> Result<ClassInference, Error> {
343 Inferer::default().infer_class(bytes)
344}
345
346pub fn infer_classes<I, B>(class_files: I) -> Result<ClassInferences, Error>
351where
352 I: IntoIterator<Item = B>,
353 B: AsRef<[u8]>,
354{
355 Inferer::default().infer_classes(class_files)
356}