Skip to content

v3.flat

Constants #

const empty_node = NodeId(-1)

fn child_count #

fn child_count(count int) i32

child_count converts a dynamic child count to Node's compact storage type.

fn node_kind_from_id #

fn node_kind_from_id(id int) NodeKind

node_kind_from_id converts node kind from id data for flat.

fn node_payload #

fn node_payload(generic_params []string) &NodePayload

node_payload creates an uncommon node payload, or nil for an empty list.

fn FlatAst.new #

fn FlatAst.new() FlatAst

new creates a FlatAst value for flat.

fn NodeKind.from #

fn NodeKind.from[W](input W) !NodeKind

fn Op.from #

fn Op.from[W](input W) !Op

type NodeId #

type NodeId = int

NodeId aliases node id values used by flat.

type TextId #

type TextId = u32

TextId is the stable identity of one canonical AST text value.

enum NodeKind #

enum NodeKind as u8 {
	empty
	// expressions
	int_literal
	float_literal
	bool_literal
	char_literal
	string_literal
	string_interp
	ident
	infix
	prefix
	postfix
	paren
	call
	selector
	index
	if_expr
	struct_init
	field_init
	array_literal
	array_init
	map_init
	fn_literal
	or_expr
	cast_expr
	as_expr
	enum_val
	assoc
	range
	nil_literal
	none_expr
	spawn_expr
	lock_expr
	lambda_expr
	sizeof_expr
	typeof_expr
	dump_expr
	offsetof_expr
	is_expr
	in_expr
	// statements
	expr_stmt
	assign
	decl_assign
	selector_assign
	index_assign
	return_stmt
	block
	for_stmt
	for_in_stmt
	break_stmt
	continue_stmt
	match_stmt
	match_branch
	defer_stmt
	assert_stmt
	goto_stmt
	label_stmt
	select_stmt
	select_branch
	comptime_if
	comptime_for
	asm_stmt
	// declarations
	fn_decl
	struct_decl
	field_decl
	global_decl
	const_decl
	const_field
	enum_decl
	enum_field
	type_decl
	interface_decl
	interface_field
	import_decl
	module_decl
	directive
	param
	c_fn_decl
	// top-level
	file
	sql_expr
}

NodeKind lists node kind values used by flat.

enum Op #

enum Op as u8 {
	none
	plus
	minus
	mul
	div
	mod
	eq
	ne
	lt
	gt
	le
	ge
	amp
	pipe
	xor
	left_shift
	right_shift
	right_shift_unsigned
	logical_and
	logical_or
	not
	bit_not
	assign
	plus_assign
	minus_assign
	mul_assign
	div_assign
	mod_assign
	amp_assign
	pipe_assign
	xor_assign
	left_shift_assign
	right_shift_assign
	right_shift_unsigned_assign
	inc
	dec
	dot
	arrow
	gated_index
}

Op lists op values used by flat.

struct FlatAst #

@[heap]
struct FlatAst {
pub mut:
	nodes           []Node
	children        []NodeId
	user_code_start int
	disabled_fns    map[string]bool
	export_fn_names map[string]string
	noreturn_fns    map[string]bool
	source_files    map[int]&token.File
	// source_buffers owns the storage behind zero-copy scanner strings retained
	// by AST nodes. Keeping the buffers on the AST makes the lifetime boundary
	// explicit and lets parser workers transfer ownership with their nodes.
	source_buffers []string
	// text_values/text_ids own one canonical copy of every non-empty string
	// stored in a node payload. Nodes keep string compatibility views while
	// semantic/compiler caches can use compact TextId identities.
	text_values []string
	text_ids    map[string]TextId
	worker_pool &workers.Pool = unsafe { nil }
	// specialized_fn_nodes identifies program-specific monomorphized function
	// declarations appended after parsing. Module-cache cgen keeps them with main.
	specialized_fn_nodes map[int]bool
}

FlatAst represents flat ast data used by flat.

fn (FlatAst) close_workers #

fn (mut a FlatAst) close_workers()

close_workers stops the compilation-owned persistent worker pool.

fn (FlatAst) worker_count #

fn (a &FlatAst) worker_count() int

worker_count reports the number of persistent compiler helper threads.

fn (FlatAst) worker_tasks_run #

fn (a &FlatAst) worker_tasks_run() u64

worker_tasks_run reports completed callbacks across all parallel phases.

fn (FlatAst) worker_stats #

fn (a &FlatAst) worker_stats() workers.Stats

worker_stats reports scheduling and utilization across compiler phases.

fn (FlatAst) set_node_is_mut #

fn (mut a FlatAst) set_node_is_mut(id NodeId, is_mut bool)

set_node_is_mut updates a node's mut declaration marker in place.

fn (FlatAst) intern_text #

fn (mut a FlatAst) intern_text(value string) (TextId, string)

intern_text returns the canonical AST-owned copy and stable identity of text.

fn (FlatAst) reserve_transform_texts #

fn (mut a FlatAst) reserve_transform_texts(headroom int)

reserve_transform_texts keeps canonical text-table backing in the compilation arena before a disposable transform scope starts.

fn (FlatAst) promote_transform_texts_from #

fn (mut a FlatAst) promote_transform_texts_from(start int, scope voidptr)

promote_transform_texts_from moves canonical strings inserted by a scoped transform into the current arena and rebuilds table backing only if it grew into the disposable scope.

fn (FlatAst) clone_text_table_owned #

fn (a &FlatAst) clone_text_table_owned() ([]string, map[string]TextId)

clone_text_table_owned copies the canonical text table and rebuilds its lookup map with storage owned by the current allocation arena.

fn (FlatAst) text #

fn (a &FlatAst) text(id TextId) string

text resolves a stable AST text identity.

fn (FlatAst) text_count #

fn (a &FlatAst) text_count() int

text_count returns the number of unique non-empty AST text values.

fn (FlatAst) intern_node_texts_from #

fn (mut a FlatAst) intern_node_texts_from(start int)

intern_node_texts_from canonicalizes managed payloads in nodes[start..]. This runs serially after parse/transform worker merges, so the text table itself requires no synchronization and cannot retain worker-arena storage.

fn (FlatAst) intern_metadata_texts #

fn (mut a FlatAst) intern_metadata_texts()

intern_metadata_texts canonicalizes all source-derived FlatAst map keys and values. Once this and intern_node_texts_from have run, source buffers are no longer part of the AST representation and can be released.

fn (FlatAst) source_position #

fn (a &FlatAst) source_position(pos token.Pos) ?token.Position

source_position resolves an AST source position to file/line/column metadata.

fn (FlatAst) add #

fn (mut a FlatAst) add(kind NodeKind) NodeId

add updates add state for FlatAst.

fn (FlatAst) add_id #

fn (mut a FlatAst) add_id(kind_id int) NodeId

add_id updates add id state for FlatAst.

fn (FlatAst) add_val #

fn (mut a FlatAst) add_val(kind NodeKind, value string) NodeId

add_val updates add val state for FlatAst.

fn (FlatAst) add_val_id #

fn (mut a FlatAst) add_val_id(kind_id int, value string) NodeId

add_val_id updates add val id state for FlatAst.

fn (FlatAst) add_node #

fn (mut a FlatAst) add_node(node Node) NodeId

add_node updates add node state for FlatAst.

fn (FlatAst) begin_children #

fn (mut a FlatAst) begin_children() int

begin_children supports begin children handling for FlatAst.

fn (FlatAst) add_child #

fn (mut a FlatAst) add_child(id NodeId)

add_child updates add child state for FlatAst.

fn (FlatAst) child #

fn (a &FlatAst) child(node &Node, index int) NodeId

child supports child handling for FlatAst.

fn (FlatAst) child_node #

fn (a &FlatAst) child_node(node &Node, index int) &Node

child_node supports child node handling for FlatAst.

fn (FlatAst) node #

fn (a &FlatAst) node(id NodeId) &Node

node supports node handling for FlatAst.

fn (FlatAst) children_of #

fn (a &FlatAst) children_of(node &Node) []NodeId

children_of supports children of handling for FlatAst.

fn (FlatAst) print_tree #

fn (a &FlatAst) print_tree(id NodeId, indent int)

print_tree updates print tree state for FlatAst.

struct Node #

struct Node {
pub mut:
	value          string
	typ            string
	payload        &NodePayload = unsafe { nil }
	children_start i32
	is_mut         bool
pub:
	kind                 NodeKind
	op                   Op
	skip_ownership_drops bool
	children_count       i32
	pos                  token.Pos
}

Node represents node data used by flat.

fn (Node) generic_params #

fn (n &Node) generic_params() []string

generic_params returns this node's uncommon generic/attribute metadata.

fn (Node) set_generic_params #

fn (mut n Node) set_generic_params(params []string)

set_generic_params replaces this node's uncommon managed payload.

fn (Node) with_shifted_children #

fn (n Node) with_shifted_children(shift i32) Node

with_shifted_children returns a copy of the node whose children_start is moved by shift. children_start lives in the immutable section of Node, so callers that relocate a node's children block (e.g. the parallel-transform merge) build a fresh node instead of mutating in place.

fn (Node) with_pos #

fn (n Node) with_pos(pos token.Pos) Node

with_pos returns a copy of the node with source position pos.

struct NodePayload #

@[heap]
struct NodePayload {
pub:
	generic_params []string
}

NodePayload holds the uncommon managed fields used only by declarations and a handful of lowering markers. Keeping it out of Node makes the hot flat header substantially smaller without changing string-facing phase APIs.