Skip to main content

php_ast/ast/
names.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::Span;
6
7use super::ArenaVec;
8
9/// A bare identifier — the kind that names a function, class, parameter,
10/// enum case, etc. Distinct from [`Name`], which represents possibly-qualified
11/// names.
12///
13/// Memory layout is identical to `&'src str` (16 bytes); `Option<Ident>` is
14/// also 16 bytes via the standard pointer niche. The "error" state — produced
15/// during error recovery when no identifier was found in the source — is
16/// represented by an empty string slice, which cannot occur for a real PHP
17/// identifier (the lexer rejects empty matches).
18///
19/// Use [`Ident::name`] / [`Ident::ERROR`] to construct, [`Ident::as_str`] to
20/// extract a real name, [`Ident::is_error`] to test for the error state.
21/// Serialises as a JSON string for real names and `null` for the error state.
22#[repr(transparent)]
23#[derive(Clone, Copy)]
24pub struct Ident<'src>(&'src str);
25
26impl<'src> Ident<'src> {
27    /// Sentinel for "no identifier was parsed" — same memory layout as a real
28    /// `Ident`, distinguished by the empty-string interior.
29    pub const ERROR: Self = Self("");
30
31    /// Construct an identifier from a non-empty source slice.
32    /// Empty input is rejected in debug builds — use [`Ident::ERROR`] instead.
33    #[inline]
34    pub fn name(s: &'src str) -> Self {
35        debug_assert!(!s.is_empty(), "Ident::name() called with empty string");
36        Self(s)
37    }
38
39    /// Returns `Some(s)` for a real identifier, `None` for the error state.
40    #[inline]
41    pub fn as_str(&self) -> Option<&'src str> {
42        if self.0.is_empty() {
43            None
44        } else {
45            Some(self.0)
46        }
47    }
48
49    /// Returns `true` if this identifier was synthesised during error recovery.
50    #[inline]
51    pub fn is_error(&self) -> bool {
52        self.0.is_empty()
53    }
54
55    /// Returns the inner string, or `"<error>"` for the error state.
56    /// Useful when constructing diagnostic messages.
57    #[inline]
58    pub fn or_error(&self) -> &'src str {
59        if self.0.is_empty() {
60            "<error>"
61        } else {
62            self.0
63        }
64    }
65}
66
67impl<'src> std::fmt::Debug for Ident<'src> {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        if self.0.is_empty() {
70            f.write_str("Ident::ERROR")
71        } else {
72            f.debug_tuple("Ident").field(&self.0).finish()
73        }
74    }
75}
76
77impl<'src> std::fmt::Display for Ident<'src> {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        f.write_str(self.or_error())
80    }
81}
82
83impl<'src> serde::Serialize for Ident<'src> {
84    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
85        if self.0.is_empty() {
86            s.serialize_none()
87        } else {
88            s.serialize_str(self.0)
89        }
90    }
91}
92
93impl<'src> PartialEq<&str> for Ident<'src> {
94    fn eq(&self, other: &&str) -> bool {
95        !self.0.is_empty() && self.0 == *other
96    }
97}
98
99#[cfg(test)]
100mod ident_layout_tests {
101    use super::Ident;
102
103    /// `Ident` is `#[repr(transparent)]` over `&str`; this test guards the
104    /// invariant so the size never accidentally regresses.
105    #[test]
106    fn ident_has_same_size_as_str_slice() {
107        assert_eq!(std::mem::size_of::<Ident>(), std::mem::size_of::<&str>());
108        assert_eq!(
109            std::mem::size_of::<Option<Ident>>(),
110            std::mem::size_of::<Option<&str>>()
111        );
112    }
113}
114
115/// A PHP name (identifier, qualified name, fully-qualified name, or relative name).
116///
117/// The `Simple` variant is the fast path for the common case (~95%) of single
118/// unqualified identifiers like `strlen`, `Foo`, `MyClass`. It avoids allocating
119/// an `ArenaVec` entirely.
120///
121/// The `Complex` variant handles qualified (`Foo\Bar`), fully-qualified (`\Foo\Bar`),
122/// and relative (`namespace\Foo`) names.
123///
124/// The `Error` variant is synthesised during error recovery when the parser
125/// expected a name but found none. It carries only a span so consumers can
126/// distinguish it from any user-written name.
127pub enum Name<'arena, 'src> {
128    /// Single unqualified identifier — no `ArenaVec` allocation.
129    /// `&'src str` instead of `Cow` since this is always a borrowed slice of the source.
130    Simple { value: &'src str, span: Span },
131    /// Multi-part or prefixed name (`Foo\Bar`, `\Foo`, `namespace\Foo`).
132    Complex {
133        parts: ArenaVec<'arena, &'src str>,
134        kind: NameKind,
135        span: Span,
136    },
137    /// Synthesised during error recovery when no real name could be parsed.
138    /// Distinguishable from any user-written name; visitors and tools can
139    /// explicitly skip or flag these.
140    Error { span: Span },
141}
142
143impl<'arena, 'src> Name<'arena, 'src> {
144    #[inline]
145    pub fn span(&self) -> Span {
146        match self {
147            Self::Simple { span, .. } | Self::Complex { span, .. } | Self::Error { span } => *span,
148        }
149    }
150
151    #[inline]
152    pub fn kind(&self) -> NameKind {
153        match self {
154            Self::Simple { .. } => NameKind::Unqualified,
155            Self::Complex { kind, .. } => *kind,
156            Self::Error { .. } => NameKind::Error,
157        }
158    }
159
160    /// Returns the name as a borrowed slice of the source string.
161    ///
162    /// Unlike [`to_string_repr`], this never allocates: it uses the stored
163    /// span to slice directly into `src`.  The slice includes any leading `\`
164    /// for fully-qualified names, exactly as it appears in the source.
165    ///
166    /// Use this when you need a zero-copy `&'src str` and already have the
167    /// source buffer available (e.g. inside [`crate::visitor::ScopeWalker`]).
168    #[inline]
169    pub fn src_repr(&self, src: &'src str) -> &'src str {
170        match self {
171            Self::Simple { value, .. } => value,
172            Self::Complex { span, .. } => &src[span.start as usize..span.end as usize],
173            Self::Error { .. } => "",
174        }
175    }
176
177    /// Joins all parts with `\` and prepends `\` if fully qualified.
178    /// Returns `Cow::Borrowed` for simple names (zero allocation).
179    /// Returns an empty `Cow::Borrowed("")` for `Name::Error`.
180    #[inline]
181    pub fn to_string_repr(&self) -> Cow<'src, str> {
182        match self {
183            Self::Simple { value, .. } => Cow::Borrowed(value),
184            Self::Complex { parts, kind, .. } => {
185                let joined = parts.join("\\");
186                if *kind == NameKind::FullyQualified {
187                    Cow::Owned(format!("\\{}", joined))
188                } else {
189                    Cow::Owned(joined)
190                }
191            }
192            Self::Error { .. } => Cow::Borrowed(""),
193        }
194    }
195
196    /// Joins all parts with `\` without any leading backslash.
197    /// Returns `Cow::Borrowed` for simple names (zero allocation).
198    /// Returns an empty `Cow::Borrowed("")` for `Name::Error`.
199    #[inline]
200    pub fn join_parts(&self) -> Cow<'src, str> {
201        match self {
202            Self::Simple { value, .. } => Cow::Borrowed(value),
203            Self::Complex { parts, .. } => Cow::Owned(parts.join("\\")),
204            Self::Error { .. } => Cow::Borrowed(""),
205        }
206    }
207
208    /// Returns the parts as a slice.
209    /// For `Simple`, returns a single-element slice of the value.
210    #[inline]
211    pub fn parts_slice(&self) -> &[&'src str] {
212        match self {
213            Self::Simple { value, .. } => std::slice::from_ref(value),
214            Self::Complex { parts, .. } => parts,
215            Self::Error { .. } => &[],
216        }
217    }
218}
219
220impl<'arena, 'src> std::fmt::Debug for Name<'arena, 'src> {
221    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
222        match self {
223            Self::Simple { value, span } => f
224                .debug_struct("Name")
225                .field("parts", &std::slice::from_ref(value))
226                .field("kind", &NameKind::Unqualified)
227                .field("span", span)
228                .finish(),
229            Self::Complex { parts, kind, span } => f
230                .debug_struct("Name")
231                .field("parts", parts)
232                .field("kind", kind)
233                .field("span", span)
234                .finish(),
235            Self::Error { span } => {
236                let empty: [&str; 0] = [];
237                f.debug_struct("Name")
238                    .field("parts", &empty)
239                    .field("kind", &NameKind::Error)
240                    .field("span", span)
241                    .finish()
242            }
243        }
244    }
245}
246
247impl<'arena, 'src> serde::Serialize for Name<'arena, 'src> {
248    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
249        use serde::ser::SerializeStruct;
250        let mut st = s.serialize_struct("Name", 3)?;
251        match self {
252            Self::Simple { value, span } => {
253                st.serialize_field("parts", std::slice::from_ref(value))?;
254                st.serialize_field("kind", &NameKind::Unqualified)?;
255                st.serialize_field("span", span)?;
256            }
257            Self::Complex { parts, kind, span } => {
258                st.serialize_field("parts", parts)?;
259                st.serialize_field("kind", kind)?;
260                st.serialize_field("span", span)?;
261            }
262            Self::Error { span } => {
263                let empty: [&str; 0] = [];
264                st.serialize_field("parts", &empty[..])?;
265                st.serialize_field("kind", &NameKind::Error)?;
266                st.serialize_field("span", span)?;
267            }
268        }
269        st.end()
270    }
271}
272
273#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
274pub enum NameKind {
275    /// A bare identifier with no namespace separator: `Foo`, `strlen`.
276    Unqualified,
277    /// A name with at least one internal `\` but no leading backslash: `Foo\Bar`.
278    Qualified,
279    /// A name with a leading `\`: `\Foo\Bar`.
280    FullyQualified,
281    /// A name starting with the `namespace` keyword: `namespace\Foo`.
282    Relative,
283    /// Synthesised during error recovery — no real name was present in the source.
284    Error,
285}
286
287/// PHP built-in type keyword — zero-cost alternative to `Name::Simple` for the
288/// 20 reserved type names. One byte instead of a `Cow<str>` + `Span` in the AST.
289#[repr(u8)]
290#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
291pub enum BuiltinType {
292    /// `int` — integer scalar type.
293    Int,
294    /// `integer` — alias for `int`, accepted in type casts.
295    Integer,
296    /// `float` — floating-point scalar type.
297    Float,
298    /// `double` — alias for `float`, accepted in type casts.
299    Double,
300    /// `string` — string scalar type.
301    String,
302    /// `bool` — boolean scalar type.
303    Bool,
304    /// `boolean` — alias for `bool`, accepted in type casts.
305    Boolean,
306    /// `void` — return-only type indicating no value is returned.
307    Void,
308    /// `never` — return-only type for functions that never return normally (PHP 8.1+).
309    Never,
310    /// `mixed` — top type; accepts any value.
311    Mixed,
312    /// `object` — any object instance.
313    Object,
314    /// `iterable` — `array` or `Traversable` (deprecated in PHP 8.2; use `array|Traversable`).
315    Iterable,
316    /// `callable` — any callable value.
317    Callable,
318    /// `array` — any PHP array.
319    Array,
320    /// `self` — refers to the class in which the type hint appears.
321    Self_,
322    /// `parent` — refers to the parent class of the class in which the type hint appears.
323    Parent_,
324    /// `static` — late-static-bound type; the class on which the method was called.
325    Static,
326    /// `null` — the null type; only valid in union types.
327    Null,
328    /// `true` — the literal boolean `true` (PHP 8.2+).
329    True,
330    /// `false` — the literal boolean `false`.
331    False,
332}
333
334impl BuiltinType {
335    /// Returns the canonical lowercase spelling used in PHP and in serialized output.
336    #[inline]
337    pub fn as_str(self) -> &'static str {
338        match self {
339            Self::Int => "int",
340            Self::Integer => "integer",
341            Self::Float => "float",
342            Self::Double => "double",
343            Self::String => "string",
344            Self::Bool => "bool",
345            Self::Boolean => "boolean",
346            Self::Void => "void",
347            Self::Never => "never",
348            Self::Mixed => "mixed",
349            Self::Object => "object",
350            Self::Iterable => "iterable",
351            Self::Callable => "callable",
352            Self::Array => "array",
353            Self::Self_ => "self",
354            Self::Parent_ => "parent",
355            Self::Static => "static",
356            Self::Null => "null",
357            Self::True => "true",
358            Self::False => "false",
359        }
360    }
361}
362
363#[derive(Debug, Serialize)]
364pub struct TypeHint<'arena, 'src> {
365    pub kind: TypeHintKind<'arena, 'src>,
366    pub span: Span,
367}
368
369/// A PHP type hint.
370///
371/// `Keyword` is the fast path for the 20 built-in type names (`int`, `string`,
372/// `bool`, `self`, `array`, etc.). It stores only a 1-byte discriminant and a
373/// `Span`, avoiding the `Cow<str>` that `Named(Name::Simple)` would require.
374///
375/// Serialises identically to `Named` so all existing snapshots remain unchanged.
376#[derive(Debug)]
377pub enum TypeHintKind<'arena, 'src> {
378    /// A user-defined or qualified class name: `Foo`, `\Ns\Bar`.
379    Named(Name<'arena, 'src>),
380    /// Built-in type keyword (`int`, `string`, `bool`, `self`, …) — serialises as `Named` for snapshot compatibility.
381    Keyword(BuiltinType, Span),
382    /// Nullable type: `?T` — equivalent to `T|null`.
383    Nullable(&'arena TypeHint<'arena, 'src>),
384    /// Union type: `A|B|C` (PHP 8.0+).
385    Union(ArenaVec<'arena, TypeHint<'arena, 'src>>),
386    /// Intersection type: `A&B` (PHP 8.1+).
387    Intersection(ArenaVec<'arena, TypeHint<'arena, 'src>>),
388}
389
390impl<'arena, 'src> serde::Serialize for TypeHintKind<'arena, 'src> {
391    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
392        match self {
393            // Standard variants — match what #[derive(Serialize)] would produce.
394            Self::Named(name) => s.serialize_newtype_variant("TypeHintKind", 0, "Named", name),
395            Self::Nullable(inner) => {
396                s.serialize_newtype_variant("TypeHintKind", 2, "Nullable", inner)
397            }
398            Self::Union(types) => s.serialize_newtype_variant("TypeHintKind", 3, "Union", types),
399            Self::Intersection(types) => {
400                s.serialize_newtype_variant("TypeHintKind", 4, "Intersection", types)
401            }
402            // Keyword — serialise as if it were Named(Name::Simple { value: kw.as_str(), span }).
403            // This preserves all existing snapshot output.
404            Self::Keyword(builtin, span) => {
405                struct BuiltinNameRepr<'a>(&'a BuiltinType, &'a Span);
406                impl serde::Serialize for BuiltinNameRepr<'_> {
407                    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
408                        use serde::ser::SerializeStruct;
409                        let mut st = s.serialize_struct("Name", 3)?;
410                        st.serialize_field("parts", &[self.0.as_str()])?;
411                        st.serialize_field("kind", &NameKind::Unqualified)?;
412                        st.serialize_field("span", self.1)?;
413                        st.end()
414                    }
415                }
416                s.serialize_newtype_variant(
417                    "TypeHintKind",
418                    0,
419                    "Named",
420                    &BuiltinNameRepr(builtin, span),
421                )
422            }
423        }
424    }
425}