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

gen: impl as cast

This commit is contained in:
Joe Conigliaro 2020-03-18 23:55:46 +11:00
parent 5fb90e12b1
commit 96af21ff68
3 changed files with 17 additions and 5 deletions

View File

@ -433,8 +433,10 @@ mut:
pub struct AsCast {
pub:
expr Expr
typ table.Type
expr Expr
typ table.Type
mut:
expr_type table.Type
}
// e.g. `[unsafe_fn]`

View File

@ -567,8 +567,7 @@ fn (c mut Checker) stmt(node ast.Stmt) {
sym.map_info().key_type
}
else {
table.int_type
}
table.int_type}
}
scope.override_var(ast.Var{
name: it.key_var
@ -618,6 +617,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
return c.array_init(mut it)
}
ast.AsCast {
it.expr_type = c.expr(it.expr)
return it.typ
}
ast.AssignExpr {

View File

@ -562,7 +562,17 @@ fn (g mut Gen) expr(node ast.Expr) {
else {}
}
ast.AsCast {
g.write('/* as */')
styp := g.typ(it.typ)
expr_type_sym := g.table.get_type_symbol(it.expr_type)
if expr_type_sym.kind == .sum_type {
g.write('/* as */ *($styp*)')
g.expr(it.expr)
g.write('.obj')
}
else {
g.write('/* as */ ($styp)')
g.expr(it.expr)
}
}
ast.AssignExpr {
if ast.expr_is_blank_ident(it.left) {