leo_passes/flattening/flattener.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 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::{Assigner, SymbolTable, TypeTable};
use leo_ast::{
AccessExpression,
ArrayAccess,
ArrayExpression,
ArrayType,
BinaryExpression,
BinaryOperation,
Block,
Composite,
CompositeType,
Expression,
ExpressionReconstructor,
Identifier,
IntegerType,
Literal,
Member,
MemberAccess,
Node,
NodeBuilder,
NonNegativeNumber,
ReturnStatement,
Statement,
StructExpression,
StructVariableInitializer,
TernaryExpression,
TupleAccess,
TupleExpression,
TupleType,
Type,
UnitExpression,
};
use leo_span::Symbol;
/// An expression representing a conditional to reach the current
/// point in the AST.
#[derive(Clone, Copy)]
pub enum Guard {
/// An Unconstructed guard is one representing a single conditional
/// on the stack of conditions.
Unconstructed(Identifier),
/// A Constructed guard is one which as been `And`ed with all previous
/// conditions on the stack.
///
/// We cache this so that we don't have to evaluate the same chain
/// of conditions repeatedly.
Constructed(Identifier),
}
#[derive(Clone, Copy)]
pub enum ReturnGuard {
/// There were no conditionals on the path to this return statement.
None,
/// There was a chain of conditionals on the path to this return statement,
/// and they are true iff this Identifier is true.
Unconstructed(Identifier),
/// There was a chain of conditionals on the path to this return statement.`
Constructed {
/// True iff the conditionals on the path to this return statement are true.
plain: Identifier,
/// True iff any of the guards to return statements so far encountered
/// are true. We cache this to guard asserts against early returns.
any_return: Identifier,
},
}
impl Guard {
fn identifier(self) -> Identifier {
match self {
Guard::Constructed(id) | Guard::Unconstructed(id) => id,
}
}
}
pub struct Flattener<'a> {
/// The symbol table associated with the program.
pub(crate) symbol_table: &'a SymbolTable,
/// A mapping between node IDs and their types.
pub(crate) type_table: &'a TypeTable,
/// A counter used to generate unique node IDs.
pub(crate) node_builder: &'a NodeBuilder,
/// A struct used to construct (unique) assignment statements.
pub(crate) assigner: &'a Assigner,
/// A stack of condition `Expression`s visited up to the current point in the AST.
pub(crate) condition_stack: Vec<Guard>,
/// A list containing tuples of guards and expressions associated `ReturnStatement`s.
/// A guard is an expression that evaluates to true on the execution path of the `ReturnStatement`.
/// Note that returns are inserted in the order they are encountered during a pre-order traversal of the AST.
/// Note that type checking guarantees that there is at most one return in a basic block.
pub(crate) returns: Vec<(ReturnGuard, ReturnStatement)>,
/// The program name.
pub(crate) program: Option<Symbol>,
/// Whether the function is an async function.
pub(crate) is_async: bool,
}
impl<'a> Flattener<'a> {
pub(crate) fn new(
symbol_table: &'a SymbolTable,
type_table: &'a TypeTable,
node_builder: &'a NodeBuilder,
assigner: &'a Assigner,
) -> Self {
Self {
symbol_table,
type_table,
node_builder,
assigner,
condition_stack: Vec::new(),
returns: Vec::new(),
program: None,
is_async: false,
}
}
/// Construct an early return guard.
///
/// That is, an Identifier assigned to a boolean that is true iff some early return was taken.
pub(crate) fn construct_early_return_guard(&mut self) -> Option<(Identifier, Vec<Statement>)> {
if self.returns.is_empty() {
return None;
}
if self.returns.iter().any(|g| matches!(g.0, ReturnGuard::None)) {
// There was a return with no conditions, so we should simple return True.
let place = Identifier {
name: self.assigner.unique_symbol("true", "$"),
span: Default::default(),
id: self.node_builder.next_id(),
};
let statement = self.simple_assign_statement(
place,
Expression::Literal(Literal::Boolean(true, Default::default(), self.node_builder.next_id())),
);
return Some((place, vec![statement]));
}
// All guards up to a certain point in the stack should be constructed.
// Find the first unconstructed one.
let start_i = (0..self.returns.len())
.rev()
.take_while(|&i| matches!(self.returns[i].0, ReturnGuard::Unconstructed(_)))
.last()
.unwrap_or(self.returns.len());
let mut statements = Vec::with_capacity(self.returns.len() - start_i);
for i in start_i..self.returns.len() {
let ReturnGuard::Unconstructed(identifier) = self.returns[i].0 else {
unreachable!("We assured above that all guards after the index are Unconstructed.");
};
if i == 0 {
self.returns[i].0 = ReturnGuard::Constructed { plain: identifier, any_return: identifier };
continue;
}
let ReturnGuard::Constructed { any_return: previous_identifier, .. } = self.returns[i - 1].0 else {
unreachable!("We're always at an index where previous guards were Constructed.");
};
let identifier_expression = Expression::Identifier(identifier);
let previous_expression = Expression::Identifier(previous_identifier);
// Construct an Or of the two expressions.
let binary = Expression::Binary(BinaryExpression {
op: BinaryOperation::Or,
left: Box::new(previous_expression),
right: Box::new(identifier_expression),
span: Default::default(),
id: {
let id = self.node_builder.next_id();
self.type_table.insert(id, Type::Boolean);
id
},
});
// Assign that Or to a new Identifier.
let place = Identifier {
name: self.assigner.unique_symbol("guard", "$"),
span: Default::default(),
id: self.node_builder.next_id(),
};
statements.push(self.simple_assign_statement(place, binary));
// Make that assigned Identifier the constructed guard.
self.returns[i].0 = ReturnGuard::Constructed { plain: identifier, any_return: place };
}
let ReturnGuard::Constructed { any_return, .. } = self.returns.last().unwrap().0 else {
unreachable!("Above we made all guards Constructed.");
};
Some((any_return, statements))
}
/// Construct a guard from the current state of the condition stack.
///
/// That is, a boolean expression which is true iff we've followed the branches
/// that led to the current point in the Leo code.
pub(crate) fn construct_guard(&mut self) -> Option<(Identifier, Vec<Statement>)> {
if self.condition_stack.is_empty() {
return None;
}
// All guards up to a certain point in the stack should be constructed.
// Find the first unconstructed one. Start the search at the end so we
// don't repeatedly traverse the whole stack with repeated calls to this
// function.
let start_i = (0..self.condition_stack.len())
.rev()
.take_while(|&i| matches!(self.condition_stack[i], Guard::Unconstructed(_)))
.last()
.unwrap_or(self.condition_stack.len());
let mut statements = Vec::with_capacity(self.condition_stack.len() - start_i);
for i in start_i..self.condition_stack.len() {
let identifier = self.condition_stack[i].identifier();
if i == 0 {
self.condition_stack[0] = Guard::Constructed(identifier);
continue;
}
let previous = self.condition_stack[i - 1].identifier();
let identifier_expression = Expression::Identifier(identifier);
let previous_expression = Expression::Identifier(previous);
// Construct an And of the two expressions.
let binary = Expression::Binary(BinaryExpression {
op: BinaryOperation::And,
left: Box::new(previous_expression),
right: Box::new(identifier_expression),
span: Default::default(),
id: {
// Create a new node ID for the binary expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, Type::Boolean);
id
},
});
// Assign that And to a new Identifier.
let place = Identifier {
name: self.assigner.unique_symbol("guard", "$"),
span: Default::default(),
id: self.node_builder.next_id(),
};
statements.push(self.simple_assign_statement(place, binary));
// Make that assigned Identifier the constructed guard.
self.condition_stack[i] = Guard::Constructed(place);
}
Some((self.condition_stack.last().unwrap().identifier(), statements))
}
/// Fold guards and expressions into a single expression.
/// Note that this function assumes that at least one guard is present.
pub(crate) fn fold_guards(
&mut self,
prefix: &str,
mut guards: Vec<(Option<Expression>, Expression)>,
) -> (Expression, Vec<Statement>) {
// Type checking guarantees that there exists at least one return statement in the function body.
let (_, last_expression) = guards.pop().unwrap();
match last_expression {
// If the expression is a unit expression, then return it directly.
Expression::Unit(_) => (last_expression, Vec::new()),
// Otherwise, fold the guards and expressions into a single expression.
_ => {
// Produce a chain of ternary expressions and assignments for the guards.
let mut statements = Vec::with_capacity(guards.len());
// Helper to construct and store ternary assignments. e.g `$ret$0 = $var$0 ? $var$1 : $var$2`
let mut construct_ternary_assignment =
|guard: Expression, if_true: Expression, if_false: Expression| {
let place = Identifier {
name: self.assigner.unique_symbol(prefix, "$"),
span: Default::default(),
id: self.node_builder.next_id(),
};
let (value, stmts) = self.reconstruct_ternary(TernaryExpression {
id: {
// Create a new node ID for the ternary expression.
let id = self.node_builder.next_id();
// Get the type of the node ID.
let type_ = match self.type_table.get(&if_true.id()) {
Some(type_) => type_,
None => unreachable!("Type checking guarantees that all expressions have a type."),
};
// Set the type of the node ID.
self.type_table.insert(id, type_);
id
},
condition: Box::new(guard),
if_true: Box::new(if_true),
if_false: Box::new(if_false),
span: Default::default(),
});
statements.extend(stmts);
match &value {
// If the expression is a tuple, then use it directly.
// This must be done to ensure that intermediate tuple assignments are not created.
Expression::Tuple(_) => value,
// Otherwise, assign the expression to a variable and return the variable.
_ => {
statements.push(self.simple_assign_statement(place, value));
Expression::Identifier(place)
}
}
};
let expression = guards.into_iter().rev().fold(last_expression, |acc, (guard, expr)| match guard {
None => unreachable!("All expressions except for the last one must have a guard."),
// Note that type checking guarantees that all expressions have the same type.
Some(guard) => construct_ternary_assignment(guard, expr, acc),
});
(expression, statements)
}
}
}
/// A wrapper around `assigner.unique_simple_assign_statement` that updates `self.structs`.
pub(crate) fn unique_simple_assign_statement(&mut self, expr: Expression) -> (Identifier, Statement) {
// Create a new variable for the expression.
let name = self.assigner.unique_symbol("$var", "$");
// Construct the lhs of the assignment.
let place = Identifier { name, span: Default::default(), id: self.node_builder.next_id() };
// Construct the assignment statement.
let statement = self.simple_assign_statement(place, expr);
(place, statement)
}
/// A wrapper around `assigner.simple_assign_statement` that tracks the type of the lhs.
pub(crate) fn simple_assign_statement(&mut self, lhs: Identifier, rhs: Expression) -> Statement {
// Update the type table.
let type_ = match self.type_table.get(&rhs.id()) {
Some(type_) => type_,
None => unreachable!("Type checking guarantees that all expressions have a type."),
};
self.type_table.insert(lhs.id(), type_);
// Construct the statement.
self.assigner.simple_assign_statement(lhs, rhs, self.node_builder.next_id())
}
/// Folds a list of return statements into a single return statement and adds the produced statements to the block.
pub(crate) fn fold_returns(&mut self, block: &mut Block, returns: Vec<(Option<Expression>, ReturnStatement)>) {
// If the list of returns is not empty, then fold them into a single return statement.
if !returns.is_empty() {
let mut return_expressions = Vec::with_capacity(returns.len());
// Aggregate the return expressions and finalize arguments and their respective guards.
for (guard, return_statement) in returns {
return_expressions.push((guard.clone(), return_statement.expression));
}
// Fold the return expressions into a single expression.
let (expression, stmts) = self.fold_guards("$ret", return_expressions);
// Add all of the accumulated statements to the end of the block.
block.statements.extend(stmts);
// Add the `ReturnStatement` to the end of the block.
block.statements.push(Statement::Return(ReturnStatement {
expression,
span: Default::default(),
id: self.node_builder.next_id(),
}));
}
// Otherwise, push a dummy return statement to the end of the block.
else {
block.statements.push(Statement::Return(ReturnStatement {
expression: {
let id = self.node_builder.next_id();
Expression::Unit(UnitExpression { span: Default::default(), id })
},
span: Default::default(),
id: self.node_builder.next_id(),
}));
}
}
pub(crate) fn ternary_array(
&mut self,
array: &ArrayType,
condition: &Expression,
first: &Identifier,
second: &Identifier,
) -> (Expression, Vec<Statement>) {
// Initialize a vector to accumulate any statements generated.
let mut statements = Vec::new();
// For each array element, construct a new ternary expression.
let elements = (0..array.length())
.map(|i| {
// Create an assignment statement for the first access expression.
let (first, stmt) =
self.unique_simple_assign_statement(Expression::Access(AccessExpression::Array(ArrayAccess {
array: Box::new(Expression::Identifier(*first)),
index: Box::new(Expression::Literal(Literal::Integer(
IntegerType::U32,
i.to_string(),
Default::default(),
{
// Create a new node ID for the literal.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, Type::Integer(IntegerType::U32));
id
},
))),
span: Default::default(),
id: {
// Create a new node ID for the access expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, array.element_type().clone());
id
},
})));
statements.push(stmt);
// Create an assignment statement for the second access expression.
let (second, stmt) =
self.unique_simple_assign_statement(Expression::Access(AccessExpression::Array(ArrayAccess {
array: Box::new(Expression::Identifier(*second)),
index: Box::new(Expression::Literal(Literal::Integer(
IntegerType::U32,
i.to_string(),
Default::default(),
{
// Create a new node ID for the literal.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, Type::Integer(IntegerType::U32));
id
},
))),
span: Default::default(),
id: {
// Create a new node ID for the access expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, array.element_type().clone());
id
},
})));
statements.push(stmt);
// Recursively reconstruct the ternary expression.
let (expression, stmts) = self.reconstruct_ternary(TernaryExpression {
condition: Box::new(condition.clone()),
// Access the member of the first expression.
if_true: Box::new(Expression::Identifier(first)),
// Access the member of the second expression.
if_false: Box::new(Expression::Identifier(second)),
span: Default::default(),
id: {
// Create a new node ID for the ternary expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, array.element_type().clone());
id
},
});
// Accumulate any statements generated.
statements.extend(stmts);
expression
})
.collect();
// Construct the array expression.
let (expr, stmts) = self.reconstruct_array(ArrayExpression {
elements,
span: Default::default(),
id: {
// Create a node ID for the array expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, Type::Array(array.clone()));
id
},
});
// Accumulate any statements generated.
statements.extend(stmts);
// Create a new assignment statement for the array expression.
let (identifier, statement) = self.unique_simple_assign_statement(expr);
statements.push(statement);
(Expression::Identifier(identifier), statements)
}
pub(crate) fn ternary_struct(
&mut self,
struct_: &Composite,
condition: &Expression,
first: &Identifier,
second: &Identifier,
) -> (Expression, Vec<Statement>) {
// Initialize a vector to accumulate any statements generated.
let mut statements = Vec::new();
// For each struct member, construct a new ternary expression.
let members = struct_
.members
.iter()
.map(|Member { identifier, type_, .. }| {
// Create an assignment statement for the first access expression.
let (first, stmt) =
self.unique_simple_assign_statement(Expression::Access(AccessExpression::Member(MemberAccess {
inner: Box::new(Expression::Identifier(*first)),
name: *identifier,
span: Default::default(),
id: {
// Create a new node ID for the access expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, type_.clone());
id
},
})));
statements.push(stmt);
// Create an assignment statement for the second access expression.
let (second, stmt) =
self.unique_simple_assign_statement(Expression::Access(AccessExpression::Member(MemberAccess {
inner: Box::new(Expression::Identifier(*second)),
name: *identifier,
span: Default::default(),
id: {
// Create a new node ID for the access expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, type_.clone());
id
},
})));
statements.push(stmt);
// Recursively reconstruct the ternary expression.
let (expression, stmts) = self.reconstruct_ternary(TernaryExpression {
condition: Box::new(condition.clone()),
// Access the member of the first expression.
if_true: Box::new(Expression::Identifier(first)),
// Access the member of the second expression.
if_false: Box::new(Expression::Identifier(second)),
span: Default::default(),
id: {
// Create a new node ID for the ternary expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, type_.clone());
id
},
});
// Accumulate any statements generated.
statements.extend(stmts);
StructVariableInitializer {
identifier: *identifier,
expression: Some(expression),
span: Default::default(),
id: self.node_builder.next_id(),
}
})
.collect();
let (expr, stmts) = self.reconstruct_struct_init(StructExpression {
name: struct_.identifier,
members,
span: Default::default(),
id: {
// Create a new node ID for the struct expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table
.insert(id, Type::Composite(CompositeType { id: struct_.identifier, program: struct_.external }));
id
},
});
// Accumulate any statements generated.
statements.extend(stmts);
// Create a new assignment statement for the struct expression.
let (identifier, statement) = self.unique_simple_assign_statement(expr);
statements.push(statement);
(Expression::Identifier(identifier), statements)
}
pub(crate) fn ternary_tuple(
&mut self,
tuple_type: &TupleType,
condition: &Expression,
first: &Identifier,
second: &Identifier,
) -> (Expression, Vec<Statement>) {
// Initialize a vector to accumulate any statements generated.
let mut statements = Vec::new();
// For each tuple element, construct a new ternary expression.
let elements = tuple_type
.elements()
.iter()
.enumerate()
.map(|(i, type_)| {
// Create an assignment statement for the first access expression.
let (first, stmt) =
self.unique_simple_assign_statement(Expression::Access(AccessExpression::Tuple(TupleAccess {
tuple: Box::new(Expression::Identifier(*first)),
index: NonNegativeNumber::from(i),
span: Default::default(),
id: {
// Create a new node ID for the access expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, type_.clone());
id
},
})));
statements.push(stmt);
// Create an assignment statement for the second access expression.
let (second, stmt) =
self.unique_simple_assign_statement(Expression::Access(AccessExpression::Tuple(TupleAccess {
tuple: Box::new(Expression::Identifier(*second)),
index: NonNegativeNumber::from(i),
span: Default::default(),
id: {
// Create a new node ID for the access expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, type_.clone());
id
},
})));
statements.push(stmt);
// Recursively reconstruct the ternary expression.
let (expression, stmts) = self.reconstruct_ternary(TernaryExpression {
condition: Box::new(condition.clone()),
// Access the member of the first expression.
if_true: Box::new(Expression::Identifier(first)),
// Access the member of the second expression.
if_false: Box::new(Expression::Identifier(second)),
span: Default::default(),
id: {
// Create a new node ID for the ternary expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, type_.clone());
id
},
});
// Accumulate any statements generated.
statements.extend(stmts);
expression
})
.collect();
// Construct the tuple expression.
let tuple = TupleExpression {
elements,
span: Default::default(),
id: {
// Create a new node ID for the tuple expression.
let id = self.node_builder.next_id();
// Set the type of the node ID.
self.type_table.insert(id, Type::Tuple(tuple_type.clone()));
id
},
};
let (expr, stmts) = self.reconstruct_tuple(tuple.clone());
// Accumulate any statements generated.
statements.extend(stmts);
// Create a new assignment statement for the tuple expression.
let (identifier, statement) = self.unique_simple_assign_statement(expr);
statements.push(statement);
(Expression::Identifier(identifier), statements)
}
}