cgen: automatic dereference and match fix

pull/4057/head
Alexander Medvednikov 2020-03-18 16:07:52 +01:00
parent d81d804cb6
commit c514f0b672
3 changed files with 20 additions and 14 deletions

View File

@ -355,7 +355,7 @@ mut:
is_expr bool // returns a value
return_type table.Type
cond_type table.Type // type of `x` in `match x {`
// expected_type table.Type // for debugging only
expected_type table.Type // for debugging only
is_sum_type bool
}

View File

@ -456,6 +456,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
})
}
}
c.expected_type = table.void_type
}
pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
@ -839,7 +840,7 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
node.is_expr = c.expected_type != table.void_type
// node.expected_type = c.expected_type
node.expected_type = c.expected_type
cond_type := c.expr(node.cond)
if cond_type == 0 {
c.error('match 0 cond type', node.pos)

View File

@ -1054,11 +1054,12 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
g.writeln(') {')
}
}
if node.is_sum_type && branch.exprs.len > 0 {
// g.writeln('/* M sum_type=$node.is_sum_type is_expr=$node.is_expr exp_type=${g.typ(node.expected_type)}*/')
if node.is_sum_type && branch.exprs.len > 0 && !node.is_expr {
// The first node in expr is an ast.Type
// Use it to generate `it` variable.
fe := branch.exprs[0]
match fe {
first_expr := branch.exprs[0]
match first_expr {
ast.Type {
it_type := g.typ(it.typ)
// g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it')
@ -1302,6 +1303,10 @@ fn (g mut Gen) return_statement(it ast.Return) {
}
// g.write('/*OPTIONAL*/')
}
if !table.type_is_ptr(g.fn_decl.return_type) && table.type_is_ptr(it.types[0]) {
// Automatic Dereference
g.write('*')
}
g.expr_with_cast(it.types[0], g.fn_decl.return_type, it.exprs[0])
}
g.writeln(';')