mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
comptime: implement field.is_enum (#16920)
This commit is contained in:
parent
c2eb4d7065
commit
413a8b5f87
@ -125,6 +125,7 @@ pub:
|
||||
is_array bool // `f []string` , TODO
|
||||
is_map bool // `f map[string]int` , TODO
|
||||
is_chan bool // `f chan int` , TODO
|
||||
is_enum bool // `f Enum` where Enum is an enum
|
||||
is_struct bool // `f Abc` where Abc is a struct , TODO
|
||||
is_alias bool // `f MyInt` where `type MyInt = int`, TODO
|
||||
//
|
||||
|
@ -766,7 +766,7 @@ fn (mut c Checker) check_comptime_is_field_selector(node ast.SelectorExpr) bool
|
||||
fn (mut c Checker) check_comptime_is_field_selector_bool(node ast.SelectorExpr) bool {
|
||||
if c.check_comptime_is_field_selector(node) {
|
||||
return node.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option',
|
||||
'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias']
|
||||
'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum']
|
||||
}
|
||||
return false
|
||||
}
|
||||
@ -787,6 +787,7 @@ fn (mut c Checker) get_comptime_selector_bool_field(field_name string) bool {
|
||||
'is_chan' { return field_sym.kind == .chan }
|
||||
'is_struct' { return field_sym.kind == .struct_ }
|
||||
'is_alias' { return field_sym.kind == .alias }
|
||||
'is_enum' { return field_sym.kind == .enum_ }
|
||||
else { return false }
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ fn (mut g Gen) get_comptime_selector_bool_field(field_name string) bool {
|
||||
'is_chan' { return field_sym.kind == .chan }
|
||||
'is_struct' { return field_sym.kind == .struct_ }
|
||||
'is_alias' { return field_sym.kind == .alias }
|
||||
'is_enum' { return field_sym.kind == .enum_ }
|
||||
else { return false }
|
||||
}
|
||||
}
|
||||
@ -571,7 +572,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
|
||||
}
|
||||
ast.SelectorExpr {
|
||||
if g.inside_comptime_for_field && cond.expr is ast.Ident
|
||||
&& (cond.expr as ast.Ident).name == g.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias'] {
|
||||
&& (cond.expr as ast.Ident).name == g.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum'] {
|
||||
ret_bool := g.get_comptime_selector_bool_field(cond.field_name)
|
||||
g.write(ret_bool.str())
|
||||
return ret_bool, true
|
||||
@ -744,6 +745,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
|
||||
g.writeln('\t${node.val_var}.is_chan = ${field_sym.kind == .chan};')
|
||||
g.writeln('\t${node.val_var}.is_struct = ${field_sym.kind == .struct_};')
|
||||
g.writeln('\t${node.val_var}.is_alias = ${field_sym.kind == .alias};')
|
||||
g.writeln('\t${node.val_var}.is_enum = ${field_sym.kind == .enum_};')
|
||||
//
|
||||
g.writeln('\t${node.val_var}.indirections = ${field.typ.nr_muls()};')
|
||||
//
|
||||
|
@ -6,6 +6,12 @@ struct Abc {
|
||||
name string
|
||||
}
|
||||
|
||||
enum JobTitle {
|
||||
manager
|
||||
executive
|
||||
worker
|
||||
}
|
||||
|
||||
struct Complex {
|
||||
s string
|
||||
i int
|
||||
@ -35,6 +41,7 @@ struct Complex {
|
||||
o_map_i ?map[int]int
|
||||
o_my_struct ?Abc
|
||||
o_my_struct_shared ?shared Abc
|
||||
jobtitle_enum JobTitle
|
||||
}
|
||||
|
||||
fn test_is_shared() {
|
||||
@ -117,6 +124,16 @@ fn test_is_alias() {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_is_enum() {
|
||||
$for f in Complex.fields {
|
||||
if f.name.contains('_enum') {
|
||||
assert f.is_enum, 'Complex.${f.name} should have f.is_enum set'
|
||||
} else {
|
||||
assert !f.is_enum, 'Complex.${f.name} should NOT have f.is_enum set'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_indirections() {
|
||||
$for f in Complex.fields {
|
||||
if f.name.contains('pointer') || f.name in ['my_struct_shared', 'o_my_struct_shared'] {
|
||||
|
Loading…
Reference in New Issue
Block a user