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

gen: add typedefs for optional enums & array clone receiver type fix

This commit is contained in:
Joe Conigliaro 2020-03-19 21:04:51 +11:00
parent be01a32f0b
commit 408553e967
2 changed files with 17 additions and 2 deletions

View File

@ -298,11 +298,18 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
}
// need to return `array_xxx` instead of `array`
method_call_expr.return_type = typ
if name == 'clone' {
method_call_expr.receiver_type = table.type_to_ptr(typ)
}
else {
method_call_expr.receiver_type = typ
}
return typ
}
else if typ_sym.kind == .array && name in ['first', 'last'] {
info := typ_sym.info as table.Array
method_call_expr.return_type = info.elem_type
method_call_expr.receiver_type = typ
return info.elem_type
}
if method := c.table.type_find_method(typ_sym, name) {

View File

@ -688,7 +688,15 @@ fn (g mut Gen) expr(node ast.Expr) {
}
ast.EnumVal {
// g.write('/*EnumVal*/${it.mod}${it.enum_name}_$it.val')
g.write(g.typ(it.typ))
styp := g.typ(it.typ)
if table.type_is_optional(it.typ) {
ostyp := styp + '_$it.val'
if !(ostyp in g.optionals) {
g.definitions.writeln('typedef Option $ostyp;')
g.optionals << ostyp
}
}
g.write(styp)
g.write('_$it.val')
}
ast.FloatLiteral {
@ -743,7 +751,7 @@ fn (g mut Gen) expr(node ast.Expr) {
if it.expr_type == 0 {
verror('method receiver type is 0, this means there are some uchecked exprs')
}
typ_sym := g.table.get_type_symbol(it.expr_type)
typ_sym := g.table.get_type_symbol(it.receiver_type)
// rec_sym := g.table.get_type_symbol(it.receiver_type)
mut receiver_name := typ_sym.name
if typ_sym.kind == .array && it.name in