pub struct SymbolTable { /* private fields */ }Expand description
A global symbol table for resolving cross-file references.
Implementations§
Source§impl SymbolTable
impl SymbolTable
pub fn new() -> Self
Sourcepub fn insert(&mut self, fqn: String, id: NodeId, file: PathBuf)
pub fn insert(&mut self, fqn: String, id: NodeId, file: PathBuf)
Registers a symbol definition.
Repeated FQNs accumulate instead of overwriting.
Sourcepub fn resolve(&self, fqn: &str) -> Option<NodeId>
pub fn resolve(&self, fqn: &str) -> Option<NodeId>
Resolves an exact FQN, but only when the definition is unambiguous.
Returns None for a colliding FQN so the caller can fall back to
context-aware resolution rather than picking a definition at random.
Sourcepub fn resolve_all(&self, fqn: &str) -> Vec<NodeId> ⓘ
pub fn resolve_all(&self, fqn: &str) -> Vec<NodeId> ⓘ
All nodes defining this exact FQN, in deterministic order.
Sourcepub fn get_file_exports(&self, file: &PathBuf) -> Option<&Vec<String>>
pub fn get_file_exports(&self, file: &PathBuf) -> Option<&Vec<String>>
Returns all symbols exported by a file.
pub fn clear(&mut self)
Sourcepub fn resolve_ref(&self, name: &str, context_file: &Path) -> Resolution
pub fn resolve_ref(&self, name: &str, context_file: &Path) -> Resolution
Resolves a reference, preferring locality and reporting ambiguity.
Order:
- Exact FQN, unique →
Exact - Candidate gathering: exact-FQN entries plus every segment-aligned suffix match, deduplicated and sorted
- Single candidate →
Exact/UniqueSuffix - Exactly one candidate in the referencing file →
SameFile - Exactly one candidate in the referencing directory →
SameDir - Otherwise →
Ambiguous
Sourcepub fn resolve_ref_with_imports(
&self,
name: &str,
context_file: &Path,
imports: Option<&HashMap<String, String>>,
) -> Resolution
pub fn resolve_ref_with_imports( &self, name: &str, context_file: &Path, imports: Option<&HashMap<String, String>>, ) -> Resolution
Resolve a reference, allowing the referencing file’s imports to break ties.
Without imports, a bare name matching definitions in several modules
falls through to Ambiguous whenever no candidate shares a file or a
directory with the caller — and the builder drops ambiguous edges. On a
codebase that reuses a name across modules that is the common case, not
the rare one: every call to it is discarded, and the module those calls
belonged to reports no callers at all.
The disambiguating fact is usually written at the top of the file.
from deep.l0.m14 import value_m14 says exactly which value_m14 is
meant here. import_map already carries that, but only
apply_import_validation consumed it, and that runs after a candidate
has been chosen — so it never saw the references that were being thrown
away. Consulting it during disambiguation instead of after it is the
whole change.
imports maps a locally-visible name to the module it came from, so an
aliased import (from x import y as z) is keyed on z.
Sourcepub fn resolve_imported(
&self,
name: &str,
module: &str,
context_file: &Path,
) -> Resolution
pub fn resolve_imported( &self, name: &str, module: &str, context_file: &Path, ) -> Resolution
Resolves name imported from module under another name
(from money import money_to_minor_units as to_minor).
The import says where the definition lives, so only definitions of
name in that module are candidates: the module’s own file first
(money.py, or a package’s __init__.py or index.ts), then anything
inside a package of that name. A same-named function elsewhere is not
the target. A module this project doesn’t define (numpy) leaves the
call unresolved, unless a project file or directory, or a directory
above the project, has the module’s name: modules are matched by name,
so it then binds there, as an unaliased import of the name does.
A relative module (./format, ../route, .money, ..core.money)
names one place, read from the importing file’s directory, so a
same-named file elsewhere (every route.ts in a Next.js app) is never a
candidate.
An import binds a module-level name, so name is matched exactly and a
method (Class.name) is never a candidate. Neither is the importing
file: in a wrapper that keeps the imported function’s name
(from money import f as _f then def f(): _f()), _f is money’s.
Sourcepub fn resolve_in_module_file(
&self,
name: &str,
module: &str,
context_file: &Path,
) -> Resolution
pub fn resolve_in_module_file( &self, name: &str, module: &str, context_file: &Path, ) -> Resolution
Resolves name in the module’s own file only: module.ts, or a
package’s index.ts or __init__.py, never another file inside the
package.
For a name a file took from a value the module produced (a hook’s or factory’s return value), not from its exports. A barrel that only re-exports the hook says nothing about the other files under it.
Sourcepub fn resolve_path(&self, path: &str, context_file: &Path) -> Resolution
pub fn resolve_path(&self, path: &str, context_file: &Path) -> Resolution
Resolves a :: path reference: jobs::enqueue, Provider::new,
crate::db::queries::get_user, my_app::app::create.
The path says where the definition lives, so it is matched on that rather than on the bare name:
- a path some parser recorded verbatim (C++
ns::fn) matches directly; - a capitalised owner is a type, and
Type::newis stored asType.new; - anything else is a module, matched against the definition’s file
(
jobs.rs,app/mod.rs). Leading segments are dropped until one matches, so a crate-name prefix (my_app::app::…) works, and a definition re-exported from a submodule is found at lower confidence.
A path into an external crate (sqlx::query, std::fs::read) matches
nothing here and stays unresolved, instead of binding to an unrelated
local function that shares the last name.
Sourcepub fn resolve_with_context(
&self,
name: &str,
context_file: &Path,
) -> Option<NodeId>
pub fn resolve_with_context( &self, name: &str, context_file: &Path, ) -> Option<NodeId>
Back-compatible wrapper returning only a confidently-resolved node.