Skip to main content

leo_ast/common/
path.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{Expression, Identifier, Location, Node, NodeID, ProgramId, simple_node_impl};
18
19use leo_span::{Span, Symbol, with_session_globals};
20
21use indexmap::IndexSet;
22use itertools::Itertools;
23use serde::{Deserialize, Serialize};
24use std::{fmt, hash::Hash};
25
26/// A Path in a program.
27#[derive(Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
28pub struct Path {
29    /// The program this path belongs to, if set by the user
30    user_program: Option<ProgramId>,
31
32    /// The qualifying namespace segments written by the user, excluding the item itself.
33    /// e.g., in `foo::bar::baz`, this would be `[foo, bar]`.
34    qualifier: Vec<Identifier>,
35
36    /// The final item in the path, e.g., `baz` in `foo::bar::baz`.
37    identifier: Identifier,
38
39    /// The target type (i.e. local v.s. global) of this path.
40    target: PathTarget,
41
42    /// A span locating where the path occurred in the source.
43    pub span: Span,
44
45    /// The ID of the node.
46    pub id: NodeID,
47}
48
49#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
50pub enum PathTarget {
51    Unresolved,
52    Local(Symbol),
53    Global(Location),
54}
55
56simple_node_impl!(Path);
57
58impl Path {
59    /// Creates a new unresolved `Path` from the given components.
60    ///
61    /// - `user_program`: An optional program name (e.g. `credits` in `credits.aleo::Bar`)
62    /// - `qualifier`: The namespace segments (e.g., `foo::bar` in `foo::bar::baz`).
63    /// - `identifier`: The final item in the path (e.g., `baz`).
64    /// - `span`: The source code span for this path.
65    /// - `id`: The node ID.
66    pub fn new(
67        user_program: Option<ProgramId>,
68        qualifier: Vec<Identifier>,
69        identifier: Identifier,
70        span: Span,
71        id: NodeID,
72    ) -> Self {
73        Self { user_program, qualifier, identifier, target: PathTarget::Unresolved, span, id }
74    }
75
76    /// Returns the final identifier of the path (e.g., `baz` in `foo::bar::baz`).
77    pub fn identifier(&self) -> &Identifier {
78        &self.identifier
79    }
80
81    /// Returns a slice of the qualifier segments (e.g., `[foo, bar]` in `foo::bar::baz`).
82    pub fn qualifier(&self) -> &[Identifier] {
83        &self.qualifier
84    }
85
86    /// Returns an iterator over all segments as `Symbol`s (qualifiers + identifier).
87    pub fn segments_iter(&self) -> impl Iterator<Item = Symbol> + '_ {
88        self.qualifier.iter().map(|id| id.name).chain(std::iter::once(self.identifier.name))
89    }
90
91    /// Returns a `Vec<Symbol>` of the segments.
92    pub fn segments(&self) -> Vec<Symbol> {
93        self.segments_iter().collect()
94    }
95
96    /// Returns the optional program identifier.
97    pub fn user_program(&self) -> Option<&ProgramId> {
98        self.user_program.as_ref()
99    }
100
101    /// Returns `self` after setting it `user_program` field to `user_program`.
102    pub fn with_user_program(mut self, user_program: ProgramId) -> Self {
103        self.user_program = Some(user_program);
104        self
105    }
106
107    pub fn span(&self) -> Span {
108        self.span
109    }
110
111    pub fn id(&self) -> NodeID {
112        self.id
113    }
114
115    pub fn is_resolved(&self) -> bool {
116        !matches!(self.target, PathTarget::Unresolved)
117    }
118
119    pub fn is_local(&self) -> bool {
120        matches!(self.target, PathTarget::Local(_))
121    }
122
123    pub fn is_global(&self) -> bool {
124        matches!(self.target, PathTarget::Global(_))
125    }
126
127    /// Returns the program symbol this path refers to, if known.
128    ///
129    /// Priority:
130    /// 1. User-written program qualifier (e.g. `foo.aleo::bar::baz`)
131    /// 2. Resolved global target program
132    /// 3. None (unresolved or local)
133    pub fn program(&self) -> Option<Symbol> {
134        if let Some(id) = &self.user_program {
135            return Some(id.as_symbol());
136        }
137
138        match &self.target {
139            PathTarget::Global(location) => Some(location.program),
140            _ => None,
141        }
142    }
143
144    /// Returns the `Symbol` if local, `None` if not.
145    pub fn try_local_symbol(&self) -> Option<Symbol> {
146        match self.target {
147            PathTarget::Local(sym) => Some(sym),
148            _ => None,
149        }
150    }
151
152    /// Returns the `Location` if global, `None` if not.
153    pub fn try_global_location(&self) -> Option<&Location> {
154        match &self.target {
155            PathTarget::Global(loc) => Some(loc),
156            _ => None,
157        }
158    }
159
160    /// Returns the `Symbol` if local, panics if not.
161    pub fn expect_local_symbol(&self) -> Symbol {
162        match self.target {
163            PathTarget::Local(sym) => sym,
164            _ => panic!("Expected a local path, found {:?}", self.target),
165        }
166    }
167
168    /// Returns the `Location` if global, panics if not.
169    pub fn expect_global_location(&self) -> &Location {
170        match &self.target {
171            PathTarget::Global(loc) => loc,
172            _ => panic!("Expected a global path, found {:?}", self.target),
173        }
174    }
175
176    /// Resolves this path to a local symbol.
177    pub fn to_local(self) -> Self {
178        Self { target: PathTarget::Local(self.identifier.name), ..self }
179    }
180
181    /// Resolves this path to a global location.
182    pub fn to_global(self, location: Location) -> Self {
183        Self { target: PathTarget::Global(location), ..self }
184    }
185
186    /// Returns a new `Path` with the final identifier replaced by `new_symbol`.
187    ///
188    /// This updates:
189    /// - `identifier.name`
190    /// - `target`:
191    ///   - `Local(_)` → `Local(new_symbol)`
192    ///   - `Global(Location)` → same location, but with the final path segment replaced
193    ///   - `Unresolved` → unchanged
194    pub fn with_updated_last_symbol(self, new_symbol: Symbol) -> Self {
195        let Path { mut identifier, target, user_program, qualifier, span, id } = self;
196
197        // Update user-visible identifier
198        identifier.name = new_symbol;
199
200        let target = match target {
201            PathTarget::Unresolved => PathTarget::Unresolved,
202
203            PathTarget::Local(_) => PathTarget::Local(new_symbol),
204
205            PathTarget::Global(location) => {
206                let Location { program, mut path } = location;
207
208                assert!(!path.is_empty(), "global location must have at least one path segment");
209
210                *path.last_mut().unwrap() = new_symbol;
211
212                PathTarget::Global(Location { program, path })
213            }
214        };
215
216        Self { user_program, qualifier, identifier, target, span, id }
217    }
218
219    /// Resolves this path as a global path within the current module context.
220    ///
221    /// This function converts a user-written path into a fully qualified
222    /// [`PathTarget::Global`] by determining which program the path belongs to
223    /// and constructing the corresponding module path.
224    ///
225    /// Resolution follows two main cases:
226    ///
227    /// 1. **External library access**
228    ///    If the path does not explicitly specify a program (`user_program` is `None`)
229    ///    and the first qualifier segment matches a known external library name,
230    ///    that segment is interpreted as the target program. The remaining qualifier
231    ///    segments and identifier form the path inside that program.
232    ///
233    /// 2. **Local or explicitly-qualified program access**
234    ///    Otherwise, the path is resolved relative to the current module context.
235    ///    The final location is constructed by combining:
236    ///      - the current module path,
237    ///      - any user-written qualifier segments, and
238    ///      - the final identifier.
239    ///
240    /// If the user explicitly wrote a program (via `user_program`), it overrides
241    /// the default `program` parameter. Otherwise, the current program is used.
242    ///
243    /// Importantly, this transformation **does not modify the user-written syntax**
244    /// (`user_program`, `qualifier`, `identifier`). It only determines the internal
245    /// `target` used during later compilation stages.
246    pub fn resolve_as_global_in_module<I>(
247        self,
248        program: Symbol,
249        external_libs: &IndexSet<Symbol>,
250        current_module: I,
251    ) -> Self
252    where
253        I: IntoIterator<Item = Symbol>,
254    {
255        let Path { user_program, qualifier, identifier, span, id, .. } = self;
256
257        // Case 1: The path starts with a known external library name, or with the name
258        // of the current program/library itself (a self-qualified path like `my_lib::module::item`),
259        // and the user did not explicitly specify a program. In either situation we interpret
260        // the first qualifier segment as the program name.
261        //
262        // The `first.name == program` branch handles intra-library self-qualified references
263        // (e.g., `my_lib::sub_mod::fn` written inside `my_lib`). It cannot accidentally fire
264        // for regular `.aleo` programs because program names always carry the `.aleo` suffix
265        // (e.g., `foo.aleo`), while Leo identifiers cannot contain `.`, so no qualifier can
266        // ever equal a program name.
267        if let Some(first) = qualifier.first()
268            && user_program.is_none()
269            && (external_libs.contains(&first.name) || first.name == program)
270        {
271            // Build the path within the external library by skipping the
272            // first qualifier (the library name itself).
273            let mut path: Vec<Symbol> = qualifier.iter().skip(1).map(|id| id.name).collect();
274            path.push(identifier.name);
275
276            let target = PathTarget::Global(Location { program: first.name, path });
277
278            Self { user_program: None, qualifier, identifier, target, span, id }
279        } else {
280            // Case 2: Resolve relative to the current module.
281            //
282            // Construct the path by concatenating:
283            //   current_module + user qualifier + identifier.
284            let mut path: Vec<Symbol> = Vec::new();
285            path.extend(current_module);
286            path.extend(qualifier.iter().map(|id| id.name));
287            path.push(identifier.name);
288
289            // Determine which program this location belongs to:
290            //   - use the explicitly written program if provided
291            //   - otherwise fall back to the current program.
292            let target = PathTarget::Global(Location {
293                program: user_program.map(|id| id.as_symbol()).unwrap_or(program),
294                path,
295            });
296
297            Self { user_program, qualifier, identifier, target, span, id }
298        }
299    }
300}
301
302impl fmt::Display for Path {
303    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
304        // Determine the program prefix and separator:
305        //
306        // 1. If the user explicitly wrote a program (e.g. `credits.aleo::Foo`), always use it
307        //    with a `::` separator.
308        //
309        // 2. Otherwise fall back to the resolved global location's program, but only when it is
310        //    an `.aleo` program.  `.aleo` programs never appear in the qualifier, so we must
311        //    reconstruct the prefix here to produce readable error messages like
312        //    `parent.aleo::Foo` vs `child.aleo::Foo`.
313        //
314        // 3. Library programs (no `.aleo` suffix) already have their name as the first qualifier
315        //    segment (e.g. `math_lib::Foo`), so adding a prefix here would double-print it.
316        if let Some(pid) = &self.user_program {
317            write!(f, "{}::", pid.as_symbol())?;
318        } else if let Some(loc) = self.try_global_location() {
319            // Use the global program as prefix only for .aleo programs.
320            if with_session_globals(|sg| loc.program.as_str(sg, |s| s.ends_with(".aleo"))) {
321                write!(f, "{}::", loc.program)?;
322            }
323        }
324
325        // Qualifiers (always `::` separator, covers library names like `math_lib::Foo`).
326        if !self.qualifier.is_empty() {
327            write!(f, "{}::", self.qualifier.iter().map(|id| &id.name).format("::"))?;
328        }
329
330        // Final identifier
331        write!(f, "{}", self.identifier.name)
332    }
333}
334
335impl fmt::Debug for Path {
336    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
337        // First, display the user-written path
338        write!(f, "{}", self)?;
339
340        // Append resolved info if available
341        match &self.target {
342            PathTarget::Local(sym) => write!(f, " [local: {sym}]"),
343            PathTarget::Global(loc) => {
344                write!(f, " [global: {loc}]")
345            }
346            PathTarget::Unresolved => write!(f, " [unresolved]"),
347        }
348    }
349}
350
351impl From<Path> for Expression {
352    fn from(value: Path) -> Self {
353        Expression::Path(value)
354    }
355}