Source file
src/go/types/scope.go
1
2
3
4
5
6
7
8
9
10 package types
11
12 import (
13 "fmt"
14 "go/token"
15 "io"
16 "slices"
17 "strings"
18 "sync"
19 )
20
21
22
23
24
25 type Scope struct {
26 parent *Scope
27 children []*Scope
28 number int
29 elems map[string]Object
30 pos, end token.Pos
31 comment string
32 isFunc bool
33 }
34
35
36
37 func NewScope(parent *Scope, pos, end token.Pos, comment string) *Scope {
38 s := &Scope{parent, nil, 0, nil, pos, end, comment, false}
39
40 if parent != nil && parent != Universe {
41 parent.children = append(parent.children, s)
42 s.number = len(parent.children)
43 }
44 return s
45 }
46
47
48 func (s *Scope) Parent() *Scope { return s.parent }
49
50
51 func (s *Scope) Len() int { return len(s.elems) }
52
53
54 func (s *Scope) Names() []string {
55 names := make([]string, len(s.elems))
56 i := 0
57 for name := range s.elems {
58 names[i] = name
59 i++
60 }
61 slices.Sort(names)
62 return names
63 }
64
65
66 func (s *Scope) NumChildren() int { return len(s.children) }
67
68
69 func (s *Scope) Child(i int) *Scope { return s.children[i] }
70
71
72
73 func (s *Scope) Lookup(name string) Object { return resolve(name, s.elems[name]) }
74
75
76
77
78 func (s *Scope) lookupIgnoringCase(name string, exported bool) []Object {
79 var matches []Object
80 for _, n := range s.Names() {
81 if (!exported || isExported(n)) && strings.EqualFold(n, name) {
82 matches = append(matches, s.Lookup(n))
83 }
84 }
85 return matches
86 }
87
88
89
90
91
92
93 func (s *Scope) Insert(obj Object) Object {
94 name := obj.Name()
95 if alt := s.Lookup(name); alt != nil {
96 return alt
97 }
98 s.insert(name, obj)
99
100
101
102
103
104 if obj.Parent() == nil {
105 obj.setParent(s)
106 }
107 return nil
108 }
109
110
111
112
113
114
115
116
117 func (s *Scope) _InsertLazy(name string, resolve func() Object) bool {
118 if s.elems[name] != nil {
119 return false
120 }
121 s.insert(name, &lazyObject{parent: s, resolve: resolve})
122 return true
123 }
124
125 func (s *Scope) insert(name string, obj Object) {
126 if s.elems == nil {
127 s.elems = make(map[string]Object)
128 }
129 s.elems[name] = obj
130 }
131
132
133
134
135
136
137 func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
138 const ind = ". "
139 indn := strings.Repeat(ind, n)
140
141 fmt.Fprintf(w, "%s%s scope %p {\n", indn, s.comment, s)
142
143 indn1 := indn + ind
144 for _, name := range s.Names() {
145 fmt.Fprintf(w, "%s%s\n", indn1, s.Lookup(name))
146 }
147
148 if recurse {
149 for _, s := range s.children {
150 s.WriteTo(w, n+1, recurse)
151 }
152 }
153
154 fmt.Fprintf(w, "%s}\n", indn)
155 }
156
157
158 func (s *Scope) String() string {
159 var buf strings.Builder
160 s.WriteTo(&buf, 0, false)
161 return buf.String()
162 }
163
164
165
166 type lazyObject struct {
167 parent *Scope
168 resolve func() Object
169 obj Object
170 once sync.Once
171 }
172
173
174
175 func resolve(name string, obj Object) Object {
176 if lazy, ok := obj.(*lazyObject); ok {
177 lazy.once.Do(func() {
178 obj := lazy.resolve()
179
180 if _, ok := obj.(*lazyObject); ok {
181 panic("recursive lazy object")
182 }
183 if obj.Name() != name {
184 panic("lazy object has unexpected name")
185 }
186
187 if obj.Parent() == nil {
188 obj.setParent(lazy.parent)
189 }
190 lazy.obj = obj
191 })
192
193 obj = lazy.obj
194 }
195 return obj
196 }
197
198
199
200 func (*lazyObject) Parent() *Scope { panic("unreachable") }
201 func (*lazyObject) Pos() token.Pos { panic("unreachable") }
202 func (*lazyObject) Pkg() *Package { panic("unreachable") }
203 func (*lazyObject) Name() string { panic("unreachable") }
204 func (*lazyObject) Type() Type { panic("unreachable") }
205 func (*lazyObject) Exported() bool { panic("unreachable") }
206 func (*lazyObject) Id() string { panic("unreachable") }
207 func (*lazyObject) String() string { panic("unreachable") }
208 func (*lazyObject) order() uint32 { panic("unreachable") }
209 func (*lazyObject) setType(Type) { panic("unreachable") }
210 func (*lazyObject) setOrder(uint32) { panic("unreachable") }
211 func (*lazyObject) setParent(*Scope) { panic("unreachable") }
212 func (*lazyObject) sameId(*Package, string, bool) bool { panic("unreachable") }
213 func (*lazyObject) scopePos() token.Pos { panic("unreachable") }
214 func (*lazyObject) setScopePos(token.Pos) { panic("unreachable") }
215
View as plain text