Skip to content

v3.gen.c

fn cache_c_flag_input_files #

fn cache_c_flag_input_files(flags []string) []string

cache_c_flag_input_files returns forced include/macro files whose contents affect every cached object compiled with the supplied C flags.

fn cache_external_input_files #

fn cache_external_input_files(a &flat.FlatAst, vroot string, source_modules map[string]bool, target pref.Target) (map[string][]string, bool)

cache_external_input_files returns local include/embed inputs grouped by the module whose cached object incorporates their contents. Forced-include inputs affect every object and are kept in a configuration-wide group. The second result reports include forms whose dependencies cannot be resolved statically.

fn tokenize_c_flag #

fn tokenize_c_flag(value string) []string

tokenize_c_flag splits a C flag on unquoted whitespace while preserving quotes.

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
	fn_segs                        []string
	test_files                     map[string]bool
	cache_program_files            map[string]bool
	str_lits                       []string
	str_lit_ids                    map[string]int
	global_types                   map[string]types.Type
	global_raw_type_texts          map[string]string
	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_files                    map[string]string // const name -> declaring file (for import-alias type resolution)
	const_init_order               []string
	fixed_storage_consts           map[string]bool
	global_modules                 map[string]string
	global_files                   map[string]string      // qualified global name -> declaring file (for import-alias type resolution)
	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
	interface_boxed_types          map[string]bool
	interface_boxed_types_done     bool
	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_pointer_alias_by_owner   map[string]string   // exact scope binding owner -> stack local whose address is stored
	local_pointer_alias_mut_param  map[string]bool     // exact scope binding owner -> alias source is a mut parameter
	local_raw_type_by_owner        map[string]string   // exact scope binding owner -> source-level raw type text
	local_shared_storage_by_owner  map[string]bool     // exact scope binding owner -> C storage is a shared wrapper pointer
	local_fn_value_c_name_by_owner map[string]string   // exact scope binding owner -> lifted fn-literal C name
	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_typedef_names        map[string]bool
	inlined_c_fns                  map[string]bool
	inlined_c_declared_fns         map[string]bool
	c_flags                        []string
	use_system_stdint              bool
	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
	used_fn_ptr_types              map[string]bool   // signatures referenced by emitted C
	multi_return_types             []types.Type
	multi_return_type_names        map[string]bool
	multi_return_types_ready       bool
	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_variadic               map[string]bool
	fn_decl_variadic_short_counts  map[string]int
	fn_decl_shared_params          map[string][]bool
	fn_shared_params_resolved      map[string][]bool
	has_shared_params              bool
	fn_decl_mut_receivers          map[string]bool
	fn_decl_ret_types              map[string]types.Type // fn decl name (and qualified variants) -> return type
	// Const dependency analysis follows helper calls. Keep declaration indexes so
	// resolving each call does not scan the whole flattened AST.
	fn_decl_nodes_by_name         map[string]flat.NodeId
	fn_decl_nodes_by_short        map[string]flat.NodeId
	fn_decl_nodes_by_module_short map[string]flat.NodeId
	// 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
	target                       pref.Target
	output_path                  string
	output_error                 string
	c99_mode                     bool
	skip_generics                bool
	cur_fn_name                  string
	struct_default_module        string
	shadowed_global_locals       map[string]bool
	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
	conditional_branch_scopes    []&types.Scope
	conditional_branch_depths    []int
	conditional_branch_depth     int
	loop_label_depths            map[string]int
	loop_control_copybacks       []LoopControlCopyback
	map_loop_copyback_guards     []MapLoopCopybackGuard
	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
	cur_return_node_id             int = -1
	ownership_return_index         int
	ownership_seen_return_sources  map[string]bool
	ownership_propagation_index    int
	ownership_loop_control_index   int
	ownership_loop_iteration_index int
	ownership_scope_index          int
	cur_return_drops               []types.OwnershipDropEntry
	pending_return_scope_drops     []types.OwnershipDropEntry
	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
	spawn_wrapper_defs_seen        map[string]bool
	callback_wrapper_names         map[string]string
	callback_wrapper_defs          []string
	callback_wrapper_defs_seen     map[string]bool
	parallel_used                  bool
	c_name_cache                   &CNameCache = unsafe { nil }
	emitted_fn_ptr_typedefs        map[string]bool
	c_extern_refs                  map[string]bool
	c_extern_refs_ready            bool
	parallel_prepared              bool
	scoped_fn_items_scope          voidptr
	scoped_fn_output_path          string
	scoped_fn_output_paths         []string
	const_short_index              &ConstShortIndex = unsafe { nil }
	mut_recv_facts                 &FnNameFactCache = unsafe { nil }
	want_parallel_prep             bool
	cache_split                    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
	scope_parallel_workers bool
	worker_scope           voidptr
	parallel_worker_scopes []voidptr
}

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_skip_generics #

fn (mut g FlatGen) set_skip_generics(on bool)

set_skip_generics removes generic-only metadata work when reachability proved that the generated program has no generic instantiations.

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) set_target #

fn (mut g FlatGen) set_target(target pref.Target)

set_target sets the canonical code-generation target.

fn (FlatGen) set_cache_split #

fn (mut g FlatGen) set_cache_split(enabled bool)

set_cache_split enables stable cache markers and string symbols in generated C. The v3 driver uses them to split one checked program into independently cached module objects without changing regular -o file.c output.

fn (FlatGen) set_cache_program_files #

fn (mut g FlatGen) set_cache_program_files(files []string)

set_cache_program_files assigns entry-module source files to the program translation unit rather than an imported module cache object.

fn (FlatGen) set_scope_parallel_workers #

fn (mut g FlatGen) set_scope_parallel_workers(enabled bool)

set_scope_parallel_workers makes cgen helpers use disposable prealloc arenas. The caller must release them with free_parallel_worker_scopes after consuming the generated C output and cgen metadata.

fn (FlatGen) free_parallel_worker_scopes #

fn (mut g FlatGen) free_parallel_worker_scopes()

free_parallel_worker_scopes releases scratch arenas retained by joined cgen helper threads.

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_to_file_with_used_test_options #

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

gen_to_file_with_used_test_options writes the completed translation unit by transferring the builder buffer to the file writer, avoiding a second full-size string allocation.

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.