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 "sort"
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 sort.Strings(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 {
74 return resolve(name, s.elems[name])
75 }
76
77
78
79
80
81
82
83
84
85
86
87 func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object) {
88 for ; s != nil; s = s.parent {
89 if obj := s.Lookup(name); obj != nil && (!pos.IsValid() || cmpPos(obj.scopePos(), pos) <= 0) {
90 return s, obj
91 }
92 }
93 return nil, nil
94 }
95
96
97
98
99
100
101 func (s *Scope) Insert(obj Object) Object {
102 name := obj.Name()
103 if alt := s.Lookup(name); alt != nil {
104 return alt
105 }
106 s.insert(name, obj)
107 if obj.Parent() == nil {
108 obj.setParent(s)
109 }
110 return nil
111 }
112
113
114
115
116
117
118
119
120 func (s *Scope) _InsertLazy(name string, resolve func() Object) bool {
121 if s.elems[name] != nil {
122 return false
123 }
124 s.insert(name, &lazyObject{parent: s, resolve: resolve})
125 return true
126 }
127
128 func (s *Scope) insert(name string, obj Object) {
129 if s.elems == nil {
130 s.elems = make(map[string]Object)
131 }
132 s.elems[name] = obj
133 }
134
135
136
137
138
139
140
141 func (s *Scope) squash(err func(obj, alt Object)) {
142 p := s.parent
143 assert(p != nil)
144 for name, obj := range s.elems {
145 obj = resolve(name, obj)
146 obj.setParent(nil)
147 if alt := p.Insert(obj); alt != nil {
148 err(obj, alt)
149 }
150 }
151
152 j := -1
153 for i, ch := range p.children {
154 if ch == s {
155 j = i
156 break
157 }
158 }
159 assert(j >= 0)
160 k := len(p.children) - 1
161 p.children[j] = p.children[k]
162 p.children = p.children[:k]
163
164 p.children = append(p.children, s.children...)
165
166 s.children = nil
167 s.elems = nil
168 }
169
170
171
172
173
174 func (s *Scope) Pos() token.Pos { return s.pos }
175 func (s *Scope) End() token.Pos { return s.end }
176
177
178
179
180 func (s *Scope) Contains(pos token.Pos) bool {
181 return cmpPos(s.pos, pos) <= 0 && cmpPos(pos, s.end) < 0
182 }
183
184
185
186
187
188
189 func (s *Scope) Innermost(pos token.Pos) *Scope {
190
191
192 if s.parent == Universe {
193 for _, s := range s.children {
194 if inner := s.Innermost(pos); inner != nil {
195 return inner
196 }
197 }
198 }
199
200 if s.Contains(pos) {
201 for _, s := range s.children {
202 if s.Contains(pos) {
203 return s.Innermost(pos)
204 }
205 }
206 return s
207 }
208 return nil
209 }
210
211
212
213
214
215
216 func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
217 const ind = ". "
218 indn := strings.Repeat(ind, n)
219
220 fmt.Fprintf(w, "%s%s scope %p {\n", indn, s.comment, s)
221
222 indn1 := indn + ind
223 for _, name := range s.Names() {
224 fmt.Fprintf(w, "%s%s\n", indn1, s.Lookup(name))
225 }
226
227 if recurse {
228 for _, s := range s.children {
229 s.WriteTo(w, n+1, recurse)
230 }
231 }
232
233 fmt.Fprintf(w, "%s}\n", indn)
234 }
235
236
237 func (s *Scope) String() string {
238 var buf strings.Builder
239 s.WriteTo(&buf, 0, false)
240 return buf.String()
241 }
242
243
244
245 type lazyObject struct {
246 parent *Scope
247 resolve func() Object
248 obj Object
249 once sync.Once
250 }
251
252
253
254 func resolve(name string, obj Object) Object {
255 if lazy, ok := obj.(*lazyObject); ok {
256 lazy.once.Do(func() {
257 obj := lazy.resolve()
258
259 if _, ok := obj.(*lazyObject); ok {
260 panic("recursive lazy object")
261 }
262 if obj.Name() != name {
263 panic("lazy object has unexpected name")
264 }
265
266 if obj.Parent() == nil {
267 obj.setParent(lazy.parent)
268 }
269 lazy.obj = obj
270 })
271
272 obj = lazy.obj
273 }
274 return obj
275 }
276
277
278
279 func (*lazyObject) Parent() *Scope { panic("unreachable") }
280 func (*lazyObject) Pos() token.Pos { panic("unreachable") }
281 func (*lazyObject) Pkg() *Package { panic("unreachable") }
282 func (*lazyObject) Name() string { panic("unreachable") }
283 func (*lazyObject) Type() Type { panic("unreachable") }
284 func (*lazyObject) Exported() bool { panic("unreachable") }
285 func (*lazyObject) Id() string { panic("unreachable") }
286 func (*lazyObject) String() string { panic("unreachable") }
287 func (*lazyObject) order() uint32 { panic("unreachable") }
288 func (*lazyObject) color() color { panic("unreachable") }
289 func (*lazyObject) setType(Type) { panic("unreachable") }
290 func (*lazyObject) setOrder(uint32) { panic("unreachable") }
291 func (*lazyObject) setColor(color color) { panic("unreachable") }
292 func (*lazyObject) setParent(*Scope) { panic("unreachable") }
293 func (*lazyObject) sameId(*Package, string, bool) bool { panic("unreachable") }
294 func (*lazyObject) scopePos() token.Pos { panic("unreachable") }
295 func (*lazyObject) setScopePos(token.Pos) { panic("unreachable") }
296
View as plain text