Skip to content

Commit

Permalink
ast, checker: change stmt(node_ ast.Stmt) to stmt(mut node ast.Stmt) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 3, 2023
1 parent ab258ae commit c1550b3
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 66 deletions.
22 changes: 12 additions & 10 deletions vlib/v/ast/ast.v
Expand Up @@ -176,9 +176,10 @@ pub const (
// `{stmts}` or `unsafe {stmts}`
pub struct Block {
pub:
stmts []Stmt
is_unsafe bool
pos token.Pos
pub mut:
stmts []Stmt
}

// | IncDecStmt k
Expand Down Expand Up @@ -1166,12 +1167,12 @@ pub enum ComptimeForKind {
pub struct ComptimeFor {
pub:
val_var string
stmts []Stmt
kind ComptimeForKind
pos token.Pos
typ_pos token.Pos
pub mut:
typ Type
stmts []Stmt
typ Type
}

pub struct ForStmt {
Expand All @@ -1193,7 +1194,6 @@ pub:
val_var string
is_range bool
high Expr // `10` in `for i in 0..10 {`
stmts []Stmt
pos token.Pos
comments []Comment
val_is_mut bool // `for mut val in vals {` means that modifying `val` will modify the array
Expand All @@ -1208,6 +1208,7 @@ pub mut:
kind Kind // array/map/string
label string // `label: for {`
scope &Scope = unsafe { nil }
stmts []Stmt
}

pub struct ForCStmt {
Expand Down Expand Up @@ -1362,9 +1363,9 @@ pub:
[minify]
pub struct DeferStmt {
pub:
stmts []Stmt
pos token.Pos
pos token.Pos
pub mut:
stmts []Stmt
defer_vars []Ident
ifdef string
idx_in_fn int = -1 // index in FnDecl.defer_stmts
Expand Down Expand Up @@ -1717,9 +1718,10 @@ pub enum OrKind {
// `or { ... }`
pub struct OrExpr {
pub:
kind OrKind
pos token.Pos
pub mut:
stmts []Stmt
kind OrKind
pos token.Pos
}

/*
Expand Down Expand Up @@ -1854,7 +1856,6 @@ pub:
is_env bool
env_pos token.Pos
is_pkgconfig bool
or_block OrExpr
pub mut:
vweb_tmpl File
left_type Type
Expand All @@ -1863,6 +1864,7 @@ pub mut:
args_var string
args []CallArg
embed_file EmbeddedFile
or_block OrExpr
}

pub struct None {
Expand Down Expand Up @@ -1916,7 +1918,6 @@ pub:
is_array bool
// is_generated indicates a statement is generated by ORM for complex queries with related tables.
is_generated bool
or_expr OrExpr
pos token.Pos
pub mut:
typ Type
Expand All @@ -1928,6 +1929,7 @@ pub mut:
table_expr TypeNode
fields []StructField
sub_structs map[int]SqlExpr
or_expr OrExpr
}

pub struct NodeError {
Expand Down
59 changes: 29 additions & 30 deletions vlib/v/checker/checker.v
Expand Up @@ -208,7 +208,7 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
for mut stmt in ast_file.stmts {
if stmt in [ast.ConstDecl, ast.ExprStmt] {
c.expr_level = 0
c.stmt(stmt)
c.stmt(mut stmt)
}
if c.should_abort {
return
Expand All @@ -219,7 +219,7 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
for mut stmt in ast_file.stmts {
if stmt is ast.GlobalDecl {
c.expr_level = 0
c.stmt(stmt)
c.stmt(mut stmt)
}
if c.should_abort {
return
Expand All @@ -230,7 +230,7 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
for mut stmt in ast_file.stmts {
if stmt !in [ast.ConstDecl, ast.GlobalDecl, ast.ExprStmt] {
c.expr_level = 0
c.stmt(stmt)
c.stmt(mut stmt)
}
if c.should_abort {
return
Expand Down Expand Up @@ -267,8 +267,8 @@ pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
// not used right now
pub fn (mut c Checker) check2(mut ast_file ast.File) []errors.Error {
c.change_current_file(ast_file)
for stmt in ast_file.stmts {
c.stmt(stmt)
for mut stmt in ast_file.stmts {
c.stmt(mut stmt)
}
return c.errors
}
Expand Down Expand Up @@ -1491,7 +1491,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
node.typ = field.typ
if node.or_block.kind == .block {
c.expected_or_type = node.typ.clear_flags(.option, .result)
c.stmts_ending_with_expression(node.or_block.stmts)
c.stmts_ending_with_expression(mut node.or_block.stmts)
c.check_or_expr(node.or_block, node.typ, c.expected_or_type, node)
c.expected_or_type = ast.void_type
}
Expand Down Expand Up @@ -1833,8 +1833,7 @@ fn (mut c Checker) check_loop_label(label string, pos token.Pos) {
c.loop_label = label
}

fn (mut c Checker) stmt(node_ ast.Stmt) {
mut node := unsafe { node_ }
fn (mut c Checker) stmt(mut node ast.Stmt) {
$if trace_checker ? {
ntype := typeof(node).replace('v.ast.', '')
eprintln('checking: ${c.file.path:-30} | pos: ${node.pos.line_str():-39} | node: ${ntype} | ${node}')
Expand All @@ -1858,13 +1857,13 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
c.assign_stmt(mut node)
}
ast.Block {
c.block(node)
c.block(mut node)
}
ast.BranchStmt {
c.branch_stmt(node)
}
ast.ComptimeFor {
c.comptime_for(node)
c.comptime_for(mut node)
}
ast.ConstDecl {
c.inside_const = true
Expand Down Expand Up @@ -1898,7 +1897,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
}
}
c.inside_defer = true
c.stmts(node.stmts)
c.stmts(mut node.stmts)
c.inside_defer = false
}
ast.EnumDecl {
Expand Down Expand Up @@ -1941,7 +1940,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
c.fn_decl(mut node)
}
ast.ForCStmt {
c.for_c_stmt(node)
c.for_c_stmt(mut node)
}
ast.ForInStmt {
c.for_in_stmt(mut node)
Expand Down Expand Up @@ -2011,14 +2010,14 @@ fn (mut c Checker) assert_stmt(node ast.AssertStmt) {
c.expected_type = cur_exp_typ
}

fn (mut c Checker) block(node ast.Block) {
fn (mut c Checker) block(mut node ast.Block) {
if node.is_unsafe {
prev_unsafe := c.inside_unsafe
c.inside_unsafe = true
c.stmts(node.stmts)
c.stmts(mut node.stmts)
c.inside_unsafe = prev_unsafe
} else {
c.stmts(node.stmts)
c.stmts(mut node.stmts)
}
}

Expand Down Expand Up @@ -2366,10 +2365,10 @@ fn (mut c Checker) import_stmt(node ast.Import) {
}

// stmts should be used for processing normal statement lists (fn bodies, for loop bodies etc).
fn (mut c Checker) stmts(stmts []ast.Stmt) {
fn (mut c Checker) stmts(mut stmts []ast.Stmt) {
old_stmt_level := c.stmt_level
c.stmt_level = 0
c.stmts_ending_with_expression(stmts)
c.stmts_ending_with_expression(mut stmts)
c.stmt_level = old_stmt_level
}

Expand All @@ -2378,7 +2377,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
// `x := opt() or { stmt1 stmt2 ExprStmt }`,
// `x := if cond { stmt1 stmt2 ExprStmt } else { stmt2 stmt3 ExprStmt }`,
// `x := match expr { Type1 { stmt1 stmt2 ExprStmt } else { stmt2 stmt3 ExprStmt }`.
fn (mut c Checker) stmts_ending_with_expression(stmts []ast.Stmt) {
fn (mut c Checker) stmts_ending_with_expression(mut stmts []ast.Stmt) {
if stmts.len == 0 {
c.scope_returns = false
return
Expand All @@ -2392,14 +2391,14 @@ fn (mut c Checker) stmts_ending_with_expression(stmts []ast.Stmt) {
line_nr: -1
}
c.stmt_level++
for i, stmt in stmts {
for i, mut stmt in stmts {
c.is_last_stmt = i == stmts.len - 1
if c.scope_returns {
if unreachable.line_nr == -1 {
unreachable = stmt.pos
}
}
c.stmt(stmt)
c.stmt(mut stmt)
if stmt is ast.GotoLabel {
unreachable = token.Pos{
line_nr: -1
Expand Down Expand Up @@ -3297,7 +3296,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
}
unwrapped_typ := typ.clear_flags(.option, .result)
c.expected_or_type = unwrapped_typ
c.stmts_ending_with_expression(node.or_expr.stmts)
c.stmts_ending_with_expression(mut node.or_expr.stmts)
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
return unwrapped_typ
}
Expand Down Expand Up @@ -3402,7 +3401,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
}
unwrapped_typ := typ.clear_flags(.option, .result)
c.expected_or_type = unwrapped_typ
c.stmts_ending_with_expression(node.or_expr.stmts)
c.stmts_ending_with_expression(mut node.or_expr.stmts)
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
return unwrapped_typ
}
Expand Down Expand Up @@ -3655,9 +3654,9 @@ fn (mut c Checker) smartcast(expr_ ast.Expr, cur_type ast.Type, to_type_ ast.Typ
fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
node.is_expr = c.expected_type != ast.void_type
node.expected_type = c.expected_type
for branch in node.branches {
c.stmt(branch.stmt)
match branch.stmt {
for mut branch in node.branches {
c.stmt(mut branch.stmt)
match mut branch.stmt {
ast.ExprStmt {
if branch.is_timeout {
if !branch.stmt.typ.is_int() {
Expand All @@ -3666,7 +3665,7 @@ fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
branch.stmt.pos)
}
} else {
if branch.stmt.expr is ast.InfixExpr {
if mut branch.stmt.expr is ast.InfixExpr {
if branch.stmt.expr.left !in [ast.Ident, ast.SelectorExpr, ast.IndexExpr] {
c.error('channel in `select` key must be predefined', branch.stmt.expr.left.pos())
}
Expand Down Expand Up @@ -3708,7 +3707,7 @@ fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
}
}
}
c.stmts(branch.stmts)
c.stmts(mut branch.stmts)
}
return ast.bool_type
}
Expand Down Expand Up @@ -3737,7 +3736,7 @@ fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type {
c.locked_names << id_name
}
}
c.stmts(node.stmts)
c.stmts(mut node.stmts)
// handle `x := rlock a { a.getval() }`
mut ret_type := ast.void_type
if node.stmts.len > 0 {
Expand Down Expand Up @@ -4026,7 +4025,7 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
}
if node.op == .arrow {
if right_sym.kind == .chan {
c.stmts_ending_with_expression(node.or_block.stmts)
c.stmts_ending_with_expression(mut node.or_block.stmts)
return right_sym.chan_info().elem_type
}
c.type_error_for_operator('<-', '`chan`', right_sym.name, node.pos)
Expand Down Expand Up @@ -4209,7 +4208,7 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
if node.or_expr.stmts.len > 0 && node.or_expr.stmts.last() is ast.ExprStmt {
c.expected_or_type = typ
}
c.stmts_ending_with_expression(node.or_expr.stmts)
c.stmts_ending_with_expression(mut node.or_expr.stmts)
c.check_expr_opt_call(node, typ)
return typ
}
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/checker/comptime.v
Expand Up @@ -149,7 +149,7 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
// check each arg expression
node.args[i].typ = c.expr(arg.expr)
}
c.stmts_ending_with_expression(node.or_block.stmts)
c.stmts_ending_with_expression(mut node.or_block.stmts)
// assume string for now
return ast.string_type
}
Expand Down Expand Up @@ -246,7 +246,7 @@ fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type {
return ast.void_type
}

fn (mut c Checker) comptime_for(node ast.ComptimeFor) {
fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
typ := c.unwrap_generic(node.typ)
sym := c.table.final_sym(typ)
if sym.kind == .placeholder || typ.has_flag(.generic) {
Expand Down Expand Up @@ -274,7 +274,7 @@ fn (mut c Checker) comptime_for(node ast.ComptimeFor) {
c.comptime_for_field_var = node.val_var
c.comptime_fields_type[node.val_var] = node.typ
c.comptime_fields_default_type = field.typ
c.stmts(node.stmts)
c.stmts(mut node.stmts)

unwrapped_expr_type := c.unwrap_generic(field.typ)
tsym := c.table.sym(unwrapped_expr_type)
Expand All @@ -294,11 +294,11 @@ fn (mut c Checker) comptime_for(node ast.ComptimeFor) {
c.comptime_enum_field_value = field
c.comptime_for_field_var = node.val_var
c.comptime_fields_type[node.val_var] = node.typ
c.stmts(node.stmts)
c.stmts(mut node.stmts)
}
}
} else {
c.stmts(node.stmts)
c.stmts(mut node.stmts)
}
}

Expand Down
6 changes: 3 additions & 3 deletions vlib/v/checker/fn.v
Expand Up @@ -379,7 +379,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
c.fn_scope = node.scope
c.stmts(node.stmts)
c.stmts(mut node.stmts)
node_has_top_return := has_top_return(node.stmts)
node.has_return = c.returns || node_has_top_return
c.check_noreturn_fn_decl(mut node)
Expand Down Expand Up @@ -463,7 +463,7 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
c.error('generic closure fn must specify type parameter, e.g. fn [foo] [T]()',
node.decl.pos)
}
c.stmts(node.decl.stmts)
c.stmts(mut node.decl.stmts)
c.fn_decl(mut node.decl)
return node.typ
}
Expand Down Expand Up @@ -514,7 +514,7 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type {
}
}
c.expected_or_type = node.return_type.clear_flag(.result)
c.stmts_ending_with_expression(node.or_block.stmts)
c.stmts_ending_with_expression(mut node.or_block.stmts)
c.expected_or_type = ast.void_type

if !c.inside_const && c.table.cur_fn != unsafe { nil } && !c.table.cur_fn.is_main
Expand Down

0 comments on commit c1550b3

Please sign in to comment.