v2.transformer #
fn Transformer.new #
fn Transformer.new(files []ast.File, env &types.Environment) &Transformer
fn Transformer.new_with_pref #
fn Transformer.new_with_pref(files []ast.File, env &types.Environment, p &pref.Preferences) &Transformer
struct Transformer #
struct Transformer {
mut:
pref &pref.Preferences = unsafe { nil }
env &types.Environment
// Current scope for type lookups (walks up scope chain)
scope &types.Scope = unsafe { nil }
// Function root scope for registering transformer-created temp variables
// This allows cleanc to look up temp variable types from the environment
fn_root_scope &types.Scope = unsafe { nil }
// Current module for scope lookups
cur_module string
// Temp variable counter for desugaring
temp_counter int
// Counter for synthesized positions (uses negative values to avoid collision)
synth_pos_counter int = -1
// Track needed auto-generated str functions (type_name -> elem_type for arrays)
needed_str_fns map[string]string
// Track needed auto-generated array helper functions
needed_array_contains_fns map[string]ArrayMethodInfo
needed_array_index_fns map[string]ArrayMethodInfo
needed_array_last_index_fns map[string]ArrayMethodInfo
// Current function's return type name (for sum type wrapping in returns)
cur_fn_ret_type_name string
// Tracks whether the current function returns Option/Result.
// Some transformations use this to avoid wrapping plain sumtype returns.
cur_fn_returns_option bool
cur_fn_returns_result bool
// When set, match branch values should be wrapped in this sum type
// (used when a match expression is returned from a function with sum type return)
sumtype_return_wrap string
// Smart cast context stack - supports nested smart casts
smartcast_stack []SmartcastContext
// Functions that should be elided (conditional compilation, e.g. @[if verbose ?])
elided_fns map[string]bool
// Runtime const initializers grouped by module, preserving module discovery order.
runtime_const_inits_by_mod map[string][]RuntimeConstInit
runtime_const_modules []string
runtime_const_init_fn_name map[string]string
// Resolved replacement for compile-time pseudo variable @VMODROOT.
comptime_vmodroot string
// Statements generated by expression-level expansions (e.g. filter/map)
// that must be hoisted before the current statement in transform_stmts.
pending_stmts []ast.Stmt
// When true, skip lowering value-position IfExpr to temp variable.
// Set during contexts that already handle IfExpr RHS (e.g. decl_assign).
skip_if_value_lowering bool
// For native backends: map interface variable names to their concrete type names.
// When we see `shape1 := Shape(rect)`, record shape1 → "Rectangle".
// Used to rewrite interface method calls to direct concrete calls.
interface_concrete_types map[string]string
// Track needed auto-generated sort comparator functions
needed_sort_fns map[string]SortComparatorInfo
needed_enum_str_fns map[string]types.Enum
// Track needed go wrapper functions (go call -> goroutine_create lowering)
needed_go_wrappers map[string]GoWrapperInfo
// Track whether post-pass should inject the synthetic embed_file helper type.
needed_embed_file_helper bool
// Override array element types for variables whose checker-inferred type is wrong
// (e.g. .map(fn_name) typed as []voidptr instead of []ReturnType)
array_elem_type_overrides map[string]string
// File set for resolving positions to line numbers (for assert messages)
file_set &token.FileSet = unsafe { nil }
// Current file and function name (for assert messages)
cur_file_name string
cur_fn_name_str string
cur_fn_recv_prefix string // C prefix for current method's receiver type (e.g., "ui__Window")
cur_fn_recv_param string // Receiver parameter name (e.g., "w")
// @[live] hot code reloading: function names and source file
live_fns []LiveFn
live_source_file string
// Cached scope/method/fn_scope snapshots for lock-free parallel access.
// Populated once in pre_pass from the shared Environment fields.
cached_scopes map[string]&types.Scope
cached_methods map[string][]&types.Fn
cached_fn_scopes map[string]&types.Scope
// Accumulated synth types for deferred application (thread-safe).
// Instead of writing directly to env.set_expr_type during parallel transform,
// store here and apply after merge.
synth_types map[int]types.Type
}
Transformer performs AST-level transformations to simplify and normalize code before codegen. This avoids duplicating transformation logic across multiple backends (SSA, cleanc, etc.)
fn (Transformer) set_file_set #
fn (mut t Transformer) set_file_set(fs &token.FileSet)
fn (Transformer) new_worker_clone #
fn (t &Transformer) new_worker_clone(worker_idx int) &Transformer
new_worker_clone creates a lightweight Transformer that shares read-only state (env, pref, elided_fns, comptime_vmodroot, file_set) but has its own accumulator maps for thread-safe per-file transformation. worker_idx offsets synth_pos_counter so workers don't generate conflicting IDs.
fn (Transformer) merge_worker #
fn (mut t Transformer) merge_worker(w &Transformer)
merge_worker merges accumulated state from a worker transformer into this one.
fn (Transformer) transform_file_pub #
fn (mut t Transformer) transform_file_pub(file ast.File) ast.File
transform_file_standalone transforms a single file, for use in parallel workers.
fn (Transformer) set_synth_pos_counter #
fn (mut t Transformer) set_synth_pos_counter(val int)
fn (Transformer) pre_pass #
fn (mut t Transformer) pre_pass(files []ast.File)
pre_pass runs the sequential pre-pass: builds elided_fns and collects runtime const inits.
fn (Transformer) post_pass #
fn (mut t Transformer) post_pass(mut result []ast.File)
post_pass runs the sequential post-pass: injects runtime const init fns, generated functions, test main, live reload, and propagates types.
fn (Transformer) transform_files #
fn (mut t Transformer) transform_files(files []ast.File) []ast.File
transform_files transforms all files and returns transformed copies