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