forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodlist.go
More file actions
79 lines (68 loc) · 2.29 KB
/
methodlist.go
File metadata and controls
79 lines (68 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import "sync"
// methodList holds a list of methods that may be lazily resolved by a provided
// resolution method.
type methodList struct {
methods []*Func
// guards synchronizes the instantiation of lazy methods. For lazy method
// lists, guards is non-nil and of the length passed to newLazyMethodList.
// For non-lazy method lists, guards is nil.
guards *[]sync.Once
}
// newMethodList creates a non-lazy method list holding the given methods.
func newMethodList(methods []*Func) *methodList {
return &methodList{methods: methods}
}
// newLazyMethodList creates a lazy method list of the given length. Methods
// may be resolved lazily for a given index by providing a resolver function.
func newLazyMethodList(length int) *methodList {
guards := make([]sync.Once, length)
return &methodList{
methods: make([]*Func, length),
guards: &guards,
}
}
// isLazy reports whether the receiver is a lazy method list.
func (l *methodList) isLazy() bool {
return l != nil && l.guards != nil
}
// Add appends a method to the method list if not not already present. Add
// panics if the receiver is lazy.
func (l *methodList) Add(m *Func) {
assert(!l.isLazy())
if i, _ := lookupMethod(l.methods, m.pkg, m.name, false); i < 0 {
l.methods = append(l.methods, m)
}
}
// Lookup looks up the method identified by pkg and name in the receiver.
// Lookup panics if the receiver is lazy. If foldCase is true, method names
// are considered equal if they are equal with case folding.
func (l *methodList) Lookup(pkg *Package, name string, foldCase bool) (int, *Func) {
assert(!l.isLazy())
if l == nil {
return -1, nil
}
return lookupMethod(l.methods, pkg, name, foldCase)
}
// Len returns the length of the method list.
func (l *methodList) Len() int {
if l == nil {
return 0
}
return len(l.methods)
}
// At returns the i'th method of the method list. At panics if i is out of
// bounds, or if the receiver is lazy and resolve is nil.
func (l *methodList) At(i int, resolve func() *Func) *Func {
if !l.isLazy() {
return l.methods[i]
}
assert(resolve != nil)
(*l.guards)[i].Do(func() {
l.methods[i] = resolve()
})
return l.methods[i]
}