Skip to content

v2.gen.cleanc #

fn Gen.new #

fn Gen.new(files []ast.File) &Gen

fn Gen.new_with_env #

fn Gen.new_with_env(files []ast.File, env &types.Environment) &Gen

fn Gen.new_with_env_and_pref #

fn Gen.new_with_env_and_pref(files []ast.File, env &types.Environment, p &pref.Preferences) &Gen

struct Gen #

struct Gen {
	files []ast.File
	env   &types.Environment = unsafe { nil }
	pref  &pref.Preferences  = unsafe { nil }
mut:
	sb                     strings.Builder
	indent                 int
	cur_fn_scope           &types.Scope = unsafe { nil }
	cur_fn_name            string
	cur_fn_ret_type        string
	cur_fn_c_ret_type      string
	cur_module             string
	emitted_types          map[string]bool
	fn_param_is_ptr        map[string][]bool
	fn_param_types         map[string][]string
	fn_return_types        map[string]string
	runtime_local_types    map[string]string
	cur_fn_returned_idents map[string]bool
	active_generic_types   map[string]types.Type
	// Comptime $for field iteration state
	comptime_field_var      string // variable name (e.g., 'field')
	comptime_field_name     string // current field name (e.g., 'id')
	comptime_field_type     string // current field C type name
	comptime_field_raw_type types.Type = types.Struct{} // raw types.Type for comptime checks
	comptime_field_attrs    []string // current field attributes
	comptime_field_idx      int      // current field index
	comptime_val_var        string   // the struct variable being decoded (e.g., 'val')
	comptime_val_type       string   // C type of val (e.g., 'Slack')

	fixed_array_fields          map[string]bool
	fixed_array_field_elem      map[string]string
	fixed_array_globals         map[string]bool
	tuple_aliases               map[string][]string
	struct_field_types          map[string]string
	enum_value_to_enum          map[string]string
	enum_type_fields            map[string]map[string]bool
	array_aliases               map[string]bool
	map_aliases                 map[string]bool
	result_aliases              map[string]bool
	option_aliases              map[string]bool
	alias_base_types            map[string]string
	emitted_result_structs      map[string]bool
	emitted_option_structs      map[string]bool
	embedded_field_owner        map[string]string
	collected_fixed_array_types map[string]FixedArrayInfo
	collected_map_types         map[string]MapTypeInfo
	fixed_array_ret_wrappers    map[string]string
	sum_type_variants           map[string][]string
	// Interface method signatures: interface_name -> [(method_name, cast_signature), ...]
	interface_methods           map[string][]InterfaceMethodInfo
	interface_data_fields       map[string][]InterfaceDataFieldInfo
	interface_decls             map[string]ast.InterfaceDecl
	emitted_interface_bodies    map[string]bool
	interface_wrapper_specs     map[string]InterfaceWrapperSpec
	needed_interface_wrappers   map[string]bool
	ierror_wrapper_bases        map[string]bool
	needed_ierror_wrapper_bases map[string]bool
	tmp_counter                 int
	cur_fn_mut_params           map[string]bool   // names of mut params in current function
	global_var_modules          map[string]string // global var name → module name
	global_var_types            map[string]string // global var name → C type string
	primitive_type_aliases      map[string]bool   // type names that are aliases for primitive types
	emit_modules                map[string]bool   // when set, emit consts/globals/fns only for these modules
	export_const_symbols        bool
	cache_bundle_name           string
	cached_init_calls           []string
	exported_const_seen         map[string]bool
	exported_const_symbols      []ExportedConstSymbol
	cur_file_name               string
	is_module_ident_cache       map[string]bool    // per-function cache for is_module_ident results
	not_local_var_cache         map[string]bool    // per-function negative cache for get_local_var_c_type
	resolved_module_names       map[string]string  // per-function cache for resolve_module_name
	cached_env_scopes           map[string]voidptr // cache of env_scope results (avoids repeated locking)

	const_exprs                map[string]string // const name → C expression string (for inlining)
	const_types                map[string]string // const name → C type string
	runtime_const_targets      map[string]bool   // module-scoped consts initialized in __v_init_consts_*
	used_fn_keys               map[string]bool
	force_emit_fn_names        map[string]bool   // function C names that must be emitted regardless of mark_used
	export_fn_names            map[string]string // V-qualified name → export name (from @[export:] attribute)
	called_fn_names            map[string]bool
	generic_spec_index         map[string][]string                // fn_name → matching keys in env.generic_types
	late_generic_specs         map[string][]map[string]types.Type // additional comptime-discovered specs
	anon_fn_defs               []string        // lifted anonymous function definitions
	late_struct_defs           []string        // struct definitions discovered during pass 5 codegen
	pending_late_body_keys     map[string]bool // body_keys in late_struct_defs but not yet flushed to g.sb
	late_generic_str_instances []string        // c_names of late generic struct instances needing str macro check
	pass5_start_pos            int             // position in sb where pass 5 starts
	deferred_m_includes        []string        // Objective-C .m file #include lines deferred until after type definitions
	spawned_fns                map[string]bool // spawn wrapper names already emitted
	spawn_wrapper_defs         []string        // spawn wrapper struct + function definitions
	emitted_trampolines        map[string]bool // bound method trampoline names already emitted
	trampoline_defs            []string        // bound method trampoline definitions
	// @[live] hot code reloading
	live_fns                []LiveFnInfo                     // @[live] functions detected during code generation
	live_source_file        string                           // source file containing @[live] functions
	test_fn_names           []string                         // test function names collected in Pass 4
	has_main                bool                             // whether a main() function was found in Pass 4
	fn_owner_file           map[string]int                   // fn_key -> first file index (for parallel dedup)
	global_owner_file       map[string]int                   // global_name -> first file index (for parallel dedup)
	generic_struct_bindings map[string]map[string]types.Type // struct_name -> {T: concrete_type}
	// Multi-instantiation support: maps base struct C name (e.g. "json2__Node") to
	// a list of (suffix, bindings) pairs for each distinct concrete instantiation.
	// E.g. [("json2__ValueInfo", {T: ValueInfo}), ("json2__StructFieldInfo", {T: StructFieldInfo})]
	generic_struct_instances map[string][]GenericStructInstance
	c_file_fn_keys           map[string]bool // fn_key -> emitted from a .c.v file, so plain .v fallback should be skipped
	typedef_c_types          map[string]bool // C struct names with @[typedef] attribute (emit without 'struct' prefix)
	blocked_fn_keys          map[string]bool // worker-only fn keys reserved to other pass5 chunks
	cached_vhash             string          // cached git short hash for @VHASH/@VCURRENTHASH
}

fn (Gen) gen #

fn (mut g Gen) gen() string

gen generates C source from the transformed AST files (sequential).

fn (Gen) gen_finalize #

fn (mut g Gen) gen_finalize() string

gen_finalize runs post-pass-5 finalization and returns the complete C source string.

fn (Gen) gen_pass5_files #

fn (mut g Gen) gen_pass5_files(file_indices []int)

gen_pass5_files generates function bodies for a range of file indices. Used by parallel dispatch — each worker calls this with its assigned chunk.

fn (Gen) gen_pass5_post #

fn (mut g Gen) gen_pass5_post()

gen_pass5_post runs post-Pass-5 finalization (interface wrappers, live reload, map helpers).

fn (Gen) gen_pass5_pre #

fn (mut g Gen) gen_pass5_pre() []int

gen_pass5_pre runs Pass 5 sequential pre-work: extern globals and extern consts for non-emitted modules. Returns the list of file indices that need gen_file().

fn (Gen) gen_passes_1_to_4 #

fn (mut g Gen) gen_passes_1_to_4()

gen_passes_1_to_4 runs setup and passes 1-4 (types, structs, constants, forward declarations).

fn (Gen) merge_pass5_worker #

fn (mut g Gen) merge_pass5_worker(w &Gen)

merge_pass5_worker merges a parallel worker's output into the main Gen.

fn (Gen) new_pass5_worker #

fn (g &Gen) new_pass5_worker(file_indices []int, worker_id int) &Gen

new_pass5_worker creates a worker Gen for parallel Pass 5. file_indices specifies which files this worker will process. Functions owned by files outside this range are pre-marked as emitted to prevent duplicate emission across workers.

fn (Gen) set_cache_bundle_name #

fn (mut g Gen) set_cache_bundle_name(name string)

set_cache_bundle_name sets the cache bundle label used for emitting a deterministic cache-init function (e.g. __v2_cached_init_builtin).

fn (Gen) set_cached_init_calls #

fn (mut g Gen) set_cached_init_calls(calls []string)

fn (Gen) set_emit_modules #

fn (mut g Gen) set_emit_modules(modules []string)

set_emit_modules limits code emission to the provided module names. Type declarations and forward declarations are still emitted for all modules.

fn (Gen) set_export_const_symbols #

fn (mut g Gen) set_export_const_symbols(enable bool)

set_export_const_symbols controls whether emitted module const macros also get exported as linkable global symbols.

fn (Gen) set_used_fn_keys #

fn (mut g Gen) set_used_fn_keys(used map[string]bool)