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

cgen: a minor optimization; checker: a c2v fix

This commit is contained in:
Alexander Medvednikov 2021-09-30 12:51:32 +03:00
parent 6cffcf515a
commit d4b3c65c45
3 changed files with 21 additions and 12 deletions

View File

@ -8340,18 +8340,22 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
} }
if node.language == .v { if node.language == .v {
// Make sure all types are valid // Make sure all types are valid
for arg in node.params { for mut param in node.params {
c.ensure_type_exists(arg.typ, arg.type_pos) or { return } c.ensure_type_exists(param.typ, param.type_pos) or { return }
if !arg.typ.is_ptr() { // value parameter, i.e. on stack - check for `[heap]` if !param.typ.is_ptr() { // value parameter, i.e. on stack - check for `[heap]`
arg_typ_sym := c.table.get_type_symbol(arg.typ) arg_typ_sym := c.table.get_type_symbol(param.typ)
if arg_typ_sym.kind == .struct_ { if arg_typ_sym.kind == .struct_ {
info := arg_typ_sym.info as ast.Struct info := arg_typ_sym.info as ast.Struct
if info.is_heap { // set auto_heap to promote value parameter 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 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 if node.language == .v && node.name.after_char(`.`) == 'init' && !node.is_method

View File

@ -1533,7 +1533,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
} }
} }
ast.FnDecl { ast.FnDecl {
g.process_fn_decl(node) g.fn_decl(node)
} }
ast.ForCStmt { ast.ForCStmt {
prev_branch_parent_pos := g.branch_parent_pos prev_branch_parent_pos := g.branch_parent_pos

View File

@ -28,7 +28,7 @@ fn (mut g Gen) is_used_by_main(node ast.FnDecl) bool {
return is_used_by_main 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) { if !g.is_used_by_main(node) {
return return
} }
@ -43,6 +43,11 @@ fn (mut g Gen) process_fn_decl(node ast.FnDecl) {
mut skip := false mut skip := false
pos := g.out.len pos := g.out.len
should_bundle_module := util.should_bundle_module(node.mod) 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 g.pref.build_mode == .build_module {
// if node.name.contains('parse_text') { // if node.name.contains('parse_text') {
// println('!!! $node.name mod=$node.mod, built=$g.module_built') // 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 { if node.is_main {
g.has_main = true g.has_main = true
} }
if node.name == 'backtrace' || node.name == 'backtrace_symbols' is_backtrace := node.name.starts_with('backtrace') // TODO PERF remove this from here
|| node.name == 'backtrace_symbols_fd' { && node.name in ['backtrace_symbols', 'backtrace', 'backtrace_symbols_fd']
if is_backtrace {
g.write('\n#ifndef __cplusplus\n') g.write('\n#ifndef __cplusplus\n')
} }
g.gen_fn_decl(node, skip) g.gen_fn_decl(node, skip)
if node.name == 'backtrace' || node.name == 'backtrace_symbols' if is_backtrace {
|| node.name == 'backtrace_symbols_fd' {
g.write('\n#endif\n') g.write('\n#endif\n')
} }
g.fn_decl = keep_fn_decl 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 pos := g.out.len
was_anon_fn := g.anon_fn was_anon_fn := g.anon_fn
g.anon_fn = true g.anon_fn = true
g.process_fn_decl(node.decl) g.fn_decl(node.decl)
g.anon_fn = was_anon_fn g.anon_fn = was_anon_fn
builder.write_string(g.out.cut_to(pos)) builder.write_string(g.out.cut_to(pos))
g.anon_fn_definitions << builder.str() g.anon_fn_definitions << builder.str()