Skip to main content

mago_names/
resolver.rs

1use bumpalo::Bump;
2use mago_syntax::ast::Program;
3use mago_syntax::walker::MutWalker;
4
5use crate::ResolvedNames;
6use crate::internal::context::NameResolutionContext;
7use crate::internal::walker::NameWalker;
8
9/// Orchestrates the process of resolving names within a PHP Abstract Syntax Tree (AST).
10///
11/// This struct acts as the main entry point for the name resolution pass.
12/// It requires an arena to store resolved names.
13#[derive(Debug, Clone)]
14#[repr(transparent)]
15pub struct NameResolver<'arena> {
16    arena: &'arena Bump,
17}
18
19impl<'arena> NameResolver<'arena> {
20    /// Creates a new `NameResolver` instance.
21    pub fn new(arena: &'arena Bump) -> Self {
22        NameResolver { arena }
23    }
24
25    /// Resolves names within the provided PHP AST `Program`.
26    ///
27    /// # Arguments
28    ///
29    /// * `program` - A reference to the root `Program` AST node. The lifetime `'ast`
30    ///   ensures the AST outlives the borrowing done within this method.
31    ///
32    /// # Returns
33    ///
34    /// A `ResolvedNames` struct containing the mapping of original names/nodes
35    /// to their resolved fully qualified names.
36    #[must_use]
37    pub fn resolve<'ast>(&self, program: &'ast Program<'arena>) -> ResolvedNames<'arena> {
38        let mut context = NameResolutionContext::new(self.arena);
39        let mut walker = NameWalker::default();
40
41        walker.walk_program(program, &mut context);
42
43        walker.resolved_names
44    }
45}