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

checker,cgen: implement support for the new FieldData.is_alias field (part 2, follow up to a6bf20f)

This commit is contained in:
Delyan Angelov 2022-12-29 19:03:18 +02:00
parent 6a8b6c010c
commit 6229f48830
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 34 additions and 8 deletions

View File

@ -750,9 +750,8 @@ fn (mut c Checker) check_comptime_is_field_selector(node ast.SelectorExpr) bool
[inline]
fn (mut c Checker) check_comptime_is_field_selector_bool(node ast.SelectorExpr) bool {
if c.check_comptime_is_field_selector(node) {
bool_fields := ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_optional', 'is_array',
'is_map', 'is_chan', 'is_struct']
return node.field_name in bool_fields
return node.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_optional',
'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias']
}
return false
}

View File

@ -32,6 +32,7 @@ fn (mut g Gen) get_comptime_selector_bool_field(field_name string) bool {
'is_map' { return field_sym.kind == .map }
'is_chan' { return field_sym.kind == .chan }
'is_struct' { return field_sym.kind == .struct_ }
'is_alias' { return field_sym.kind == .alias }
else { return false }
}
}
@ -488,7 +489,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist 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_optional', 'is_array', 'is_map', 'is_chan', 'is_struct'] {
&& (cond.expr as ast.Ident).name == g.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_optional', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias'] {
ret_bool := g.get_comptime_selector_bool_field(cond.field_name)
g.write(ret_bool.str())
return true
@ -639,6 +640,8 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
g.writeln('\t${node.val_var}.is_map = ${field_sym.kind == .map};')
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}.indirections = ${field.typ.nr_muls()};')
//
g.comptime_var_type_map['${node.val_var}.typ'] = styp

View File

@ -1,3 +1,5 @@
type MyInt = int
struct Abc {
x int
y int
@ -9,6 +11,7 @@ struct Complex {
i int
ch_i chan int
atomic_i atomic int
my_alias MyInt = 144
//
pointer1_i &int = unsafe { nil }
pointer2_i &&int = unsafe { nil }
@ -19,9 +22,10 @@ struct Complex {
my_struct Abc
my_struct_shared shared Abc
//
o_s ?string
o_i ?int
o_ch_i ?chan int = chan int{cap: 10}
o_s ?string
o_i ?int
o_ch_i ?chan int = chan int{cap: 10}
o_my_alias ?MyInt = 123
// o_atomic_i ?atomic int // TODO: cgen error, but should be probably a checker one, since optional atomics do not make sense
o_pointer1_i ?&int = unsafe { nil }
o_pointer2_i ?&&int = unsafe { nil }
@ -103,6 +107,16 @@ fn test_is_struct() {
}
}
fn test_is_alias() {
$for f in Complex.fields {
if f.name.contains('_alias') {
assert f.is_alias, 'Complex.${f.name} should have f.is_alias set'
} else {
assert !f.is_alias, 'Complex.${f.name} should NOT have f.is_alias set'
}
}
}
fn test_indirections() {
$for f in Complex.fields {
if f.name.contains('pointer') || f.name in ['my_struct_shared', 'o_my_struct_shared'] {

View File

@ -16,3 +16,7 @@ field: f
f is pub?: true
f is mut?: true
f is array?: true
field: zzz
zzz is pub?: true
zzz is mut?: true
zzz is alias?: true

View File

@ -1,3 +1,5 @@
type MyInt = int
struct Bb {
mut:
a int
@ -8,7 +10,8 @@ pub:
d map[string]int
e atomic int
pub mut:
f []f64
f []f64
zzz MyInt = 123
}
fn foo[U](val U) {
@ -33,6 +36,9 @@ fn foo[U](val U) {
$if field.is_map {
println(field.name + ' is map?: ${field.is_map}')
}
$if field.is_alias {
println(field.name + ' is alias?: ${field.is_alias}')
}
$if field.is_shared {
println(field.name + ' is shared?: ${field.is_shared}')
}