From d4b3c65c4578a7ac7022a255aef43863f324f31a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 30 Sep 2021 12:51:32 +0300 Subject: [PATCH] cgen: a minor optimization; checker: a c2v fix --- vlib/v/checker/checker.v | 14 +++++++++----- vlib/v/gen/c/cgen.v | 2 +- vlib/v/gen/c/fn.v | 17 +++++++++++------ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f4a2bc803c..e1e8787ce7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -8340,18 +8340,22 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } if node.language == .v { // Make sure all types are valid - for arg in node.params { - c.ensure_type_exists(arg.typ, arg.type_pos) or { return } - if !arg.typ.is_ptr() { // value parameter, i.e. on stack - check for `[heap]` - arg_typ_sym := c.table.get_type_symbol(arg.typ) + for mut param in node.params { + c.ensure_type_exists(param.typ, param.type_pos) or { return } + if !param.typ.is_ptr() { // value parameter, i.e. on stack - check for `[heap]` + arg_typ_sym := c.table.get_type_symbol(param.typ) if arg_typ_sym.kind == .struct_ { info := arg_typ_sym.info as ast.Struct if info.is_heap { // set auto_heap to promote value parameter - mut v := node.scope.find_var(arg.name) or { continue } + mut v := node.scope.find_var(param.name) or { continue } v.is_auto_heap = true } } } + if c.pref.translated && node.is_variadic && node.params.len == 1 && param.typ.is_ptr() { + // TODO c2v hack to fix `(const char *s, ...)` + param.typ = ast.int_type.to_ptr() + } } } if node.language == .v && node.name.after_char(`.`) == 'init' && !node.is_method diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 2a04780429..1e8593854d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1533,7 +1533,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { } } ast.FnDecl { - g.process_fn_decl(node) + g.fn_decl(node) } ast.ForCStmt { prev_branch_parent_pos := g.branch_parent_pos diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index aa7bf401c0..2cf6426e7b 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -28,7 +28,7 @@ fn (mut g Gen) is_used_by_main(node ast.FnDecl) bool { return is_used_by_main } -fn (mut g Gen) process_fn_decl(node ast.FnDecl) { +fn (mut g Gen) fn_decl(node ast.FnDecl) { if !g.is_used_by_main(node) { return } @@ -43,6 +43,11 @@ fn (mut g Gen) process_fn_decl(node ast.FnDecl) { mut skip := false pos := g.out.len should_bundle_module := util.should_bundle_module(node.mod) + /* + if node.name.contains('i_error') { + println(g.table.type_str(node.params[0].typ)) + } + */ if g.pref.build_mode == .build_module { // if node.name.contains('parse_text') { // println('!!! $node.name mod=$node.mod, built=$g.module_built') @@ -77,13 +82,13 @@ fn (mut g Gen) process_fn_decl(node ast.FnDecl) { if node.is_main { g.has_main = true } - if node.name == 'backtrace' || node.name == 'backtrace_symbols' - || node.name == 'backtrace_symbols_fd' { + is_backtrace := node.name.starts_with('backtrace') // TODO PERF remove this from here + && node.name in ['backtrace_symbols', 'backtrace', 'backtrace_symbols_fd'] + if is_backtrace { g.write('\n#ifndef __cplusplus\n') } g.gen_fn_decl(node, skip) - if node.name == 'backtrace' || node.name == 'backtrace_symbols' - || node.name == 'backtrace_symbols_fd' { + if is_backtrace { g.write('\n#endif\n') } g.fn_decl = keep_fn_decl @@ -454,7 +459,7 @@ fn (mut g Gen) gen_anon_fn_decl(mut node ast.AnonFn) { pos := g.out.len was_anon_fn := g.anon_fn g.anon_fn = true - g.process_fn_decl(node.decl) + g.fn_decl(node.decl) g.anon_fn = was_anon_fn builder.write_string(g.out.cut_to(pos)) g.anon_fn_definitions << builder.str()