Skip to content

Commit 9ae0a9d

Browse files
authored
v,ast,fmt,parser: support @[tag] for hash statements, like #define and #flag (#23210)
1 parent 30ececc commit 9ae0a9d

File tree

10 files changed

+45
-9
lines changed

10 files changed

+45
-9
lines changed

cmd/tools/vast/vast.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ fn (t Tree) fn_decl(node ast.FnDecl) &Node {
592592
obj.add_terse('return_type', t.type_node(node.return_type))
593593
obj.add('source_file', t.number_node(int(node.source_file)))
594594
obj.add('scope', t.number_node(int(node.scope)))
595-
obj.add('attrs', t.array_node_attr(node.attrs))
595+
obj.add_terse('attrs', t.array_node_attr(node.attrs))
596596
obj.add_terse('params', t.array_node_arg(node.params))
597597
obj.add_terse('generic_names', t.array_node_string(node.generic_names))
598598
obj.add_terse('stmts', t.array_node_stmt(node.stmts))
@@ -756,8 +756,10 @@ fn (t Tree) hash_stmt(node ast.HashStmt) &Node {
756756
obj.add_terse('kind', t.string_node(node.kind))
757757
obj.add_terse('main', t.string_node(node.main))
758758
obj.add_terse('msg', t.string_node(node.msg))
759+
obj.add_terse('is_use_once', t.bool_node(node.is_use_once))
759760
obj.add_terse('ct_conds', t.array_node_expr(node.ct_conds))
760761
obj.add_terse('source_file', t.string_node(node.source_file))
762+
obj.add_terse('attrs', t.array_node_attr(node.attrs))
761763
obj.add('pos', t.pos(node.pos))
762764
return obj
763765
}
@@ -778,11 +780,11 @@ fn (t Tree) global_decl(node ast.GlobalDecl) &Node {
778780
mut obj := new_object()
779781
obj.add_terse('ast_type', t.string_node('GlobalDecl'))
780782
obj.add_terse('mod', t.string_node(node.mod))
781-
obj.add('pos', t.pos(node.pos))
783+
obj.add_terse('attrs', t.array_node_attr(node.attrs))
782784
obj.add_terse('is_block', t.bool_node(node.is_block))
783785
obj.add_terse('fields', t.array_node_global_field(node.fields))
786+
obj.add('pos', t.pos(node.pos))
784787
obj.add('end_comments', t.array_node_comment(node.end_comments))
785-
obj.add('attrs', t.array_node_attr(node.attrs))
786788
return obj
787789
}
788790

vlib/fontstash/fontstash.c.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module fontstash
22

33
#flag -I @VEXEROOT/thirdparty/fontstash
4+
5+
@[use_once]
46
#define FONTSTASH_IMPLEMENTATION
57
$if gcboehm ? {
68
#define FONTSTASH_MALLOC GC_MALLOC

vlib/sokol/audio/audio.c.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ $if linux {
1010
#flag -I @VEXEROOT/thirdparty/sokol
1111
// FreeBSD requires the audio/alsa-lib to be installed
1212
#flag freebsd -I/usr/local/include
13+
14+
@[use_once]
1315
#define SOKOL_IMPL
1416
#include "sokol_audio.h"
1517
#flag linux -lasound -lpthread

vlib/sokol/c/declaration.c.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ $if emscripten ? {
5656
//#flag windows -DSOKOL_D3D11
5757
// for simplicity, all header includes are here because import order matters and we dont have any way
5858
// to ensure import order with V yet
59+
60+
@[use_once]
5961
#define SOKOL_IMPL
6062
// TODO: should not be defined for android graphic (apk/aab using sokol) builds, but we have no ways to undefine
6163
//#define SOKOL_NO_ENTRY
@@ -75,9 +77,13 @@ $if emscripten ? {
7577
$if !no_sokol_app ? {
7678
#include "sokol_app.h"
7779
}
80+
81+
@[use_once]
7882
#define SOKOL_IMPL
7983
#define SOKOL_NO_DEPRECATED
8084
#include "sokol_gfx.h"
85+
86+
@[use_once]
8187
#define SOKOL_GL_IMPL
8288
#include "util/sokol_gl.h"
8389
#include "sokol_v.post.h"

vlib/sokol/f/f.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ import sokol.c as _
77

88
//#include "ft2build.h"
99

10+
@[use_once]
1011
#define SOKOL_FONTSTASH_IMPL
1112
#include "util/sokol_fontstash.h"

vlib/v/ast/ast.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,13 +1365,15 @@ pub:
13651365
mod string
13661366
pos token.Pos
13671367
source_file string
1368+
is_use_once bool // true for @[use_once]
13681369
pub mut:
13691370
val string // example: 'include <openssl/rand.h> # please install openssl // comment'
13701371
kind string // : 'include'
13711372
main string // : '<openssl/rand.h>'
13721373
msg string // : 'please install openssl'
13731374
ct_conds []Expr // *all* comptime conditions, that must be true, for the hash to be processed
13741375
// ct_conds is filled by the checker, based on the current nesting of `$if cond1 {}` blocks
1376+
attrs []Attr
13751377
}
13761378

13771379
// variable assign statement

vlib/v/fmt/fmt.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ pub fn (mut f Fmt) goto_stmt(node ast.GotoStmt) {
13611361
}
13621362

13631363
pub fn (mut f Fmt) hash_stmt(node ast.HashStmt) {
1364+
f.attrs(node.attrs)
13641365
f.writeln('#${node.val}')
13651366
}
13661367

vlib/v/fmt/tests/hashstmt_keep.vv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
// comment between without newlines
1111
#include "sqlite3.h"
1212
// comment directly after the HsahStmt
13+
14+
@[another; use_once]
15+
#define SOME_SINGLE_HEADER_IMPL 1
16+
17+
@[custom_tag; use_once]
18+
#flag -I @VMODROOT/c/something/else

vlib/v/parser/comptime.v

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fn (mut p Parser) hash() ast.HashStmt {
7878
pos := p.tok.pos()
7979
val := p.tok.lit
8080
kind := val.all_before(' ')
81+
attrs := p.attrs
8182
p.next()
8283
mut main_str := ''
8384
mut msg := ''
@@ -89,6 +90,15 @@ fn (mut p Parser) hash() ast.HashStmt {
8990
main_str = content.trim_space()
9091
msg = ''
9192
}
93+
94+
mut is_use_once := false
95+
for fna in attrs {
96+
match fna.name {
97+
'use_once' { is_use_once = true }
98+
else {}
99+
}
100+
}
101+
92102
return ast.HashStmt{
93103
mod: p.mod
94104
source_file: p.file_path
@@ -97,6 +107,8 @@ fn (mut p Parser) hash() ast.HashStmt {
97107
main: main_str
98108
msg: msg
99109
pos: pos
110+
attrs: attrs
111+
is_use_once: is_use_once
100112
}
101113
}
102114

vlib/v/parser/fn.v

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
203203
mut is_markused := false
204204
mut is_expand_simple_interpolation := false
205205
mut comments := []ast.Comment{}
206-
for fna in p.attrs {
206+
fn_attrs := p.attrs
207+
p.attrs = []
208+
for fna in fn_attrs {
207209
match fna.name {
208210
'noreturn' {
209211
is_noreturn = true
@@ -271,7 +273,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
271273
else {}
272274
}
273275
}
274-
conditional_ctdefine_idx := p.attrs.find_comptime_define() or { -1 }
276+
conditional_ctdefine_idx := fn_attrs.find_comptime_define() or { -1 }
275277
is_pub := p.tok.kind == .key_pub
276278
if is_pub {
277279
p.next()
@@ -286,7 +288,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
286288
mut language := p.parse_language()
287289
p.fn_language = language
288290
if language != .v {
289-
for fna in p.attrs {
291+
for fna in fn_attrs {
290292
if fna.name == 'export' {
291293
p.error_with_pos('interop function cannot be exported', fna.pos)
292294
break
@@ -555,7 +557,7 @@ run them via `v file.v` instead',
555557
is_method: true
556558
receiver_type: rec.typ
557559
//
558-
attrs: p.attrs
560+
attrs: fn_attrs
559561
is_conditional: conditional_ctdefine_idx != ast.invalid_type_idx
560562
ctdefine_idx: conditional_ctdefine_idx
561563
//
@@ -611,7 +613,7 @@ run them via `v file.v` instead',
611613
receiver_type: if is_static_type_method { rec.typ } else { 0 } // used only if is static type method
612614
is_file_translated: p.is_translated
613615
//
614-
attrs: p.attrs
616+
attrs: fn_attrs
615617
is_conditional: conditional_ctdefine_idx != ast.invalid_type_idx
616618
ctdefine_idx: conditional_ctdefine_idx
617619
//
@@ -686,7 +688,7 @@ run them via `v file.v` instead',
686688
is_markused: is_markused
687689
is_file_translated: p.is_translated
688690
//
689-
attrs: p.attrs
691+
attrs: fn_attrs
690692
is_conditional: conditional_ctdefine_idx != ast.invalid_type_idx
691693
ctdefine_idx: conditional_ctdefine_idx
692694
//

0 commit comments

Comments
 (0)