@@ -46,7 +46,7 @@ fn (s &Scope) dont_lookup_parent() bool {
4646}
4747
4848pub 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
6479pub 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
8499pub 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
94110pub 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
104121pub 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
114132pub 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
282300pub 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
0 commit comments