Skip to content

v.checker #

Constants #

const (
	valid_comptime_if_os             = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu',
		'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'solaris',
		'haiku', 'serenity', 'vinix']
	valid_comptime_compression_types = ['none', 'zlib']
	valid_comptime_if_compilers      = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
	valid_comptime_if_platforms      = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32']
	valid_comptime_if_cpu_features   = ['x64', 'x32', 'little_endian', 'big_endian']
	valid_comptime_if_other          = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
		'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
		'interpreter', 'es5', 'profile']
	valid_comptime_not_user_defined  = all_valid_comptime_idents()
	array_builtin_methods            = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice',
		'sort', 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
// TODO: remove `byte` from this list when it is no longer supported
	reserved_type_names              = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8',
		'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune']
	vroot_is_deprecated_message      = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead'
)

fn new_checker #

fn new_checker(table &ast.Table, pref &pref.Preferences) &Checker

struct Checker #

struct Checker {
	pref &pref.Preferences // Preferences shared from V struct
pub mut:
	table              &ast.Table
	file               &ast.File = 0
	nr_errors          int
	nr_warnings        int
	nr_notices         int
	errors             []errors.Error
	warnings           []errors.Warning
	notices            []errors.Notice
	error_lines        []int // to avoid printing multiple errors for the same line
	expected_type      ast.Type
	expected_or_type   ast.Type // fn() or { 'this type' } eg. string. expected or block type
	expected_expr_type ast.Type // if/match is_expr: expected_type
	mod                string   // current module name
	const_decl         string
	const_deps         []string
	const_names        []string
	global_names       []string
	locked_names       []string // vars that are currently locked
	rlocked_names      []string // vars that are currently read-locked
	in_for_count       int      // if checker is currently in a for loop
// checked_ident  string // to avoid infinite checker loops
	should_abort              bool // when too many errors/warnings/notices are accumulated, .should_abort becomes true. It is checked in statement/expression loops, so the checker can return early, instead of wasting time.
	returns                   bool
	scope_returns             bool
	is_builtin_mod            bool // true inside the 'builtin', 'os' or 'strconv' modules; TODO: remove the need for special casing this
	is_just_builtin_mod       bool // true only inside 'builtin'
	is_generated              bool // true for `[generated] module xyz` .v files
	inside_unsafe             bool // true inside `unsafe {}` blocks
	inside_const              bool // true inside `const ( ... )` blocks
	inside_anon_fn            bool // true inside `fn() { ... }()`
	inside_ref_lit            bool // true inside `a := &something`
	inside_defer              bool // true inside `defer {}` blocks
	inside_fn_arg             bool // `a`, `b` in `a.f(b)`
	inside_ct_attr            bool // true inside `[if expr]`
	inside_comptime_for_field bool
	skip_flags                bool      // should `#flag` and `#include` be skipped
	fn_level                  int       // 0 for the top level, 1 for `fn abc() {}`, 2 for a nested fn, etc
	smartcast_mut_pos         token.Pos // match mut foo, if mut foo is Foo
	smartcast_cond_pos        token.Pos // match cond
	ct_cond_stack             []ast.Expr
mut:
	stmt_level int // the nesting level inside each stmts list;
// .stmt_level is used to check for `evaluated but not used` ExprStmts like `1 << 1`
// 1 for statements directly at each inner scope level;
// increases for `x := if cond { statement_list1} else {statement_list2}`;
// increases for `x := optfn() or { statement_list3 }`;
	files                            []ast.File
	expr_level                       int // to avoid infinite recursion segfaults due to compiler bugs
	cur_orm_ts                       ast.TypeSymbol
	error_details                    []string
	vmod_file_content                string     // needed for @VMOD_FILE, contents of the file, *NOT its path**
	loop_label                       string     // set when inside a labelled for loop
	vweb_gen_types                   []ast.Type // vweb route checks
	timers                           &util.Timers = util.get_timers()
	comptime_fields_default_type     ast.Type
	comptime_fields_type             map[string]ast.Type
	fn_scope                         &ast.Scope = voidptr(0)
	main_fn_decl_node                ast.FnDecl
	match_exhaustive_cutoff_limit    int = 10
	is_last_stmt                     bool
	prevent_sum_type_unwrapping_once bool // needed for assign new values to sum type, stopping unwrapping then
	using_new_err_struct             bool
	need_recheck_generic_fns         bool // need recheck generic fns because there are cascaded nested generic fn
	inside_sql                       bool // to handle sql table fields pseudo variables
	inside_selector_expr             bool
	inside_println_arg               bool
	inside_decl_rhs                  bool
	inside_if_guard                  bool // true inside the guard condition of `if x := opt() {}`
	comptime_call_pos                int  // needed for correctly checking use before decl for templates
}

fn (Checker) add_error_detail #

fn (mut c Checker) add_error_detail(s string)

call this before calling error or warn

fn (Checker) alias_type_decl #

fn (mut c Checker) alias_type_decl(node ast.AliasTypeDecl)

fn (Checker) array_init #

fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type

fn (Checker) assign_stmt #

fn (mut c Checker) assign_stmt(mut node ast.AssignStmt)

TODO 600 line function

fn (Checker) call_expr #

fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type

fn (Checker) cast_expr #

fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type
for bit_size, array in ast.x86_no_number_register_list { 		if name in array { 			return c.table.bitsize_to_type(bit_size) 		} 	} 	for bit_size, array in ast.x86_with_number_register_list { 		if name in array { 			return c.table.bitsize_to_type(bit_size) 		} 	} 	c.error('invalid register name: `$name`', node.pos) 	return ast.void_type }

fn (Checker) chan_init #

fn (mut c Checker) chan_init(mut node ast.ChanInit) ast.Type

fn (Checker) change_current_file #

fn (mut c Checker) change_current_file(file &ast.File)

fn (Checker) check #

fn (mut c Checker) check(ast_file_ &ast.File)

fn (Checker) check2 #

fn (mut c Checker) check2(ast_file &ast.File) []errors.Error

not used right now

fn (Checker) check_basic #

fn (mut c Checker) check_basic(got ast.Type, expected ast.Type) bool

fn (Checker) check_dup_keys #

fn (mut c Checker) check_dup_keys(node &ast.MapInit, i int)

fn (Checker) check_expected #

fn (mut c Checker) check_expected(got ast.Type, expected ast.Type) ?

fn (Checker) check_expected_arg_count #

fn (mut c Checker) check_expected_arg_count(mut node ast.CallExpr, f &ast.Fn) ?

fn (Checker) check_expected_call_arg #

fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, language ast.Language, arg ast.CallArg) ?

fn (Checker) check_expr_opt_call #

fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Type

return the actual type of the expression, once the optional is handled

fn (Checker) check_files #

fn (mut c Checker) check_files(ast_files []&ast.File)

fn (Checker) check_matching_function_symbols #

fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSymbol, exp_type_sym &ast.TypeSymbol) bool

fn (Checker) check_or_expr #

fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return_type ast.Type)

fn (Checker) check_scope_vars #

fn (mut c Checker) check_scope_vars(sc &ast.Scope)

fn (Checker) check_types #

fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool

TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged

fn (Checker) concat_expr #

fn (mut c Checker) concat_expr(mut node ast.ConcatExpr) ast.Type

fn (Checker) const_decl #

fn (mut c Checker) const_decl(mut node ast.ConstDecl)

fn (Checker) ensure_sumtype_array_has_default_value #

fn (mut c Checker) ensure_sumtype_array_has_default_value(node ast.ArrayInit)

fn (Checker) enum_decl #

fn (mut c Checker) enum_decl(mut node ast.EnumDecl)

fn (Checker) enum_val #

fn (mut c Checker) enum_val(mut node ast.EnumVal) ast.Type

.green or Color.green If a short form is used, expected_type needs to be an enum with this value.

fn (Checker) error #

fn (mut c Checker) error(message string, pos token.Pos)

fn (Checker) expand_iface_embeds #

fn (mut c Checker) expand_iface_embeds(idecl &ast.InterfaceDecl, level int, iface_embeds []ast.InterfaceEmbedding) []ast.InterfaceEmbedding

fn (Checker) expr #

fn (mut c Checker) expr(node_ ast.Expr) ast.Type

TODO node must be mut

fn (Checker) fail_if_unreadable #

fn (mut c Checker) fail_if_unreadable(expr ast.Expr, typ ast.Type, what string)

fn (Checker) find_unreachable_statements_after_noreturn_calls #

fn (mut c Checker) find_unreachable_statements_after_noreturn_calls(stmts []ast.Stmt)

fn (Checker) fn_call #

fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.Type

fn (Checker) fn_type_decl #

fn (mut c Checker) fn_type_decl(node ast.FnTypeDecl)

fn (Checker) get_base_name #

fn (mut c Checker) get_base_name(node &ast.Expr) string

fn (Checker) get_default_fmt #

fn (mut c Checker) get_default_fmt(ftyp ast.Type, typ ast.Type) u8

fn (Checker) ident #

fn (mut c Checker) ident(mut node ast.Ident) ast.Type

fn (Checker) if_expr #

fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type

fn (Checker) index_expr #

fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type

fn (Checker) infer_fn_generic_types #

fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr)

fn (Checker) infix_expr #

fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type

fn (Checker) int_lit #

fn (mut c Checker) int_lit(mut node ast.IntegerLiteral) ast.Type

fn (Checker) interface_decl #

fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl)

fn (Checker) lock_expr #

fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type

fn (Checker) map_init #

fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type

fn (Checker) mark_as_referenced #

fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool)

fn (Checker) match_expr #

fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type

fn (Checker) method_call #

fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type

fn (Checker) note #

fn (mut c Checker) note(message string, pos token.Pos)

fn (Checker) offset_of #

fn (mut c Checker) offset_of(node ast.OffsetOf) ast.Type

fn (Checker) postfix_expr #

fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) ast.Type

fn (Checker) prefix_expr #

fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type

fn (Checker) promote #

fn (mut c Checker) promote(left_type ast.Type, right_type ast.Type) ast.Type

fn (Checker) return_stmt #

fn (mut c Checker) return_stmt(mut node ast.Return)

TODO: non deferred

fn (Checker) select_expr #

fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type

fn (Checker) selector_expr #

fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type

fn (Checker) string_inter_lit #

fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type

fn (Checker) string_lit #

fn (mut c Checker) string_lit(mut node ast.StringLiteral) ast.Type

unicode character literals are limited to a maximum value of 0x10ffff https://stackoverflow.com/questions/52203351/why-unicode-is-restricted-to-0x10ffff

fn (Checker) struct_decl #

fn (mut c Checker) struct_decl(mut node ast.StructDecl)

fn (Checker) struct_init #

fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type

fn (Checker) sum_type_decl #

fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl)

fn (Checker) symmetric_check #

fn (mut c Checker) symmetric_check(left ast.Type, right ast.Type) bool

fn (Checker) type_decl #

fn (mut c Checker) type_decl(node ast.TypeDecl)

fn (Checker) unsafe_expr #

fn (mut c Checker) unsafe_expr(mut node ast.UnsafeExpr) ast.Type

fn (Checker) unwrap_generic #

fn (mut c Checker) unwrap_generic(typ ast.Type) ast.Type

fn (Checker) warn #

fn (mut c Checker) warn(s string, pos token.Pos)