make function arguments immutable

pull/1003/head
Alexander Medvednikov 2019-07-05 00:20:59 +02:00
parent 74dbb1bce3
commit 0f0ed8d716
8 changed files with 42 additions and 33 deletions

View File

@ -443,8 +443,8 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
// Normal function => just its name, method => TYPE_FNNAME
mut fn_name := f.name
if f.is_method {
receiver_type = receiver_type.replace('*', '')
fn_name = '${receiver_type}_${f.name}'
_receiver_type := receiver_type.replace('*', '')
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(_typ string) string {
typ := _typ.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(_typ string) string {
typ := _typ.replace('array_', '')
fn_name := js_enc_name(typ)
return '
o = cJSON_CreateArray();

View File

@ -418,7 +418,7 @@ string _STR_TMP(const char *fmt, ...) {
}
}
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

@ -1044,7 +1044,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')
}
is_str := v.typ == 'string'
@ -1454,7 +1455,7 @@ fn (p mut Parser) var_expr(v Var) string {
}
// a++ and a--
if p.tok == INC || p.tok == DEC {
if !v.is_mut && !v.is_arg && !p.pref.translated {
if !v.is_mut && !p.pref.translated {
p.error('`$v.name` is immutable')
}
if typ != 'int' {
@ -1581,11 +1582,12 @@ 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 {
//if p.fileis('main.v') {
//println('index expr typ=$typ')
//}
// a[0]
mut typ := _typ
v := p.expr_var
is_map := typ.starts_with('map_')
is_str := typ == 'string'
@ -2133,8 +2135,8 @@ fn (p mut Parser) typ_to_fmt(typ string) string {
return ''
}
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')
}
@ -3077,7 +3079,8 @@ fn (p mut Parser) go_statement() {
}
}
fn (p mut Parser) register_var(v Var) {
fn (p mut Parser) register_var(var Var) {
mut v := var
if v.line_nr == 0 {
v.line_nr = p.scanner.line_nr
}
@ -3141,8 +3144,8 @@ fn (p mut Parser) js_decode() 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
}
@ -3166,15 +3169,17 @@ fn (p &Parser) building_v() bool {
///////////////////////////////////////////////////////////////////////////////
// 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
s = strings.repeat(`\t`, scanner.fmt_indent) + s
}
scanner.fmt_out.write(s)
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
}

View File

@ -184,7 +184,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)
@ -380,7 +381,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)
}
@ -393,7 +395,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 expected := _expected
mut got := _got
p.log('check types got="$got" exp="$expected" ')
if p.pref.translated {
return true
@ -518,8 +522,8 @@ 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))'
elm_type := typ.right(6)
return 'new_array(0, 1, sizeof($elm_type))'
}
// Always set pointers to 0
if typ.ends_with('*') {

View File

@ -642,14 +642,14 @@ pub fn (s string) ustring_tmp() ustring {
}
fn (u ustring) substr(start, end int) string {
start = u.runes[start]
if end >= u.runes.len {
end = u.s.len
_start := u.runes[start]
_end := if end >= u.runes.len {
u.s.len
}
else {
end = u.runes[end]
u.runes[end]
}
return u.s.substr(start, end)
return u.s.substr(_start, _end)
}
fn (u ustring) left(pos int) string {

View File

@ -262,8 +262,8 @@ fn popen(path string) *FILE {
}
// exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(cmd string) string {
cmd = '$cmd 2>&1'
pub fn exec(_cmd string) string {
cmd := '$_cmd 2>&1'
f := popen(cmd)
if isnil(f) {
// TODO optional or error code
@ -331,8 +331,8 @@ pub fn dir_exists(path string) bool {
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string) {
$if windows {
path = path.replace('/', '\\')
C.CreateDirectory(path.cstr(), 0)
_path := path.replace('/', '\\')
C.CreateDirectory(_path.cstr(), 0)
}
$else {
C.mkdir(path.cstr(), 511)// S_IRWXU | S_IRWXG | S_IRWXO

View File

@ -33,7 +33,7 @@ pub fn (b Builder) str() string {
return tos(b.buf.data, b.len)
}
pub fn (b Builder) cut(n int) {
pub fn (b mut Builder) cut(n int) {
b.len -= n
}