1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: extract valid_comptime_ constants into v.checker.constants (#16371)

This commit is contained in:
Taegon Kim 2022-11-09 17:07:21 +09:00 committed by GitHub
parent a07f77ac52
commit ce79c9c876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 46 deletions

View File

@ -12,6 +12,7 @@ import v.util
import v.util.version
import v.errors
import v.pkgconfig
import v.checker.constants
const (
int_min = int(0x80000000)
@ -23,37 +24,16 @@ const (
)
pub const (
valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu',
'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux',
'solaris', 'haiku', 'serenity', 'vinix']
valid_comptime_compression_types = ['none', 'zlib']
valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32']
valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian']
valid_comptime_if_other = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
'interpreter', 'es5', 'profile', 'wasm32_emscripten']
valid_comptime_not_user_defined = all_valid_comptime_idents()
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice',
'sort', 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods)
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort',
'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods)
// TODO: remove `byte` from this list when it is no longer supported
reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8',
'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune']
reserved_type_names_chk = token.new_keywords_matcher_from_array_trie(reserved_type_names)
vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead'
reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', 'u16',
'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune']
reserved_type_names_chk = token.new_keywords_matcher_from_array_trie(reserved_type_names)
vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead'
)
fn all_valid_comptime_idents() []string {
mut res := []string{}
res << checker.valid_comptime_if_os
res << checker.valid_comptime_if_compilers
res << checker.valid_comptime_if_platforms
res << checker.valid_comptime_if_cpu_features
res << checker.valid_comptime_if_other
return res
}
[heap; minify]
pub struct Checker {
pref &pref.Preferences = unsafe { nil } // Preferences shared from V struct
@ -1644,7 +1624,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
for i, ident in node.defer_vars {
mut id := ident
if mut id.info is ast.IdentVar {
if id.comptime && id.name in checker.valid_comptime_not_user_defined {
if id.comptime && id.name in constants.valid_comptime_not_user_defined {
node.defer_vars[i] = ast.Ident{
scope: 0
name: ''

View File

@ -7,6 +7,7 @@ import v.pref
import v.token
import v.util
import v.pkgconfig
import v.checker.constants
fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
if node.left !is ast.EmptyExpr {
@ -29,8 +30,8 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
}
if node.is_embed {
// c.file.embedded_files << node.embed_file
if node.embed_file.compression_type !in valid_comptime_compression_types {
supported := valid_comptime_compression_types.map('.$it').join(', ')
if node.embed_file.compression_type !in constants.valid_comptime_compression_types {
supported := constants.valid_comptime_compression_types.map('.$it').join(', ')
c.error('not supported compression type: .${node.embed_file.compression_type}. supported: $supported',
node.pos)
}
@ -394,7 +395,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool {
}
if node.ct_expr is ast.Ident {
if node.ct_opt {
if node.ct_expr.name in valid_comptime_not_user_defined {
if node.ct_expr.name in constants.valid_comptime_not_user_defined {
c.error('optional `[if expression ?]` tags, can be used only for user defined identifiers',
node.pos)
node.ct_skip = true
@ -404,7 +405,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool {
node.ct_evaled = true
return node.ct_skip
} else {
if node.ct_expr.name !in valid_comptime_not_user_defined {
if node.ct_expr.name !in constants.valid_comptime_not_user_defined {
c.note('`[if $node.ct_expr.name]` is deprecated. Use `[if $node.ct_expr.name ?]` instead',
node.pos)
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
}
ast.Ident {
cname := cond.name
if cname in valid_comptime_if_os {
if cname in constants.valid_comptime_if_os {
mut is_os_target_equal := true
if !c.pref.output_cross_c {
target_os := c.pref.os.str().to_lower()
is_os_target_equal = cname == target_os
}
return if is_os_target_equal { .eval } else { .skip }
} else if cname in valid_comptime_if_compilers {
} else if cname in constants.valid_comptime_if_compilers {
return if pref.cc_from_string(cname) == c.pref.ccompiler_type {
.eval
} else {
.skip
}
} else if cname in valid_comptime_if_platforms {
} else if cname in constants.valid_comptime_if_platforms {
if cname == 'aarch64' {
c.note('use `arm64` instead of `aarch64`', pos)
}
@ -582,9 +583,9 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
'rv32' { return if c.pref.arch == .rv32 { .eval } else { .skip } }
else { return .unknown }
}
} else if cname in valid_comptime_if_cpu_features {
} else if cname in constants.valid_comptime_if_cpu_features {
return .unknown
} else if cname in valid_comptime_if_other {
} else if cname in constants.valid_comptime_if_other {
match cname {
'apk' {
return if c.pref.is_apk { .eval } else { .skip }

View File

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