Source file
src/go/types/methodlist.go
1
2
3
4
5 package types
6
7 import "sync"
8
9
10
11 type methodList struct {
12 methods []*Func
13
14
15
16
17 guards *[]sync.Once
18 }
19
20
21 func newMethodList(methods []*Func) *methodList {
22 return &methodList{methods: methods}
23 }
24
25
26
27 func newLazyMethodList(length int) *methodList {
28 guards := make([]sync.Once, length)
29 return &methodList{
30 methods: make([]*Func, length),
31 guards: &guards,
32 }
33 }
34
35
36 func (l *methodList) isLazy() bool {
37 return l != nil && l.guards != nil
38 }
39
40
41
42 func (l *methodList) Add(m *Func) {
43 assert(!l.isLazy())
44 if i, _ := lookupMethod(l.methods, m.pkg, m.name, false); i < 0 {
45 l.methods = append(l.methods, m)
46 }
47 }
48
49
50
51
52 func (l *methodList) Lookup(pkg *Package, name string, foldCase bool) (int, *Func) {
53 assert(!l.isLazy())
54 if l == nil {
55 return -1, nil
56 }
57 return lookupMethod(l.methods, pkg, name, foldCase)
58 }
59
60
61 func (l *methodList) Len() int {
62 if l == nil {
63 return 0
64 }
65 return len(l.methods)
66 }
67
68
69
70 func (l *methodList) At(i int, resolve func() *Func) *Func {
71 if !l.isLazy() {
72 return l.methods[i]
73 }
74 assert(resolve != nil)
75 (*l.guards)[i].Do(func() {
76 l.methods[i] = resolve()
77 })
78 return l.methods[i]
79 }
80
View as plain text