Skip to content

v3.gen.c

fn FlatGen.new #

fn FlatGen.new() FlatGen

new creates a FlatGen value for c.

struct FlatGen #

struct FlatGen {
mut:
	sb                             strings.Builder
	indent                         int
	a                              &flat.FlatAst = unsafe { nil }
	used_fns                       map[string]bool
	used_fn_names                  []string
	fn_gen_items                   []FlatFnGenItem
	test_files                     map[string]bool
	str_lits                       []string
	str_lit_ids                    map[string]int
	global_types                   map[string]types.Type
	enum_vals                      map[string]int
	enum_value_exprs               map[string]string
	defers                         []flat.NodeId
	fn_defers                      []flat.NodeId
	fn_defer_counts                map[int]string
	defer_capture_names            []string
	defer_capture_types            map[string]types.Type
	interfaces                     map[string][]string
	const_vals                     map[string]flat.NodeId
	const_modules                  map[string]string
	const_init_order               []string
	fixed_storage_consts           map[string]bool
	global_modules                 map[string]string
	global_inits                   map[string]flat.NodeId // qualified global name -> initializer value node
	global_init_order              []string               // qualified global names, in declaration order
	enum_backing_infos             map[string]EnumBackingInfo
	iface_impls                    map[string][]string // interface name -> implementing concrete type names
	iface_type_ids                 map[string]int      // "${iface}::${concrete}" -> 1-based type id
	ierror_method_emit_names       map[string]bool     // names/lowered names of concrete IError msg/code methods
	ierror_stack_pointer_aliases   []map[string]bool   // scoped local pointer aliases to stack subobjects
	local_pointer_storage_by_owner map[string]bool     // exact scope binding owner -> C storage is already a pointer
	local_c_type_by_owner          map[string]string   // exact scope binding owner -> emitted C declaration type
	local_shared_storage_by_owner  map[string]bool     // exact scope binding owner -> C storage is a shared wrapper pointer
	sum_name_lookup                map[string]string   // full/short sum type name -> canonical sum type name
	module_init_fns                []string            // C names of module-level `init()` fns, in source order
	module_init_fn_modules         map[string]string   // C init fn name -> V module name
	module_imports                 map[string][]string // module -> imported modules
	c_directives                   []CDirective
	inlined_c_structs              map[string]bool
	inlined_c_fns                  map[string]bool
	inlined_c_declared_fns         map[string]bool
	c_flags                        []string
	libc_compat_fns                map[string]bool
	tc                             &types.TypeChecker = unsafe { nil }
	has_builtins                   bool
	tmp_count                      int
	line_start                     bool
	field_name_set                 map[string]bool   // every struct field's C name (lazy) — for const/field collision checks
	modules                        map[string]string // alias -> full module name
	fn_ptr_types                   map[string]string // fn_ptr:ret|params -> typedef name
	fixed_array_ret_wrappers       map[string]bool   // bare fixed-array c_type name -> has a return wrapper struct
	emitted_fixed_array_typedefs   map[string]bool   // bare fixed-array typedefs already written (shared across passes)
	concrete_optional_abi_fns      map[string]bool   // emitted fn names whose option/result params use Optional_T ABI
	fixed_array_typedefs_needed    map[string]FixedArrayTypedefInfo
	fixed_array_typedefs_ready     bool
	fn_decl_param_types            map[string][]types.Type
	fn_decl_shared_params          map[string][]bool
	fn_decl_mut_receivers          map[string]bool
	fn_decl_ret_types              map[string]types.Type // fn decl name (and qualified variants) -> return type
	// set of `${module}\x01${name}` for every non-generic fn decl, built once in
	// precompute_non_generic_fn_index. Replaces the former full-node scan in
	// non_generic_fn_decl_exists_in_module (O(nodes) per call, hot in cgen).
	non_generic_fn_names_by_module map[string]bool
	// indexes over tc.fn_generic_params keys, built once in
	// precompute_generic_fn_key_index. They replace the former full-map scan in
	// generic_plain_fn_base_for_call (O(generic fns) with two string allocations
	// per key, run for nearly every emitted call). The ordinal preserves the
	// map's iteration order so multi-match resolution stays byte-identical.
	generic_fn_keys_by_short     map[string][]string
	generic_fn_keys_by_cname     map[string][]string
	generic_fn_key_ordinal       map[string]int
	struct_decl_infos            map[string]StructDeclInfo
	struct_decl_short_infos      map[string]StructDeclInfo
	shared_type_names            map[string]SharedTypeInfo // __shared__ wrapper name -> wrapped type metadata
	needs_shared_runtime         bool
	const_runtime_inits          []string
	const_runtime_init_modules   []string
	runtime_inits                []string
	runtime_init_modules         []string
	compiler_vroot               string
	compiler_vexe                string
	c99_mode                     bool
	cur_fn_name                  string
	cur_param_names              []string
	cur_param_type_values        []types.Type
	cur_param_types              map[string]types.Type
	cur_concrete_optional_params map[string]bool
	cur_mut_params               map[string]bool
	cur_mut_param_owners         map[string]types.ScopeBindingOwner
	cur_fn_ret                   types.Type = types.Type(types.void_)
	cur_fn_ret_is_optional       bool
	cur_fn_ret_base              types.Type = types.Type(types.void_)
	active_locks                 []ActiveLock
	loop_depth                   int
	loop_label_depths            map[string]int
	goto_label_lock_scopes       map[string][]int
	pending_loop_label           string
	// in_return is true only while generating a `return` statement's value, so a bare
	// generic literal (`return Box{...}`) may adopt `cur_fn_ret`'s concrete instance —
	// but a literal in a local decl / argument elsewhere in the body does not.
	in_return                 bool
	expected_expr_type        types.Type = types.Type(types.void_)
	expected_enum             string
	needed_optional_types     map[string]string
	emitted_optional_types    map[string]bool
	emitted_fns               map[string]bool
	array_method_cache        map[string]string
	param_types_cache         map[string][]types.Type        // (name|fallback) -> resolved param types
	embedded_fields_by_type   map[string][]types.StructField // type name -> its embedded fields (usually empty)
	param_types_by_short      map[string][]types.Type        // method short-name suffix -> param types (fallback index)
	generic_method_candidates map[string][]GenericMethodCandidate
	spawn_wrapper_names       map[string]string
	spawn_wrapper_defs        []string
	callback_wrapper_names    map[string]string
	callback_wrapper_defs     []string
	parallel_used             bool
	c_name_cache              &CNameCache = unsafe { nil }
	// Body-independent postamble segments emitted on helper threads while the
	// fn-body workers run; spliced into the final output in the exact order
	// the serial postamble produces them.
	post_segs               []string
	emitted_fn_ptr_typedefs map[string]bool
	c_extern_refs           map[string]bool
	c_extern_refs_ready     bool
	parallel_prepared       bool
	post_str_lits_snapshot  int
	const_short_index       &ConstShortIndex = unsafe { nil }
	mut_recv_facts          &FnNameFactCache = unsafe { nil }
	want_parallel_prep      bool
	// Set when the target is built with -prealloc / -d prealloc: the bump
	// arena's base block pointer must be thread-local (matching V1's cgen),
	// or every spawned thread would race on the same arena.
	prealloc bool
}

FlatGen emits flat gen output used by c.

fn (FlatGen) was_parallel #

fn (g &FlatGen) was_parallel() bool

was_parallel reports whether the last fn codegen actually ran across threads.

fn (FlatGen) c_flags #

fn (g &FlatGen) c_flags() []string

fn (FlatGen) set_c99_mode #

fn (mut g FlatGen) set_c99_mode(enabled bool)

set_c99_mode configures whether generated C should support strict C99 builds.

fn (FlatGen) set_prealloc #

fn (mut g FlatGen) set_prealloc(on bool)

set_prealloc marks the build as using the -prealloc bump arena.

fn (FlatGen) set_compiler_vexe #

fn (mut g FlatGen) set_compiler_vexe(path string)

set_compiler_vexe sets the V executable path baked into generated test/runtime helpers.

fn (FlatGen) gen #

fn (mut g FlatGen) gen(a &flat.FlatAst) string

gen supports gen handling for FlatGen.

fn (FlatGen) gen_with_used #

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

gen_with_used emits with used output for c.

fn (FlatGen) gen_with_used_test_options #

fn (mut g FlatGen) gen_with_used_test_options(a &flat.FlatAst, used_fns map[string]bool, tc &types.TypeChecker, no_parallel bool, test_files []string) string

fn (FlatGen) gen_with_used_options #

fn (mut g FlatGen) gen_with_used_options(a &flat.FlatAst, used_fns map[string]bool, tc &types.TypeChecker, no_parallel bool) string

gen_with_used_options emits with used options output for c.