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

v: fixes for v -autofree -o v2 cmd/v

This commit is contained in:
Delyan Angelov 2021-03-14 09:37:38 +02:00
parent 64d0006ff9
commit 69f1e7c9c3
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
13 changed files with 94 additions and 56 deletions

View File

@ -43,14 +43,14 @@ __global (
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`')

View File

@ -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' }

View File

@ -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')
}
}

View File

@ -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))
}

View File

@ -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

View File

@ -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(';')

View File

@ -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..] }
mut parent_styp := parent.cname
if is_c_parent {
if !is_typedef {
parent_styp = 'struct ' + parent.cname[3..]
} else {
parent.cname
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 {

View File

@ -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)

View File

@ -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`

View File

@ -690,7 +690,8 @@ 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)
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 {
@ -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')

View File

@ -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)

View File

@ -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
}

View File

@ -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
}