php-ast 0.26.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
#[cfg(feature = "serde")]
use serde::Serialize;

use crate::Span;

use super::{ArenaVec, Attribute, Block, Comment, Expr, Ident, Name, TypeHint};

/// A named function declaration: `function foo() {}`.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct FunctionDecl<'arena, 'src> {
    /// Function name.
    pub name: Ident<'src>,
    /// Declared parameters.
    pub params: ArenaVec<'arena, Param<'arena, 'src>>,
    /// `true` when the parameter list is missing its `(`, which PHP rejects.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub missing_param_list: bool,
    /// Function body.
    pub body: &'arena Block<'arena, 'src>,
    /// Declared return type, if any.
    pub return_type: Option<TypeHint<'arena, 'src>>,
    /// `true` when by reference (`&`).
    pub by_ref: bool,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// The immediately preceding `/** */` doc-block, if any.
    ///
    /// When present, this comment is **removed** from
    /// `ParseResult::comments` — the
    /// two collections are disjoint. All other comment forms (line, hash,
    /// block) remain in `ParseResult::comments` regardless of position.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> FunctionDecl<'arena, 'src> {
    /// Builds a `FunctionDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(
        arena: &'arena bumpalo::Bump,
        name: Ident<'src>,
        body: &'arena Block<'arena, 'src>,
    ) -> Self {
        Self {
            name,
            params: ArenaVec::new_in(arena),
            missing_param_list: false,
            body,
            return_type: None,
            by_ref: false,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// A function, method, closure or hook parameter.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct Param<'arena, 'src> {
    /// Parameter name without `$`.
    pub name: Ident<'src>,
    /// Declared parameter type.
    pub type_hint: Option<TypeHint<'arena, 'src>>,
    /// Default value expression.
    pub default: Option<Expr<'arena, 'src>>,
    /// `true` when by reference (`&`).
    pub by_ref: bool,
    /// `true` for a variadic parameter (`...$x`).
    pub variadic: bool,
    /// Visibility, `static`, `final`, `abstract` and `readonly` modifiers.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Modifiers::is_empty"))]
    pub modifiers: Modifiers,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Property hooks on a promoted property (PHP 8.4+).
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "ArenaVec::is_empty"))]
    pub hooks: ArenaVec<'arena, PropertyHook<'arena, 'src>>,
    /// `true` for an empty hook list, `{}`, which PHP rejects.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub empty_hook_list: bool,
    /// A doc-block attached directly to this parameter (PHP 8.6 recognizes
    /// this for reflection purposes), either immediately before it or
    /// trailing it up to the comma. Parsed at every version since a bare
    /// comment is legal PHP regardless of target version.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
    /// Source range of this node.
    pub span: Span,
}

impl<'arena, 'src> Param<'arena, 'src> {
    /// A parameter with `name` and `span` set and every other field empty
    /// (`None`, `false`, no attributes or hooks). The fields are public, so set
    /// the rest by assignment; `Param` is `#[non_exhaustive]` and cannot be
    /// built with a struct literal outside this crate.
    pub fn new(arena: &'arena bumpalo::Bump, name: Ident<'src>, span: Span) -> Self {
        Self {
            name,
            type_hint: None,
            default: None,
            by_ref: false,
            variadic: false,
            modifiers: Modifiers::new(),
            attributes: ArenaVec::new_in(arena),
            hooks: ArenaVec::new_in(arena),
            empty_hook_list: false,
            doc_comment: None,
            span,
        }
    }
}

/// The modifiers of a member or promoted parameter. Which flags are valid depends on the node
/// that carries them.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct Modifiers {
    /// Declared visibility, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub visibility: Option<Visibility>,
    /// Asymmetric write visibility, e.g. `private(set)` (PHP 8.4+).
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub set_visibility: Option<Visibility>,
    /// `true` for `static`.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub is_static: bool,
    /// `true` for `abstract`.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub is_abstract: bool,
    /// `true` for `final`.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub is_final: bool,
    /// `true` for `readonly`.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub is_readonly: bool,
    /// `true` for a property declared with `var`, whose `visibility` is `Public`.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub is_var: bool,
}

impl Modifiers {
    /// No modifiers. The fields are public, so set the ones that apply by assignment;
    /// `Modifiers` is `#[non_exhaustive]` and cannot be built with a struct literal outside
    /// this crate.
    pub const fn new() -> Self {
        Self {
            visibility: None,
            set_visibility: None,
            is_static: false,
            is_abstract: false,
            is_final: false,
            is_readonly: false,
            is_var: false,
        }
    }

    /// Whether no modifier is set.
    pub fn is_empty(&self) -> bool {
        *self == Self::new()
    }
}

/// Member visibility modifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum Visibility {
    /// `public` — accessible from anywhere.
    Public,
    /// `protected` — accessible within the class and its subclasses.
    Protected,
    /// `private` — accessible only within the declaring class.
    Private,
}

/// The braced member list of a class-like declaration.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ClassBody<'arena, 'src> {
    /// Class members in source order.
    pub members: ArenaVec<'arena, ClassMember<'arena, 'src>>,
    /// Span covering `{` to `}` of the body.
    #[cfg_attr(feature = "serde", serde(skip))]
    pub span: Span,
}

/// A class declaration, or the body of an anonymous class.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct ClassDecl<'arena, 'src> {
    /// Class name; `None` for anonymous classes.
    pub name: Option<Ident<'src>>,
    /// `abstract`, `final` and `readonly` modifiers.
    pub modifiers: ClassModifiers,
    /// Parent class.
    pub extends: Option<Name<'arena, 'src>>,
    /// Implemented interfaces.
    pub implements: ArenaVec<'arena, Name<'arena, 'src>>,
    /// Class body, flattened on serialization.
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub body: ClassBody<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> ClassDecl<'arena, 'src> {
    /// Builds a `ClassDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(
        arena: &'arena bumpalo::Bump,
        name: Option<Ident<'src>>,
        body: ClassBody<'arena, 'src>,
    ) -> Self {
        Self {
            name,
            modifiers: ClassModifiers::default(),
            extends: None,
            implements: ArenaVec::new_in(arena),
            body,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// Modifiers preceding `class`.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct ClassModifiers {
    /// `true` for `abstract class`.
    pub is_abstract: bool,
    /// `true` for `final class`.
    pub is_final: bool,
    /// `true` for `readonly class`.
    pub is_readonly: bool,
}

/// A member of a class, interface or trait body.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ClassMember<'arena, 'src> {
    /// Kind of member.
    pub kind: ClassMemberKind<'arena, 'src>,
    /// Source range of this node.
    pub span: Span,
}

/// The kinds of class-like member.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum ClassMemberKind<'arena, 'src> {
    /// Property declaration.
    Property(PropertyDecl<'arena, 'src>),
    /// Method declaration.
    Method(MethodDecl<'arena, 'src>),
    /// Class constant declaration.
    ClassConst(ClassConstDecl<'arena, 'src>),
    /// Trait `use` statement.
    TraitUse(TraitUseDecl<'arena, 'src>),
}

/// A property declaration: `public int $x = 0;`.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct PropertyDecl<'arena, 'src> {
    /// Property name without `$`.
    pub name: Ident<'src>,
    /// Visibility, `static`, `final`, `abstract` and `readonly` modifiers.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Modifiers::is_empty"))]
    pub modifiers: Modifiers,
    /// Declared type, if any.
    pub type_hint: Option<TypeHint<'arena, 'src>>,
    /// Default value expression.
    pub default: Option<Expr<'arena, 'src>>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Property hooks (PHP 8.4+).
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "ArenaVec::is_empty"))]
    pub hooks: ArenaVec<'arena, PropertyHook<'arena, 'src>>,
    /// `true` for an empty hook list, `{}`, which PHP rejects.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub empty_hook_list: bool,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> PropertyDecl<'arena, 'src> {
    /// Builds a `PropertyDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(arena: &'arena bumpalo::Bump, name: Ident<'src>) -> Self {
        Self {
            name,
            modifiers: Modifiers::new(),
            type_hint: None,
            default: None,
            attributes: ArenaVec::new_in(arena),
            hooks: ArenaVec::new_in(arena),
            empty_hook_list: false,
            doc_comment: None,
        }
    }
}

/// Which accessor a property hook implements.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum PropertyHookKind {
    /// `get` hook — called when the property is read.
    Get,
    /// `set` hook — called when the property is written; receives the incoming value as `$value`.
    Set,
}

/// The body form of a property hook.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum PropertyHookBody<'arena, 'src> {
    /// `{ stmts }` — a full statement block.
    Block(&'arena Block<'arena, 'src>),
    /// `=> expr` — short-form expression body.
    Expression(Expr<'arena, 'src>),
    /// No body — the hook is declared abstract (on an abstract class or interface).
    Abstract,
}

/// A property hook: `get { ... }` or `set => ...` (PHP 8.4+).
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct PropertyHook<'arena, 'src> {
    /// Whether this is a `get` or `set` hook.
    pub kind: PropertyHookKind,
    /// Hook body.
    pub body: PropertyHookBody<'arena, 'src>,
    /// Visibility, `static`, `final`, `abstract` and `readonly` modifiers.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Modifiers::is_empty"))]
    pub modifiers: Modifiers,
    /// `true` for `&get`.
    pub by_ref: bool,
    /// Explicit parameters, e.g. `set(string $v)`.
    pub params: ArenaVec<'arena, Param<'arena, 'src>>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Source range of this node.
    pub span: Span,
}

impl<'arena, 'src> PropertyHook<'arena, 'src> {
    /// Builds a `PropertyHook` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(
        arena: &'arena bumpalo::Bump,
        kind: PropertyHookKind,
        body: PropertyHookBody<'arena, 'src>,
        span: Span,
    ) -> Self {
        Self {
            kind,
            body,
            modifiers: Modifiers::new(),
            by_ref: false,
            params: ArenaVec::new_in(arena),
            attributes: ArenaVec::new_in(arena),
            span,
        }
    }
}

/// A method declaration inside a class-like body.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct MethodDecl<'arena, 'src> {
    /// Method name.
    pub name: Ident<'src>,
    /// Visibility, `static`, `final`, `abstract` and `readonly` modifiers.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Modifiers::is_empty"))]
    pub modifiers: Modifiers,
    /// `true` when by reference (`&`).
    pub by_ref: bool,
    /// Declared parameters.
    pub params: ArenaVec<'arena, Param<'arena, 'src>>,
    /// `true` when the parameter list is missing its `(`, which PHP rejects.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
    pub missing_param_list: bool,
    /// Declared return type, if any.
    pub return_type: Option<TypeHint<'arena, 'src>>,
    /// `None` for an abstract/interface method.
    pub body: Option<&'arena Block<'arena, 'src>>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> MethodDecl<'arena, 'src> {
    /// A method with `name` set and every other field empty (no modifiers,
    /// parameters, return type, body or attributes). Set the rest by
    /// assignment; see [`Param::new`].
    pub fn new(arena: &'arena bumpalo::Bump, name: Ident<'src>) -> Self {
        Self {
            name,
            modifiers: Modifiers::new(),
            by_ref: false,
            params: ArenaVec::new_in(arena),
            missing_param_list: false,
            return_type: None,
            body: None,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// A class constant: `const X = 1;`.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct ClassConstDecl<'arena, 'src> {
    /// Constant name.
    pub name: Ident<'src>,
    /// Visibility, `static`, `final`, `abstract` and `readonly` modifiers.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Modifiers::is_empty"))]
    pub modifiers: Modifiers,
    /// Typed class constant type (PHP 8.3+).
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub type_hint: Option<&'arena TypeHint<'arena, 'src>>,
    /// Constant value expression.
    pub value: Expr<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> ClassConstDecl<'arena, 'src> {
    /// Builds a `ClassConstDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(arena: &'arena bumpalo::Bump, name: Ident<'src>, value: Expr<'arena, 'src>) -> Self {
        Self {
            name,
            modifiers: Modifiers::new(),
            type_hint: None,
            value,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// A `use` of traits inside a class-like body.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct TraitUseDecl<'arena, 'src> {
    /// Traits being used.
    pub traits: ArenaVec<'arena, Name<'arena, 'src>>,
    /// `insteadof` and `as` rules from the adaptation block.
    pub adaptations: ArenaVec<'arena, TraitAdaptation<'arena, 'src>>,
    /// The `{` that opens the adaptations block; `None` when there are no
    /// adaptations. Printer layout detail, outside semver; not serialized.
    #[cfg_attr(feature = "serde", serde(skip))]
    #[doc(hidden)]
    pub adaptations_brace_span: Option<Span>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> TraitUseDecl<'arena, 'src> {
    /// Builds a `TraitUseDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(arena: &'arena bumpalo::Bump, traits: ArenaVec<'arena, Name<'arena, 'src>>) -> Self {
        Self {
            traits,
            adaptations: ArenaVec::new_in(arena),
            adaptations_brace_span: None,
            doc_comment: None,
        }
    }
}

/// A single rule in a trait `use` adaptation block.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct TraitAdaptation<'arena, 'src> {
    /// Kind of adaptation rule.
    pub kind: TraitAdaptationKind<'arena, 'src>,
    /// Source range of this node.
    pub span: Span,
}

/// The kinds of trait adaptation rule.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum TraitAdaptationKind<'arena, 'src> {
    /// `A::foo insteadof B, C;`
    Precedence {
        /// Trait qualifying the method, if written.
        trait_name: Name<'arena, 'src>,
        /// Method being adapted.
        method: Name<'arena, 'src>,
        /// Traits whose method is excluded.
        insteadof: ArenaVec<'arena, Name<'arena, 'src>>,
    },
    /// `foo as bar;` or `A::foo as protected bar;` or `foo as protected;`
    Alias {
        /// Trait qualifying the method, if written.
        trait_name: Option<Name<'arena, 'src>>,
        /// Method being adapted.
        method: Name<'arena, 'src>,
        /// New visibility, if changed.
        new_modifier: Option<Visibility>,
        /// `true` for `foo as final bar;` (PHP 8.3+).
        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "super::is_false"))]
        is_final: bool,
        /// Alias name, if given.
        new_name: Option<Name<'arena, 'src>>,
    },
}

/// An interface declaration.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct InterfaceDecl<'arena, 'src> {
    /// Interface name.
    pub name: Ident<'src>,
    /// Extended interfaces.
    pub extends: ArenaVec<'arena, Name<'arena, 'src>>,
    /// Interface body, flattened on serialization.
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub body: ClassBody<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> InterfaceDecl<'arena, 'src> {
    /// Builds a `InterfaceDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(
        arena: &'arena bumpalo::Bump,
        name: Ident<'src>,
        body: ClassBody<'arena, 'src>,
    ) -> Self {
        Self {
            name,
            extends: ArenaVec::new_in(arena),
            body,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// A trait declaration.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct TraitDecl<'arena, 'src> {
    /// Trait name.
    pub name: Ident<'src>,
    /// Trait body, flattened on serialization.
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub body: ClassBody<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> TraitDecl<'arena, 'src> {
    /// Builds a `TraitDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(
        arena: &'arena bumpalo::Bump,
        name: Ident<'src>,
        body: ClassBody<'arena, 'src>,
    ) -> Self {
        Self {
            name,
            body,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// The braced member list of an enum.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct EnumBody<'arena, 'src> {
    /// Enum members in source order.
    pub members: ArenaVec<'arena, EnumMember<'arena, 'src>>,
    /// Span covering `{` to `}` of the body.
    #[cfg_attr(feature = "serde", serde(skip))]
    pub span: Span,
}

/// An enum declaration (PHP 8.1+).
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct EnumDecl<'arena, 'src> {
    /// Enum name.
    pub name: Ident<'src>,
    /// Backing type of a backed enum.
    pub scalar_type: Option<Name<'arena, 'src>>,
    /// Implemented interfaces.
    pub implements: ArenaVec<'arena, Name<'arena, 'src>>,
    /// Enum body, flattened on serialization.
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub body: EnumBody<'arena, 'src>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> EnumDecl<'arena, 'src> {
    /// Builds a `EnumDecl` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(
        arena: &'arena bumpalo::Bump,
        name: Ident<'src>,
        body: EnumBody<'arena, 'src>,
    ) -> Self {
        Self {
            name,
            scalar_type: None,
            implements: ArenaVec::new_in(arena),
            body,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}

/// A member of an enum body.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct EnumMember<'arena, 'src> {
    /// Kind of member.
    pub kind: EnumMemberKind<'arena, 'src>,
    /// Source range of this node.
    pub span: Span,
}

/// The kinds of enum member.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum EnumMemberKind<'arena, 'src> {
    /// An enum case: `case Foo;` or `case Foo = 'foo';` (backed enum).
    Case(EnumCase<'arena, 'src>),
    /// A method defined inside the enum body.
    Method(MethodDecl<'arena, 'src>),
    /// A constant defined inside the enum body: `const X = 1;`.
    ClassConst(ClassConstDecl<'arena, 'src>),
    /// A trait use inside the enum body: `use SomeTrait;`.
    TraitUse(TraitUseDecl<'arena, 'src>),
    /// A property, which PHP rejects in enums: `public $x;`.
    Property(PropertyDecl<'arena, 'src>),
}

/// A `case` inside an enum.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct EnumCase<'arena, 'src> {
    /// Case name.
    pub name: Ident<'src>,
    /// Backing value of a backed case.
    pub value: Option<Expr<'arena, 'src>>,
    /// `#[...]` attributes applied to this node.
    pub attributes: ArenaVec<'arena, Attribute<'arena, 'src>>,
    /// Preceding `/** */` doc-block, if any.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub doc_comment: Option<Comment<'src>>,
}

impl<'arena, 'src> EnumCase<'arena, 'src> {
    /// Builds a `EnumCase` from its identifying fields with every other field empty. Set
    /// the rest by assignment, as with `Param::new`.
    pub fn new(arena: &'arena bumpalo::Bump, name: Ident<'src>) -> Self {
        Self {
            name,
            value: None,
            attributes: ArenaVec::new_in(arena),
            doc_comment: None,
        }
    }
}