1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: simplify global_scope processing (#10848)

This commit is contained in:
yuyi 2021-07-18 20:29:34 +08:00 committed by GitHub
parent eb65ad078d
commit a5c784830b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 35 additions and 76 deletions

View File

@ -131,13 +131,9 @@ fn json(file string) string {
root: new_object() root: new_object()
table: ast.new_table() table: ast.new_table()
pref: pref pref: pref
global_scope: &ast.Scope{
start_pos: 0
parent: 0
}
} }
// parse file with comment // 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) t.root = t.ast_file(ast_file)
// generate the ast string // generate the ast string
s := json_print(t.root) s := json_print(t.root)
@ -146,9 +142,8 @@ fn json(file string) string {
// the ast tree // the ast tree
struct Tree { struct Tree {
table &ast.Table table &ast.Table
pref &pref.Preferences pref &pref.Preferences
global_scope &ast.Scope
mut: mut:
root Node // the root of tree root Node // the root of tree
} }

View File

@ -151,9 +151,7 @@ fn (foptions &FormatOptions) format_file(file string) {
} }
table := ast.new_table() table := ast.new_table()
// checker := checker.new_checker(table, prefs) // checker := checker.new_checker(table, prefs)
file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{ file_ast := parser.parse_file(file, table, .parse_comments, prefs)
parent: 0
})
// checker.check(file_ast) // checker.check(file_ast)
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug) formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
file_name := os.file_name(file) file_name := os.file_name(file)
@ -175,9 +173,7 @@ fn (foptions &FormatOptions) format_pipe() {
input_text := os.get_raw_lines_joined() input_text := os.get_raw_lines_joined()
table := ast.new_table() table := ast.new_table()
// checker := checker.new_checker(table, prefs) // checker := checker.new_checker(table, prefs)
file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs, &ast.Scope{ file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs)
parent: 0
})
// checker.check(file_ast) // checker.check(file_ast)
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug) formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
print(formatted_content) print(formatted_content)

View File

@ -61,8 +61,7 @@ fn main() {
time.sleep(ms * time.millisecond) time.sleep(ms * time.millisecond)
exit(ecode_timeout) exit(ecode_timeout)
}(context.timeout_ms) }(context.timeout_ms)
_ := parser.parse_text(source, context.path, context.table, .skip_comments, context.pref, _ := parser.parse_text(source, context.path, context.table, .skip_comments, context.pref)
context.scope)
context.log('> worker ${pid:5} finished parsing $context.path') context.log('> worker ${pid:5} finished parsing $context.path')
exit(0) exit(0)
} else { } else {

View File

@ -16,6 +16,7 @@ pub mut:
dumps map[int]string // needed for efficiently generating all _v_dump_expr_TNAME() functions dumps map[int]string // needed for efficiently generating all _v_dump_expr_TNAME() functions
imports []string // List of all imports imports []string // List of all imports
modules []string // Topologically sorted list of all modules registered by the application modules []string // Topologically sorted list of all modules registered by the application
global_scope &Scope
cflags []cflag.CFlag cflags []cflag.CFlag
redefined_fns []string redefined_fns []string
fn_generic_types map[string][][]Type // for generic functions fn_generic_types map[string][][]Type // for generic functions
@ -167,6 +168,9 @@ mut:
pub fn new_table() &Table { pub fn new_table() &Table {
mut t := &Table{ mut t := &Table{
type_symbols: []TypeSymbol{cap: 64000} type_symbols: []TypeSymbol{cap: 64000}
global_scope: &Scope{
parent: 0
}
cur_fn: 0 cur_fn: 0
} }
t.register_builtin_type_symbols() t.register_builtin_type_symbols()

View File

@ -6,10 +6,7 @@ import v.pref
fn parse_text(text string) &ast.File { fn parse_text(text string) &ast.File {
tbl := ast.new_table() tbl := ast.new_table()
prefs := pref.new_preferences() prefs := pref.new_preferences()
scope := &ast.Scope{ return parser.parse_text(text, '', tbl, .skip_comments, prefs)
parent: 0
}
return parser.parse_text(text, '', tbl, .skip_comments, prefs, scope)
} }
struct NodeByOffset { struct NodeByOffset {

View File

@ -18,7 +18,6 @@ pub:
mut: mut:
pref &pref.Preferences pref &pref.Preferences
checker &checker.Checker checker &checker.Checker
global_scope &ast.Scope
out_name_c string out_name_c string
out_name_js string out_name_js string
max_nr_errors int = 100 max_nr_errors int = 100
@ -56,9 +55,6 @@ pub fn new_builder(pref &pref.Preferences) Builder {
pref: pref pref: pref
table: table table: table
checker: checker.new_checker(table, pref) checker: checker.new_checker(table, pref)
global_scope: &ast.Scope{
parent: 0
}
compiled_dir: compiled_dir compiled_dir: compiled_dir
max_nr_errors: if pref.error_limit > 0 { pref.error_limit } else { 100 } max_nr_errors: if pref.error_limit > 0 { pref.error_limit } else { 100 }
cached_msvc: msvc 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) ? { pub fn (mut b Builder) front_stages(v_files []string) ? {
util.timing_start('PARSE') 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() b.parse_imports()
mut timers := util.get_timers() mut timers := util.get_timers()
timers.show('SCAN') timers.show('SCAN')
@ -146,7 +142,7 @@ pub fn (mut b Builder) parse_imports() {
ast_file.path, imp.pos) ast_file.path, imp.pos)
} }
// Add all imports referenced by these libs // 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 { for file in parsed_files {
mut name := file.mod.name mut name := file.mod.name
if name == '' { if name == '' {

View File

@ -96,7 +96,7 @@ pub struct Doc {
pub mut: pub mut:
prefs &pref.Preferences = new_vdoc_preferences() prefs &pref.Preferences = new_vdoc_preferences()
base_path string base_path string
table &ast.Table = &ast.Table{} table &ast.Table = ast.new_table()
checker checker.Checker = checker.Checker{ checker checker.Checker = checker.Checker{
table: 0 table: 0
pref: 0 pref: 0
@ -431,15 +431,12 @@ pub fn (mut d Doc) generate() ? {
if d.with_comments { if d.with_comments {
comments_mode = .toplevel_comments comments_mode = .toplevel_comments
} }
global_scope := &ast.Scope{
parent: 0
}
mut file_asts := []ast.File{} mut file_asts := []ast.File{}
for i, file_path in v_files { for i, file_path in v_files {
if i == 0 { if i == 0 {
d.parent_mod_name = get_parent_mod(d.base_path) or { '' } 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) return d.file_asts(file_asts)
} }

View File

@ -40,10 +40,7 @@ fn get_parent_mod(input_dir string) ?string {
return error('No V files found.') return error('No V files found.')
} }
tbl := ast.new_table() tbl := ast.new_table()
scope := &ast.Scope{ file_ast := parser.parse_file(v_files[0], tbl, .skip_comments, prefs)
parent: 0
}
file_ast := parser.parse_file(v_files[0], tbl, .skip_comments, prefs, scope)
if file_ast.mod.name == 'main' { if file_ast.mod.name == 'main' {
return '' return ''
} }

View File

@ -54,9 +54,7 @@ fn test_fmt() {
continue continue
} }
table := ast.new_table() table := ast.new_table()
file_ast := parser.parse_file(ipath, table, .parse_comments, fpref, &ast.Scope{ file_ast := parser.parse_file(ipath, table, .parse_comments, fpref)
parent: 0
})
result_ocontent := fmt.fmt(file_ast, table, fpref, false) result_ocontent := fmt.fmt(file_ast, table, fpref, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()

View File

@ -49,9 +49,7 @@ fn test_fmt() {
continue continue
} }
table := ast.new_table() table := ast.new_table()
file_ast := parser.parse_file(ipath, table, .parse_comments, fpref, &ast.Scope{ file_ast := parser.parse_file(ipath, table, .parse_comments, fpref)
parent: 0
})
result_ocontent := fmt.fmt(file_ast, table, fpref, false) result_ocontent := fmt.fmt(file_ast, table, fpref, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()

View File

@ -47,9 +47,7 @@ fn test_vlib_fmt() {
continue continue
} }
table := ast.new_table() table := ast.new_table()
file_ast := parser.parse_file(ipath, table, .parse_comments, fpref, &ast.Scope{ file_ast := parser.parse_file(ipath, table, .parse_comments, fpref)
parent: 0
})
result_ocontent := fmt.fmt(file_ast, table, fpref, false) result_ocontent := fmt.fmt(file_ast, table, fpref, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()

View File

@ -8,9 +8,7 @@ fn test_macho() {
mut g := native.Gen{ mut g := native.Gen{
pref: &pref.Preferences{} pref: &pref.Preferences{}
out_name: 'test.bin' out_name: 'test.bin'
table: &ast.Table{ table: ast.new_table()
cur_fn: 0
}
} }
g.generate_macho_header() g.generate_macho_header()
g.generate_macho_footer() g.generate_macho_footer()

View File

@ -188,7 +188,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall {
} }
mut scope := &ast.Scope{ mut scope := &ast.Scope{
start_pos: 0 start_pos: 0
parent: p.global_scope parent: p.table.global_scope
} }
$if trace_comptime ? { $if trace_comptime ? {
println('') println('')
@ -197,7 +197,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall {
println('>>> end of template END') println('>>> end of template END')
println('') 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 file.path = tmpl_path
// copy vars from current fn scope into vweb_tmpl scope // copy vars from current fn scope into vweb_tmpl scope
for stmt in file.stmts { for stmt in file.stmts {

View File

@ -22,7 +22,7 @@ pub fn (mut p Parser) parse_array_type() ast.Type {
} }
ast.Ident { ast.Ident {
mut show_non_const_error := false 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 { if const_field.expr is ast.IntegerLiteral {
fixed_size = const_field.expr.val.int() fixed_size = const_field.expr.val.int()
} else { } else {

View File

@ -50,7 +50,6 @@ mut:
attrs []ast.Attr // attributes before next decl stmt attrs []ast.Attr // attributes before next decl stmt
expr_mod string // for constructing full type names in parse_type() expr_mod string // for constructing full type names in parse_type()
scope &ast.Scope scope &ast.Scope
global_scope &ast.Scope
imports map[string]string // alias => mod_name imports map[string]string // alias => mod_name
ast_imports []ast.Import // mod_names ast_imports []ast.Import // mod_names
used_imports []string // alias used_imports []string // alias
@ -90,10 +89,6 @@ pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt {
table: table table: table
pref: &pref.Preferences{} pref: &pref.Preferences{}
scope: scope scope: scope
global_scope: &ast.Scope{
start_pos: 0
parent: 0
}
} }
p.init_parse_fns() p.init_parse_fns()
util.timing_start('PARSE stmt') 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) 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{ mut p := Parser{
scanner: scanner.new_scanner(text, .skip_comments, pref) scanner: scanner.new_scanner(text, .skip_comments, pref)
table: table table: table
@ -112,12 +107,11 @@ pub fn parse_comptime(text string, table &ast.Table, pref &pref.Preferences, sco
scope: scope scope: scope
errors: []errors.Error{} errors: []errors.Error{}
warnings: []errors.Warning{} warnings: []errors.Warning{}
global_scope: global_scope
} }
return p.parse() 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{ mut p := Parser{
scanner: scanner.new_scanner(text, comments_mode, pref) scanner: scanner.new_scanner(text, comments_mode, pref)
comments_mode: comments_mode comments_mode: comments_mode
@ -125,11 +119,10 @@ pub fn parse_text(text string, path string, table &ast.Table, comments_mode scan
pref: pref pref: pref
scope: &ast.Scope{ scope: &ast.Scope{
start_pos: 0 start_pos: 0
parent: global_scope parent: table.global_scope
} }
errors: []errors.Error{} errors: []errors.Error{}
warnings: []errors.Warning{} warnings: []errors.Warning{}
global_scope: global_scope
} }
p.set_path(path) p.set_path(path)
return p.parse() 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, // NB: when comments_mode == .toplevel_comments,
// the parser gives feedback to the scanner about toplevel statements, so that the scanner can skip // 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 // 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 pref: pref
scope: &ast.Scope{ scope: &ast.Scope{
start_pos: 0 start_pos: 0
parent: global_scope parent: table.global_scope
} }
errors: []errors.Error{} errors: []errors.Error{}
warnings: []errors.Warning{} warnings: []errors.Warning{}
global_scope: global_scope
} }
p.set_path(path) p.set_path(path)
return p.parse() return p.parse()
@ -213,7 +205,6 @@ pub fn parse_vet_file(path string, table_ &ast.Table, pref &pref.Preferences) (&
} }
errors: []errors.Error{} errors: []errors.Error{}
warnings: []errors.Warning{} warnings: []errors.Warning{}
global_scope: global_scope
} }
p.set_path(path) p.set_path(path)
if p.scanner.text.contains_any_substr(['\n ', ' \n']) { 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 auto_imports: p.auto_imports
stmts: stmts stmts: stmts
scope: p.scope scope: p.scope
global_scope: p.global_scope global_scope: p.table.global_scope
errors: p.errors errors: p.errors
warnings: p.warnings warnings: p.warnings
global_labels: p.global_labels 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) mut timers := util.new_timers(false)
$if time_parsing ? { $if time_parsing ? {
timers.should_print = true 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{} mut files := []&ast.File{}
for path in paths { for path in paths {
timers.start('parse_file $path') 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') timers.show('parse_file $path')
} }
return files return files
@ -2873,7 +2864,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
comments: comments comments: comments
} }
fields << field fields << field
p.global_scope.register(field) p.table.global_scope.register(field)
comments = [] comments = []
if !is_block { if !is_block {
break break
@ -2975,7 +2966,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
comments: comments comments: comments
} }
fields << field fields << field
p.global_scope.register(field) p.table.global_scope.register(field)
comments = [] comments = []
if !is_block { if !is_block {
break break

View File

@ -74,14 +74,9 @@ x := 10
5+7 5+7
8+4 8+4
' '
table := &ast.Table{ table := ast.new_table()
cur_fn: 0
}
vpref := &pref.Preferences{} vpref := &pref.Preferences{}
gscope := &ast.Scope{ prog := parse_file(s, table, .skip_comments, vpref)
parent: 0
}
prog := parse_file(s, table, .skip_comments, vpref, gscope)
mut checker := checker.new_checker(table, vpref) mut checker := checker.new_checker(table, vpref)
checker.check(prog) checker.check(prog)
res := c.gen([prog], table, vpref) res := c.gen([prog], table, vpref)