From a5c784830b758f8164d6fa181b1cd0a259183d16 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 18 Jul 2021 20:29:34 +0800 Subject: [PATCH] all: simplify global_scope processing (#10848) --- cmd/tools/vast/vast.v | 11 +++-------- cmd/tools/vfmt.v | 8 ++------ cmd/tools/vtest-parser.v | 3 +-- vlib/v/ast/table.v | 4 ++++ vlib/v/ast/walker/walker_test.v | 5 +---- vlib/v/builder/builder.v | 8 ++------ vlib/v/doc/doc.v | 7 ++----- vlib/v/doc/module.v | 5 +---- vlib/v/fmt/fmt_keep_test.v | 4 +--- vlib/v/fmt/fmt_test.v | 4 +--- vlib/v/fmt/fmt_vlib_test.v | 4 +--- vlib/v/gen/native/macho_test.v | 4 +--- vlib/v/parser/comptime.v | 4 ++-- vlib/v/parser/parse_type.v | 2 +- vlib/v/parser/parser.v | 29 ++++++++++------------------- vlib/v/parser/v_parser_test.v | 9 ++------- 16 files changed, 35 insertions(+), 76 deletions(-) diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index e65f7d5ad8..fe2d5da891 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -131,13 +131,9 @@ fn json(file string) string { root: new_object() table: ast.new_table() pref: pref - global_scope: &ast.Scope{ - start_pos: 0 - parent: 0 - } } // parse file with comment - ast_file := parser.parse_file(file, t.table, .parse_comments, t.pref, t.global_scope) + ast_file := parser.parse_file(file, t.table, .parse_comments, t.pref) t.root = t.ast_file(ast_file) // generate the ast string s := json_print(t.root) @@ -146,9 +142,8 @@ fn json(file string) string { // the ast tree struct Tree { - table &ast.Table - pref &pref.Preferences - global_scope &ast.Scope + table &ast.Table + pref &pref.Preferences mut: root Node // the root of tree } diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index 3995800a95..ae10f1a3c7 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -151,9 +151,7 @@ fn (foptions &FormatOptions) format_file(file string) { } table := ast.new_table() // checker := checker.new_checker(table, prefs) - file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{ - parent: 0 - }) + file_ast := parser.parse_file(file, table, .parse_comments, prefs) // checker.check(file_ast) formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug) file_name := os.file_name(file) @@ -175,9 +173,7 @@ fn (foptions &FormatOptions) format_pipe() { input_text := os.get_raw_lines_joined() table := ast.new_table() // checker := checker.new_checker(table, prefs) - file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs, &ast.Scope{ - parent: 0 - }) + file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs) // checker.check(file_ast) formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug) print(formatted_content) diff --git a/cmd/tools/vtest-parser.v b/cmd/tools/vtest-parser.v index a85e5d2c31..c021f10fbf 100644 --- a/cmd/tools/vtest-parser.v +++ b/cmd/tools/vtest-parser.v @@ -61,8 +61,7 @@ fn main() { time.sleep(ms * time.millisecond) exit(ecode_timeout) }(context.timeout_ms) - _ := parser.parse_text(source, context.path, context.table, .skip_comments, context.pref, - context.scope) + _ := parser.parse_text(source, context.path, context.table, .skip_comments, context.pref) context.log('> worker ${pid:5} finished parsing $context.path') exit(0) } else { diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 674aa7a581..65f72fc2ba 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -16,6 +16,7 @@ pub mut: dumps map[int]string // needed for efficiently generating all _v_dump_expr_TNAME() functions imports []string // List of all imports modules []string // Topologically sorted list of all modules registered by the application + global_scope &Scope cflags []cflag.CFlag redefined_fns []string fn_generic_types map[string][][]Type // for generic functions @@ -167,6 +168,9 @@ mut: pub fn new_table() &Table { mut t := &Table{ type_symbols: []TypeSymbol{cap: 64000} + global_scope: &Scope{ + parent: 0 + } cur_fn: 0 } t.register_builtin_type_symbols() diff --git a/vlib/v/ast/walker/walker_test.v b/vlib/v/ast/walker/walker_test.v index 0365fbe782..1730339503 100644 --- a/vlib/v/ast/walker/walker_test.v +++ b/vlib/v/ast/walker/walker_test.v @@ -6,10 +6,7 @@ import v.pref fn parse_text(text string) &ast.File { tbl := ast.new_table() prefs := pref.new_preferences() - scope := &ast.Scope{ - parent: 0 - } - return parser.parse_text(text, '', tbl, .skip_comments, prefs, scope) + return parser.parse_text(text, '', tbl, .skip_comments, prefs) } struct NodeByOffset { diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index c99ed35300..a9379732d0 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -18,7 +18,6 @@ pub: mut: pref &pref.Preferences checker &checker.Checker - global_scope &ast.Scope out_name_c string out_name_js string max_nr_errors int = 100 @@ -56,9 +55,6 @@ pub fn new_builder(pref &pref.Preferences) Builder { pref: pref table: table checker: checker.new_checker(table, pref) - global_scope: &ast.Scope{ - parent: 0 - } compiled_dir: compiled_dir max_nr_errors: if pref.error_limit > 0 { pref.error_limit } else { 100 } cached_msvc: msvc @@ -68,7 +64,7 @@ pub fn new_builder(pref &pref.Preferences) Builder { pub fn (mut b Builder) front_stages(v_files []string) ? { util.timing_start('PARSE') - b.parsed_files = parser.parse_files(v_files, b.table, b.pref, b.global_scope) + b.parsed_files = parser.parse_files(v_files, b.table, b.pref) b.parse_imports() mut timers := util.get_timers() timers.show('SCAN') @@ -146,7 +142,7 @@ pub fn (mut b Builder) parse_imports() { ast_file.path, imp.pos) } // Add all imports referenced by these libs - parsed_files := parser.parse_files(v_files, b.table, b.pref, b.global_scope) + parsed_files := parser.parse_files(v_files, b.table, b.pref) for file in parsed_files { mut name := file.mod.name if name == '' { diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index f784a5ca16..316cd333da 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -96,7 +96,7 @@ pub struct Doc { pub mut: prefs &pref.Preferences = new_vdoc_preferences() base_path string - table &ast.Table = &ast.Table{} + table &ast.Table = ast.new_table() checker checker.Checker = checker.Checker{ table: 0 pref: 0 @@ -431,15 +431,12 @@ pub fn (mut d Doc) generate() ? { if d.with_comments { comments_mode = .toplevel_comments } - global_scope := &ast.Scope{ - parent: 0 - } mut file_asts := []ast.File{} for i, file_path in v_files { if i == 0 { d.parent_mod_name = get_parent_mod(d.base_path) or { '' } } - file_asts << parser.parse_file(file_path, d.table, comments_mode, d.prefs, global_scope) + file_asts << parser.parse_file(file_path, d.table, comments_mode, d.prefs) } return d.file_asts(file_asts) } diff --git a/vlib/v/doc/module.v b/vlib/v/doc/module.v index 2e3f10c2b9..2b1d3e7b08 100644 --- a/vlib/v/doc/module.v +++ b/vlib/v/doc/module.v @@ -40,10 +40,7 @@ fn get_parent_mod(input_dir string) ?string { return error('No V files found.') } tbl := ast.new_table() - scope := &ast.Scope{ - parent: 0 - } - file_ast := parser.parse_file(v_files[0], tbl, .skip_comments, prefs, scope) + file_ast := parser.parse_file(v_files[0], tbl, .skip_comments, prefs) if file_ast.mod.name == 'main' { return '' } diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index 5f2af07191..74078b4b35 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -54,9 +54,7 @@ fn test_fmt() { continue } table := ast.new_table() - file_ast := parser.parse_file(ipath, table, .parse_comments, fpref, &ast.Scope{ - parent: 0 - }) + file_ast := parser.parse_file(ipath, table, .parse_comments, fpref) result_ocontent := fmt.fmt(file_ast, table, fpref, false) if expected_ocontent != result_ocontent { fmt_bench.fail() diff --git a/vlib/v/fmt/fmt_test.v b/vlib/v/fmt/fmt_test.v index a1130c9b69..32d7445ad4 100644 --- a/vlib/v/fmt/fmt_test.v +++ b/vlib/v/fmt/fmt_test.v @@ -49,9 +49,7 @@ fn test_fmt() { continue } table := ast.new_table() - file_ast := parser.parse_file(ipath, table, .parse_comments, fpref, &ast.Scope{ - parent: 0 - }) + file_ast := parser.parse_file(ipath, table, .parse_comments, fpref) result_ocontent := fmt.fmt(file_ast, table, fpref, false) if expected_ocontent != result_ocontent { fmt_bench.fail() diff --git a/vlib/v/fmt/fmt_vlib_test.v b/vlib/v/fmt/fmt_vlib_test.v index 9315ff8bbb..00eecb08e2 100644 --- a/vlib/v/fmt/fmt_vlib_test.v +++ b/vlib/v/fmt/fmt_vlib_test.v @@ -47,9 +47,7 @@ fn test_vlib_fmt() { continue } table := ast.new_table() - file_ast := parser.parse_file(ipath, table, .parse_comments, fpref, &ast.Scope{ - parent: 0 - }) + file_ast := parser.parse_file(ipath, table, .parse_comments, fpref) result_ocontent := fmt.fmt(file_ast, table, fpref, false) if expected_ocontent != result_ocontent { fmt_bench.fail() diff --git a/vlib/v/gen/native/macho_test.v b/vlib/v/gen/native/macho_test.v index 849b4e3a00..8035e4c597 100644 --- a/vlib/v/gen/native/macho_test.v +++ b/vlib/v/gen/native/macho_test.v @@ -8,9 +8,7 @@ fn test_macho() { mut g := native.Gen{ pref: &pref.Preferences{} out_name: 'test.bin' - table: &ast.Table{ - cur_fn: 0 - } + table: ast.new_table() } g.generate_macho_header() g.generate_macho_footer() diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 2cd0f26a34..7fde8301ee 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -188,7 +188,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { } mut scope := &ast.Scope{ start_pos: 0 - parent: p.global_scope + parent: p.table.global_scope } $if trace_comptime ? { println('') @@ -197,7 +197,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { println('>>> end of template END') println('') } - mut file := parse_comptime(v_code, p.table, p.pref, scope, p.global_scope) + mut file := parse_comptime(v_code, p.table, p.pref, scope) file.path = tmpl_path // copy vars from current fn scope into vweb_tmpl scope for stmt in file.stmts { diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 6ac48f5188..718e78c85f 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -22,7 +22,7 @@ pub fn (mut p Parser) parse_array_type() ast.Type { } ast.Ident { mut show_non_const_error := false - if const_field := p.global_scope.find_const('${p.mod}.$size_expr.name') { + if const_field := p.table.global_scope.find_const('${p.mod}.$size_expr.name') { if const_field.expr is ast.IntegerLiteral { fixed_size = const_field.expr.val.int() } else { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 2ebb8eb7db..91be19e89d 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -50,7 +50,6 @@ mut: attrs []ast.Attr // attributes before next decl stmt expr_mod string // for constructing full type names in parse_type() scope &ast.Scope - global_scope &ast.Scope imports map[string]string // alias => mod_name ast_imports []ast.Import // mod_names used_imports []string // alias @@ -90,10 +89,6 @@ pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt { table: table pref: &pref.Preferences{} scope: scope - global_scope: &ast.Scope{ - start_pos: 0 - parent: 0 - } } p.init_parse_fns() util.timing_start('PARSE stmt') @@ -104,7 +99,7 @@ pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt { return p.stmt(false) } -pub fn parse_comptime(text string, table &ast.Table, pref &pref.Preferences, scope &ast.Scope, global_scope &ast.Scope) &ast.File { +pub fn parse_comptime(text string, table &ast.Table, pref &pref.Preferences, scope &ast.Scope) &ast.File { mut p := Parser{ scanner: scanner.new_scanner(text, .skip_comments, pref) table: table @@ -112,12 +107,11 @@ pub fn parse_comptime(text string, table &ast.Table, pref &pref.Preferences, sco scope: scope errors: []errors.Error{} warnings: []errors.Warning{} - global_scope: global_scope } return p.parse() } -pub fn parse_text(text string, path string, table &ast.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) &ast.File { +pub fn parse_text(text string, path string, table &ast.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences) &ast.File { mut p := Parser{ scanner: scanner.new_scanner(text, comments_mode, pref) comments_mode: comments_mode @@ -125,11 +119,10 @@ pub fn parse_text(text string, path string, table &ast.Table, comments_mode scan pref: pref scope: &ast.Scope{ start_pos: 0 - parent: global_scope + parent: table.global_scope } errors: []errors.Error{} warnings: []errors.Warning{} - global_scope: global_scope } p.set_path(path) return p.parse() @@ -176,7 +169,7 @@ pub fn (mut p Parser) set_path(path string) { } } -pub fn parse_file(path string, table &ast.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) &ast.File { +pub fn parse_file(path string, table &ast.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences) &ast.File { // NB: when comments_mode == .toplevel_comments, // the parser gives feedback to the scanner about toplevel statements, so that the scanner can skip // all the tricky inner comments. This is needed because we do not have a good general solution @@ -188,11 +181,10 @@ pub fn parse_file(path string, table &ast.Table, comments_mode scanner.CommentsM pref: pref scope: &ast.Scope{ start_pos: 0 - parent: global_scope + parent: table.global_scope } errors: []errors.Error{} warnings: []errors.Warning{} - global_scope: global_scope } p.set_path(path) return p.parse() @@ -213,7 +205,6 @@ pub fn parse_vet_file(path string, table_ &ast.Table, pref &pref.Preferences) (& } errors: []errors.Error{} warnings: []errors.Warning{} - global_scope: global_scope } p.set_path(path) if p.scanner.text.contains_any_substr(['\n ', ' \n']) { @@ -290,7 +281,7 @@ pub fn (mut p Parser) parse() &ast.File { auto_imports: p.auto_imports stmts: stmts scope: p.scope - global_scope: p.global_scope + global_scope: p.table.global_scope errors: p.errors warnings: p.warnings global_labels: p.global_labels @@ -330,7 +321,7 @@ fn (mut q Queue) run() { } } */ -pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences, global_scope &ast.Scope) []&ast.File { +pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences) []&ast.File { mut timers := util.new_timers(false) $if time_parsing ? { timers.should_print = true @@ -361,7 +352,7 @@ pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences, glo mut files := []&ast.File{} for path in paths { timers.start('parse_file $path') - files << parse_file(path, table, .skip_comments, pref, global_scope) + files << parse_file(path, table, .skip_comments, pref) timers.show('parse_file $path') } return files @@ -2873,7 +2864,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl { comments: comments } fields << field - p.global_scope.register(field) + p.table.global_scope.register(field) comments = [] if !is_block { break @@ -2975,7 +2966,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl { comments: comments } fields << field - p.global_scope.register(field) + p.table.global_scope.register(field) comments = [] if !is_block { break diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index dfd77e7f71..41dd65dae9 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -74,14 +74,9 @@ x := 10 5+7 8+4 ' - table := &ast.Table{ - cur_fn: 0 - } + table := ast.new_table() vpref := &pref.Preferences{} - gscope := &ast.Scope{ - parent: 0 - } - prog := parse_file(s, table, .skip_comments, vpref, gscope) + prog := parse_file(s, table, .skip_comments, vpref) mut checker := checker.new_checker(table, vpref) checker.check(prog) res := c.gen([prog], table, vpref)