Skip to content

Commit 4b4c706

Browse files
authored
v.ast: implement Scope.find_ptr/1; use it internally instead of Scope.find/1 (#25386)
1 parent 7101472 commit 4b4c706

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

‎vlib/v/ast/scope.v‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn (s &Scope) dont_lookup_parent() bool {
4646
}
4747

4848
pub fn (s &Scope) find(name string) ?ScopeObject {
49-
if s == unsafe { nil } {
49+
if _unlikely_(s == unsafe { nil }) {
5050
return none
5151
}
5252
for sc := unsafe { s }; true; sc = sc.parent {
@@ -60,9 +60,24 @@ pub fn (s &Scope) find(name string) ?ScopeObject {
6060
return none
6161
}
6262

63+
pub fn (s &Scope) find_ptr(name string) &ScopeObject {
64+
if _unlikely_(s == unsafe { nil }) {
65+
return 0
66+
}
67+
for sc := unsafe { s }; true; sc = sc.parent {
68+
if name in sc.objects {
69+
return unsafe { &sc.objects[name] }
70+
}
71+
if sc.dont_lookup_parent() {
72+
break
73+
}
74+
}
75+
return 0
76+
}
77+
6378
// selector_expr: name.field_name
6479
pub fn (s &Scope) find_struct_field(name string, struct_type Type, field_name string) &ScopeStructField {
65-
if s == unsafe { nil } {
80+
if _unlikely_(s == unsafe { nil }) {
6681
return unsafe { nil }
6782
}
6883
k := '${name}.${field_name}'
@@ -82,7 +97,8 @@ pub fn (s &Scope) find_struct_field(name string, struct_type Type, field_name st
8297
}
8398

8499
pub fn (s &Scope) find_var(name string) ?&Var {
85-
if obj := s.find(name) {
100+
obj := s.find_ptr(name)
101+
if _likely_(obj != unsafe { nil }) {
86102
match obj {
87103
Var { return &obj }
88104
else {}
@@ -92,7 +108,8 @@ pub fn (s &Scope) find_var(name string) ?&Var {
92108
}
93109

94110
pub fn (s &Scope) find_global(name string) ?&GlobalField {
95-
if obj := s.find(name) {
111+
obj := s.find_ptr(name)
112+
if _likely_(obj != unsafe { nil }) {
96113
match obj {
97114
GlobalField { return &obj }
98115
else {}
@@ -102,7 +119,8 @@ pub fn (s &Scope) find_global(name string) ?&GlobalField {
102119
}
103120

104121
pub fn (s &Scope) find_const(name string) ?&ConstField {
105-
if obj := s.find(name) {
122+
obj := s.find_ptr(name)
123+
if _likely_(obj != unsafe { nil }) {
106124
match obj {
107125
ConstField { return &obj }
108126
else {}
@@ -112,7 +130,7 @@ pub fn (s &Scope) find_const(name string) ?&ConstField {
112130
}
113131

114132
pub fn (s &Scope) known_var(name string) bool {
115-
if s == unsafe { nil } {
133+
if _unlikely_(s == unsafe { nil }) {
116134
return false
117135
}
118136
for sc := unsafe { s }; true; sc = sc.parent {
@@ -280,7 +298,10 @@ pub fn (sc &Scope) show(depth int, max_depth int) string {
280298
}
281299

282300
pub fn (mut sc Scope) mark_var_as_used(varname string) bool {
283-
mut obj := sc.find(varname) or { return false }
301+
mut obj := sc.find_ptr(varname)
302+
if obj == unsafe { nil } {
303+
return false
304+
}
284305
if mut obj is Var {
285306
obj.is_used = true
286307
return true

‎vlib/v/checker/checker.v‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4212,7 +4212,9 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
42124212
c.error('`mut` is not allowed with `=` (use `:=` to declare a variable)',
42134213
node.pos)
42144214
}
4215-
if mut obj := node.scope.find(node.name) {
4215+
mut pobj := node.scope.find_ptr(node.name)
4216+
if pobj != unsafe { nil } {
4217+
mut obj := *pobj
42164218
match mut obj {
42174219
ast.GlobalField {
42184220
if node.mod == '' {
@@ -4322,7 +4324,9 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
43224324
else if !name.contains('.') && node.mod != 'builtin' {
43234325
name = '${node.mod}.${node.name}'
43244326
}
4325-
if mut obj := c.file.global_scope.find(name) {
4327+
pobj = c.file.global_scope.find_ptr(name)
4328+
if pobj != unsafe { nil } {
4329+
mut obj := *pobj
43264330
match mut obj {
43274331
ast.GlobalField {
43284332
node.kind = .global

0 commit comments

Comments
 (0)