1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use ;
/// Trait for global macro expansion functions.
///
/// A global macro expander receives a list of arguments and produces a transformed
/// expression. Global macros are triggered by function calls, not method calls.
///
/// # Type Requirements
///
/// The expander function must be:
/// - `Fn(&mut MacroExprFactory, Vec<Expr>) -> Option<Expr>`: The expansion signature
/// - `Send + Sync`: Thread-safe for use in concurrent environments
/// - `'static`: No borrowed references in the closure
///
/// # Return Value
///
/// - `Some(expr)`: The macro expansion succeeded and `expr` replaces the original call
/// - `None`: The macro cannot be expanded (keeps the original expression)
///
/// # Examples
///
/// ```rust,no_run
/// # use cel_cxx::macros::{GlobalMacroExpander, MacroExprFactory, Expr};
/// # use cel_cxx::Constant;
/// // Simple constant-folding macro
/// fn optimize_add(factory: &mut MacroExprFactory, args: Vec<Expr>) -> Option<Expr> {
/// if args.len() != 2 {
/// return None;
/// }
///
/// // If both args are constant integers, fold them
/// let left = match args[0].kind()?.as_constant()? { Constant::Int(n) => *n, _ => return None };
/// let right = match args[1].kind()?.as_constant()? { Constant::Int(n) => *n, _ => return None };
///
/// Some(factory.new_const(left + right))
/// }
/// ```
/// Trait for receiver macro expansion functions.
///
/// A receiver macro expander receives a target expression and a list of arguments,
/// then produces a transformed expression. Receiver macros are triggered by method
/// calls on a target object.
///
/// # Type Requirements
///
/// The expander function must be:
/// - `Fn(&mut MacroExprFactory, Expr, Vec<Expr>) -> Option<Expr>`: The expansion signature
/// - `Send + Sync`: Thread-safe for use in concurrent environments
/// - `'static`: No borrowed references in the closure
///
/// # Parameters
///
/// - `factory`: Factory for creating new expression nodes
/// - `target`: The receiver expression (the object before the dot)
/// - `args`: The argument list passed to the method
///
/// # Return Value
///
/// - `Some(expr)`: The macro expansion succeeded and `expr` replaces the original call
/// - `None`: The macro cannot be expanded (keeps the original expression)
///
/// # Examples
///
/// ```rust,no_run
/// # use cel_cxx::macros::{ReceiverMacroExpander, MacroExprFactory, Expr};
/// // Macro for optional chaining: target.get_or(default)
/// fn get_or_macro(
/// factory: &mut MacroExprFactory,
/// target: Expr,
/// mut args: Vec<Expr>
/// ) -> Option<Expr> {
/// if args.len() != 1 {
/// return None;
/// }
///
/// let default_value = args.pop()?;
///
/// // Expand to: target != null ? target : default_value
/// let target_copy = factory.copy_expr(&target);
/// Some(factory.new_call("_?_:_", &[
/// factory.new_call("_!=_", &[target_copy, factory.new_const(())]),
/// target,
/// default_value,
/// ]))
/// }
/// ```