leo_passes/static_single_assignment/rename_program.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
// 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::StaticSingleAssigner;
use leo_ast::{
Block,
Composite,
Function,
FunctionConsumer,
Member,
Program,
ProgramConsumer,
ProgramScope,
ProgramScopeConsumer,
StatementConsumer,
StructConsumer,
};
use leo_span::{Symbol, sym};
use indexmap::IndexMap;
impl StructConsumer for StaticSingleAssigner<'_> {
type Output = Composite;
/// Reconstructs records in the program, ordering its fields such that `owner` and is the first field.
fn consume_struct(&mut self, struct_: Composite) -> Self::Output {
match struct_.is_record {
false => struct_,
true => {
let mut members = Vec::with_capacity(struct_.members.len());
let mut member_map: IndexMap<Symbol, Member> =
struct_.members.into_iter().map(|member| (member.identifier.name, member)).collect();
// Add the owner field to the beginning of the members list.
// Note that type checking ensures that the owner field exists.
members.push(member_map.shift_remove(&sym::owner).unwrap());
// Add the remaining fields to the members list.
members.extend(member_map.into_iter().map(|(_, member)| member));
Composite { members, ..struct_ }
}
}
}
}
impl FunctionConsumer for StaticSingleAssigner<'_> {
type Output = Function;
/// Reconstructs the `Function`s in the `Program`, while allocating the appropriate `RenameTable`s.
fn consume_function(&mut self, function: Function) -> Self::Output {
// Allocate a `RenameTable` for the function.
self.push();
// There is no need to reconstruct `function.inputs`.
// However, for each input, we must add each symbol to the rename table.
for input_variable in function.input.iter() {
let identifier = input_variable.identifier();
self.rename_table.update(identifier.name, identifier.name, identifier.id);
}
let block =
Block { span: function.block.span, id: function.block.id, statements: self.consume_block(function.block) };
// Remove the `RenameTable` for the function.
self.pop();
Function {
annotations: function.annotations,
variant: function.variant,
identifier: function.identifier,
input: function.input,
output: function.output,
output_type: function.output_type,
block,
span: function.span,
id: function.id,
}
}
}
impl ProgramScopeConsumer for StaticSingleAssigner<'_> {
type Output = ProgramScope;
fn consume_program_scope(&mut self, input: ProgramScope) -> Self::Output {
self.program = Some(input.program_id.name.name);
ProgramScope {
program_id: input.program_id,
structs: input.structs.into_iter().map(|(i, s)| (i, self.consume_struct(s))).collect(),
mappings: input.mappings,
functions: input.functions.into_iter().map(|(i, f)| (i, self.consume_function(f))).collect(),
consts: input.consts,
span: input.span,
}
}
}
impl ProgramConsumer for StaticSingleAssigner<'_> {
type Output = Program;
fn consume_program(&mut self, input: Program) -> Self::Output {
Program {
imports: input
.imports
.into_iter()
.map(|(name, (import, span))| (name, (self.consume_program(import), span)))
.collect(),
stubs: input.stubs,
program_scopes: input
.program_scopes
.into_iter()
.map(|(name, scope)| (name, self.consume_program_scope(scope)))
.collect(),
}
}
}