diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index ffdfae85b9..e0a1c8d362 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -41,7 +41,7 @@ pub fn (g mut Gen) init() { g.definitions.writeln(c_headers) g.write_sorted_types() g.write_multi_return_types() - g.definitions.writeln('// end of definitions #endif') + g.definitions.writeln('// end of definitions') } pub fn (g mut Gen) write_multi_return_types() { @@ -498,9 +498,6 @@ fn (g mut Gen) expr(node ast.Expr) { receiver_name = typ_sym.name } name := '${receiver_name}_$it.name'.replace('.', '__') - if table.type_is_ptr(it.typ) { - g.write('&') - } g.write('${name}(') g.expr(it.expr) if it.args.len > 0 { @@ -618,34 +615,21 @@ fn (g mut Gen) write_sorted_types() { mut builtin_types := []table.TypeSymbol // builtin types // builtin types need to be on top builtins := ['string', 'array', 'KeyValue', 'map', 'Option'] - for builtin in builtins { - // typ := table.Type( g.table.type_idxs[builtin]) - typ := g.table.find_type(builtin) or { - continue - // panic('failed to find type $builtin') - } - builtin_types << typ - } // everything except builtin will get sorted - for t_name, t in g.table.type_idxs { - if t == 0 { - continue - } - if t_name in builtins { + for typ in g.table.types { + if typ.name in builtins { // || t.is_generic { + builtin_types << typ continue } - println(t_name) - x := g.table.get_type_symbol(table.Type(t)) - // types << g.table.get_type_symbol(table.Type(t)) - types << *x + types << typ } // sort structs types_sorted := g.sort_structs(types) // Generate C code g.definitions.writeln('// builtin types:') g.write_types(builtin_types) - g.definitions.writeln('//------------------\n') + g.definitions.writeln('//------------------\n #endbuiltin') g.write_types(types_sorted) } @@ -682,28 +666,26 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol { } // loop over types for t in types { + // create list of deps + mut field_deps := []string match t.info { table.Struct { - // create list of deps - mut field_deps := []string info := t.info as table.Struct for field in info.fields { // Need to handle fixed size arrays as well (`[10]Point`) // ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ } + dep := g.table.get_type_symbol(field.typ).name // skip if not in types list or already in deps - /* - if !(ft in type_names) || ft in field_deps { - continue - } - */ - field_deps << g.table.get_type_symbol(field.typ).name - // field_deps << ft // field.typ + if !(dep in type_names) || dep in field_deps { + continue + } + field_deps << dep } - // add type and dependant types to graph - dep_graph.add(t.name, field_deps) } else {} - } + } + // add type and dependant types to graph + dep_graph.add(t.name, field_deps) } // sort graph dep_graph_sorted := dep_graph.resolve() @@ -720,8 +702,5 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol { } } } - for x in types_sorted { - println(x.name) - } return types_sorted } diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 9452981cb0..290408848d 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -24,7 +24,7 @@ fn test_c_files() { ctext = ctext // unused warn mut b := builder.new_builder(pref.Preferences{}) b.module_search_paths = ['$vroot/vlib/v/gen/tests/'] - res := b.gen_c([path]).after('#endif') + res := b.gen_c([path]).after('#endbuiltin') if compare_texts(res, ctext) { eprintln('${term_ok} ${i}') } diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 9c7b3905fe..f8791ed7dc 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -1,3 +1,20 @@ +struct Two { +}; + +struct User { + int age; +}; + +struct One { + Two two; +}; + +// multi return structs +typedef struct { + int arg0; + string arg1; +} multi_return_int_string; +// end of definitions void foo(int a); void User_inc_age(User u, int n); int get_int(string a); @@ -15,10 +32,6 @@ int localmod__get_int_10(); int pi = 3; int pi2 = pi; -typedef struct { - int age; -} User; - typedef enum { Color_red, // 0 Color_green, // 1 @@ -72,7 +85,7 @@ i < 10; i++) { bool q = true || false; bool b2 = bools[0] || true; bool b3 = get_bool() || true; - int f = TODO_first(nums); + int f = array_int_first(nums); } void User_inc_age(User u, int n) { diff --git a/vlib/v/gen/tests/2.c b/vlib/v/gen/tests/2.c index cd4f16e04c..207ae3c053 100644 --- a/vlib/v/gen/tests/2.c +++ b/vlib/v/gen/tests/2.c @@ -1,3 +1,9 @@ +struct User { + string name; +}; + +// multi return structs +// end of definitions int function1(); void foo(int a); void init_user(); @@ -16,11 +22,6 @@ int function1() { void foo(int a) { } - -typedef struct { - string name; -} User; - void init_user() { User user = (User){ .name = tos3("Bob"), diff --git a/vlib/v/gen/tests/3.c b/vlib/v/gen/tests/3.c index 0d00389255..4dc6a9547d 100644 --- a/vlib/v/gen/tests/3.c +++ b/vlib/v/gen/tests/3.c @@ -1,8 +1,10 @@ -typedef struct { +struct User { int age; string name; -} User; +}; +// multi return structs +// end of definitions int main() { User user = (User){ }; diff --git a/vlib/v/gen/tests/4.c b/vlib/v/gen/tests/4.c index 6373b5b056..aa2c6e4bba 100644 --- a/vlib/v/gen/tests/4.c +++ b/vlib/v/gen/tests/4.c @@ -1,3 +1,19 @@ +struct Bar { + int a; +}; + +struct Foo { + string a; + Bar b; +}; + +// multi return structs +typedef struct { + int arg0; + string arg1; +} multi_return_int_string; + +// end of definitions multi_return_int_string mr_test(); int testa(); string testb(int a); @@ -17,7 +33,7 @@ int main() { a.a = tos3("da"); a.b.a = 111; string a1 = a.a; - int a2 = TODO_testa(b); + int a2 = Bar_testa(b); int c = testa(); c = 1; string d = testb(1); @@ -56,7 +72,7 @@ int testc(int a) { } int Foo_testa(Foo f) { - int a = TODO_testb(f); + int a = Foo_testb(f); a = 1; return 4; } @@ -68,12 +84,3 @@ int Foo_testb(Foo f) { int Bar_testa(Bar b) { return 4; } - -typedef struct { - int a; -} Bar; - -typedef struct { - string a; - Bar b; -} Foo;