Skip to main content

mir_codebase/
storage.rs

1use std::sync::Arc;
2
3use indexmap::IndexMap;
4use mir_types::{Location, Name, Type};
5use rustc_hash::FxHashMap;
6use serde::{Deserialize, Serialize};
7
8// ---------------------------------------------------------------------------
9// Interned common types for deduplication
10// ---------------------------------------------------------------------------
11
12/// Interned Type types for common parameter/property types.
13/// Deduplicates allocations when thousands of parameters share types like `string`, `int`, etc.
14mod interned_types {
15    use super::*;
16    use std::sync::OnceLock;
17
18    fn intern_string() -> Arc<Type> {
19        Arc::new(Type::string())
20    }
21
22    fn intern_int() -> Arc<Type> {
23        Arc::new(Type::int())
24    }
25
26    fn intern_float() -> Arc<Type> {
27        Arc::new(Type::float())
28    }
29
30    fn intern_bool() -> Arc<Type> {
31        Arc::new(Type::bool())
32    }
33
34    fn intern_mixed() -> Arc<Type> {
35        Arc::new(Type::mixed())
36    }
37
38    fn intern_null() -> Arc<Type> {
39        Arc::new(Type::null())
40    }
41
42    fn intern_void() -> Arc<Type> {
43        Arc::new(Type::void())
44    }
45
46    static STRING: OnceLock<Arc<Type>> = OnceLock::new();
47    static INT: OnceLock<Arc<Type>> = OnceLock::new();
48    static FLOAT: OnceLock<Arc<Type>> = OnceLock::new();
49    static BOOL: OnceLock<Arc<Type>> = OnceLock::new();
50    static MIXED: OnceLock<Arc<Type>> = OnceLock::new();
51    static NULL: OnceLock<Arc<Type>> = OnceLock::new();
52    static VOID: OnceLock<Arc<Type>> = OnceLock::new();
53
54    pub fn string() -> Arc<Type> {
55        STRING.get_or_init(intern_string).clone()
56    }
57
58    pub fn int() -> Arc<Type> {
59        INT.get_or_init(intern_int).clone()
60    }
61
62    pub fn float() -> Arc<Type> {
63        FLOAT.get_or_init(intern_float).clone()
64    }
65
66    pub fn bool() -> Arc<Type> {
67        BOOL.get_or_init(intern_bool).clone()
68    }
69
70    pub fn mixed() -> Arc<Type> {
71        MIXED.get_or_init(intern_mixed).clone()
72    }
73
74    pub fn null() -> Arc<Type> {
75        NULL.get_or_init(intern_null).clone()
76    }
77
78    pub fn void() -> Arc<Type> {
79        VOID.get_or_init(intern_void).clone()
80    }
81
82    /// Global content-keyed `Arc<Type>` interner. Any structurally-identical
83    /// Type is shared as a single Arc across the session.
84    ///
85    /// Why: PHP codebases re-declare a small set of type shapes thousands of
86    /// times — `string|null` return types, `int` params, `array<string, mixed>`
87    /// property types. Without interning, each declaration allocates its own
88    /// `Arc<Type>` plus the inline `SmallVec<[Atomic; 2]>` and any boxed
89    /// `Atomic` payloads. With interning, only the first occurrence allocates.
90    ///
91    /// Trade-off: every `intern_or_wrap` call hashes + does one DashMap lookup.
92    /// Hashing a `Type` is cheap (SmallVec, small atomics) — measured cost is
93    /// well below the alloc-savings benefit on real workloads.
94    static GLOBAL_UNION_INTERN: std::sync::OnceLock<dashmap::DashMap<Type, Arc<Type>>> =
95        std::sync::OnceLock::new();
96
97    fn global_intern_table() -> &'static dashmap::DashMap<Type, Arc<Type>> {
98        GLOBAL_UNION_INTERN.get_or_init(dashmap::DashMap::default)
99    }
100
101    /// Try to intern a Type if it matches a common type, otherwise wrap in Arc.
102    pub fn intern_or_wrap(union: Type) -> Arc<Type> {
103        // Fast path 1: single-atomic scalar — covered by `OnceLock` constants.
104        // Avoids any DashMap traffic for the most common case.
105        if union.types.len() == 1 && !union.possibly_undefined && !union.from_docblock {
106            match &union.types[0] {
107                mir_types::Atomic::TString => return string(),
108                mir_types::Atomic::TInt => return int(),
109                mir_types::Atomic::TFloat => return float(),
110                mir_types::Atomic::TBool => return bool(),
111                mir_types::Atomic::TMixed => return mixed(),
112                mir_types::Atomic::TNull => return null(),
113                mir_types::Atomic::TVoid => return void(),
114                _ => {}
115            }
116        }
117        // Fast path 2: empty Type — also a common case (e.g. unresolved
118        // return type). Don't pollute the intern table with these.
119        if union.types.is_empty() {
120            return Arc::new(union);
121        }
122        // Global path: dedup against any previously-seen identical Type.
123        let table = global_intern_table();
124        if let Some(existing) = table.get(&union) {
125            return Arc::clone(existing.value());
126        }
127        let arc = Arc::new(union.clone());
128        // `insert` semantics: if a parallel thread beat us, its Arc wins.
129        // The lookup-before-insert race is benign — both Arcs are content-
130        // equal — but we still want to share the canonical one going forward.
131        match table.entry(union) {
132            dashmap::mapref::entry::Entry::Occupied(o) => Arc::clone(o.get()),
133            dashmap::mapref::entry::Entry::Vacant(v) => {
134                v.insert(Arc::clone(&arc));
135                arc
136            }
137        }
138    }
139}
140
141// ---------------------------------------------------------------------------
142// Shared primitives
143// ---------------------------------------------------------------------------
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
146pub enum Visibility {
147    Public,
148    Protected,
149    Private,
150}
151
152impl Visibility {
153    pub fn is_at_least(&self, required: Visibility) -> bool {
154        *self <= required
155    }
156}
157
158impl std::fmt::Display for Visibility {
159    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160        match self {
161            Visibility::Public => write!(f, "public"),
162            Visibility::Protected => write!(f, "protected"),
163            Visibility::Private => write!(f, "private"),
164        }
165    }
166}
167
168fn serialize_template_bound<S>(value: &Option<Arc<Type>>, serializer: S) -> Result<S::Ok, S::Error>
169where
170    S: serde::Serializer,
171{
172    value.as_deref().serialize(serializer)
173}
174
175fn deserialize_template_bound<'de, D>(deserializer: D) -> Result<Option<Arc<Type>>, D::Error>
176where
177    D: serde::Deserializer<'de>,
178{
179    Option::<Type>::deserialize(deserializer).map(|opt| opt.map(interned_types::intern_or_wrap))
180}
181
182#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
183pub struct TemplateParam {
184    pub name: Name,
185    /// Declared upper bound, e.g. `@template T of Traversable`.
186    /// Stored as `Option<Arc<Type>>` so common bounds (e.g. `object`, `mixed`)
187    /// are deduplicated across all template params via the global intern table.
188    #[serde(
189        serialize_with = "serialize_template_bound",
190        deserialize_with = "deserialize_template_bound"
191    )]
192    pub bound: Option<Arc<Type>>,
193    /// The entity (class or function FQN) that declared this template param.
194    pub defining_entity: Name,
195    pub variance: mir_types::Variance,
196}
197
198#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
199pub struct FnParam {
200    pub name: Name,
201    /// Parameter type. Stored as `Option<Arc<Type>>` to enable deduplication of
202    /// common types across parameters. Many parameters share types like `string`,
203    /// `int`, `bool`, etc., so interning via Arc saves allocations.
204    #[serde(
205        deserialize_with = "deserialize_param_type",
206        serialize_with = "serialize_param_type"
207    )]
208    pub ty: Option<Arc<Type>>,
209    /// Whether this parameter has a default value. During analysis, defaults are
210    /// never used for their value — only for marking parameters as optional.
211    pub has_default: bool,
212    pub is_variadic: bool,
213    pub is_byref: bool,
214    pub is_optional: bool,
215}
216
217impl std::hash::Hash for FnParam {
218    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
219        self.name.hash(state);
220        self.has_default.hash(state);
221        self.is_variadic.hash(state);
222        self.is_byref.hash(state);
223        self.is_optional.hash(state);
224        // Hash the type value (not the Arc pointer) so that two FnParams with
225        // equal types (PartialEq) always produce the same hash, even when they
226        // are backed by different Arc allocations.
227        self.ty.as_deref().hash(state);
228    }
229}
230
231// Serde helpers to transparently convert between Option<Type> and Option<Arc<Type>>
232fn deserialize_param_type<'de, D>(deserializer: D) -> Result<Option<Arc<Type>>, D::Error>
233where
234    D: serde::Deserializer<'de>,
235{
236    Option::<Type>::deserialize(deserializer).map(|opt| opt.map(interned_types::intern_or_wrap))
237}
238
239fn serialize_param_type<S>(value: &Option<Arc<Type>>, serializer: S) -> Result<S::Ok, S::Error>
240where
241    S: serde::Serializer,
242{
243    let opt = value.as_ref().map(|arc| (**arc).clone());
244    opt.serialize(serializer)
245}
246
247fn deserialize_return_type<'de, D>(deserializer: D) -> Result<Option<Arc<Type>>, D::Error>
248where
249    D: serde::Deserializer<'de>,
250{
251    Option::<Type>::deserialize(deserializer).map(|opt| opt.map(interned_types::intern_or_wrap))
252}
253
254fn serialize_return_type<S>(value: &Option<Arc<Type>>, serializer: S) -> Result<S::Ok, S::Error>
255where
256    S: serde::Serializer,
257{
258    let opt = value.as_ref().map(|arc| (**arc).clone());
259    opt.serialize(serializer)
260}
261
262fn deserialize_params<'de, D>(deserializer: D) -> Result<Arc<[FnParam]>, D::Error>
263where
264    D: serde::Deserializer<'de>,
265{
266    Vec::<FnParam>::deserialize(deserializer).map(|v| Arc::from(v.into_boxed_slice()))
267}
268
269fn default_imports() -> Arc<FxHashMap<Name, Name>> {
270    Arc::new(FxHashMap::default())
271}
272
273/// Deserialize imports map. Supports both new (Name-keyed) and legacy
274/// (String-keyed) on-disk formats — older `cache.bin` files have plain
275/// `HashMap<String, String>`. Either way, we intern at load time so the
276/// in-memory representation is always `Arc<FxHashMap<Name, Name>>`.
277fn deserialize_imports<'de, D>(deserializer: D) -> Result<Arc<FxHashMap<Name, Name>>, D::Error>
278where
279    D: serde::Deserializer<'de>,
280{
281    let raw = FxHashMap::<String, String>::deserialize(deserializer)?;
282    let mut out: FxHashMap<Name, Name> =
283        FxHashMap::with_capacity_and_hasher(raw.len(), Default::default());
284    for (k, v) in raw {
285        out.insert(Name::new(&k), Name::new(&v));
286    }
287    Ok(Arc::new(out))
288}
289
290/// Serialize imports as the legacy `HashMap<String, String>` shape so disk
291/// caches written by this version remain compatible with readers that haven't
292/// been recompiled yet (and vice-versa).
293fn serialize_imports<S>(
294    value: &Arc<FxHashMap<Name, Name>>,
295    serializer: S,
296) -> Result<S::Ok, S::Error>
297where
298    S: serde::Serializer,
299{
300    use serde::ser::SerializeMap;
301    let mut map = serializer.serialize_map(Some(value.len()))?;
302    for (k, v) in value.iter() {
303        map.serialize_entry(k.as_str(), v.as_str())?;
304    }
305    map.end()
306}
307
308fn serialize_params<S>(value: &Arc<[FnParam]>, serializer: S) -> Result<S::Ok, S::Error>
309where
310    S: serde::Serializer,
311{
312    value.as_ref().serialize(serializer)
313}
314
315/// Helper to wrap Option<Type> in interned Arc<Type>.
316pub fn wrap_param_type(ty: Option<Type>) -> Option<Arc<Type>> {
317    ty.map(interned_types::intern_or_wrap)
318}
319
320/// Helper to wrap return type Option<Type> in interned Arc<Type>.
321pub fn wrap_return_type(ty: Option<Type>) -> Option<Arc<Type>> {
322    ty.map(interned_types::intern_or_wrap)
323}
324
325/// Helper to wrap a `PropertyDef` type field (`ty`/`inferred_ty`/`default`) in
326/// an interned `Arc<Type>`, deduplicating common property types via the global
327/// pool. See [`PropertyDef`].
328pub fn wrap_property_type(ty: Option<Type>) -> Option<Arc<Type>> {
329    ty.map(interned_types::intern_or_wrap)
330}
331
332/// Helper to wrap a `TemplateParam.bound` in an interned `Arc<Type>`.
333pub fn wrap_template_bound(ty: Option<Type>) -> Option<Arc<Type>> {
334    ty.map(interned_types::intern_or_wrap)
335}
336
337/// Wrap a variable type in an interned `Arc<Type>`. Use instead of
338/// `Arc::new(ty)` at `FlowState::set_var` and parameter-init sites so that
339/// common scalars (string, int, bool, null, mixed) share a static Arc rather
340/// than allocating a fresh one per assignment.
341pub fn wrap_var_type(ty: Type) -> Arc<Type> {
342    interned_types::intern_or_wrap(ty)
343}
344
345// ---------------------------------------------------------------------------
346// Assertion — `@psalm-assert`, `@psalm-assert-if-true`, etc.
347// ---------------------------------------------------------------------------
348
349#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
350pub enum AssertionKind {
351    Assert,
352    AssertIfTrue,
353    AssertIfFalse,
354}
355
356#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
357pub struct Assertion {
358    pub kind: AssertionKind,
359    pub param: Arc<str>,
360    pub ty: Type,
361}
362
363// ---------------------------------------------------------------------------
364// MethodDef
365// ---------------------------------------------------------------------------
366
367#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
368pub struct MethodDef {
369    pub name: Arc<str>,
370    pub fqcn: Arc<str>,
371    #[serde(
372        deserialize_with = "deserialize_params",
373        serialize_with = "serialize_params"
374    )]
375    pub params: Arc<[FnParam]>,
376    /// Type from annotation (`@return` / native type hint). `None` means unannotated.
377    /// Stored as `Option<Arc<Type>>` to enable deduplication of common return types
378    /// (e.g., `void`, `string`, `mixed`, `bool`) across thousands of methods.
379    #[serde(
380        deserialize_with = "deserialize_return_type",
381        serialize_with = "serialize_return_type"
382    )]
383    pub return_type: Option<Arc<Type>>,
384    /// Type inferred from body analysis. Stored as `Option<Arc<Type>>` (8 B) rather
385    /// than inline `Option<Type>` (176 B, no niche) — inference is now demand-driven
386    /// via salsa (`inferred_*_return_type_demand`), so this field is a rarely/never
387    /// populated fallback; shrinking it saves ~168 B on every MethodDef.
388    #[serde(
389        deserialize_with = "deserialize_return_type",
390        serialize_with = "serialize_return_type"
391    )]
392    pub inferred_return_type: Option<Arc<Type>>,
393    pub visibility: Visibility,
394    pub is_static: bool,
395    pub is_abstract: bool,
396    pub is_final: bool,
397    pub is_constructor: bool,
398    pub template_params: Vec<TemplateParam>,
399    pub assertions: Vec<Assertion>,
400    pub throws: Vec<Arc<str>>,
401    pub deprecated: Option<Arc<str>>,
402    pub is_internal: bool,
403    pub is_pure: bool,
404    /// `@no-named-arguments` — callers must not use named argument syntax.
405    #[serde(default)]
406    pub no_named_arguments: bool,
407    /// True when the method has the `#[Override]` PHP attribute.
408    #[serde(default)]
409    pub is_override: bool,
410    pub location: Option<Location>,
411    /// Plain-text description from the docblock (text before `@tag` lines).
412    /// Used for hover info.
413    #[serde(default)]
414    pub docstring: Option<Arc<str>>,
415    /// True for methods added via `@method` docblock annotations. Virtual
416    /// methods must not be required as concrete interface implementations.
417    #[serde(default)]
418    pub is_virtual: bool,
419    /// Parameters declared as taint sinks via `@taint-sink <kind> $param`.
420    /// Each entry is `(param_name_without_dollar, sink_kind_string)`.
421    #[serde(default)]
422    pub taint_sink_params: Vec<(Arc<str>, Arc<str>)>,
423}
424
425impl MethodDef {
426    pub fn effective_return_type(&self) -> Option<&Type> {
427        self.return_type
428            .as_deref()
429            .or(self.inferred_return_type.as_deref())
430    }
431}
432
433// ---------------------------------------------------------------------------
434// PropertyDef
435// ---------------------------------------------------------------------------
436
437#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
438pub struct PropertyDef {
439    pub name: Arc<str>,
440    /// Declared/inferred/default types. Stored as `Option<Arc<Type>>` (8 B)
441    /// rather than inline `Option<Type>` (176 B, no niche) and interned via the
442    /// global pool on construction/deserialization — common property types
443    /// (`string`, `int`, a shared class type) dedup to one allocation. Mirrors
444    /// `FnParam::ty`. On-disk format is unchanged (the serde helpers (de)serialize
445    /// the inner `Type` transparently).
446    #[serde(
447        deserialize_with = "deserialize_param_type",
448        serialize_with = "serialize_param_type"
449    )]
450    pub ty: Option<Arc<Type>>,
451    #[serde(
452        deserialize_with = "deserialize_param_type",
453        serialize_with = "serialize_param_type"
454    )]
455    pub inferred_ty: Option<Arc<Type>>,
456    pub visibility: Visibility,
457    pub is_static: bool,
458    pub is_readonly: bool,
459    #[serde(
460        deserialize_with = "deserialize_param_type",
461        serialize_with = "serialize_param_type"
462    )]
463    pub default: Option<Arc<Type>>,
464    pub location: Option<Location>,
465    /// `@deprecated` docblock annotation, if present.
466    #[serde(default)]
467    pub deprecated: Option<Arc<str>>,
468}
469
470// ---------------------------------------------------------------------------
471// ConstantDef
472// ---------------------------------------------------------------------------
473
474#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
475pub struct ConstantDef {
476    pub name: Arc<str>,
477    pub ty: Type,
478    pub visibility: Option<Visibility>,
479    #[serde(default)]
480    pub is_final: bool,
481    pub location: Option<Location>,
482    /// `@deprecated` docblock annotation, if present.
483    #[serde(default)]
484    pub deprecated: Option<Arc<str>>,
485}
486
487// ---------------------------------------------------------------------------
488// ClassDef
489// ---------------------------------------------------------------------------
490
491#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
492pub struct ClassDef {
493    pub fqcn: Arc<str>,
494    pub short_name: Arc<str>,
495    pub parent: Option<Arc<str>>,
496    pub interfaces: Vec<Arc<str>>,
497    pub traits: Vec<Arc<str>>,
498    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
499    pub own_properties: IndexMap<Arc<str>, PropertyDef>,
500    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
501    #[serde(default)]
502    pub mixins: Vec<Arc<str>>,
503    pub template_params: Vec<TemplateParam>,
504    /// Type arguments from `@extends ParentClass<T1, T2>` — maps parent's template params to concrete types.
505    pub extends_type_args: Vec<Type>,
506    /// Type arguments from `@implements Interface<T1, T2>`.
507    #[serde(default)]
508    pub implements_type_args: Vec<(Arc<str>, Vec<Type>)>,
509    pub is_abstract: bool,
510    pub is_final: bool,
511    pub is_readonly: bool,
512    pub deprecated: Option<Arc<str>>,
513    pub is_internal: bool,
514    /// Attribute target flags if this class has `#[Attribute]` annotation.
515    /// `None` = not an attribute class. The value is a bitmask of PHP's
516    /// `Attribute::TARGET_*` constants (e.g. `Attribute::TARGET_CLASS = 1`).
517    #[serde(default)]
518    pub attribute_flags: Option<i64>,
519    pub location: Option<Location>,
520    /// Per-`use` statement locations for each used trait: `(fqcn, location)` in
521    /// declaration order, parallel to `traits`.  Absent from older serialized
522    /// slices; defaults to empty.
523    #[serde(default)]
524    pub trait_use_locations: Vec<(Arc<str>, Location)>,
525    /// Type aliases declared on this class via `@psalm-type` / `@phpstan-type`.
526    #[serde(default)]
527    pub type_aliases: FxHashMap<Arc<str>, Type>,
528    /// Raw import-type declarations (`(local_name, original_name, from_class)`) — resolved during finalization.
529    #[serde(default)]
530    pub pending_import_types: Vec<(Arc<str>, Arc<str>, Arc<str>)>,
531    /// Trait precedence exclusions from `insteadof` declarations in this class's `use` blocks.
532    /// Maps method_name_lowercase → list of trait FQCNs whose version of the method is excluded.
533    /// E.g. `use A, B { B::hello insteadof A; }` stores `"hello" → ["A"]`.
534    #[serde(default)]
535    pub trait_insteadof: IndexMap<Arc<str>, Vec<Arc<str>>>,
536}
537
538impl ClassDef {
539    pub fn get_method(&self, name: &str) -> Option<&MethodDef> {
540        // PHP method names are case-insensitive; caller should pass lowercase name.
541        // Only searches own_methods — inherited method resolution is done by
542        // `db::lookup_method_in_chain`.
543        self.own_methods.get(name).map(Arc::as_ref).or_else(|| {
544            self.own_methods
545                .iter()
546                .find(|(k, _)| k.as_ref().eq_ignore_ascii_case(name))
547                .map(|(_, v)| v.as_ref())
548        })
549    }
550
551    pub fn get_property(&self, name: &str) -> Option<&PropertyDef> {
552        self.own_properties.get(name)
553    }
554}
555
556// ---------------------------------------------------------------------------
557// InterfaceDef
558// ---------------------------------------------------------------------------
559
560#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
561pub struct InterfaceDef {
562    pub fqcn: Arc<str>,
563    pub short_name: Arc<str>,
564    pub extends: Vec<Arc<str>>,
565    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
566    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
567    pub template_params: Vec<TemplateParam>,
568    pub location: Option<Location>,
569    /// `@deprecated` docblock annotation, if present.
570    #[serde(default)]
571    pub deprecated: Option<Arc<str>>,
572    /// Properties declared via `@property*` docblock annotations on the interface.
573    #[serde(default)]
574    pub own_properties: IndexMap<Arc<str>, PropertyDef>,
575    /// `@seal-properties` / `@psalm-seal-properties` — disallows undeclared property access.
576    #[serde(default)]
577    pub seal_properties: bool,
578}
579
580// ---------------------------------------------------------------------------
581// TraitDef
582// ---------------------------------------------------------------------------
583
584#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
585pub struct TraitDef {
586    pub fqcn: Arc<str>,
587    pub short_name: Arc<str>,
588    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
589    pub own_properties: IndexMap<Arc<str>, PropertyDef>,
590    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
591    pub template_params: Vec<TemplateParam>,
592    /// Traits used by this trait (`use OtherTrait;` inside a trait body).
593    pub traits: Vec<Arc<str>>,
594    pub location: Option<Location>,
595    /// `@psalm-require-extends` / `@phpstan-require-extends` — FQCNs that using classes must extend.
596    #[serde(default)]
597    pub require_extends: Vec<Arc<str>>,
598    /// `@psalm-require-implements` / `@phpstan-require-implements` — FQCNs that using classes must implement.
599    #[serde(default)]
600    pub require_implements: Vec<Arc<str>>,
601    /// `@deprecated` docblock annotation, if present.
602    #[serde(default)]
603    pub deprecated: Option<Arc<str>>,
604}
605
606// ---------------------------------------------------------------------------
607// EnumDef
608// ---------------------------------------------------------------------------
609
610#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
611pub struct EnumCaseDef {
612    pub name: Arc<str>,
613    pub value: Option<Type>,
614    pub location: Option<Location>,
615    /// `@deprecated` docblock annotation, if present.
616    #[serde(default)]
617    pub deprecated: Option<Arc<str>>,
618}
619
620#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
621pub struct EnumDef {
622    pub fqcn: Arc<str>,
623    pub short_name: Arc<str>,
624    pub scalar_type: Option<Type>,
625    pub interfaces: Vec<Arc<str>>,
626    pub cases: IndexMap<Arc<str>, EnumCaseDef>,
627    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
628    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
629    pub location: Option<Location>,
630}
631
632// ---------------------------------------------------------------------------
633// FunctionDef
634// ---------------------------------------------------------------------------
635
636#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
637pub struct FunctionDef {
638    pub fqn: Arc<str>,
639    pub short_name: Arc<str>,
640    #[serde(
641        deserialize_with = "deserialize_params",
642        serialize_with = "serialize_params"
643    )]
644    pub params: Arc<[FnParam]>,
645    /// Type from annotation (`@return` / native type hint). `None` means unannotated.
646    /// Stored as `Option<Arc<Type>>` to enable deduplication of common return types.
647    #[serde(
648        deserialize_with = "deserialize_return_type",
649        serialize_with = "serialize_return_type"
650    )]
651    pub return_type: Option<Arc<Type>>,
652    /// See `MethodDef::inferred_return_type` — `Option<Arc<Type>>` (8 B) for the
653    /// same demand-driven-inference reason.
654    #[serde(
655        deserialize_with = "deserialize_return_type",
656        serialize_with = "serialize_return_type"
657    )]
658    pub inferred_return_type: Option<Arc<Type>>,
659    pub template_params: Vec<TemplateParam>,
660    pub assertions: Vec<Assertion>,
661    pub throws: Vec<Arc<str>>,
662    pub deprecated: Option<Arc<str>>,
663    pub is_pure: bool,
664    /// `@no-named-arguments` — callers must not use named argument syntax.
665    #[serde(default)]
666    pub no_named_arguments: bool,
667    pub location: Option<Location>,
668    /// Plain-text description from the docblock (text before `@tag` lines).
669    /// Used for hover info.
670    #[serde(default)]
671    pub docstring: Option<Arc<str>>,
672    /// Parameters declared as taint sinks via `@taint-sink <kind> $param`.
673    /// Each entry is `(param_name_without_dollar, sink_kind_string)`.
674    #[serde(default)]
675    pub taint_sink_params: Vec<(Arc<str>, Arc<str>)>,
676}
677
678impl FunctionDef {
679    pub fn effective_return_type(&self) -> Option<&Type> {
680        self.return_type
681            .as_deref()
682            .or(self.inferred_return_type.as_deref())
683    }
684}
685
686// ---------------------------------------------------------------------------
687// StubSlice — serializable bundle of definitions from one extension's stubs
688// ---------------------------------------------------------------------------
689
690/// A snapshot of all PHP definitions contributed by a single stub file set.
691///
692/// Produced by `mir-stubs-gen` at code-generation time and deserialized at
693/// runtime to ingest definitions into the salsa db via
694/// `MirDatabase::ingest_stub_slice`.
695#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
696pub struct StubSlice {
697    pub classes: Vec<Arc<ClassDef>>,
698    pub interfaces: Vec<Arc<InterfaceDef>>,
699    pub traits: Vec<Arc<TraitDef>>,
700    pub enums: Vec<Arc<EnumDef>>,
701    pub functions: Vec<Arc<FunctionDef>>,
702    #[serde(default)]
703    pub constants: Vec<(Arc<str>, Type)>,
704    /// Source file this slice was collected from. `None` for bundled stub slices
705    /// that were pre-computed and are not tied to a specific on-disk file.
706    #[serde(default)]
707    pub file: Option<Arc<str>>,
708    /// Types of `@var`-annotated global variables collected from this file.
709    /// Populated by `DefinitionCollector`; ingested into the salsa db's
710    /// `global_vars` table by `ingest_stub_slice` when `file` is `Some`.
711    #[serde(default)]
712    pub global_vars: Vec<(Arc<str>, Type)>,
713    /// The first namespace declared in this file (e.g. `"App\\Service"`).
714    /// Populated by `DefinitionCollector`; ingested into the salsa db's
715    /// `file_namespaces` table by `ingest_stub_slice` when `file` is `Some`.
716    #[serde(default)]
717    pub namespace: Option<Arc<str>>,
718    /// `use` alias map for this file: alias → FQCN.
719    ///
720    /// Stored as `Arc<FxHashMap<Name, Name>>` so that `file_imports()`
721    /// returns a cheap Arc clone instead of deep-cloning the map on every
722    /// `resolve_name` call (which fires once per symbol reference in
723    /// Pass 2). `Name` keys/values shrink each entry from ~108 bytes
724    /// (two `String` headers + two heap allocs averaging ~30 chars) to
725    /// 16 bytes (two `Ustr` u64 handles); the global ustr interner holds
726    /// one copy of each unique alias / FQCN string for the whole session.
727    #[serde(
728        deserialize_with = "deserialize_imports",
729        serialize_with = "serialize_imports"
730    )]
731    #[serde(default = "default_imports")]
732    pub imports: Arc<FxHashMap<Name, Name>>,
733    /// Set to `true` after `deduplicate_params_in_slice` has run on this slice.
734    /// `ingest_stub_slice` skips the clone+re-dedup when this flag is set.
735    #[serde(skip)]
736    pub is_deduped: bool,
737}
738
739// ---------------------------------------------------------------------------
740// Param list deduplication
741// ---------------------------------------------------------------------------
742
743use std::sync::Mutex;
744
745type ParamCache = Mutex<FxHashMap<Vec<FnParam>, Arc<[FnParam]>>>;
746
747/// Global cache of canonical Arc<[FnParam]> instances for deduplication.
748/// Shared across all StubSlices to deduplicate vendor code with millions of
749/// methods that often have identical parameter lists.
750static PARAM_DEDUP_CACHE: std::sync::OnceLock<ParamCache> = std::sync::OnceLock::new();
751
752/// Deduplicate parameter lists across all methods and functions in a StubSlice.
753/// Many PHP framework methods share identical parameter lists (e.g., thousands
754/// of `(string $arg, array $opts)` signatures). This function groups identical
755/// param lists globally (across all slices processed so far) and replaces them
756/// with Arc<[FnParam]> pointers to shared allocations.
757///
758/// Expected memory savings: 100–150 MiB on cold start (vendor collection).
759pub fn deduplicate_params_in_slice(slice: &mut StubSlice) {
760    let cache: &ParamCache = PARAM_DEDUP_CACHE.get_or_init(|| Mutex::new(FxHashMap::default()));
761    let mut canonical_params = cache.lock().unwrap();
762
763    let mut deduplicate = |params: &mut Arc<[FnParam]>| {
764        if let Some(existing) = canonical_params.get(params.as_ref()) {
765            *params = existing.clone();
766        } else {
767            canonical_params.insert(params.as_ref().to_vec(), params.clone());
768        }
769    };
770
771    // Deduplicate method params in all classes
772    for cls in &mut slice.classes {
773        for method in Arc::make_mut(cls).own_methods.values_mut() {
774            deduplicate(&mut Arc::make_mut(method).params);
775        }
776    }
777
778    // Deduplicate method params in all interfaces
779    for iface in &mut slice.interfaces {
780        for method in Arc::make_mut(iface).own_methods.values_mut() {
781            deduplicate(&mut Arc::make_mut(method).params);
782        }
783    }
784
785    // Deduplicate method params in all traits
786    for tr in &mut slice.traits {
787        for method in Arc::make_mut(tr).own_methods.values_mut() {
788            deduplicate(&mut Arc::make_mut(method).params);
789        }
790    }
791
792    // Deduplicate method params in all enums
793    for en in &mut slice.enums {
794        for method in Arc::make_mut(en).own_methods.values_mut() {
795            deduplicate(&mut Arc::make_mut(method).params);
796        }
797    }
798
799    // Deduplicate function params
800    for func in &mut slice.functions {
801        deduplicate(&mut Arc::make_mut(func).params);
802    }
803    slice.is_deduped = true;
804}