mago_analyzer/plugin/provider/throw.rs
1//! Throw type providers for expressions, functions, and methods.
2//!
3//! These providers allow plugins to specify what exceptions an expression,
4//! function call, or method call can throw.
5
6use ahash::HashSet;
7
8use mago_atom::Atom;
9use mago_syntax::ast::Expression;
10
11use crate::plugin::context::InvocationInfo;
12use crate::plugin::context::ProviderContext;
13use crate::plugin::provider::Provider;
14use crate::plugin::provider::function::FunctionTarget;
15use crate::plugin::provider::method::MethodTarget;
16
17/// Provider for getting thrown exception class names from any expression.
18///
19/// This is the most general form of throw type provider, allowing plugins
20/// to specify exceptions for any expression type (property access, array access, etc.).
21pub trait ExpressionThrowTypeProvider: Provider {
22 /// Get the exception class names that an expression can throw.
23 ///
24 /// Returns a set of fully qualified exception class names.
25 /// Returns an empty set if the expression doesn't throw.
26 fn get_thrown_exceptions(
27 &self,
28 context: &ProviderContext<'_, '_, '_>,
29 expression: &Expression<'_>,
30 ) -> HashSet<Atom>;
31}
32
33/// Provider for getting thrown exception class names from function calls.
34pub trait FunctionThrowTypeProvider: Provider {
35 /// The functions this provider handles.
36 fn targets() -> FunctionTarget
37 where
38 Self: Sized;
39
40 /// Get the exception class names that a function invocation can throw.
41 ///
42 /// Returns a set of fully qualified exception class names.
43 /// Returns an empty set if the function doesn't throw.
44 fn get_thrown_exceptions(
45 &self,
46 context: &ProviderContext<'_, '_, '_>,
47 invocation: &InvocationInfo<'_, '_, '_>,
48 ) -> HashSet<Atom>;
49}
50
51/// Provider for getting thrown exception class names from method calls.
52pub trait MethodThrowTypeProvider: Provider {
53 /// The methods this provider handles.
54 fn targets() -> &'static [MethodTarget]
55 where
56 Self: Sized;
57
58 /// Get the exception class names that a method invocation can throw.
59 ///
60 /// Returns a set of fully qualified exception class names.
61 /// Returns an empty set if the method doesn't throw.
62 fn get_thrown_exceptions(
63 &self,
64 context: &ProviderContext<'_, '_, '_>,
65 class_name: &str,
66 method_name: &str,
67 invocation: &InvocationInfo<'_, '_, '_>,
68 ) -> HashSet<Atom>;
69}