php-ast 0.21.0

PHP Abstract Syntax Tree (AST) node definitions
Documentation
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
use serde::Serialize;

use crate::Span;

use super::{is_false, ArenaVec, Arg, Attribute, Block, ClassDecl, Param, TypeHint};

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum NameStrInner<'arena, 'src> {
    Src(&'src str),
    Arena(&'arena str),
}

/// A name string from either the source buffer or the bump arena. Use [`as_str`](NameStr::as_str)
/// or `Deref` to get the string value — the allocation origin is an internal parser detail.
///
/// Using this as the payload for both `ExprKind::Variable` and `ExprKind::Identifier`
/// gives them the same binding type, so or-patterns compile natively:
///
/// ```
/// # use php_ast::ast::{ExprKind, NameStr};
/// # fn example<'a, 'b>(kind: &ExprKind<'a, 'b>) {
/// if let ExprKind::Variable(name) | ExprKind::Identifier(name) = kind {
///     let _s: &str = name.as_str();
/// }
/// # }
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct NameStr<'arena, 'src>(NameStrInner<'arena, 'src>);

impl<'arena, 'src> NameStr<'arena, 'src> {
    /// Borrowed directly from the source buffer.
    #[doc(hidden)]
    #[inline]
    pub fn __src(s: &'src str) -> Self {
        Self(NameStrInner::Src(s))
    }

    /// Allocated in the bump arena (e.g. a joined qualified name or a keyword).
    #[doc(hidden)]
    #[inline]
    pub fn __arena(s: &'arena str) -> Self {
        Self(NameStrInner::Arena(s))
    }

    /// Returns the arena slice if this value was arena-allocated, otherwise `None`.
    /// Used internally to avoid re-allocating strings already in the arena.
    #[doc(hidden)]
    #[inline]
    pub fn __into_arena_str(self) -> Option<&'arena str> {
        match self.0 {
            NameStrInner::Arena(s) => Some(s),
            NameStrInner::Src(_) => None,
        }
    }

    /// Returns the source slice if this value was borrowed from the source buffer, otherwise `None`.
    /// Used by the fold infrastructure to preserve source-borrowed strings without re-allocation.
    #[doc(hidden)]
    #[inline]
    pub fn __into_src_str(self) -> Option<&'src str> {
        match self.0 {
            NameStrInner::Src(s) => Some(s),
            NameStrInner::Arena(_) => None,
        }
    }

    /// The string value, regardless of where it is stored.
    #[inline]
    pub fn as_str(&self) -> &str {
        match self.0 {
            NameStrInner::Src(s) | NameStrInner::Arena(s) => s,
        }
    }
}

impl<'arena, 'src> std::ops::Deref for NameStr<'arena, 'src> {
    type Target = str;
    #[inline]
    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl<'arena, 'src> std::fmt::Debug for NameStr<'arena, 'src> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_str().fmt(f)
    }
}

impl<'arena, 'src> serde::Serialize for NameStr<'arena, 'src> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.as_str().serialize(serializer)
    }
}

/// An expression node.
#[derive(Debug, Serialize)]
pub struct Expr<'arena, 'src> {
    /// Expression kind.
    pub kind: ExprKind<'arena, 'src>,
    /// Source range of this node.
    pub span: Span,
}

/// The kinds of expression.
#[derive(Debug, Serialize)]
pub enum ExprKind<'arena, 'src> {
    /// Integer literal
    Int(i64),

    /// Float literal
    Float(f64),

    /// String literal
    String(&'arena str),

    /// Interpolated string: `"Hello $name, you are {$age} years old"`
    InterpolatedString(ArenaVec<'arena, StringPart<'arena, 'src>>),

    /// Heredoc: `<<<EOT ... EOT`
    Heredoc {
        /// Heredoc/nowdoc label.
        label: &'src str,
        /// Literal and interpolated segments.
        parts: ArenaVec<'arena, StringPart<'arena, 'src>>,
    },

    /// Nowdoc: `<<<'EOT' ... EOT`
    Nowdoc {
        /// Heredoc/nowdoc label.
        label: &'src str,
        /// Nowdoc contents.
        value: &'arena str,
    },

    /// Shell execution: `` `command $var` ``
    ShellExec(ArenaVec<'arena, StringPart<'arena, 'src>>),

    /// Boolean literal
    Bool(bool),

    /// Null literal
    Null,

    /// Variable: `$name`
    Variable(NameStr<'arena, 'src>),

    /// Variable variable: `$$var`, `$$$var`, `${expr}`
    VariableVariable(&'arena Expr<'arena, 'src>),

    /// Identifier (bare name, e.g. function name in a call)
    Identifier(NameStr<'arena, 'src>),

    /// Assignment: `$x = expr` or `$x += expr`
    Assign(AssignExpr<'arena, 'src>),

    /// Binary operation: `expr op expr`
    Binary(BinaryExpr<'arena, 'src>),

    /// Unary prefix: `-expr`, `!expr`, `~expr`, `++$x`, `--$x`
    UnaryPrefix(UnaryPrefixExpr<'arena, 'src>),

    /// Unary postfix: `$x++`, `$x--`
    UnaryPostfix(UnaryPostfixExpr<'arena, 'src>),

    /// Ternary: `cond ? then : else` or short `cond ?: else`
    Ternary(TernaryExpr<'arena, 'src>),

    /// Null coalescing: `expr ?? fallback`
    NullCoalesce(NullCoalesceExpr<'arena, 'src>),

    /// Function call: `name(args)`
    FunctionCall(FunctionCallExpr<'arena, 'src>),

    /// Array literal: `[1, 2, 3]` or `['a' => 1]`
    Array(ArenaVec<'arena, ArrayElement<'arena, 'src>>),

    /// Array access: `$arr[index]`
    ArrayAccess(ArrayAccessExpr<'arena, 'src>),

    /// Print expression: `print expr`
    Print(&'arena Expr<'arena, 'src>),

    /// Parenthesized expression: `(expr)`
    Parenthesized(&'arena Expr<'arena, 'src>),

    /// Cast expression: `(int)$x`, `(string)$x`, etc.
    Cast(CastKind, &'arena Expr<'arena, 'src>),

    /// Error suppression: `@expr`
    ErrorSuppress(&'arena Expr<'arena, 'src>),

    /// Isset: `isset($a, $b)`
    Isset(ArenaVec<'arena, Expr<'arena, 'src>>),

    /// Empty: `empty($a)`
    Empty(&'arena Expr<'arena, 'src>),

    /// Include/require: `include 'file.php'`
    Include(IncludeKind, &'arena Expr<'arena, 'src>),

    /// Eval: `eval('code')`
    Eval(&'arena Expr<'arena, 'src>),

    /// Exit/die: `exit`, `exit(1)`, `die('msg')`
    Exit(Option<&'arena Expr<'arena, 'src>>),

    /// Magic constant: `__LINE__`, `__FILE__`, etc.
    MagicConst(MagicConstKind),

    /// Clone: `clone $obj`
    Clone(&'arena Expr<'arena, 'src>),

    /// Clone with property overrides: `clone($obj, ['prop' => $val])` — PHP 8.5+
    CloneWith(&'arena Expr<'arena, 'src>, &'arena Expr<'arena, 'src>),

    /// New: `new Class(args)`
    New(NewExpr<'arena, 'src>),

    /// Property access: `$obj->prop`
    PropertyAccess(PropertyAccessExpr<'arena, 'src>),

    /// Nullsafe property access: `$obj?->prop`
    NullsafePropertyAccess(PropertyAccessExpr<'arena, 'src>),

    /// Method call: `$obj->method(args)`
    MethodCall(&'arena MethodCallExpr<'arena, 'src>),

    /// Nullsafe method call: `$obj?->method(args)`
    NullsafeMethodCall(&'arena MethodCallExpr<'arena, 'src>),

    /// Static property access: `Class::$prop`
    StaticPropertyAccess(StaticAccessExpr<'arena, 'src>),

    /// Static method call: `Class::method(args)`
    StaticMethodCall(&'arena StaticMethodCallExpr<'arena, 'src>),

    /// Dynamic static method call: `Class::$method(args)`
    StaticDynMethodCall(&'arena StaticDynMethodCallExpr<'arena, 'src>),

    /// Class constant access: `Class::CONST`
    ClassConstAccess(StaticAccessExpr<'arena, 'src>),

    /// Dynamic class constant access: `Foo::{expr}`
    ClassConstAccessDynamic {
        /// Class expression.
        class: &'arena Expr<'arena, 'src>,
        /// Member expression.
        member: &'arena Expr<'arena, 'src>,
    },

    /// Dynamic static property access: `A::$$b`, `A::${'b'}`
    StaticPropertyAccessDynamic {
        /// Class expression.
        class: &'arena Expr<'arena, 'src>,
        /// Member expression.
        member: &'arena Expr<'arena, 'src>,
    },

    /// Closure: `function($x) use($y) { }`
    Closure(&'arena ClosureExpr<'arena, 'src>),

    /// Arrow function: `fn($x) => expr`
    ArrowFunction(&'arena ArrowFunctionExpr<'arena, 'src>),

    /// Match: `match(expr) { ... }`
    Match(MatchExpr<'arena, 'src>),

    /// Throw as expression (PHP 8)
    ThrowExpr(&'arena Expr<'arena, 'src>),

    /// Yield: `yield` / `yield $val` / `yield $key => $val`
    Yield(YieldExpr<'arena, 'src>),

    /// Anonymous class: `new class(args) extends Foo implements Bar { ... }`
    AnonymousClass(&'arena ClassDecl<'arena, 'src>),

    /// First-class callable: `strlen(...)`, `$obj->method(...)`, `Foo::bar(...)`
    CallableCreate(CallableCreateExpr<'arena, 'src>),

    /// Omitted element in destructuring: `[$a, , $c]` or `list($a, , $c)`
    Omit,

    /// Error placeholder
    Error,
}

impl<'arena, 'src> Expr<'arena, 'src> {
    /// Returns the name string for `Variable` and `Identifier` nodes, `None` for everything else.
    pub fn name_str(&self) -> Option<&str> {
        match &self.kind {
            ExprKind::Variable(s) | ExprKind::Identifier(s) => Some(s.as_str()),
            _ => None,
        }
    }
}

/// The type of a cast expression.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum CastKind {
    /// `(int)` or `(integer)` cast.
    Int,
    /// `(float)`, `(double)`, or `(real)` cast.
    Float,
    /// `(string)` cast.
    String,
    /// `(bool)` or `(boolean)` cast.
    Bool,
    /// `(array)` cast.
    Array,
    /// `(object)` cast.
    Object,
    /// `(unset)` cast — deprecated; casts to `null`.
    Unset,
    /// `(void)` cast — non-standard; treated as discarding the value.
    Void,
}

/// The include/require keyword used.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum IncludeKind {
    /// `include 'file.php'` — emits a warning if the file is not found.
    Include,
    /// `include_once 'file.php'` — like `include`, but skipped if the file has already been included.
    IncludeOnce,
    /// `require 'file.php'` — fatal error if the file is not found.
    Require,
    /// `require_once 'file.php'` — like `require`, but skipped if the file has already been included.
    RequireOnce,
}

/// The magic constants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum MagicConstKind {
    /// `__CLASS__` — name of the current class, or empty string outside a class.
    Class,
    /// `__DIR__` — directory of the current file.
    Dir,
    /// `__FILE__` — absolute path of the current file.
    File,
    /// `__FUNCTION__` — name of the current function or closure.
    Function,
    /// `__LINE__` — current line number in the source file.
    Line,
    /// `__METHOD__` — name of the current method including its class: `ClassName::methodName`.
    Method,
    /// `__NAMESPACE__` — name of the current namespace, or empty string in the global namespace.
    Namespace,
    /// `__TRAIT__` — name of the current trait, or empty string outside a trait.
    Trait,
    /// `__PROPERTY__` — name of the current property inside a property hook (PHP 8.4+).
    Property,
}

// --- Expression sub-types ---

/// An assignment expression.
#[derive(Debug, Serialize)]
pub struct AssignExpr<'arena, 'src> {
    /// Assignment target.
    pub target: &'arena Expr<'arena, 'src>,
    /// Assignment operator.
    pub op: AssignOp,
    /// Assigned value.
    pub value: &'arena Expr<'arena, 'src>,
    /// `true` for reference assignment `=&`.
    #[serde(skip_serializing_if = "is_false")]
    pub by_ref: bool,
}

/// Assignment operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum AssignOp {
    /// `=`
    Assign,
    /// `+=`
    Plus,
    /// `-=`
    Minus,
    /// `*=`
    Mul,
    /// `/=`
    Div,
    /// `%=`
    Mod,
    /// `**=`
    Pow,
    /// `.=`
    Concat,
    /// `&=`
    BitwiseAnd,
    /// `|=`
    BitwiseOr,
    /// `^=`
    BitwiseXor,
    /// `<<=`
    ShiftLeft,
    /// `>>=`
    ShiftRight,
    /// `??=`
    Coalesce,
}

/// A binary operation.
#[derive(Debug, Serialize)]
pub struct BinaryExpr<'arena, 'src> {
    /// Left operand.
    pub left: &'arena Expr<'arena, 'src>,
    /// Binary operator.
    pub op: BinaryOp,
    /// Right operand.
    pub right: &'arena Expr<'arena, 'src>,
}

/// Binary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum BinaryOp {
    /// `+`
    Add,
    /// `-`
    Sub,
    /// `*`
    Mul,
    /// `/`
    Div,
    /// `%`
    Mod,
    /// `**`
    Pow,
    /// `.` — string concatenation.
    Concat,
    /// `==` — loose equality (type-coercing).
    Equal,
    /// `!=` or `<>` — loose inequality.
    NotEqual,
    /// `===` — strict equality (type and value).
    Identical,
    /// `!==` — strict inequality.
    NotIdentical,
    /// `<`
    Less,
    /// `>`
    Greater,
    /// `<=`
    LessOrEqual,
    /// `>=`
    GreaterOrEqual,
    /// `<=>` — spaceship / three-way comparison; returns -1, 0, or 1.
    Spaceship,
    /// `&&` — short-circuit boolean AND (higher precedence than `and`).
    BooleanAnd,
    /// `||` — short-circuit boolean OR (higher precedence than `or`).
    BooleanOr,
    /// `&` — bitwise AND.
    BitwiseAnd,
    /// `|` — bitwise OR.
    BitwiseOr,
    /// `^` — bitwise XOR.
    BitwiseXor,
    /// `<<` — left bit-shift.
    ShiftLeft,
    /// `>>` — right bit-shift.
    ShiftRight,
    /// `and` — boolean AND (lower precedence than `&&`).
    LogicalAnd,
    /// `or` — boolean OR (lower precedence than `||`).
    LogicalOr,
    /// `xor` — boolean XOR.
    LogicalXor,
    /// `instanceof` — type-check operator; `$x instanceof Foo`.
    Instanceof,
    /// `|>` — pipe operator (PHP 8.5+); passes the left operand as the first argument of the right callable.
    Pipe,
}

/// A prefix unary operation.
#[derive(Debug, Serialize)]
pub struct UnaryPrefixExpr<'arena, 'src> {
    /// Prefix operator.
    pub op: UnaryPrefixOp,
    /// Operand expression.
    pub operand: &'arena Expr<'arena, 'src>,
}

/// Prefix unary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum UnaryPrefixOp {
    /// `-expr` — arithmetic negation.
    Negate,
    /// `+expr` — unary plus (no-op for numbers, promotes to numeric).
    Plus,
    /// `!expr` — boolean NOT.
    BooleanNot,
    /// `~expr` — bitwise NOT.
    BitwiseNot,
    /// `++$x` — pre-increment; increments then returns the new value.
    PreIncrement,
    /// `--$x` — pre-decrement; decrements then returns the new value.
    PreDecrement,
}

/// A postfix unary operation.
#[derive(Debug, Serialize)]
pub struct UnaryPostfixExpr<'arena, 'src> {
    /// Operand expression.
    pub operand: &'arena Expr<'arena, 'src>,
    /// Postfix operator.
    pub op: UnaryPostfixOp,
}

/// Postfix unary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum UnaryPostfixOp {
    /// `$x++` — post-increment; returns the current value then increments.
    PostIncrement,
    /// `$x--` — post-decrement; returns the current value then decrements.
    PostDecrement,
}

/// A ternary `?:` expression.
#[derive(Debug, Serialize)]
pub struct TernaryExpr<'arena, 'src> {
    /// Condition tested.
    pub condition: &'arena Expr<'arena, 'src>,
    /// None for short ternary `$x ?: $y`
    pub then_expr: Option<&'arena Expr<'arena, 'src>>,
    /// Value when the condition is falsy.
    pub else_expr: &'arena Expr<'arena, 'src>,
}

/// A `??` expression.
#[derive(Debug, Serialize)]
pub struct NullCoalesceExpr<'arena, 'src> {
    /// Value tested for null.
    pub left: &'arena Expr<'arena, 'src>,
    /// Fallback when the left side is null or unset.
    pub right: &'arena Expr<'arena, 'src>,
}

/// A function call: `name(args)`.
#[derive(Debug, Serialize)]
pub struct FunctionCallExpr<'arena, 'src> {
    /// Callee expression.
    pub name: &'arena Expr<'arena, 'src>,
    /// Call arguments.
    pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
}

/// An element of an array literal.
#[derive(Debug, Serialize)]
pub struct ArrayElement<'arena, 'src> {
    /// Key expression, if any.
    pub key: Option<Expr<'arena, 'src>>,
    /// Element value.
    pub value: Expr<'arena, 'src>,
    /// `true` for a spread element (`...$x`).
    pub unpack: bool,
    /// `true` for a reference element (`&$x`).
    #[serde(skip_serializing_if = "is_false")]
    pub by_ref: bool,
    /// Source range of this node.
    pub span: Span,
}

/// An array index access: `$a[i]`.
#[derive(Debug, Serialize)]
pub struct ArrayAccessExpr<'arena, 'src> {
    /// Array being indexed.
    pub array: &'arena Expr<'arena, 'src>,
    /// Index; `None` for `$a[]`.
    pub index: Option<&'arena Expr<'arena, 'src>>,
}

// --- OOP Expression sub-types ---

/// An instantiation: `new Foo(args)`.
#[derive(Debug, Serialize)]
pub struct NewExpr<'arena, 'src> {
    /// Class expression being instantiated.
    pub class: &'arena Expr<'arena, 'src>,
    /// Call arguments.
    pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
}

/// A property access: `$obj->prop`.
#[derive(Debug, Serialize)]
pub struct PropertyAccessExpr<'arena, 'src> {
    /// Object expression.
    pub object: &'arena Expr<'arena, 'src>,
    /// Property name expression.
    pub property: &'arena Expr<'arena, 'src>,
}

/// A method call: `$obj->method(args)`.
#[derive(Debug, Serialize)]
pub struct MethodCallExpr<'arena, 'src> {
    /// Object expression.
    pub object: &'arena Expr<'arena, 'src>,
    /// Method name expression.
    pub method: &'arena Expr<'arena, 'src>,
    /// Call arguments.
    pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
}

/// A static member access: `Class::$prop` or `Class::CONST`.
#[derive(Debug, Serialize)]
pub struct StaticAccessExpr<'arena, 'src> {
    /// Class expression or name.
    pub class: &'arena Expr<'arena, 'src>,
    /// Constant or property name expression.
    pub member: &'arena Expr<'arena, 'src>,
}

/// A static method call: `Class::method(args)`.
#[derive(Debug, Serialize)]
pub struct StaticMethodCallExpr<'arena, 'src> {
    /// Class expression or name.
    pub class: &'arena Expr<'arena, 'src>,
    /// Method name expression.
    pub method: &'arena Expr<'arena, 'src>,
    /// Call arguments.
    pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
}

/// A static call with a dynamic method name: `Class::$method(args)`.
#[derive(Debug, Serialize)]
pub struct StaticDynMethodCallExpr<'arena, 'src> {
    /// Class expression or name.
    pub class: &'arena Expr<'arena, 'src>,
    /// Expression yielding the method name.
    pub method: &'arena Expr<'arena, 'src>,
    /// Call arguments.
    pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
}

/// A closure: `function() use ($x) { ... }`.
#[derive(Debug, Serialize)]
pub struct ClosureExpr<'arena, 'src> {
    /// `true` for `static`.
    pub is_static: bool,
    /// `true` when by reference (`&`).
    pub by_ref: bool,
    /// Declared parameters.
    pub params: ArenaVec<'arena, Param<'arena, 'src>>,
    /// Variables captured by `use (...)`.
    pub use_vars: ArenaVec<'arena, ClosureUseVar<'src>>,
    /// Declared return type, if any.
    pub return_type: Option<TypeHint<'arena, 'src>>,
    /// Closure body.
    pub body: &'arena Block<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
}

/// A variable captured in a closure `use` list.
#[derive(Debug, Clone, Serialize)]
pub struct ClosureUseVar<'src> {
    /// Captured variable name without `$`.
    pub name: &'src str,
    /// `true` for `use (&$x)`.
    pub by_ref: bool,
    /// Source range of this node.
    pub span: Span,
}

/// An arrow function: `fn($x) => expr`.
#[derive(Debug, Serialize)]
pub struct ArrowFunctionExpr<'arena, 'src> {
    /// `true` for `static`.
    pub is_static: bool,
    /// `true` when by reference (`&`).
    pub by_ref: bool,
    /// Declared parameters.
    pub params: ArenaVec<'arena, Param<'arena, 'src>>,
    /// Declared return type, if any.
    pub return_type: Option<TypeHint<'arena, 'src>>,
    /// Single result expression.
    pub body: &'arena Expr<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
}

/// A `match` expression.
#[derive(Debug, Serialize)]
pub struct MatchExpr<'arena, 'src> {
    /// Expression being matched.
    pub subject: &'arena Expr<'arena, 'src>,
    /// Match arms in order.
    pub arms: ArenaVec<'arena, MatchArm<'arena, 'src>>,
    /// Start byte offset of the `{` in `match (...) {`.
    #[serde(skip)]
    pub brace_start: u32,
}

/// One arm of a `match`.
#[derive(Debug, Serialize)]
pub struct MatchArm<'arena, 'src> {
    /// None for `default`
    pub conditions: Option<ArenaVec<'arena, Expr<'arena, 'src>>>,
    /// Result expression.
    pub body: Expr<'arena, 'src>,
    /// Source range of this node.
    pub span: Span,
}

/// A `yield` or `yield from` expression.
#[derive(Debug, Serialize)]
pub struct YieldExpr<'arena, 'src> {
    /// Yielded key.
    pub key: Option<&'arena Expr<'arena, 'src>>,
    /// Yielded value.
    pub value: Option<&'arena Expr<'arena, 'src>>,
    /// `true` for `yield from expr` (generator delegation), `false` for plain `yield`
    pub is_from: bool,
}

// --- First-class callable ---

/// A first-class callable expression.
#[derive(Debug, Serialize)]
pub struct CallableCreateExpr<'arena, 'src> {
    /// Kind of callable being created.
    pub kind: CallableCreateKind<'arena, 'src>,
}

/// The callee forms of a first-class callable.
#[derive(Debug, Serialize)]
pub enum CallableCreateKind<'arena, 'src> {
    /// `foo(...)`, `$var(...)`, `\Ns\func(...)`
    Function(&'arena Expr<'arena, 'src>),
    /// `$obj->method(...)`
    Method {
        /// Object expression.
        object: &'arena Expr<'arena, 'src>,
        /// Method name expression.
        method: &'arena Expr<'arena, 'src>,
    },
    /// `$obj?->method(...)`
    NullsafeMethod {
        /// Object expression.
        object: &'arena Expr<'arena, 'src>,
        /// Method name expression.
        method: &'arena Expr<'arena, 'src>,
    },
    /// `Foo::bar(...)`
    StaticMethod {
        /// Class expression.
        class: &'arena Expr<'arena, 'src>,
        /// Method name expression.
        method: &'arena Expr<'arena, 'src>,
    },
}

// --- String interpolation ---

/// A segment of an interpolated string.
#[derive(Debug, Serialize)]
pub enum StringPart<'arena, 'src> {
    /// A plain text segment of an interpolated string or heredoc.
    Literal(&'arena str),
    /// An embedded expression: `$var`, `{$expr}`, or `${var}`.
    Expr(Expr<'arena, 'src>),
}