diff --git a/src/protocol/parser/pass_rewriting.rs b/src/protocol/parser/pass_rewriting.rs index 2b8b2229277749989eba786a202138f38a24de83..54b7c3b8d91feaafe05f565a28351992e0fffe24 100644 --- a/src/protocol/parser/pass_rewriting.rs +++ b/src/protocol/parser/pass_rewriting.rs @@ -5,6 +5,7 @@ use super::visitor::*; pub(crate) struct PassRewriting { current_scope: ScopeId, + definition_buffer: ScopedBuffer, statement_buffer: ScopedBuffer, call_expr_buffer: ScopedBuffer, expression_buffer: ScopedBuffer, @@ -15,6 +16,7 @@ impl PassRewriting { pub(crate) fn new() -> Self { Self{ current_scope: ScopeId::new_invalid(), + definition_buffer: ScopedBuffer::with_capacity(BUFFER_INIT_CAP_LARGE), statement_buffer: ScopedBuffer::with_capacity(BUFFER_INIT_CAP_SMALL), call_expr_buffer: ScopedBuffer::with_capacity(BUFFER_INIT_CAP_SMALL), expression_buffer: ScopedBuffer::with_capacity(BUFFER_INIT_CAP_SMALL), @@ -24,6 +26,23 @@ impl PassRewriting { } impl Visitor for PassRewriting { + fn visit_module(&mut self, ctx: &mut Ctx) -> VisitorResult { + let module = ctx.module(); + debug_assert_eq!(module.phase, ModuleCompilationPhase::Typed); + + let root_id = module.root_id; + let root = &ctx.heap[root_id]; + let definition_section = self.definition_buffer.start_section_initialized(&root.definitions); + for definition_index in 0..definition_section.len() { + let definition_id = definition_section[definition_index]; + self.visit_definition(ctx, definition_id)?; + } + + definition_section.forget(); + ctx.module_mut().phase = ModuleCompilationPhase::Rewritten; + return Ok(()) + } + // --- Visiting procedures fn visit_component_definition(&mut self, ctx: &mut Ctx, id: ComponentDefinitionId) -> VisitorResult { @@ -135,7 +154,7 @@ impl Visitor for PassRewriting { outer_end_block_stmt.next = end_select_stmt_id.upcast(); // --- for the scopes - link_existing_child_to_new_parent_scope(ctx, &mut self.scope_buffer, self.current_scope, outer_scope_id, select_stmt_relative_pos); + link_new_child_to_existing_parent_scope(ctx, &mut self.scope_buffer, self.current_scope, outer_scope_id, select_stmt_relative_pos); // Create statements that will create temporary variables for all of the // ports passed to the "get" calls in the select case guards. @@ -144,7 +163,6 @@ impl Visitor for PassRewriting { let mut total_num_ports = 0; let end_select_stmt_id = select_stmt.end_select; let end_select = &ctx.heap[end_select_stmt_id]; - let stmt_id_after_select_stmt = end_select.next; // Put heap IDs into temporary buffers to handle borrowing rules let mut call_id_section = self.call_expr_buffer.start_section();