Skip to content

Commit 2563552

Browse files
authored
ast, native, cgen: add support for $if native {} (#19500)
1 parent b1f141a commit 2563552

File tree

6 files changed

+16
-1
lines changed

6 files changed

+16
-1
lines changed

vlib/v/ast/comptime_valid_idents.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ pub const (
99
valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian']
1010
valid_comptime_if_other = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
1111
'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
12-
'interpreter', 'es5', 'profile', 'wasm32', 'wasm32_emscripten', 'wasm32_wasi', 'fast_math']
12+
'interpreter', 'es5', 'profile', 'wasm32', 'wasm32_emscripten', 'wasm32_wasi', 'fast_math',
13+
'native']
1314
valid_comptime_not_user_defined = all_valid_comptime_idents()
1415
valid_comptime_compression_types = ['none', 'zlib']
1516
)

vlib/v/gen/c/comptime.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,9 @@ fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_option bool) !strin
10781078
'wasm32_emscripten' {
10791079
return '__EMSCRIPTEN__'
10801080
}
1081+
'native' {
1082+
return '_VNATIVE' // when using the native backend, cgen is inactive
1083+
}
10811084
// compilers:
10821085
'gcc' {
10831086
return '__V_GCC__'

vlib/v/gen/js/comptime.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ fn (mut g JsGen) comptime_if_to_ifdef(name string, is_comptime_option bool) !str
236236
'js' {
237237
return 'true'
238238
}
239+
'native' {
240+
return 'false'
241+
}
239242
// compilers:
240243
'gcc' {
241244
return 'false'

vlib/v/gen/native/comptime.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ fn (mut g Gen) comptime_ident(name string, is_comptime_option bool) bool {
173173
//
174174
// Other
175175
//
176+
'native' {
177+
true
178+
}
176179
'debug' {
177180
g.pref.is_debug
178181
}

vlib/v/gen/native/tests/comptime.vv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ fn comptime_if() {
77
println('linux or windows or macos')
88
}
99

10+
$if native {
11+
println('using the native backend')
12+
}
13+
1014
os := $if linux || windows || macos {
1115
'linux or windows or macos'
1216
} $else {

vlib/v/gen/native/tests/comptime.vv.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
linux or windows or macos
2+
using the native backend
23
linux or windows or macos
34
custom defines work
45
printing directly

0 commit comments

Comments
 (0)