Skip to main content

leo_ast/stub/
mod.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
17//! A stub contains function templates as well as definitions for mappings, composites, records, and constants.
18
19pub mod function_stub;
20pub use function_stub::*;
21
22use crate::{Composite, ConstDeclaration, Identifier, Indent, Mapping, NodeID, Program, ProgramId};
23use indexmap::IndexSet;
24use leo_span::{Span, Symbol};
25use serde::{Deserialize, Serialize};
26use std::fmt;
27
28#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
29pub enum Stub {
30    /// A dependency that is a Leo program parsed into an AST.
31    FromLeo {
32        program: Program,
33        parents: IndexSet<Symbol>, // These are the names of all the programs that import this dependency.
34    },
35    /// A dependency that is an Aleo program.
36    FromAleo {
37        program: AleoProgram,
38        parents: IndexSet<Symbol>, // These are the names of all the programs that import this dependency.
39    },
40}
41
42impl Stub {
43    /// Returns the programs that this stub imports.
44    pub fn imports(&self) -> Box<dyn Iterator<Item = &Symbol> + '_> {
45        match self {
46            Stub::FromLeo { program, .. } => Box::new(program.imports.keys()),
47            Stub::FromAleo { program, .. } => Box::new(program.imports.iter().map(|id| &id.name.name)),
48        }
49    }
50
51    /// Inserts the given program name as a parent for this stub, implying that the stub is
52    /// imported by this parent.
53    pub fn add_parent(&mut self, parent: Symbol) {
54        match self {
55            Stub::FromLeo { parents, .. } | Stub::FromAleo { parents, .. } => {
56                parents.insert(parent);
57            }
58        }
59    }
60}
61
62impl fmt::Display for Stub {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            Stub::FromLeo { program, .. } => write!(f, "{program}"),
66            Stub::FromAleo { program, .. } => write!(f, "{program}"),
67        }
68    }
69}
70
71impl From<Program> for Stub {
72    fn from(program: Program) -> Self {
73        Stub::FromLeo { program, parents: IndexSet::new() }
74    }
75}
76
77impl From<AleoProgram> for Stub {
78    fn from(program: AleoProgram) -> Self {
79        Stub::FromAleo { program, parents: IndexSet::new() }
80    }
81}
82
83/// Stores the Leo stub abstract syntax tree.
84#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
85pub struct AleoProgram {
86    /// A vector of imported programs.
87    pub imports: Vec<ProgramId>,
88    /// The stub id
89    pub stub_id: ProgramId,
90    /// A vector of const definitions.
91    pub consts: Vec<(Symbol, ConstDeclaration)>,
92    /// A vector of composite definitions.
93    pub composites: Vec<(Symbol, Composite)>,
94    /// A vector of mapping definitions.
95    pub mappings: Vec<(Symbol, Mapping)>,
96    /// A vector of function stub definitions.
97    pub functions: Vec<(Symbol, FunctionStub)>,
98    /// The span associated with the stub.
99    pub span: Span,
100}
101
102impl Default for AleoProgram {
103    /// Constructs an empty program stub
104    fn default() -> Self {
105        Self {
106            imports: Vec::new(),
107            stub_id: ProgramId {
108                name: Identifier::new(Symbol::intern(""), NodeID::default()),
109                network: Identifier::new(Symbol::intern(""), NodeID::default()),
110            },
111            consts: Vec::new(),
112            composites: Vec::new(),
113            mappings: Vec::new(),
114            functions: Vec::new(),
115            span: Span::default(),
116        }
117    }
118}
119
120impl fmt::Display for AleoProgram {
121    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122        writeln!(f, "stub {} {{", self.stub_id)?;
123        for import in self.imports.iter() {
124            writeln!(f, "    import {import};")?;
125        }
126        for (_, mapping) in self.mappings.iter() {
127            writeln!(f, "{};", Indent(mapping))?;
128        }
129        for (_, composite) in self.composites.iter() {
130            writeln!(f, "{}", Indent(composite))?;
131        }
132        for (_, function) in self.functions.iter() {
133            writeln!(f, "{}", Indent(function))?;
134        }
135        write!(f, "}}")
136    }
137}