1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

memory: handle arrays

This commit is contained in:
Alexander Medvednikov 2020-03-22 13:19:45 +01:00
parent 3a938972df
commit 4cbba8c45d
2 changed files with 20 additions and 3 deletions

View File

@ -22,6 +22,7 @@ mut:
errors []string errors []string
expected_type table.Type expected_type table.Type
fn_return_type table.Type // current function's return type fn_return_type table.Type // current function's return type
// fn_decl ast.FnDecl
} }
pub fn new_checker(table &table.Table) Checker { pub fn new_checker(table &table.Table) Checker {
@ -368,6 +369,10 @@ pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) {
if return_stmt.exprs.len == 0 { if return_stmt.exprs.len == 0 {
return return
} }
if return_stmt.exprs.len > 0 && c.fn_return_type == table.void_type {
c.error('too many arguments to return, current function does not return anything', return_stmt.pos)
return
}
expected_type := c.fn_return_type expected_type := c.fn_return_type
expected_type_sym := c.table.get_type_symbol(expected_type) expected_type_sym := c.table.get_type_symbol(expected_type)
exp_is_optional := table.type_is_optional(expected_type) exp_is_optional := table.type_is_optional(expected_type)

View File

@ -431,7 +431,7 @@ fn (g mut Gen) expr_with_cast(expr ast.Expr, got_type table.Type, exp_type table
fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
// multi return // multi return
// g.write('/*assign*/') // g.write('/*assign_stmt*/')
if assign_stmt.left.len > assign_stmt.right.len { if assign_stmt.left.len > assign_stmt.right.len {
mut return_type := table.void_type mut return_type := table.void_type
match assign_stmt.right[0] { match assign_stmt.right[0] {
@ -490,9 +490,14 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
else { else {
mut is_fixed_array_init := false mut is_fixed_array_init := false
mut is_ident := false
right_sym := g.table.get_type_symbol(assign_stmt.right_types[i])
match val { match val {
ast.ArrayInit { ast.ArrayInit {
is_fixed_array_init = g.table.get_type_symbol(it.typ).kind == .array_fixed is_fixed_array_init = right_sym.kind == .array_fixed
}
ast.Ident {
is_ident = true
} }
else {} else {}
} }
@ -501,7 +506,13 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.write('$styp ') g.write('$styp ')
} }
g.expr(ident) g.expr(ident)
if !is_fixed_array_init { if g.autofree && right_sym.kind == .array && is_ident {
// `arr1 = arr2` => `arr1 = arr2.clone()`
g.write(' = array_clone(&')
g.expr(val)
g.write(')')
}
else if !is_fixed_array_init {
g.write(' = ') g.write(' = ')
if !is_decl { if !is_decl {
g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ) g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ)
@ -674,6 +685,7 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
} }
ast.AssignExpr { ast.AssignExpr {
// g.write('/*assign_expr*/')
if ast.expr_is_blank_ident(it.left) { if ast.expr_is_blank_ident(it.left) {
if ast.expr_is_call(it.val) { if ast.expr_is_call(it.val) {
g.expr(it.val) g.expr(it.val)