Compiler passes
User can implement compiler passes in user code. Compiler passes are functions that transform the AST of a program.
Proc Implementation
This outlines the memory management of the compiler passes, where the pass is free to clone the input AST and source file, provided it calls maybeDeinit on the input AST and source file.
pub fn optimize(allocator: std.mem.Allocator, program_ast: *const SourceFile, source: *const SourceFile)
!*const SourceFile {
var report = try detector.detect(source);
defer report.deinit();
if (report.opportunities.items.len == 0) {
return source; // No changes
}
// Create optimized AST
const optimized = try ast_functional.cloneSourceFile(allocator, source);
// ... apply optimizations ...
// Clean up input
maybeDeinitAst(source);
return &optimized;
} maybeDeinitAst(source); is a helper method that checks whether the AST is the original static AST and source file, and if so, it does not call deinit on them.