Revert "cgen: sort const inits/cleanups topologically too"
This reverts commit 38000f8622.
			
			
				pull/5750/head
			
			
		
							parent
							
								
									88248b1b66
								
							
						
					
					
						commit
						2425c05c42
					
				| 
						 | 
				
			
			@ -142,7 +142,6 @@ pub mut:
 | 
			
		|||
 | 
			
		||||
pub struct ConstField {
 | 
			
		||||
pub:
 | 
			
		||||
	mod      string
 | 
			
		||||
	name     string
 | 
			
		||||
	expr     Expr
 | 
			
		||||
	is_pub   bool
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue