Skip to content

Commit 4cace8c

Browse files
committed
v2: improve and enable the parallel checker
1 parent 10240b5 commit 4cace8c

6 files changed

Lines changed: 215 additions & 74 deletions

File tree

‎vlib/v2/builder/builder.v‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ import v2.ssa
1414
import v2.ssa.optimize
1515
import v2.token
1616
import v2.transform
17+
import v2.types
1718
import time
1819

1920
struct Builder {
2021
pref &pref.Preferences
2122
mut:
2223
files []ast.File
2324
user_files []string // original user-provided files (for output name)
24-
file_set &token.FileSet = token.FileSet.new()
25+
file_set &token.FileSet = token.FileSet.new()
26+
env &types.Environment = unsafe { nil } // Type checker environment
2527
}
2628

2729
pub fn new_builder(prefs &pref.Preferences) &Builder {
@@ -42,7 +44,13 @@ pub fn (mut b Builder) build(files []string) {
4244
}
4345
parse_time := sw.elapsed()
4446

45-
// b.type_check_files()
47+
b.env = if b.pref.skip_type_check {
48+
types.Environment.new()
49+
} else if b.pref.no_parallel {
50+
b.type_check_files()
51+
} else {
52+
b.type_check_files_parallel()
53+
}
4654
type_check_time := time.Duration(sw.elapsed() - parse_time)
4755

4856
// Generate output based on backend
@@ -153,7 +161,7 @@ fn (mut b Builder) gen_native(backend_arch pref.Arch) {
153161

154162
// Build all files into a single SSA module
155163
mut mod := ssa.Module.new('main')
156-
mut ssa_builder := ssa.Builder.new(mod)
164+
mut ssa_builder := ssa.Builder.new_with_env(mod, b.env)
157165
mut t := transform.Transformer.new()
158166

159167
// Transform all files first

‎vlib/v2/builder/type_check.v‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
module builder
22

3-
import v2.ast
43
import v2.types
5-
// import v2.util
6-
// import runtime
74

8-
fn type_check_worker(ch_in chan []ast.File, ch_out chan string) {
9-
for {
10-
// ast_files := <- ch_in or { break }
11-
}
12-
}
13-
14-
fn (mut b Builder) type_check_files() {
15-
// mod := types.new_module('main', '')
5+
fn (mut b Builder) type_check_files() &types.Environment {
166
env := types.Environment.new()
177
mut checker := types.Checker.new(b.pref, b.file_set, env)
188
checker.check_files(b.files)
19-
20-
// mut ch_in := chan []ast.File{cap: 1000}
21-
// mut ch_out := chan string{cap: 1000}
22-
// mut worker_pool := util.WorkerPool.new[[]ast.File, string](mut ch_in, mut ch_out)
23-
// for _ in 0..runtime.nr_jobs() {
24-
// worker_pool.workers << spawn type_check_worker(ch_in, ch_out)
25-
// }
9+
return env
2610
}

‎vlib/v2/parser/parser.v‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,9 @@ fn (mut p Parser) expr(min_bp token.BindingPower) ast.Expr {
11541154
pos: pos
11551155
}
11561156
}
1157-
// range
1158-
else if p.tok in [.dotdot, .ellipsis] {
1157+
// range - only create RangeExpr at lowest precedence to avoid
1158+
// consuming `..` inside higher-precedence expressions like `a + b..`
1159+
else if min_bp == .lowest && p.tok in [.dotdot, .ellipsis] {
11591160
// p.log('ast.RangeExpr')
11601161
// no need to continue
11611162
return ast.RangeExpr{

‎vlib/v2/pref/pref.v‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ pub enum Arch {
2121

2222
pub struct Preferences {
2323
pub mut:
24-
debug bool
25-
verbose bool
26-
skip_genv bool
27-
skip_builtin bool
28-
skip_imports bool
29-
no_parallel bool = true // default to sequential parsing until parallel is fixed
30-
backend Backend
31-
arch Arch = .auto
32-
output_file string
24+
debug bool
25+
verbose bool
26+
skip_genv bool
27+
skip_builtin bool
28+
skip_imports bool
29+
skip_type_check bool // Skip type checking phase (for backends that don't need it yet)
30+
no_parallel bool = true // default to sequential parsing until parallel is fixed
31+
backend Backend
32+
arch Arch = .auto
33+
output_file string
3334
pub:
3435
vroot string = os.dir(@VEXE)
3536
vmodules_path string = os.vmodules_dir()
@@ -69,15 +70,16 @@ pub fn new_preferences_from_args(args []string) Preferences {
6970
// Default to sequential parsing (no_parallel=true) unless --parallel is specified
7071
use_parallel := '--parallel' in options
7172
return Preferences{
72-
debug: '--debug' in options || '-d' in options
73-
verbose: '--verbose' in options || '-v' in options
74-
skip_genv: '--skip-genv' in options
75-
skip_builtin: '--skip-builtin' in options
76-
skip_imports: '--skip-imports' in options
77-
no_parallel: !use_parallel
78-
backend: backend
79-
arch: arch
80-
output_file: output_file
73+
debug: '--debug' in options || '-d' in options
74+
verbose: '--verbose' in options || '-v' in options
75+
skip_genv: '--skip-genv' in options
76+
skip_builtin: '--skip-builtin' in options
77+
skip_imports: '--skip-imports' in options
78+
skip_type_check: '--skip-type-check' in options
79+
no_parallel: !use_parallel
80+
backend: backend
81+
arch: arch
82+
output_file: output_file
8183
}
8284
}
8385

@@ -105,14 +107,15 @@ pub fn new_preferences_using_options(options []string) Preferences {
105107
use_parallel := '--parallel' in options
106108
return Preferences{
107109
// config flags
108-
debug: '--debug' in options || '-d' in options
109-
verbose: '--verbose' in options || '-v' in options
110-
skip_genv: '--skip-genv' in options
111-
skip_builtin: '--skip-builtin' in options
112-
skip_imports: '--skip-imports' in options
113-
no_parallel: !use_parallel
114-
backend: backend
115-
arch: arch
110+
debug: '--debug' in options || '-d' in options
111+
verbose: '--verbose' in options || '-v' in options
112+
skip_genv: '--skip-genv' in options
113+
skip_builtin: '--skip-builtin' in options
114+
skip_imports: '--skip-imports' in options
115+
skip_type_check: '--skip-type-check' in options
116+
no_parallel: !use_parallel
117+
backend: backend
118+
arch: arch
116119
}
117120
}
118121

‎vlib/v2/ssa/builder.v‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ mut:
1414
cur_func int = -1
1515
cur_block BlockID = -1
1616

17+
// Type checker environment with populated scopes
18+
env &types.Environment = unsafe { nil }
19+
1720
// Maps AST variable name to SSA ValueID (pointer to stack slot)
1821
vars map[string]ValueID
1922

@@ -68,6 +71,10 @@ struct LoopInfo {
6871
}
6972

7073
pub fn Builder.new(mod &Module) &Builder {
74+
return Builder.new_with_env(mod, unsafe { nil })
75+
}
76+
77+
pub fn Builder.new_with_env(mod &Module, env &types.Environment) &Builder {
7178
mut b := &Builder{
7279
mod: mod
7380
vars: map[string]ValueID{}
@@ -88,9 +95,48 @@ pub fn Builder.new(mod &Module) &Builder {
8895
type_alias_bases: map[string]string{}
8996
enum_names: map[string]bool{}
9097
}
98+
unsafe {
99+
b.env = env
100+
}
91101
return b
92102
}
93103

104+
// lookup_type_in_scope looks up a type by name in the environment's scopes.
105+
// First tries module scope, then builtin scope.
106+
// Returns none if not found or if env is nil.
107+
fn (b &Builder) lookup_type_in_scope(name string, module_name string) ?types.Type {
108+
if b.env == unsafe { nil } {
109+
return none
110+
}
111+
// Try module scope first
112+
mut scope := lock b.env.scopes {
113+
b.env.scopes[module_name] or {
114+
// Try builtin scope as fallback
115+
b.env.scopes['builtin'] or { return none }
116+
}
117+
}
118+
if obj := scope.lookup_parent(name, 0) {
119+
if obj is types.Type {
120+
return obj
121+
}
122+
}
123+
return none
124+
}
125+
126+
// lookup_struct_type looks up a struct type by name from the environment.
127+
// Falls back to the local struct_types map if not found in environment.
128+
fn (mut b Builder) lookup_struct_type(name string) ?types.Struct {
129+
// Try environment first
130+
if typ := b.lookup_type_in_scope(name, 'builtin') {
131+
if typ is types.Struct {
132+
return typ
133+
}
134+
}
135+
// Fallback: check if we have it in struct_types but can't get full info
136+
// This path is used when type checker is skipped
137+
return none
138+
}
139+
94140
// Convert v2.types.Type to SSA TypeID
95141
fn (mut b Builder) type_to_ssa(t types.Type) TypeID {
96142
match t {

0 commit comments

Comments
 (0)