From d9854f2abd5a016c798e7d30accc1aff5106a930 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 7 Mar 2020 22:37:03 +0100 Subject: [PATCH] cgen: fix mutable receivers --- vlib/v/ast/ast.v | 34 +++++++++++++++++----------------- vlib/v/gen/cgen.v | 15 +++++---------- vlib/v/gen/tests/1.c | 4 ++-- 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index b11eccb95c..aa18661b43 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -10,15 +10,15 @@ import ( pub type TypeDecl = AliasTypeDecl | SumTypeDecl -pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | -FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | -AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | -CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | +pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | +FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | +AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | +CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | ConcatExpr | Type | AsCast -pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | -ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | -HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | +pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | +ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | +HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | LineComment | MultiLineComment | AssertStmt | UnsafeStmt // pub type Type = StructType | ArrayType // pub struct StructType { @@ -443,15 +443,15 @@ pub: pub struct AliasTypeDecl { pub: - name string - is_pub bool + name string + is_pub bool parent_type table.Type } pub struct SumTypeDecl { pub: - name string - is_pub bool + name string + is_pub bool sub_types []table.Type } @@ -473,12 +473,12 @@ pub: pub struct AssignExpr { pub: - op token.Kind - pos token.Position - left Expr - val Expr -//mut: - //left_type table.Type + op token.Kind + pos token.Position + left Expr + val Expr + // mut: + // left_type table.Type } pub struct GotoLabel { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 6f92ef05f8..57ebfc4f11 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -317,7 +317,11 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) { } // Receiver is the first argument if it.is_method { - styp := g.typ(it.receiver.typ) + mut styp := g.typ(it.receiver.typ) + // if table.type_nr_muls(it.receiver.typ) > 0 { + if it.rec_mut { + styp += '*' + } g.write('$styp $it.receiver.name') // TODO mut g.definitions.write('$styp $it.receiver.name') @@ -415,15 +419,6 @@ fn (g mut Gen) expr(node ast.Expr) { g.call_args(it.args) g.write(')') g.is_c_call = false - /* - for i, expr in it.args { - g.expr(expr) - if i != it.args.len - 1 { - g.write(', ') - } - } - */ - } ast.CastExpr { if it.typ == table.string_type_idx { diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 2035a7e35b..efb9a7b282 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -17,7 +17,7 @@ typedef struct { // end of definitions #endif int pi2; // inited later void foo(int a); -void User_inc_age(User u, int n); +void User_inc_age(User* u, int n); int get_int(string a); bool get_bool(); int get_int2(); @@ -93,7 +93,7 @@ i < 10; i++) { println(string_add(s, d)); } -void User_inc_age(User u, int n) { +void User_inc_age(User* u, int n) { u.age += n; }