From 2425c05c42725c7c2fcd4f2d277b726de8be66f1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 8 Jul 2020 12:56:56 +0200 Subject: [PATCH] Revert "cgen: sort const inits/cleanups topologically too" This reverts commit 38000f862260fafa310f92c07f052ef0fc5e48fe. --- vlib/v/ast/ast.v | 1 - vlib/v/builder/builder.v | 3 +-- vlib/v/gen/cgen.v | 35 +++++++++++----------------- vlib/v/parser/parser.v | 1 - vlib/v/table/table.v | 3 ++- vlib/v/tests/const_init_order_test.v | 10 -------- 6 files changed, 16 insertions(+), 37 deletions(-) delete mode 100644 vlib/v/tests/const_init_order_test.v diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2a167f8b9a..5ec7cb3bf7 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -142,7 +142,6 @@ pub mut: pub struct ConstField { pub: - mod string name string expr Expr is_pub bool diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 4e4d318466..461b6e5256 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -12,10 +12,10 @@ import v.depgraph pub struct Builder { pub: + table &table.Table compiled_dir string // contains os.real_path() of the dir of the final file beeing compiled, or the dir itself when doing `v .` module_path string mut: - table &table.Table checker checker.Checker pref &pref.Preferences global_scope &ast.Scope @@ -146,7 +146,6 @@ pub fn (mut b Builder) resolve_deps() { } } } - b.table.modules = mods b.parsed_files = reordered_parsed_files } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 70fad1ef14..5c96aed974 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -35,8 +35,8 @@ mut: typedefs2 strings.Builder type_definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file) definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file) - inits map[string]strings.Builder // contents of `void _vinit(){}` - cleanups map[string]strings.Builder // contents of `void _vcleanup(){}` + inits strings.Builder // contents of `void _vinit(){}` + cleanups strings.Builder // contents of `void _vcleanup(){}` gowrappers strings.Builder // all go callsite wrappers stringliterals strings.Builder // all string literals (they depend on tos3() beeing defined auto_str_funcs strings.Builder // function bodies of all auto generated _str funcs @@ -119,6 +119,8 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string stringliterals: strings.new_builder(100) auto_str_funcs: strings.new_builder(100) comptime_defines: strings.new_builder(100) + inits: strings.new_builder(100) + cleanups: strings.new_builder(100) pcs_declarations: strings.new_builder(100) hotcode_definitions: strings.new_builder(100) options: strings.new_builder(100) @@ -132,10 +134,6 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string indent: -1 module_built: pref.path.after('vlib/') } - for mod in g.table.modules { - g.inits[mod] = strings.new_builder(100) - g.cleanups[mod] = strings.new_builder(100) - } g.init() // mut tests_inited := false @@ -2729,7 +2727,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { styp := g.typ(it.typ) g.definitions.writeln('$styp _const_$name = $val; // fixed array const') } else { - g.const_decl_init_later(field.mod, name, val, field.typ) + g.const_decl_init_later(name, val, field.typ) } } ast.StringLiteral { @@ -2739,7 +2737,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { } } else { - g.const_decl_init_later(field.mod, name, val, field.typ) + g.const_decl_init_later(name, val, field.typ) } } } @@ -2754,20 +2752,20 @@ fn (mut g Gen) const_decl_simple_define(name, val string) { g.definitions.writeln(val) } -fn (mut g Gen) const_decl_init_later(mod, name, val string, typ table.Type) { +fn (mut g Gen) const_decl_init_later(name, val string, typ table.Type) { // Initialize more complex consts in `void _vinit(){}` // (C doesn't allow init expressions that can't be resolved at compile time). styp := g.typ(typ) // cname := '_const_$name' g.definitions.writeln('$styp $cname; // inited later') - g.inits[mod].writeln('\t$cname = $val;') + g.inits.writeln('\t$cname = $val;') if g.pref.autofree { if styp.starts_with('array_') { - g.cleanups[mod].writeln('\tarray_free(&$cname);') + g.cleanups.writeln('\tarray_free(&$cname);') } if styp == 'string' { - g.cleanups[mod].writeln('\tstring_free(&$cname);') + g.cleanups.writeln('\tstring_free(&$cname);') } } } @@ -3015,10 +3013,8 @@ fn (mut g Gen) write_init_function() { } g.writeln('\tbuiltin_init();') g.writeln('\tvinit_string_literals();') - // - for mod_name in g.table.modules { - g.writeln('\t// Initializations for module $mod_name :') - g.write(g.inits[mod_name].str()) + g.write(g.inits.str()) + for mod_name in g.table.imports { init_fn_name := '${mod_name}.init' if _ := g.table.find_fn(init_fn_name) { mod_c_name := util.no_dots(mod_name) @@ -3026,7 +3022,6 @@ fn (mut g Gen) write_init_function() { g.writeln('\t${init_fn_c_name}();') } } - // g.writeln('}') if g.pref.printfn_list.len > 0 && '_vinit' in g.pref.printfn_list { println(g.out.after(fn_vinit_start_pos)) @@ -3035,11 +3030,7 @@ fn (mut g Gen) write_init_function() { fn_vcleanup_start_pos := g.out.len g.writeln('void _vcleanup() {') // g.writeln('puts("cleaning up...");') - reversed_table_modules := g.table.modules.reverse() - for mod_name in reversed_table_modules { - g.writeln('\t// Cleanups for module $mod_name :') - g.writeln(g.cleanups[mod_name].str()) - } + g.writeln(g.cleanups.str()) // g.writeln('\tfree(g_str_buf);') g.writeln('}') if g.pref.printfn_list.len > 0 && '_vcleanup' in g.pref.printfn_list { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 62790ca2cc..d259cb1cdd 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1376,7 +1376,6 @@ fn (mut p Parser) const_decl() ast.ConstDecl { expr := p.expr(0) field := ast.ConstField{ name: full_name - mod: p.mod expr: expr pos: pos comments: comments diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 4b88ffb041..04eb6c3288 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -13,7 +13,7 @@ pub mut: type_idxs map[string]int fns map[string]Fn imports []string // List of all imports - modules []string // Topologically sorted list of all modules registered by the application + modules []string // List of all modules registered by the application cflags []cflag.CFlag redefined_fns []string fn_gen_types map[string][]Type // for generic functions @@ -510,3 +510,4 @@ pub fn (table &Table) sumtype_has_variant(parent Type, variant Type) bool { } return false } + diff --git a/vlib/v/tests/const_init_order_test.v b/vlib/v/tests/const_init_order_test.v deleted file mode 100644 index ac9eaa9b59..0000000000 --- a/vlib/v/tests/const_init_order_test.v +++ /dev/null @@ -1,10 +0,0 @@ -import rand - -const ( - my_random_letter_const = byte(65 + rand.u32n(25)) -) - -fn test_rand_is_initialized_before_main(){ - eprintln('random letter: $my_random_letter_const.str() | ASCII code: $my_random_letter_const') - assert my_random_letter_const.is_capital() -}