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 { return resolve(name, s.elems[name]) }
71
72
73
74
75 func (s *Scope) lookupIgnoringCase(name string, exported bool) []Object {
76 var matches []Object
77 for _, n := range s.Names() {
78 if (!exported || isExported(n)) && strings.EqualFold(n, name) {
79 matches = append(matches, s.Lookup(n))
80 }
81 }
82 return matches
83 }
84
85
86
87
88
89
90 func (s *Scope) Insert(obj Object) Object {
91 name := obj.Name()
92 if alt := s.Lookup(name); alt != nil {
93 return alt
94 }
95 s.insert(name, obj)
96
97
98
99
100
101 if obj.Parent() == nil {
102 obj.setParent(s)
103 }
104 return nil
105 }
106
107
108
109
110
111
112
113
114 func (s *Scope) InsertLazy(name string, resolve func() Object) bool {
115 if s.elems[name] != nil {
116 return false
117 }
118 s.insert(name, &lazyObject{parent: s, resolve: resolve})
119 return true
120 }
121
122 func (s *Scope) insert(name string, obj Object) {
123 if s.elems == nil {
124 s.elems = make(map[string]Object)
125 }
126 s.elems[name] = obj
127 }
128
129
130
131
132
133
134 func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
135 const ind = ". "
136 indn := strings.Repeat(ind, n)
137
138 fmt.Fprintf(w, "%s%s scope %p {\n", indn, s.comment, s)
139
140 indn1 := indn + ind
141 for _, name := range s.Names() {
142 fmt.Fprintf(w, "%s%s\n", indn1, s.Lookup(name))
143 }
144
145 if recurse {
146 for _, s := range s.children {
147 s.WriteTo(w, n+1, recurse)
148 }
149 }
150
151 fmt.Fprintf(w, "%s}\n", indn)
152 }
153
154
155 func (s *Scope) String() string {
156 var buf strings.Builder
157 s.WriteTo(&buf, 0, false)
158 return buf.String()
159 }
160
161
162
163 type lazyObject struct {
164 parent *Scope
165 resolve func() Object
166 obj Object
167 once sync.Once
168 }
169
170
171
172 func resolve(name string, obj Object) Object {
173 if lazy, ok := obj.(*lazyObject); ok {
174 lazy.once.Do(func() {
175 obj := lazy.resolve()
176
177 if _, ok := obj.(*lazyObject); ok {
178 panic("recursive lazy object")
179 }
180 if obj.Name() != name {
181 panic("lazy object has unexpected name")
182 }
183
184 if obj.Parent() == nil {
185 obj.setParent(lazy.parent)
186 }
187 lazy.obj = obj
188 })
189
190 obj = lazy.obj
191 }
192 return obj
193 }
194
195
196
197 func (*lazyObject) Parent() *Scope { panic("unreachable") }
198 func (*lazyObject) Pos() syntax.Pos { panic("unreachable") }
199 func (*lazyObject) Pkg() *Package { panic("unreachable") }
200 func (*lazyObject) Name() string { panic("unreachable") }
201 func (*lazyObject) Type() Type { panic("unreachable") }
202 func (*lazyObject) Exported() bool { panic("unreachable") }
203 func (*lazyObject) Id() string { panic("unreachable") }
204 func (*lazyObject) String() string { panic("unreachable") }
205 func (*lazyObject) order() uint32 { panic("unreachable") }
206 func (*lazyObject) setType(Type) { panic("unreachable") }
207 func (*lazyObject) setOrder(uint32) { panic("unreachable") }
208 func (*lazyObject) setParent(*Scope) { panic("unreachable") }
209 func (*lazyObject) sameId(*Package, string, bool) bool { panic("unreachable") }
210 func (*lazyObject) scopePos() syntax.Pos { panic("unreachable") }
211 func (*lazyObject) setScopePos(syntax.Pos) { panic("unreachable") }
212
View as plain text