From e99059471b5f18e33b899a900c0c31d7da800f8b Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 14 Apr 2022 08:37:22 +0300 Subject: [PATCH] v.ast,v.fmt: support attributes on FnTypeDecl too --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/fmt.v | 1 + .../fn_type_attributes_and_calling_convention_keep.vv | 10 ++++++++++ vlib/v/parser/parser.v | 3 +++ 4 files changed, 15 insertions(+) create mode 100644 vlib/v/fmt/tests/fn_type_attributes_and_calling_convention_keep.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9f5e4fe596..25844c74b5 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1152,6 +1152,7 @@ pub: pos token.Pos type_pos token.Pos comments []Comment + attrs []Attr // attributes of type declaration } // TODO: handle this differently diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 3d6ef34dc8..5430241a55 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1290,6 +1290,7 @@ pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) { } pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) { + f.attrs(node.attrs) if node.is_pub { f.write('pub ') } diff --git a/vlib/v/fmt/tests/fn_type_attributes_and_calling_convention_keep.vv b/vlib/v/fmt/tests/fn_type_attributes_and_calling_convention_keep.vv new file mode 100644 index 0000000000..4012843980 --- /dev/null +++ b/vlib/v/fmt/tests/fn_type_attributes_and_calling_convention_keep.vv @@ -0,0 +1,10 @@ +[callconv: 'stdcall'] +fn C.DefWindowProc(hwnd int, msg int, lparam int, wparam int) + +[callconv: 'stdcall'] +type FastFn = fn (int) bool + +[callconv: 'fastcall'] +fn zzz(x int, y int) int { + return x + y * 2 +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 964411f487..a12e12a726 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3579,6 +3579,8 @@ fn (mut p Parser) type_decl() ast.TypeDecl { p.table.sym(fn_type).is_pub = is_pub type_pos = type_pos.extend(p.tok.pos()) comments = p.eat_comments(same_line: true) + attrs := p.attrs + p.attrs = [] return ast.FnTypeDecl{ name: fn_name is_pub: is_pub @@ -3586,6 +3588,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { pos: decl_pos type_pos: type_pos comments: comments + attrs: attrs } } sum_variants << p.parse_sum_type_variants()