use crate::{
ClassName, Diagnostic, DynamicCallKind, GenericSignature, InferredType, MethodDescriptor,
ReturnType, TypeDescriptor,
};
#[derive(Debug, Clone)]
pub struct ClassInference {
class_name: ClassName,
generic_signature: Option<GenericSignature>,
methods: Vec<MethodInference>,
diagnostics: Vec<Diagnostic>,
}
impl ClassInference {
pub(crate) fn new(
class_name: ClassName,
generic_signature: Option<GenericSignature>,
methods: Vec<MethodInference>,
diagnostics: Vec<Diagnostic>,
) -> Self {
Self {
class_name,
generic_signature,
methods,
diagnostics,
}
}
#[must_use]
pub const fn class_name(&self) -> &ClassName {
&self.class_name
}
#[must_use]
pub const fn generic_signature(&self) -> Option<&GenericSignature> {
self.generic_signature.as_ref()
}
#[must_use]
pub fn methods(&self) -> &[MethodInference] {
&self.methods
}
#[must_use]
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
#[must_use]
pub fn analysis_complete(&self) -> bool {
self.methods.iter().all(MethodInference::analysis_complete)
}
pub(crate) fn mark_batch_analysis_incomplete(&mut self) {
for method in &mut self.methods {
method.mark_analysis_incomplete();
}
self.diagnostics.push(Diagnostic::new(
crate::DiagnosticSeverity::Error,
crate::DiagnosticKind::AnalysisLimitReached,
crate::DiagnosticLocation::class_level(),
"batch method-summary work-item budget was reached",
));
}
}
#[derive(Debug, Clone)]
pub struct ClassInferences {
classes: Vec<ClassInference>,
}
impl ClassInferences {
pub(crate) const fn new(classes: Vec<ClassInference>) -> Self {
Self { classes }
}
#[must_use]
pub fn classes(&self) -> &[ClassInference] {
&self.classes
}
#[must_use]
pub fn get(&self, class_name: &ClassName) -> Option<&ClassInference> {
self.classes
.iter()
.find(|inference| inference.class_name() == class_name)
}
#[must_use]
pub const fn len(&self) -> usize {
self.classes.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.classes.is_empty()
}
#[must_use]
pub fn analysis_complete(&self) -> bool {
self.classes.iter().all(ClassInference::analysis_complete)
}
}
#[derive(Debug, Clone)]
pub struct MethodInference {
name: String,
descriptor: MethodDescriptor,
generic_signature: Option<GenericSignature>,
analysis_complete: bool,
parameter_types: Vec<InferredType>,
return_type: ReturnType,
inferred_return_type: Option<InferredType>,
returned_parameter_index: Option<usize>,
local_types: Vec<InferredType>,
instructions: Vec<InstructionInference>,
}
pub(crate) struct MethodHeader {
pub(crate) descriptor: MethodDescriptor,
pub(crate) generic_signature: Option<GenericSignature>,
pub(crate) analysis_complete: bool,
pub(crate) parameter_types: Vec<InferredType>,
pub(crate) return_type: ReturnType,
pub(crate) inferred_return_type: Option<InferredType>,
pub(crate) returned_parameter_index: Option<usize>,
}
impl MethodInference {
pub(crate) fn new(
name: String,
header: MethodHeader,
local_types: Vec<InferredType>,
instructions: Vec<InstructionInference>,
) -> Self {
Self {
name,
descriptor: header.descriptor,
generic_signature: header.generic_signature,
analysis_complete: header.analysis_complete,
parameter_types: header.parameter_types,
return_type: header.return_type,
inferred_return_type: header.inferred_return_type,
returned_parameter_index: header.returned_parameter_index,
local_types,
instructions,
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub const fn descriptor(&self) -> &MethodDescriptor {
&self.descriptor
}
#[must_use]
pub const fn generic_signature(&self) -> Option<&GenericSignature> {
self.generic_signature.as_ref()
}
#[must_use]
pub const fn analysis_complete(&self) -> bool {
self.analysis_complete
}
#[must_use]
pub fn parameter_types(&self) -> &[InferredType] {
&self.parameter_types
}
#[must_use]
pub const fn return_type(&self) -> &ReturnType {
&self.return_type
}
#[must_use]
pub const fn inferred_return_type(&self) -> Option<&InferredType> {
self.inferred_return_type.as_ref()
}
#[must_use]
pub const fn returned_parameter_index(&self) -> Option<usize> {
self.returned_parameter_index
}
pub(crate) fn mark_analysis_incomplete(&mut self) {
self.analysis_complete = false;
self.inferred_return_type = None;
self.returned_parameter_index = None;
}
#[must_use]
pub fn local_types(&self) -> &[InferredType] {
&self.local_types
}
#[must_use]
pub fn instructions(&self) -> &[InstructionInference] {
&self.instructions
}
}
#[derive(Debug, Clone)]
pub struct InstructionInference {
bytecode_offset: u16,
dynamic_call_kind: Option<DynamicCallKind>,
operand_expectations: Vec<OperandExpectation>,
local_types: std::sync::Arc<[InferredType]>,
stack_before: Vec<InferredType>,
stack_after: Vec<InferredType>,
}
impl InstructionInference {
pub(crate) fn new(
bytecode_offset: u16,
dynamic_call_kind: Option<DynamicCallKind>,
operand_expectations: Vec<OperandExpectation>,
local_types: std::sync::Arc<[InferredType]>,
stack_before: Vec<InferredType>,
stack_after: Vec<InferredType>,
) -> Self {
Self {
bytecode_offset,
dynamic_call_kind,
operand_expectations,
local_types,
stack_before,
stack_after,
}
}
#[must_use]
pub const fn bytecode_offset(&self) -> u16 {
self.bytecode_offset
}
#[must_use]
pub const fn dynamic_call_kind(&self) -> Option<DynamicCallKind> {
self.dynamic_call_kind
}
#[must_use]
pub fn operand_expectations(&self) -> &[OperandExpectation] {
&self.operand_expectations
}
#[must_use]
pub fn local_types(&self) -> &[InferredType] {
&self.local_types
}
#[must_use]
pub fn stack_before(&self) -> &[InferredType] {
&self.stack_before
}
#[must_use]
pub fn stack_after(&self) -> &[InferredType] {
&self.stack_after
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OperandExpectation {
stack_index: usize,
constraint: OperandConstraint,
}
impl OperandExpectation {
pub(crate) const fn new(stack_index: usize, constraint: OperandConstraint) -> Self {
Self {
stack_index,
constraint,
}
}
#[must_use]
pub const fn stack_index(&self) -> usize {
self.stack_index
}
#[must_use]
pub const fn constraint(&self) -> &OperandConstraint {
&self.constraint
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OperandConstraint {
Descriptor(TypeDescriptor),
ReceiverAssignableTo(ClassName),
}