v2: fix struct sorting & cgen tests

pull/3940/head
Joe Conigliaro 2020-03-06 22:01:56 +11:00
parent 49f3ce0571
commit 24dbfbcccf
6 changed files with 63 additions and 61 deletions

View File

@ -41,7 +41,7 @@ pub fn (g mut Gen) init() {
g.definitions.writeln(c_headers) g.definitions.writeln(c_headers)
g.write_sorted_types() g.write_sorted_types()
g.write_multi_return_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() { 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 receiver_name = typ_sym.name
} }
name := '${receiver_name}_$it.name'.replace('.', '__') name := '${receiver_name}_$it.name'.replace('.', '__')
if table.type_is_ptr(it.typ) {
g.write('&')
}
g.write('${name}(') g.write('${name}(')
g.expr(it.expr) g.expr(it.expr)
if it.args.len > 0 { if it.args.len > 0 {
@ -618,34 +615,21 @@ fn (g mut Gen) write_sorted_types() {
mut builtin_types := []table.TypeSymbol // builtin types mut builtin_types := []table.TypeSymbol // builtin types
// builtin types need to be on top // builtin types need to be on top
builtins := ['string', 'array', 'KeyValue', 'map', 'Option'] 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 // everything except builtin will get sorted
for t_name, t in g.table.type_idxs { for typ in g.table.types {
if t == 0 { if typ.name in builtins {
continue
}
if t_name in builtins {
// || t.is_generic { // || t.is_generic {
builtin_types << typ
continue continue
} }
println(t_name) types << typ
x := g.table.get_type_symbol(table.Type(t))
// types << g.table.get_type_symbol(table.Type(t))
types << *x
} }
// sort structs // sort structs
types_sorted := g.sort_structs(types) types_sorted := g.sort_structs(types)
// Generate C code // Generate C code
g.definitions.writeln('// builtin types:') g.definitions.writeln('// builtin types:')
g.write_types(builtin_types) g.write_types(builtin_types)
g.definitions.writeln('//------------------\n') g.definitions.writeln('//------------------\n #endbuiltin')
g.write_types(types_sorted) g.write_types(types_sorted)
} }
@ -682,28 +666,26 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol {
} }
// loop over types // loop over types
for t in types { for t in types {
// create list of deps
mut field_deps := []string
match t.info { match t.info {
table.Struct { table.Struct {
// create list of deps
mut field_deps := []string
info := t.info as table.Struct info := t.info as table.Struct
for field in info.fields { for field in info.fields {
// Need to handle fixed size arrays as well (`[10]Point`) // Need to handle fixed size arrays as well (`[10]Point`)
// ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ } // 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 // skip if not in types list or already in deps
/* if !(dep in type_names) || dep in field_deps {
if !(ft in type_names) || ft in field_deps { continue
continue }
} field_deps << dep
*/
field_deps << g.table.get_type_symbol(field.typ).name
// field_deps << ft // field.typ
} }
// add type and dependant types to graph
dep_graph.add(t.name, field_deps)
} }
else {} else {}
} }
// add type and dependant types to graph
dep_graph.add(t.name, field_deps)
} }
// sort graph // sort graph
dep_graph_sorted := dep_graph.resolve() 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 return types_sorted
} }

View File

@ -24,7 +24,7 @@ fn test_c_files() {
ctext = ctext // unused warn ctext = ctext // unused warn
mut b := builder.new_builder(pref.Preferences{}) mut b := builder.new_builder(pref.Preferences{})
b.module_search_paths = ['$vroot/vlib/v/gen/tests/'] 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) { if compare_texts(res, ctext) {
eprintln('${term_ok} ${i}') eprintln('${term_ok} ${i}')
} }

View File

@ -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 foo(int a);
void User_inc_age(User u, int n); void User_inc_age(User u, int n);
int get_int(string a); int get_int(string a);
@ -15,10 +32,6 @@ int localmod__get_int_10();
int pi = 3; int pi = 3;
int pi2 = pi; int pi2 = pi;
typedef struct {
int age;
} User;
typedef enum { typedef enum {
Color_red, // 0 Color_red, // 0
Color_green, // 1 Color_green, // 1
@ -72,7 +85,7 @@ i < 10; i++) {
bool q = true || false; bool q = true || false;
bool b2 = bools[0] || true; bool b2 = bools[0] || true;
bool b3 = get_bool() || 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) { void User_inc_age(User u, int n) {

View File

@ -1,3 +1,9 @@
struct User {
string name;
};
// multi return structs
// end of definitions
int function1(); int function1();
void foo(int a); void foo(int a);
void init_user(); void init_user();
@ -16,11 +22,6 @@ int function1() {
void foo(int a) { void foo(int a) {
} }
typedef struct {
string name;
} User;
void init_user() { void init_user() {
User user = (User){ User user = (User){
.name = tos3("Bob"), .name = tos3("Bob"),

View File

@ -1,8 +1,10 @@
typedef struct { struct User {
int age; int age;
string name; string name;
} User; };
// multi return structs
// end of definitions
int main() { int main() {
User user = (User){ User user = (User){
}; };

View File

@ -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(); multi_return_int_string mr_test();
int testa(); int testa();
string testb(int a); string testb(int a);
@ -17,7 +33,7 @@ int main() {
a.a = tos3("da"); a.a = tos3("da");
a.b.a = 111; a.b.a = 111;
string a1 = a.a; string a1 = a.a;
int a2 = TODO_testa(b); int a2 = Bar_testa(b);
int c = testa(); int c = testa();
c = 1; c = 1;
string d = testb(1); string d = testb(1);
@ -56,7 +72,7 @@ int testc(int a) {
} }
int Foo_testa(Foo f) { int Foo_testa(Foo f) {
int a = TODO_testb(f); int a = Foo_testb(f);
a = 1; a = 1;
return 4; return 4;
} }
@ -68,12 +84,3 @@ int Foo_testb(Foo f) {
int Bar_testa(Bar b) { int Bar_testa(Bar b) {
return 4; return 4;
} }
typedef struct {
int a;
} Bar;
typedef struct {
string a;
Bar b;
} Foo;