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

vfmt: alias type and sum type + sum type fix

This commit is contained in:
Delyan Angelov
2020-03-07 18:37:55 +02:00
committed by GitHub
parent fbf80dc315
commit 5e541e1f11
12 changed files with 125 additions and 32 deletions

View File

@ -901,6 +901,7 @@ fn (p mut Parser) type_decl() {
})
if p.pass == .main {
p.cgen.consts << 'const char * __SumTypeNames__${name}[] = {'
p.cgen.consts << ' "$name",'
for ctype_name in ctype_names {
p.cgen.consts << ' "$ctype_name",'
}

View File

@ -831,7 +831,9 @@ fn (p mut Parser) factor() string {
is_sum_type := type_of_var in p.table.sum_types
if is_sum_type && vname.len > 0 {
// TODO: make this work for arbitrary sumtype expressions, not just simple vars
p.gen('${vname}.typ == 0 ? tos3("typeof(): typ == 0") : tos3(__SumTypeNames__${type_of_var}[${vname}.typ - 1])')
// NB: __SumTypeNames__[xxx][0] is the name of the sumtype itself;
// idx>0 are the names of the sumtype children
p.gen('tos3(__SumTypeNames__${type_of_var}[${vname}.typ])')
}else{
p.gen('tos3("$type_of_var")')
}