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

make function arguments immutable by default

This commit is contained in:
Alexander Medvednikov
2019-08-07 08:19:27 +02:00
parent 06b8bd9382
commit 34e0b164eb
22 changed files with 107 additions and 83 deletions

View File

@ -512,8 +512,8 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
// Normal function => just its name, method => TYPE_FN.name
mut fn_name := f.name
if f.is_method {
receiver_type = receiver_type.replace('*', '')
fn_name = '${receiver_type}_${f.name}'
fn_name = receiver_type.replace('*', '') + '_' + f.name
//fn_name = '${receiver_type}_${f.name}'
}
// Generate tmp struct with args
arg_struct_name := 'thread_arg_$fn_name'

View File

@ -115,8 +115,8 @@ fn is_js_prim(typ string) bool {
typ == 'i8' || typ == 'i16' || typ == 'i32' || typ == 'i64'
}
fn (p mut Parser) decode_array(typ string) string {
typ = typ.replace('array_', '')
fn (p mut Parser) decode_array(array_type string) string {
typ := array_type.replace('array_', '')
t := p.table.find_type(typ)
fn_name := js_dec_name(typ)
// If we have `[]Profile`, have to register a Profile en(de)coder first
@ -149,8 +149,8 @@ fn js_dec_name(typ string) string {
return name
}
fn (p &Parser) encode_array(typ string) string {
typ = typ.replace('array_', '')
fn (p &Parser) encode_array(array_type string) string {
typ := array_type.replace('array_', '')
fn_name := js_enc_name(typ)
return '
o = cJSON_CreateArray();

View File

@ -669,7 +669,7 @@ fn (v V) run_compiled_executable_and_exit() {
exit(0)
}
fn (c &V) cc_windows_cross() {
fn (c mut V) cc_windows_cross() {
if !c.out_name.ends_with('.exe') {
c.out_name = c.out_name + '.exe'
}

View File

@ -1192,7 +1192,8 @@ fn (p mut Parser) statement(add_semi bool) string {
fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
p.log('assign_statement() name=$v.name tok=')
tok := p.tok
if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
//if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
if !v.is_mut && !p.pref.translated && !v.is_global{
p.error('`$v.name` is immutable')
}
if !v.is_changed {
@ -1799,7 +1800,8 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
return method.typ
}
fn (p mut Parser) index_expr(typ string, fn_ph int) string {
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
mut typ := typ_
// a[0]
v := p.expr_var
//if p.fileis('fn_test.v') {
@ -2361,8 +2363,8 @@ fn (p mut Parser) char_expr() {
}
fn format_str(str string) string {
str = str.replace('"', '\\"')
fn format_str(_str string) string {
mut str := _str.replace('"', '\\"')
$if windows {
str = str.replace('\r\n', '\\n')
}
@ -3298,9 +3300,11 @@ fn (p mut Parser) go_statement() {
fn (p mut Parser) register_var(v Var) {
if v.line_nr == 0 {
v.line_nr = p.scanner.line_nr
}
p.cur_fn.register_var(v)
//v.line_nr = p.scanner.line_nr
p.cur_fn.register_var({ v | line_nr: p.scanner.line_nr })
} else {
p.cur_fn.register_var(v)
}
}
// user:=jsdecode(User, user_json_string)

View File

@ -550,7 +550,7 @@ fn (s mut Scanner) scan() ScanRes {
fn (s &Scanner) error(msg string) {
file := s.file_path.all_after('/')
println('$file:${s.line_nr + 1} panic: $msg')
println('$file:${s.line_nr + 1} $msg')
exit(1)
}

View File

@ -242,7 +242,8 @@ fn (t mut Table) register_fn(new_fn Fn) {
t.fns[new_fn.name] = new_fn
}
fn (table &Table) known_type(typ string) bool {
fn (table &Table) known_type(typ_ string) bool {
mut typ := typ_
// 'byte*' => look up 'byte', but don't mess up fns
if typ.ends_with('*') && !typ.contains(' ') {
typ = typ.left(typ.len - 1)
@ -441,7 +442,8 @@ fn (p &Parser) find_type(name string) *Type {
return typ
}
fn (t &Table) find_type(name string) *Type {
fn (t &Table) find_type(name_ string) *Type {
mut name := name_
if name.ends_with('*') && !name.contains(' ') {
name = name.left(name.len - 1)
}
@ -454,7 +456,9 @@ fn (t &Table) find_type(name string) *Type {
return &Type{}
}
fn (p mut Parser) _check_types(got, expected string, throw bool) bool {
fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool {
mut got := got_
mut expected := expected_
p.log('check types got="$got" exp="$expected" ')
if p.pref.translated {
return true
@ -582,8 +586,7 @@ fn (p mut Parser) satisfies_interface(interface_name, _typ string, throw bool) b
fn type_default(typ string) string {
if typ.starts_with('array_') {
typ = typ.right(6)
return 'new_array(0, 1, sizeof($typ))'
return 'new_array(0, 1, sizeof( ${typ.right(6)} ))'
}
// Always set pointers to 0
if typ.ends_with('*') {
@ -776,8 +779,8 @@ fn (p mut Parser) typ_to_fmt(typ string, level int) string {
return ''
}
fn is_compile_time_const(s string) bool {
s = s.trim_space()
fn is_compile_time_const(s_ string) bool {
s := s_.trim_space()
if s == '' {
return false
}

View File

@ -7,7 +7,8 @@ module main
import strings
// fmt helpers
fn (scanner mut Scanner) fgen(s string) {
fn (scanner mut Scanner) fgen(s_ string) {
mut s := s_
if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s
}
@ -15,7 +16,8 @@ fn (scanner mut Scanner) fgen(s string) {
scanner.fmt_line_empty = false
}
fn (scanner mut Scanner) fgenln(s string) {
fn (scanner mut Scanner) fgenln(s_ string) {
mut s := s_
if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s
}