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
// 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
// @[live] hot code reloading: function names and source file
live_fns []LiveFn
live_source_file string
}
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) transform_files #
fn (mut t Transformer) transform_files(files []ast.File) []ast.File
transform_files transforms all files and returns transformed copies