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

ast, cgen: restore Type.str() and fix error for it (#13815)

This commit is contained in:
yuyi 2022-03-24 17:45:06 +08:00 committed by GitHub
parent 1566f7f766
commit 78d9975798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 10 deletions

View File

@ -257,6 +257,10 @@ fn (ts TypeSymbol) dbg_common(mut res []string) {
res << 'language: $ts.language'
}
pub fn (t Type) str() string {
return 'ast.Type(0x$t.hex() = ${u32(t)})'
}
pub fn (t &Table) type_str(typ Type) string {
sym := t.sym(typ)
return sym.name

View File

@ -66,7 +66,7 @@ fn (mut g Gen) gen_sumtype_equality_fn(left_type ast.Type) string {
fn_builder.writeln('\tif (a._typ != b._typ) { return false; }')
for typ in info.variants {
variant := g.unwrap(typ)
fn_builder.writeln('\tif (a._typ == $variant.typ) {')
fn_builder.writeln('\tif (a._typ == $variant.typ.idx()) {')
name := '_$variant.sym.cname'
if variant.sym.kind == .string {
fn_builder.writeln('\t\treturn string__eq(*a.$name, *b.$name);')
@ -427,7 +427,7 @@ fn (mut g Gen) gen_interface_equality_fn(left_type ast.Type) string {
fn_builder.writeln('\t\tint idx = v_typeof_interface_idx_${ptr_styp}(a._typ);')
if info is ast.Interface {
for typ in info.types {
fn_builder.writeln('\t\tif (idx == $typ) {')
fn_builder.writeln('\t\tif (idx == $typ.idx()) {')
fn_builder.write_string('\t\t\treturn ')
match g.table.type_kind(typ) {
.struct_ {

View File

@ -472,7 +472,7 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, str_fn_
{_SLIT("${clean_sum_type_v_type_name}(\'"), $c.si_s_code, {.d_s = $val}},
{_SLIT("\')"), 0, {.d_c = 0 }}
}))'
fn_builder.write_string('\t\tcase $typ: return $res;')
fn_builder.write_string('\t\tcase $typ.idx(): return $res;')
} else {
mut val := '${func_name}(${deref}($typ_str*)x._$sym.cname'
if should_use_indent_func(sym.kind) && !sym_has_str_method {
@ -483,7 +483,7 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, str_fn_
{_SLIT("${clean_sum_type_v_type_name}("), $c.si_s_code, {.d_s = $val}},
{_SLIT(")"), 0, {.d_c = 0 }}
}))'
fn_builder.write_string('\t\tcase $typ: return $res;')
fn_builder.write_string('\t\tcase $typ.idx(): return $res;')
}
}
fn_builder.writeln('\t\tdefault: return _SLIT("unknown sum type value");')

View File

@ -812,7 +812,7 @@ pub fn (mut g Gen) write_typeof_functions() {
g.writeln('\t\tcase $tidx: return "${util.strip_main_name(sym.name)}";')
for v in sum_info.variants {
subtype := g.table.sym(v)
g.writeln('\t\tcase $v: return "${util.strip_main_name(subtype.name)}";')
g.writeln('\t\tcase $v.idx(): return "${util.strip_main_name(subtype.name)}";')
}
g.writeln('\t\tdefault: return "unknown ${util.strip_main_name(sym.name)}";')
g.writeln('\t}')
@ -832,7 +832,7 @@ pub fn (mut g Gen) write_typeof_functions() {
g.writeln('\tswitch(sidx) {')
g.writeln('\t\tcase $tidx: return ${int(ityp)};')
for v in sum_info.variants {
g.writeln('\t\tcase $v: return ${int(v)};')
g.writeln('\t\tcase $v.idx(): return ${int(v)};')
}
g.writeln('\t\tdefault: return ${int(ityp)};')
g.writeln('\t}')

View File

@ -402,10 +402,10 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) bool {
}
if cond.op == .key_is {
g.write('$exp_type == $got_type')
g.write('$exp_type.idx() == $got_type.idx()')
return exp_type == got_type
} else {
g.write('$exp_type != $got_type')
g.write('$exp_type.idx() != $got_type.idx()')
return exp_type != got_type
}
}
@ -544,7 +544,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
// field_sym := g.table.sym(field.typ)
// g.writeln('\t${node.val_var}.typ = _SLIT("$field_sym.name");')
styp := field.typ
g.writeln('\t${node.val_var}.typ = $styp;')
g.writeln('\t${node.val_var}.typ = $styp.idx();')
g.writeln('\t${node.val_var}.is_pub = $field.is_pub;')
g.writeln('\t${node.val_var}.is_mut = $field.is_mut;')
g.writeln('\t${node.val_var}.is_shared = ${field.typ.has_flag(.shared_f)};')

View File

@ -181,7 +181,7 @@ fn (mut g Gen) gen_sumtype_enc_dec(sym ast.TypeSymbol, mut enc strings.Builder,
g.definitions.writeln('static inline $sym.cname ${variant_typ}_to_sumtype_${sym.cname}($variant_typ* x);')
// ENCODING
enc.writeln('\tif (val._typ == $variant) {')
enc.writeln('\tif (val._typ == $variant.idx()) {')
$if json_no_inline_sumtypes ? {
if variant_sym.kind == .enum_ {
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", ${js_enc_name('u64')}(*val._$variant_typ));')