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

gen: fix sum type casting & map init

This commit is contained in:
Joe Conigliaro 2020-03-18 01:13:55 +11:00
parent f5e44fbef2
commit f965a7d0e6

View File

@ -339,27 +339,25 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
} }
fn (g &Gen) is_sum_cast(got_type table.Type, exp_type table.Type) bool { // use instead of expr() when you need to cast to sum type (can add other casts also)
fn (g mut Gen) expr_with_cast(got_type table.Type, exp_type table.Type, expr ast.Expr) {
// cast to sum type
if exp_type != table.void_type && exp_type != 0 && got_type != 0{ if exp_type != table.void_type && exp_type != 0 && got_type != 0{
exp_sym := g.table.get_type_symbol(exp_type) exp_sym := g.table.get_type_symbol(exp_type)
// got_sym := g.table.get_type_symbol(got_type)
if exp_sym.kind == .sum_type { if exp_sym.kind == .sum_type {
sum_info := exp_sym.info as table.SumType sum_info := exp_sym.info as table.SumType
if got_type in sum_info.variants { if got_type in sum_info.variants {
return true got_styp := g.typ(got_type)
exp_styp := g.typ(exp_type)
got_idx := table.type_idx(got_type)
g.write('/* SUM TYPE CAST */ ($exp_styp) {.obj = memdup(&(${got_styp}[]) {')
g.expr(expr)
g.writeln('}, sizeof($got_styp)), .typ = $got_idx};')
} }
} }
} }
return false // no cast
}
fn (g mut Gen) sum_cast(got_type table.Type, exp_type table.Type, expr ast.Expr) {
got_styp := g.typ(got_type)
exp_styp := g.typ(exp_type)
got_idx := table.type_idx(got_type)
g.write('/* SUM TYPE CAST */ ($exp_styp) {.obj = memdup(&(${got_styp}[]) {')
g.expr(expr) g.expr(expr)
g.writeln('}, sizeof($got_styp)), .typ = $got_idx};')
} }
fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
@ -430,17 +428,15 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
else {} else {}
} }
mut is_sum_cast := false is_decl := assign_stmt.op == .decl_assign
if assign_stmt.op == .decl_assign { if is_decl {
g.write('$styp ') g.write('$styp ')
} else {
is_sum_cast = g.is_sum_cast(assign_stmt.right_types[i], assign_stmt.left_types[i])
} }
g.expr(ident) g.expr(ident)
if !is_fixed_array_init { if !is_fixed_array_init {
g.write(' = ') g.write(' = ')
if is_sum_cast { if !is_decl {
g.sum_cast(ident_var_info.typ, assign_stmt.left_types[i], val) g.expr_with_cast(assign_stmt.left_types[i], ident_var_info.typ, val)
} else { } else {
g.expr(val) g.expr(val)
} }
@ -593,11 +589,7 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write(', ') g.write(', ')
} }
g.is_assign_expr = false g.is_assign_expr = false
if g.is_sum_cast(it.left_type, it.right_type) { g.expr_with_cast(it.right_type, it.left_type, it.val)
g.sum_cast(it.left_type, it.right_type, it.val)
} else {
g.expr(it.val)
}
if g.is_array_set { if g.is_array_set {
g.write(' })') g.write(' })')
g.is_array_set = false g.is_array_set = false
@ -755,14 +747,16 @@ fn (g mut Gen) expr(node ast.Expr) {
ast.MapInit { ast.MapInit {
key_typ_sym := g.table.get_type_symbol(it.key_type) key_typ_sym := g.table.get_type_symbol(it.key_type)
value_typ_sym := g.table.get_type_symbol(it.value_type) value_typ_sym := g.table.get_type_symbol(it.value_type)
key_typ_str := key_typ_sym.name.replace('.', '__')
value_typ_str := value_typ_sym.name.replace('.', '__')
size := it.vals.len size := it.vals.len
if size > 0 { if size > 0 {
g.write('new_map_init($size, sizeof($value_typ_sym.name), (${key_typ_sym.name}[$size]){') g.write('new_map_init($size, sizeof($value_typ_str), (${key_typ_str}[$size]){')
for expr in it.keys { for expr in it.keys {
g.expr(expr) g.expr(expr)
g.write(', ') g.write(', ')
} }
g.write('}, (${value_typ_sym.name}[$size]){') g.write('}, (${value_typ_str}[$size]){')
for expr in it.vals { for expr in it.vals {
g.expr(expr) g.expr(expr)
g.write(', ') g.write(', ')
@ -770,7 +764,7 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('})') g.write('})')
} }
else { else {
g.write('new_map(1, sizeof($value_typ_sym.name))') g.write('new_map(1, sizeof($value_typ_str))')
} }
} }
ast.MethodCallExpr { ast.MethodCallExpr {
@ -979,7 +973,7 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
if right_sym.kind == .array { if right_sym.kind == .array {
// push an array => PUSH_MANY // push an array => PUSH_MANY
g.write('_PUSH_MANY(&') g.write('_PUSH_MANY(&')
g.expr(node.left) g.expr_with_cast(node.right_type, node.left_type, node.left)
g.write(', (') g.write(', (')
g.expr(node.right) g.expr(node.right)
styp := g.typ(node.left_type) styp := g.typ(node.left_type)
@ -991,7 +985,7 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
elem_type_str := g.typ(info.elem_type) elem_type_str := g.typ(info.elem_type)
// g.write('array_push(&') // g.write('array_push(&')
g.write('_PUSH(&') g.write('_PUSH(&')
g.expr(node.left) g.expr_with_cast(node.right_type, info.elem_type, node.left)
g.write(', (') g.write(', (')
g.expr(node.right) g.expr(node.right)
g.write('), $tmp, $elem_type_str)') g.write('), $tmp, $elem_type_str)')
@ -1229,12 +1223,7 @@ fn (g mut Gen) return_statement(it ast.Return) {
} }
// g.write('/*OPTIONAL*/') // g.write('/*OPTIONAL*/')
} }
if g.is_sum_cast(it.types[0], g.fn_decl.return_type) { g.expr_with_cast(it.types[0], g.fn_decl.return_type, it.exprs[0])
g.sum_cast(it.types[0], g.fn_decl.return_type, it.exprs[0])
}
else {
g.expr(it.exprs[0])
}
} }
g.writeln(';') g.writeln(';')
} }