Skip to content

Commit ce79c9c

Browse files
authored
checker: extract valid_comptime_ constants into v.checker.constants (#16371)
1 parent a07f77a commit ce79c9c

File tree

3 files changed

+42
-46
lines changed

3 files changed

+42
-46
lines changed

vlib/v/checker/checker.v

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import v.util
1212
import v.util.version
1313
import v.errors
1414
import v.pkgconfig
15+
import v.checker.constants
1516

1617
const (
1718
int_min = int(0x80000000)
@@ -23,37 +24,16 @@ const (
2324
)
2425

2526
pub const (
26-
valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu',
27-
'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux',
28-
'solaris', 'haiku', 'serenity', 'vinix']
29-
valid_comptime_compression_types = ['none', 'zlib']
30-
valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
31-
valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32']
32-
valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian']
33-
valid_comptime_if_other = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
34-
'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
35-
'interpreter', 'es5', 'profile', 'wasm32_emscripten']
36-
valid_comptime_not_user_defined = all_valid_comptime_idents()
37-
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice',
38-
'sort', 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
39-
array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods)
27+
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort',
28+
'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
29+
array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods)
4030
// TODO: remove `byte` from this list when it is no longer supported
41-
reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8',
42-
'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune']
43-
reserved_type_names_chk = token.new_keywords_matcher_from_array_trie(reserved_type_names)
44-
vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead'
31+
reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', 'u16',
32+
'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune']
33+
reserved_type_names_chk = token.new_keywords_matcher_from_array_trie(reserved_type_names)
34+
vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead'
4535
)
4636

47-
fn all_valid_comptime_idents() []string {
48-
mut res := []string{}
49-
res << checker.valid_comptime_if_os
50-
res << checker.valid_comptime_if_compilers
51-
res << checker.valid_comptime_if_platforms
52-
res << checker.valid_comptime_if_cpu_features
53-
res << checker.valid_comptime_if_other
54-
return res
55-
}
56-
5737
[heap; minify]
5838
pub struct Checker {
5939
pref &pref.Preferences = unsafe { nil } // Preferences shared from V struct
@@ -1644,7 +1624,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
16441624
for i, ident in node.defer_vars {
16451625
mut id := ident
16461626
if mut id.info is ast.IdentVar {
1647-
if id.comptime && id.name in checker.valid_comptime_not_user_defined {
1627+
if id.comptime && id.name in constants.valid_comptime_not_user_defined {
16481628
node.defer_vars[i] = ast.Ident{
16491629
scope: 0
16501630
name: ''

vlib/v/checker/comptime.v

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import v.pref
77
import v.token
88
import v.util
99
import v.pkgconfig
10+
import v.checker.constants
1011

1112
fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
1213
if node.left !is ast.EmptyExpr {
@@ -29,8 +30,8 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
2930
}
3031
if node.is_embed {
3132
// c.file.embedded_files << node.embed_file
32-
if node.embed_file.compression_type !in valid_comptime_compression_types {
33-
supported := valid_comptime_compression_types.map('.$it').join(', ')
33+
if node.embed_file.compression_type !in constants.valid_comptime_compression_types {
34+
supported := constants.valid_comptime_compression_types.map('.$it').join(', ')
3435
c.error('not supported compression type: .${node.embed_file.compression_type}. supported: $supported',
3536
node.pos)
3637
}
@@ -394,7 +395,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool {
394395
}
395396
if node.ct_expr is ast.Ident {
396397
if node.ct_opt {
397-
if node.ct_expr.name in valid_comptime_not_user_defined {
398+
if node.ct_expr.name in constants.valid_comptime_not_user_defined {
398399
c.error('optional `[if expression ?]` tags, can be used only for user defined identifiers',
399400
node.pos)
400401
node.ct_skip = true
@@ -404,7 +405,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool {
404405
node.ct_evaled = true
405406
return node.ct_skip
406407
} else {
407-
if node.ct_expr.name !in valid_comptime_not_user_defined {
408+
if node.ct_expr.name !in constants.valid_comptime_not_user_defined {
408409
c.note('`[if $node.ct_expr.name]` is deprecated. Use `[if $node.ct_expr.name ?]` instead',
409410
node.pos)
410411
node.ct_skip = node.ct_expr.name !in c.pref.compile_defines
@@ -555,20 +556,20 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
555556
}
556557
ast.Ident {
557558
cname := cond.name
558-
if cname in valid_comptime_if_os {
559+
if cname in constants.valid_comptime_if_os {
559560
mut is_os_target_equal := true
560561
if !c.pref.output_cross_c {
561562
target_os := c.pref.os.str().to_lower()
562563
is_os_target_equal = cname == target_os
563564
}
564565
return if is_os_target_equal { .eval } else { .skip }
565-
} else if cname in valid_comptime_if_compilers {
566+
} else if cname in constants.valid_comptime_if_compilers {
566567
return if pref.cc_from_string(cname) == c.pref.ccompiler_type {
567568
.eval
568569
} else {
569570
.skip
570571
}
571-
} else if cname in valid_comptime_if_platforms {
572+
} else if cname in constants.valid_comptime_if_platforms {
572573
if cname == 'aarch64' {
573574
c.note('use `arm64` instead of `aarch64`', pos)
574575
}
@@ -582,9 +583,9 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
582583
'rv32' { return if c.pref.arch == .rv32 { .eval } else { .skip } }
583584
else { return .unknown }
584585
}
585-
} else if cname in valid_comptime_if_cpu_features {
586+
} else if cname in constants.valid_comptime_if_cpu_features {
586587
return .unknown
587-
} else if cname in valid_comptime_if_other {
588+
} else if cname in constants.valid_comptime_if_other {
588589
match cname {
589590
'apk' {
590591
return if c.pref.is_apk { .eval } else { .skip }

vlib/v/checker/constants/constants.v

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
module constants
22

3-
// TODO: move all constants from `checker` to here, and replace the hardcoded one with the computed one
4-
pub const valid_comptime_not_user_defined = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux',
5-
'gnu', 'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux',
6-
'solaris', 'haiku', 'serenity', 'vinix', 'gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus',
7-
'amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32', 'x64', 'x32', 'little_endian',
8-
'big_endian', 'apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking',
9-
'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding', 'interpreter', 'es5',
10-
'profile', 'wasm32_emscripten']
3+
pub const (
4+
valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu',
5+
'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux',
6+
'solaris', 'haiku', 'serenity', 'vinix']
7+
valid_comptime_compression_types = ['none', 'zlib']
8+
valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
9+
valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32']
10+
valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian']
11+
valid_comptime_if_other = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
12+
'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
13+
'interpreter', 'es5', 'profile', 'wasm32_emscripten']
14+
valid_comptime_not_user_defined = all_valid_comptime_idents()
15+
)
16+
17+
fn all_valid_comptime_idents() []string {
18+
mut res := []string{}
19+
res << constants.valid_comptime_if_os
20+
res << constants.valid_comptime_if_compilers
21+
res << constants.valid_comptime_if_platforms
22+
res << constants.valid_comptime_if_cpu_features
23+
res << constants.valid_comptime_if_other
24+
return res
25+
}

0 commit comments

Comments
 (0)