diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 8ed066ab97..554a3b94da 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -141,6 +141,7 @@ pub: is_method bool rec_mut bool // is receiver mutable is_c bool + no_body bool // just a definition `fn C.malloc()` } pub struct BranchStmt { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 73f087c1ad..6080311048 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -173,7 +173,7 @@ fn (g mut Gen) stmt(node ast.Stmt) { } } ast.FnDecl { - if it.is_c || it.name == 'malloc' { + if it.is_c || it.name == 'malloc' || it.no_body { return } g.reset_tmp_count() @@ -276,8 +276,8 @@ fn (g mut Gen) stmt(node ast.Stmt) { g.writeln('}') } ast.GlobalDecl { - // TODO - g.writeln('__global') + styp := g.typ(g.table.get_type_symbol(it.typ).name) + g.definitions.writeln('$styp $it.name; // global') } ast.GotoLabel { g.writeln('$it.name:') @@ -620,7 +620,8 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) { g.writeln('') } else { - g.writeln('$field_type_sym.name $name; // inited later') // = ') + styp := g.typ(field_type_sym.name) + g.definitions.writeln('$styp $name; // inited later') // = ') // TODO // g.expr(node.exprs[i]) } diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 5e4247c82c..627d8c362e 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -15,6 +15,7 @@ typedef struct { string arg1; } multi_return_int_string; // end of definitions #endif +int pi2; // inited later void foo(int a); void User_inc_age(User u, int n); int get_int(string a); @@ -30,7 +31,6 @@ void end(); void localmod__pub_foo(); int localmod__get_int_10(); #define pi 3 -int pi2; // inited later typedef enum { Color_red, // 0 diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index bd8deb233d..5f13a020c7 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -24,7 +24,7 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr { args: args muts: muts // tok: tok - + pos: tok.position() is_c: is_c or_block: ast.OrExpr{ @@ -155,6 +155,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl { }) } mut stmts := []ast.Stmt + no_body := p.tok.kind != .lcbr if p.tok.kind == .lcbr { stmts = p.parse_block() } @@ -175,6 +176,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl { is_method: is_method rec_mut: rec_mut is_c: is_c + no_body: no_body } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b82449789b..3acf8bff2c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1593,6 +1593,7 @@ fn (p mut Parser) global_decl() ast.GlobalDecl { return ast.GlobalDecl{ name: name + typ: typ } }