Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ workspace = true
[dependencies.leo-package]
workspace = true

[dependencies.leo-passes]
workspace = true

[dependencies.leo-span]
workspace = true

Expand Down
14 changes: 9 additions & 5 deletions compiler/ast/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{CoreFunction, Identifier, IntegerType, Node, NodeBuilder, NodeID, Path, Type};
use crate::{CoreFunction, Identifier, IntegerType, Location, Node, NodeBuilder, NodeID, Path, Type};
use leo_span::{Span, Symbol};

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -400,7 +400,8 @@ impl Expression {
ty: &Type,
span: Span,
node_builder: &NodeBuilder,
struct_lookup: &dyn Fn(&[Symbol]) -> Vec<(Symbol, Type)>,
current_program: Symbol,
struct_lookup: &dyn Fn(&Location) -> Vec<(Symbol, Type)>,
) -> Option<Self> {
let id = node_builder.next_id();

Expand Down Expand Up @@ -444,13 +445,16 @@ impl Expression {
// Structs (composite types)
Type::Composite(composite_type) => {
let path = &composite_type.path;
let members = struct_lookup(&path.absolute_path());
let members = struct_lookup(&Location::new(
composite_type.program.unwrap_or(current_program),
path.absolute_path(),
));

let struct_members = members
.into_iter()
.map(|(symbol, member_type)| {
let member_id = node_builder.next_id();
let zero_expr = Self::zero(&member_type, span, node_builder, struct_lookup)?;
let zero_expr = Self::zero(&member_type, span, node_builder, current_program, struct_lookup)?;

Some(StructVariableInitializer {
span,
Expand All @@ -474,7 +478,7 @@ impl Expression {
Type::Array(array_type) => {
let element_ty = &array_type.element_type;

let element_expr = Self::zero(element_ty, span, node_builder, struct_lookup)?;
let element_expr = Self::zero(element_ty, span, node_builder, current_program, struct_lookup)?;

Some(Expression::Repeat(
RepeatExpression { span, id, expr: element_expr, count: *array_type.length.clone() }.into(),
Expand Down
25 changes: 19 additions & 6 deletions compiler/ast/src/interpreter_value/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,9 @@ impl Value {
&self,
span: Span,
node_builder: &NodeBuilder,
current_program: Symbol,
ty: &Type,
struct_lookup: &dyn Fn(&[Symbol]) -> Vec<(Symbol, Type)>,
struct_lookup: &dyn Fn(&Location) -> Vec<(Symbol, Type)>,
) -> Option<Expression> {
use crate::{Literal, TupleExpression, UnitExpression};

Expand All @@ -850,15 +851,15 @@ impl Value {
elements: vec
.iter()
.zip(tuple_type.elements())
.map(|(val, ty)| val.to_expression(span, node_builder, ty, struct_lookup))
.map(|(val, ty)| val.to_expression(span, node_builder, current_program, ty, struct_lookup))
.collect::<Option<Vec<_>>>()?,
}
.into()
}
ValueVariants::Unsuffixed(s) => Literal::unsuffixed(s.clone(), span, id).into(),
ValueVariants::Svm(value) => match value {
SvmValueParam::Plaintext(plaintext) => {
plaintext_to_expression(plaintext, span, node_builder, ty, &struct_lookup)?
plaintext_to_expression(plaintext, span, node_builder, current_program, ty, &struct_lookup)?
}
SvmValueParam::Record(..) => return None,
SvmValueParam::Future(..) => return None,
Expand All @@ -876,8 +877,9 @@ fn plaintext_to_expression(
plaintext: &SvmPlaintext,
span: Span,
node_builder: &NodeBuilder,
current_program: Symbol,
ty: &Type,
struct_lookup: &dyn Fn(&[Symbol]) -> Vec<(Symbol, Type)>,
struct_lookup: &dyn Fn(&Location) -> Vec<(Symbol, Type)>,
) -> Option<Expression> {
use crate::{ArrayExpression, Identifier, IntegerType, Literal, StructExpression, StructVariableInitializer};

Expand Down Expand Up @@ -925,7 +927,8 @@ fn plaintext_to_expression(
return None;
};
let symbols = composite_type.path.as_symbols();
let iter_members = struct_lookup(&symbols);
let iter_members =
struct_lookup(&Location::new(composite_type.program.unwrap_or(current_program), symbols));
StructExpression {
span,
id,
Expand All @@ -946,6 +949,7 @@ fn plaintext_to_expression(
index_map.get(&svm_identifier)?,
span,
node_builder,
current_program,
&ty,
&struct_lookup,
)?),
Expand All @@ -964,7 +968,16 @@ fn plaintext_to_expression(
id,
elements: vec
.iter()
.map(|pt| plaintext_to_expression(pt, span, node_builder, &array_ty.element_type, &struct_lookup))
.map(|pt| {
plaintext_to_expression(
pt,
span,
node_builder,
current_program,
&array_ty.element_type,
&struct_lookup,
)
})
.collect::<Option<Vec<_>>>()?,
}
.into()
Expand Down
6 changes: 6 additions & 0 deletions compiler/ast/src/passes/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ pub trait ProgramConsumer {
fn consume_program(&mut self, input: Program) -> Self::Output;
}

/// A Consumer trait for a stub in the the AST.
pub trait StubConsumer {
type Output;
fn consume_stub(&mut self, input: Stub) -> Self::Output;
}

/// A Consumer trait for modules in the AST.
pub trait ModuleConsumer {
type Output;
Expand Down
31 changes: 15 additions & 16 deletions compiler/ast/src/passes/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,22 +569,16 @@ pub trait AstReconstructor {
/// A Reconstructor trait for the program represented by the AST.
pub trait ProgramReconstructor: AstReconstructor {
fn reconstruct_program(&mut self, input: Program) -> Program {
let stubs = input.stubs.into_iter().map(|(id, stub)| (id, self.reconstruct_stub(stub))).collect();
let program_scopes =
input.program_scopes.into_iter().map(|(id, scope)| (id, self.reconstruct_program_scope(scope))).collect();
Program {
imports: input
.imports
.into_iter()
.map(|(id, import)| (id, (self.reconstruct_import(import.0), import.1)))
.collect(),
stubs: input.stubs.into_iter().map(|(id, stub)| (id, self.reconstruct_stub(stub))).collect(),
modules: input.modules.into_iter().map(|(id, module)| (id, self.reconstruct_module(module))).collect(),
program_scopes,
}
let modules = input.modules.into_iter().map(|(id, module)| (id, self.reconstruct_module(module))).collect();

Program { modules, imports: input.imports, stubs, program_scopes }
}

fn reconstruct_stub(&mut self, input: Stub) -> Stub {
Stub {
fn reconstruct_aleo_program(&mut self, input: AleoProgram) -> AleoProgram {
AleoProgram {
imports: input.imports,
stub_id: input.stub_id,
consts: input.consts,
Expand All @@ -595,6 +589,15 @@ pub trait ProgramReconstructor: AstReconstructor {
}
}

fn reconstruct_stub(&mut self, input: Stub) -> Stub {
match input {
Stub::FromLeo { program, parents } => Stub::FromLeo { program: self.reconstruct_program(program), parents },
Stub::FromAleo { program, parents } => {
Stub::FromAleo { program: self.reconstruct_aleo_program(program), parents }
}
}
}

fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
ProgramScope {
program_id: input.program_id,
Expand Down Expand Up @@ -692,10 +695,6 @@ pub trait ProgramReconstructor: AstReconstructor {
}
}

fn reconstruct_import(&mut self, input: Program) -> Program {
self.reconstruct_program(input)
}

fn reconstruct_mapping(&mut self, input: Mapping) -> Mapping {
Mapping {
key_type: self.reconstruct_type(input.key_type).0,
Expand Down
16 changes: 9 additions & 7 deletions compiler/ast/src/passes/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,18 @@ pub trait ProgramVisitor: AstVisitor {
fn visit_program(&mut self, input: &Program) {
input.program_scopes.values().for_each(|scope| self.visit_program_scope(scope));
input.modules.values().for_each(|module| self.visit_module(module));
input.imports.values().for_each(|import| self.visit_import(&import.0));
input.stubs.values().for_each(|stub| self.visit_stub(stub));
}

fn visit_aleo_program(&mut self, _input: &AleoProgram) {}

fn visit_stub(&mut self, input: &Stub) {
match input {
Stub::FromLeo { program, .. } => self.visit_program(program),
Stub::FromAleo { program, .. } => self.visit_aleo_program(program),
}
}

fn visit_program_scope(&mut self, input: &ProgramScope) {
input.consts.iter().for_each(|(_, c)| self.visit_const(c));
input.structs.iter().for_each(|(_, c)| self.visit_struct(c));
Expand All @@ -353,12 +361,6 @@ pub trait ProgramVisitor: AstVisitor {
input.functions.iter().for_each(|(_, c)| self.visit_function(c));
}

fn visit_stub(&mut self, _input: &Stub) {}

fn visit_import(&mut self, input: &Program) {
self.visit_program(input)
}

fn visit_struct(&mut self, input: &Composite) {
input.const_parameters.iter().for_each(|input| self.visit_type(&input.type_));
input.members.iter().for_each(|member| self.visit_type(&member.type_));
Expand Down
8 changes: 4 additions & 4 deletions compiler/ast/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Program {
/// A map from module paths to module definitions.
pub modules: IndexMap<Vec<Symbol>, Module>,
/// A map from import names to import definitions.
pub imports: IndexMap<Symbol, (Program, Span)>,
pub imports: IndexMap<Symbol, Span>,
/// A map from program stub names to program stub scopes.
pub stubs: IndexMap<Symbol, Stub>,
/// A map from program names to program scopes.
Expand All @@ -44,15 +44,15 @@ pub struct Program {

impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (_, stub) in self.stubs.iter() {
writeln!(f, "{stub}")?;
}
for (_, module) in self.modules.iter() {
writeln!(f, "{module}")?;
}
for (id, _import) in self.imports.iter() {
writeln!(f, "import {id}.aleo;")?;
}
for (_, stub) in self.stubs.iter() {
writeln!(f, "{stub}")?;
}
for (_, program_scope) in self.program_scopes.iter() {
writeln!(f, "{program_scope}")?;
}
Expand Down
22 changes: 1 addition & 21 deletions compiler/ast/src/program/program_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! A Leo program scope consists of struct, function, and mapping definitions.

use crate::{Composite, ConstDeclaration, Constructor, Function, Indent, Mapping, ProgramId, StorageVariable, Stub};
use crate::{Composite, ConstDeclaration, Constructor, Function, Indent, Mapping, ProgramId, StorageVariable};

use leo_span::{Span, Symbol};
use serde::{Deserialize, Serialize};
Expand All @@ -43,26 +43,6 @@ pub struct ProgramScope {
pub span: Span,
}

impl From<Stub> for ProgramScope {
fn from(stub: Stub) -> Self {
Self {
program_id: stub.stub_id,
consts: stub.consts,
structs: stub.structs,
mappings: stub.mappings,
storage_variables: Vec::new(), // stubs don't have storage variables
functions: stub
.functions
.into_iter()
.map(|(symbol, function)| (symbol, Function::from(function)))
.collect(),
// A program scope constructed from a stub does not need a constructor, since they are not externally callable.
constructor: None,
span: stub.span,
}
}
}

impl fmt::Display for ProgramScope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "program {} {{", self.program_id)?;
Expand Down
Loading