Skip to content

v3.transform

Constants #

const option_unwrap_marker = '?opt'

option_unwrap_marker tags a SmartcastContext produced by an x != none condition: variant_name holds the option's base type and the access is lowered to the option's .value field instead of a sum union field.

fn erase_generic_templates #

fn erase_generic_templates(mut a flat.FlatAst, tc &types.TypeChecker, used_fns map[string]bool) map[string]bool

fn monomorphize_with_used #

fn monomorphize_with_used(mut a flat.FlatAst, tc &types.TypeChecker, used_fns map[string]bool) map[string]bool

fn sum_eq_helper_name #

fn sum_eq_helper_name(sum_name string) string

sum_eq_helper_name is the global C-level name of the synthesized deep-equality helper for a sum type.

fn transform #

fn transform(mut a flat.FlatAst, tc &types.TypeChecker)

transform supports transform handling for transform.

fn transform_with_used #

fn transform_with_used(mut a flat.FlatAst, tc &types.TypeChecker, used_fns map[string]bool) map[string]bool

transform_with_used transforms transform with used data for transform.

fn transform_with_used_opt #

fn transform_with_used_opt(mut a flat.FlatAst, tc &types.TypeChecker, used_fns map[string]bool, want_parallel bool) (map[string]bool, bool)

transform_with_used_opt is transform_with_used with an opt-in for parallel function-body transform. It returns the augmented used-fn set and whether the function bodies were actually transformed across threads (false when parallel was not requested, the build lacks thread support, or there was too little work).

fn transform_with_used_opt_config #

fn transform_with_used_opt_config(mut a flat.FlatAst, tc &types.TypeChecker, used_fns map[string]bool, want_parallel bool, skip_generics bool) (map[string]bool, bool)

transform_with_used_opt_config is transform_with_used_opt with extra pipeline switches for self-host builds.

struct FieldInfo #

struct FieldInfo {
pub:
	name         string
	typ          string
	raw_typ      string
	default_expr flat.NodeId
	is_embedded  bool
}

FieldInfo stores field info metadata used by transform.

struct SmartcastContext #

struct SmartcastContext {
pub:
	expr_name     string // the expression being smartcast (e.g. "node")
	variant_name  string // the variant type name (e.g. "Ident")
	sum_type_name string // the parent sum type name (e.g. "Expr")
}

SmartcastContext stores smartcast context state used by transform.

struct StructInfo #

struct StructInfo {
pub:
	name      string
	module    string
	is_params bool
	fields    []FieldInfo
}

StructInfo stores struct info metadata used by transform.

struct SumEqRequest #

struct SumEqRequest {
pub:
	module string
	file   string
}

SumEqRequest records where a sum type's equality helper was first requested, so the helper body is built under that module/file resolution context.

struct Transformer #

struct Transformer {
mut:
	a                            &flat.FlatAst      = unsafe { nil }
	tc                           &types.TypeChecker = unsafe { nil }
	structs                      map[string]StructInfo
	unique_fields                map[string]string
	alias_methods                map[string]string
	globals                      map[string]string
	sum_types                    map[string][]string
	sum_variant_parents          map[string][]string
	sum_variant_names            map[string]bool
	sum_variant_fields           map[string]string
	qualified_types              map[string]string
	fn_ret_types                 map[string]string
	receiver_method_suffix_index map[string]string
	const_suffixes               map[string]string
	enum_types                   map[string][]string
	enum_backing_types           map[string]string
	cur_file                     string
	cur_module                   string
	cur_fn_name                  string
	cur_fn_ret_type              string
	cur_fn_is_generic            bool
	skip_generics                bool
	var_types                    []VarTypeBinding
	mut_param_values             map[string]bool
	pointer_value_lvalues        map[string]bool
	pointer_value_rvalues        map[string]bool
	temp_counter                 int
	pending_stmts                []flat.NodeId
	smartcast_stack              []SmartcastContext
	invalidated_smartcasts       map[string]bool
	in_call_callee               bool
	in_spawn_expr                bool
	in_const_init                bool
	in_return_expr               bool
	in_selector_base             bool
	alias_cache                  &AliasCache             = unsafe { nil }
	sum_cache                    &AliasCache             = unsafe { nil }
	generic_unresolved_cache     &GenericUnresolvedCache = unsafe { nil }
	struct_field_type_cache      &LookupCache            = unsafe { nil }
	variant_short_name_cache     &LookupCache            = unsafe { nil }
	call_param_types_decl_cache  map[string][]types.Type
	call_param_types_decl_misses map[string]bool
	call_param_types_decl_index  map[string]FnParamDeclRef
	call_param_types_index_ready bool
	used_fns                     map[string]bool
	// sum_eq_types records sum types whose deep-equality helper fn
	// (__v3_sum_eq_<name>) is called somewhere, keyed by sum name with the
	// module/file context of the requesting call site (type resolution inside
	// the helper body needs that context). The helpers are synthesized
	// serially after the (possibly parallel) transform completes.
	sum_eq_types                 map[string]SumEqRequest
	sum_eq_synthesized           map[string]bool
	interface_boxed_types        map[string]bool
	interface_boxed_types_done   bool
	interface_var_concrete_types map[string]string
	// used_struct_operator_fns holds the callee names of direct calls seen during
	// monomorphize. Infix operators on generic instances are lowered to direct calls
	// (`Vec_int__plus(a, b)`) before this pass, so an operator overload is specialized for
	// an instantiated generic struct only when its mangled name appears here — an instance
	// whose type argument never has the operator applied is not emitted with a body that
	// would fail C compilation.
	used_struct_operator_fns map[string]bool
	// active_generic_params holds the generic parameter names of the decl currently
	// being specialized/rewritten, in the same order as the inferred type `args`.
	// It lets type-text substitution map placeholders by name (so non-canonical
	// params like `D`/`F` resolve to the right arg) instead of by the positional
	// `generic_param_index` heuristic (which collapses anything outside the T/U/C
	// sequences to index 0). Empty for struct-generic specialization, which keeps
	// the legacy positional behaviour.
	active_generic_params []string
	// cloning_comptime_for_depth > 0 while a generic clone descends into a `$for` body: nested
	// generic calls there must not be specialized (the loop var members are not resolved yet).
	cloning_comptime_for_depth int
	// escaping_amp_ptrs holds the names of pointer locals `p` declared as `p := &v`
	// (v a value local) whose pointer escapes the function (is returned). V semantics
	// auto-heap such a `v`; v3 otherwise takes the address of a stack local that dies
	// on return. Recomputed per function (structural pre-pass in transform_fn_body),
	// consumed when the `p := &v` decl is transformed (RHS rewritten to a heap copy).
	escaping_amp_ptrs map[string]bool
	// escaping_amp_sources holds the source locals `v` of such `p := &v` escapes — the
	// values whose address leaves the frame. The local itself is moved to the heap at its
	// declaration (its type becomes `&T`) so a mutation between `p := &v` and `return p`
	// is observed by the caller; copying eagerly at the alias would return stale data.
	escaping_amp_sources map[string]bool
	// heaped_amp_locals records which of those sources were actually moved to the heap, so
	// the `p := &v` alias emits `p = v` (the heap pointer) instead of a fresh memdup copy.
	heaped_amp_locals                map[string]bool
	generic_specialization_args      map[string][]string
	generic_fn_specs_in_progress     map[string]bool
	generic_fn_decls_cache           map[string]GenericFnDecl
	generic_receiver_methods_by_name map[string][]string
	generic_fn_decls_ready           bool
	generic_call_spec_cache          map[int]GenericCallSpec
	generic_call_spec_misses         map[int]bool
	stringify_stack                  []string
	node_module_map_cache            []string
	node_module_map_nodes            int = -1
	// used_fns_log records names newly inserted into used_fns while the
	// late-used-fn-bodies pass runs, so that pass can tell "was this name
	// already used before the current body's transform" without cloning the
	// whole used_fns map per function (those clones dominated the pass's time
	// and, under -gc none, were never freed).
	used_fns_log        []string
	used_fns_log_active bool
	// transformed_fns[i] is set when the fn_decl at node id i has had its body
	// transformed (main pass, any thread — worker chunks are marked at merge).
	// The late-used-fn-bodies pass excludes these candidates: lowered bodies
	// surface sanitized call spellings (`seed__time_seed_array`) that the
	// used-set (holding `seed.time_seed_array`) cannot filter, which used to
	// re-transform hundreds of already-transformed bodies every build.
	transformed_fns []bool
	// Shared-base (clone-free) parallel transform: all threads operate on views
	// of the master arrays, appending into pre-partitioned capacity regions.
	// While base_write_intercept is set, in-place writes to base-range node
	// slots outside the current item's subtree range [item_range_lo,
	// item_range_hi] are dropped (workers — matching the old clone path, where
	// such writes stayed in the discarded clone) or deferred until after join
	// (master, defer_oor_writes — matching the old path where the master's
	// writes landed on the shared AST).
	base_write_intercept bool
	defer_oor_writes     bool
	shared_base_nodes    int = -1
	item_range_lo        int = -1
	item_range_hi        int = -1
	deferred_base_writes []DeferredBaseWrite
}

Transformer represents transformer data used by transform.

fn (Transformer) drain_pending #

fn (mut t Transformer) drain_pending(mut result []flat.NodeId)

drain_pending supports drain pending handling for Transformer.

fn (Transformer) find_smartcast #

fn (t &Transformer) find_smartcast(expr_name string) ?SmartcastContext

find_smartcast resolves find smartcast information for transform.

fn (Transformer) get_global_type #

fn (t &Transformer) get_global_type(name string) ?string

get_global_type returns get global type data for Transformer.

fn (Transformer) get_struct_info #

fn (t &Transformer) get_struct_info(name string) ?StructInfo

get_struct_info returns get struct info data for Transformer.

fn (Transformer) is_sum_variant #

fn (t &Transformer) is_sum_variant(name string) bool

is_sum_variant reports whether is sum variant applies in transform.

fn (Transformer) make_array_init #

fn (mut t Transformer) make_array_init(elem_type string) flat.NodeId

make_array_init builds make array init data for transform.

fn (Transformer) make_assign #

fn (mut t Transformer) make_assign(lhs flat.NodeId, rhs flat.NodeId) flat.NodeId

make_assign builds make assign data for transform.

fn (Transformer) make_assign_op #

fn (mut t Transformer) make_assign_op(lhs flat.NodeId, rhs flat.NodeId, op flat.Op) flat.NodeId

make_assign_op builds make assign op data for transform.

fn (Transformer) make_block #

fn (mut t Transformer) make_block(stmts []flat.NodeId) flat.NodeId

make_block builds make block data for transform.

fn (Transformer) make_bool_literal #

fn (mut t Transformer) make_bool_literal(value bool) flat.NodeId

make_bool_literal builds make bool literal data for transform.

fn (Transformer) make_call #

fn (mut t Transformer) make_call(fn_name string, args []flat.NodeId) flat.NodeId

make_call builds make call data for transform.

fn (Transformer) make_call_expr_typed #

fn (mut t Transformer) make_call_expr_typed(fn_expr flat.NodeId, args []flat.NodeId, typ string) flat.NodeId

make_call_expr_typed builds make call expr typed data for transform.

fn (Transformer) make_call_typed #

fn (mut t Transformer) make_call_typed(fn_name string, args []flat.NodeId, typ string) flat.NodeId

make_call_typed builds make call typed data for transform.

fn (Transformer) make_cast #

fn (mut t Transformer) make_cast(target_type string, expr flat.NodeId, typ string) flat.NodeId

make_cast builds make cast data for transform.

fn (Transformer) make_decl_assign #

fn (mut t Transformer) make_decl_assign(name string, rhs flat.NodeId) flat.NodeId

make_decl_assign builds make decl assign data for transform.

fn (Transformer) make_empty #

fn (mut t Transformer) make_empty() flat.NodeId

make_empty builds make empty data for transform.

fn (Transformer) make_expr_stmt #

fn (mut t Transformer) make_expr_stmt(expr flat.NodeId) flat.NodeId

make_expr_stmt builds make expr stmt data for transform.

fn (Transformer) make_float_literal #

fn (mut t Transformer) make_float_literal(value string) flat.NodeId

make_float_literal builds make float literal data for transform.

fn (Transformer) make_float_literal_typed #

fn (mut t Transformer) make_float_literal_typed(value string, typ string) flat.NodeId

fn (Transformer) make_ident #

fn (mut t Transformer) make_ident(name string) flat.NodeId

make_ident builds make ident data for transform.

fn (Transformer) make_if #

fn (mut t Transformer) make_if(cond flat.NodeId, then_block flat.NodeId, else_block flat.NodeId) flat.NodeId

make_if builds make if data for transform.

fn (Transformer) make_index #

fn (mut t Transformer) make_index(base flat.NodeId, index flat.NodeId, typ string) flat.NodeId

make_index builds make index data for transform.

fn (Transformer) make_infix #

fn (mut t Transformer) make_infix(op flat.Op, lhs flat.NodeId, rhs flat.NodeId) flat.NodeId

make_infix builds make infix data for transform.

fn (Transformer) make_int_literal #

fn (mut t Transformer) make_int_literal(value int) flat.NodeId

make_int_literal builds make int literal data for transform.

fn (Transformer) make_int_literal_typed #

fn (mut t Transformer) make_int_literal_typed(value string, typ string) flat.NodeId

fn (Transformer) make_map_init #

fn (mut t Transformer) make_map_init(map_type string) flat.NodeId

make_map_init builds make map init data for transform.

fn (Transformer) make_method_call #

fn (mut t Transformer) make_method_call(receiver flat.NodeId, method_name string, args []flat.NodeId) flat.NodeId

make_method_call builds make method call data for transform.

fn (Transformer) make_paren #

fn (mut t Transformer) make_paren(expr flat.NodeId) flat.NodeId

make_paren builds make paren data for transform.

fn (Transformer) make_postfix #

fn (mut t Transformer) make_postfix(expr flat.NodeId, op flat.Op) flat.NodeId

make_postfix builds make postfix data for transform.

fn (Transformer) make_prefix #

fn (mut t Transformer) make_prefix(op flat.Op, expr flat.NodeId) flat.NodeId

make_prefix builds make prefix data for transform.

fn (Transformer) make_selector #

fn (mut t Transformer) make_selector(base flat.NodeId, field string, typ string) flat.NodeId

make_selector builds make selector data for transform.

fn (Transformer) make_selector_op #

fn (mut t Transformer) make_selector_op(base flat.NodeId, field string, typ string, op flat.Op) flat.NodeId

make_selector_op builds make selector op data for transform.

fn (Transformer) make_sizeof_type #

fn (mut t Transformer) make_sizeof_type(type_name string) flat.NodeId

make_sizeof_type builds make sizeof type data for transform.

fn (Transformer) make_string_literal #

fn (mut t Transformer) make_string_literal(value string) flat.NodeId

make_string_literal builds make string literal data for transform.

fn (Transformer) make_struct_init #

fn (mut t Transformer) make_struct_init(name string) flat.NodeId

make_struct_init builds make struct init data for transform.

fn (Transformer) make_sum_tag_selector #

fn (mut t Transformer) make_sum_tag_selector(base flat.NodeId, op flat.Op) flat.NodeId

make_sum_tag_selector builds an internal selector for a sumtype discriminator.

fn (Transformer) new_temp #

fn (mut t Transformer) new_temp(prefix string) string

new_temp supports new temp handling for Transformer.

fn (Transformer) pop_smartcast #

fn (mut t Transformer) pop_smartcast()

pop_smartcast updates pop smartcast state for Transformer.

fn (Transformer) push_smartcast #

fn (mut t Transformer) push_smartcast(expr_name string, variant string, sum_type string)

push_smartcast updates push smartcast state for Transformer.

fn (Transformer) synthesize_sum_eq_helpers #

fn (mut t Transformer) synthesize_sum_eq_helpers() []string

synthesize_sum_eq_helpers generates the fn_decl for every sum type whose equality helper was requested during the transform. Building one helper body can request helpers for nested sum types, so this drains a worklist. Runs serially on the merged AST (workers only record names). Returns the names newly marked used while building the helper bodies (e.g. a payload struct's overloaded ==), so the caller can run them through the late-used-fn pass — synthesis happens after that pass, so such functions would otherwise miss body transformation.

fn (Transformer) transform_expr #

fn (mut t Transformer) transform_expr(id flat.NodeId) flat.NodeId

transform_expr transforms transform expr data for transform.

fn (Transformer) transform_lvalue #

fn (mut t Transformer) transform_lvalue(id flat.NodeId) flat.NodeId

transform_lvalue transforms transform lvalue data for transform.

fn (Transformer) transform_stmt #

fn (mut t Transformer) transform_stmt(id flat.NodeId) []flat.NodeId

transform_stmt transforms transform stmt data for transform.

fn (Transformer) transform_stmts #

fn (mut t Transformer) transform_stmts(ids []flat.NodeId) []flat.NodeId

transform_stmts transforms transform stmts data for transform.