From 69f1e7c9c3a6387f0ef4ac74096f903dd1e91b5f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 14 Mar 2021 09:37:38 +0200 Subject: [PATCH] v: fixes for `v -autofree -o v2 cmd/v` --- vlib/builtin/builtin.v | 14 +++++++------- vlib/v/ast/str.v | 8 +++++--- vlib/v/builder/builder.v | 4 +++- vlib/v/builder/c.v | 4 +++- vlib/v/builder/compile.v | 7 ++++--- vlib/v/gen/c/assert.v | 12 ++++++++---- vlib/v/gen/c/cgen.v | 35 +++++++++++++++++++++++------------ vlib/v/gen/c/fn.v | 3 ++- vlib/v/gen/c/sql.v | 3 ++- vlib/v/gen/js/js.v | 37 ++++++++++++++++++++++--------------- vlib/v/gen/x64/gen.v | 4 +++- vlib/v/parser/struct.v | 6 ++++-- vlib/v/pref/pref.v | 13 ++++++++----- 13 files changed, 94 insertions(+), 56 deletions(-) diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index cbf2161a26..1e48fb8053 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -4,8 +4,8 @@ module builtin __global ( - g_m2_buf byteptr - g_m2_ptr byteptr + g_m2_buf byteptr + g_m2_ptr byteptr ) // isnil returns true if an object is nil (only for C objects). @@ -38,19 +38,19 @@ __global ( total_m = i64(0) nr_mallocs = int(0) // will be filled in cgen - as_cast_type_indexes []VCastTypeIndexName + as_cast_type_indexes []VCastTypeIndexName ) fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr { if obj_type != expected_type { - mut obj_name := as_cast_type_indexes[0].tname - mut expected_name := as_cast_type_indexes[0].tname + mut obj_name := as_cast_type_indexes[0].tname.clone() + mut expected_name := as_cast_type_indexes[0].tname.clone() for x in as_cast_type_indexes { if x.tindex == obj_type { - obj_name = x.tname + obj_name = x.tname.clone() } if x.tindex == expected_type { - expected_name = x.tname + expected_name = x.tname.clone() } } panic('as cast: cannot cast `$obj_name` to `$expected_name`') diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index e02e7a7173..ea2ce114f0 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -408,9 +408,7 @@ pub fn (node Stmt) str() string { return node.str() } ConstDecl { - fields := node.fields.map(fn (f ConstField) string { - return '${f.name.trim_prefix(f.mod + '.')} = $f.expr' - }) + fields := node.fields.map(field_to_string) return 'const (${fields.join(' ')})' } ExprStmt { @@ -441,6 +439,10 @@ pub fn (node Stmt) str() string { } } +fn field_to_string(f ConstField) string { + return '${f.name.trim_prefix(f.mod + '.')} = $f.expr' +} + pub fn (e CompForKind) str() string { match e { .methods { return 'methods' } diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index ff45f5313d..dd57b85e6e 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -285,7 +285,9 @@ pub fn (b &Builder) find_module_path(mod string, fpath string) ?string { fn (b &Builder) show_total_warns_and_errors_stats() { if b.pref.is_stats { - println('checker summary: ${util.bold(b.checker.nr_errors.str())} V errors, ${util.bold(b.checker.nr_warnings.str())} V warnings') + estring := util.bold(b.checker.nr_errors.str()) + wstring := util.bold(b.checker.nr_warnings.str()) + println('checker summary: $estring V errors, $wstring V warnings') } } diff --git a/vlib/v/builder/c.v b/vlib/v/builder/c.v index b0da21f188..77a51ff6fa 100644 --- a/vlib/v/builder/c.v +++ b/vlib/v/builder/c.v @@ -46,7 +46,9 @@ pub fn (mut b Builder) build_c(v_files []string, out_file string) { f.writeln(output2) or { panic(err) } f.close() if b.pref.is_stats { - println('generated C source code size: ${util.bold((output2.count('\n') + 1).str())} lines, ${util.bold(output2.len.str())} bytes') + slines := util.bold((output2.count('\n') + 1).str()) + sbytes := util.bold(output2.len.str()) + println('generated C source code size: $slines lines, $sbytes bytes') } // os.write_file(out_file, b.gen_c(v_files)) } diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index ea51093a7b..3de6555851 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -15,8 +15,8 @@ fn (mut b Builder) get_vtmp_filename(base_file_name string, postfix string) stri if !b.pref.reuse_tmpc { uniq = '.$rand.u64()' } - return os.real_path(os.join_path(vtmp, os.file_name(os.real_path(base_file_name)) + - '$uniq$postfix')) + fname := os.file_name(os.real_path(base_file_name)) + '$uniq$postfix' + return os.real_path(os.join_path(vtmp, fname)) } pub fn compile(command string, pref &pref.Preferences) { @@ -45,7 +45,8 @@ pub fn compile(command string, pref &pref.Preferences) { .x64 { b.compile_x64() } } if pref.is_stats { - println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms') + compilation_time := util.bold(sw.elapsed().milliseconds().str()) + println('compilation took: $compilation_time ms') } b.exit_on_invalid_syntax() // running does not require the parsers anymore diff --git a/vlib/v/gen/c/assert.v b/vlib/v/gen/c/assert.v index 8c4091821c..1979d0192c 100644 --- a/vlib/v/gen/c/assert.v +++ b/vlib/v/gen/c/assert.v @@ -58,12 +58,16 @@ fn (mut g Gen) gen_assert_metainfo(node ast.AssertStmt) string { g.writeln('\t${metaname}.fpath = ${ctoslit(mod_path)};') g.writeln('\t${metaname}.line_nr = $line_nr;') g.writeln('\t${metaname}.fn_name = ${ctoslit(fn_name)};') - g.writeln('\t${metaname}.src = ${cnewlines(ctoslit(src))};') + metasrc := cnewlines(ctoslit(src)) + g.writeln('\t${metaname}.src = $metasrc;') match mut node.expr { ast.InfixExpr { - g.writeln('\t${metaname}.op = ${ctoslit(node.expr.op.str())};') - g.writeln('\t${metaname}.llabel = ${cnewlines(ctoslit(node.expr.left.str()))};') - g.writeln('\t${metaname}.rlabel = ${cnewlines(ctoslit(node.expr.right.str()))};') + expr_op_str := ctoslit(node.expr.op.str()) + expr_left_str := cnewlines(ctoslit(node.expr.left.str())) + expr_right_str := cnewlines(ctoslit(node.expr.right.str())) + g.writeln('\t${metaname}.op = $expr_op_str;') + g.writeln('\t${metaname}.llabel = $expr_left_str;') + g.writeln('\t${metaname}.rlabel = $expr_right_str;') g.write('\t${metaname}.lvalue = ') g.gen_assert_single_expr(node.expr.left, node.expr.left_type) g.writeln(';') diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 43787c5160..64ff0bb599 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -737,10 +737,13 @@ pub fn (mut g Gen) write_typedef_types() { if parent.info is table.Struct { is_typedef = parent.info.is_typedef } - parent_styp := if is_c_parent { - if !is_typedef { 'struct ' + parent.cname[3..] } else { parent.cname[3..] } - } else { - parent.cname + mut parent_styp := parent.cname + if is_c_parent { + if !is_typedef { + parent_styp = 'struct ' + parent.cname[3..] + } else { + parent_styp = parent.cname[3..] + } } g.type_definitions.writeln('typedef $parent_styp $typ.cname;') } @@ -1375,8 +1378,9 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) { g.writeln(';') } i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var } - op_field := if node.cond_type.is_ptr() { '->' } else { '.' } + - if node.cond_type.share() == .shared_t { 'val.' } else { '' } + field_accessor := if node.cond_type.is_ptr() { '->' } else { '.' } + share_accessor := if node.cond_type.share() == .shared_t { 'val.' } else { '' } + op_field := field_accessor + share_accessor g.empty_line = true g.writeln('for (int $i = 0; $i < $cond_var${op_field}len; ++$i) {') if node.val_var != '_' { @@ -3454,7 +3458,8 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { e := right_sym.kind !in [.voidptr, .int_literal, .int] if node.op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && ((a && b && e) || c || d) { // Overloaded operators - g.write(g.typ(if !d { left_type } else { (left_sym.info as table.Alias).parent_type })) + the_left_type := if !d { left_type } else { (left_sym.info as table.Alias).parent_type } + g.write(g.typ(the_left_type)) g.write('_') g.write(util.replace_op(node.op.str())) g.write('(') @@ -3463,7 +3468,8 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { g.expr(node.right) g.write(')') } else if node.op in [.ne, .gt, .ge, .le] && ((a && b && e) || c || d) { - typ := g.typ(if !d { left_type } else { (left_sym.info as table.Alias).parent_type }) + the_left_type := if !d { left_type } else { (left_sym.info as table.Alias).parent_type } + typ := g.typ(the_left_type) if node.op == .gt { g.write('$typ') } else { @@ -3987,7 +3993,11 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) { objs_array := g.new_tmp_var() g.write('Array_voidptr $objs_array = new_array_from_c_array($n_channels, $n_channels, sizeof(voidptr), _MOV((voidptr[$n_channels]){') for i in 0 .. n_channels { - g.write(if i > 0 { ', &' } else { '&' }) + if i > 0 { + g.write(', &') + } else { + g.write('&') + } if tmp_objs[i] == '' { g.expr(objs[i]) } else { @@ -5574,11 +5584,12 @@ fn (mut g Gen) go_stmt(node ast.GoStmt, joinable bool) string { g.writeln('$arg_tmp_var->ret_ptr = malloc(sizeof($s_ret_typ));') } is_opt := node.call_expr.return_type.has_flag(.optional) - gohandle_name := if node.call_expr.return_type == table.void_type { - if is_opt { '__v_thread_Option_void' } else { '__v_thread' } + mut gohandle_name := '' + if node.call_expr.return_type == table.void_type { + gohandle_name = if is_opt { '__v_thread_Option_void' } else { '__v_thread' } } else { opt := if is_opt { 'Option_' } else { '' } - '__v_thread_$opt${g.table.get_type_symbol(g.unwrap_generic(node.call_expr.return_type)).cname}' + gohandle_name = '__v_thread_$opt${g.table.get_type_symbol(g.unwrap_generic(node.call_expr.return_type)).cname}' } if g.pref.os == .windows { simple_handle := if joinable && node.call_expr.return_type != table.void_type { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 12ef826c0c..b6aa0e5d6b 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -127,7 +127,8 @@ fn (mut g Gen) gen_fn_decl(node ast.FnDecl, skip bool) { for gen_types in g.table.fn_gen_types[node.name] { if g.pref.is_verbose { syms := gen_types.map(g.table.get_type_symbol(it)) - println('gen fn `$node.name` for type `${syms.map(node.name).join(', ')}`') + the_type := syms.map(node.name).join(', ') + println('gen fn `$node.name` for type `$the_type`') } g.cur_generic_types = gen_types g.gen_fn_decl(node, skip) diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 7996bd7174..e1812db7a3 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -402,7 +402,8 @@ fn (mut g Gen) expr_to_sql(expr ast.Expr, typ SqlType) { // true/false literals were added to Sqlite 3.23 (2018-04-02) // but lots of apps/distros use older sqlite (e.g. Ubuntu 18.04 LTS ) g.inc_sql_i() - g.sql_bind_int(if expr.val { '1' } else { '0' }, typ) + eval := if expr.val { '1' } else { '0' } + g.sql_bind_int(eval, typ) } ast.Ident { // `name == user_name` => `name == ?1` diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 2dc1c0cf0a..76575975fa 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -690,8 +690,9 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) { } else { g.write(' $op ') // TODO: Multiple types?? - should_cast := (g.table.type_kind(stmt.left_types.first()) in shallow_equatables) - && (g.cast_stack.len <= 0 || stmt.left_types.first() != g.cast_stack.last()) + should_cast := + (g.table.type_kind(stmt.left_types.first()) in js.shallow_equatables) + && (g.cast_stack.len <= 0 || stmt.left_types.first() != g.cast_stack.last()) if should_cast { g.cast_stack << stmt.left_types.first() @@ -929,26 +930,28 @@ fn (mut g JsGen) gen_for_in_stmt(it ast.ForInStmt) { if it.kind == .string { g.write('Array.from(') g.expr(it.cond) - g.write('.str.split(\'\').entries(), ([$it.key_var, $val]) => [$it.key_var, ') - if g.ns.name == 'builtin' { g.write('new ') } + g.write(".str.split(\'\').entries(), ([$it.key_var, $val]) => [$it.key_var, ") + if g.ns.name == 'builtin' { + g.write('new ') + } g.write('byte($val)])') } else { g.expr(it.cond) g.write('.entries()') } - } else { g.write('for (const $val of ') g.expr(it.cond) if it.kind == .string { - g.write('.str.split(\'\')') + g.write(".str.split('')") } // cast characters to bytes if val !in ['', '_'] && it.kind == .string { g.write('.map(c => ') - if g.ns.name == 'builtin' { g.write('new ') } + if g.ns.name == 'builtin' { + g.write('new ') + } g.write('byte(c))') - } } g.writeln(') {') @@ -1089,7 +1092,11 @@ fn (mut g JsGen) gen_struct_decl(node ast.StructDecl) { g.inc_indent() g.write('return `$js_name {') for i, field in node.fields { - g.write(if i == 0 { ' ' } else { ', ' }) + if i == 0 { + g.write(' ') + } else { + g.write(', ') + } match g.typ(field.typ).split('.').last() { 'string' { g.write('$field.name: "\${this["$field.name"].toString()}"') } else { g.write('$field.name: \${this["$field.name"].toString()} ') } @@ -1417,13 +1424,13 @@ fn (mut g JsGen) gen_infix_expr(it ast.InfixExpr) { g.write(')') } else if r_sym.kind in [.array, .map, .string] && it.op in [.key_in, .not_in] { g.expr(it.right) - g.write(if r_sym.kind == .map { - '.has(' + if r_sym.kind == .map { + g.write('.has(') } else if r_sym.kind == .string { - '.str.includes(' + g.write('.str.includes(') } else { - '.includes(' - }) + g.write('.includes(') + } g.expr(it.left) if l_sym.kind == .string { g.write('.str') @@ -1443,7 +1450,7 @@ fn (mut g JsGen) gen_infix_expr(it ast.InfixExpr) { needs_cast = g.cast_stack.last() != greater_typ } } - + if needs_cast { if g.ns.name == 'builtin' { g.write('new ') diff --git a/vlib/v/gen/x64/gen.v b/vlib/v/gen/x64/gen.v index 57ea40a2f0..0f6c27c6ba 100644 --- a/vlib/v/gen/x64/gen.v +++ b/vlib/v/gen/x64/gen.v @@ -284,7 +284,9 @@ fn (mut g Gen) println(comment string) { if s.len == 1 { print(term.blue('0')) } - print(term.blue(g.buf[i].hex()) + ' ') + gbihex := g.buf[i].hex() + hexstr := term.blue(gbihex) + ' ' + print(hexstr) } g.debug_pos = g.buf.len print(' ' + comment) diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 9d0c612868..73428ead4d 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -267,14 +267,16 @@ fn (mut p Parser) struct_decl() ast.StructDecl { is_public: is_field_pub } } + is_pub_field := is_embed || is_field_pub + is_mut_field := is_embed || is_field_mut // save embeds as table fields too, it will be used in generation phase fields << table.Field{ name: field_name typ: typ default_expr: ast.ex2fe(default_expr) has_default_expr: has_default_expr - is_pub: if is_embed { true } else { is_field_pub } - is_mut: if is_embed { true } else { is_field_mut } + is_pub: is_pub_field + is_mut: is_mut_field is_global: is_field_global attrs: p.attrs } diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 24d0f0ab6c..8e9ce09ffd 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -156,7 +156,7 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences // for i, arg in args { for i := 0; i < args.len; i++ { arg := args[i] - current_args := args[i..] + current_args := args[i..].clone() match arg { '-apk' { res.is_apk = true @@ -441,12 +441,12 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences command_pos = i continue } - if command !in ['', 'build-module'] { + if command != '' && command != 'build-module' { // arguments for e.g. fmt should be checked elsewhere continue } - eprint('Unknown argument `$arg`') - eprintln(if command.len == 0 { '' } else { ' for command `$command`' }) + extension := if command.len == 0 { '' } else { ' for command `$command`' } + eprintln('Unknown argument `$arg`$extension') exit(1) } } @@ -554,7 +554,10 @@ pub fn cc_from_string(cc_str string) CompilerType { return .gcc } // TODO - cc := cc_str.replace('\\', '/').split('/').last().all_before('.') + normalized_cc := cc_str.replace('\\', '/') + normalized_cc_array := normalized_cc.split('/') + last_elem := normalized_cc_array.last() + cc := last_elem.all_before('.') if '++' in cc { return .cplusplus }