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

Revert "make function arguments immutable"

This reverts commit 0f0ed8d716.
This commit is contained in:
Alexander Medvednikov 2019-07-05 02:44:22 +02:00
parent 0f0ed8d716
commit d47e2f113f
8 changed files with 33 additions and 42 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 // Normal function => just its name, method => TYPE_FNNAME
mut fn_name := f.name mut fn_name := f.name
if f.is_method { if f.is_method {
_receiver_type := receiver_type.replace('*', '') receiver_type = receiver_type.replace('*', '')
fn_name = '${_receiver_type}_${f.name}' fn_name = '${receiver_type}_${f.name}'
} }
// Generate tmp struct with args // Generate tmp struct with args
arg_struct_name := 'thread_arg_$fn_name' 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' typ == 'i8' || typ == 'i16' || typ == 'i32' || typ == 'i64'
} }
fn (p mut Parser) decode_array(_typ string) string { fn (p mut Parser) decode_array(typ string) string {
typ := _typ.replace('array_', '') typ = typ.replace('array_', '')
t := p.table.find_type(typ) t := p.table.find_type(typ)
fn_name := js_dec_name(typ) fn_name := js_dec_name(typ)
// If we have `[]Profile`, have to register a Profile en(de)coder first // 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 return name
} }
fn (p &Parser) encode_array(_typ string) string { fn (p &Parser) encode_array(typ string) string {
typ := _typ.replace('array_', '') typ = typ.replace('array_', '')
fn_name := js_enc_name(typ) fn_name := js_enc_name(typ)
return ' return '
o = cJSON_CreateArray(); o = cJSON_CreateArray();

View File

@ -418,7 +418,7 @@ string _STR_TMP(const char *fmt, ...) {
} }
} }
fn (c mut V) cc_windows_cross() { fn (c &V) cc_windows_cross() {
if !c.out_name.ends_with('.exe') { if !c.out_name.ends_with('.exe') {
c.out_name = c.out_name + '.exe' c.out_name = c.out_name + '.exe'
} }

View File

@ -1044,8 +1044,7 @@ fn (p mut Parser) statement(add_semi bool) string {
fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) { fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
p.log('assign_statement() name=$v.name tok=') p.log('assign_statement() name=$v.name tok=')
tok := p.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') p.error('`$v.name` is immutable')
} }
is_str := v.typ == 'string' is_str := v.typ == 'string'
@ -1455,7 +1454,7 @@ fn (p mut Parser) var_expr(v Var) string {
} }
// a++ and a-- // a++ and a--
if p.tok == INC || p.tok == DEC { if p.tok == INC || p.tok == DEC {
if !v.is_mut && !p.pref.translated { if !v.is_mut && !v.is_arg && !p.pref.translated {
p.error('`$v.name` is immutable') p.error('`$v.name` is immutable')
} }
if typ != 'int' { if typ != 'int' {
@ -1582,12 +1581,11 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
return method.typ 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') { //if p.fileis('main.v') {
//println('index expr typ=$typ') //println('index expr typ=$typ')
//} //}
// a[0] // a[0]
mut typ := _typ
v := p.expr_var v := p.expr_var
is_map := typ.starts_with('map_') is_map := typ.starts_with('map_')
is_str := typ == 'string' is_str := typ == 'string'
@ -2135,8 +2133,8 @@ fn (p mut Parser) typ_to_fmt(typ string) string {
return '' return ''
} }
fn format_str(_str string) string { fn format_str(str string) string {
mut str := _str.replace('"', '\\"') str = str.replace('"', '\\"')
$if windows { $if windows {
str = str.replace('\r\n', '\\n') str = str.replace('\r\n', '\\n')
} }
@ -3079,8 +3077,7 @@ fn (p mut Parser) go_statement() {
} }
} }
fn (p mut Parser) register_var(var Var) { fn (p mut Parser) register_var(v Var) {
mut v := var
if v.line_nr == 0 { if v.line_nr == 0 {
v.line_nr = p.scanner.line_nr v.line_nr = p.scanner.line_nr
} }
@ -3144,8 +3141,8 @@ fn (p mut Parser) js_decode() string {
return '' return ''
} }
fn is_compile_time_const(_s string) bool { fn is_compile_time_const(s string) bool {
s := _s.trim_space() s = s.trim_space()
if s == '' { if s == '' {
return false return false
} }
@ -3169,17 +3166,15 @@ fn (p &Parser) building_v() bool {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// fmt helpers // fmt helpers
fn (scanner mut Scanner) fgen(_s string) { fn (scanner mut Scanner) fgen(s string) {
mut s := _s
if scanner.fmt_line_empty { 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_out.write(s)
scanner.fmt_line_empty = false 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 { if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s s = strings.repeat(`\t`, scanner.fmt_indent) + s
} }

View File

@ -184,8 +184,7 @@ fn (t mut Table) register_fn(new_fn Fn) {
t.fns[new_fn.name] = new_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 // 'byte*' => look up 'byte', but don't mess up fns
if typ.ends_with('*') && !typ.contains(' ') { if typ.ends_with('*') && !typ.contains(' ') {
typ = typ.left(typ.len - 1) typ = typ.left(typ.len - 1)
@ -381,8 +380,7 @@ fn (p &Parser) find_type(name string) *Type {
return typ 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(' ') { if name.ends_with('*') && !name.contains(' ') {
name = name.left(name.len - 1) name = name.left(name.len - 1)
} }
@ -395,9 +393,7 @@ fn (t &Table) find_type(_name string) *Type {
return &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" ') p.log('check types got="$got" exp="$expected" ')
if p.pref.translated { if p.pref.translated {
return true return true
@ -522,8 +518,8 @@ fn (p mut Parser) satisfies_interface(interface_name, _typ string, throw bool) b
fn type_default(typ string) string { fn type_default(typ string) string {
if typ.starts_with('array_') { if typ.starts_with('array_') {
elm_type := typ.right(6) typ = typ.right(6)
return 'new_array(0, 1, sizeof($elm_type))' return 'new_array(0, 1, sizeof($typ))'
} }
// Always set pointers to 0 // Always set pointers to 0
if typ.ends_with('*') { 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 { fn (u ustring) substr(start, end int) string {
_start := u.runes[start] start = u.runes[start]
_end := if end >= u.runes.len { if end >= u.runes.len {
u.s.len end = u.s.len
} }
else { else {
u.runes[end] end = u.runes[end]
} }
return u.s.substr(_start, _end) return u.s.substr(start, end)
} }
fn (u ustring) left(pos int) string { 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. // exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(_cmd string) string { pub fn exec(cmd string) string {
cmd := '$_cmd 2>&1' cmd = '$cmd 2>&1'
f := popen(cmd) f := popen(cmd)
if isnil(f) { if isnil(f) {
// TODO optional or error code // 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. // mkdir creates a new directory with the specified path.
pub fn mkdir(path string) { pub fn mkdir(path string) {
$if windows { $if windows {
_path := path.replace('/', '\\') path = path.replace('/', '\\')
C.CreateDirectory(_path.cstr(), 0) C.CreateDirectory(path.cstr(), 0)
} }
$else { $else {
C.mkdir(path.cstr(), 511)// S_IRWXU | S_IRWXG | S_IRWXO 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) return tos(b.buf.data, b.len)
} }
pub fn (b mut Builder) cut(n int) { pub fn (b Builder) cut(n int) {
b.len -= n b.len -= n
} }