mir_analyzer/symbol.rs
1//! Per-expression resolved symbol data, retained from Pass 2.
2//!
3//! The static analyzer already resolves types for every expression during
4//! analysis but historically discarded the intermediate state. This module
5//! exposes that data so that downstream tools can build position indexes for
6//! hover, go-to-definition, and completions.
7
8use std::sync::Arc;
9
10use mir_types::Union;
11use php_ast::Span;
12
13/// A single resolved symbol observed during Pass 2.
14#[derive(Debug, Clone)]
15pub struct ResolvedSymbol {
16 /// Absolute path of the file this symbol was found in.
17 pub file: Arc<str>,
18 /// Byte-offset span in the source file.
19 pub span: Span,
20 /// What kind of symbol this is.
21 pub kind: SymbolKind,
22 /// The resolved type at this location.
23 pub resolved_type: Union,
24}
25
26impl ResolvedSymbol {
27 /// Return the key used in the salsa db's reference-location table for this
28 /// symbol, or `None` for kinds that are not tracked there (e.g. variables).
29 ///
30 /// Key format mirrors `MirDatabase::record_reference_location`:
31 /// - method / static call : `"ClassName::methodname"` (method lowercased)
32 /// - property access : `"ClassName::propName"`
33 /// - function call : fully-qualified function name
34 /// - class reference : fully-qualified class name
35 pub fn codebase_key(&self) -> Option<String> {
36 match &self.kind {
37 SymbolKind::MethodCall { class, method } | SymbolKind::StaticCall { class, method } => {
38 Some(format!("{}::{}", class, method.to_lowercase()))
39 }
40 SymbolKind::PropertyAccess { class, property } => Some(format!("{class}::{property}")),
41 SymbolKind::FunctionCall(fqn) => Some(fqn.to_string()),
42 SymbolKind::ClassReference(fqcn) => Some(fqcn.to_string()),
43 SymbolKind::Variable(_) => None,
44 }
45 }
46}
47
48/// One declaration emitted by [`crate::AnalysisSession::document_symbols`].
49/// Tool-agnostic shape for outline / breadcrumb features; consumers map this
50/// onto whatever protocol-specific symbol type they need.
51#[derive(Debug, Clone)]
52pub struct DocumentSymbol {
53 /// FQCN for classes/interfaces/traits/enums; FQN for functions /
54 /// constants. Consumers typically display the unqualified last segment.
55 pub name: Arc<str>,
56 /// Coarse kind suitable for icon / severity selection in outlines.
57 pub kind: DocumentSymbolKind,
58 /// Source location of the declaration (file + 1-based lines, 0-based
59 /// columns). `None` only for synthetic stub-only definitions that don't
60 /// have a recorded source span.
61 pub location: Option<mir_codebase::storage::Location>,
62}
63
64/// Coarse declaration kind used by [`DocumentSymbol`]. Six categories chosen
65/// so consumers can map cleanly onto outline-style symbol kinds in any
66/// protocol they target.
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum DocumentSymbolKind {
69 Class,
70 Interface,
71 Trait,
72 Enum,
73 Function,
74 Constant,
75}
76
77/// The kind of symbol that was resolved.
78#[derive(Debug, Clone)]
79pub enum SymbolKind {
80 /// A variable reference (`$foo`).
81 Variable(String),
82 /// An instance method call (`$obj->method()`).
83 MethodCall { class: Arc<str>, method: Arc<str> },
84 /// A static method call (`Foo::method()`).
85 StaticCall { class: Arc<str>, method: Arc<str> },
86 /// A property access (`$obj->prop`).
87 PropertyAccess { class: Arc<str>, property: Arc<str> },
88 /// A free function call (`foo()`).
89 FunctionCall(Arc<str>),
90 /// A class reference (`new Foo`, `instanceof Foo`, type hints).
91 ClassReference(Arc<str>),
92}