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