From 75db3e44161727d634481d5d91c9776e924be5b5 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Mar 2020 05:25:11 +0100 Subject: [PATCH] cgen: fix ptr indexing --- vlib/builtin/map.v | 1 + vlib/v/checker/checker.v | 7 +++++++ vlib/v/gen/cgen.v | 6 ++++-- vlib/v/parser/fn.v | 1 - 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index b198735c83..948a18d517 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -328,6 +328,7 @@ fn (m map) exists(key string) bool { pub fn (m &map) keys() []string { mut keys := [''].repeat(m.size) + //mut keys := []string{len: m.size} if m.value_bytes == 0 { return keys } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 413e073c4e..8a329a674b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -311,7 +311,9 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr) pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.Type { typ := c.expr(selector_expr.expr) selector_expr.expr_type = typ + // if selector_expr.field == 'size' { // println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type') + // } typ_sym := c.table.get_type_symbol(typ) field_name := selector_expr.field if field := typ_sym.find_field(field_name) { @@ -880,6 +882,11 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type { } return table.byte_type } + else if table.type_is_ptr(typ) { + // byte* => byte + // bytes[0] is a byte, not byte* + return table.type_deref(typ) + } // else { // return table.int_type // } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 68c3b99c08..87f0c8af6f 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -590,11 +590,9 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('/* guard */') } ast.IndexExpr { - // g.index_expr(it) } ast.InfixExpr { - // sdf g.infix_expr(it) } ast.IntegerLiteral { @@ -763,8 +761,12 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('->') } else { + // g.write('. /*typ= $it.expr_type */') // ${g.typ(it.expr_type)} /') g.write('.') } + if it.expr_type == 0 { + verror('cgen: SelectorExpr typ=0 field=$it.field') + } g.write(it.field) } ast.Type { diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 3ef5414068..0edf3396d5 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -82,7 +82,6 @@ fn (p mut Parser) fn_decl() ast.FnDecl { p.next() rec_name = p.check_name() if p.tok.kind == .key_mut { - // p.next() rec_mut = true } rec_type = p.parse_type()