From 6398043094e8eeec1727e7e23b5ca595a61f04e8 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 6 Jun 2022 14:33:24 +0300 Subject: [PATCH] cgen: fix missing function names in declarations on `[c2v_variadic][c: xyz]fn deh_fprintf(fstream &C.FILE, fmt &i8)` --- vlib/v/ast/attr.v | 19 +++++++++++++++++++ vlib/v/gen/c/cgen.v | 18 +++++++++--------- vlib/v/gen/c/fn.v | 28 ++++++++++++---------------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/vlib/v/ast/attr.v b/vlib/v/ast/attr.v index 973d542bd0..0366129f0f 100644 --- a/vlib/v/ast/attr.v +++ b/vlib/v/ast/attr.v @@ -54,6 +54,25 @@ pub fn (attrs []Attr) contains(str string) bool { return attrs.any(it.name == str) } +pub fn (attrs []Attr) find_first(aname string) ?Attr { + for a in attrs { + if a.name == aname { + return a + } + } + return none +} + +pub fn (attrs []Attr) find_last(aname string) ?Attr { + for idx := attrs.len - 1; idx > -1; idx-- { + a := attrs[idx] + if a.name == aname { + return a + } + } + return none +} + pub fn (attrs []Attr) find_comptime_define() ?int { for idx in 0 .. attrs.len { if attrs[idx].kind == .comptime_define { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index b26ad28a6e..fcf03d3138 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3925,8 +3925,8 @@ fn (mut g Gen) ident(node ast.Ident) { // `p_mobjthinker` => `P_MobjThinker` if f := g.table.find_fn(node.name) { // TODO PERF fn lookup for each fn call in translated mode - if f.attrs.contains('c') { - name = f.attrs[0].arg + if cattr := f.attrs.find_first('c') { + name = cattr.arg } } } @@ -3970,8 +3970,8 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { // TODO handle the type in fn casts, not just exprs /* info := sym.info as ast.FnType - if info.func.attrs.contains('c') { - // name = f.attrs[0].arg + if cattr := info.func.attrs.find_first('c') { + name = cattr.arg } */ } @@ -4398,11 +4398,11 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { } } name := c_name(field.name) - const_name := if node.attrs.contains('export') && !g.is_builtin_mod { - // TODO this only works for the first const in the group for now - node.attrs[0].arg - } else { - '_const_' + name + mut const_name := '_const_' + name + if !g.is_builtin_mod { + if cattr := node.attrs.find_first('export') { + const_name = cattr.arg + } } field_expr := field.expr match field.expr { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 50525aea96..ee9fc70e4e 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -446,20 +446,16 @@ fn (mut g Gen) c_fn_name(node &ast.FnDecl) ?string { name = g.generic_fn_name(g.cur_concrete_types, name, true) } - if (g.pref.translated || g.file.is_translated) && node.attrs.contains('c') { - // This fixes unknown symbols errors when building separate .c => .v files - // into .o files - // - // example: - // [c: 'P_TryMove'] - // fn p_trymove(thing &Mobj_t, x int, y int) bool - // - // => - // - // bool P_TryMove(main__Mobj_t* thing, int x, int y); - // - // In fn_call every time `p_trymove` is called, `P_TryMove` will be generated instead. - name = node.attrs[0].arg + if g.pref.translated || g.file.is_translated { + if cattr := node.attrs.find_first('c') { + // This fixes unknown symbols errors when building separate .c => .v files into .o files + // example: + // [c: 'P_TryMove'] fn p_trymove(thing &Mobj_t, x int, y int) bool + // translates to: + // bool P_TryMove(main__Mobj_t* thing, int x, int y); + // In fn_call every time `p_trymove` is called, `P_TryMove` will be generated instead. + name = cattr.arg + } } return name } @@ -1239,8 +1235,8 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { // every time `p_trymove` is called, `P_TryMove` must be generated instead. if f := g.table.find_fn(node.name) { // TODO PERF fn lookup for each fn call in translated mode - if f.attrs.contains('c') { - name = f.attrs[0].arg + if cattr := f.attrs.find_first('c') { + name = cattr.arg } } }