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
use std::borrow::Cow;

#[cfg(feature = "serde")]
use serde::Serialize;

use crate::Span;

use super::ArenaVec;

/// A bare identifier — the kind that names a function, class, parameter,
/// enum case, etc. Distinct from [`Name`], which represents possibly-qualified
/// names.
///
/// The "error" state — produced during error recovery when no identifier was
/// found in the source — is represented by an empty name, which cannot occur
/// for a real PHP identifier (the lexer rejects empty matches).
///
/// Use [`Ident::new`] / [`Ident::error`] to construct, [`Ident::as_str`] to
/// extract a real name, [`Ident::is_error`] to test for the error state.
/// Serialises as `{"name": .., "span": ..}` for real names and `null` for the
/// error state.
#[derive(Clone, Copy)]
pub struct Ident<'src> {
    name: &'src str,
    /// Source range of the identifier token; includes the `$` of a variable name.
    pub span: Span,
}

impl<'src> Ident<'src> {
    /// Construct an identifier from a non-empty name.
    /// Empty input is rejected in debug builds — use [`Ident::error`] instead.
    #[inline]
    pub fn new(name: &'src str, span: Span) -> Self {
        debug_assert!(!name.is_empty(), "Ident::new() called with empty name");
        Self { name, span }
    }

    /// Placeholder for "no identifier was parsed" at `span`.
    #[inline]
    pub const fn error(span: Span) -> Self {
        Self { name: "", span }
    }

    /// Returns `Some(s)` for a real identifier, `None` for the error state.
    #[inline]
    pub fn as_str(&self) -> Option<&'src str> {
        if self.name.is_empty() {
            None
        } else {
            Some(self.name)
        }
    }

    /// Returns `true` if this identifier was synthesised during error recovery.
    #[inline]
    pub fn is_error(&self) -> bool {
        self.name.is_empty()
    }

    /// Returns the inner string, or `"<error>"` for the error state.
    /// Useful when constructing diagnostic messages.
    #[inline]
    pub fn or_error(&self) -> &'src str {
        if self.name.is_empty() {
            "<error>"
        } else {
            self.name
        }
    }
}

impl<'src> std::fmt::Debug for Ident<'src> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.name.is_empty() {
            write!(f, "Ident::error({:?})", self.span)
        } else {
            write!(f, "Ident({:?}, {:?})", self.name, self.span)
        }
    }
}

impl<'src> std::fmt::Display for Ident<'src> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.or_error())
    }
}

#[cfg(feature = "serde")]
impl<'src> serde::Serialize for Ident<'src> {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        if self.name.is_empty() {
            return s.serialize_none();
        }
        let mut st = s.serialize_struct("Ident", 2)?;
        st.serialize_field("name", self.name)?;
        st.serialize_field("span", &self.span)?;
        st.end()
    }
}

impl<'src> PartialEq<&str> for Ident<'src> {
    fn eq(&self, other: &&str) -> bool {
        !self.name.is_empty() && self.name == *other
    }
}

/// A PHP name (identifier, qualified name, fully-qualified name, or relative name).
///
/// The `Simple` variant is the fast path for the common case (~95%) of single
/// unqualified identifiers like `strlen`, `Foo`, `MyClass`. It avoids allocating
/// an `ArenaVec` entirely.
///
/// The `Complex` variant handles qualified (`Foo\Bar`), fully-qualified (`\Foo\Bar`),
/// and relative (`namespace\Foo`) names.
///
/// The `Error` variant is synthesised during error recovery when the parser
/// expected a name but found none. It carries only a span so consumers can
/// distinguish it from any user-written name.
#[non_exhaustive]
pub enum Name<'arena, 'src> {
    /// Single unqualified identifier — no `ArenaVec` allocation.
    /// `&'src str` instead of `Cow` since this is always a borrowed slice of the source.
    Simple {
        /// The identifier text.
        value: &'src str,
        /// Source range of the name.
        span: Span,
    },
    /// Multi-part or prefixed name (`Foo\Bar`, `\Foo`, `namespace\Foo`).
    Complex {
        /// Name segments split on `\`.
        parts: ArenaVec<'arena, &'src str>,
        /// Qualification form.
        kind: NameKind,
        /// Source range of the name.
        span: Span,
    },
    /// Synthesised during error recovery when no real name could be parsed.
    /// Distinguishable from any user-written name; visitors and tools can
    /// explicitly skip or flag these.
    Error {
        /// Source range where a name was expected.
        span: Span,
    },
}

impl<'arena, 'src> Name<'arena, 'src> {
    /// Source range of the name.
    #[inline]
    pub fn span(&self) -> Span {
        match self {
            Self::Simple { span, .. } | Self::Complex { span, .. } | Self::Error { span } => *span,
        }
    }

    /// Qualification form of the name.
    #[inline]
    pub fn kind(&self) -> NameKind {
        match self {
            Self::Simple { .. } => NameKind::Unqualified,
            Self::Complex { kind, .. } => *kind,
            Self::Error { .. } => NameKind::Error,
        }
    }

    /// Returns the name as a borrowed slice of the source string.
    ///
    /// Unlike `to_string_repr`, this never allocates: it uses the stored
    /// span to slice directly into `src`.  The slice includes any leading `\`
    /// for fully-qualified names, exactly as it appears in the source.
    ///
    /// Use this when you need a zero-copy `&'src str` and already have the
    /// source buffer available (e.g. inside [`crate::visitor::ScopeWalker`]).
    #[inline]
    pub fn src_repr(&self, src: &'src str) -> &'src str {
        match self {
            Self::Simple { value, .. } => value,
            Self::Complex { span, .. } => &src[span.start as usize..span.end as usize],
            Self::Error { .. } => "",
        }
    }

    /// Joins all parts with `\` and prepends `\` if fully qualified.
    /// Returns `Cow::Borrowed` for simple names (zero allocation).
    /// Returns an empty `Cow::Borrowed("")` for `Name::Error`.
    #[inline]
    pub fn to_string_repr(&self) -> Cow<'src, str> {
        match self {
            Self::Complex {
                parts,
                kind: NameKind::FullyQualified,
                ..
            } => Cow::Owned(fully_qualified(parts)),
            _ => self.join_parts(),
        }
    }

    /// Joins all parts with `\` without any leading backslash.
    /// Returns `Cow::Borrowed` for single-part names (zero allocation).
    /// Returns an empty `Cow::Borrowed("")` for `Name::Error`.
    ///
    /// Prefer [`Name::join_parts_in`] when the source is at hand: it also
    /// borrows multi-part names.
    #[inline]
    pub fn join_parts(&self) -> Cow<'src, str> {
        match self {
            Self::Simple { value, .. } => Cow::Borrowed(value),
            Self::Complex { parts, .. } if parts.len() == 1 => Cow::Borrowed(parts[0]),
            Self::Complex { parts, .. } => Cow::Owned(parts.join("\\")),
            Self::Error { .. } => Cow::Borrowed(""),
        }
    }

    /// Same result as [`Name::join_parts`], but borrows from `src` (the source
    /// the name was parsed from) whenever the name is written there without
    /// whitespace or comments, which avoids allocating for multi-part names.
    ///
    /// Falls back to joining for names that are not contiguous in the source,
    /// such as `Foo \ Bar` (valid before PHP 8.0) or `A\B` built from the
    /// group use `use A\{B}`.
    pub fn join_parts_in(&self, src: &'src str) -> Cow<'src, str> {
        let Self::Complex { parts, .. } = self else {
            return self.join_parts();
        };
        if parts.len() == 1 {
            return Cow::Borrowed(parts[0]);
        }
        match contiguous_in(parts, src) {
            Some(joined) => Cow::Borrowed(joined),
            None => Cow::Owned(parts.join("\\")),
        }
    }

    /// Returns the parts as a slice.
    /// For `Simple`, returns a single-element slice of the value.
    #[inline]
    pub fn parts_slice(&self) -> &[&'src str] {
        match self {
            Self::Simple { value, .. } => std::slice::from_ref(value),
            Self::Complex { parts, .. } => parts,
            Self::Error { .. } => &[],
        }
    }
}

/// `parts` joined with `\` behind a leading `\`, in one allocation.
pub(crate) fn fully_qualified<S: AsRef<str>>(parts: &[S]) -> String {
    let mut s =
        String::with_capacity(parts.iter().map(|p| p.as_ref().len() + 1).sum::<usize>() + 1);
    s.push('\\');
    for (i, part) in parts.iter().enumerate() {
        if i > 0 {
            s.push('\\');
        }
        s.push_str(part.as_ref());
    }
    s
}

/// The slice of `src` that reads `parts` joined with `\`, if the parts sit there back to back.
fn contiguous_in<'src>(parts: &[&'src str], src: &'src str) -> Option<&'src str> {
    let base = src.as_ptr() as usize;
    let start = (parts.first()?.as_ptr() as usize).checked_sub(base)?;
    let len = parts.iter().map(|p| p.len()).sum::<usize>() + parts.len() - 1;
    let joined = src.get(start..start.checked_add(len)?)?;
    let bytes = joined.as_bytes();
    let mut offset = 0;
    for (i, part) in parts.iter().enumerate() {
        // Pointer identity, not text equality: a part at this exact offset is these bytes.
        if part.as_ptr() as usize != base + start + offset {
            return None;
        }
        offset += part.len();
        if i + 1 < parts.len() {
            if bytes[offset] != b'\\' {
                return None;
            }
            offset += 1;
        }
    }
    Some(joined)
}

impl<'arena, 'src> std::fmt::Debug for Name<'arena, 'src> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Simple { value, span } => f
                .debug_struct("Name")
                .field("parts", &std::slice::from_ref(value))
                .field("kind", &NameKind::Unqualified)
                .field("span", span)
                .finish(),
            Self::Complex { parts, kind, span } => f
                .debug_struct("Name")
                .field("parts", parts)
                .field("kind", kind)
                .field("span", span)
                .finish(),
            Self::Error { span } => {
                let empty: [&str; 0] = [];
                f.debug_struct("Name")
                    .field("parts", &empty)
                    .field("kind", &NameKind::Error)
                    .field("span", span)
                    .finish()
            }
        }
    }
}

#[cfg(feature = "serde")]
impl<'arena, 'src> serde::Serialize for Name<'arena, 'src> {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut st = s.serialize_struct("Name", 3)?;
        match self {
            Self::Simple { value, span } => {
                st.serialize_field("parts", std::slice::from_ref(value))?;
                st.serialize_field("kind", &NameKind::Unqualified)?;
                st.serialize_field("span", span)?;
            }
            Self::Complex { parts, kind, span } => {
                st.serialize_field("parts", parts)?;
                st.serialize_field("kind", kind)?;
                st.serialize_field("span", span)?;
            }
            Self::Error { span } => {
                let empty: [&str; 0] = [];
                st.serialize_field("parts", &empty[..])?;
                st.serialize_field("kind", &NameKind::Error)?;
                st.serialize_field("span", span)?;
            }
        }
        st.end()
    }
}

/// How a name is qualified.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum NameKind {
    /// A bare identifier with no namespace separator: `Foo`, `strlen`.
    Unqualified,
    /// A name with at least one internal `\` but no leading backslash: `Foo\Bar`.
    Qualified,
    /// A name with a leading `\`: `\Foo\Bar`.
    FullyQualified,
    /// A name starting with the `namespace` keyword: `namespace\Foo`.
    Relative,
    /// Synthesised during error recovery — no real name was present in the source.
    Error,
}

/// PHP built-in type keyword — compact alternative to `Name::Simple` for the
/// 17 reserved type names.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum BuiltinType {
    /// `int` — integer scalar type.
    Int,
    /// `float` — floating-point scalar type.
    Float,
    /// `string` — string scalar type.
    String,
    /// `bool` — boolean scalar type.
    Bool,
    /// `void` — return-only type indicating no value is returned.
    Void,
    /// `never` — return-only type for functions that never return normally (PHP 8.1+).
    Never,
    /// `mixed` — top type; accepts any value.
    Mixed,
    /// `object` — any object instance.
    Object,
    /// `iterable` — `array` or `Traversable` (deprecated in PHP 8.2; use `array|Traversable`).
    Iterable,
    /// `callable` — any callable value.
    Callable,
    /// `array` — any PHP array.
    Array,
    /// `self` — refers to the class in which the type hint appears.
    Self_,
    /// `parent` — refers to the parent class of the class in which the type hint appears.
    Parent_,
    /// `static` — late-static-bound type; the class on which the method was called.
    Static,
    /// `null` — the null type; only valid in union types.
    Null,
    /// `true` — the literal boolean `true` (PHP 8.2+).
    True,
    /// `false` — the literal boolean `false`.
    False,
}

impl BuiltinType {
    /// Returns the canonical lowercase spelling used in PHP and in serialized output.
    #[inline]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Int => "int",
            Self::Float => "float",
            Self::String => "string",
            Self::Bool => "bool",
            Self::Void => "void",
            Self::Never => "never",
            Self::Mixed => "mixed",
            Self::Object => "object",
            Self::Iterable => "iterable",
            Self::Callable => "callable",
            Self::Array => "array",
            Self::Self_ => "self",
            Self::Parent_ => "parent",
            Self::Static => "static",
            Self::Null => "null",
            Self::True => "true",
            Self::False => "false",
        }
    }
}

/// A type declaration with its source range.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct TypeHint<'arena, 'src> {
    /// Type form.
    pub kind: TypeHintKind<'arena, 'src>,
    /// Source range of this node.
    pub span: Span,
}

/// A PHP type hint.
///
/// `Keyword` is the fast path for the 17 built-in type names (`int`, `string`,
/// `bool`, `self`, `array`, etc.). It stores only a 1-byte discriminant and a
/// `Span` instead of a `Name`.
///
/// Serialises identically to `Named` so all existing snapshots remain unchanged.
#[derive(Debug)]
#[non_exhaustive]
pub enum TypeHintKind<'arena, 'src> {
    /// A user-defined or qualified class name: `Foo`, `\Ns\Bar`.
    Named(Name<'arena, 'src>),
    /// Built-in type keyword (`int`, `string`, `bool`, `self`, …) — serialises as `Named` for snapshot compatibility.
    Keyword(BuiltinType, Span),
    /// Nullable type: `?T` — equivalent to `T|null`.
    Nullable(&'arena TypeHint<'arena, 'src>),
    /// Union type: `A|B|C` (PHP 8.0+).
    Union(ArenaVec<'arena, TypeHint<'arena, 'src>>),
    /// Intersection type: `A&B` (PHP 8.1+).
    Intersection(ArenaVec<'arena, TypeHint<'arena, 'src>>),
}

#[cfg(feature = "serde")]
impl<'arena, 'src> serde::Serialize for TypeHintKind<'arena, 'src> {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            // Standard variants — match what #[derive(Serialize)] would produce.
            Self::Named(name) => s.serialize_newtype_variant("TypeHintKind", 0, "Named", name),
            Self::Nullable(inner) => {
                s.serialize_newtype_variant("TypeHintKind", 2, "Nullable", inner)
            }
            Self::Union(types) => s.serialize_newtype_variant("TypeHintKind", 3, "Union", types),
            Self::Intersection(types) => {
                s.serialize_newtype_variant("TypeHintKind", 4, "Intersection", types)
            }
            // Keyword — serialise as if it were Named(Name::Simple { value: kw.as_str(), span }).
            // This preserves all existing snapshot output.
            Self::Keyword(builtin, span) => {
                struct BuiltinNameRepr<'a>(&'a BuiltinType, &'a Span);
                impl serde::Serialize for BuiltinNameRepr<'_> {
                    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                        use serde::ser::SerializeStruct;
                        let mut st = s.serialize_struct("Name", 3)?;
                        st.serialize_field("parts", &[self.0.as_str()])?;
                        st.serialize_field("kind", &NameKind::Unqualified)?;
                        st.serialize_field("span", self.1)?;
                        st.end()
                    }
                }
                s.serialize_newtype_variant(
                    "TypeHintKind",
                    0,
                    "Named",
                    &BuiltinNameRepr(builtin, span),
                )
            }
        }
    }
}