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
// Copyright (C) 2019-2024 Aleo Systems 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;
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<Expression>,
/// 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<(Option<Expression>, 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,
}
}
/// Clears the state associated with `ReturnStatements`, returning the ones that were previously stored.
pub(crate) fn clear_early_returns(&mut self) -> Vec<(Option<Expression>, ReturnStatement)> {
core::mem::take(&mut self.returns)
}
/// Constructs a guard from the current state of the condition stack.
pub(crate) fn construct_guard(&mut self) -> Option<Expression> {
match self.condition_stack.is_empty() {
true => None,
false => {
let (first, rest) = self.condition_stack.split_first().unwrap();
Some(rest.iter().cloned().fold(first.clone(), |acc, condition| {
// Construct the binary expression.
Expression::Binary(BinaryExpression {
op: BinaryOperation::And,
left: Box::new(acc),
right: Box::new(condition),
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
},
})
}))
}
}
}
/// 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)
}
}